1 /* 2 * IPv6 fragment reassembly 3 * Linux INET6 implementation 4 * 5 * Authors: 6 * Pedro Roque <roque@di.fc.ul.pt> 7 * 8 * Based on: net/ipv4/ip_fragment.c 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 13 * 2 of the License, or (at your option) any later version. 14 */ 15 16 /* 17 * Fixes: 18 * Andi Kleen Make it work with multiple hosts. 19 * More RFC compliance. 20 * 21 * Horst von Brand Add missing #include <linux/string.h> 22 * Alexey Kuznetsov SMP races, threading, cleanup. 23 * Patrick McHardy LRU queue of frag heads for evictor. 24 * Mitsuru KANDA @USAGI Register inet6_protocol{}. 25 * David Stevens and 26 * YOSHIFUJI,H. @USAGI Always remove fragment header to 27 * calculate ICV correctly. 28 */ 29 30 #define pr_fmt(fmt) "IPv6: " fmt 31 32 #include <linux/errno.h> 33 #include <linux/types.h> 34 #include <linux/string.h> 35 #include <linux/socket.h> 36 #include <linux/sockios.h> 37 #include <linux/jiffies.h> 38 #include <linux/net.h> 39 #include <linux/list.h> 40 #include <linux/netdevice.h> 41 #include <linux/in6.h> 42 #include <linux/ipv6.h> 43 #include <linux/icmpv6.h> 44 #include <linux/random.h> 45 #include <linux/jhash.h> 46 #include <linux/skbuff.h> 47 #include <linux/slab.h> 48 #include <linux/export.h> 49 50 #include <net/sock.h> 51 #include <net/snmp.h> 52 53 #include <net/ipv6.h> 54 #include <net/ip6_route.h> 55 #include <net/protocol.h> 56 #include <net/transp_v6.h> 57 #include <net/rawv6.h> 58 #include <net/ndisc.h> 59 #include <net/addrconf.h> 60 #include <net/ipv6_frag.h> 61 #include <net/inet_ecn.h> 62 63 static const char ip6_frag_cache_name[] = "ip6-frags"; 64 65 static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h) 66 { 67 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK); 68 } 69 70 static struct inet_frags ip6_frags; 71 72 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb, 73 struct sk_buff *prev_tail, struct net_device *dev); 74 75 static void ip6_frag_expire(struct timer_list *t) 76 { 77 struct inet_frag_queue *frag = from_timer(frag, t, timer); 78 struct frag_queue *fq; 79 80 fq = container_of(frag, struct frag_queue, q); 81 82 ip6frag_expire_frag_queue(fq->q.fqdir->net, fq); 83 } 84 85 static struct frag_queue * 86 fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif) 87 { 88 struct frag_v6_compare_key key = { 89 .id = id, 90 .saddr = hdr->saddr, 91 .daddr = hdr->daddr, 92 .user = IP6_DEFRAG_LOCAL_DELIVER, 93 .iif = iif, 94 }; 95 struct inet_frag_queue *q; 96 97 if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST | 98 IPV6_ADDR_LINKLOCAL))) 99 key.iif = 0; 100 101 q = inet_frag_find(net->ipv6.fqdir, &key); 102 if (!q) 103 return NULL; 104 105 return container_of(q, struct frag_queue, q); 106 } 107 108 static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, 109 struct frag_hdr *fhdr, int nhoff, 110 u32 *prob_offset) 111 { 112 struct net *net = dev_net(skb_dst(skb)->dev); 113 int offset, end, fragsize; 114 struct sk_buff *prev_tail; 115 struct net_device *dev; 116 int err = -ENOENT; 117 u8 ecn; 118 119 if (fq->q.flags & INET_FRAG_COMPLETE) 120 goto err; 121 122 err = -EINVAL; 123 offset = ntohs(fhdr->frag_off) & ~0x7; 124 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - 125 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); 126 127 if ((unsigned int)end > IPV6_MAXPLEN) { 128 *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb); 129 /* note that if prob_offset is set, the skb is freed elsewhere, 130 * we do not free it here. 131 */ 132 return -1; 133 } 134 135 ecn = ip6_frag_ecn(ipv6_hdr(skb)); 136 137 if (skb->ip_summed == CHECKSUM_COMPLETE) { 138 const unsigned char *nh = skb_network_header(skb); 139 skb->csum = csum_sub(skb->csum, 140 csum_partial(nh, (u8 *)(fhdr + 1) - nh, 141 0)); 142 } 143 144 /* Is this the final fragment? */ 145 if (!(fhdr->frag_off & htons(IP6_MF))) { 146 /* If we already have some bits beyond end 147 * or have different end, the segment is corrupted. 148 */ 149 if (end < fq->q.len || 150 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) 151 goto discard_fq; 152 fq->q.flags |= INET_FRAG_LAST_IN; 153 fq->q.len = end; 154 } else { 155 /* Check if the fragment is rounded to 8 bytes. 156 * Required by the RFC. 157 */ 158 if (end & 0x7) { 159 /* RFC2460 says always send parameter problem in 160 * this case. -DaveM 161 */ 162 *prob_offset = offsetof(struct ipv6hdr, payload_len); 163 return -1; 164 } 165 if (end > fq->q.len) { 166 /* Some bits beyond end -> corruption. */ 167 if (fq->q.flags & INET_FRAG_LAST_IN) 168 goto discard_fq; 169 fq->q.len = end; 170 } 171 } 172 173 if (end == offset) 174 goto discard_fq; 175 176 err = -ENOMEM; 177 /* Point into the IP datagram 'data' part. */ 178 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) 179 goto discard_fq; 180 181 err = pskb_trim_rcsum(skb, end - offset); 182 if (err) 183 goto discard_fq; 184 185 /* Note : skb->rbnode and skb->dev share the same location. */ 186 dev = skb->dev; 187 /* Makes sure compiler wont do silly aliasing games */ 188 barrier(); 189 190 prev_tail = fq->q.fragments_tail; 191 err = inet_frag_queue_insert(&fq->q, skb, offset, end); 192 if (err) 193 goto insert_error; 194 195 if (dev) 196 fq->iif = dev->ifindex; 197 198 fq->q.stamp = skb->tstamp; 199 fq->q.meat += skb->len; 200 fq->ecn |= ecn; 201 add_frag_mem_limit(fq->q.fqdir, skb->truesize); 202 203 fragsize = -skb_network_offset(skb) + skb->len; 204 if (fragsize > fq->q.max_size) 205 fq->q.max_size = fragsize; 206 207 /* The first fragment. 208 * nhoffset is obtained from the first fragment, of course. 209 */ 210 if (offset == 0) { 211 fq->nhoffset = nhoff; 212 fq->q.flags |= INET_FRAG_FIRST_IN; 213 } 214 215 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && 216 fq->q.meat == fq->q.len) { 217 unsigned long orefdst = skb->_skb_refdst; 218 219 skb->_skb_refdst = 0UL; 220 err = ip6_frag_reasm(fq, skb, prev_tail, dev); 221 skb->_skb_refdst = orefdst; 222 return err; 223 } 224 225 skb_dst_drop(skb); 226 return -EINPROGRESS; 227 228 insert_error: 229 if (err == IPFRAG_DUP) { 230 kfree_skb(skb); 231 return -EINVAL; 232 } 233 err = -EINVAL; 234 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 235 IPSTATS_MIB_REASM_OVERLAPS); 236 discard_fq: 237 inet_frag_kill(&fq->q); 238 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 239 IPSTATS_MIB_REASMFAILS); 240 err: 241 kfree_skb(skb); 242 return err; 243 } 244 245 /* 246 * Check if this packet is complete. 247 * 248 * It is called with locked fq, and caller must check that 249 * queue is eligible for reassembly i.e. it is not COMPLETE, 250 * the last and the first frames arrived and all the bits are here. 251 */ 252 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb, 253 struct sk_buff *prev_tail, struct net_device *dev) 254 { 255 struct net *net = fq->q.fqdir->net; 256 unsigned int nhoff; 257 void *reasm_data; 258 int payload_len; 259 u8 ecn; 260 261 inet_frag_kill(&fq->q); 262 263 ecn = ip_frag_ecn_table[fq->ecn]; 264 if (unlikely(ecn == 0xff)) 265 goto out_fail; 266 267 reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail); 268 if (!reasm_data) 269 goto out_oom; 270 271 payload_len = ((skb->data - skb_network_header(skb)) - 272 sizeof(struct ipv6hdr) + fq->q.len - 273 sizeof(struct frag_hdr)); 274 if (payload_len > IPV6_MAXPLEN) 275 goto out_oversize; 276 277 /* We have to remove fragment header from datagram and to relocate 278 * header in order to calculate ICV correctly. */ 279 nhoff = fq->nhoffset; 280 skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0]; 281 memmove(skb->head + sizeof(struct frag_hdr), skb->head, 282 (skb->data - skb->head) - sizeof(struct frag_hdr)); 283 if (skb_mac_header_was_set(skb)) 284 skb->mac_header += sizeof(struct frag_hdr); 285 skb->network_header += sizeof(struct frag_hdr); 286 287 skb_reset_transport_header(skb); 288 289 inet_frag_reasm_finish(&fq->q, skb, reasm_data); 290 291 skb->dev = dev; 292 ipv6_hdr(skb)->payload_len = htons(payload_len); 293 ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn); 294 IP6CB(skb)->nhoff = nhoff; 295 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; 296 IP6CB(skb)->frag_max_size = fq->q.max_size; 297 298 /* Yes, and fold redundant checksum back. 8) */ 299 skb_postpush_rcsum(skb, skb_network_header(skb), 300 skb_network_header_len(skb)); 301 302 rcu_read_lock(); 303 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS); 304 rcu_read_unlock(); 305 fq->q.rb_fragments = RB_ROOT; 306 fq->q.fragments_tail = NULL; 307 fq->q.last_run_head = NULL; 308 return 1; 309 310 out_oversize: 311 net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len); 312 goto out_fail; 313 out_oom: 314 net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n"); 315 out_fail: 316 rcu_read_lock(); 317 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); 318 rcu_read_unlock(); 319 inet_frag_kill(&fq->q); 320 return -1; 321 } 322 323 static int ipv6_frag_rcv(struct sk_buff *skb) 324 { 325 struct frag_hdr *fhdr; 326 struct frag_queue *fq; 327 const struct ipv6hdr *hdr = ipv6_hdr(skb); 328 struct net *net = dev_net(skb_dst(skb)->dev); 329 int iif; 330 331 if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED) 332 goto fail_hdr; 333 334 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS); 335 336 /* Jumbo payload inhibits frag. header */ 337 if (hdr->payload_len == 0) 338 goto fail_hdr; 339 340 if (!pskb_may_pull(skb, (skb_transport_offset(skb) + 341 sizeof(struct frag_hdr)))) 342 goto fail_hdr; 343 344 hdr = ipv6_hdr(skb); 345 fhdr = (struct frag_hdr *)skb_transport_header(skb); 346 347 if (!(fhdr->frag_off & htons(0xFFF9))) { 348 /* It is not a fragmented frame */ 349 skb->transport_header += sizeof(struct frag_hdr); 350 __IP6_INC_STATS(net, 351 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS); 352 353 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); 354 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; 355 return 1; 356 } 357 358 iif = skb->dev ? skb->dev->ifindex : 0; 359 fq = fq_find(net, fhdr->identification, hdr, iif); 360 if (fq) { 361 u32 prob_offset = 0; 362 int ret; 363 364 spin_lock(&fq->q.lock); 365 366 fq->iif = iif; 367 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff, 368 &prob_offset); 369 370 spin_unlock(&fq->q.lock); 371 inet_frag_put(&fq->q); 372 if (prob_offset) { 373 __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 374 IPSTATS_MIB_INHDRERRORS); 375 /* icmpv6_param_prob() calls kfree_skb(skb) */ 376 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset); 377 } 378 return ret; 379 } 380 381 __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS); 382 kfree_skb(skb); 383 return -1; 384 385 fail_hdr: 386 __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 387 IPSTATS_MIB_INHDRERRORS); 388 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb)); 389 return -1; 390 } 391 392 static const struct inet6_protocol frag_protocol = { 393 .handler = ipv6_frag_rcv, 394 .flags = INET6_PROTO_NOPOLICY, 395 }; 396 397 #ifdef CONFIG_SYSCTL 398 399 static struct ctl_table ip6_frags_ns_ctl_table[] = { 400 { 401 .procname = "ip6frag_high_thresh", 402 .maxlen = sizeof(unsigned long), 403 .mode = 0644, 404 .proc_handler = proc_doulongvec_minmax, 405 }, 406 { 407 .procname = "ip6frag_low_thresh", 408 .maxlen = sizeof(unsigned long), 409 .mode = 0644, 410 .proc_handler = proc_doulongvec_minmax, 411 }, 412 { 413 .procname = "ip6frag_time", 414 .maxlen = sizeof(int), 415 .mode = 0644, 416 .proc_handler = proc_dointvec_jiffies, 417 }, 418 { } 419 }; 420 421 /* secret interval has been deprecated */ 422 static int ip6_frags_secret_interval_unused; 423 static struct ctl_table ip6_frags_ctl_table[] = { 424 { 425 .procname = "ip6frag_secret_interval", 426 .data = &ip6_frags_secret_interval_unused, 427 .maxlen = sizeof(int), 428 .mode = 0644, 429 .proc_handler = proc_dointvec_jiffies, 430 }, 431 { } 432 }; 433 434 static int __net_init ip6_frags_ns_sysctl_register(struct net *net) 435 { 436 struct ctl_table *table; 437 struct ctl_table_header *hdr; 438 439 table = ip6_frags_ns_ctl_table; 440 if (!net_eq(net, &init_net)) { 441 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL); 442 if (!table) 443 goto err_alloc; 444 445 } 446 table[0].data = &net->ipv6.fqdir->high_thresh; 447 table[0].extra1 = &net->ipv6.fqdir->low_thresh; 448 table[1].data = &net->ipv6.fqdir->low_thresh; 449 table[1].extra2 = &net->ipv6.fqdir->high_thresh; 450 table[2].data = &net->ipv6.fqdir->timeout; 451 452 hdr = register_net_sysctl(net, "net/ipv6", table); 453 if (!hdr) 454 goto err_reg; 455 456 net->ipv6.sysctl.frags_hdr = hdr; 457 return 0; 458 459 err_reg: 460 if (!net_eq(net, &init_net)) 461 kfree(table); 462 err_alloc: 463 return -ENOMEM; 464 } 465 466 static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net) 467 { 468 struct ctl_table *table; 469 470 table = net->ipv6.sysctl.frags_hdr->ctl_table_arg; 471 unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr); 472 if (!net_eq(net, &init_net)) 473 kfree(table); 474 } 475 476 static struct ctl_table_header *ip6_ctl_header; 477 478 static int ip6_frags_sysctl_register(void) 479 { 480 ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6", 481 ip6_frags_ctl_table); 482 return ip6_ctl_header == NULL ? -ENOMEM : 0; 483 } 484 485 static void ip6_frags_sysctl_unregister(void) 486 { 487 unregister_net_sysctl_table(ip6_ctl_header); 488 } 489 #else 490 static int ip6_frags_ns_sysctl_register(struct net *net) 491 { 492 return 0; 493 } 494 495 static void ip6_frags_ns_sysctl_unregister(struct net *net) 496 { 497 } 498 499 static int ip6_frags_sysctl_register(void) 500 { 501 return 0; 502 } 503 504 static void ip6_frags_sysctl_unregister(void) 505 { 506 } 507 #endif 508 509 static int __net_init ipv6_frags_init_net(struct net *net) 510 { 511 int res; 512 513 res = fqdir_init(&net->ipv6.fqdir, &ip6_frags, net); 514 if (res < 0) 515 return res; 516 517 net->ipv6.fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH; 518 net->ipv6.fqdir->low_thresh = IPV6_FRAG_LOW_THRESH; 519 net->ipv6.fqdir->timeout = IPV6_FRAG_TIMEOUT; 520 521 res = ip6_frags_ns_sysctl_register(net); 522 if (res < 0) 523 fqdir_exit(net->ipv6.fqdir); 524 return res; 525 } 526 527 static void __net_exit ipv6_frags_exit_net(struct net *net) 528 { 529 ip6_frags_ns_sysctl_unregister(net); 530 fqdir_exit(net->ipv6.fqdir); 531 } 532 533 static struct pernet_operations ip6_frags_ops = { 534 .init = ipv6_frags_init_net, 535 .exit = ipv6_frags_exit_net, 536 }; 537 538 static const struct rhashtable_params ip6_rhash_params = { 539 .head_offset = offsetof(struct inet_frag_queue, node), 540 .hashfn = ip6frag_key_hashfn, 541 .obj_hashfn = ip6frag_obj_hashfn, 542 .obj_cmpfn = ip6frag_obj_cmpfn, 543 .automatic_shrinking = true, 544 }; 545 546 int __init ipv6_frag_init(void) 547 { 548 int ret; 549 550 ip6_frags.constructor = ip6frag_init; 551 ip6_frags.destructor = NULL; 552 ip6_frags.qsize = sizeof(struct frag_queue); 553 ip6_frags.frag_expire = ip6_frag_expire; 554 ip6_frags.frags_cache_name = ip6_frag_cache_name; 555 ip6_frags.rhash_params = ip6_rhash_params; 556 ret = inet_frags_init(&ip6_frags); 557 if (ret) 558 goto out; 559 560 ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT); 561 if (ret) 562 goto err_protocol; 563 564 ret = ip6_frags_sysctl_register(); 565 if (ret) 566 goto err_sysctl; 567 568 ret = register_pernet_subsys(&ip6_frags_ops); 569 if (ret) 570 goto err_pernet; 571 572 out: 573 return ret; 574 575 err_pernet: 576 ip6_frags_sysctl_unregister(); 577 err_sysctl: 578 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 579 err_protocol: 580 inet_frags_fini(&ip6_frags); 581 goto out; 582 } 583 584 void ipv6_frag_exit(void) 585 { 586 ip6_frags_sysctl_unregister(); 587 unregister_pernet_subsys(&ip6_frags_ops); 588 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 589 inet_frags_fini(&ip6_frags); 590 } 591