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