1 /*- 2 * Copyright (c) 2015 Gleb Smirnoff <[email protected]> 3 * Copyright (c) 2015 Adrian Chadd <[email protected]> 4 * Copyright (c) 1982, 1986, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 4. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_rss.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/eventhandler.h> 42 #include <sys/hash.h> 43 #include <sys/mbuf.h> 44 #include <sys/malloc.h> 45 #include <sys/limits.h> 46 #include <sys/lock.h> 47 #include <sys/mutex.h> 48 #include <sys/sysctl.h> 49 50 #include <net/rss_config.h> 51 #include <net/netisr.h> 52 #include <net/vnet.h> 53 54 #include <netinet/in.h> 55 #include <netinet/ip.h> 56 #include <netinet/ip_var.h> 57 #include <netinet/in_rss.h> 58 #ifdef MAC 59 #include <security/mac/mac_framework.h> 60 #endif 61 62 SYSCTL_DECL(_net_inet_ip); 63 64 /* 65 * Reassembly headers are stored in hash buckets. 66 */ 67 #define IPREASS_NHASH_LOG2 10 68 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) 69 #define IPREASS_HMASK (IPREASS_NHASH - 1) 70 71 struct ipqbucket { 72 TAILQ_HEAD(ipqhead, ipq) head; 73 struct mtx lock; 74 int count; 75 }; 76 77 static VNET_DEFINE(struct ipqbucket, ipq[IPREASS_NHASH]); 78 #define V_ipq VNET(ipq) 79 static VNET_DEFINE(uint32_t, ipq_hashseed); 80 #define V_ipq_hashseed VNET(ipq_hashseed) 81 82 #define IPQ_LOCK(i) mtx_lock(&V_ipq[i].lock) 83 #define IPQ_TRYLOCK(i) mtx_trylock(&V_ipq[i].lock) 84 #define IPQ_UNLOCK(i) mtx_unlock(&V_ipq[i].lock) 85 #define IPQ_LOCK_ASSERT(i) mtx_assert(&V_ipq[i].lock, MA_OWNED) 86 87 static VNET_DEFINE(int, ipreass_maxbucketsize); 88 #define V_ipreass_maxbucketsize VNET(ipreass_maxbucketsize) 89 90 void ipreass_init(void); 91 void ipreass_drain(void); 92 void ipreass_slowtimo(void); 93 #ifdef VIMAGE 94 void ipreass_destroy(void); 95 #endif 96 static int sysctl_maxfragpackets(SYSCTL_HANDLER_ARGS); 97 static int sysctl_maxfragbucketsize(SYSCTL_HANDLER_ARGS); 98 static void ipreass_zone_change(void *); 99 static void ipreass_drain_tomax(void); 100 static void ipq_free(struct ipqbucket *, struct ipq *); 101 static struct ipq * ipq_reuse(int); 102 103 static inline void 104 ipq_timeout(struct ipqbucket *bucket, struct ipq *fp) 105 { 106 107 IPSTAT_ADD(ips_fragtimeout, fp->ipq_nfrags); 108 ipq_free(bucket, fp); 109 } 110 111 static inline void 112 ipq_drop(struct ipqbucket *bucket, struct ipq *fp) 113 { 114 115 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags); 116 ipq_free(bucket, fp); 117 } 118 119 /* 120 * By default, limit the number of IP fragments across all reassembly 121 * queues to 1/32 of the total number of mbuf clusters. 122 * 123 * Limit the total number of reassembly queues per VNET to the 124 * IP fragment limit, but ensure the limit will not allow any bucket 125 * to grow above 100 items. (The bucket limit is 126 * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct 127 * multiplier to reach a 100-item limit.) 128 * The 100-item limit was chosen as brief testing seems to show that 129 * this produces "reasonable" performance on some subset of systems 130 * under DoS attack. 131 */ 132 #define IP_MAXFRAGS (nmbclusters / 32) 133 #define IP_MAXFRAGPACKETS (imin(IP_MAXFRAGS, IPREASS_NHASH * 50)) 134 135 static int maxfrags; 136 static volatile u_int nfrags; 137 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfrags, CTLFLAG_RW, 138 &maxfrags, 0, 139 "Maximum number of IPv4 fragments allowed across all reassembly queues"); 140 SYSCTL_UINT(_net_inet_ip, OID_AUTO, curfrags, CTLFLAG_RD, 141 __DEVOLATILE(u_int *, &nfrags), 0, 142 "Current number of IPv4 fragments across all reassembly queues"); 143 144 static VNET_DEFINE(uma_zone_t, ipq_zone); 145 #define V_ipq_zone VNET(ipq_zone) 146 SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_VNET | 147 CTLTYPE_INT | CTLFLAG_RW, NULL, 0, sysctl_maxfragpackets, "I", 148 "Maximum number of IPv4 fragment reassembly queue entries"); 149 SYSCTL_UMA_CUR(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_VNET, 150 &VNET_NAME(ipq_zone), 151 "Current number of IPv4 fragment reassembly queue entries"); 152 153 static VNET_DEFINE(int, noreass); 154 #define V_noreass VNET(noreass) 155 156 static VNET_DEFINE(int, maxfragsperpacket); 157 #define V_maxfragsperpacket VNET(maxfragsperpacket) 158 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_VNET | CTLFLAG_RW, 159 &VNET_NAME(maxfragsperpacket), 0, 160 "Maximum number of IPv4 fragments allowed per packet"); 161 SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragbucketsize, 162 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, 163 sysctl_maxfragbucketsize, "I", 164 "Maximum number of IPv4 fragment reassembly queue entries per bucket"); 165 166 /* 167 * Take incoming datagram fragment and try to reassemble it into 168 * whole datagram. If the argument is the first fragment or one 169 * in between the function will return NULL and store the mbuf 170 * in the fragment chain. If the argument is the last fragment 171 * the packet will be reassembled and the pointer to the new 172 * mbuf returned for further processing. Only m_tags attached 173 * to the first packet/fragment are preserved. 174 * The IP header is *NOT* adjusted out of iplen. 175 */ 176 #define M_IP_FRAG M_PROTO9 177 struct mbuf * 178 ip_reass(struct mbuf *m) 179 { 180 struct ip *ip; 181 struct mbuf *p, *q, *nq, *t; 182 struct ipq *fp; 183 struct ipqhead *head; 184 int i, hlen, next, tmpmax; 185 u_int8_t ecn, ecn0; 186 uint32_t hash, hashkey[3]; 187 #ifdef RSS 188 uint32_t rss_hash, rss_type; 189 #endif 190 191 /* 192 * If no reassembling or maxfragsperpacket are 0, 193 * never accept fragments. 194 * Also, drop packet if it would exceed the maximum 195 * number of fragments. 196 */ 197 tmpmax = maxfrags; 198 if (V_noreass == 1 || V_maxfragsperpacket == 0 || 199 (tmpmax >= 0 && nfrags >= (u_int)tmpmax)) { 200 IPSTAT_INC(ips_fragments); 201 IPSTAT_INC(ips_fragdropped); 202 m_freem(m); 203 return (NULL); 204 } 205 206 ip = mtod(m, struct ip *); 207 hlen = ip->ip_hl << 2; 208 209 /* 210 * Adjust ip_len to not reflect header, 211 * convert offset of this to bytes. 212 */ 213 ip->ip_len = htons(ntohs(ip->ip_len) - hlen); 214 if (ip->ip_off & htons(IP_MF)) { 215 /* 216 * Make sure that fragments have a data length 217 * that's a non-zero multiple of 8 bytes. 218 */ 219 if (ip->ip_len == htons(0) || (ntohs(ip->ip_len) & 0x7) != 0) { 220 IPSTAT_INC(ips_toosmall); /* XXX */ 221 IPSTAT_INC(ips_fragdropped); 222 m_freem(m); 223 return (NULL); 224 } 225 m->m_flags |= M_IP_FRAG; 226 } else 227 m->m_flags &= ~M_IP_FRAG; 228 ip->ip_off = htons(ntohs(ip->ip_off) << 3); 229 230 /* 231 * Attempt reassembly; if it succeeds, proceed. 232 * ip_reass() will return a different mbuf. 233 */ 234 IPSTAT_INC(ips_fragments); 235 m->m_pkthdr.PH_loc.ptr = ip; 236 237 /* 238 * Presence of header sizes in mbufs 239 * would confuse code below. 240 */ 241 m->m_data += hlen; 242 m->m_len -= hlen; 243 244 hashkey[0] = ip->ip_src.s_addr; 245 hashkey[1] = ip->ip_dst.s_addr; 246 hashkey[2] = (uint32_t)ip->ip_p << 16; 247 hashkey[2] += ip->ip_id; 248 hash = jenkins_hash32(hashkey, nitems(hashkey), V_ipq_hashseed); 249 hash &= IPREASS_HMASK; 250 head = &V_ipq[hash].head; 251 IPQ_LOCK(hash); 252 253 /* 254 * Look for queue of fragments 255 * of this datagram. 256 */ 257 TAILQ_FOREACH(fp, head, ipq_list) 258 if (ip->ip_id == fp->ipq_id && 259 ip->ip_src.s_addr == fp->ipq_src.s_addr && 260 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 261 #ifdef MAC 262 mac_ipq_match(m, fp) && 263 #endif 264 ip->ip_p == fp->ipq_p) 265 break; 266 /* 267 * If first fragment to arrive, create a reassembly queue. 268 */ 269 if (fp == NULL) { 270 if (V_ipq[hash].count < V_ipreass_maxbucketsize) 271 fp = uma_zalloc(V_ipq_zone, M_NOWAIT); 272 if (fp == NULL) 273 fp = ipq_reuse(hash); 274 if (fp == NULL) 275 goto dropfrag; 276 #ifdef MAC 277 if (mac_ipq_init(fp, M_NOWAIT) != 0) { 278 uma_zfree(V_ipq_zone, fp); 279 fp = NULL; 280 goto dropfrag; 281 } 282 mac_ipq_create(m, fp); 283 #endif 284 TAILQ_INSERT_HEAD(head, fp, ipq_list); 285 V_ipq[hash].count++; 286 fp->ipq_nfrags = 1; 287 atomic_add_int(&nfrags, 1); 288 fp->ipq_ttl = IPFRAGTTL; 289 fp->ipq_p = ip->ip_p; 290 fp->ipq_id = ip->ip_id; 291 fp->ipq_src = ip->ip_src; 292 fp->ipq_dst = ip->ip_dst; 293 fp->ipq_frags = m; 294 m->m_nextpkt = NULL; 295 goto done; 296 } else { 297 fp->ipq_nfrags++; 298 atomic_add_int(&nfrags, 1); 299 #ifdef MAC 300 mac_ipq_update(m, fp); 301 #endif 302 } 303 304 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.PH_loc.ptr)) 305 306 /* 307 * Handle ECN by comparing this segment with the first one; 308 * if CE is set, do not lose CE. 309 * drop if CE and not-ECT are mixed for the same packet. 310 */ 311 ecn = ip->ip_tos & IPTOS_ECN_MASK; 312 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; 313 if (ecn == IPTOS_ECN_CE) { 314 if (ecn0 == IPTOS_ECN_NOTECT) 315 goto dropfrag; 316 if (ecn0 != IPTOS_ECN_CE) 317 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; 318 } 319 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 320 goto dropfrag; 321 322 /* 323 * Find a segment which begins after this one does. 324 */ 325 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 326 if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off)) 327 break; 328 329 /* 330 * If there is a preceding segment, it may provide some of 331 * our data already. If so, drop the data from the incoming 332 * segment. If it provides all of our data, drop us, otherwise 333 * stick new segment in the proper place. 334 * 335 * If some of the data is dropped from the preceding 336 * segment, then it's checksum is invalidated. 337 */ 338 if (p) { 339 i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) - 340 ntohs(ip->ip_off); 341 if (i > 0) { 342 if (i >= ntohs(ip->ip_len)) 343 goto dropfrag; 344 m_adj(m, i); 345 m->m_pkthdr.csum_flags = 0; 346 ip->ip_off = htons(ntohs(ip->ip_off) + i); 347 ip->ip_len = htons(ntohs(ip->ip_len) - i); 348 } 349 m->m_nextpkt = p->m_nextpkt; 350 p->m_nextpkt = m; 351 } else { 352 m->m_nextpkt = fp->ipq_frags; 353 fp->ipq_frags = m; 354 } 355 356 /* 357 * While we overlap succeeding segments trim them or, 358 * if they are completely covered, dequeue them. 359 */ 360 for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) > 361 ntohs(GETIP(q)->ip_off); q = nq) { 362 i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) - 363 ntohs(GETIP(q)->ip_off); 364 if (i < ntohs(GETIP(q)->ip_len)) { 365 GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i); 366 GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i); 367 m_adj(q, i); 368 q->m_pkthdr.csum_flags = 0; 369 break; 370 } 371 nq = q->m_nextpkt; 372 m->m_nextpkt = nq; 373 IPSTAT_INC(ips_fragdropped); 374 fp->ipq_nfrags--; 375 atomic_subtract_int(&nfrags, 1); 376 m_freem(q); 377 } 378 379 /* 380 * Check for complete reassembly and perform frag per packet 381 * limiting. 382 * 383 * Frag limiting is performed here so that the nth frag has 384 * a chance to complete the packet before we drop the packet. 385 * As a result, n+1 frags are actually allowed per packet, but 386 * only n will ever be stored. (n = maxfragsperpacket.) 387 * 388 */ 389 next = 0; 390 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 391 if (ntohs(GETIP(q)->ip_off) != next) { 392 if (fp->ipq_nfrags > V_maxfragsperpacket) 393 ipq_drop(&V_ipq[hash], fp); 394 goto done; 395 } 396 next += ntohs(GETIP(q)->ip_len); 397 } 398 /* Make sure the last packet didn't have the IP_MF flag */ 399 if (p->m_flags & M_IP_FRAG) { 400 if (fp->ipq_nfrags > V_maxfragsperpacket) 401 ipq_drop(&V_ipq[hash], fp); 402 goto done; 403 } 404 405 /* 406 * Reassembly is complete. Make sure the packet is a sane size. 407 */ 408 q = fp->ipq_frags; 409 ip = GETIP(q); 410 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) { 411 IPSTAT_INC(ips_toolong); 412 ipq_drop(&V_ipq[hash], fp); 413 goto done; 414 } 415 416 /* 417 * Concatenate fragments. 418 */ 419 m = q; 420 t = m->m_next; 421 m->m_next = NULL; 422 m_cat(m, t); 423 nq = q->m_nextpkt; 424 q->m_nextpkt = NULL; 425 for (q = nq; q != NULL; q = nq) { 426 nq = q->m_nextpkt; 427 q->m_nextpkt = NULL; 428 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 429 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 430 m_cat(m, q); 431 } 432 /* 433 * In order to do checksumming faster we do 'end-around carry' here 434 * (and not in for{} loop), though it implies we are not going to 435 * reassemble more than 64k fragments. 436 */ 437 while (m->m_pkthdr.csum_data & 0xffff0000) 438 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) + 439 (m->m_pkthdr.csum_data >> 16); 440 atomic_subtract_int(&nfrags, fp->ipq_nfrags); 441 #ifdef MAC 442 mac_ipq_reassemble(fp, m); 443 mac_ipq_destroy(fp); 444 #endif 445 446 /* 447 * Create header for new ip packet by modifying header of first 448 * packet; dequeue and discard fragment reassembly header. 449 * Make header visible. 450 */ 451 ip->ip_len = htons((ip->ip_hl << 2) + next); 452 ip->ip_src = fp->ipq_src; 453 ip->ip_dst = fp->ipq_dst; 454 TAILQ_REMOVE(head, fp, ipq_list); 455 V_ipq[hash].count--; 456 uma_zfree(V_ipq_zone, fp); 457 m->m_len += (ip->ip_hl << 2); 458 m->m_data -= (ip->ip_hl << 2); 459 /* some debugging cruft by sklower, below, will go away soon */ 460 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */ 461 m_fixhdr(m); 462 IPSTAT_INC(ips_reassembled); 463 IPQ_UNLOCK(hash); 464 465 #ifdef RSS 466 /* 467 * Query the RSS layer for the flowid / flowtype for the 468 * mbuf payload. 469 * 470 * For now, just assume we have to calculate a new one. 471 * Later on we should check to see if the assigned flowid matches 472 * what RSS wants for the given IP protocol and if so, just keep it. 473 * 474 * We then queue into the relevant netisr so it can be dispatched 475 * to the correct CPU. 476 * 477 * Note - this may return 1, which means the flowid in the mbuf 478 * is correct for the configured RSS hash types and can be used. 479 */ 480 if (rss_mbuf_software_hash_v4(m, 0, &rss_hash, &rss_type) == 0) { 481 m->m_pkthdr.flowid = rss_hash; 482 M_HASHTYPE_SET(m, rss_type); 483 } 484 485 /* 486 * Queue/dispatch for reprocessing. 487 * 488 * Note: this is much slower than just handling the frame in the 489 * current receive context. It's likely worth investigating 490 * why this is. 491 */ 492 netisr_dispatch(NETISR_IP_DIRECT, m); 493 return (NULL); 494 #endif 495 496 /* Handle in-line */ 497 return (m); 498 499 dropfrag: 500 IPSTAT_INC(ips_fragdropped); 501 if (fp != NULL) { 502 fp->ipq_nfrags--; 503 atomic_subtract_int(&nfrags, 1); 504 } 505 m_freem(m); 506 done: 507 IPQ_UNLOCK(hash); 508 return (NULL); 509 510 #undef GETIP 511 } 512 513 /* 514 * Initialize IP reassembly structures. 515 */ 516 void 517 ipreass_init(void) 518 { 519 int max; 520 521 for (int i = 0; i < IPREASS_NHASH; i++) { 522 TAILQ_INIT(&V_ipq[i].head); 523 mtx_init(&V_ipq[i].lock, "IP reassembly", NULL, 524 MTX_DEF | MTX_DUPOK); 525 V_ipq[i].count = 0; 526 } 527 V_ipq_hashseed = arc4random(); 528 V_maxfragsperpacket = 16; 529 V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL, 530 NULL, UMA_ALIGN_PTR, 0); 531 max = IP_MAXFRAGPACKETS; 532 max = uma_zone_set_max(V_ipq_zone, max); 533 V_ipreass_maxbucketsize = imax(max / (IPREASS_NHASH / 2), 1); 534 535 if (IS_DEFAULT_VNET(curvnet)) { 536 maxfrags = IP_MAXFRAGS; 537 EVENTHANDLER_REGISTER(nmbclusters_change, ipreass_zone_change, 538 NULL, EVENTHANDLER_PRI_ANY); 539 } 540 } 541 542 /* 543 * If a timer expires on a reassembly queue, discard it. 544 */ 545 void 546 ipreass_slowtimo(void) 547 { 548 struct ipq *fp, *tmp; 549 550 for (int i = 0; i < IPREASS_NHASH; i++) { 551 IPQ_LOCK(i); 552 TAILQ_FOREACH_SAFE(fp, &V_ipq[i].head, ipq_list, tmp) 553 if (--fp->ipq_ttl == 0) 554 ipq_timeout(&V_ipq[i], fp); 555 IPQ_UNLOCK(i); 556 } 557 } 558 559 /* 560 * Drain off all datagram fragments. 561 */ 562 void 563 ipreass_drain(void) 564 { 565 566 for (int i = 0; i < IPREASS_NHASH; i++) { 567 IPQ_LOCK(i); 568 while(!TAILQ_EMPTY(&V_ipq[i].head)) 569 ipq_drop(&V_ipq[i], TAILQ_FIRST(&V_ipq[i].head)); 570 KASSERT(V_ipq[i].count == 0, 571 ("%s: V_ipq[%d] count %d (V_ipq=%p)", __func__, i, 572 V_ipq[i].count, V_ipq)); 573 IPQ_UNLOCK(i); 574 } 575 } 576 577 #ifdef VIMAGE 578 /* 579 * Destroy IP reassembly structures. 580 */ 581 void 582 ipreass_destroy(void) 583 { 584 585 ipreass_drain(); 586 uma_zdestroy(V_ipq_zone); 587 for (int i = 0; i < IPREASS_NHASH; i++) 588 mtx_destroy(&V_ipq[i].lock); 589 } 590 #endif 591 592 /* 593 * After maxnipq has been updated, propagate the change to UMA. The UMA zone 594 * max has slightly different semantics than the sysctl, for historical 595 * reasons. 596 */ 597 static void 598 ipreass_drain_tomax(void) 599 { 600 struct ipq *fp; 601 int target; 602 603 /* 604 * Make sure each bucket is under the new limit. If 605 * necessary, drop enough of the oldest elements from 606 * each bucket to get under the new limit. 607 */ 608 for (int i = 0; i < IPREASS_NHASH; i++) { 609 IPQ_LOCK(i); 610 while (V_ipq[i].count > V_ipreass_maxbucketsize && 611 (fp = TAILQ_LAST(&V_ipq[i].head, ipqhead)) != NULL) 612 ipq_timeout(&V_ipq[i], fp); 613 IPQ_UNLOCK(i); 614 } 615 616 /* 617 * If we are over the maximum number of fragments, 618 * drain off enough to get down to the new limit, 619 * stripping off last elements on queues. Every 620 * run we strip the oldest element from each bucket. 621 */ 622 target = uma_zone_get_max(V_ipq_zone); 623 while (uma_zone_get_cur(V_ipq_zone) > target) { 624 for (int i = 0; i < IPREASS_NHASH; i++) { 625 IPQ_LOCK(i); 626 fp = TAILQ_LAST(&V_ipq[i].head, ipqhead); 627 if (fp != NULL) 628 ipq_timeout(&V_ipq[i], fp); 629 IPQ_UNLOCK(i); 630 } 631 } 632 } 633 634 static void 635 ipreass_zone_change(void *tag) 636 { 637 VNET_ITERATOR_DECL(vnet_iter); 638 int max; 639 640 maxfrags = IP_MAXFRAGS; 641 max = IP_MAXFRAGPACKETS; 642 VNET_LIST_RLOCK_NOSLEEP(); 643 VNET_FOREACH(vnet_iter) { 644 CURVNET_SET(vnet_iter); 645 max = uma_zone_set_max(V_ipq_zone, max); 646 V_ipreass_maxbucketsize = imax(max / (IPREASS_NHASH / 2), 1); 647 ipreass_drain_tomax(); 648 CURVNET_RESTORE(); 649 } 650 VNET_LIST_RUNLOCK_NOSLEEP(); 651 } 652 653 /* 654 * Change the limit on the UMA zone, or disable the fragment allocation 655 * at all. Since 0 and -1 is a special values here, we need our own handler, 656 * instead of sysctl_handle_uma_zone_max(). 657 */ 658 static int 659 sysctl_maxfragpackets(SYSCTL_HANDLER_ARGS) 660 { 661 int error, max; 662 663 if (V_noreass == 0) { 664 max = uma_zone_get_max(V_ipq_zone); 665 if (max == 0) 666 max = -1; 667 } else 668 max = 0; 669 error = sysctl_handle_int(oidp, &max, 0, req); 670 if (error || !req->newptr) 671 return (error); 672 if (max > 0) { 673 /* 674 * XXXRW: Might be a good idea to sanity check the argument 675 * and place an extreme upper bound. 676 */ 677 max = uma_zone_set_max(V_ipq_zone, max); 678 V_ipreass_maxbucketsize = imax(max / (IPREASS_NHASH / 2), 1); 679 ipreass_drain_tomax(); 680 V_noreass = 0; 681 } else if (max == 0) { 682 V_noreass = 1; 683 ipreass_drain(); 684 } else if (max == -1) { 685 V_noreass = 0; 686 uma_zone_set_max(V_ipq_zone, 0); 687 V_ipreass_maxbucketsize = INT_MAX; 688 } else 689 return (EINVAL); 690 return (0); 691 } 692 693 /* 694 * Seek for old fragment queue header that can be reused. Try to 695 * reuse a header from currently locked hash bucket. 696 */ 697 static struct ipq * 698 ipq_reuse(int start) 699 { 700 struct ipq *fp; 701 int bucket, i; 702 703 IPQ_LOCK_ASSERT(start); 704 705 for (i = 0; i < IPREASS_NHASH; i++) { 706 bucket = (start + i) % IPREASS_NHASH; 707 if (bucket != start && IPQ_TRYLOCK(bucket) == 0) 708 continue; 709 fp = TAILQ_LAST(&V_ipq[bucket].head, ipqhead); 710 if (fp) { 711 struct mbuf *m; 712 713 IPSTAT_ADD(ips_fragtimeout, fp->ipq_nfrags); 714 atomic_subtract_int(&nfrags, fp->ipq_nfrags); 715 while (fp->ipq_frags) { 716 m = fp->ipq_frags; 717 fp->ipq_frags = m->m_nextpkt; 718 m_freem(m); 719 } 720 TAILQ_REMOVE(&V_ipq[bucket].head, fp, ipq_list); 721 V_ipq[bucket].count--; 722 if (bucket != start) 723 IPQ_UNLOCK(bucket); 724 break; 725 } 726 if (bucket != start) 727 IPQ_UNLOCK(bucket); 728 } 729 IPQ_LOCK_ASSERT(start); 730 return (fp); 731 } 732 733 /* 734 * Free a fragment reassembly header and all associated datagrams. 735 */ 736 static void 737 ipq_free(struct ipqbucket *bucket, struct ipq *fp) 738 { 739 struct mbuf *q; 740 741 atomic_subtract_int(&nfrags, fp->ipq_nfrags); 742 while (fp->ipq_frags) { 743 q = fp->ipq_frags; 744 fp->ipq_frags = q->m_nextpkt; 745 m_freem(q); 746 } 747 TAILQ_REMOVE(&bucket->head, fp, ipq_list); 748 bucket->count--; 749 uma_zfree(V_ipq_zone, fp); 750 } 751 752 /* 753 * Get or set the maximum number of reassembly queues per bucket. 754 */ 755 static int 756 sysctl_maxfragbucketsize(SYSCTL_HANDLER_ARGS) 757 { 758 int error, max; 759 760 max = V_ipreass_maxbucketsize; 761 error = sysctl_handle_int(oidp, &max, 0, req); 762 if (error || !req->newptr) 763 return (error); 764 if (max <= 0) 765 return (EINVAL); 766 V_ipreass_maxbucketsize = max; 767 ipreass_drain_tomax(); 768 return (0); 769 } 770