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