1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * Copyright (c) 2019 Netflix, Inc. 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 * $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $ 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include "opt_rss.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/domain.h> 43 #include <sys/eventhandler.h> 44 #include <sys/hash.h> 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/protosw.h> 49 #include <sys/queue.h> 50 #include <sys/socket.h> 51 #include <sys/sysctl.h> 52 #include <sys/syslog.h> 53 54 #include <net/if.h> 55 #include <net/if_var.h> 56 #include <net/if_private.h> 57 #include <net/netisr.h> 58 #include <net/route.h> 59 #include <net/vnet.h> 60 61 #include <netinet/in.h> 62 #include <netinet/in_var.h> 63 #include <netinet/ip6.h> 64 #include <netinet6/ip6_var.h> 65 #include <netinet/icmp6.h> 66 #include <netinet/in_systm.h> /* For ECN definitions. */ 67 #include <netinet/ip.h> /* For ECN definitions. */ 68 69 #ifdef MAC 70 #include <security/mac/mac_framework.h> 71 #endif 72 73 /* 74 * A "big picture" of how IPv6 fragment queues are all linked together. 75 * 76 * struct ip6qbucket ip6qb[...]; hashed buckets 77 * |||||||| 78 * | 79 * +--- TAILQ(struct ip6q, packets) *q6; tailq entries holding 80 * |||||||| fragmented packets 81 * | (1 per original packet) 82 * | 83 * +--- TAILQ(struct ip6asfrag, ip6q_frags) *af6; tailq entries of IPv6 84 * | *ip6af;fragment packets 85 * | for one original packet 86 * + *mbuf 87 */ 88 89 /* Reassembly headers are stored in hash buckets. */ 90 #define IP6REASS_NHASH_LOG2 10 91 #define IP6REASS_NHASH (1 << IP6REASS_NHASH_LOG2) 92 #define IP6REASS_HMASK (IP6REASS_NHASH - 1) 93 94 TAILQ_HEAD(ip6qhead, ip6q); 95 struct ip6qbucket { 96 struct ip6qhead packets; 97 struct mtx lock; 98 int count; 99 }; 100 101 struct ip6asfrag { 102 TAILQ_ENTRY(ip6asfrag) ip6af_tq; 103 struct mbuf *ip6af_m; 104 int ip6af_offset; /* Offset in ip6af_m to next header. */ 105 int ip6af_frglen; /* Fragmentable part length. */ 106 int ip6af_off; /* Fragment offset. */ 107 bool ip6af_mff; /* More fragment bit in frag off. */ 108 }; 109 110 static MALLOC_DEFINE(M_FRAG6, "frag6", "IPv6 fragment reassembly header"); 111 112 #ifdef VIMAGE 113 /* A flag to indicate if IPv6 fragmentation is initialized. */ 114 VNET_DEFINE_STATIC(bool, frag6_on); 115 #define V_frag6_on VNET(frag6_on) 116 #endif 117 118 /* System wide (global) maximum and count of packets in reassembly queues. */ 119 static int ip6_maxfrags; 120 static u_int __exclusive_cache_line frag6_nfrags; 121 122 /* Maximum and current packets in per-VNET reassembly queue. */ 123 VNET_DEFINE_STATIC(int, ip6_maxfragpackets); 124 VNET_DEFINE_STATIC(volatile u_int, frag6_nfragpackets); 125 #define V_ip6_maxfragpackets VNET(ip6_maxfragpackets) 126 #define V_frag6_nfragpackets VNET(frag6_nfragpackets) 127 128 /* Maximum per-VNET reassembly timeout (milliseconds) */ 129 VNET_DEFINE_STATIC(u_int, ip6_fraglifetime) = IPV6_DEFFRAGTTL; 130 #define V_ip6_fraglifetime VNET(ip6_fraglifetime) 131 132 /* Maximum per-VNET reassembly queues per bucket and fragments per packet. */ 133 VNET_DEFINE_STATIC(int, ip6_maxfragbucketsize); 134 VNET_DEFINE_STATIC(int, ip6_maxfragsperpacket); 135 #define V_ip6_maxfragbucketsize VNET(ip6_maxfragbucketsize) 136 #define V_ip6_maxfragsperpacket VNET(ip6_maxfragsperpacket) 137 138 /* Per-VNET reassembly queue buckets. */ 139 VNET_DEFINE_STATIC(struct ip6qbucket, ip6qb[IP6REASS_NHASH]); 140 VNET_DEFINE_STATIC(uint32_t, ip6qb_hashseed); 141 #define V_ip6qb VNET(ip6qb) 142 #define V_ip6qb_hashseed VNET(ip6qb_hashseed) 143 144 #define IP6QB_LOCK(_b) mtx_lock(&V_ip6qb[(_b)].lock) 145 #define IP6QB_TRYLOCK(_b) mtx_trylock(&V_ip6qb[(_b)].lock) 146 #define IP6QB_LOCK_ASSERT(_b) mtx_assert(&V_ip6qb[(_b)].lock, MA_OWNED) 147 #define IP6QB_UNLOCK(_b) mtx_unlock(&V_ip6qb[(_b)].lock) 148 #define IP6QB_HEAD(_b) (&V_ip6qb[(_b)].packets) 149 150 /* 151 * By default, limit the number of IP6 fragments across all reassembly 152 * queues to 1/32 of the total number of mbuf clusters. 153 * 154 * Limit the total number of reassembly queues per VNET to the 155 * IP6 fragment limit, but ensure the limit will not allow any bucket 156 * to grow above 100 items. (The bucket limit is 157 * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct 158 * multiplier to reach a 100-item limit.) 159 * The 100-item limit was chosen as brief testing seems to show that 160 * this produces "reasonable" performance on some subset of systems 161 * under DoS attack. 162 */ 163 #define IP6_MAXFRAGS (nmbclusters / 32) 164 #define IP6_MAXFRAGPACKETS (imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50)) 165 166 /* Interval between periodic reassembly queue inspections */ 167 #define IP6_CALLOUT_INTERVAL_MS 500 168 169 /* 170 * Sysctls and helper function. 171 */ 172 SYSCTL_DECL(_net_inet6_ip6); 173 174 SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfrags, 175 CTLFLAG_RD, &frag6_nfrags, 0, 176 "Global number of IPv6 fragments across all reassembly queues."); 177 178 static void 179 frag6_set_bucketsize(void) 180 { 181 int i; 182 183 if ((i = V_ip6_maxfragpackets) > 0) 184 V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1); 185 } 186 187 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGS, maxfrags, 188 CTLFLAG_RW, &ip6_maxfrags, 0, 189 "Maximum allowed number of outstanding IPv6 packet fragments. " 190 "A value of 0 means no fragmented packets will be accepted, while " 191 "a value of -1 means no limit"); 192 193 static int 194 sysctl_ip6_maxfragpackets(SYSCTL_HANDLER_ARGS) 195 { 196 int error, val; 197 198 val = V_ip6_maxfragpackets; 199 error = sysctl_handle_int(oidp, &val, 0, req); 200 if (error != 0 || !req->newptr) 201 return (error); 202 V_ip6_maxfragpackets = val; 203 frag6_set_bucketsize(); 204 return (0); 205 } 206 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS, maxfragpackets, 207 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 208 NULL, 0, sysctl_ip6_maxfragpackets, "I", 209 "Default maximum number of outstanding fragmented IPv6 packets. " 210 "A value of 0 means no fragmented packets will be accepted, while a " 211 "a value of -1 means no limit"); 212 SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfragpackets, 213 CTLFLAG_VNET | CTLFLAG_RD, 214 __DEVOLATILE(u_int *, &VNET_NAME(frag6_nfragpackets)), 0, 215 "Per-VNET number of IPv6 fragments across all reassembly queues."); 216 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGSPERPACKET, maxfragsperpacket, 217 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragsperpacket), 0, 218 "Maximum allowed number of fragments per packet"); 219 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGBUCKETSIZE, maxfragbucketsize, 220 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragbucketsize), 0, 221 "Maximum number of reassembly queues per hash bucket"); 222 223 static int 224 frag6_milli_to_callout_ticks(int ms) 225 { 226 return (ms / IP6_CALLOUT_INTERVAL_MS); 227 } 228 229 static int 230 frag6_callout_ticks_to_milli(int ms) 231 { 232 return (ms * IP6_CALLOUT_INTERVAL_MS); 233 } 234 235 _Static_assert(sizeof(((struct ip6q *)NULL)->ip6q_ttl) >= 2, 236 "ip6q_ttl field is not large enough"); 237 238 static int 239 sysctl_ip6_fraglifetime(SYSCTL_HANDLER_ARGS) 240 { 241 int error, val; 242 243 val = V_ip6_fraglifetime; 244 error = sysctl_handle_int(oidp, &val, 0, req); 245 if (error != 0 || !req->newptr) 246 return (error); 247 if (val <= 0) 248 val = IPV6_DEFFRAGTTL; 249 250 if (frag6_milli_to_callout_ticks(val) >= 65536) 251 val = frag6_callout_ticks_to_milli(65535); 252 #ifdef VIMAGE 253 if (!IS_DEFAULT_VNET(curvnet)) { 254 CURVNET_SET(vnet0); 255 int host_val = V_ip6_fraglifetime; 256 CURVNET_RESTORE(); 257 258 if (val > host_val) 259 val = host_val; 260 } 261 #endif 262 V_ip6_fraglifetime = val; 263 return (0); 264 } 265 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, fraglifetime_ms, 266 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 267 NULL, 0, sysctl_ip6_fraglifetime, "I", 268 "Fragment lifetime, in milliseconds"); 269 270 /* 271 * Remove the IPv6 fragmentation header from the mbuf. 272 */ 273 int 274 ip6_deletefraghdr(struct mbuf *m, int offset, int wait __unused) 275 { 276 struct ip6_hdr *ip6; 277 278 KASSERT(m->m_len >= offset + sizeof(struct ip6_frag), 279 ("%s: ext headers not contigous in mbuf %p m_len %d >= " 280 "offset %d + %zu\n", __func__, m, m->m_len, offset, 281 sizeof(struct ip6_frag))); 282 283 /* Delete frag6 header. */ 284 ip6 = mtod(m, struct ip6_hdr *); 285 bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag), offset); 286 m->m_data += sizeof(struct ip6_frag); 287 m->m_len -= sizeof(struct ip6_frag); 288 m->m_flags |= M_FRAGMENTED; 289 290 return (0); 291 } 292 293 /* 294 * Free a fragment reassembly header and all associated datagrams. 295 */ 296 static void 297 frag6_freef(struct ip6q *q6, uint32_t bucket) 298 { 299 struct ip6_hdr *ip6; 300 struct ip6asfrag *af6; 301 struct mbuf *m; 302 303 IP6QB_LOCK_ASSERT(bucket); 304 305 while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) { 306 m = af6->ip6af_m; 307 TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq); 308 309 /* 310 * Return ICMP time exceeded error for the 1st fragment. 311 * Just free other fragments. 312 */ 313 if (af6->ip6af_off == 0 && m->m_pkthdr.rcvif != NULL) { 314 /* Adjust pointer. */ 315 ip6 = mtod(m, struct ip6_hdr *); 316 317 /* Restore source and destination addresses. */ 318 ip6->ip6_src = q6->ip6q_src; 319 ip6->ip6_dst = q6->ip6q_dst; 320 321 icmp6_error(m, ICMP6_TIME_EXCEEDED, 322 ICMP6_TIME_EXCEED_REASSEMBLY, 0); 323 } else 324 m_freem(m); 325 326 free(af6, M_FRAG6); 327 } 328 329 TAILQ_REMOVE(IP6QB_HEAD(bucket), q6, ip6q_tq); 330 V_ip6qb[bucket].count--; 331 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag); 332 #ifdef MAC 333 mac_ip6q_destroy(q6); 334 #endif 335 free(q6, M_FRAG6); 336 atomic_subtract_int(&V_frag6_nfragpackets, 1); 337 } 338 339 /* 340 * Drain off all datagram fragments belonging to 341 * the given network interface. 342 */ 343 static void 344 frag6_cleanup(void *arg __unused, struct ifnet *ifp) 345 { 346 struct ip6qhead *head; 347 struct ip6q *q6; 348 struct ip6asfrag *af6; 349 uint32_t bucket; 350 351 KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 352 353 CURVNET_SET_QUIET(ifp->if_vnet); 354 #ifdef VIMAGE 355 /* 356 * Skip processing if IPv6 reassembly is not initialised or 357 * torn down by frag6_destroy(). 358 */ 359 if (!V_frag6_on) { 360 CURVNET_RESTORE(); 361 return; 362 } 363 #endif 364 365 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) { 366 IP6QB_LOCK(bucket); 367 head = IP6QB_HEAD(bucket); 368 /* Scan fragment list. */ 369 TAILQ_FOREACH(q6, head, ip6q_tq) { 370 TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) { 371 /* Clear no longer valid rcvif pointer. */ 372 if (af6->ip6af_m->m_pkthdr.rcvif == ifp) 373 af6->ip6af_m->m_pkthdr.rcvif = NULL; 374 } 375 } 376 IP6QB_UNLOCK(bucket); 377 } 378 CURVNET_RESTORE(); 379 } 380 EVENTHANDLER_DEFINE(ifnet_departure_event, frag6_cleanup, NULL, 0); 381 382 /* 383 * Like in RFC2460, in RFC8200, fragment and reassembly rules do not agree with 384 * each other, in terms of next header field handling in fragment header. 385 * While the sender will use the same value for all of the fragmented packets, 386 * receiver is suggested not to check for consistency. 387 * 388 * Fragment rules (p18,p19): 389 * (2) A Fragment header containing: 390 * The Next Header value that identifies the first header 391 * after the Per-Fragment headers of the original packet. 392 * -> next header field is same for all fragments 393 * 394 * Reassembly rule (p20): 395 * The Next Header field of the last header of the Per-Fragment 396 * headers is obtained from the Next Header field of the first 397 * fragment's Fragment header. 398 * -> should grab it from the first fragment only 399 * 400 * The following note also contradicts with fragment rule - no one is going to 401 * send different fragment with different next header field. 402 * 403 * Additional note (p22) [not an error]: 404 * The Next Header values in the Fragment headers of different 405 * fragments of the same original packet may differ. Only the value 406 * from the Offset zero fragment packet is used for reassembly. 407 * -> should grab it from the first fragment only 408 * 409 * There is no explicit reason given in the RFC. Historical reason maybe? 410 */ 411 /* 412 * Fragment input. 413 */ 414 int 415 frag6_input(struct mbuf **mp, int *offp, int proto) 416 { 417 struct mbuf *m, *t; 418 struct ip6_hdr *ip6; 419 struct ip6_frag *ip6f; 420 struct ip6qhead *head; 421 struct ip6q *q6; 422 struct ip6asfrag *af6, *ip6af, *af6tmp; 423 struct in6_ifaddr *ia6; 424 struct ifnet *dstifp, *srcifp; 425 uint32_t hashkey[(sizeof(struct in6_addr) * 2 + 426 sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)]; 427 uint32_t bucket, *hashkeyp; 428 int fragoff, frgpartlen; /* Must be larger than uint16_t. */ 429 int nxt, offset, plen; 430 uint8_t ecn, ecn0; 431 bool only_frag; 432 #ifdef RSS 433 struct ip6_direct_ctx *ip6dc; 434 struct m_tag *mtag; 435 #endif 436 437 m = *mp; 438 offset = *offp; 439 440 M_ASSERTPKTHDR(m); 441 442 if (m->m_len < offset + sizeof(struct ip6_frag)) { 443 m = m_pullup(m, offset + sizeof(struct ip6_frag)); 444 if (m == NULL) { 445 IP6STAT_INC(ip6s_exthdrtoolong); 446 *mp = NULL; 447 return (IPPROTO_DONE); 448 } 449 } 450 ip6 = mtod(m, struct ip6_hdr *); 451 452 dstifp = NULL; 453 /* Find the destination interface of the packet. */ 454 ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 455 if (ia6 != NULL) 456 dstifp = ia6->ia_ifp; 457 458 /* Jumbo payload cannot contain a fragment header. */ 459 if (ip6->ip6_plen == 0) { 460 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset); 461 in6_ifstat_inc(dstifp, ifs6_reass_fail); 462 *mp = NULL; 463 return (IPPROTO_DONE); 464 } 465 466 /* 467 * Check whether fragment packet's fragment length is a 468 * multiple of 8 octets (unless it is the last one). 469 * sizeof(struct ip6_frag) == 8 470 * sizeof(struct ip6_hdr) = 40 471 */ 472 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset); 473 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) && 474 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) { 475 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 476 offsetof(struct ip6_hdr, ip6_plen)); 477 in6_ifstat_inc(dstifp, ifs6_reass_fail); 478 *mp = NULL; 479 return (IPPROTO_DONE); 480 } 481 482 IP6STAT_INC(ip6s_fragments); 483 in6_ifstat_inc(dstifp, ifs6_reass_reqd); 484 485 /* 486 * Handle "atomic" fragments (offset and m bit set to 0) upfront, 487 * unrelated to any reassembly. We need to remove the frag hdr 488 * which is ugly. 489 * See RFC 6946 and section 4.5 of RFC 8200. 490 */ 491 if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) { 492 IP6STAT_INC(ip6s_atomicfrags); 493 nxt = ip6f->ip6f_nxt; 494 /* 495 * Set nxt(-hdr field value) to the original value. 496 * We cannot just set ip6->ip6_nxt as there might be 497 * an unfragmentable part with extension headers and 498 * we must update the last one. 499 */ 500 m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t), 501 (caddr_t)&nxt); 502 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - 503 sizeof(struct ip6_frag)); 504 if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) 505 goto dropfrag2; 506 m->m_pkthdr.len -= sizeof(struct ip6_frag); 507 in6_ifstat_inc(dstifp, ifs6_reass_ok); 508 *mp = m; 509 return (nxt); 510 } 511 512 /* Offset now points to data portion. */ 513 offset += sizeof(struct ip6_frag); 514 515 /* Get fragment length and discard 0-byte fragments. */ 516 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset; 517 if (frgpartlen == 0) { 518 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 519 offsetof(struct ip6_hdr, ip6_plen)); 520 in6_ifstat_inc(dstifp, ifs6_reass_fail); 521 IP6STAT_INC(ip6s_fragdropped); 522 *mp = NULL; 523 return (IPPROTO_DONE); 524 } 525 526 /* 527 * Enforce upper bound on number of fragments for the entire system. 528 * If maxfrag is 0, never accept fragments. 529 * If maxfrag is -1, accept all fragments without limitation. 530 */ 531 if (ip6_maxfrags < 0) 532 ; 533 else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags) 534 goto dropfrag2; 535 536 /* 537 * Validate that a full header chain to the ULP is present in the 538 * packet containing the first fragment as per RFC RFC7112 and 539 * RFC 8200 pages 18,19: 540 * The first fragment packet is composed of: 541 * (3) Extension headers, if any, and the Upper-Layer header. These 542 * headers must be in the first fragment. ... 543 */ 544 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK); 545 /* XXX TODO. thj has D16851 open for this. */ 546 /* Send ICMPv6 4,3 in case of violation. */ 547 548 /* Store receive network interface pointer for later. */ 549 srcifp = m->m_pkthdr.rcvif; 550 551 /* Generate a hash value for fragment bucket selection. */ 552 hashkeyp = hashkey; 553 memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr)); 554 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp); 555 memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr)); 556 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp); 557 *hashkeyp = ip6f->ip6f_ident; 558 bucket = jenkins_hash32(hashkey, nitems(hashkey), V_ip6qb_hashseed); 559 bucket &= IP6REASS_HMASK; 560 IP6QB_LOCK(bucket); 561 head = IP6QB_HEAD(bucket); 562 563 TAILQ_FOREACH(q6, head, ip6q_tq) 564 if (ip6f->ip6f_ident == q6->ip6q_ident && 565 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) && 566 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst) 567 #ifdef MAC 568 && mac_ip6q_match(m, q6) 569 #endif 570 ) 571 break; 572 573 only_frag = false; 574 if (q6 == NULL) { 575 /* A first fragment to arrive creates a reassembly queue. */ 576 only_frag = true; 577 578 /* 579 * Enforce upper bound on number of fragmented packets 580 * for which we attempt reassembly; 581 * If maxfragpackets is 0, never accept fragments. 582 * If maxfragpackets is -1, accept all fragments without 583 * limitation. 584 */ 585 if (V_ip6_maxfragpackets < 0) 586 ; 587 else if (V_ip6qb[bucket].count >= V_ip6_maxfragbucketsize || 588 atomic_load_int(&V_frag6_nfragpackets) >= 589 (u_int)V_ip6_maxfragpackets) 590 goto dropfrag; 591 592 /* Allocate IPv6 fragement packet queue entry. */ 593 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FRAG6, 594 M_NOWAIT | M_ZERO); 595 if (q6 == NULL) 596 goto dropfrag; 597 #ifdef MAC 598 if (mac_ip6q_init(q6, M_NOWAIT) != 0) { 599 free(q6, M_FRAG6); 600 goto dropfrag; 601 } 602 mac_ip6q_create(m, q6); 603 #endif 604 atomic_add_int(&V_frag6_nfragpackets, 1); 605 606 /* ip6q_nxt will be filled afterwards, from 1st fragment. */ 607 TAILQ_INIT(&q6->ip6q_frags); 608 q6->ip6q_ident = ip6f->ip6f_ident; 609 q6->ip6q_ttl = frag6_milli_to_callout_ticks(V_ip6_fraglifetime); 610 q6->ip6q_src = ip6->ip6_src; 611 q6->ip6q_dst = ip6->ip6_dst; 612 q6->ip6q_ecn = IPV6_ECN(ip6); 613 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */ 614 615 /* Add the fragemented packet to the bucket. */ 616 TAILQ_INSERT_HEAD(head, q6, ip6q_tq); 617 V_ip6qb[bucket].count++; 618 } 619 620 /* 621 * If it is the 1st fragment, record the length of the 622 * unfragmentable part and the next header of the fragment header. 623 * Assume the first 1st fragement to arrive will be correct. 624 * We do not have any duplicate checks here yet so another packet 625 * with fragoff == 0 could come and overwrite the ip6q_unfrglen 626 * and worse, the next header, at any time. 627 */ 628 if (fragoff == 0 && q6->ip6q_unfrglen == -1) { 629 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) - 630 sizeof(struct ip6_frag); 631 q6->ip6q_nxt = ip6f->ip6f_nxt; 632 /* XXX ECN? */ 633 } 634 635 /* 636 * Check that the reassembled packet would not exceed 65535 bytes 637 * in size. 638 * If it would exceed, discard the fragment and return an ICMP error. 639 */ 640 if (q6->ip6q_unfrglen >= 0) { 641 /* The 1st fragment has already arrived. */ 642 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) { 643 if (only_frag) { 644 TAILQ_REMOVE(head, q6, ip6q_tq); 645 V_ip6qb[bucket].count--; 646 atomic_subtract_int(&V_frag6_nfragpackets, 1); 647 #ifdef MAC 648 mac_ip6q_destroy(q6); 649 #endif 650 free(q6, M_FRAG6); 651 } 652 IP6QB_UNLOCK(bucket); 653 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 654 offset - sizeof(struct ip6_frag) + 655 offsetof(struct ip6_frag, ip6f_offlg)); 656 *mp = NULL; 657 return (IPPROTO_DONE); 658 } 659 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) { 660 if (only_frag) { 661 TAILQ_REMOVE(head, q6, ip6q_tq); 662 V_ip6qb[bucket].count--; 663 atomic_subtract_int(&V_frag6_nfragpackets, 1); 664 #ifdef MAC 665 mac_ip6q_destroy(q6); 666 #endif 667 free(q6, M_FRAG6); 668 } 669 IP6QB_UNLOCK(bucket); 670 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 671 offset - sizeof(struct ip6_frag) + 672 offsetof(struct ip6_frag, ip6f_offlg)); 673 *mp = NULL; 674 return (IPPROTO_DONE); 675 } 676 677 /* 678 * If it is the first fragment, do the above check for each 679 * fragment already stored in the reassembly queue. 680 */ 681 if (fragoff == 0 && !only_frag) { 682 TAILQ_FOREACH_SAFE(af6, &q6->ip6q_frags, ip6af_tq, af6tmp) { 683 if (q6->ip6q_unfrglen + af6->ip6af_off + 684 af6->ip6af_frglen > IPV6_MAXPACKET) { 685 struct ip6_hdr *ip6err; 686 struct mbuf *merr; 687 int erroff; 688 689 merr = af6->ip6af_m; 690 erroff = af6->ip6af_offset; 691 692 /* Dequeue the fragment. */ 693 TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq); 694 q6->ip6q_nfrag--; 695 atomic_subtract_int(&frag6_nfrags, 1); 696 free(af6, M_FRAG6); 697 698 /* Set a valid receive interface pointer. */ 699 merr->m_pkthdr.rcvif = srcifp; 700 701 /* Adjust pointer. */ 702 ip6err = mtod(merr, struct ip6_hdr *); 703 704 /* 705 * Restore source and destination addresses 706 * in the erroneous IPv6 header. 707 */ 708 ip6err->ip6_src = q6->ip6q_src; 709 ip6err->ip6_dst = q6->ip6q_dst; 710 711 icmp6_error(merr, ICMP6_PARAM_PROB, 712 ICMP6_PARAMPROB_HEADER, 713 erroff - sizeof(struct ip6_frag) + 714 offsetof(struct ip6_frag, ip6f_offlg)); 715 } 716 } 717 } 718 719 /* Allocate an IPv6 fragement queue entry for this fragmented part. */ 720 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FRAG6, 721 M_NOWAIT | M_ZERO); 722 if (ip6af == NULL) 723 goto dropfrag; 724 ip6af->ip6af_mff = (ip6f->ip6f_offlg & IP6F_MORE_FRAG) ? true : false; 725 ip6af->ip6af_off = fragoff; 726 ip6af->ip6af_frglen = frgpartlen; 727 ip6af->ip6af_offset = offset; 728 ip6af->ip6af_m = m; 729 730 if (only_frag) { 731 /* 732 * Do a manual insert rather than a hard-to-understand cast 733 * to a different type relying on data structure order to work. 734 */ 735 TAILQ_INSERT_HEAD(&q6->ip6q_frags, ip6af, ip6af_tq); 736 goto postinsert; 737 } 738 739 /* Do duplicate, condition, and boundry checks. */ 740 /* 741 * Handle ECN by comparing this segment with the first one; 742 * if CE is set, do not lose CE. 743 * Drop if CE and not-ECT are mixed for the same packet. 744 */ 745 ecn = IPV6_ECN(ip6); 746 ecn0 = q6->ip6q_ecn; 747 if (ecn == IPTOS_ECN_CE) { 748 if (ecn0 == IPTOS_ECN_NOTECT) { 749 free(ip6af, M_FRAG6); 750 goto dropfrag; 751 } 752 if (ecn0 != IPTOS_ECN_CE) 753 q6->ip6q_ecn = IPTOS_ECN_CE; 754 } 755 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) { 756 free(ip6af, M_FRAG6); 757 goto dropfrag; 758 } 759 760 /* Find a fragmented part which begins after this one does. */ 761 TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) 762 if (af6->ip6af_off > ip6af->ip6af_off) 763 break; 764 765 /* 766 * If the incoming framgent overlaps some existing fragments in 767 * the reassembly queue, drop both the new fragment and the 768 * entire reassembly queue. However, if the new fragment 769 * is an exact duplicate of an existing fragment, only silently 770 * drop the existing fragment and leave the fragmentation queue 771 * unchanged, as allowed by the RFC. (RFC 8200, 4.5) 772 */ 773 if (af6 != NULL) 774 af6tmp = TAILQ_PREV(af6, ip6fraghead, ip6af_tq); 775 else 776 af6tmp = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead); 777 if (af6tmp != NULL) { 778 if (af6tmp->ip6af_off + af6tmp->ip6af_frglen - 779 ip6af->ip6af_off > 0) { 780 if (af6tmp->ip6af_off != ip6af->ip6af_off || 781 af6tmp->ip6af_frglen != ip6af->ip6af_frglen) 782 frag6_freef(q6, bucket); 783 free(ip6af, M_FRAG6); 784 goto dropfrag; 785 } 786 } 787 if (af6 != NULL) { 788 if (ip6af->ip6af_off + ip6af->ip6af_frglen - 789 af6->ip6af_off > 0) { 790 if (af6->ip6af_off != ip6af->ip6af_off || 791 af6->ip6af_frglen != ip6af->ip6af_frglen) 792 frag6_freef(q6, bucket); 793 free(ip6af, M_FRAG6); 794 goto dropfrag; 795 } 796 } 797 798 #ifdef MAC 799 mac_ip6q_update(m, q6); 800 #endif 801 802 /* 803 * Stick new segment in its place; check for complete reassembly. 804 * If not complete, check fragment limit. Move to front of packet 805 * queue, as we are the most recently active fragmented packet. 806 */ 807 if (af6 != NULL) 808 TAILQ_INSERT_BEFORE(af6, ip6af, ip6af_tq); 809 else 810 TAILQ_INSERT_TAIL(&q6->ip6q_frags, ip6af, ip6af_tq); 811 postinsert: 812 atomic_add_int(&frag6_nfrags, 1); 813 q6->ip6q_nfrag++; 814 815 plen = 0; 816 TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) { 817 if (af6->ip6af_off != plen) { 818 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) { 819 IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag); 820 frag6_freef(q6, bucket); 821 } 822 IP6QB_UNLOCK(bucket); 823 *mp = NULL; 824 return (IPPROTO_DONE); 825 } 826 plen += af6->ip6af_frglen; 827 } 828 af6 = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead); 829 if (af6->ip6af_mff) { 830 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) { 831 IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag); 832 frag6_freef(q6, bucket); 833 } 834 IP6QB_UNLOCK(bucket); 835 *mp = NULL; 836 return (IPPROTO_DONE); 837 } 838 839 /* Reassembly is complete; concatenate fragments. */ 840 ip6af = TAILQ_FIRST(&q6->ip6q_frags); 841 t = m = ip6af->ip6af_m; 842 TAILQ_REMOVE(&q6->ip6q_frags, ip6af, ip6af_tq); 843 while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) { 844 m->m_pkthdr.csum_flags &= 845 af6->ip6af_m->m_pkthdr.csum_flags; 846 m->m_pkthdr.csum_data += 847 af6->ip6af_m->m_pkthdr.csum_data; 848 849 TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq); 850 t = m_last(t); 851 m_adj(af6->ip6af_m, af6->ip6af_offset); 852 m_demote_pkthdr(af6->ip6af_m); 853 m_cat(t, af6->ip6af_m); 854 free(af6, M_FRAG6); 855 } 856 857 while (m->m_pkthdr.csum_data & 0xffff0000) 858 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) + 859 (m->m_pkthdr.csum_data >> 16); 860 861 /* Adjust offset to point where the original next header starts. */ 862 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag); 863 free(ip6af, M_FRAG6); 864 ip6 = mtod(m, struct ip6_hdr *); 865 ip6->ip6_plen = htons((u_short)plen + offset - sizeof(struct ip6_hdr)); 866 if (q6->ip6q_ecn == IPTOS_ECN_CE) 867 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20); 868 nxt = q6->ip6q_nxt; 869 870 TAILQ_REMOVE(head, q6, ip6q_tq); 871 V_ip6qb[bucket].count--; 872 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag); 873 874 ip6_deletefraghdr(m, offset, M_NOWAIT); 875 876 /* Set nxt(-hdr field value) to the original value. */ 877 m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t), 878 (caddr_t)&nxt); 879 880 #ifdef MAC 881 mac_ip6q_reassemble(q6, m); 882 mac_ip6q_destroy(q6); 883 #endif 884 free(q6, M_FRAG6); 885 atomic_subtract_int(&V_frag6_nfragpackets, 1); 886 887 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */ 888 889 plen = 0; 890 for (t = m; t; t = t->m_next) 891 plen += t->m_len; 892 m->m_pkthdr.len = plen; 893 /* Set a valid receive interface pointer. */ 894 m->m_pkthdr.rcvif = srcifp; 895 } 896 897 #ifdef RSS 898 mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc), 899 M_NOWAIT); 900 if (mtag == NULL) 901 goto dropfrag; 902 903 ip6dc = (struct ip6_direct_ctx *)(mtag + 1); 904 ip6dc->ip6dc_nxt = nxt; 905 ip6dc->ip6dc_off = offset; 906 907 m_tag_prepend(m, mtag); 908 #endif 909 910 IP6QB_UNLOCK(bucket); 911 IP6STAT_INC(ip6s_reassembled); 912 in6_ifstat_inc(dstifp, ifs6_reass_ok); 913 914 #ifdef RSS 915 /* Queue/dispatch for reprocessing. */ 916 netisr_dispatch(NETISR_IPV6_DIRECT, m); 917 *mp = NULL; 918 return (IPPROTO_DONE); 919 #endif 920 921 /* Tell launch routine the next header. */ 922 *mp = m; 923 *offp = offset; 924 925 return (nxt); 926 927 dropfrag: 928 IP6QB_UNLOCK(bucket); 929 dropfrag2: 930 in6_ifstat_inc(dstifp, ifs6_reass_fail); 931 IP6STAT_INC(ip6s_fragdropped); 932 m_freem(m); 933 *mp = NULL; 934 return (IPPROTO_DONE); 935 } 936 937 /* 938 * IPv6 reassembling timer processing; 939 * if a timer expires on a reassembly queue, discard it. 940 */ 941 static struct callout frag6_callout; 942 static void 943 frag6_slowtimo(void *arg __unused) 944 { 945 VNET_ITERATOR_DECL(vnet_iter); 946 struct ip6qhead *head; 947 struct ip6q *q6, *q6tmp; 948 uint32_t bucket; 949 950 if (atomic_load_int(&frag6_nfrags) == 0) 951 goto done; 952 953 VNET_LIST_RLOCK_NOSLEEP(); 954 VNET_FOREACH(vnet_iter) { 955 CURVNET_SET(vnet_iter); 956 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) { 957 if (V_ip6qb[bucket].count == 0) 958 continue; 959 IP6QB_LOCK(bucket); 960 head = IP6QB_HEAD(bucket); 961 TAILQ_FOREACH_SAFE(q6, head, ip6q_tq, q6tmp) 962 if (--q6->ip6q_ttl == 0) { 963 IP6STAT_ADD(ip6s_fragtimeout, 964 q6->ip6q_nfrag); 965 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 966 frag6_freef(q6, bucket); 967 } 968 /* 969 * If we are over the maximum number of fragments 970 * (due to the limit being lowered), drain off 971 * enough to get down to the new limit. 972 * Note that we drain all reassembly queues if 973 * maxfragpackets is 0 (fragmentation is disabled), 974 * and do not enforce a limit when maxfragpackets 975 * is negative. 976 */ 977 while ((V_ip6_maxfragpackets == 0 || 978 (V_ip6_maxfragpackets > 0 && 979 V_ip6qb[bucket].count > V_ip6_maxfragbucketsize)) && 980 (q6 = TAILQ_LAST(head, ip6qhead)) != NULL) { 981 IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag); 982 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 983 frag6_freef(q6, bucket); 984 } 985 IP6QB_UNLOCK(bucket); 986 } 987 /* 988 * If we are still over the maximum number of fragmented 989 * packets, drain off enough to get down to the new limit. 990 */ 991 bucket = 0; 992 while (V_ip6_maxfragpackets >= 0 && 993 atomic_load_int(&V_frag6_nfragpackets) > 994 (u_int)V_ip6_maxfragpackets) { 995 IP6QB_LOCK(bucket); 996 q6 = TAILQ_LAST(IP6QB_HEAD(bucket), ip6qhead); 997 if (q6 != NULL) { 998 IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag); 999 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 1000 frag6_freef(q6, bucket); 1001 } 1002 IP6QB_UNLOCK(bucket); 1003 bucket = (bucket + 1) % IP6REASS_NHASH; 1004 } 1005 CURVNET_RESTORE(); 1006 } 1007 VNET_LIST_RUNLOCK_NOSLEEP(); 1008 done: 1009 callout_reset_sbt(&frag6_callout, SBT_1MS * IP6_CALLOUT_INTERVAL_MS, 1010 SBT_1MS * 10, frag6_slowtimo, NULL, 0); 1011 } 1012 1013 static void 1014 frag6_slowtimo_init(void *arg __unused) 1015 { 1016 1017 callout_init(&frag6_callout, 1); 1018 callout_reset_sbt(&frag6_callout, SBT_1MS * IP6_CALLOUT_INTERVAL_MS, 1019 SBT_1MS * 10, frag6_slowtimo, NULL, 0); 1020 } 1021 SYSINIT(frag6, SI_SUB_VNET_DONE, SI_ORDER_ANY, frag6_slowtimo_init, NULL); 1022 1023 /* 1024 * Eventhandler to adjust limits in case nmbclusters change. 1025 */ 1026 static void 1027 frag6_change(void *tag) 1028 { 1029 VNET_ITERATOR_DECL(vnet_iter); 1030 1031 ip6_maxfrags = IP6_MAXFRAGS; 1032 VNET_LIST_RLOCK_NOSLEEP(); 1033 VNET_FOREACH(vnet_iter) { 1034 CURVNET_SET(vnet_iter); 1035 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS; 1036 frag6_set_bucketsize(); 1037 CURVNET_RESTORE(); 1038 } 1039 VNET_LIST_RUNLOCK_NOSLEEP(); 1040 } 1041 1042 /* 1043 * Initialise reassembly queue and fragment identifier. 1044 */ 1045 void 1046 frag6_init(void) 1047 { 1048 uint32_t bucket; 1049 1050 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS; 1051 frag6_set_bucketsize(); 1052 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) { 1053 TAILQ_INIT(IP6QB_HEAD(bucket)); 1054 mtx_init(&V_ip6qb[bucket].lock, "ip6qb", NULL, MTX_DEF); 1055 V_ip6qb[bucket].count = 0; 1056 } 1057 V_ip6qb_hashseed = arc4random(); 1058 V_ip6_maxfragsperpacket = 64; 1059 #ifdef VIMAGE 1060 V_frag6_on = true; 1061 #endif 1062 if (!IS_DEFAULT_VNET(curvnet)) 1063 return; 1064 1065 ip6_maxfrags = IP6_MAXFRAGS; 1066 EVENTHANDLER_REGISTER(nmbclusters_change, 1067 frag6_change, NULL, EVENTHANDLER_PRI_ANY); 1068 } 1069 1070 /* 1071 * Drain off all datagram fragments. 1072 */ 1073 static void 1074 frag6_drain_one(void) 1075 { 1076 struct ip6q *q6; 1077 uint32_t bucket; 1078 1079 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) { 1080 IP6QB_LOCK(bucket); 1081 while ((q6 = TAILQ_FIRST(IP6QB_HEAD(bucket))) != NULL) { 1082 IP6STAT_INC(ip6s_fragdropped); 1083 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 1084 frag6_freef(q6, bucket); 1085 } 1086 IP6QB_UNLOCK(bucket); 1087 } 1088 } 1089 1090 void 1091 frag6_drain(void) 1092 { 1093 VNET_ITERATOR_DECL(vnet_iter); 1094 1095 VNET_LIST_RLOCK_NOSLEEP(); 1096 VNET_FOREACH(vnet_iter) { 1097 CURVNET_SET(vnet_iter); 1098 frag6_drain_one(); 1099 CURVNET_RESTORE(); 1100 } 1101 VNET_LIST_RUNLOCK_NOSLEEP(); 1102 } 1103 1104 #ifdef VIMAGE 1105 /* 1106 * Clear up IPv6 reassembly structures. 1107 */ 1108 void 1109 frag6_destroy(void) 1110 { 1111 uint32_t bucket; 1112 1113 frag6_drain_one(); 1114 V_frag6_on = false; 1115 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) { 1116 KASSERT(V_ip6qb[bucket].count == 0, 1117 ("%s: V_ip6qb[%d] (%p) count not 0 (%d)", __func__, 1118 bucket, &V_ip6qb[bucket], V_ip6qb[bucket].count)); 1119 mtx_destroy(&V_ip6qb[bucket].lock); 1120 } 1121 } 1122 #endif 1123