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