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