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