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