nf_conntrack_reasm.c revision 86e93e470cadedda9181a2bd9aee1d9d2e5e9c0f
1/* 2 * IPv6 fragment reassembly for connection tracking 3 * 4 * Copyright (C)2004 USAGI/WIDE Project 5 * 6 * Author: 7 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> 8 * 9 * Based on: net/ipv6/reassembly.c 10 * 11 * This program is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU General Public License 13 * as published by the Free Software Foundation; either version 14 * 2 of the License, or (at your option) any later version. 15 */ 16 17#define pr_fmt(fmt) "IPv6-nf: " fmt 18 19#include <linux/errno.h> 20#include <linux/types.h> 21#include <linux/string.h> 22#include <linux/socket.h> 23#include <linux/sockios.h> 24#include <linux/jiffies.h> 25#include <linux/net.h> 26#include <linux/list.h> 27#include <linux/netdevice.h> 28#include <linux/in6.h> 29#include <linux/ipv6.h> 30#include <linux/icmpv6.h> 31#include <linux/random.h> 32#include <linux/slab.h> 33 34#include <net/sock.h> 35#include <net/snmp.h> 36#include <net/inet_frag.h> 37 38#include <net/ipv6.h> 39#include <net/protocol.h> 40#include <net/transp_v6.h> 41#include <net/rawv6.h> 42#include <net/ndisc.h> 43#include <net/addrconf.h> 44#include <net/inet_ecn.h> 45#include <net/netfilter/ipv6/nf_conntrack_ipv6.h> 46#include <linux/sysctl.h> 47#include <linux/netfilter.h> 48#include <linux/netfilter_ipv6.h> 49#include <linux/kernel.h> 50#include <linux/module.h> 51#include <net/netfilter/ipv6/nf_defrag_ipv6.h> 52 53 54struct nf_ct_frag6_skb_cb 55{ 56 struct inet6_skb_parm h; 57 int offset; 58 struct sk_buff *orig; 59}; 60 61#define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb)) 62 63static struct inet_frags nf_frags; 64 65#ifdef CONFIG_SYSCTL 66static struct ctl_table nf_ct_frag6_sysctl_table[] = { 67 { 68 .procname = "nf_conntrack_frag6_timeout", 69 .data = &init_net.nf_frag.frags.timeout, 70 .maxlen = sizeof(unsigned int), 71 .mode = 0644, 72 .proc_handler = proc_dointvec_jiffies, 73 }, 74 { 75 .procname = "nf_conntrack_frag6_low_thresh", 76 .data = &init_net.nf_frag.frags.low_thresh, 77 .maxlen = sizeof(unsigned int), 78 .mode = 0644, 79 .proc_handler = proc_dointvec, 80 }, 81 { 82 .procname = "nf_conntrack_frag6_high_thresh", 83 .data = &init_net.nf_frag.frags.high_thresh, 84 .maxlen = sizeof(unsigned int), 85 .mode = 0644, 86 .proc_handler = proc_dointvec, 87 }, 88 { } 89}; 90 91static int nf_ct_frag6_sysctl_register(struct net *net) 92{ 93 struct ctl_table *table; 94 struct ctl_table_header *hdr; 95 96 table = nf_ct_frag6_sysctl_table; 97 if (!net_eq(net, &init_net)) { 98 table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table), 99 GFP_KERNEL); 100 if (table == NULL) 101 goto err_alloc; 102 103 table[0].data = &net->nf_frag.frags.timeout; 104 table[1].data = &net->nf_frag.frags.low_thresh; 105 table[2].data = &net->nf_frag.frags.high_thresh; 106 } 107 108 hdr = register_net_sysctl(net, "net/netfilter", table); 109 if (hdr == NULL) 110 goto err_reg; 111 112 net->nf_frag.sysctl.frags_hdr = hdr; 113 return 0; 114 115err_reg: 116 if (!net_eq(net, &init_net)) 117 kfree(table); 118err_alloc: 119 return -ENOMEM; 120} 121 122static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net) 123{ 124 struct ctl_table *table; 125 126 table = net->nf_frag.sysctl.frags_hdr->ctl_table_arg; 127 unregister_net_sysctl_table(net->nf_frag.sysctl.frags_hdr); 128 if (!net_eq(net, &init_net)) 129 kfree(table); 130} 131 132#else 133static int nf_ct_frag6_sysctl_register(struct net *net) 134{ 135 return 0; 136} 137static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net) 138{ 139} 140#endif 141 142static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h) 143{ 144 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK); 145} 146 147static unsigned int nf_hash_frag(__be32 id, const struct in6_addr *saddr, 148 const struct in6_addr *daddr) 149{ 150 net_get_random_once(&nf_frags.rnd, sizeof(nf_frags.rnd)); 151 return jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr), 152 (__force u32)id, nf_frags.rnd); 153} 154 155 156static unsigned int nf_hashfn(const struct inet_frag_queue *q) 157{ 158 const struct frag_queue *nq; 159 160 nq = container_of(q, struct frag_queue, q); 161 return nf_hash_frag(nq->id, &nq->saddr, &nq->daddr); 162} 163 164static void nf_skb_free(struct sk_buff *skb) 165{ 166 if (NFCT_FRAG6_CB(skb)->orig) 167 kfree_skb(NFCT_FRAG6_CB(skb)->orig); 168} 169 170static void nf_ct_frag6_expire(unsigned long data) 171{ 172 struct frag_queue *fq; 173 struct net *net; 174 175 fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q); 176 net = container_of(fq->q.net, struct net, nf_frag.frags); 177 178 ip6_expire_frag_queue(net, fq, &nf_frags); 179} 180 181/* Creation primitives. */ 182static inline struct frag_queue *fq_find(struct net *net, __be32 id, 183 u32 user, struct in6_addr *src, 184 struct in6_addr *dst, u8 ecn) 185{ 186 struct inet_frag_queue *q; 187 struct ip6_create_arg arg; 188 unsigned int hash; 189 190 arg.id = id; 191 arg.user = user; 192 arg.src = src; 193 arg.dst = dst; 194 arg.ecn = ecn; 195 196 read_lock_bh(&nf_frags.lock); 197 hash = nf_hash_frag(id, src, dst); 198 199 q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash); 200 local_bh_enable(); 201 if (IS_ERR_OR_NULL(q)) { 202 inet_frag_maybe_warn_overflow(q, pr_fmt()); 203 return NULL; 204 } 205 return container_of(q, struct frag_queue, q); 206} 207 208 209static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb, 210 const struct frag_hdr *fhdr, int nhoff) 211{ 212 struct sk_buff *prev, *next; 213 unsigned int payload_len; 214 int offset, end; 215 u8 ecn; 216 217 if (fq->q.last_in & INET_FRAG_COMPLETE) { 218 pr_debug("Already completed\n"); 219 goto err; 220 } 221 222 payload_len = ntohs(ipv6_hdr(skb)->payload_len); 223 224 offset = ntohs(fhdr->frag_off) & ~0x7; 225 end = offset + (payload_len - 226 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); 227 228 if ((unsigned int)end > IPV6_MAXPLEN) { 229 pr_debug("offset is too large.\n"); 230 return -1; 231 } 232 233 ecn = ip6_frag_ecn(ipv6_hdr(skb)); 234 235 if (skb->ip_summed == CHECKSUM_COMPLETE) { 236 const unsigned char *nh = skb_network_header(skb); 237 skb->csum = csum_sub(skb->csum, 238 csum_partial(nh, (u8 *)(fhdr + 1) - nh, 239 0)); 240 } 241 242 /* Is this the final fragment? */ 243 if (!(fhdr->frag_off & htons(IP6_MF))) { 244 /* If we already have some bits beyond end 245 * or have different end, the segment is corrupted. 246 */ 247 if (end < fq->q.len || 248 ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) { 249 pr_debug("already received last fragment\n"); 250 goto err; 251 } 252 fq->q.last_in |= INET_FRAG_LAST_IN; 253 fq->q.len = end; 254 } else { 255 /* Check if the fragment is rounded to 8 bytes. 256 * Required by the RFC. 257 */ 258 if (end & 0x7) { 259 /* RFC2460 says always send parameter problem in 260 * this case. -DaveM 261 */ 262 pr_debug("end of fragment not rounded to 8 bytes.\n"); 263 return -1; 264 } 265 if (end > fq->q.len) { 266 /* Some bits beyond end -> corruption. */ 267 if (fq->q.last_in & INET_FRAG_LAST_IN) { 268 pr_debug("last packet already reached.\n"); 269 goto err; 270 } 271 fq->q.len = end; 272 } 273 } 274 275 if (end == offset) 276 goto err; 277 278 /* Point into the IP datagram 'data' part. */ 279 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) { 280 pr_debug("queue: message is too short.\n"); 281 goto err; 282 } 283 if (pskb_trim_rcsum(skb, end - offset)) { 284 pr_debug("Can't trim\n"); 285 goto err; 286 } 287 288 /* Find out which fragments are in front and at the back of us 289 * in the chain of fragments so far. We must know where to put 290 * this fragment, right? 291 */ 292 prev = fq->q.fragments_tail; 293 if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) { 294 next = NULL; 295 goto found; 296 } 297 prev = NULL; 298 for (next = fq->q.fragments; next != NULL; next = next->next) { 299 if (NFCT_FRAG6_CB(next)->offset >= offset) 300 break; /* bingo! */ 301 prev = next; 302 } 303 304found: 305 /* RFC5722, Section 4: 306 * When reassembling an IPv6 datagram, if 307 * one or more its constituent fragments is determined to be an 308 * overlapping fragment, the entire datagram (and any constituent 309 * fragments, including those not yet received) MUST be silently 310 * discarded. 311 */ 312 313 /* Check for overlap with preceding fragment. */ 314 if (prev && 315 (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset) 316 goto discard_fq; 317 318 /* Look for overlap with succeeding segment. */ 319 if (next && NFCT_FRAG6_CB(next)->offset < end) 320 goto discard_fq; 321 322 NFCT_FRAG6_CB(skb)->offset = offset; 323 324 /* Insert this fragment in the chain of fragments. */ 325 skb->next = next; 326 if (!next) 327 fq->q.fragments_tail = skb; 328 if (prev) 329 prev->next = skb; 330 else 331 fq->q.fragments = skb; 332 333 if (skb->dev) { 334 fq->iif = skb->dev->ifindex; 335 skb->dev = NULL; 336 } 337 fq->q.stamp = skb->tstamp; 338 fq->q.meat += skb->len; 339 fq->ecn |= ecn; 340 if (payload_len > fq->q.max_size) 341 fq->q.max_size = payload_len; 342 add_frag_mem_limit(&fq->q, skb->truesize); 343 344 /* The first fragment. 345 * nhoffset is obtained from the first fragment, of course. 346 */ 347 if (offset == 0) { 348 fq->nhoffset = nhoff; 349 fq->q.last_in |= INET_FRAG_FIRST_IN; 350 } 351 352 inet_frag_lru_move(&fq->q); 353 return 0; 354 355discard_fq: 356 inet_frag_kill(&fq->q, &nf_frags); 357err: 358 return -1; 359} 360 361/* 362 * Check if this packet is complete. 363 * Returns NULL on failure by any reason, and pointer 364 * to current nexthdr field in reassembled frame. 365 * 366 * It is called with locked fq, and caller must check that 367 * queue is eligible for reassembly i.e. it is not COMPLETE, 368 * the last and the first frames arrived and all the bits are here. 369 */ 370static struct sk_buff * 371nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev) 372{ 373 struct sk_buff *fp, *op, *head = fq->q.fragments; 374 int payload_len; 375 u8 ecn; 376 377 inet_frag_kill(&fq->q, &nf_frags); 378 379 WARN_ON(head == NULL); 380 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0); 381 382 ecn = ip_frag_ecn_table[fq->ecn]; 383 if (unlikely(ecn == 0xff)) 384 goto out_fail; 385 386 /* Unfragmented part is taken from the first segment. */ 387 payload_len = ((head->data - skb_network_header(head)) - 388 sizeof(struct ipv6hdr) + fq->q.len - 389 sizeof(struct frag_hdr)); 390 if (payload_len > IPV6_MAXPLEN) { 391 pr_debug("payload len is too large.\n"); 392 goto out_oversize; 393 } 394 395 /* Head of list must not be cloned. */ 396 if (skb_unclone(head, GFP_ATOMIC)) { 397 pr_debug("skb is cloned but can't expand head"); 398 goto out_oom; 399 } 400 401 /* If the first fragment is fragmented itself, we split 402 * it to two chunks: the first with data and paged part 403 * and the second, holding only fragments. */ 404 if (skb_has_frag_list(head)) { 405 struct sk_buff *clone; 406 int i, plen = 0; 407 408 clone = alloc_skb(0, GFP_ATOMIC); 409 if (clone == NULL) 410 goto out_oom; 411 412 clone->next = head->next; 413 head->next = clone; 414 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; 415 skb_frag_list_init(head); 416 for (i = 0; i < skb_shinfo(head)->nr_frags; i++) 417 plen += skb_frag_size(&skb_shinfo(head)->frags[i]); 418 clone->len = clone->data_len = head->data_len - plen; 419 head->data_len -= clone->len; 420 head->len -= clone->len; 421 clone->csum = 0; 422 clone->ip_summed = head->ip_summed; 423 424 NFCT_FRAG6_CB(clone)->orig = NULL; 425 add_frag_mem_limit(&fq->q, clone->truesize); 426 } 427 428 /* We have to remove fragment header from datagram and to relocate 429 * header in order to calculate ICV correctly. */ 430 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0]; 431 memmove(head->head + sizeof(struct frag_hdr), head->head, 432 (head->data - head->head) - sizeof(struct frag_hdr)); 433 head->mac_header += sizeof(struct frag_hdr); 434 head->network_header += sizeof(struct frag_hdr); 435 436 skb_shinfo(head)->frag_list = head->next; 437 skb_reset_transport_header(head); 438 skb_push(head, head->data - skb_network_header(head)); 439 440 for (fp=head->next; fp; fp = fp->next) { 441 head->data_len += fp->len; 442 head->len += fp->len; 443 if (head->ip_summed != fp->ip_summed) 444 head->ip_summed = CHECKSUM_NONE; 445 else if (head->ip_summed == CHECKSUM_COMPLETE) 446 head->csum = csum_add(head->csum, fp->csum); 447 head->truesize += fp->truesize; 448 } 449 sub_frag_mem_limit(&fq->q, head->truesize); 450 451 head->ignore_df = 1; 452 head->next = NULL; 453 head->dev = dev; 454 head->tstamp = fq->q.stamp; 455 ipv6_hdr(head)->payload_len = htons(payload_len); 456 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn); 457 IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size; 458 459 /* Yes, and fold redundant checksum back. 8) */ 460 if (head->ip_summed == CHECKSUM_COMPLETE) 461 head->csum = csum_partial(skb_network_header(head), 462 skb_network_header_len(head), 463 head->csum); 464 465 fq->q.fragments = NULL; 466 fq->q.fragments_tail = NULL; 467 468 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */ 469 fp = skb_shinfo(head)->frag_list; 470 if (fp && NFCT_FRAG6_CB(fp)->orig == NULL) 471 /* at above code, head skb is divided into two skbs. */ 472 fp = fp->next; 473 474 op = NFCT_FRAG6_CB(head)->orig; 475 for (; fp; fp = fp->next) { 476 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig; 477 478 op->next = orig; 479 op = orig; 480 NFCT_FRAG6_CB(fp)->orig = NULL; 481 } 482 483 return head; 484 485out_oversize: 486 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n", 487 payload_len); 488 goto out_fail; 489out_oom: 490 net_dbg_ratelimited("nf_ct_frag6_reasm: no memory for reassembly\n"); 491out_fail: 492 return NULL; 493} 494 495/* 496 * find the header just before Fragment Header. 497 * 498 * if success return 0 and set ... 499 * (*prevhdrp): the value of "Next Header Field" in the header 500 * just before Fragment Header. 501 * (*prevhoff): the offset of "Next Header Field" in the header 502 * just before Fragment Header. 503 * (*fhoff) : the offset of Fragment Header. 504 * 505 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c 506 * 507 */ 508static int 509find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff) 510{ 511 u8 nexthdr = ipv6_hdr(skb)->nexthdr; 512 const int netoff = skb_network_offset(skb); 513 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr); 514 int start = netoff + sizeof(struct ipv6hdr); 515 int len = skb->len - start; 516 u8 prevhdr = NEXTHDR_IPV6; 517 518 while (nexthdr != NEXTHDR_FRAGMENT) { 519 struct ipv6_opt_hdr hdr; 520 int hdrlen; 521 522 if (!ipv6_ext_hdr(nexthdr)) { 523 return -1; 524 } 525 if (nexthdr == NEXTHDR_NONE) { 526 pr_debug("next header is none\n"); 527 return -1; 528 } 529 if (len < (int)sizeof(struct ipv6_opt_hdr)) { 530 pr_debug("too short\n"); 531 return -1; 532 } 533 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr))) 534 BUG(); 535 if (nexthdr == NEXTHDR_AUTH) 536 hdrlen = (hdr.hdrlen+2)<<2; 537 else 538 hdrlen = ipv6_optlen(&hdr); 539 540 prevhdr = nexthdr; 541 prev_nhoff = start; 542 543 nexthdr = hdr.nexthdr; 544 len -= hdrlen; 545 start += hdrlen; 546 } 547 548 if (len < 0) 549 return -1; 550 551 *prevhdrp = prevhdr; 552 *prevhoff = prev_nhoff; 553 *fhoff = start; 554 555 return 0; 556} 557 558struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user) 559{ 560 struct sk_buff *clone; 561 struct net_device *dev = skb->dev; 562 struct net *net = skb_dst(skb) ? dev_net(skb_dst(skb)->dev) 563 : dev_net(skb->dev); 564 struct frag_hdr *fhdr; 565 struct frag_queue *fq; 566 struct ipv6hdr *hdr; 567 int fhoff, nhoff; 568 u8 prevhdr; 569 struct sk_buff *ret_skb = NULL; 570 571 /* Jumbo payload inhibits frag. header */ 572 if (ipv6_hdr(skb)->payload_len == 0) { 573 pr_debug("payload len = 0\n"); 574 return skb; 575 } 576 577 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0) 578 return skb; 579 580 clone = skb_clone(skb, GFP_ATOMIC); 581 if (clone == NULL) { 582 pr_debug("Can't clone skb\n"); 583 return skb; 584 } 585 586 NFCT_FRAG6_CB(clone)->orig = skb; 587 588 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) { 589 pr_debug("message is too short.\n"); 590 goto ret_orig; 591 } 592 593 skb_set_transport_header(clone, fhoff); 594 hdr = ipv6_hdr(clone); 595 fhdr = (struct frag_hdr *)skb_transport_header(clone); 596 597 fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr, 598 ip6_frag_ecn(hdr)); 599 if (fq == NULL) { 600 pr_debug("Can't find and can't create new queue\n"); 601 goto ret_orig; 602 } 603 604 spin_lock_bh(&fq->q.lock); 605 606 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) { 607 spin_unlock_bh(&fq->q.lock); 608 pr_debug("Can't insert skb to queue\n"); 609 inet_frag_put(&fq->q, &nf_frags); 610 goto ret_orig; 611 } 612 613 if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && 614 fq->q.meat == fq->q.len) { 615 ret_skb = nf_ct_frag6_reasm(fq, dev); 616 if (ret_skb == NULL) 617 pr_debug("Can't reassemble fragmented packets\n"); 618 } 619 spin_unlock_bh(&fq->q.lock); 620 621 inet_frag_put(&fq->q, &nf_frags); 622 return ret_skb; 623 624ret_orig: 625 kfree_skb(clone); 626 return skb; 627} 628 629void nf_ct_frag6_consume_orig(struct sk_buff *skb) 630{ 631 struct sk_buff *s, *s2; 632 633 for (s = NFCT_FRAG6_CB(skb)->orig; s;) { 634 s2 = s->next; 635 s->next = NULL; 636 consume_skb(s); 637 s = s2; 638 } 639} 640 641static int nf_ct_net_init(struct net *net) 642{ 643 net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH; 644 net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH; 645 net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT; 646 inet_frags_init_net(&net->nf_frag.frags); 647 648 return nf_ct_frag6_sysctl_register(net); 649} 650 651static void nf_ct_net_exit(struct net *net) 652{ 653 nf_ct_frags6_sysctl_unregister(net); 654 inet_frags_exit_net(&net->nf_frag.frags, &nf_frags); 655} 656 657static struct pernet_operations nf_ct_net_ops = { 658 .init = nf_ct_net_init, 659 .exit = nf_ct_net_exit, 660}; 661 662int nf_ct_frag6_init(void) 663{ 664 int ret = 0; 665 666 nf_frags.hashfn = nf_hashfn; 667 nf_frags.constructor = ip6_frag_init; 668 nf_frags.destructor = NULL; 669 nf_frags.skb_free = nf_skb_free; 670 nf_frags.qsize = sizeof(struct frag_queue); 671 nf_frags.match = ip6_frag_match; 672 nf_frags.frag_expire = nf_ct_frag6_expire; 673 nf_frags.secret_interval = 10 * 60 * HZ; 674 inet_frags_init(&nf_frags); 675 676 ret = register_pernet_subsys(&nf_ct_net_ops); 677 if (ret) 678 inet_frags_fini(&nf_frags); 679 680 return ret; 681} 682 683void nf_ct_frag6_cleanup(void) 684{ 685 unregister_pernet_subsys(&nf_ct_net_ops); 686 inet_frags_fini(&nf_frags); 687} 688