1 /* 2 * Packet matching code. 3 * 4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling 5 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org> 6 * Copyright (C) 2006-2010 Patrick McHardy <kaber@trash.net> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 13 #include <linux/cache.h> 14 #include <linux/capability.h> 15 #include <linux/skbuff.h> 16 #include <linux/kmod.h> 17 #include <linux/vmalloc.h> 18 #include <linux/netdevice.h> 19 #include <linux/module.h> 20 #include <linux/icmp.h> 21 #include <net/ip.h> 22 #include <net/compat.h> 23 #include <asm/uaccess.h> 24 #include <linux/mutex.h> 25 #include <linux/proc_fs.h> 26 #include <linux/err.h> 27 #include <linux/cpumask.h> 28 29 #include <linux/netfilter/x_tables.h> 30 #include <linux/netfilter_ipv4/ip_tables.h> 31 #include <net/netfilter/nf_log.h> 32 #include "../../netfilter/xt_repldata.h" 33 34 MODULE_LICENSE("GPL"); 35 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); 36 MODULE_DESCRIPTION("IPv4 packet filter"); 37 38 #ifdef CONFIG_NETFILTER_DEBUG 39 #define IP_NF_ASSERT(x) WARN_ON(!(x)) 40 #else 41 #define IP_NF_ASSERT(x) 42 #endif 43 44 void *ipt_alloc_initial_table(const struct xt_table *info) 45 { 46 return xt_alloc_initial_table(ipt, IPT); 47 } 48 EXPORT_SYMBOL_GPL(ipt_alloc_initial_table); 49 50 /* Returns whether matches rule or not. */ 51 /* Performance critical - called for every packet */ 52 static inline bool 53 ip_packet_match(const struct iphdr *ip, 54 const char *indev, 55 const char *outdev, 56 const struct ipt_ip *ipinfo, 57 int isfrag) 58 { 59 unsigned long ret; 60 61 #define FWINV(bool, invflg) ((bool) ^ !!(ipinfo->invflags & (invflg))) 62 63 if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr, 64 IPT_INV_SRCIP) || 65 FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr, 66 IPT_INV_DSTIP)) 67 return false; 68 69 ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask); 70 71 if (FWINV(ret != 0, IPT_INV_VIA_IN)) 72 return false; 73 74 ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask); 75 76 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) 77 return false; 78 79 /* Check specific protocol */ 80 if (ipinfo->proto && 81 FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) 82 return false; 83 84 /* If we have a fragment rule but the packet is not a fragment 85 * then we return zero */ 86 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) 87 return false; 88 89 return true; 90 } 91 92 static bool 93 ip_checkentry(const struct ipt_ip *ip) 94 { 95 if (ip->flags & ~IPT_F_MASK) 96 return false; 97 if (ip->invflags & ~IPT_INV_MASK) 98 return false; 99 return true; 100 } 101 102 static unsigned int 103 ipt_error(struct sk_buff *skb, const struct xt_action_param *par) 104 { 105 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo); 106 107 return NF_DROP; 108 } 109 110 /* Performance critical */ 111 static inline struct ipt_entry * 112 get_entry(const void *base, unsigned int offset) 113 { 114 return (struct ipt_entry *)(base + offset); 115 } 116 117 /* All zeroes == unconditional rule. */ 118 /* Mildly perf critical (only if packet tracing is on) */ 119 static inline bool unconditional(const struct ipt_entry *e) 120 { 121 static const struct ipt_ip uncond; 122 123 return e->target_offset == sizeof(struct ipt_entry) && 124 memcmp(&e->ip, &uncond, sizeof(uncond)) == 0; 125 #undef FWINV 126 } 127 128 /* for const-correctness */ 129 static inline const struct xt_entry_target * 130 ipt_get_target_c(const struct ipt_entry *e) 131 { 132 return ipt_get_target((struct ipt_entry *)e); 133 } 134 135 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) 136 static const char *const hooknames[] = { 137 [NF_INET_PRE_ROUTING] = "PREROUTING", 138 [NF_INET_LOCAL_IN] = "INPUT", 139 [NF_INET_FORWARD] = "FORWARD", 140 [NF_INET_LOCAL_OUT] = "OUTPUT", 141 [NF_INET_POST_ROUTING] = "POSTROUTING", 142 }; 143 144 enum nf_ip_trace_comments { 145 NF_IP_TRACE_COMMENT_RULE, 146 NF_IP_TRACE_COMMENT_RETURN, 147 NF_IP_TRACE_COMMENT_POLICY, 148 }; 149 150 static const char *const comments[] = { 151 [NF_IP_TRACE_COMMENT_RULE] = "rule", 152 [NF_IP_TRACE_COMMENT_RETURN] = "return", 153 [NF_IP_TRACE_COMMENT_POLICY] = "policy", 154 }; 155 156 static struct nf_loginfo trace_loginfo = { 157 .type = NF_LOG_TYPE_LOG, 158 .u = { 159 .log = { 160 .level = 4, 161 .logflags = NF_LOG_MASK, 162 }, 163 }, 164 }; 165 166 /* Mildly perf critical (only if packet tracing is on) */ 167 static inline int 168 get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e, 169 const char *hookname, const char **chainname, 170 const char **comment, unsigned int *rulenum) 171 { 172 const struct xt_standard_target *t = (void *)ipt_get_target_c(s); 173 174 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) { 175 /* Head of user chain: ERROR target with chainname */ 176 *chainname = t->target.data; 177 (*rulenum) = 0; 178 } else if (s == e) { 179 (*rulenum)++; 180 181 if (unconditional(s) && 182 strcmp(t->target.u.kernel.target->name, 183 XT_STANDARD_TARGET) == 0 && 184 t->verdict < 0) { 185 /* Tail of chains: STANDARD target (return/policy) */ 186 *comment = *chainname == hookname 187 ? comments[NF_IP_TRACE_COMMENT_POLICY] 188 : comments[NF_IP_TRACE_COMMENT_RETURN]; 189 } 190 return 1; 191 } else 192 (*rulenum)++; 193 194 return 0; 195 } 196 197 static void trace_packet(struct net *net, 198 const struct sk_buff *skb, 199 unsigned int hook, 200 const struct net_device *in, 201 const struct net_device *out, 202 const char *tablename, 203 const struct xt_table_info *private, 204 const struct ipt_entry *e) 205 { 206 const struct ipt_entry *root; 207 const char *hookname, *chainname, *comment; 208 const struct ipt_entry *iter; 209 unsigned int rulenum = 0; 210 211 root = get_entry(private->entries, private->hook_entry[hook]); 212 213 hookname = chainname = hooknames[hook]; 214 comment = comments[NF_IP_TRACE_COMMENT_RULE]; 215 216 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook]) 217 if (get_chainname_rulenum(iter, e, hookname, 218 &chainname, &comment, &rulenum) != 0) 219 break; 220 221 nf_log_trace(net, AF_INET, hook, skb, in, out, &trace_loginfo, 222 "TRACE: %s:%s:%s:%u ", 223 tablename, chainname, comment, rulenum); 224 } 225 #endif 226 227 static inline 228 struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry) 229 { 230 return (void *)entry + entry->next_offset; 231 } 232 233 /* Returns one of the generic firewall policies, like NF_ACCEPT. */ 234 unsigned int 235 ipt_do_table(struct sk_buff *skb, 236 const struct nf_hook_state *state, 237 struct xt_table *table) 238 { 239 unsigned int hook = state->hook; 240 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long)))); 241 const struct iphdr *ip; 242 /* Initializing verdict to NF_DROP keeps gcc happy. */ 243 unsigned int verdict = NF_DROP; 244 const char *indev, *outdev; 245 const void *table_base; 246 struct ipt_entry *e, **jumpstack; 247 unsigned int stackidx, cpu; 248 const struct xt_table_info *private; 249 struct xt_action_param acpar; 250 unsigned int addend; 251 252 /* Initialization */ 253 stackidx = 0; 254 ip = ip_hdr(skb); 255 indev = state->in ? state->in->name : nulldevname; 256 outdev = state->out ? state->out->name : nulldevname; 257 /* We handle fragments by dealing with the first fragment as 258 * if it was a normal packet. All other fragments are treated 259 * normally, except that they will NEVER match rules that ask 260 * things we don't know, ie. tcp syn flag or ports). If the 261 * rule is also a fragment-specific rule, non-fragments won't 262 * match it. */ 263 acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET; 264 acpar.thoff = ip_hdrlen(skb); 265 acpar.hotdrop = false; 266 acpar.net = state->net; 267 acpar.in = state->in; 268 acpar.out = state->out; 269 acpar.family = NFPROTO_IPV4; 270 acpar.hooknum = hook; 271 272 IP_NF_ASSERT(table->valid_hooks & (1 << hook)); 273 local_bh_disable(); 274 addend = xt_write_recseq_begin(); 275 private = table->private; 276 cpu = smp_processor_id(); 277 /* 278 * Ensure we load private-> members after we've fetched the base 279 * pointer. 280 */ 281 smp_read_barrier_depends(); 282 table_base = private->entries; 283 jumpstack = (struct ipt_entry **)private->jumpstack[cpu]; 284 285 /* Switch to alternate jumpstack if we're being invoked via TEE. 286 * TEE issues XT_CONTINUE verdict on original skb so we must not 287 * clobber the jumpstack. 288 * 289 * For recursion via REJECT or SYNPROXY the stack will be clobbered 290 * but it is no problem since absolute verdict is issued by these. 291 */ 292 if (static_key_false(&xt_tee_enabled)) 293 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated); 294 295 e = get_entry(table_base, private->hook_entry[hook]); 296 297 do { 298 const struct xt_entry_target *t; 299 const struct xt_entry_match *ematch; 300 struct xt_counters *counter; 301 302 IP_NF_ASSERT(e); 303 if (!ip_packet_match(ip, indev, outdev, 304 &e->ip, acpar.fragoff)) { 305 no_match: 306 e = ipt_next_entry(e); 307 continue; 308 } 309 310 xt_ematch_foreach(ematch, e) { 311 acpar.match = ematch->u.kernel.match; 312 acpar.matchinfo = ematch->data; 313 if (!acpar.match->match(skb, &acpar)) 314 goto no_match; 315 } 316 317 counter = xt_get_this_cpu_counter(&e->counters); 318 ADD_COUNTER(*counter, skb->len, 1); 319 320 t = ipt_get_target(e); 321 IP_NF_ASSERT(t->u.kernel.target); 322 323 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) 324 /* The packet is traced: log it */ 325 if (unlikely(skb->nf_trace)) 326 trace_packet(state->net, skb, hook, state->in, 327 state->out, table->name, private, e); 328 #endif 329 /* Standard target? */ 330 if (!t->u.kernel.target->target) { 331 int v; 332 333 v = ((struct xt_standard_target *)t)->verdict; 334 if (v < 0) { 335 /* Pop from stack? */ 336 if (v != XT_RETURN) { 337 verdict = (unsigned int)(-v) - 1; 338 break; 339 } 340 if (stackidx == 0) { 341 e = get_entry(table_base, 342 private->underflow[hook]); 343 } else { 344 e = jumpstack[--stackidx]; 345 e = ipt_next_entry(e); 346 } 347 continue; 348 } 349 if (table_base + v != ipt_next_entry(e) && 350 !(e->ip.flags & IPT_F_GOTO)) 351 jumpstack[stackidx++] = e; 352 353 e = get_entry(table_base, v); 354 continue; 355 } 356 357 acpar.target = t->u.kernel.target; 358 acpar.targinfo = t->data; 359 360 verdict = t->u.kernel.target->target(skb, &acpar); 361 /* Target might have changed stuff. */ 362 ip = ip_hdr(skb); 363 if (verdict == XT_CONTINUE) 364 e = ipt_next_entry(e); 365 else 366 /* Verdict */ 367 break; 368 } while (!acpar.hotdrop); 369 370 xt_write_recseq_end(addend); 371 local_bh_enable(); 372 373 if (acpar.hotdrop) 374 return NF_DROP; 375 else return verdict; 376 } 377 378 static bool find_jump_target(const struct xt_table_info *t, 379 const struct ipt_entry *target) 380 { 381 struct ipt_entry *iter; 382 383 xt_entry_foreach(iter, t->entries, t->size) { 384 if (iter == target) 385 return true; 386 } 387 return false; 388 } 389 390 /* Figures out from what hook each rule can be called: returns 0 if 391 there are loops. Puts hook bitmask in comefrom. */ 392 static int 393 mark_source_chains(const struct xt_table_info *newinfo, 394 unsigned int valid_hooks, void *entry0) 395 { 396 unsigned int hook; 397 398 /* No recursion; use packet counter to save back ptrs (reset 399 to 0 as we leave), and comefrom to save source hook bitmask */ 400 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) { 401 unsigned int pos = newinfo->hook_entry[hook]; 402 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos); 403 404 if (!(valid_hooks & (1 << hook))) 405 continue; 406 407 /* Set initial back pointer. */ 408 e->counters.pcnt = pos; 409 410 for (;;) { 411 const struct xt_standard_target *t 412 = (void *)ipt_get_target_c(e); 413 int visited = e->comefrom & (1 << hook); 414 415 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) 416 return 0; 417 418 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS)); 419 420 /* Unconditional return/END. */ 421 if ((unconditional(e) && 422 (strcmp(t->target.u.user.name, 423 XT_STANDARD_TARGET) == 0) && 424 t->verdict < 0) || visited) { 425 unsigned int oldpos, size; 426 427 if ((strcmp(t->target.u.user.name, 428 XT_STANDARD_TARGET) == 0) && 429 t->verdict < -NF_MAX_VERDICT - 1) 430 return 0; 431 432 /* Return: backtrack through the last 433 big jump. */ 434 do { 435 e->comefrom ^= (1<<NF_INET_NUMHOOKS); 436 oldpos = pos; 437 pos = e->counters.pcnt; 438 e->counters.pcnt = 0; 439 440 /* We're at the start. */ 441 if (pos == oldpos) 442 goto next; 443 444 e = (struct ipt_entry *) 445 (entry0 + pos); 446 } while (oldpos == pos + e->next_offset); 447 448 /* Move along one */ 449 size = e->next_offset; 450 e = (struct ipt_entry *) 451 (entry0 + pos + size); 452 if (pos + size >= newinfo->size) 453 return 0; 454 e->counters.pcnt = pos; 455 pos += size; 456 } else { 457 int newpos = t->verdict; 458 459 if (strcmp(t->target.u.user.name, 460 XT_STANDARD_TARGET) == 0 && 461 newpos >= 0) { 462 /* This a jump; chase it. */ 463 e = (struct ipt_entry *) 464 (entry0 + newpos); 465 if (!find_jump_target(newinfo, e)) 466 return 0; 467 } else { 468 /* ... this is a fallthru */ 469 newpos = pos + e->next_offset; 470 if (newpos >= newinfo->size) 471 return 0; 472 } 473 e = (struct ipt_entry *) 474 (entry0 + newpos); 475 e->counters.pcnt = pos; 476 pos = newpos; 477 } 478 } 479 next: ; 480 } 481 return 1; 482 } 483 484 static void cleanup_match(struct xt_entry_match *m, struct net *net) 485 { 486 struct xt_mtdtor_param par; 487 488 par.net = net; 489 par.match = m->u.kernel.match; 490 par.matchinfo = m->data; 491 par.family = NFPROTO_IPV4; 492 if (par.match->destroy != NULL) 493 par.match->destroy(&par); 494 module_put(par.match->me); 495 } 496 497 static int 498 check_match(struct xt_entry_match *m, struct xt_mtchk_param *par) 499 { 500 const struct ipt_ip *ip = par->entryinfo; 501 502 par->match = m->u.kernel.match; 503 par->matchinfo = m->data; 504 505 return xt_check_match(par, m->u.match_size - sizeof(*m), 506 ip->proto, ip->invflags & IPT_INV_PROTO); 507 } 508 509 static int 510 find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par) 511 { 512 struct xt_match *match; 513 int ret; 514 515 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name, 516 m->u.user.revision); 517 if (IS_ERR(match)) 518 return PTR_ERR(match); 519 m->u.kernel.match = match; 520 521 ret = check_match(m, par); 522 if (ret) 523 goto err; 524 525 return 0; 526 err: 527 module_put(m->u.kernel.match->me); 528 return ret; 529 } 530 531 static int check_target(struct ipt_entry *e, struct net *net, const char *name) 532 { 533 struct xt_entry_target *t = ipt_get_target(e); 534 struct xt_tgchk_param par = { 535 .net = net, 536 .table = name, 537 .entryinfo = e, 538 .target = t->u.kernel.target, 539 .targinfo = t->data, 540 .hook_mask = e->comefrom, 541 .family = NFPROTO_IPV4, 542 }; 543 544 return xt_check_target(&par, t->u.target_size - sizeof(*t), 545 e->ip.proto, e->ip.invflags & IPT_INV_PROTO); 546 } 547 548 static int 549 find_check_entry(struct ipt_entry *e, struct net *net, const char *name, 550 unsigned int size) 551 { 552 struct xt_entry_target *t; 553 struct xt_target *target; 554 int ret; 555 unsigned int j; 556 struct xt_mtchk_param mtpar; 557 struct xt_entry_match *ematch; 558 unsigned long pcnt; 559 560 pcnt = xt_percpu_counter_alloc(); 561 if (IS_ERR_VALUE(pcnt)) 562 return -ENOMEM; 563 e->counters.pcnt = pcnt; 564 565 j = 0; 566 mtpar.net = net; 567 mtpar.table = name; 568 mtpar.entryinfo = &e->ip; 569 mtpar.hook_mask = e->comefrom; 570 mtpar.family = NFPROTO_IPV4; 571 xt_ematch_foreach(ematch, e) { 572 ret = find_check_match(ematch, &mtpar); 573 if (ret != 0) 574 goto cleanup_matches; 575 ++j; 576 } 577 578 t = ipt_get_target(e); 579 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name, 580 t->u.user.revision); 581 if (IS_ERR(target)) { 582 ret = PTR_ERR(target); 583 goto cleanup_matches; 584 } 585 t->u.kernel.target = target; 586 587 ret = check_target(e, net, name); 588 if (ret) 589 goto err; 590 591 return 0; 592 err: 593 module_put(t->u.kernel.target->me); 594 cleanup_matches: 595 xt_ematch_foreach(ematch, e) { 596 if (j-- == 0) 597 break; 598 cleanup_match(ematch, net); 599 } 600 601 xt_percpu_counter_free(e->counters.pcnt); 602 603 return ret; 604 } 605 606 static bool check_underflow(const struct ipt_entry *e) 607 { 608 const struct xt_entry_target *t; 609 unsigned int verdict; 610 611 if (!unconditional(e)) 612 return false; 613 t = ipt_get_target_c(e); 614 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0) 615 return false; 616 verdict = ((struct xt_standard_target *)t)->verdict; 617 verdict = -verdict - 1; 618 return verdict == NF_DROP || verdict == NF_ACCEPT; 619 } 620 621 static int 622 check_entry_size_and_hooks(struct ipt_entry *e, 623 struct xt_table_info *newinfo, 624 const unsigned char *base, 625 const unsigned char *limit, 626 const unsigned int *hook_entries, 627 const unsigned int *underflows, 628 unsigned int valid_hooks) 629 { 630 unsigned int h; 631 int err; 632 633 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 || 634 (unsigned char *)e + sizeof(struct ipt_entry) >= limit || 635 (unsigned char *)e + e->next_offset > limit) 636 return -EINVAL; 637 638 if (e->next_offset 639 < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target)) 640 return -EINVAL; 641 642 if (!ip_checkentry(&e->ip)) 643 return -EINVAL; 644 645 err = xt_check_entry_offsets(e, e->elems, e->target_offset, 646 e->next_offset); 647 if (err) 648 return err; 649 650 /* Check hooks & underflows */ 651 for (h = 0; h < NF_INET_NUMHOOKS; h++) { 652 if (!(valid_hooks & (1 << h))) 653 continue; 654 if ((unsigned char *)e - base == hook_entries[h]) 655 newinfo->hook_entry[h] = hook_entries[h]; 656 if ((unsigned char *)e - base == underflows[h]) { 657 if (!check_underflow(e)) 658 return -EINVAL; 659 660 newinfo->underflow[h] = underflows[h]; 661 } 662 } 663 664 /* Clear counters and comefrom */ 665 e->counters = ((struct xt_counters) { 0, 0 }); 666 e->comefrom = 0; 667 return 0; 668 } 669 670 static void 671 cleanup_entry(struct ipt_entry *e, struct net *net) 672 { 673 struct xt_tgdtor_param par; 674 struct xt_entry_target *t; 675 struct xt_entry_match *ematch; 676 677 /* Cleanup all matches */ 678 xt_ematch_foreach(ematch, e) 679 cleanup_match(ematch, net); 680 t = ipt_get_target(e); 681 682 par.net = net; 683 par.target = t->u.kernel.target; 684 par.targinfo = t->data; 685 par.family = NFPROTO_IPV4; 686 if (par.target->destroy != NULL) 687 par.target->destroy(&par); 688 module_put(par.target->me); 689 xt_percpu_counter_free(e->counters.pcnt); 690 } 691 692 /* Checks and translates the user-supplied table segment (held in 693 newinfo) */ 694 static int 695 translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0, 696 const struct ipt_replace *repl) 697 { 698 struct ipt_entry *iter; 699 unsigned int i; 700 int ret = 0; 701 702 newinfo->size = repl->size; 703 newinfo->number = repl->num_entries; 704 705 /* Init all hooks to impossible value. */ 706 for (i = 0; i < NF_INET_NUMHOOKS; i++) { 707 newinfo->hook_entry[i] = 0xFFFFFFFF; 708 newinfo->underflow[i] = 0xFFFFFFFF; 709 } 710 711 i = 0; 712 /* Walk through entries, checking offsets. */ 713 xt_entry_foreach(iter, entry0, newinfo->size) { 714 ret = check_entry_size_and_hooks(iter, newinfo, entry0, 715 entry0 + repl->size, 716 repl->hook_entry, 717 repl->underflow, 718 repl->valid_hooks); 719 if (ret != 0) 720 return ret; 721 ++i; 722 if (strcmp(ipt_get_target(iter)->u.user.name, 723 XT_ERROR_TARGET) == 0) 724 ++newinfo->stacksize; 725 } 726 727 if (i != repl->num_entries) 728 return -EINVAL; 729 730 /* Check hooks all assigned */ 731 for (i = 0; i < NF_INET_NUMHOOKS; i++) { 732 /* Only hooks which are valid */ 733 if (!(repl->valid_hooks & (1 << i))) 734 continue; 735 if (newinfo->hook_entry[i] == 0xFFFFFFFF) 736 return -EINVAL; 737 if (newinfo->underflow[i] == 0xFFFFFFFF) 738 return -EINVAL; 739 } 740 741 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) 742 return -ELOOP; 743 744 /* Finally, each sanity check must pass */ 745 i = 0; 746 xt_entry_foreach(iter, entry0, newinfo->size) { 747 ret = find_check_entry(iter, net, repl->name, repl->size); 748 if (ret != 0) 749 break; 750 ++i; 751 } 752 753 if (ret != 0) { 754 xt_entry_foreach(iter, entry0, newinfo->size) { 755 if (i-- == 0) 756 break; 757 cleanup_entry(iter, net); 758 } 759 return ret; 760 } 761 762 return ret; 763 } 764 765 static void 766 get_counters(const struct xt_table_info *t, 767 struct xt_counters counters[]) 768 { 769 struct ipt_entry *iter; 770 unsigned int cpu; 771 unsigned int i; 772 773 for_each_possible_cpu(cpu) { 774 seqcount_t *s = &per_cpu(xt_recseq, cpu); 775 776 i = 0; 777 xt_entry_foreach(iter, t->entries, t->size) { 778 struct xt_counters *tmp; 779 u64 bcnt, pcnt; 780 unsigned int start; 781 782 tmp = xt_get_per_cpu_counter(&iter->counters, cpu); 783 do { 784 start = read_seqcount_begin(s); 785 bcnt = tmp->bcnt; 786 pcnt = tmp->pcnt; 787 } while (read_seqcount_retry(s, start)); 788 789 ADD_COUNTER(counters[i], bcnt, pcnt); 790 ++i; /* macro does multi eval of i */ 791 } 792 } 793 } 794 795 static struct xt_counters *alloc_counters(const struct xt_table *table) 796 { 797 unsigned int countersize; 798 struct xt_counters *counters; 799 const struct xt_table_info *private = table->private; 800 801 /* We need atomic snapshot of counters: rest doesn't change 802 (other than comefrom, which userspace doesn't care 803 about). */ 804 countersize = sizeof(struct xt_counters) * private->number; 805 counters = vzalloc(countersize); 806 807 if (counters == NULL) 808 return ERR_PTR(-ENOMEM); 809 810 get_counters(private, counters); 811 812 return counters; 813 } 814 815 static int 816 copy_entries_to_user(unsigned int total_size, 817 const struct xt_table *table, 818 void __user *userptr) 819 { 820 unsigned int off, num; 821 const struct ipt_entry *e; 822 struct xt_counters *counters; 823 const struct xt_table_info *private = table->private; 824 int ret = 0; 825 const void *loc_cpu_entry; 826 827 counters = alloc_counters(table); 828 if (IS_ERR(counters)) 829 return PTR_ERR(counters); 830 831 loc_cpu_entry = private->entries; 832 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) { 833 ret = -EFAULT; 834 goto free_counters; 835 } 836 837 /* FIXME: use iterator macros --RR */ 838 /* ... then go back and fix counters and names */ 839 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){ 840 unsigned int i; 841 const struct xt_entry_match *m; 842 const struct xt_entry_target *t; 843 844 e = (struct ipt_entry *)(loc_cpu_entry + off); 845 if (copy_to_user(userptr + off 846 + offsetof(struct ipt_entry, counters), 847 &counters[num], 848 sizeof(counters[num])) != 0) { 849 ret = -EFAULT; 850 goto free_counters; 851 } 852 853 for (i = sizeof(struct ipt_entry); 854 i < e->target_offset; 855 i += m->u.match_size) { 856 m = (void *)e + i; 857 858 if (copy_to_user(userptr + off + i 859 + offsetof(struct xt_entry_match, 860 u.user.name), 861 m->u.kernel.match->name, 862 strlen(m->u.kernel.match->name)+1) 863 != 0) { 864 ret = -EFAULT; 865 goto free_counters; 866 } 867 } 868 869 t = ipt_get_target_c(e); 870 if (copy_to_user(userptr + off + e->target_offset 871 + offsetof(struct xt_entry_target, 872 u.user.name), 873 t->u.kernel.target->name, 874 strlen(t->u.kernel.target->name)+1) != 0) { 875 ret = -EFAULT; 876 goto free_counters; 877 } 878 } 879 880 free_counters: 881 vfree(counters); 882 return ret; 883 } 884 885 #ifdef CONFIG_COMPAT 886 static void compat_standard_from_user(void *dst, const void *src) 887 { 888 int v = *(compat_int_t *)src; 889 890 if (v > 0) 891 v += xt_compat_calc_jump(AF_INET, v); 892 memcpy(dst, &v, sizeof(v)); 893 } 894 895 static int compat_standard_to_user(void __user *dst, const void *src) 896 { 897 compat_int_t cv = *(int *)src; 898 899 if (cv > 0) 900 cv -= xt_compat_calc_jump(AF_INET, cv); 901 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0; 902 } 903 904 static int compat_calc_entry(const struct ipt_entry *e, 905 const struct xt_table_info *info, 906 const void *base, struct xt_table_info *newinfo) 907 { 908 const struct xt_entry_match *ematch; 909 const struct xt_entry_target *t; 910 unsigned int entry_offset; 911 int off, i, ret; 912 913 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry); 914 entry_offset = (void *)e - base; 915 xt_ematch_foreach(ematch, e) 916 off += xt_compat_match_offset(ematch->u.kernel.match); 917 t = ipt_get_target_c(e); 918 off += xt_compat_target_offset(t->u.kernel.target); 919 newinfo->size -= off; 920 ret = xt_compat_add_offset(AF_INET, entry_offset, off); 921 if (ret) 922 return ret; 923 924 for (i = 0; i < NF_INET_NUMHOOKS; i++) { 925 if (info->hook_entry[i] && 926 (e < (struct ipt_entry *)(base + info->hook_entry[i]))) 927 newinfo->hook_entry[i] -= off; 928 if (info->underflow[i] && 929 (e < (struct ipt_entry *)(base + info->underflow[i]))) 930 newinfo->underflow[i] -= off; 931 } 932 return 0; 933 } 934 935 static int compat_table_info(const struct xt_table_info *info, 936 struct xt_table_info *newinfo) 937 { 938 struct ipt_entry *iter; 939 const void *loc_cpu_entry; 940 int ret; 941 942 if (!newinfo || !info) 943 return -EINVAL; 944 945 /* we dont care about newinfo->entries */ 946 memcpy(newinfo, info, offsetof(struct xt_table_info, entries)); 947 newinfo->initial_entries = 0; 948 loc_cpu_entry = info->entries; 949 xt_compat_init_offsets(AF_INET, info->number); 950 xt_entry_foreach(iter, loc_cpu_entry, info->size) { 951 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo); 952 if (ret != 0) 953 return ret; 954 } 955 return 0; 956 } 957 #endif 958 959 static int get_info(struct net *net, void __user *user, 960 const int *len, int compat) 961 { 962 char name[XT_TABLE_MAXNAMELEN]; 963 struct xt_table *t; 964 int ret; 965 966 if (*len != sizeof(struct ipt_getinfo)) 967 return -EINVAL; 968 969 if (copy_from_user(name, user, sizeof(name)) != 0) 970 return -EFAULT; 971 972 name[XT_TABLE_MAXNAMELEN-1] = '\0'; 973 #ifdef CONFIG_COMPAT 974 if (compat) 975 xt_compat_lock(AF_INET); 976 #endif 977 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name), 978 "iptable_%s", name); 979 if (!IS_ERR_OR_NULL(t)) { 980 struct ipt_getinfo info; 981 const struct xt_table_info *private = t->private; 982 #ifdef CONFIG_COMPAT 983 struct xt_table_info tmp; 984 985 if (compat) { 986 ret = compat_table_info(private, &tmp); 987 xt_compat_flush_offsets(AF_INET); 988 private = &tmp; 989 } 990 #endif 991 memset(&info, 0, sizeof(info)); 992 info.valid_hooks = t->valid_hooks; 993 memcpy(info.hook_entry, private->hook_entry, 994 sizeof(info.hook_entry)); 995 memcpy(info.underflow, private->underflow, 996 sizeof(info.underflow)); 997 info.num_entries = private->number; 998 info.size = private->size; 999 strcpy(info.name, name); 1000 1001 if (copy_to_user(user, &info, *len) != 0) 1002 ret = -EFAULT; 1003 else 1004 ret = 0; 1005 1006 xt_table_unlock(t); 1007 module_put(t->me); 1008 } else 1009 ret = t ? PTR_ERR(t) : -ENOENT; 1010 #ifdef CONFIG_COMPAT 1011 if (compat) 1012 xt_compat_unlock(AF_INET); 1013 #endif 1014 return ret; 1015 } 1016 1017 static int 1018 get_entries(struct net *net, struct ipt_get_entries __user *uptr, 1019 const int *len) 1020 { 1021 int ret; 1022 struct ipt_get_entries get; 1023 struct xt_table *t; 1024 1025 if (*len < sizeof(get)) 1026 return -EINVAL; 1027 if (copy_from_user(&get, uptr, sizeof(get)) != 0) 1028 return -EFAULT; 1029 if (*len != sizeof(struct ipt_get_entries) + get.size) 1030 return -EINVAL; 1031 get.name[sizeof(get.name) - 1] = '\0'; 1032 1033 t = xt_find_table_lock(net, AF_INET, get.name); 1034 if (!IS_ERR_OR_NULL(t)) { 1035 const struct xt_table_info *private = t->private; 1036 if (get.size == private->size) 1037 ret = copy_entries_to_user(private->size, 1038 t, uptr->entrytable); 1039 else 1040 ret = -EAGAIN; 1041 1042 module_put(t->me); 1043 xt_table_unlock(t); 1044 } else 1045 ret = t ? PTR_ERR(t) : -ENOENT; 1046 1047 return ret; 1048 } 1049 1050 static int 1051 __do_replace(struct net *net, const char *name, unsigned int valid_hooks, 1052 struct xt_table_info *newinfo, unsigned int num_counters, 1053 void __user *counters_ptr) 1054 { 1055 int ret; 1056 struct xt_table *t; 1057 struct xt_table_info *oldinfo; 1058 struct xt_counters *counters; 1059 struct ipt_entry *iter; 1060 1061 ret = 0; 1062 counters = vzalloc(num_counters * sizeof(struct xt_counters)); 1063 if (!counters) { 1064 ret = -ENOMEM; 1065 goto out; 1066 } 1067 1068 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name), 1069 "iptable_%s", name); 1070 if (IS_ERR_OR_NULL(t)) { 1071 ret = t ? PTR_ERR(t) : -ENOENT; 1072 goto free_newinfo_counters_untrans; 1073 } 1074 1075 /* You lied! */ 1076 if (valid_hooks != t->valid_hooks) { 1077 ret = -EINVAL; 1078 goto put_module; 1079 } 1080 1081 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret); 1082 if (!oldinfo) 1083 goto put_module; 1084 1085 /* Update module usage count based on number of rules */ 1086 if ((oldinfo->number > oldinfo->initial_entries) || 1087 (newinfo->number <= oldinfo->initial_entries)) 1088 module_put(t->me); 1089 if ((oldinfo->number > oldinfo->initial_entries) && 1090 (newinfo->number <= oldinfo->initial_entries)) 1091 module_put(t->me); 1092 1093 /* Get the old counters, and synchronize with replace */ 1094 get_counters(oldinfo, counters); 1095 1096 /* Decrease module usage counts and free resource */ 1097 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size) 1098 cleanup_entry(iter, net); 1099 1100 xt_free_table_info(oldinfo); 1101 if (copy_to_user(counters_ptr, counters, 1102 sizeof(struct xt_counters) * num_counters) != 0) { 1103 /* Silent error, can't fail, new table is already in place */ 1104 net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n"); 1105 } 1106 vfree(counters); 1107 xt_table_unlock(t); 1108 return ret; 1109 1110 put_module: 1111 module_put(t->me); 1112 xt_table_unlock(t); 1113 free_newinfo_counters_untrans: 1114 vfree(counters); 1115 out: 1116 return ret; 1117 } 1118 1119 static int 1120 do_replace(struct net *net, const void __user *user, unsigned int len) 1121 { 1122 int ret; 1123 struct ipt_replace tmp; 1124 struct xt_table_info *newinfo; 1125 void *loc_cpu_entry; 1126 struct ipt_entry *iter; 1127 1128 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) 1129 return -EFAULT; 1130 1131 /* overflow check */ 1132 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters)) 1133 return -ENOMEM; 1134 if (tmp.num_counters == 0) 1135 return -EINVAL; 1136 1137 tmp.name[sizeof(tmp.name)-1] = 0; 1138 1139 newinfo = xt_alloc_table_info(tmp.size); 1140 if (!newinfo) 1141 return -ENOMEM; 1142 1143 loc_cpu_entry = newinfo->entries; 1144 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), 1145 tmp.size) != 0) { 1146 ret = -EFAULT; 1147 goto free_newinfo; 1148 } 1149 1150 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp); 1151 if (ret != 0) 1152 goto free_newinfo; 1153 1154 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo, 1155 tmp.num_counters, tmp.counters); 1156 if (ret) 1157 goto free_newinfo_untrans; 1158 return 0; 1159 1160 free_newinfo_untrans: 1161 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) 1162 cleanup_entry(iter, net); 1163 free_newinfo: 1164 xt_free_table_info(newinfo); 1165 return ret; 1166 } 1167 1168 static int 1169 do_add_counters(struct net *net, const void __user *user, 1170 unsigned int len, int compat) 1171 { 1172 unsigned int i; 1173 struct xt_counters_info tmp; 1174 struct xt_counters *paddc; 1175 struct xt_table *t; 1176 const struct xt_table_info *private; 1177 int ret = 0; 1178 struct ipt_entry *iter; 1179 unsigned int addend; 1180 1181 paddc = xt_copy_counters_from_user(user, len, &tmp, compat); 1182 if (IS_ERR(paddc)) 1183 return PTR_ERR(paddc); 1184 1185 t = xt_find_table_lock(net, AF_INET, tmp.name); 1186 if (IS_ERR_OR_NULL(t)) { 1187 ret = t ? PTR_ERR(t) : -ENOENT; 1188 goto free; 1189 } 1190 1191 local_bh_disable(); 1192 private = t->private; 1193 if (private->number != tmp.num_counters) { 1194 ret = -EINVAL; 1195 goto unlock_up_free; 1196 } 1197 1198 i = 0; 1199 addend = xt_write_recseq_begin(); 1200 xt_entry_foreach(iter, private->entries, private->size) { 1201 struct xt_counters *tmp; 1202 1203 tmp = xt_get_this_cpu_counter(&iter->counters); 1204 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt); 1205 ++i; 1206 } 1207 xt_write_recseq_end(addend); 1208 unlock_up_free: 1209 local_bh_enable(); 1210 xt_table_unlock(t); 1211 module_put(t->me); 1212 free: 1213 vfree(paddc); 1214 1215 return ret; 1216 } 1217 1218 #ifdef CONFIG_COMPAT 1219 struct compat_ipt_replace { 1220 char name[XT_TABLE_MAXNAMELEN]; 1221 u32 valid_hooks; 1222 u32 num_entries; 1223 u32 size; 1224 u32 hook_entry[NF_INET_NUMHOOKS]; 1225 u32 underflow[NF_INET_NUMHOOKS]; 1226 u32 num_counters; 1227 compat_uptr_t counters; /* struct xt_counters * */ 1228 struct compat_ipt_entry entries[0]; 1229 }; 1230 1231 static int 1232 compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr, 1233 unsigned int *size, struct xt_counters *counters, 1234 unsigned int i) 1235 { 1236 struct xt_entry_target *t; 1237 struct compat_ipt_entry __user *ce; 1238 u_int16_t target_offset, next_offset; 1239 compat_uint_t origsize; 1240 const struct xt_entry_match *ematch; 1241 int ret = 0; 1242 1243 origsize = *size; 1244 ce = (struct compat_ipt_entry __user *)*dstptr; 1245 if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 || 1246 copy_to_user(&ce->counters, &counters[i], 1247 sizeof(counters[i])) != 0) 1248 return -EFAULT; 1249 1250 *dstptr += sizeof(struct compat_ipt_entry); 1251 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry); 1252 1253 xt_ematch_foreach(ematch, e) { 1254 ret = xt_compat_match_to_user(ematch, dstptr, size); 1255 if (ret != 0) 1256 return ret; 1257 } 1258 target_offset = e->target_offset - (origsize - *size); 1259 t = ipt_get_target(e); 1260 ret = xt_compat_target_to_user(t, dstptr, size); 1261 if (ret) 1262 return ret; 1263 next_offset = e->next_offset - (origsize - *size); 1264 if (put_user(target_offset, &ce->target_offset) != 0 || 1265 put_user(next_offset, &ce->next_offset) != 0) 1266 return -EFAULT; 1267 return 0; 1268 } 1269 1270 static int 1271 compat_find_calc_match(struct xt_entry_match *m, 1272 const struct ipt_ip *ip, 1273 int *size) 1274 { 1275 struct xt_match *match; 1276 1277 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name, 1278 m->u.user.revision); 1279 if (IS_ERR(match)) 1280 return PTR_ERR(match); 1281 1282 m->u.kernel.match = match; 1283 *size += xt_compat_match_offset(match); 1284 return 0; 1285 } 1286 1287 static void compat_release_entry(struct compat_ipt_entry *e) 1288 { 1289 struct xt_entry_target *t; 1290 struct xt_entry_match *ematch; 1291 1292 /* Cleanup all matches */ 1293 xt_ematch_foreach(ematch, e) 1294 module_put(ematch->u.kernel.match->me); 1295 t = compat_ipt_get_target(e); 1296 module_put(t->u.kernel.target->me); 1297 } 1298 1299 static int 1300 check_compat_entry_size_and_hooks(struct compat_ipt_entry *e, 1301 struct xt_table_info *newinfo, 1302 unsigned int *size, 1303 const unsigned char *base, 1304 const unsigned char *limit) 1305 { 1306 struct xt_entry_match *ematch; 1307 struct xt_entry_target *t; 1308 struct xt_target *target; 1309 unsigned int entry_offset; 1310 unsigned int j; 1311 int ret, off; 1312 1313 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 || 1314 (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit || 1315 (unsigned char *)e + e->next_offset > limit) 1316 return -EINVAL; 1317 1318 if (e->next_offset < sizeof(struct compat_ipt_entry) + 1319 sizeof(struct compat_xt_entry_target)) 1320 return -EINVAL; 1321 1322 if (!ip_checkentry(&e->ip)) 1323 return -EINVAL; 1324 1325 ret = xt_compat_check_entry_offsets(e, e->elems, 1326 e->target_offset, e->next_offset); 1327 if (ret) 1328 return ret; 1329 1330 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry); 1331 entry_offset = (void *)e - (void *)base; 1332 j = 0; 1333 xt_ematch_foreach(ematch, e) { 1334 ret = compat_find_calc_match(ematch, &e->ip, &off); 1335 if (ret != 0) 1336 goto release_matches; 1337 ++j; 1338 } 1339 1340 t = compat_ipt_get_target(e); 1341 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name, 1342 t->u.user.revision); 1343 if (IS_ERR(target)) { 1344 ret = PTR_ERR(target); 1345 goto release_matches; 1346 } 1347 t->u.kernel.target = target; 1348 1349 off += xt_compat_target_offset(target); 1350 *size += off; 1351 ret = xt_compat_add_offset(AF_INET, entry_offset, off); 1352 if (ret) 1353 goto out; 1354 1355 return 0; 1356 1357 out: 1358 module_put(t->u.kernel.target->me); 1359 release_matches: 1360 xt_ematch_foreach(ematch, e) { 1361 if (j-- == 0) 1362 break; 1363 module_put(ematch->u.kernel.match->me); 1364 } 1365 return ret; 1366 } 1367 1368 static void 1369 compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr, 1370 unsigned int *size, 1371 struct xt_table_info *newinfo, unsigned char *base) 1372 { 1373 struct xt_entry_target *t; 1374 struct xt_target *target; 1375 struct ipt_entry *de; 1376 unsigned int origsize; 1377 int h; 1378 struct xt_entry_match *ematch; 1379 1380 origsize = *size; 1381 de = (struct ipt_entry *)*dstptr; 1382 memcpy(de, e, sizeof(struct ipt_entry)); 1383 memcpy(&de->counters, &e->counters, sizeof(e->counters)); 1384 1385 *dstptr += sizeof(struct ipt_entry); 1386 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry); 1387 1388 xt_ematch_foreach(ematch, e) 1389 xt_compat_match_from_user(ematch, dstptr, size); 1390 1391 de->target_offset = e->target_offset - (origsize - *size); 1392 t = compat_ipt_get_target(e); 1393 target = t->u.kernel.target; 1394 xt_compat_target_from_user(t, dstptr, size); 1395 1396 de->next_offset = e->next_offset - (origsize - *size); 1397 1398 for (h = 0; h < NF_INET_NUMHOOKS; h++) { 1399 if ((unsigned char *)de - base < newinfo->hook_entry[h]) 1400 newinfo->hook_entry[h] -= origsize - *size; 1401 if ((unsigned char *)de - base < newinfo->underflow[h]) 1402 newinfo->underflow[h] -= origsize - *size; 1403 } 1404 } 1405 1406 static int 1407 translate_compat_table(struct net *net, 1408 struct xt_table_info **pinfo, 1409 void **pentry0, 1410 const struct compat_ipt_replace *compatr) 1411 { 1412 unsigned int i, j; 1413 struct xt_table_info *newinfo, *info; 1414 void *pos, *entry0, *entry1; 1415 struct compat_ipt_entry *iter0; 1416 struct ipt_replace repl; 1417 unsigned int size; 1418 int ret; 1419 1420 info = *pinfo; 1421 entry0 = *pentry0; 1422 size = compatr->size; 1423 info->number = compatr->num_entries; 1424 1425 j = 0; 1426 xt_compat_lock(AF_INET); 1427 xt_compat_init_offsets(AF_INET, compatr->num_entries); 1428 /* Walk through entries, checking offsets. */ 1429 xt_entry_foreach(iter0, entry0, compatr->size) { 1430 ret = check_compat_entry_size_and_hooks(iter0, info, &size, 1431 entry0, 1432 entry0 + compatr->size); 1433 if (ret != 0) 1434 goto out_unlock; 1435 ++j; 1436 } 1437 1438 ret = -EINVAL; 1439 if (j != compatr->num_entries) 1440 goto out_unlock; 1441 1442 ret = -ENOMEM; 1443 newinfo = xt_alloc_table_info(size); 1444 if (!newinfo) 1445 goto out_unlock; 1446 1447 newinfo->number = compatr->num_entries; 1448 for (i = 0; i < NF_INET_NUMHOOKS; i++) { 1449 newinfo->hook_entry[i] = compatr->hook_entry[i]; 1450 newinfo->underflow[i] = compatr->underflow[i]; 1451 } 1452 entry1 = newinfo->entries; 1453 pos = entry1; 1454 size = compatr->size; 1455 xt_entry_foreach(iter0, entry0, compatr->size) 1456 compat_copy_entry_from_user(iter0, &pos, &size, 1457 newinfo, entry1); 1458 1459 /* all module references in entry0 are now gone. 1460 * entry1/newinfo contains a 64bit ruleset that looks exactly as 1461 * generated by 64bit userspace. 1462 * 1463 * Call standard translate_table() to validate all hook_entrys, 1464 * underflows, check for loops, etc. 1465 */ 1466 xt_compat_flush_offsets(AF_INET); 1467 xt_compat_unlock(AF_INET); 1468 1469 memcpy(&repl, compatr, sizeof(*compatr)); 1470 1471 for (i = 0; i < NF_INET_NUMHOOKS; i++) { 1472 repl.hook_entry[i] = newinfo->hook_entry[i]; 1473 repl.underflow[i] = newinfo->underflow[i]; 1474 } 1475 1476 repl.num_counters = 0; 1477 repl.counters = NULL; 1478 repl.size = newinfo->size; 1479 ret = translate_table(net, newinfo, entry1, &repl); 1480 if (ret) 1481 goto free_newinfo; 1482 1483 *pinfo = newinfo; 1484 *pentry0 = entry1; 1485 xt_free_table_info(info); 1486 return 0; 1487 1488 free_newinfo: 1489 xt_free_table_info(newinfo); 1490 return ret; 1491 out_unlock: 1492 xt_compat_flush_offsets(AF_INET); 1493 xt_compat_unlock(AF_INET); 1494 xt_entry_foreach(iter0, entry0, compatr->size) { 1495 if (j-- == 0) 1496 break; 1497 compat_release_entry(iter0); 1498 } 1499 return ret; 1500 } 1501 1502 static int 1503 compat_do_replace(struct net *net, void __user *user, unsigned int len) 1504 { 1505 int ret; 1506 struct compat_ipt_replace tmp; 1507 struct xt_table_info *newinfo; 1508 void *loc_cpu_entry; 1509 struct ipt_entry *iter; 1510 1511 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) 1512 return -EFAULT; 1513 1514 /* overflow check */ 1515 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters)) 1516 return -ENOMEM; 1517 if (tmp.num_counters == 0) 1518 return -EINVAL; 1519 1520 tmp.name[sizeof(tmp.name)-1] = 0; 1521 1522 newinfo = xt_alloc_table_info(tmp.size); 1523 if (!newinfo) 1524 return -ENOMEM; 1525 1526 loc_cpu_entry = newinfo->entries; 1527 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), 1528 tmp.size) != 0) { 1529 ret = -EFAULT; 1530 goto free_newinfo; 1531 } 1532 1533 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp); 1534 if (ret != 0) 1535 goto free_newinfo; 1536 1537 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo, 1538 tmp.num_counters, compat_ptr(tmp.counters)); 1539 if (ret) 1540 goto free_newinfo_untrans; 1541 return 0; 1542 1543 free_newinfo_untrans: 1544 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) 1545 cleanup_entry(iter, net); 1546 free_newinfo: 1547 xt_free_table_info(newinfo); 1548 return ret; 1549 } 1550 1551 static int 1552 compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, 1553 unsigned int len) 1554 { 1555 int ret; 1556 1557 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) 1558 return -EPERM; 1559 1560 switch (cmd) { 1561 case IPT_SO_SET_REPLACE: 1562 ret = compat_do_replace(sock_net(sk), user, len); 1563 break; 1564 1565 case IPT_SO_SET_ADD_COUNTERS: 1566 ret = do_add_counters(sock_net(sk), user, len, 1); 1567 break; 1568 1569 default: 1570 ret = -EINVAL; 1571 } 1572 1573 return ret; 1574 } 1575 1576 struct compat_ipt_get_entries { 1577 char name[XT_TABLE_MAXNAMELEN]; 1578 compat_uint_t size; 1579 struct compat_ipt_entry entrytable[0]; 1580 }; 1581 1582 static int 1583 compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table, 1584 void __user *userptr) 1585 { 1586 struct xt_counters *counters; 1587 const struct xt_table_info *private = table->private; 1588 void __user *pos; 1589 unsigned int size; 1590 int ret = 0; 1591 unsigned int i = 0; 1592 struct ipt_entry *iter; 1593 1594 counters = alloc_counters(table); 1595 if (IS_ERR(counters)) 1596 return PTR_ERR(counters); 1597 1598 pos = userptr; 1599 size = total_size; 1600 xt_entry_foreach(iter, private->entries, total_size) { 1601 ret = compat_copy_entry_to_user(iter, &pos, 1602 &size, counters, i++); 1603 if (ret != 0) 1604 break; 1605 } 1606 1607 vfree(counters); 1608 return ret; 1609 } 1610 1611 static int 1612 compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr, 1613 int *len) 1614 { 1615 int ret; 1616 struct compat_ipt_get_entries get; 1617 struct xt_table *t; 1618 1619 if (*len < sizeof(get)) 1620 return -EINVAL; 1621 1622 if (copy_from_user(&get, uptr, sizeof(get)) != 0) 1623 return -EFAULT; 1624 1625 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) 1626 return -EINVAL; 1627 1628 get.name[sizeof(get.name) - 1] = '\0'; 1629 1630 xt_compat_lock(AF_INET); 1631 t = xt_find_table_lock(net, AF_INET, get.name); 1632 if (!IS_ERR_OR_NULL(t)) { 1633 const struct xt_table_info *private = t->private; 1634 struct xt_table_info info; 1635 ret = compat_table_info(private, &info); 1636 if (!ret && get.size == info.size) 1637 ret = compat_copy_entries_to_user(private->size, 1638 t, uptr->entrytable); 1639 else if (!ret) 1640 ret = -EAGAIN; 1641 1642 xt_compat_flush_offsets(AF_INET); 1643 module_put(t->me); 1644 xt_table_unlock(t); 1645 } else 1646 ret = t ? PTR_ERR(t) : -ENOENT; 1647 1648 xt_compat_unlock(AF_INET); 1649 return ret; 1650 } 1651 1652 static int do_ipt_get_ctl(struct sock *, int, void __user *, int *); 1653 1654 static int 1655 compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) 1656 { 1657 int ret; 1658 1659 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) 1660 return -EPERM; 1661 1662 switch (cmd) { 1663 case IPT_SO_GET_INFO: 1664 ret = get_info(sock_net(sk), user, len, 1); 1665 break; 1666 case IPT_SO_GET_ENTRIES: 1667 ret = compat_get_entries(sock_net(sk), user, len); 1668 break; 1669 default: 1670 ret = do_ipt_get_ctl(sk, cmd, user, len); 1671 } 1672 return ret; 1673 } 1674 #endif 1675 1676 static int 1677 do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len) 1678 { 1679 int ret; 1680 1681 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) 1682 return -EPERM; 1683 1684 switch (cmd) { 1685 case IPT_SO_SET_REPLACE: 1686 ret = do_replace(sock_net(sk), user, len); 1687 break; 1688 1689 case IPT_SO_SET_ADD_COUNTERS: 1690 ret = do_add_counters(sock_net(sk), user, len, 0); 1691 break; 1692 1693 default: 1694 ret = -EINVAL; 1695 } 1696 1697 return ret; 1698 } 1699 1700 static int 1701 do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) 1702 { 1703 int ret; 1704 1705 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) 1706 return -EPERM; 1707 1708 switch (cmd) { 1709 case IPT_SO_GET_INFO: 1710 ret = get_info(sock_net(sk), user, len, 0); 1711 break; 1712 1713 case IPT_SO_GET_ENTRIES: 1714 ret = get_entries(sock_net(sk), user, len); 1715 break; 1716 1717 case IPT_SO_GET_REVISION_MATCH: 1718 case IPT_SO_GET_REVISION_TARGET: { 1719 struct xt_get_revision rev; 1720 int target; 1721 1722 if (*len != sizeof(rev)) { 1723 ret = -EINVAL; 1724 break; 1725 } 1726 if (copy_from_user(&rev, user, sizeof(rev)) != 0) { 1727 ret = -EFAULT; 1728 break; 1729 } 1730 rev.name[sizeof(rev.name)-1] = 0; 1731 1732 if (cmd == IPT_SO_GET_REVISION_TARGET) 1733 target = 1; 1734 else 1735 target = 0; 1736 1737 try_then_request_module(xt_find_revision(AF_INET, rev.name, 1738 rev.revision, 1739 target, &ret), 1740 "ipt_%s", rev.name); 1741 break; 1742 } 1743 1744 default: 1745 ret = -EINVAL; 1746 } 1747 1748 return ret; 1749 } 1750 1751 static void __ipt_unregister_table(struct net *net, struct xt_table *table) 1752 { 1753 struct xt_table_info *private; 1754 void *loc_cpu_entry; 1755 struct module *table_owner = table->me; 1756 struct ipt_entry *iter; 1757 1758 private = xt_unregister_table(table); 1759 1760 /* Decrease module usage counts and free resources */ 1761 loc_cpu_entry = private->entries; 1762 xt_entry_foreach(iter, loc_cpu_entry, private->size) 1763 cleanup_entry(iter, net); 1764 if (private->number > private->initial_entries) 1765 module_put(table_owner); 1766 xt_free_table_info(private); 1767 } 1768 1769 int ipt_register_table(struct net *net, const struct xt_table *table, 1770 const struct ipt_replace *repl, 1771 const struct nf_hook_ops *ops, struct xt_table **res) 1772 { 1773 int ret; 1774 struct xt_table_info *newinfo; 1775 struct xt_table_info bootstrap = {0}; 1776 void *loc_cpu_entry; 1777 struct xt_table *new_table; 1778 1779 newinfo = xt_alloc_table_info(repl->size); 1780 if (!newinfo) 1781 return -ENOMEM; 1782 1783 loc_cpu_entry = newinfo->entries; 1784 memcpy(loc_cpu_entry, repl->entries, repl->size); 1785 1786 ret = translate_table(net, newinfo, loc_cpu_entry, repl); 1787 if (ret != 0) 1788 goto out_free; 1789 1790 new_table = xt_register_table(net, table, &bootstrap, newinfo); 1791 if (IS_ERR(new_table)) { 1792 ret = PTR_ERR(new_table); 1793 goto out_free; 1794 } 1795 1796 /* set res now, will see skbs right after nf_register_net_hooks */ 1797 WRITE_ONCE(*res, new_table); 1798 1799 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks)); 1800 if (ret != 0) { 1801 __ipt_unregister_table(net, new_table); 1802 *res = NULL; 1803 } 1804 1805 return ret; 1806 1807 out_free: 1808 xt_free_table_info(newinfo); 1809 return ret; 1810 } 1811 1812 void ipt_unregister_table(struct net *net, struct xt_table *table, 1813 const struct nf_hook_ops *ops) 1814 { 1815 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks)); 1816 __ipt_unregister_table(net, table); 1817 } 1818 1819 /* Returns 1 if the type and code is matched by the range, 0 otherwise */ 1820 static inline bool 1821 icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code, 1822 u_int8_t type, u_int8_t code, 1823 bool invert) 1824 { 1825 return ((test_type == 0xFF) || 1826 (type == test_type && code >= min_code && code <= max_code)) 1827 ^ invert; 1828 } 1829 1830 static bool 1831 icmp_match(const struct sk_buff *skb, struct xt_action_param *par) 1832 { 1833 const struct icmphdr *ic; 1834 struct icmphdr _icmph; 1835 const struct ipt_icmp *icmpinfo = par->matchinfo; 1836 1837 /* Must not be a fragment. */ 1838 if (par->fragoff != 0) 1839 return false; 1840 1841 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph); 1842 if (ic == NULL) { 1843 /* We've been asked to examine this packet, and we 1844 * can't. Hence, no choice but to drop. 1845 */ 1846 par->hotdrop = true; 1847 return false; 1848 } 1849 1850 return icmp_type_code_match(icmpinfo->type, 1851 icmpinfo->code[0], 1852 icmpinfo->code[1], 1853 ic->type, ic->code, 1854 !!(icmpinfo->invflags&IPT_ICMP_INV)); 1855 } 1856 1857 static int icmp_checkentry(const struct xt_mtchk_param *par) 1858 { 1859 const struct ipt_icmp *icmpinfo = par->matchinfo; 1860 1861 /* Must specify no unknown invflags */ 1862 return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0; 1863 } 1864 1865 static struct xt_target ipt_builtin_tg[] __read_mostly = { 1866 { 1867 .name = XT_STANDARD_TARGET, 1868 .targetsize = sizeof(int), 1869 .family = NFPROTO_IPV4, 1870 #ifdef CONFIG_COMPAT 1871 .compatsize = sizeof(compat_int_t), 1872 .compat_from_user = compat_standard_from_user, 1873 .compat_to_user = compat_standard_to_user, 1874 #endif 1875 }, 1876 { 1877 .name = XT_ERROR_TARGET, 1878 .target = ipt_error, 1879 .targetsize = XT_FUNCTION_MAXNAMELEN, 1880 .family = NFPROTO_IPV4, 1881 }, 1882 }; 1883 1884 static struct nf_sockopt_ops ipt_sockopts = { 1885 .pf = PF_INET, 1886 .set_optmin = IPT_BASE_CTL, 1887 .set_optmax = IPT_SO_SET_MAX+1, 1888 .set = do_ipt_set_ctl, 1889 #ifdef CONFIG_COMPAT 1890 .compat_set = compat_do_ipt_set_ctl, 1891 #endif 1892 .get_optmin = IPT_BASE_CTL, 1893 .get_optmax = IPT_SO_GET_MAX+1, 1894 .get = do_ipt_get_ctl, 1895 #ifdef CONFIG_COMPAT 1896 .compat_get = compat_do_ipt_get_ctl, 1897 #endif 1898 .owner = THIS_MODULE, 1899 }; 1900 1901 static struct xt_match ipt_builtin_mt[] __read_mostly = { 1902 { 1903 .name = "icmp", 1904 .match = icmp_match, 1905 .matchsize = sizeof(struct ipt_icmp), 1906 .checkentry = icmp_checkentry, 1907 .proto = IPPROTO_ICMP, 1908 .family = NFPROTO_IPV4, 1909 }, 1910 }; 1911 1912 static int __net_init ip_tables_net_init(struct net *net) 1913 { 1914 return xt_proto_init(net, NFPROTO_IPV4); 1915 } 1916 1917 static void __net_exit ip_tables_net_exit(struct net *net) 1918 { 1919 xt_proto_fini(net, NFPROTO_IPV4); 1920 } 1921 1922 static struct pernet_operations ip_tables_net_ops = { 1923 .init = ip_tables_net_init, 1924 .exit = ip_tables_net_exit, 1925 }; 1926 1927 static int __init ip_tables_init(void) 1928 { 1929 int ret; 1930 1931 ret = register_pernet_subsys(&ip_tables_net_ops); 1932 if (ret < 0) 1933 goto err1; 1934 1935 /* No one else will be downing sem now, so we won't sleep */ 1936 ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg)); 1937 if (ret < 0) 1938 goto err2; 1939 ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt)); 1940 if (ret < 0) 1941 goto err4; 1942 1943 /* Register setsockopt */ 1944 ret = nf_register_sockopt(&ipt_sockopts); 1945 if (ret < 0) 1946 goto err5; 1947 1948 pr_info("(C) 2000-2006 Netfilter Core Team\n"); 1949 return 0; 1950 1951 err5: 1952 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt)); 1953 err4: 1954 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg)); 1955 err2: 1956 unregister_pernet_subsys(&ip_tables_net_ops); 1957 err1: 1958 return ret; 1959 } 1960 1961 static void __exit ip_tables_fini(void) 1962 { 1963 nf_unregister_sockopt(&ipt_sockopts); 1964 1965 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt)); 1966 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg)); 1967 unregister_pernet_subsys(&ip_tables_net_ops); 1968 } 1969 1970 EXPORT_SYMBOL(ipt_register_table); 1971 EXPORT_SYMBOL(ipt_unregister_table); 1972 EXPORT_SYMBOL(ipt_do_table); 1973 module_init(ip_tables_init); 1974 module_exit(ip_tables_fini); 1975