1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * INET An implementation of the TCP/IP protocol suite for the LINUX 4 * operating system. INET is implemented using the BSD Socket 5 * interface as the means of communication with the user level. 6 * 7 * The IP fragmentation functionality. 8 * 9 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG> 10 * Alan Cox <alan@lxorguk.ukuu.org.uk> 11 * 12 * Fixes: 13 * Alan Cox : Split from ip.c , see ip_input.c for history. 14 * David S. Miller : Begin massive cleanup... 15 * Andi Kleen : Add sysctls. 16 * xxxx : Overlapfrag bug. 17 * Ultima : ip_expire() kernel panic. 18 * Bill Hawes : Frag accounting and evictor fixes. 19 * John McDonald : 0 length frag bug. 20 * Alexey Kuznetsov: SMP races, threading, cleanup. 21 * Patrick McHardy : LRU queue of frag heads for evictor. 22 */ 23 24 #define pr_fmt(fmt) "IPv4: " fmt 25 26 #include <linux/compiler.h> 27 #include <linux/module.h> 28 #include <linux/types.h> 29 #include <linux/mm.h> 30 #include <linux/jiffies.h> 31 #include <linux/skbuff.h> 32 #include <linux/list.h> 33 #include <linux/ip.h> 34 #include <linux/icmp.h> 35 #include <linux/netdevice.h> 36 #include <linux/jhash.h> 37 #include <linux/random.h> 38 #include <linux/slab.h> 39 #include <net/route.h> 40 #include <net/dst.h> 41 #include <net/sock.h> 42 #include <net/ip.h> 43 #include <net/icmp.h> 44 #include <net/checksum.h> 45 #include <net/inetpeer.h> 46 #include <net/inet_frag.h> 47 #include <linux/tcp.h> 48 #include <linux/udp.h> 49 #include <linux/inet.h> 50 #include <linux/netfilter_ipv4.h> 51 #include <net/inet_ecn.h> 52 #include <net/l3mdev.h> 53 54 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6 55 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c 56 * as well. Or notify me, at least. --ANK 57 */ 58 static const char ip_frag_cache_name[] = "ip4-frags"; 59 60 /* Describe an entry in the "incomplete datagrams" queue. */ 61 struct ipq { 62 struct inet_frag_queue q; 63 64 u8 ecn; /* RFC3168 support */ 65 u16 max_df_size; /* largest frag with DF set seen */ 66 int iif; 67 unsigned int rid; 68 struct inet_peer *peer; 69 }; 70 71 static u8 ip4_frag_ecn(u8 tos) 72 { 73 return 1 << (tos & INET_ECN_MASK); 74 } 75 76 static struct inet_frags ip4_frags; 77 78 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb, 79 struct sk_buff *prev_tail, struct net_device *dev, 80 int *refs); 81 82 83 static void ip4_frag_init(struct inet_frag_queue *q, const void *a) 84 { 85 struct ipq *qp = container_of(q, struct ipq, q); 86 const struct frag_v4_compare_key *key = a; 87 struct net *net = q->fqdir->net; 88 struct inet_peer *p = NULL; 89 90 q->key.v4 = *key; 91 qp->ecn = 0; 92 if (q->fqdir->max_dist) { 93 rcu_read_lock(); 94 p = inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif); 95 if (p && !refcount_inc_not_zero(&p->refcnt)) 96 p = NULL; 97 rcu_read_unlock(); 98 } 99 qp->peer = p; 100 } 101 102 static void ip4_frag_free(struct inet_frag_queue *q) 103 { 104 struct ipq *qp; 105 106 qp = container_of(q, struct ipq, q); 107 if (qp->peer) 108 inet_putpeer(qp->peer); 109 } 110 111 static bool frag_expire_skip_icmp(u32 user) 112 { 113 return user == IP_DEFRAG_AF_PACKET || 114 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN, 115 __IP_DEFRAG_CONNTRACK_IN_END) || 116 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN, 117 __IP_DEFRAG_CONNTRACK_BRIDGE_IN); 118 } 119 120 /* 121 * Oops, a fragment queue timed out. Kill it and send an ICMP reply. 122 */ 123 static void ip_expire(struct timer_list *t) 124 { 125 enum skb_drop_reason reason = SKB_DROP_REASON_FRAG_REASM_TIMEOUT; 126 struct inet_frag_queue *frag = timer_container_of(frag, t, timer); 127 const struct iphdr *iph; 128 struct sk_buff *head = NULL; 129 struct net *net; 130 struct ipq *qp; 131 int refs = 1; 132 133 qp = container_of(frag, struct ipq, q); 134 net = qp->q.fqdir->net; 135 136 rcu_read_lock(); 137 spin_lock(&qp->q.lock); 138 139 if (qp->q.flags & INET_FRAG_COMPLETE) 140 goto out; 141 142 qp->q.flags |= INET_FRAG_DROP; 143 inet_frag_kill(&qp->q, &refs); 144 145 /* Paired with WRITE_ONCE() in fqdir_pre_exit(). */ 146 if (READ_ONCE(qp->q.fqdir->dead)) { 147 inet_frag_queue_flush(&qp->q, 0); 148 goto out; 149 } 150 151 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); 152 __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT); 153 154 if (!(qp->q.flags & INET_FRAG_FIRST_IN)) 155 goto out; 156 157 /* sk_buff::dev and sk_buff::rbnode are unionized. So we 158 * pull the head out of the tree in order to be able to 159 * deal with head->dev. 160 */ 161 head = inet_frag_pull_head(&qp->q); 162 if (!head) 163 goto out; 164 head->dev = dev_get_by_index_rcu(net, qp->iif); 165 if (!head->dev) 166 goto out; 167 168 169 /* skb has no dst, perform route lookup again */ 170 iph = ip_hdr(head); 171 reason = ip_route_input_noref(head, iph->daddr, iph->saddr, 172 ip4h_dscp(iph), head->dev); 173 if (reason) 174 goto out; 175 176 /* Only an end host needs to send an ICMP 177 * "Fragment Reassembly Timeout" message, per RFC792. 178 */ 179 reason = SKB_DROP_REASON_FRAG_REASM_TIMEOUT; 180 if (frag_expire_skip_icmp(qp->q.key.v4.user) && 181 (skb_rtable(head)->rt_type != RTN_LOCAL)) 182 goto out; 183 184 spin_unlock(&qp->q.lock); 185 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0); 186 goto out_rcu_unlock; 187 188 out: 189 spin_unlock(&qp->q.lock); 190 out_rcu_unlock: 191 rcu_read_unlock(); 192 kfree_skb_reason(head, reason); 193 inet_frag_putn(&qp->q, refs); 194 } 195 196 /* Find the correct entry in the "incomplete datagrams" queue for 197 * this IP datagram, and create new one, if nothing is found. 198 */ 199 static struct ipq *ip_find(struct net *net, struct iphdr *iph, 200 u32 user, int vif) 201 { 202 struct frag_v4_compare_key key = { 203 .saddr = iph->saddr, 204 .daddr = iph->daddr, 205 .user = user, 206 .vif = vif, 207 .id = iph->id, 208 .protocol = iph->protocol, 209 }; 210 struct inet_frag_queue *q; 211 212 q = inet_frag_find(net->ipv4.fqdir, &key); 213 if (!q) 214 return NULL; 215 216 return container_of(q, struct ipq, q); 217 } 218 219 /* Is the fragment too far ahead to be part of ipq? */ 220 static int ip_frag_too_far(struct ipq *qp) 221 { 222 struct inet_peer *peer = qp->peer; 223 unsigned int max = qp->q.fqdir->max_dist; 224 unsigned int start, end; 225 226 int rc; 227 228 if (!peer || !max) 229 return 0; 230 231 start = qp->rid; 232 end = atomic_inc_return(&peer->rid); 233 qp->rid = end; 234 235 rc = qp->q.fragments_tail && (end - start) > max; 236 237 if (rc) 238 __IP_INC_STATS(qp->q.fqdir->net, IPSTATS_MIB_REASMFAILS); 239 240 return rc; 241 } 242 243 static int ip_frag_reinit(struct ipq *qp) 244 { 245 if (!mod_timer_pending(&qp->q.timer, jiffies + qp->q.fqdir->timeout)) 246 return -ETIMEDOUT; 247 248 inet_frag_queue_flush(&qp->q, SKB_DROP_REASON_FRAG_TOO_FAR); 249 250 qp->q.flags = 0; 251 qp->q.len = 0; 252 qp->q.meat = 0; 253 qp->iif = 0; 254 qp->ecn = 0; 255 256 return 0; 257 } 258 259 /* Add new segment to existing queue. */ 260 static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb, int *refs) 261 { 262 struct net *net = qp->q.fqdir->net; 263 int ihl, end, flags, offset; 264 struct sk_buff *prev_tail; 265 struct net_device *dev; 266 unsigned int fragsize; 267 int err = -ENOENT; 268 SKB_DR(reason); 269 u8 ecn; 270 271 /* If reassembly is already done, @skb must be a duplicate frag. */ 272 if (qp->q.flags & INET_FRAG_COMPLETE) { 273 SKB_DR_SET(reason, DUP_FRAG); 274 goto err; 275 } 276 277 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) && 278 unlikely(ip_frag_too_far(qp)) && 279 unlikely(err = ip_frag_reinit(qp))) { 280 inet_frag_kill(&qp->q, refs); 281 goto err; 282 } 283 284 ecn = ip4_frag_ecn(ip_hdr(skb)->tos); 285 offset = ntohs(ip_hdr(skb)->frag_off); 286 flags = offset & ~IP_OFFSET; 287 offset &= IP_OFFSET; 288 offset <<= 3; /* offset is in 8-byte chunks */ 289 ihl = ip_hdrlen(skb); 290 291 /* Determine the position of this fragment. */ 292 end = offset + skb->len - skb_network_offset(skb) - ihl; 293 err = -EINVAL; 294 295 /* Is this the final fragment? */ 296 if ((flags & IP_MF) == 0) { 297 /* If we already have some bits beyond end 298 * or have different end, the segment is corrupted. 299 */ 300 if (end < qp->q.len || 301 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len)) 302 goto discard_qp; 303 qp->q.flags |= INET_FRAG_LAST_IN; 304 qp->q.len = end; 305 } else { 306 if (end&7) { 307 end &= ~7; 308 if (skb->ip_summed != CHECKSUM_UNNECESSARY) 309 skb->ip_summed = CHECKSUM_NONE; 310 } 311 if (end > qp->q.len) { 312 /* Some bits beyond end -> corruption. */ 313 if (qp->q.flags & INET_FRAG_LAST_IN) 314 goto discard_qp; 315 qp->q.len = end; 316 } 317 } 318 if (end == offset) 319 goto discard_qp; 320 321 err = -ENOMEM; 322 if (!pskb_pull(skb, skb_network_offset(skb) + ihl)) 323 goto discard_qp; 324 325 err = pskb_trim_rcsum(skb, end - offset); 326 if (err) 327 goto discard_qp; 328 329 /* Note : skb->rbnode and skb->dev share the same location. */ 330 dev = skb->dev; 331 /* Makes sure compiler wont do silly aliasing games */ 332 barrier(); 333 334 prev_tail = qp->q.fragments_tail; 335 err = inet_frag_queue_insert(&qp->q, skb, offset, end); 336 if (err) 337 goto insert_error; 338 339 if (dev) 340 qp->iif = dev->ifindex; 341 342 qp->q.stamp = skb->tstamp; 343 qp->q.tstamp_type = skb->tstamp_type; 344 qp->q.meat += skb->len; 345 qp->ecn |= ecn; 346 add_frag_mem_limit(qp->q.fqdir, skb->truesize); 347 if (offset == 0) 348 qp->q.flags |= INET_FRAG_FIRST_IN; 349 350 fragsize = skb->len + ihl; 351 352 if (fragsize > qp->q.max_size) 353 qp->q.max_size = fragsize; 354 355 if (ip_hdr(skb)->frag_off & htons(IP_DF) && 356 fragsize > qp->max_df_size) 357 qp->max_df_size = fragsize; 358 359 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && 360 qp->q.meat == qp->q.len) { 361 unsigned long orefdst = skb->_skb_refdst; 362 363 skb->_skb_refdst = 0UL; 364 err = ip_frag_reasm(qp, skb, prev_tail, dev, refs); 365 skb->_skb_refdst = orefdst; 366 if (err) 367 inet_frag_kill(&qp->q, refs); 368 return err; 369 } 370 371 skb_dst_drop(skb); 372 skb_orphan(skb); 373 return -EINPROGRESS; 374 375 insert_error: 376 if (err == IPFRAG_DUP) { 377 SKB_DR_SET(reason, DUP_FRAG); 378 err = -EINVAL; 379 goto err; 380 } 381 if (err == IPFRAG_OVERLAP) 382 __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS); 383 err = -EINVAL; 384 discard_qp: 385 inet_frag_kill(&qp->q, refs); 386 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); 387 err: 388 kfree_skb_reason(skb, reason); 389 return err; 390 } 391 392 static bool ip_frag_coalesce_ok(const struct ipq *qp) 393 { 394 return qp->q.key.v4.user == IP_DEFRAG_LOCAL_DELIVER; 395 } 396 397 /* Build a new IP datagram from all its fragments. */ 398 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb, 399 struct sk_buff *prev_tail, struct net_device *dev, 400 int *refs) 401 { 402 struct net *net = qp->q.fqdir->net; 403 struct iphdr *iph; 404 void *reasm_data; 405 int len, err; 406 u8 ecn; 407 408 inet_frag_kill(&qp->q, refs); 409 410 ecn = ip_frag_ecn_table[qp->ecn]; 411 if (unlikely(ecn == 0xff)) { 412 err = -EINVAL; 413 goto out_fail; 414 } 415 416 /* Check the size on the first fragment, which will become the head: 417 * inet_frag_reasm_finish() must follow inet_frag_reasm_prepare(). 418 */ 419 len = ip_hdrlen(skb_rb_first(&qp->q.rb_fragments)) + qp->q.len; 420 err = -E2BIG; 421 if (len > 65535) 422 goto out_oversize; 423 424 /* Make the one we just received the head. */ 425 reasm_data = inet_frag_reasm_prepare(&qp->q, skb, prev_tail); 426 if (!reasm_data) 427 goto out_nomem; 428 429 inet_frag_reasm_finish(&qp->q, skb, reasm_data, 430 ip_frag_coalesce_ok(qp)); 431 432 skb->dev = dev; 433 IPCB(skb)->frag_max_size = max(qp->max_df_size, qp->q.max_size); 434 435 iph = ip_hdr(skb); 436 iph->tot_len = htons(len); 437 iph->tos |= ecn; 438 439 /* When we set IP_DF on a refragmented skb we must also force a 440 * call to ip_fragment to avoid forwarding a DF-skb of size s while 441 * original sender only sent fragments of size f (where f < s). 442 * 443 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest 444 * frag seen to avoid sending tiny DF-fragments in case skb was built 445 * from one very small df-fragment and one large non-df frag. 446 */ 447 if (qp->max_df_size == qp->q.max_size) { 448 IPCB(skb)->flags |= IPSKB_FRAG_PMTU; 449 iph->frag_off = htons(IP_DF); 450 } else { 451 iph->frag_off = 0; 452 } 453 454 ip_send_check(iph); 455 456 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS); 457 qp->q.rb_fragments = RB_ROOT; 458 qp->q.fragments_tail = NULL; 459 qp->q.last_run_head = NULL; 460 return 0; 461 462 out_nomem: 463 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp); 464 err = -ENOMEM; 465 goto out_fail; 466 out_oversize: 467 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr); 468 out_fail: 469 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); 470 return err; 471 } 472 473 /* Process an incoming IP datagram fragment. */ 474 int ip_defrag(struct net *net, struct sk_buff *skb, u32 user) 475 { 476 struct net_device *dev; 477 struct ipq *qp; 478 int vif; 479 480 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS); 481 482 /* Lookup (or create) queue header */ 483 rcu_read_lock(); 484 dev = skb->dev ? : skb_dst_dev_rcu(skb); 485 vif = l3mdev_master_ifindex_rcu(dev); 486 qp = ip_find(net, ip_hdr(skb), user, vif); 487 if (qp) { 488 int ret, refs = 0; 489 490 spin_lock(&qp->q.lock); 491 492 ret = ip_frag_queue(qp, skb, &refs); 493 494 spin_unlock(&qp->q.lock); 495 rcu_read_unlock(); 496 inet_frag_putn(&qp->q, refs); 497 return ret; 498 } 499 rcu_read_unlock(); 500 501 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); 502 kfree_skb(skb); 503 return -ENOMEM; 504 } 505 EXPORT_SYMBOL(ip_defrag); 506 507 struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user) 508 { 509 struct iphdr iph; 510 int netoff; 511 u32 len; 512 513 if (skb->protocol != htons(ETH_P_IP)) 514 return skb; 515 516 netoff = skb_network_offset(skb); 517 518 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0) 519 return skb; 520 521 if (iph.ihl < 5 || iph.version != 4) 522 return skb; 523 524 len = ntohs(iph.tot_len); 525 if (skb->len < netoff + len || len < (iph.ihl * 4)) 526 return skb; 527 528 if (ip_is_fragment(&iph)) { 529 skb = skb_share_check(skb, GFP_ATOMIC); 530 if (skb) { 531 if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) { 532 kfree_skb(skb); 533 return NULL; 534 } 535 if (pskb_trim_rcsum(skb, netoff + len)) { 536 kfree_skb(skb); 537 return NULL; 538 } 539 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 540 if (ip_defrag(net, skb, user)) 541 return NULL; 542 skb_clear_hash(skb); 543 } 544 } 545 return skb; 546 } 547 EXPORT_SYMBOL(ip_check_defrag); 548 549 #ifdef CONFIG_SYSCTL 550 static int dist_min; 551 552 static struct ctl_table ip4_frags_ns_ctl_table[] = { 553 { 554 .procname = "ipfrag_high_thresh", 555 .maxlen = sizeof(unsigned long), 556 .mode = 0644, 557 .proc_handler = proc_doulongvec_minmax, 558 }, 559 { 560 .procname = "ipfrag_low_thresh", 561 .maxlen = sizeof(unsigned long), 562 .mode = 0644, 563 .proc_handler = proc_doulongvec_minmax, 564 }, 565 { 566 .procname = "ipfrag_time", 567 .maxlen = sizeof(int), 568 .mode = 0644, 569 .proc_handler = proc_dointvec_jiffies, 570 }, 571 { 572 .procname = "ipfrag_max_dist", 573 .maxlen = sizeof(int), 574 .mode = 0644, 575 .proc_handler = proc_dointvec_minmax, 576 .extra1 = &dist_min, 577 }, 578 }; 579 580 /* secret interval has been deprecated */ 581 static int ip4_frags_secret_interval_unused; 582 static struct ctl_table ip4_frags_ctl_table[] = { 583 { 584 .procname = "ipfrag_secret_interval", 585 .data = &ip4_frags_secret_interval_unused, 586 .maxlen = sizeof(int), 587 .mode = 0644, 588 .proc_handler = proc_dointvec_jiffies, 589 }, 590 }; 591 592 static int __net_init ip4_frags_ns_ctl_register(struct net *net) 593 { 594 struct ctl_table *table; 595 struct ctl_table_header *hdr; 596 597 table = ip4_frags_ns_ctl_table; 598 if (!net_eq(net, &init_net)) { 599 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL); 600 if (!table) 601 goto err_alloc; 602 603 } 604 table[0].data = &net->ipv4.fqdir->high_thresh; 605 table[0].extra1 = &net->ipv4.fqdir->low_thresh; 606 table[1].data = &net->ipv4.fqdir->low_thresh; 607 table[1].extra2 = &net->ipv4.fqdir->high_thresh; 608 table[2].data = &net->ipv4.fqdir->timeout; 609 table[3].data = &net->ipv4.fqdir->max_dist; 610 611 hdr = register_net_sysctl_sz(net, "net/ipv4", table, 612 ARRAY_SIZE(ip4_frags_ns_ctl_table)); 613 if (!hdr) 614 goto err_reg; 615 616 net->ipv4.frags_hdr = hdr; 617 return 0; 618 619 err_reg: 620 if (!net_eq(net, &init_net)) 621 kfree(table); 622 err_alloc: 623 return -ENOMEM; 624 } 625 626 static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net) 627 { 628 const struct ctl_table *table; 629 630 table = net->ipv4.frags_hdr->ctl_table_arg; 631 unregister_net_sysctl_table(net->ipv4.frags_hdr); 632 kfree(table); 633 } 634 635 static void __init ip4_frags_ctl_register(void) 636 { 637 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table); 638 } 639 #else 640 static int ip4_frags_ns_ctl_register(struct net *net) 641 { 642 return 0; 643 } 644 645 static void ip4_frags_ns_ctl_unregister(struct net *net) 646 { 647 } 648 649 static void __init ip4_frags_ctl_register(void) 650 { 651 } 652 #endif 653 654 static int __net_init ipv4_frags_init_net(struct net *net) 655 { 656 int res; 657 658 res = fqdir_init(&net->ipv4.fqdir, &ip4_frags, net); 659 if (res < 0) 660 return res; 661 /* Fragment cache limits. 662 * 663 * The fragment memory accounting code, (tries to) account for 664 * the real memory usage, by measuring both the size of frag 665 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue)) 666 * and the SKB's truesize. 667 * 668 * A 64K fragment consumes 129736 bytes (44*2944)+200 669 * (1500 truesize == 2944, sizeof(struct ipq) == 200) 670 * 671 * We will commit 4MB at one time. Should we cross that limit 672 * we will prune down to 3MB, making room for approx 8 big 64K 673 * fragments 8x128k. 674 */ 675 net->ipv4.fqdir->high_thresh = 4 * 1024 * 1024; 676 net->ipv4.fqdir->low_thresh = 3 * 1024 * 1024; 677 /* 678 * Important NOTE! Fragment queue must be destroyed before MSL expires. 679 * RFC791 is wrong proposing to prolongate timer each fragment arrival 680 * by TTL. 681 */ 682 net->ipv4.fqdir->timeout = IP_FRAG_TIME; 683 684 net->ipv4.fqdir->max_dist = 64; 685 686 res = ip4_frags_ns_ctl_register(net); 687 if (res < 0) 688 fqdir_exit(net->ipv4.fqdir); 689 return res; 690 } 691 692 static void __net_exit ipv4_frags_pre_exit_net(struct net *net) 693 { 694 fqdir_pre_exit(net->ipv4.fqdir); 695 } 696 697 static void __net_exit ipv4_frags_exit_net(struct net *net) 698 { 699 ip4_frags_ns_ctl_unregister(net); 700 fqdir_exit(net->ipv4.fqdir); 701 } 702 703 static struct pernet_operations ip4_frags_ops = { 704 .init = ipv4_frags_init_net, 705 .pre_exit = ipv4_frags_pre_exit_net, 706 .exit = ipv4_frags_exit_net, 707 }; 708 709 710 static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed) 711 { 712 return jhash2(data, 713 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed); 714 } 715 716 static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed) 717 { 718 const struct inet_frag_queue *fq = data; 719 720 return jhash2((const u32 *)&fq->key.v4, 721 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed); 722 } 723 724 static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr) 725 { 726 const struct frag_v4_compare_key *key = arg->key; 727 const struct inet_frag_queue *fq = ptr; 728 729 return !!memcmp(&fq->key, key, sizeof(*key)); 730 } 731 732 static const struct rhashtable_params ip4_rhash_params = { 733 .head_offset = offsetof(struct inet_frag_queue, node), 734 .key_offset = offsetof(struct inet_frag_queue, key), 735 .key_len = sizeof(struct frag_v4_compare_key), 736 .hashfn = ip4_key_hashfn, 737 .obj_hashfn = ip4_obj_hashfn, 738 .obj_cmpfn = ip4_obj_cmpfn, 739 .automatic_shrinking = true, 740 }; 741 742 void __init ipfrag_init(void) 743 { 744 ip4_frags.constructor = ip4_frag_init; 745 ip4_frags.destructor = ip4_frag_free; 746 ip4_frags.qsize = sizeof(struct ipq); 747 ip4_frags.frag_expire = ip_expire; 748 ip4_frags.frags_cache_name = ip_frag_cache_name; 749 ip4_frags.rhash_params = ip4_rhash_params; 750 if (inet_frags_init(&ip4_frags)) 751 panic("IP: failed to allocate ip4_frags cache\n"); 752 ip4_frags_ctl_register(); 753 register_pernet_subsys(&ip4_frags_ops); 754 } 755