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 err = -EINVAL; 382 __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS); 383 discard_qp: 384 inet_frag_kill(&qp->q, refs); 385 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); 386 err: 387 kfree_skb_reason(skb, reason); 388 return err; 389 } 390 391 static bool ip_frag_coalesce_ok(const struct ipq *qp) 392 { 393 return qp->q.key.v4.user == IP_DEFRAG_LOCAL_DELIVER; 394 } 395 396 /* Build a new IP datagram from all its fragments. */ 397 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb, 398 struct sk_buff *prev_tail, struct net_device *dev, 399 int *refs) 400 { 401 struct net *net = qp->q.fqdir->net; 402 struct iphdr *iph; 403 void *reasm_data; 404 int len, err; 405 u8 ecn; 406 407 inet_frag_kill(&qp->q, refs); 408 409 ecn = ip_frag_ecn_table[qp->ecn]; 410 if (unlikely(ecn == 0xff)) { 411 err = -EINVAL; 412 goto out_fail; 413 } 414 415 /* Make the one we just received the head. */ 416 reasm_data = inet_frag_reasm_prepare(&qp->q, skb, prev_tail); 417 if (!reasm_data) 418 goto out_nomem; 419 420 len = ip_hdrlen(skb) + qp->q.len; 421 err = -E2BIG; 422 if (len > 65535) 423 goto out_oversize; 424 425 inet_frag_reasm_finish(&qp->q, skb, reasm_data, 426 ip_frag_coalesce_ok(qp)); 427 428 skb->dev = dev; 429 IPCB(skb)->frag_max_size = max(qp->max_df_size, qp->q.max_size); 430 431 iph = ip_hdr(skb); 432 iph->tot_len = htons(len); 433 iph->tos |= ecn; 434 435 /* When we set IP_DF on a refragmented skb we must also force a 436 * call to ip_fragment to avoid forwarding a DF-skb of size s while 437 * original sender only sent fragments of size f (where f < s). 438 * 439 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest 440 * frag seen to avoid sending tiny DF-fragments in case skb was built 441 * from one very small df-fragment and one large non-df frag. 442 */ 443 if (qp->max_df_size == qp->q.max_size) { 444 IPCB(skb)->flags |= IPSKB_FRAG_PMTU; 445 iph->frag_off = htons(IP_DF); 446 } else { 447 iph->frag_off = 0; 448 } 449 450 ip_send_check(iph); 451 452 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS); 453 qp->q.rb_fragments = RB_ROOT; 454 qp->q.fragments_tail = NULL; 455 qp->q.last_run_head = NULL; 456 return 0; 457 458 out_nomem: 459 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp); 460 err = -ENOMEM; 461 goto out_fail; 462 out_oversize: 463 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr); 464 out_fail: 465 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); 466 return err; 467 } 468 469 /* Process an incoming IP datagram fragment. */ 470 int ip_defrag(struct net *net, struct sk_buff *skb, u32 user) 471 { 472 struct net_device *dev; 473 struct ipq *qp; 474 int vif; 475 476 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS); 477 478 /* Lookup (or create) queue header */ 479 rcu_read_lock(); 480 dev = skb->dev ? : skb_dst_dev_rcu(skb); 481 vif = l3mdev_master_ifindex_rcu(dev); 482 qp = ip_find(net, ip_hdr(skb), user, vif); 483 if (qp) { 484 int ret, refs = 0; 485 486 spin_lock(&qp->q.lock); 487 488 ret = ip_frag_queue(qp, skb, &refs); 489 490 spin_unlock(&qp->q.lock); 491 rcu_read_unlock(); 492 inet_frag_putn(&qp->q, refs); 493 return ret; 494 } 495 rcu_read_unlock(); 496 497 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); 498 kfree_skb(skb); 499 return -ENOMEM; 500 } 501 EXPORT_SYMBOL(ip_defrag); 502 503 struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user) 504 { 505 struct iphdr iph; 506 int netoff; 507 u32 len; 508 509 if (skb->protocol != htons(ETH_P_IP)) 510 return skb; 511 512 netoff = skb_network_offset(skb); 513 514 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0) 515 return skb; 516 517 if (iph.ihl < 5 || iph.version != 4) 518 return skb; 519 520 len = ntohs(iph.tot_len); 521 if (skb->len < netoff + len || len < (iph.ihl * 4)) 522 return skb; 523 524 if (ip_is_fragment(&iph)) { 525 skb = skb_share_check(skb, GFP_ATOMIC); 526 if (skb) { 527 if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) { 528 kfree_skb(skb); 529 return NULL; 530 } 531 if (pskb_trim_rcsum(skb, netoff + len)) { 532 kfree_skb(skb); 533 return NULL; 534 } 535 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 536 if (ip_defrag(net, skb, user)) 537 return NULL; 538 skb_clear_hash(skb); 539 } 540 } 541 return skb; 542 } 543 EXPORT_SYMBOL(ip_check_defrag); 544 545 #ifdef CONFIG_SYSCTL 546 static int dist_min; 547 548 static struct ctl_table ip4_frags_ns_ctl_table[] = { 549 { 550 .procname = "ipfrag_high_thresh", 551 .maxlen = sizeof(unsigned long), 552 .mode = 0644, 553 .proc_handler = proc_doulongvec_minmax, 554 }, 555 { 556 .procname = "ipfrag_low_thresh", 557 .maxlen = sizeof(unsigned long), 558 .mode = 0644, 559 .proc_handler = proc_doulongvec_minmax, 560 }, 561 { 562 .procname = "ipfrag_time", 563 .maxlen = sizeof(int), 564 .mode = 0644, 565 .proc_handler = proc_dointvec_jiffies, 566 }, 567 { 568 .procname = "ipfrag_max_dist", 569 .maxlen = sizeof(int), 570 .mode = 0644, 571 .proc_handler = proc_dointvec_minmax, 572 .extra1 = &dist_min, 573 }, 574 }; 575 576 /* secret interval has been deprecated */ 577 static int ip4_frags_secret_interval_unused; 578 static struct ctl_table ip4_frags_ctl_table[] = { 579 { 580 .procname = "ipfrag_secret_interval", 581 .data = &ip4_frags_secret_interval_unused, 582 .maxlen = sizeof(int), 583 .mode = 0644, 584 .proc_handler = proc_dointvec_jiffies, 585 }, 586 }; 587 588 static int __net_init ip4_frags_ns_ctl_register(struct net *net) 589 { 590 struct ctl_table *table; 591 struct ctl_table_header *hdr; 592 593 table = ip4_frags_ns_ctl_table; 594 if (!net_eq(net, &init_net)) { 595 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL); 596 if (!table) 597 goto err_alloc; 598 599 } 600 table[0].data = &net->ipv4.fqdir->high_thresh; 601 table[0].extra1 = &net->ipv4.fqdir->low_thresh; 602 table[1].data = &net->ipv4.fqdir->low_thresh; 603 table[1].extra2 = &net->ipv4.fqdir->high_thresh; 604 table[2].data = &net->ipv4.fqdir->timeout; 605 table[3].data = &net->ipv4.fqdir->max_dist; 606 607 hdr = register_net_sysctl_sz(net, "net/ipv4", table, 608 ARRAY_SIZE(ip4_frags_ns_ctl_table)); 609 if (!hdr) 610 goto err_reg; 611 612 net->ipv4.frags_hdr = hdr; 613 return 0; 614 615 err_reg: 616 if (!net_eq(net, &init_net)) 617 kfree(table); 618 err_alloc: 619 return -ENOMEM; 620 } 621 622 static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net) 623 { 624 const struct ctl_table *table; 625 626 table = net->ipv4.frags_hdr->ctl_table_arg; 627 unregister_net_sysctl_table(net->ipv4.frags_hdr); 628 kfree(table); 629 } 630 631 static void __init ip4_frags_ctl_register(void) 632 { 633 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table); 634 } 635 #else 636 static int ip4_frags_ns_ctl_register(struct net *net) 637 { 638 return 0; 639 } 640 641 static void ip4_frags_ns_ctl_unregister(struct net *net) 642 { 643 } 644 645 static void __init ip4_frags_ctl_register(void) 646 { 647 } 648 #endif 649 650 static int __net_init ipv4_frags_init_net(struct net *net) 651 { 652 int res; 653 654 res = fqdir_init(&net->ipv4.fqdir, &ip4_frags, net); 655 if (res < 0) 656 return res; 657 /* Fragment cache limits. 658 * 659 * The fragment memory accounting code, (tries to) account for 660 * the real memory usage, by measuring both the size of frag 661 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue)) 662 * and the SKB's truesize. 663 * 664 * A 64K fragment consumes 129736 bytes (44*2944)+200 665 * (1500 truesize == 2944, sizeof(struct ipq) == 200) 666 * 667 * We will commit 4MB at one time. Should we cross that limit 668 * we will prune down to 3MB, making room for approx 8 big 64K 669 * fragments 8x128k. 670 */ 671 net->ipv4.fqdir->high_thresh = 4 * 1024 * 1024; 672 net->ipv4.fqdir->low_thresh = 3 * 1024 * 1024; 673 /* 674 * Important NOTE! Fragment queue must be destroyed before MSL expires. 675 * RFC791 is wrong proposing to prolongate timer each fragment arrival 676 * by TTL. 677 */ 678 net->ipv4.fqdir->timeout = IP_FRAG_TIME; 679 680 net->ipv4.fqdir->max_dist = 64; 681 682 res = ip4_frags_ns_ctl_register(net); 683 if (res < 0) 684 fqdir_exit(net->ipv4.fqdir); 685 return res; 686 } 687 688 static void __net_exit ipv4_frags_pre_exit_net(struct net *net) 689 { 690 fqdir_pre_exit(net->ipv4.fqdir); 691 } 692 693 static void __net_exit ipv4_frags_exit_net(struct net *net) 694 { 695 ip4_frags_ns_ctl_unregister(net); 696 fqdir_exit(net->ipv4.fqdir); 697 } 698 699 static struct pernet_operations ip4_frags_ops = { 700 .init = ipv4_frags_init_net, 701 .pre_exit = ipv4_frags_pre_exit_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