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