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