1/* $FreeBSD: head/sys/netinet6/frag6.c 171260 2007-07-05 16:29:40Z delphij $ */
2/* $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $ */
3
4/*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/malloc.h>
36#include <sys/mbuf.h>
37#include <sys/domain.h>
38#include <sys/protosw.h>
39#include <sys/socket.h>
40#include <sys/errno.h>
41#include <sys/time.h>
42#include <sys/kernel.h>
43#include <sys/syslog.h>
44
45#include <net/if.h>
46#include <net/route.h>
47
48#include <netinet/in.h>
49#include <netinet/in_var.h>
50#include <netinet/ip6.h>
51#include <netinet6/ip6_var.h>
52#include <netinet/icmp6.h>
53#include <netinet/in_systm.h> /* for ECN definitions */
54#include <netinet/ip.h> /* for ECN definitions */
55
56/*
57 * Define it to get a correct behavior on per-interface statistics.
58 * You will need to perform an extra routing table lookup, per fragment,
59 * to do it. This may, or may not be, a performance hit.
60 */
61#define IN6_IFSTAT_STRICT
62
63static void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
64static void frag6_deq __P((struct ip6asfrag *));
65static void frag6_insque __P((struct ip6q *, struct ip6q *));
66static void frag6_remque __P((struct ip6q *));
67static void frag6_freef __P((struct ip6q *));
68
69static struct mtx ip6qlock;
70/*
71 * These fields all protected by ip6qlock.
72 */
73static u_int frag6_nfragpackets;
74static u_int frag6_nfrags;
75static struct ip6q ip6q; /* ip6 reassemble queue */
76
77#define IP6Q_LOCK_INIT() mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF);
78#define IP6Q_LOCK() mtx_lock(&ip6qlock)
79#define IP6Q_TRYLOCK() mtx_trylock(&ip6qlock)
80#define IP6Q_LOCK_ASSERT() mtx_assert(&ip6qlock, MA_OWNED)
81#define IP6Q_UNLOCK() mtx_unlock(&ip6qlock)
82
83static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
84
85/*
86 * Initialise reassembly queue and fragment identifier.
87 */
88static void
89frag6_change(void *tag)
90{
91
92 ip6_maxfragpackets = nmbclusters / 4;
93 ip6_maxfrags = nmbclusters / 4;
94}
95
96void
97frag6_init(void)
98{
99
100 ip6_maxfragpackets = nmbclusters / 4;
101 ip6_maxfrags = nmbclusters / 4;
102 EVENTHANDLER_REGISTER(nmbclusters_change,
103 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
104
105 IP6Q_LOCK_INIT();
106
107 ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
108}
109
110/*
111 * In RFC2460, fragment and reassembly rule do not agree with each other,
112 * in terms of next header field handling in fragment header.
113 * While the sender will use the same value for all of the fragmented packets,
114 * receiver is suggested not to check the consistency.
115 *
116 * fragment rule (p20):
117 * (2) A Fragment header containing:
118 * The Next Header value that identifies the first header of
119 * the Fragmentable Part of the original packet.
120 * -> next header field is same for all fragments
121 *
122 * reassembly rule (p21):
123 * The Next Header field of the last header of the Unfragmentable
124 * Part is obtained from the Next Header field of the first
125 * fragment's Fragment header.
126 * -> should grab it from the first fragment only
127 *
128 * The following note also contradicts with fragment rule - noone is going to
129 * send different fragment with different next header field.
130 *
131 * additional note (p22):
132 * The Next Header values in the Fragment headers of different
133 * fragments of the same original packet may differ. Only the value
134 * from the Offset zero fragment packet is used for reassembly.
135 * -> should grab it from the first fragment only
136 *
137 * There is no explicit reason given in the RFC. Historical reason maybe?
138 */
139/*
140 * Fragment input
141 */
142int
143frag6_input(struct mbuf **mp, int *offp, int proto)
144{
145 struct mbuf *m = *mp, *t;
146 struct ip6_hdr *ip6;
147 struct ip6_frag *ip6f;
148 struct ip6q *q6;
149 struct ip6asfrag *af6, *ip6af, *af6dwn;
150#ifdef IN6_IFSTAT_STRICT
151 struct in6_ifaddr *ia;
152#endif
153 int offset = *offp, nxt, i, next;
154 int first_frag = 0;
155 int fragoff, frgpartlen; /* must be larger than u_int16_t */
156 struct ifnet *dstifp;
157 u_int8_t ecn, ecn0;
158#if 0
159 char ip6buf[INET6_ADDRSTRLEN];
160#endif
161
162 ip6 = mtod(m, struct ip6_hdr *);
163#ifndef PULLDOWN_TEST
164 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
165 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
166#else
167 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
168 if (ip6f == NULL)
169 return (IPPROTO_DONE);
170#endif
171
172 dstifp = NULL;
173#ifdef IN6_IFSTAT_STRICT
174 /* find the destination interface of the packet. */
175 if ((ia = ip6_getdstifaddr(m)) != NULL)
176 dstifp = ia->ia_ifp;
177#else
178 /* we are violating the spec, this is not the destination interface */
179 if ((m->m_flags & M_PKTHDR) != 0)
180 dstifp = m->m_pkthdr.rcvif;
181#endif
182
183 /* jumbo payload can't contain a fragment header */
184 if (ip6->ip6_plen == 0) {
185 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
186 in6_ifstat_inc(dstifp, ifs6_reass_fail);
187 return IPPROTO_DONE;
188 }
189
190 /*
191 * check whether fragment packet's fragment length is
192 * multiple of 8 octets.
193 * sizeof(struct ip6_frag) == 8
194 * sizeof(struct ip6_hdr) = 40
195 */
196 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
197 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
198 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
199 offsetof(struct ip6_hdr, ip6_plen));
200 in6_ifstat_inc(dstifp, ifs6_reass_fail);
201 return IPPROTO_DONE;
202 }
203
204 ip6stat.ip6s_fragments++;
205 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
206
207 /* offset now points to data portion */
208 offset += sizeof(struct ip6_frag);
209
210 IP6Q_LOCK();
211
212 /*
213 * Enforce upper bound on number of fragments.
214 * If maxfrag is 0, never accept fragments.
215 * If maxfrag is -1, accept all fragments without limitation.
216 */
217 if (ip6_maxfrags < 0)
218 ;
219 else if (frag6_nfrags >= (u_int)ip6_maxfrags)
220 goto dropfrag;
221
222 for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
223 if (ip6f->ip6f_ident == q6->ip6q_ident &&
224 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
225 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
226 break;
227
228 if (q6 == &ip6q) {
229 /*
230 * the first fragment to arrive, create a reassembly queue.
231 */
232 first_frag = 1;
233
234 /*
235 * Enforce upper bound on number of fragmented packets
236 * for which we attempt reassembly;
237 * If maxfragpackets is 0, never accept fragments.
238 * If maxfragpackets is -1, accept all fragments without
239 * limitation.
240 */
241 if (ip6_maxfragpackets < 0)
242 ;
243 else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
244 goto dropfrag;
245 frag6_nfragpackets++;
246 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
247 M_NOWAIT);
248 if (q6 == NULL)
249 goto dropfrag;
250 bzero(q6, sizeof(*q6));
251
252 frag6_insque(q6, &ip6q);
253
254 /* ip6q_nxt will be filled afterwards, from 1st fragment */
255 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
256#ifdef notyet
257 q6->ip6q_nxtp = (u_char *)nxtp;
258#endif
259 q6->ip6q_ident = ip6f->ip6f_ident;
260 q6->ip6q_ttl = IPV6_FRAGTTL;
261 q6->ip6q_src = ip6->ip6_src;
262 q6->ip6q_dst = ip6->ip6_dst;
263 q6->ip6q_ecn =
264 (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
265 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
266
267 q6->ip6q_nfrag = 0;
268 }
269
270 /*
271 * If it's the 1st fragment, record the length of the
272 * unfragmentable part and the next header of the fragment header.
273 */
274 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
275 if (fragoff == 0) {
276 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
277 sizeof(struct ip6_frag);
278 q6->ip6q_nxt = ip6f->ip6f_nxt;
279 }
280
281 /*
282 * Check that the reassembled packet would not exceed 65535 bytes
283 * in size.
284 * If it would exceed, discard the fragment and return an ICMP error.
285 */
286 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
287 if (q6->ip6q_unfrglen >= 0) {
288 /* The 1st fragment has already arrived. */
289 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
290 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
291 offset - sizeof(struct ip6_frag) +
292 offsetof(struct ip6_frag, ip6f_offlg));
293 IP6Q_UNLOCK();
294 return (IPPROTO_DONE);
295 }
296 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
297 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
298 offset - sizeof(struct ip6_frag) +
299 offsetof(struct ip6_frag, ip6f_offlg));
300 IP6Q_UNLOCK();
301 return (IPPROTO_DONE);
302 }
303 /*
304 * If it's the first fragment, do the above check for each
305 * fragment already stored in the reassembly queue.
306 */
307 if (fragoff == 0) {
308 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
309 af6 = af6dwn) {
310 af6dwn = af6->ip6af_down;
311
312 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
313 IPV6_MAXPACKET) {
314 struct mbuf *merr = IP6_REASS_MBUF(af6);
315 struct ip6_hdr *ip6err;
316 int erroff = af6->ip6af_offset;
317
318 /* dequeue the fragment. */
319 frag6_deq(af6);
320 free(af6, M_FTABLE);
321
322 /* adjust pointer. */
323 ip6err = mtod(merr, struct ip6_hdr *);
324
325 /*
326 * Restore source and destination addresses
327 * in the erroneous IPv6 header.
328 */
329 ip6err->ip6_src = q6->ip6q_src;
330 ip6err->ip6_dst = q6->ip6q_dst;
331
332 icmp6_error(merr, ICMP6_PARAM_PROB,
333 ICMP6_PARAMPROB_HEADER,
334 erroff - sizeof(struct ip6_frag) +
335 offsetof(struct ip6_frag, ip6f_offlg));
336 }
337 }
338 }
339
340 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
341 M_NOWAIT);
342 if (ip6af == NULL)
343 goto dropfrag;
344 bzero(ip6af, sizeof(*ip6af));
345 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
346 ip6af->ip6af_off = fragoff;
347 ip6af->ip6af_frglen = frgpartlen;
348 ip6af->ip6af_offset = offset;
349 IP6_REASS_MBUF(ip6af) = m;
350
351 if (first_frag) {
352 af6 = (struct ip6asfrag *)q6;
353 goto insert;
354 }
355
356 /*
357 * Handle ECN by comparing this segment with the first one;
358 * if CE is set, do not lose CE.
359 * drop if CE and not-ECT are mixed for the same packet.
360 */
361 ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
362 ecn0 = q6->ip6q_ecn;
363 if (ecn == IPTOS_ECN_CE) {
364 if (ecn0 == IPTOS_ECN_NOTECT) {
365 free(ip6af, M_FTABLE);
366 goto dropfrag;
367 }
368 if (ecn0 != IPTOS_ECN_CE)
369 q6->ip6q_ecn = IPTOS_ECN_CE;
370 }
371 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
372 free(ip6af, M_FTABLE);
373 goto dropfrag;
374 }
375
376 /*
377 * Find a segment which begins after this one does.
378 */
379 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
380 af6 = af6->ip6af_down)
381 if (af6->ip6af_off > ip6af->ip6af_off)
382 break;
383
384#if 0
385 /*
386 * If there is a preceding segment, it may provide some of
387 * our data already. If so, drop the data from the incoming
388 * segment. If it provides all of our data, drop us.
389 */
390 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
391 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
392 - ip6af->ip6af_off;
393 if (i > 0) {
394 if (i >= ip6af->ip6af_frglen)
395 goto dropfrag;
396 m_adj(IP6_REASS_MBUF(ip6af), i);
397 ip6af->ip6af_off += i;
398 ip6af->ip6af_frglen -= i;
399 }
400 }
401
402 /*
403 * While we overlap succeeding segments trim them or,
404 * if they are completely covered, dequeue them.
405 */
406 while (af6 != (struct ip6asfrag *)q6 &&
407 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
408 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
409 if (i < af6->ip6af_frglen) {
410 af6->ip6af_frglen -= i;
411 af6->ip6af_off += i;
412 m_adj(IP6_REASS_MBUF(af6), i);
413 break;
414 }
415 af6 = af6->ip6af_down;
416 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
417 frag6_deq(af6->ip6af_up);
418 }
419#else
420 /*
421 * If the incoming framgent overlaps some existing fragments in
422 * the reassembly queue, drop it, since it is dangerous to override
423 * existing fragments from a security point of view.
424 * We don't know which fragment is the bad guy - here we trust
425 * fragment that came in earlier, with no real reason.
426 *
427 * Note: due to changes after disabling this part, mbuf passed to
428 * m_adj() below now does not meet the requirement.
429 */
430 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
431 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
432 - ip6af->ip6af_off;
433 if (i > 0) {
434#if 0 /* suppress the noisy log */
435 log(LOG_ERR, "%d bytes of a fragment from %s "
436 "overlaps the previous fragment\n",
437 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
438#endif
439 free(ip6af, M_FTABLE);
440 goto dropfrag;
441 }
442 }
443 if (af6 != (struct ip6asfrag *)q6) {
444 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
445 if (i > 0) {
446#if 0 /* suppress the noisy log */
447 log(LOG_ERR, "%d bytes of a fragment from %s "
448 "overlaps the succeeding fragment",
449 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
450#endif
451 free(ip6af, M_FTABLE);
452 goto dropfrag;
453 }
454 }
455#endif
456
457insert:
458
459 /*
460 * Stick new segment in its place;
461 * check for complete reassembly.
462 * Move to front of packet queue, as we are
463 * the most recently active fragmented packet.
464 */
465 frag6_enq(ip6af, af6->ip6af_up);
466 frag6_nfrags++;
467 q6->ip6q_nfrag++;
468#if 0 /* xxx */
469 if (q6 != ip6q.ip6q_next) {
470 frag6_remque(q6);
471 frag6_insque(q6, &ip6q);
472 }
473#endif
474 next = 0;
475 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
476 af6 = af6->ip6af_down) {
477 if (af6->ip6af_off != next) {
478 IP6Q_UNLOCK();
479 return IPPROTO_DONE;
480 }
481 next += af6->ip6af_frglen;
482 }
483 if (af6->ip6af_up->ip6af_mff) {
484 IP6Q_UNLOCK();
485 return IPPROTO_DONE;
486 }
487
488 /*
489 * Reassembly is complete; concatenate fragments.
490 */
491 ip6af = q6->ip6q_down;
492 t = m = IP6_REASS_MBUF(ip6af);
493 af6 = ip6af->ip6af_down;
494 frag6_deq(ip6af);
495 while (af6 != (struct ip6asfrag *)q6) {
496 af6dwn = af6->ip6af_down;
497 frag6_deq(af6);
498 while (t->m_next)
499 t = t->m_next;
500 t->m_next = IP6_REASS_MBUF(af6);
501 m_adj(t->m_next, af6->ip6af_offset);
502 free(af6, M_FTABLE);
503 af6 = af6dwn;
504 }
505
506 /* adjust offset to point where the original next header starts */
507 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
508 free(ip6af, M_FTABLE);
509 ip6 = mtod(m, struct ip6_hdr *);
510 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
511 if (q6->ip6q_ecn == IPTOS_ECN_CE)
512 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
513 nxt = q6->ip6q_nxt;
514#ifdef notyet
515 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
516#endif
517
518 /* Delete frag6 header */
519 if (m->m_len >= offset + sizeof(struct ip6_frag)) {
520 /* This is the only possible case with !PULLDOWN_TEST */
521 ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
522 offset);
523 m->m_data += sizeof(struct ip6_frag);
524 m->m_len -= sizeof(struct ip6_frag);
525 } else {
526 /* this comes with no copy if the boundary is on cluster */
527 if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
528 frag6_remque(q6);
529 frag6_nfrags -= q6->ip6q_nfrag;
530 free(q6, M_FTABLE);
531 frag6_nfragpackets--;
532 goto dropfrag;
533 }
534 m_adj(t, sizeof(struct ip6_frag));
535 m_cat(m, t);
536 }
537
538 /*
539 * Store NXT to the original.
540 */
541 {
542 char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
543 *prvnxtp = nxt;
544 }
545
546 frag6_remque(q6);
547 frag6_nfrags -= q6->ip6q_nfrag;
548 free(q6, M_FTABLE);
549 frag6_nfragpackets--;
550
551 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
552 int plen = 0;
553 for (t = m; t; t = t->m_next)
554 plen += t->m_len;
555 m->m_pkthdr.len = plen;
556 }
557
558 ip6stat.ip6s_reassembled++;
559 in6_ifstat_inc(dstifp, ifs6_reass_ok);
560
561 /*
562 * Tell launch routine the next header
563 */
564
565 *mp = m;
566 *offp = offset;
567
568 IP6Q_UNLOCK();
569 return nxt;
570
571 dropfrag:
572 IP6Q_UNLOCK();
573 in6_ifstat_inc(dstifp, ifs6_reass_fail);
574 ip6stat.ip6s_fragdropped++;
575 m_freem(m);
576 return IPPROTO_DONE;
577}
578
579/*
580 * Free a fragment reassembly header and all
581 * associated datagrams.
582 */
583void
584frag6_freef(struct ip6q *q6)
585{
586 struct ip6asfrag *af6, *down6;
587
588 IP6Q_LOCK_ASSERT();
589
590 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
591 af6 = down6) {
592 struct mbuf *m = IP6_REASS_MBUF(af6);
593
594 down6 = af6->ip6af_down;
595 frag6_deq(af6);
596
597 /*
598 * Return ICMP time exceeded error for the 1st fragment.
599 * Just free other fragments.
600 */
601 if (af6->ip6af_off == 0) {
602 struct ip6_hdr *ip6;
603
604 /* adjust pointer */
605 ip6 = mtod(m, struct ip6_hdr *);
606
607 /* restore source and destination addresses */
608 ip6->ip6_src = q6->ip6q_src;
609 ip6->ip6_dst = q6->ip6q_dst;
610
611 icmp6_error(m, ICMP6_TIME_EXCEEDED,
612 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
613 } else
614 m_freem(m);
615 free(af6, M_FTABLE);
616 }
617 frag6_remque(q6);
618 frag6_nfrags -= q6->ip6q_nfrag;
619 free(q6, M_FTABLE);
620 frag6_nfragpackets--;
621}
622
623/*
624 * Put an ip fragment on a reassembly chain.
625 * Like insque, but pointers in middle of structure.
626 */
627void
628frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
629{
630
631 IP6Q_LOCK_ASSERT();
632
633 af6->ip6af_up = up6;
634 af6->ip6af_down = up6->ip6af_down;
635 up6->ip6af_down->ip6af_up = af6;
636 up6->ip6af_down = af6;
637}
638
639/*
640 * To frag6_enq as remque is to insque.
641 */
642void
643frag6_deq(struct ip6asfrag *af6)
644{
645
646 IP6Q_LOCK_ASSERT();
647
648 af6->ip6af_up->ip6af_down = af6->ip6af_down;
649 af6->ip6af_down->ip6af_up = af6->ip6af_up;
650}
651
652void
653frag6_insque(struct ip6q *new, struct ip6q *old)
654{
655
656 IP6Q_LOCK_ASSERT();
657
658 new->ip6q_prev = old;
659 new->ip6q_next = old->ip6q_next;
660 old->ip6q_next->ip6q_prev= new;
661 old->ip6q_next = new;
662}
663
664void
665frag6_remque(struct ip6q *p6)
666{
667
668 IP6Q_LOCK_ASSERT();
669
670 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
671 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
672}
673
674/*
675 * IPv6 reassembling timer processing;
676 * if a timer expires on a reassembly
677 * queue, discard it.
678 */
679void
680frag6_slowtimo(void)
681{
682 struct ip6q *q6;
683
684#if 0
685 GIANT_REQUIRED; /* XXX bz: ip6_forward_rt */
686#endif
687
688 IP6Q_LOCK();
689 q6 = ip6q.ip6q_next;
690 if (q6)
691 while (q6 != &ip6q) {
692 --q6->ip6q_ttl;
693 q6 = q6->ip6q_next;
694 if (q6->ip6q_prev->ip6q_ttl == 0) {
695 ip6stat.ip6s_fragtimeout++;
696 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
697 frag6_freef(q6->ip6q_prev);
698 }
699 }
700 /*
701 * If we are over the maximum number of fragments
702 * (due to the limit being lowered), drain off
703 * enough to get down to the new limit.
704 */
705 while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
706 ip6q.ip6q_prev) {
707 ip6stat.ip6s_fragoverflow++;
708 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
709 frag6_freef(ip6q.ip6q_prev);
710 }
711 IP6Q_UNLOCK();
712
713#if 0
714 /*
715 * Routing changes might produce a better route than we last used;
716 * make sure we notice eventually, even if forwarding only for one
717 * destination and the cache is never replaced.
718 */
719 if (ip6_forward_rt.ro_rt) {
720 RTFREE(ip6_forward_rt.ro_rt);
721 ip6_forward_rt.ro_rt = 0;
722 }
723 if (ipsrcchk_rt.ro_rt) {
724 RTFREE(ipsrcchk_rt.ro_rt);
725 ipsrcchk_rt.ro_rt = 0;
726 }
727#endif
728}
729
730/*
731 * Drain off all datagram fragments.
732 */
733void
734frag6_drain(void)
735{
736
737 if (IP6Q_TRYLOCK() == 0)
738 return;
739 while (ip6q.ip6q_next != &ip6q) {
740 ip6stat.ip6s_fragdropped++;
741 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
742 frag6_freef(ip6q.ip6q_next);
743 }
744 IP6Q_UNLOCK();
745}
2/* $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $ */
3
4/*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/malloc.h>
36#include <sys/mbuf.h>
37#include <sys/domain.h>
38#include <sys/protosw.h>
39#include <sys/socket.h>
40#include <sys/errno.h>
41#include <sys/time.h>
42#include <sys/kernel.h>
43#include <sys/syslog.h>
44
45#include <net/if.h>
46#include <net/route.h>
47
48#include <netinet/in.h>
49#include <netinet/in_var.h>
50#include <netinet/ip6.h>
51#include <netinet6/ip6_var.h>
52#include <netinet/icmp6.h>
53#include <netinet/in_systm.h> /* for ECN definitions */
54#include <netinet/ip.h> /* for ECN definitions */
55
56/*
57 * Define it to get a correct behavior on per-interface statistics.
58 * You will need to perform an extra routing table lookup, per fragment,
59 * to do it. This may, or may not be, a performance hit.
60 */
61#define IN6_IFSTAT_STRICT
62
63static void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
64static void frag6_deq __P((struct ip6asfrag *));
65static void frag6_insque __P((struct ip6q *, struct ip6q *));
66static void frag6_remque __P((struct ip6q *));
67static void frag6_freef __P((struct ip6q *));
68
69static struct mtx ip6qlock;
70/*
71 * These fields all protected by ip6qlock.
72 */
73static u_int frag6_nfragpackets;
74static u_int frag6_nfrags;
75static struct ip6q ip6q; /* ip6 reassemble queue */
76
77#define IP6Q_LOCK_INIT() mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF);
78#define IP6Q_LOCK() mtx_lock(&ip6qlock)
79#define IP6Q_TRYLOCK() mtx_trylock(&ip6qlock)
80#define IP6Q_LOCK_ASSERT() mtx_assert(&ip6qlock, MA_OWNED)
81#define IP6Q_UNLOCK() mtx_unlock(&ip6qlock)
82
83static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
84
85/*
86 * Initialise reassembly queue and fragment identifier.
87 */
88static void
89frag6_change(void *tag)
90{
91
92 ip6_maxfragpackets = nmbclusters / 4;
93 ip6_maxfrags = nmbclusters / 4;
94}
95
96void
97frag6_init(void)
98{
99
100 ip6_maxfragpackets = nmbclusters / 4;
101 ip6_maxfrags = nmbclusters / 4;
102 EVENTHANDLER_REGISTER(nmbclusters_change,
103 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
104
105 IP6Q_LOCK_INIT();
106
107 ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
108}
109
110/*
111 * In RFC2460, fragment and reassembly rule do not agree with each other,
112 * in terms of next header field handling in fragment header.
113 * While the sender will use the same value for all of the fragmented packets,
114 * receiver is suggested not to check the consistency.
115 *
116 * fragment rule (p20):
117 * (2) A Fragment header containing:
118 * The Next Header value that identifies the first header of
119 * the Fragmentable Part of the original packet.
120 * -> next header field is same for all fragments
121 *
122 * reassembly rule (p21):
123 * The Next Header field of the last header of the Unfragmentable
124 * Part is obtained from the Next Header field of the first
125 * fragment's Fragment header.
126 * -> should grab it from the first fragment only
127 *
128 * The following note also contradicts with fragment rule - noone is going to
129 * send different fragment with different next header field.
130 *
131 * additional note (p22):
132 * The Next Header values in the Fragment headers of different
133 * fragments of the same original packet may differ. Only the value
134 * from the Offset zero fragment packet is used for reassembly.
135 * -> should grab it from the first fragment only
136 *
137 * There is no explicit reason given in the RFC. Historical reason maybe?
138 */
139/*
140 * Fragment input
141 */
142int
143frag6_input(struct mbuf **mp, int *offp, int proto)
144{
145 struct mbuf *m = *mp, *t;
146 struct ip6_hdr *ip6;
147 struct ip6_frag *ip6f;
148 struct ip6q *q6;
149 struct ip6asfrag *af6, *ip6af, *af6dwn;
150#ifdef IN6_IFSTAT_STRICT
151 struct in6_ifaddr *ia;
152#endif
153 int offset = *offp, nxt, i, next;
154 int first_frag = 0;
155 int fragoff, frgpartlen; /* must be larger than u_int16_t */
156 struct ifnet *dstifp;
157 u_int8_t ecn, ecn0;
158#if 0
159 char ip6buf[INET6_ADDRSTRLEN];
160#endif
161
162 ip6 = mtod(m, struct ip6_hdr *);
163#ifndef PULLDOWN_TEST
164 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
165 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
166#else
167 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
168 if (ip6f == NULL)
169 return (IPPROTO_DONE);
170#endif
171
172 dstifp = NULL;
173#ifdef IN6_IFSTAT_STRICT
174 /* find the destination interface of the packet. */
175 if ((ia = ip6_getdstifaddr(m)) != NULL)
176 dstifp = ia->ia_ifp;
177#else
178 /* we are violating the spec, this is not the destination interface */
179 if ((m->m_flags & M_PKTHDR) != 0)
180 dstifp = m->m_pkthdr.rcvif;
181#endif
182
183 /* jumbo payload can't contain a fragment header */
184 if (ip6->ip6_plen == 0) {
185 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
186 in6_ifstat_inc(dstifp, ifs6_reass_fail);
187 return IPPROTO_DONE;
188 }
189
190 /*
191 * check whether fragment packet's fragment length is
192 * multiple of 8 octets.
193 * sizeof(struct ip6_frag) == 8
194 * sizeof(struct ip6_hdr) = 40
195 */
196 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
197 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
198 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
199 offsetof(struct ip6_hdr, ip6_plen));
200 in6_ifstat_inc(dstifp, ifs6_reass_fail);
201 return IPPROTO_DONE;
202 }
203
204 ip6stat.ip6s_fragments++;
205 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
206
207 /* offset now points to data portion */
208 offset += sizeof(struct ip6_frag);
209
210 IP6Q_LOCK();
211
212 /*
213 * Enforce upper bound on number of fragments.
214 * If maxfrag is 0, never accept fragments.
215 * If maxfrag is -1, accept all fragments without limitation.
216 */
217 if (ip6_maxfrags < 0)
218 ;
219 else if (frag6_nfrags >= (u_int)ip6_maxfrags)
220 goto dropfrag;
221
222 for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
223 if (ip6f->ip6f_ident == q6->ip6q_ident &&
224 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
225 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
226 break;
227
228 if (q6 == &ip6q) {
229 /*
230 * the first fragment to arrive, create a reassembly queue.
231 */
232 first_frag = 1;
233
234 /*
235 * Enforce upper bound on number of fragmented packets
236 * for which we attempt reassembly;
237 * If maxfragpackets is 0, never accept fragments.
238 * If maxfragpackets is -1, accept all fragments without
239 * limitation.
240 */
241 if (ip6_maxfragpackets < 0)
242 ;
243 else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
244 goto dropfrag;
245 frag6_nfragpackets++;
246 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
247 M_NOWAIT);
248 if (q6 == NULL)
249 goto dropfrag;
250 bzero(q6, sizeof(*q6));
251
252 frag6_insque(q6, &ip6q);
253
254 /* ip6q_nxt will be filled afterwards, from 1st fragment */
255 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
256#ifdef notyet
257 q6->ip6q_nxtp = (u_char *)nxtp;
258#endif
259 q6->ip6q_ident = ip6f->ip6f_ident;
260 q6->ip6q_ttl = IPV6_FRAGTTL;
261 q6->ip6q_src = ip6->ip6_src;
262 q6->ip6q_dst = ip6->ip6_dst;
263 q6->ip6q_ecn =
264 (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
265 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
266
267 q6->ip6q_nfrag = 0;
268 }
269
270 /*
271 * If it's the 1st fragment, record the length of the
272 * unfragmentable part and the next header of the fragment header.
273 */
274 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
275 if (fragoff == 0) {
276 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
277 sizeof(struct ip6_frag);
278 q6->ip6q_nxt = ip6f->ip6f_nxt;
279 }
280
281 /*
282 * Check that the reassembled packet would not exceed 65535 bytes
283 * in size.
284 * If it would exceed, discard the fragment and return an ICMP error.
285 */
286 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
287 if (q6->ip6q_unfrglen >= 0) {
288 /* The 1st fragment has already arrived. */
289 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
290 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
291 offset - sizeof(struct ip6_frag) +
292 offsetof(struct ip6_frag, ip6f_offlg));
293 IP6Q_UNLOCK();
294 return (IPPROTO_DONE);
295 }
296 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
297 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
298 offset - sizeof(struct ip6_frag) +
299 offsetof(struct ip6_frag, ip6f_offlg));
300 IP6Q_UNLOCK();
301 return (IPPROTO_DONE);
302 }
303 /*
304 * If it's the first fragment, do the above check for each
305 * fragment already stored in the reassembly queue.
306 */
307 if (fragoff == 0) {
308 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
309 af6 = af6dwn) {
310 af6dwn = af6->ip6af_down;
311
312 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
313 IPV6_MAXPACKET) {
314 struct mbuf *merr = IP6_REASS_MBUF(af6);
315 struct ip6_hdr *ip6err;
316 int erroff = af6->ip6af_offset;
317
318 /* dequeue the fragment. */
319 frag6_deq(af6);
320 free(af6, M_FTABLE);
321
322 /* adjust pointer. */
323 ip6err = mtod(merr, struct ip6_hdr *);
324
325 /*
326 * Restore source and destination addresses
327 * in the erroneous IPv6 header.
328 */
329 ip6err->ip6_src = q6->ip6q_src;
330 ip6err->ip6_dst = q6->ip6q_dst;
331
332 icmp6_error(merr, ICMP6_PARAM_PROB,
333 ICMP6_PARAMPROB_HEADER,
334 erroff - sizeof(struct ip6_frag) +
335 offsetof(struct ip6_frag, ip6f_offlg));
336 }
337 }
338 }
339
340 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
341 M_NOWAIT);
342 if (ip6af == NULL)
343 goto dropfrag;
344 bzero(ip6af, sizeof(*ip6af));
345 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
346 ip6af->ip6af_off = fragoff;
347 ip6af->ip6af_frglen = frgpartlen;
348 ip6af->ip6af_offset = offset;
349 IP6_REASS_MBUF(ip6af) = m;
350
351 if (first_frag) {
352 af6 = (struct ip6asfrag *)q6;
353 goto insert;
354 }
355
356 /*
357 * Handle ECN by comparing this segment with the first one;
358 * if CE is set, do not lose CE.
359 * drop if CE and not-ECT are mixed for the same packet.
360 */
361 ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
362 ecn0 = q6->ip6q_ecn;
363 if (ecn == IPTOS_ECN_CE) {
364 if (ecn0 == IPTOS_ECN_NOTECT) {
365 free(ip6af, M_FTABLE);
366 goto dropfrag;
367 }
368 if (ecn0 != IPTOS_ECN_CE)
369 q6->ip6q_ecn = IPTOS_ECN_CE;
370 }
371 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
372 free(ip6af, M_FTABLE);
373 goto dropfrag;
374 }
375
376 /*
377 * Find a segment which begins after this one does.
378 */
379 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
380 af6 = af6->ip6af_down)
381 if (af6->ip6af_off > ip6af->ip6af_off)
382 break;
383
384#if 0
385 /*
386 * If there is a preceding segment, it may provide some of
387 * our data already. If so, drop the data from the incoming
388 * segment. If it provides all of our data, drop us.
389 */
390 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
391 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
392 - ip6af->ip6af_off;
393 if (i > 0) {
394 if (i >= ip6af->ip6af_frglen)
395 goto dropfrag;
396 m_adj(IP6_REASS_MBUF(ip6af), i);
397 ip6af->ip6af_off += i;
398 ip6af->ip6af_frglen -= i;
399 }
400 }
401
402 /*
403 * While we overlap succeeding segments trim them or,
404 * if they are completely covered, dequeue them.
405 */
406 while (af6 != (struct ip6asfrag *)q6 &&
407 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
408 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
409 if (i < af6->ip6af_frglen) {
410 af6->ip6af_frglen -= i;
411 af6->ip6af_off += i;
412 m_adj(IP6_REASS_MBUF(af6), i);
413 break;
414 }
415 af6 = af6->ip6af_down;
416 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
417 frag6_deq(af6->ip6af_up);
418 }
419#else
420 /*
421 * If the incoming framgent overlaps some existing fragments in
422 * the reassembly queue, drop it, since it is dangerous to override
423 * existing fragments from a security point of view.
424 * We don't know which fragment is the bad guy - here we trust
425 * fragment that came in earlier, with no real reason.
426 *
427 * Note: due to changes after disabling this part, mbuf passed to
428 * m_adj() below now does not meet the requirement.
429 */
430 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
431 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
432 - ip6af->ip6af_off;
433 if (i > 0) {
434#if 0 /* suppress the noisy log */
435 log(LOG_ERR, "%d bytes of a fragment from %s "
436 "overlaps the previous fragment\n",
437 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
438#endif
439 free(ip6af, M_FTABLE);
440 goto dropfrag;
441 }
442 }
443 if (af6 != (struct ip6asfrag *)q6) {
444 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
445 if (i > 0) {
446#if 0 /* suppress the noisy log */
447 log(LOG_ERR, "%d bytes of a fragment from %s "
448 "overlaps the succeeding fragment",
449 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
450#endif
451 free(ip6af, M_FTABLE);
452 goto dropfrag;
453 }
454 }
455#endif
456
457insert:
458
459 /*
460 * Stick new segment in its place;
461 * check for complete reassembly.
462 * Move to front of packet queue, as we are
463 * the most recently active fragmented packet.
464 */
465 frag6_enq(ip6af, af6->ip6af_up);
466 frag6_nfrags++;
467 q6->ip6q_nfrag++;
468#if 0 /* xxx */
469 if (q6 != ip6q.ip6q_next) {
470 frag6_remque(q6);
471 frag6_insque(q6, &ip6q);
472 }
473#endif
474 next = 0;
475 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
476 af6 = af6->ip6af_down) {
477 if (af6->ip6af_off != next) {
478 IP6Q_UNLOCK();
479 return IPPROTO_DONE;
480 }
481 next += af6->ip6af_frglen;
482 }
483 if (af6->ip6af_up->ip6af_mff) {
484 IP6Q_UNLOCK();
485 return IPPROTO_DONE;
486 }
487
488 /*
489 * Reassembly is complete; concatenate fragments.
490 */
491 ip6af = q6->ip6q_down;
492 t = m = IP6_REASS_MBUF(ip6af);
493 af6 = ip6af->ip6af_down;
494 frag6_deq(ip6af);
495 while (af6 != (struct ip6asfrag *)q6) {
496 af6dwn = af6->ip6af_down;
497 frag6_deq(af6);
498 while (t->m_next)
499 t = t->m_next;
500 t->m_next = IP6_REASS_MBUF(af6);
501 m_adj(t->m_next, af6->ip6af_offset);
502 free(af6, M_FTABLE);
503 af6 = af6dwn;
504 }
505
506 /* adjust offset to point where the original next header starts */
507 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
508 free(ip6af, M_FTABLE);
509 ip6 = mtod(m, struct ip6_hdr *);
510 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
511 if (q6->ip6q_ecn == IPTOS_ECN_CE)
512 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
513 nxt = q6->ip6q_nxt;
514#ifdef notyet
515 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
516#endif
517
518 /* Delete frag6 header */
519 if (m->m_len >= offset + sizeof(struct ip6_frag)) {
520 /* This is the only possible case with !PULLDOWN_TEST */
521 ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
522 offset);
523 m->m_data += sizeof(struct ip6_frag);
524 m->m_len -= sizeof(struct ip6_frag);
525 } else {
526 /* this comes with no copy if the boundary is on cluster */
527 if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
528 frag6_remque(q6);
529 frag6_nfrags -= q6->ip6q_nfrag;
530 free(q6, M_FTABLE);
531 frag6_nfragpackets--;
532 goto dropfrag;
533 }
534 m_adj(t, sizeof(struct ip6_frag));
535 m_cat(m, t);
536 }
537
538 /*
539 * Store NXT to the original.
540 */
541 {
542 char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
543 *prvnxtp = nxt;
544 }
545
546 frag6_remque(q6);
547 frag6_nfrags -= q6->ip6q_nfrag;
548 free(q6, M_FTABLE);
549 frag6_nfragpackets--;
550
551 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
552 int plen = 0;
553 for (t = m; t; t = t->m_next)
554 plen += t->m_len;
555 m->m_pkthdr.len = plen;
556 }
557
558 ip6stat.ip6s_reassembled++;
559 in6_ifstat_inc(dstifp, ifs6_reass_ok);
560
561 /*
562 * Tell launch routine the next header
563 */
564
565 *mp = m;
566 *offp = offset;
567
568 IP6Q_UNLOCK();
569 return nxt;
570
571 dropfrag:
572 IP6Q_UNLOCK();
573 in6_ifstat_inc(dstifp, ifs6_reass_fail);
574 ip6stat.ip6s_fragdropped++;
575 m_freem(m);
576 return IPPROTO_DONE;
577}
578
579/*
580 * Free a fragment reassembly header and all
581 * associated datagrams.
582 */
583void
584frag6_freef(struct ip6q *q6)
585{
586 struct ip6asfrag *af6, *down6;
587
588 IP6Q_LOCK_ASSERT();
589
590 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
591 af6 = down6) {
592 struct mbuf *m = IP6_REASS_MBUF(af6);
593
594 down6 = af6->ip6af_down;
595 frag6_deq(af6);
596
597 /*
598 * Return ICMP time exceeded error for the 1st fragment.
599 * Just free other fragments.
600 */
601 if (af6->ip6af_off == 0) {
602 struct ip6_hdr *ip6;
603
604 /* adjust pointer */
605 ip6 = mtod(m, struct ip6_hdr *);
606
607 /* restore source and destination addresses */
608 ip6->ip6_src = q6->ip6q_src;
609 ip6->ip6_dst = q6->ip6q_dst;
610
611 icmp6_error(m, ICMP6_TIME_EXCEEDED,
612 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
613 } else
614 m_freem(m);
615 free(af6, M_FTABLE);
616 }
617 frag6_remque(q6);
618 frag6_nfrags -= q6->ip6q_nfrag;
619 free(q6, M_FTABLE);
620 frag6_nfragpackets--;
621}
622
623/*
624 * Put an ip fragment on a reassembly chain.
625 * Like insque, but pointers in middle of structure.
626 */
627void
628frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
629{
630
631 IP6Q_LOCK_ASSERT();
632
633 af6->ip6af_up = up6;
634 af6->ip6af_down = up6->ip6af_down;
635 up6->ip6af_down->ip6af_up = af6;
636 up6->ip6af_down = af6;
637}
638
639/*
640 * To frag6_enq as remque is to insque.
641 */
642void
643frag6_deq(struct ip6asfrag *af6)
644{
645
646 IP6Q_LOCK_ASSERT();
647
648 af6->ip6af_up->ip6af_down = af6->ip6af_down;
649 af6->ip6af_down->ip6af_up = af6->ip6af_up;
650}
651
652void
653frag6_insque(struct ip6q *new, struct ip6q *old)
654{
655
656 IP6Q_LOCK_ASSERT();
657
658 new->ip6q_prev = old;
659 new->ip6q_next = old->ip6q_next;
660 old->ip6q_next->ip6q_prev= new;
661 old->ip6q_next = new;
662}
663
664void
665frag6_remque(struct ip6q *p6)
666{
667
668 IP6Q_LOCK_ASSERT();
669
670 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
671 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
672}
673
674/*
675 * IPv6 reassembling timer processing;
676 * if a timer expires on a reassembly
677 * queue, discard it.
678 */
679void
680frag6_slowtimo(void)
681{
682 struct ip6q *q6;
683
684#if 0
685 GIANT_REQUIRED; /* XXX bz: ip6_forward_rt */
686#endif
687
688 IP6Q_LOCK();
689 q6 = ip6q.ip6q_next;
690 if (q6)
691 while (q6 != &ip6q) {
692 --q6->ip6q_ttl;
693 q6 = q6->ip6q_next;
694 if (q6->ip6q_prev->ip6q_ttl == 0) {
695 ip6stat.ip6s_fragtimeout++;
696 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
697 frag6_freef(q6->ip6q_prev);
698 }
699 }
700 /*
701 * If we are over the maximum number of fragments
702 * (due to the limit being lowered), drain off
703 * enough to get down to the new limit.
704 */
705 while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
706 ip6q.ip6q_prev) {
707 ip6stat.ip6s_fragoverflow++;
708 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
709 frag6_freef(ip6q.ip6q_prev);
710 }
711 IP6Q_UNLOCK();
712
713#if 0
714 /*
715 * Routing changes might produce a better route than we last used;
716 * make sure we notice eventually, even if forwarding only for one
717 * destination and the cache is never replaced.
718 */
719 if (ip6_forward_rt.ro_rt) {
720 RTFREE(ip6_forward_rt.ro_rt);
721 ip6_forward_rt.ro_rt = 0;
722 }
723 if (ipsrcchk_rt.ro_rt) {
724 RTFREE(ipsrcchk_rt.ro_rt);
725 ipsrcchk_rt.ro_rt = 0;
726 }
727#endif
728}
729
730/*
731 * Drain off all datagram fragments.
732 */
733void
734frag6_drain(void)
735{
736
737 if (IP6Q_TRYLOCK() == 0)
738 return;
739 while (ip6q.ip6q_next != &ip6q) {
740 ip6stat.ip6s_fragdropped++;
741 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
742 frag6_freef(ip6q.ip6q_next);
743 }
744 IP6Q_UNLOCK();
745}