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