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