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