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