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_ip *ip)
172{
173 static const struct ipt_ip uncond;
174
175 return memcmp(ip, &uncond, sizeof(uncond)) == 0;
176#undef FWINV
177}
178
179
180static inline const struct xt_entry_target *
181ipt_get_target_c(const struct ipt_entry *e)
182{
183 return ipt_get_target((struct ipt_entry *)e);
184}
185
186#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
187static const char *const hooknames[] = {
188 [NF_INET_PRE_ROUTING] = "PREROUTING",
189 [NF_INET_LOCAL_IN] = "INPUT",
190 [NF_INET_FORWARD] = "FORWARD",
191 [NF_INET_LOCAL_OUT] = "OUTPUT",
192 [NF_INET_POST_ROUTING] = "POSTROUTING",
193};
194
195enum nf_ip_trace_comments {
196 NF_IP_TRACE_COMMENT_RULE,
197 NF_IP_TRACE_COMMENT_RETURN,
198 NF_IP_TRACE_COMMENT_POLICY,
199};
200
201static const char *const comments[] = {
202 [NF_IP_TRACE_COMMENT_RULE] = "rule",
203 [NF_IP_TRACE_COMMENT_RETURN] = "return",
204 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
205};
206
207static struct nf_loginfo trace_loginfo = {
208 .type = NF_LOG_TYPE_LOG,
209 .u = {
210 .log = {
211 .level = 4,
212 .logflags = NF_LOG_MASK,
213 },
214 },
215};
216
217
218static inline int
219get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e,
220 const char *hookname, const char **chainname,
221 const char **comment, unsigned int *rulenum)
222{
223 const struct xt_standard_target *t = (void *)ipt_get_target_c(s);
224
225 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
226
227 *chainname = t->target.data;
228 (*rulenum) = 0;
229 } else if (s == e) {
230 (*rulenum)++;
231
232 if (s->target_offset == sizeof(struct ipt_entry) &&
233 strcmp(t->target.u.kernel.target->name,
234 XT_STANDARD_TARGET) == 0 &&
235 t->verdict < 0 &&
236 unconditional(&s->ip)) {
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{
447 unsigned int hook;
448
449
450
451 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
452 unsigned int pos = newinfo->hook_entry[hook];
453 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
454
455 if (!(valid_hooks & (1 << hook)))
456 continue;
457
458
459 e->counters.pcnt = pos;
460
461 for (;;) {
462 const struct xt_standard_target *t
463 = (void *)ipt_get_target_c(e);
464 int visited = e->comefrom & (1 << hook);
465
466 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
467 pr_err("iptables: loop hook %u pos %u %08X.\n",
468 hook, pos, e->comefrom);
469 return 0;
470 }
471 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
472
473
474 if ((e->target_offset == sizeof(struct ipt_entry) &&
475 (strcmp(t->target.u.user.name,
476 XT_STANDARD_TARGET) == 0) &&
477 t->verdict < 0 && unconditional(&e->ip)) ||
478 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 e->counters.pcnt = pos;
520 pos += size;
521 } else {
522 int newpos = t->verdict;
523
524 if (strcmp(t->target.u.user.name,
525 XT_STANDARD_TARGET) == 0 &&
526 newpos >= 0) {
527 if (newpos > newinfo->size -
528 sizeof(struct ipt_entry)) {
529 duprintf("mark_source_chains: "
530 "bad verdict (%i)\n",
531 newpos);
532 return 0;
533 }
534
535 duprintf("Jump rule %u -> %u\n",
536 pos, newpos);
537 } else {
538
539 newpos = pos + e->next_offset;
540 }
541 e = (struct ipt_entry *)
542 (entry0 + newpos);
543 e->counters.pcnt = pos;
544 pos = newpos;
545 }
546 }
547 next:
548 duprintf("Finished chain %u\n", hook);
549 }
550 return 1;
551}
552
553static void cleanup_match(struct xt_entry_match *m, struct net *net)
554{
555 struct xt_mtdtor_param par;
556
557 par.net = net;
558 par.match = m->u.kernel.match;
559 par.matchinfo = m->data;
560 par.family = NFPROTO_IPV4;
561 if (par.match->destroy != NULL)
562 par.match->destroy(&par);
563 module_put(par.match->me);
564}
565
566static int
567check_entry(const struct ipt_entry *e, const char *name)
568{
569 const struct xt_entry_target *t;
570
571 if (!ip_checkentry(&e->ip)) {
572 duprintf("ip check failed %p %s.\n", e, name);
573 return -EINVAL;
574 }
575
576 if (e->target_offset + sizeof(struct xt_entry_target) >
577 e->next_offset)
578 return -EINVAL;
579
580 t = ipt_get_target_c(e);
581 if (e->target_offset + t->u.target_size > e->next_offset)
582 return -EINVAL;
583
584 return 0;
585}
586
587static int
588check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
589{
590 const struct ipt_ip *ip = par->entryinfo;
591 int ret;
592
593 par->match = m->u.kernel.match;
594 par->matchinfo = m->data;
595
596 ret = xt_check_match(par, m->u.match_size - sizeof(*m),
597 ip->proto, ip->invflags & IPT_INV_PROTO);
598 if (ret < 0) {
599 duprintf("check failed for `%s'.\n", par->match->name);
600 return ret;
601 }
602 return 0;
603}
604
605static int
606find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
607{
608 struct xt_match *match;
609 int ret;
610
611 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
612 m->u.user.revision);
613 if (IS_ERR(match)) {
614 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
615 return PTR_ERR(match);
616 }
617 m->u.kernel.match = match;
618
619 ret = check_match(m, par);
620 if (ret)
621 goto err;
622
623 return 0;
624err:
625 module_put(m->u.kernel.match->me);
626 return ret;
627}
628
629static int check_target(struct ipt_entry *e, struct net *net, const char *name)
630{
631 struct xt_entry_target *t = ipt_get_target(e);
632 struct xt_tgchk_param par = {
633 .net = net,
634 .table = name,
635 .entryinfo = e,
636 .target = t->u.kernel.target,
637 .targinfo = t->data,
638 .hook_mask = e->comefrom,
639 .family = NFPROTO_IPV4,
640 };
641 int ret;
642
643 ret = xt_check_target(&par, t->u.target_size - sizeof(*t),
644 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
645 if (ret < 0) {
646 duprintf("check failed for `%s'.\n",
647 t->u.kernel.target->name);
648 return ret;
649 }
650 return 0;
651}
652
653static int
654find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
655 unsigned int size)
656{
657 struct xt_entry_target *t;
658 struct xt_target *target;
659 int ret;
660 unsigned int j;
661 struct xt_mtchk_param mtpar;
662 struct xt_entry_match *ematch;
663
664 ret = check_entry(e, name);
665 if (ret)
666 return ret;
667
668 e->counters.pcnt = xt_percpu_counter_alloc();
669 if (IS_ERR_VALUE(e->counters.pcnt))
670 return -ENOMEM;
671
672 j = 0;
673 mtpar.net = net;
674 mtpar.table = name;
675 mtpar.entryinfo = &e->ip;
676 mtpar.hook_mask = e->comefrom;
677 mtpar.family = NFPROTO_IPV4;
678 xt_ematch_foreach(ematch, e) {
679 ret = find_check_match(ematch, &mtpar);
680 if (ret != 0)
681 goto cleanup_matches;
682 ++j;
683 }
684
685 t = ipt_get_target(e);
686 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
687 t->u.user.revision);
688 if (IS_ERR(target)) {
689 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
690 ret = PTR_ERR(target);
691 goto cleanup_matches;
692 }
693 t->u.kernel.target = target;
694
695 ret = check_target(e, net, name);
696 if (ret)
697 goto err;
698
699 return 0;
700 err:
701 module_put(t->u.kernel.target->me);
702 cleanup_matches:
703 xt_ematch_foreach(ematch, e) {
704 if (j-- == 0)
705 break;
706 cleanup_match(ematch, net);
707 }
708
709 xt_percpu_counter_free(e->counters.pcnt);
710
711 return ret;
712}
713
714static bool check_underflow(const struct ipt_entry *e)
715{
716 const struct xt_entry_target *t;
717 unsigned int verdict;
718
719 if (!unconditional(&e->ip))
720 return false;
721 t = ipt_get_target_c(e);
722 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
723 return false;
724 verdict = ((struct xt_standard_target *)t)->verdict;
725 verdict = -verdict - 1;
726 return verdict == NF_DROP || verdict == NF_ACCEPT;
727}
728
729static int
730check_entry_size_and_hooks(struct ipt_entry *e,
731 struct xt_table_info *newinfo,
732 const unsigned char *base,
733 const unsigned char *limit,
734 const unsigned int *hook_entries,
735 const unsigned int *underflows,
736 unsigned int valid_hooks)
737{
738 unsigned int h;
739
740 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
741 (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
742 duprintf("Bad offset %p\n", e);
743 return -EINVAL;
744 }
745
746 if (e->next_offset
747 < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target)) {
748 duprintf("checking: element %p size %u\n",
749 e, e->next_offset);
750 return -EINVAL;
751 }
752
753
754 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
755 if (!(valid_hooks & (1 << h)))
756 continue;
757 if ((unsigned char *)e - base == hook_entries[h])
758 newinfo->hook_entry[h] = hook_entries[h];
759 if ((unsigned char *)e - base == underflows[h]) {
760 if (!check_underflow(e)) {
761 pr_err("Underflows must be unconditional and "
762 "use the STANDARD target with "
763 "ACCEPT/DROP\n");
764 return -EINVAL;
765 }
766 newinfo->underflow[h] = underflows[h];
767 }
768 }
769
770
771 e->counters = ((struct xt_counters) { 0, 0 });
772 e->comefrom = 0;
773 return 0;
774}
775
776static void
777cleanup_entry(struct ipt_entry *e, struct net *net)
778{
779 struct xt_tgdtor_param par;
780 struct xt_entry_target *t;
781 struct xt_entry_match *ematch;
782
783
784 xt_ematch_foreach(ematch, e)
785 cleanup_match(ematch, net);
786 t = ipt_get_target(e);
787
788 par.net = net;
789 par.target = t->u.kernel.target;
790 par.targinfo = t->data;
791 par.family = NFPROTO_IPV4;
792 if (par.target->destroy != NULL)
793 par.target->destroy(&par);
794 module_put(par.target->me);
795 xt_percpu_counter_free(e->counters.pcnt);
796}
797
798
799
800static int
801translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
802 const struct ipt_replace *repl)
803{
804 struct ipt_entry *iter;
805 unsigned int i;
806 int ret = 0;
807
808 newinfo->size = repl->size;
809 newinfo->number = repl->num_entries;
810
811
812 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
813 newinfo->hook_entry[i] = 0xFFFFFFFF;
814 newinfo->underflow[i] = 0xFFFFFFFF;
815 }
816
817 duprintf("translate_table: size %u\n", newinfo->size);
818 i = 0;
819
820 xt_entry_foreach(iter, entry0, newinfo->size) {
821 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
822 entry0 + repl->size,
823 repl->hook_entry,
824 repl->underflow,
825 repl->valid_hooks);
826 if (ret != 0)
827 return ret;
828 ++i;
829 if (strcmp(ipt_get_target(iter)->u.user.name,
830 XT_ERROR_TARGET) == 0)
831 ++newinfo->stacksize;
832 }
833
834 if (i != repl->num_entries) {
835 duprintf("translate_table: %u not %u entries\n",
836 i, repl->num_entries);
837 return -EINVAL;
838 }
839
840
841 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
842
843 if (!(repl->valid_hooks & (1 << i)))
844 continue;
845 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
846 duprintf("Invalid hook entry %u %u\n",
847 i, repl->hook_entry[i]);
848 return -EINVAL;
849 }
850 if (newinfo->underflow[i] == 0xFFFFFFFF) {
851 duprintf("Invalid underflow %u %u\n",
852 i, repl->underflow[i]);
853 return -EINVAL;
854 }
855 }
856
857 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0))
858 return -ELOOP;
859
860
861 i = 0;
862 xt_entry_foreach(iter, entry0, newinfo->size) {
863 ret = find_check_entry(iter, net, repl->name, repl->size);
864 if (ret != 0)
865 break;
866 ++i;
867 }
868
869 if (ret != 0) {
870 xt_entry_foreach(iter, entry0, newinfo->size) {
871 if (i-- == 0)
872 break;
873 cleanup_entry(iter, net);
874 }
875 return ret;
876 }
877
878 return ret;
879}
880
881static void
882get_counters(const struct xt_table_info *t,
883 struct xt_counters counters[])
884{
885 struct ipt_entry *iter;
886 unsigned int cpu;
887 unsigned int i;
888
889 for_each_possible_cpu(cpu) {
890 seqcount_t *s = &per_cpu(xt_recseq, cpu);
891
892 i = 0;
893 xt_entry_foreach(iter, t->entries, t->size) {
894 struct xt_counters *tmp;
895 u64 bcnt, pcnt;
896 unsigned int start;
897
898 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
899 do {
900 start = read_seqcount_begin(s);
901 bcnt = tmp->bcnt;
902 pcnt = tmp->pcnt;
903 } while (read_seqcount_retry(s, start));
904
905 ADD_COUNTER(counters[i], bcnt, pcnt);
906 ++i;
907 }
908 }
909}
910
911static struct xt_counters *alloc_counters(const struct xt_table *table)
912{
913 unsigned int countersize;
914 struct xt_counters *counters;
915 const struct xt_table_info *private = table->private;
916
917
918
919
920 countersize = sizeof(struct xt_counters) * private->number;
921 counters = vzalloc(countersize);
922
923 if (counters == NULL)
924 return ERR_PTR(-ENOMEM);
925
926 get_counters(private, counters);
927
928 return counters;
929}
930
931static int
932copy_entries_to_user(unsigned int total_size,
933 const struct xt_table *table,
934 void __user *userptr)
935{
936 unsigned int off, num;
937 const struct ipt_entry *e;
938 struct xt_counters *counters;
939 const struct xt_table_info *private = table->private;
940 int ret = 0;
941 const void *loc_cpu_entry;
942
943 counters = alloc_counters(table);
944 if (IS_ERR(counters))
945 return PTR_ERR(counters);
946
947 loc_cpu_entry = private->entries;
948 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
949 ret = -EFAULT;
950 goto free_counters;
951 }
952
953
954
955 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
956 unsigned int i;
957 const struct xt_entry_match *m;
958 const struct xt_entry_target *t;
959
960 e = (struct ipt_entry *)(loc_cpu_entry + off);
961 if (copy_to_user(userptr + off
962 + offsetof(struct ipt_entry, counters),
963 &counters[num],
964 sizeof(counters[num])) != 0) {
965 ret = -EFAULT;
966 goto free_counters;
967 }
968
969 for (i = sizeof(struct ipt_entry);
970 i < e->target_offset;
971 i += m->u.match_size) {
972 m = (void *)e + i;
973
974 if (copy_to_user(userptr + off + i
975 + offsetof(struct xt_entry_match,
976 u.user.name),
977 m->u.kernel.match->name,
978 strlen(m->u.kernel.match->name)+1)
979 != 0) {
980 ret = -EFAULT;
981 goto free_counters;
982 }
983 }
984
985 t = ipt_get_target_c(e);
986 if (copy_to_user(userptr + off + e->target_offset
987 + offsetof(struct xt_entry_target,
988 u.user.name),
989 t->u.kernel.target->name,
990 strlen(t->u.kernel.target->name)+1) != 0) {
991 ret = -EFAULT;
992 goto free_counters;
993 }
994 }
995
996 free_counters:
997 vfree(counters);
998 return ret;
999}
1000
1001#ifdef CONFIG_COMPAT
1002static void compat_standard_from_user(void *dst, const void *src)
1003{
1004 int v = *(compat_int_t *)src;
1005
1006 if (v > 0)
1007 v += xt_compat_calc_jump(AF_INET, v);
1008 memcpy(dst, &v, sizeof(v));
1009}
1010
1011static int compat_standard_to_user(void __user *dst, const void *src)
1012{
1013 compat_int_t cv = *(int *)src;
1014
1015 if (cv > 0)
1016 cv -= xt_compat_calc_jump(AF_INET, cv);
1017 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
1018}
1019
1020static int compat_calc_entry(const struct ipt_entry *e,
1021 const struct xt_table_info *info,
1022 const void *base, struct xt_table_info *newinfo)
1023{
1024 const struct xt_entry_match *ematch;
1025 const struct xt_entry_target *t;
1026 unsigned int entry_offset;
1027 int off, i, ret;
1028
1029 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1030 entry_offset = (void *)e - base;
1031 xt_ematch_foreach(ematch, e)
1032 off += xt_compat_match_offset(ematch->u.kernel.match);
1033 t = ipt_get_target_c(e);
1034 off += xt_compat_target_offset(t->u.kernel.target);
1035 newinfo->size -= off;
1036 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
1037 if (ret)
1038 return ret;
1039
1040 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1041 if (info->hook_entry[i] &&
1042 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
1043 newinfo->hook_entry[i] -= off;
1044 if (info->underflow[i] &&
1045 (e < (struct ipt_entry *)(base + info->underflow[i])))
1046 newinfo->underflow[i] -= off;
1047 }
1048 return 0;
1049}
1050
1051static int compat_table_info(const struct xt_table_info *info,
1052 struct xt_table_info *newinfo)
1053{
1054 struct ipt_entry *iter;
1055 const void *loc_cpu_entry;
1056 int ret;
1057
1058 if (!newinfo || !info)
1059 return -EINVAL;
1060
1061
1062 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1063 newinfo->initial_entries = 0;
1064 loc_cpu_entry = info->entries;
1065 xt_compat_init_offsets(AF_INET, info->number);
1066 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
1067 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
1068 if (ret != 0)
1069 return ret;
1070 }
1071 return 0;
1072}
1073#endif
1074
1075static int get_info(struct net *net, void __user *user,
1076 const int *len, int compat)
1077{
1078 char name[XT_TABLE_MAXNAMELEN];
1079 struct xt_table *t;
1080 int ret;
1081
1082 if (*len != sizeof(struct ipt_getinfo)) {
1083 duprintf("length %u != %zu\n", *len,
1084 sizeof(struct ipt_getinfo));
1085 return -EINVAL;
1086 }
1087
1088 if (copy_from_user(name, user, sizeof(name)) != 0)
1089 return -EFAULT;
1090
1091 name[XT_TABLE_MAXNAMELEN-1] = '\0';
1092#ifdef CONFIG_COMPAT
1093 if (compat)
1094 xt_compat_lock(AF_INET);
1095#endif
1096 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
1097 "iptable_%s", name);
1098 if (!IS_ERR_OR_NULL(t)) {
1099 struct ipt_getinfo info;
1100 const struct xt_table_info *private = t->private;
1101#ifdef CONFIG_COMPAT
1102 struct xt_table_info tmp;
1103
1104 if (compat) {
1105 ret = compat_table_info(private, &tmp);
1106 xt_compat_flush_offsets(AF_INET);
1107 private = &tmp;
1108 }
1109#endif
1110 memset(&info, 0, sizeof(info));
1111 info.valid_hooks = t->valid_hooks;
1112 memcpy(info.hook_entry, private->hook_entry,
1113 sizeof(info.hook_entry));
1114 memcpy(info.underflow, private->underflow,
1115 sizeof(info.underflow));
1116 info.num_entries = private->number;
1117 info.size = private->size;
1118 strcpy(info.name, name);
1119
1120 if (copy_to_user(user, &info, *len) != 0)
1121 ret = -EFAULT;
1122 else
1123 ret = 0;
1124
1125 xt_table_unlock(t);
1126 module_put(t->me);
1127 } else
1128 ret = t ? PTR_ERR(t) : -ENOENT;
1129#ifdef CONFIG_COMPAT
1130 if (compat)
1131 xt_compat_unlock(AF_INET);
1132#endif
1133 return ret;
1134}
1135
1136static int
1137get_entries(struct net *net, struct ipt_get_entries __user *uptr,
1138 const int *len)
1139{
1140 int ret;
1141 struct ipt_get_entries get;
1142 struct xt_table *t;
1143
1144 if (*len < sizeof(get)) {
1145 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
1146 return -EINVAL;
1147 }
1148 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1149 return -EFAULT;
1150 if (*len != sizeof(struct ipt_get_entries) + get.size) {
1151 duprintf("get_entries: %u != %zu\n",
1152 *len, sizeof(get) + get.size);
1153 return -EINVAL;
1154 }
1155
1156 t = xt_find_table_lock(net, AF_INET, get.name);
1157 if (!IS_ERR_OR_NULL(t)) {
1158 const struct xt_table_info *private = t->private;
1159 duprintf("t->private->number = %u\n", private->number);
1160 if (get.size == private->size)
1161 ret = copy_entries_to_user(private->size,
1162 t, uptr->entrytable);
1163 else {
1164 duprintf("get_entries: I've got %u not %u!\n",
1165 private->size, get.size);
1166 ret = -EAGAIN;
1167 }
1168 module_put(t->me);
1169 xt_table_unlock(t);
1170 } else
1171 ret = t ? PTR_ERR(t) : -ENOENT;
1172
1173 return ret;
1174}
1175
1176static int
1177__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
1178 struct xt_table_info *newinfo, unsigned int num_counters,
1179 void __user *counters_ptr)
1180{
1181 int ret;
1182 struct xt_table *t;
1183 struct xt_table_info *oldinfo;
1184 struct xt_counters *counters;
1185 struct ipt_entry *iter;
1186
1187 ret = 0;
1188 counters = vzalloc(num_counters * sizeof(struct xt_counters));
1189 if (!counters) {
1190 ret = -ENOMEM;
1191 goto out;
1192 }
1193
1194 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
1195 "iptable_%s", name);
1196 if (IS_ERR_OR_NULL(t)) {
1197 ret = t ? PTR_ERR(t) : -ENOENT;
1198 goto free_newinfo_counters_untrans;
1199 }
1200
1201
1202 if (valid_hooks != t->valid_hooks) {
1203 duprintf("Valid hook crap: %08X vs %08X\n",
1204 valid_hooks, t->valid_hooks);
1205 ret = -EINVAL;
1206 goto put_module;
1207 }
1208
1209 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1210 if (!oldinfo)
1211 goto put_module;
1212
1213
1214 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1215 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1216 if ((oldinfo->number > oldinfo->initial_entries) ||
1217 (newinfo->number <= oldinfo->initial_entries))
1218 module_put(t->me);
1219 if ((oldinfo->number > oldinfo->initial_entries) &&
1220 (newinfo->number <= oldinfo->initial_entries))
1221 module_put(t->me);
1222
1223
1224 get_counters(oldinfo, counters);
1225
1226
1227 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
1228 cleanup_entry(iter, net);
1229
1230 xt_free_table_info(oldinfo);
1231 if (copy_to_user(counters_ptr, counters,
1232 sizeof(struct xt_counters) * num_counters) != 0) {
1233
1234 net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
1235 }
1236 vfree(counters);
1237 xt_table_unlock(t);
1238 return ret;
1239
1240 put_module:
1241 module_put(t->me);
1242 xt_table_unlock(t);
1243 free_newinfo_counters_untrans:
1244 vfree(counters);
1245 out:
1246 return ret;
1247}
1248
1249static int
1250do_replace(struct net *net, const void __user *user, unsigned int len)
1251{
1252 int ret;
1253 struct ipt_replace tmp;
1254 struct xt_table_info *newinfo;
1255 void *loc_cpu_entry;
1256 struct ipt_entry *iter;
1257
1258 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1259 return -EFAULT;
1260
1261
1262 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1263 return -ENOMEM;
1264 if (tmp.num_counters == 0)
1265 return -EINVAL;
1266
1267 tmp.name[sizeof(tmp.name)-1] = 0;
1268
1269 newinfo = xt_alloc_table_info(tmp.size);
1270 if (!newinfo)
1271 return -ENOMEM;
1272
1273 loc_cpu_entry = newinfo->entries;
1274 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1275 tmp.size) != 0) {
1276 ret = -EFAULT;
1277 goto free_newinfo;
1278 }
1279
1280 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
1281 if (ret != 0)
1282 goto free_newinfo;
1283
1284 duprintf("Translated table\n");
1285
1286 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1287 tmp.num_counters, tmp.counters);
1288 if (ret)
1289 goto free_newinfo_untrans;
1290 return 0;
1291
1292 free_newinfo_untrans:
1293 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1294 cleanup_entry(iter, net);
1295 free_newinfo:
1296 xt_free_table_info(newinfo);
1297 return ret;
1298}
1299
1300static int
1301do_add_counters(struct net *net, const void __user *user,
1302 unsigned int len, int compat)
1303{
1304 unsigned int i;
1305 struct xt_counters_info tmp;
1306 struct xt_counters *paddc;
1307 unsigned int num_counters;
1308 const char *name;
1309 int size;
1310 void *ptmp;
1311 struct xt_table *t;
1312 const struct xt_table_info *private;
1313 int ret = 0;
1314 struct ipt_entry *iter;
1315 unsigned int addend;
1316#ifdef CONFIG_COMPAT
1317 struct compat_xt_counters_info compat_tmp;
1318
1319 if (compat) {
1320 ptmp = &compat_tmp;
1321 size = sizeof(struct compat_xt_counters_info);
1322 } else
1323#endif
1324 {
1325 ptmp = &tmp;
1326 size = sizeof(struct xt_counters_info);
1327 }
1328
1329 if (copy_from_user(ptmp, user, size) != 0)
1330 return -EFAULT;
1331
1332#ifdef CONFIG_COMPAT
1333 if (compat) {
1334 num_counters = compat_tmp.num_counters;
1335 name = compat_tmp.name;
1336 } else
1337#endif
1338 {
1339 num_counters = tmp.num_counters;
1340 name = tmp.name;
1341 }
1342
1343 if (len != size + num_counters * sizeof(struct xt_counters))
1344 return -EINVAL;
1345
1346 paddc = vmalloc(len - size);
1347 if (!paddc)
1348 return -ENOMEM;
1349
1350 if (copy_from_user(paddc, user + size, len - size) != 0) {
1351 ret = -EFAULT;
1352 goto free;
1353 }
1354
1355 t = xt_find_table_lock(net, AF_INET, name);
1356 if (IS_ERR_OR_NULL(t)) {
1357 ret = t ? PTR_ERR(t) : -ENOENT;
1358 goto free;
1359 }
1360
1361 local_bh_disable();
1362 private = t->private;
1363 if (private->number != num_counters) {
1364 ret = -EINVAL;
1365 goto unlock_up_free;
1366 }
1367
1368 i = 0;
1369 addend = xt_write_recseq_begin();
1370 xt_entry_foreach(iter, private->entries, private->size) {
1371 struct xt_counters *tmp;
1372
1373 tmp = xt_get_this_cpu_counter(&iter->counters);
1374 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1375 ++i;
1376 }
1377 xt_write_recseq_end(addend);
1378 unlock_up_free:
1379 local_bh_enable();
1380 xt_table_unlock(t);
1381 module_put(t->me);
1382 free:
1383 vfree(paddc);
1384
1385 return ret;
1386}
1387
1388#ifdef CONFIG_COMPAT
1389struct compat_ipt_replace {
1390 char name[XT_TABLE_MAXNAMELEN];
1391 u32 valid_hooks;
1392 u32 num_entries;
1393 u32 size;
1394 u32 hook_entry[NF_INET_NUMHOOKS];
1395 u32 underflow[NF_INET_NUMHOOKS];
1396 u32 num_counters;
1397 compat_uptr_t counters;
1398 struct compat_ipt_entry entries[0];
1399};
1400
1401static int
1402compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
1403 unsigned int *size, struct xt_counters *counters,
1404 unsigned int i)
1405{
1406 struct xt_entry_target *t;
1407 struct compat_ipt_entry __user *ce;
1408 u_int16_t target_offset, next_offset;
1409 compat_uint_t origsize;
1410 const struct xt_entry_match *ematch;
1411 int ret = 0;
1412
1413 origsize = *size;
1414 ce = (struct compat_ipt_entry __user *)*dstptr;
1415 if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
1416 copy_to_user(&ce->counters, &counters[i],
1417 sizeof(counters[i])) != 0)
1418 return -EFAULT;
1419
1420 *dstptr += sizeof(struct compat_ipt_entry);
1421 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1422
1423 xt_ematch_foreach(ematch, e) {
1424 ret = xt_compat_match_to_user(ematch, dstptr, size);
1425 if (ret != 0)
1426 return ret;
1427 }
1428 target_offset = e->target_offset - (origsize - *size);
1429 t = ipt_get_target(e);
1430 ret = xt_compat_target_to_user(t, dstptr, size);
1431 if (ret)
1432 return ret;
1433 next_offset = e->next_offset - (origsize - *size);
1434 if (put_user(target_offset, &ce->target_offset) != 0 ||
1435 put_user(next_offset, &ce->next_offset) != 0)
1436 return -EFAULT;
1437 return 0;
1438}
1439
1440static int
1441compat_find_calc_match(struct xt_entry_match *m,
1442 const char *name,
1443 const struct ipt_ip *ip,
1444 int *size)
1445{
1446 struct xt_match *match;
1447
1448 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
1449 m->u.user.revision);
1450 if (IS_ERR(match)) {
1451 duprintf("compat_check_calc_match: `%s' not found\n",
1452 m->u.user.name);
1453 return PTR_ERR(match);
1454 }
1455 m->u.kernel.match = match;
1456 *size += xt_compat_match_offset(match);
1457 return 0;
1458}
1459
1460static void compat_release_entry(struct compat_ipt_entry *e)
1461{
1462 struct xt_entry_target *t;
1463 struct xt_entry_match *ematch;
1464
1465
1466 xt_ematch_foreach(ematch, e)
1467 module_put(ematch->u.kernel.match->me);
1468 t = compat_ipt_get_target(e);
1469 module_put(t->u.kernel.target->me);
1470}
1471
1472static int
1473check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
1474 struct xt_table_info *newinfo,
1475 unsigned int *size,
1476 const unsigned char *base,
1477 const unsigned char *limit,
1478 const unsigned int *hook_entries,
1479 const unsigned int *underflows,
1480 const char *name)
1481{
1482 struct xt_entry_match *ematch;
1483 struct xt_entry_target *t;
1484 struct xt_target *target;
1485 unsigned int entry_offset;
1486 unsigned int j;
1487 int ret, off, h;
1488
1489 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1490 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
1491 (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1492 duprintf("Bad offset %p, limit = %p\n", e, limit);
1493 return -EINVAL;
1494 }
1495
1496 if (e->next_offset < sizeof(struct compat_ipt_entry) +
1497 sizeof(struct compat_xt_entry_target)) {
1498 duprintf("checking: element %p size %u\n",
1499 e, e->next_offset);
1500 return -EINVAL;
1501 }
1502
1503
1504 ret = check_entry((struct ipt_entry *)e, name);
1505 if (ret)
1506 return ret;
1507
1508 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1509 entry_offset = (void *)e - (void *)base;
1510 j = 0;
1511 xt_ematch_foreach(ematch, e) {
1512 ret = compat_find_calc_match(ematch, name, &e->ip, &off);
1513 if (ret != 0)
1514 goto release_matches;
1515 ++j;
1516 }
1517
1518 t = compat_ipt_get_target(e);
1519 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
1520 t->u.user.revision);
1521 if (IS_ERR(target)) {
1522 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1523 t->u.user.name);
1524 ret = PTR_ERR(target);
1525 goto release_matches;
1526 }
1527 t->u.kernel.target = target;
1528
1529 off += xt_compat_target_offset(target);
1530 *size += off;
1531 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
1532 if (ret)
1533 goto out;
1534
1535
1536 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1537 if ((unsigned char *)e - base == hook_entries[h])
1538 newinfo->hook_entry[h] = hook_entries[h];
1539 if ((unsigned char *)e - base == underflows[h])
1540 newinfo->underflow[h] = underflows[h];
1541 }
1542
1543
1544 memset(&e->counters, 0, sizeof(e->counters));
1545 e->comefrom = 0;
1546 return 0;
1547
1548out:
1549 module_put(t->u.kernel.target->me);
1550release_matches:
1551 xt_ematch_foreach(ematch, e) {
1552 if (j-- == 0)
1553 break;
1554 module_put(ematch->u.kernel.match->me);
1555 }
1556 return ret;
1557}
1558
1559static int
1560compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
1561 unsigned int *size, const char *name,
1562 struct xt_table_info *newinfo, unsigned char *base)
1563{
1564 struct xt_entry_target *t;
1565 struct xt_target *target;
1566 struct ipt_entry *de;
1567 unsigned int origsize;
1568 int ret, h;
1569 struct xt_entry_match *ematch;
1570
1571 ret = 0;
1572 origsize = *size;
1573 de = (struct ipt_entry *)*dstptr;
1574 memcpy(de, e, sizeof(struct ipt_entry));
1575 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1576
1577 *dstptr += sizeof(struct ipt_entry);
1578 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1579
1580 xt_ematch_foreach(ematch, e) {
1581 ret = xt_compat_match_from_user(ematch, dstptr, size);
1582 if (ret != 0)
1583 return ret;
1584 }
1585 de->target_offset = e->target_offset - (origsize - *size);
1586 t = compat_ipt_get_target(e);
1587 target = t->u.kernel.target;
1588 xt_compat_target_from_user(t, dstptr, size);
1589
1590 de->next_offset = e->next_offset - (origsize - *size);
1591 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1592 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1593 newinfo->hook_entry[h] -= origsize - *size;
1594 if ((unsigned char *)de - base < newinfo->underflow[h])
1595 newinfo->underflow[h] -= origsize - *size;
1596 }
1597 return ret;
1598}
1599
1600static int
1601compat_check_entry(struct ipt_entry *e, struct net *net, const char *name)
1602{
1603 struct xt_entry_match *ematch;
1604 struct xt_mtchk_param mtpar;
1605 unsigned int j;
1606 int ret = 0;
1607
1608 e->counters.pcnt = xt_percpu_counter_alloc();
1609 if (IS_ERR_VALUE(e->counters.pcnt))
1610 return -ENOMEM;
1611
1612 j = 0;
1613 mtpar.net = net;
1614 mtpar.table = name;
1615 mtpar.entryinfo = &e->ip;
1616 mtpar.hook_mask = e->comefrom;
1617 mtpar.family = NFPROTO_IPV4;
1618 xt_ematch_foreach(ematch, e) {
1619 ret = check_match(ematch, &mtpar);
1620 if (ret != 0)
1621 goto cleanup_matches;
1622 ++j;
1623 }
1624
1625 ret = check_target(e, net, name);
1626 if (ret)
1627 goto cleanup_matches;
1628 return 0;
1629
1630 cleanup_matches:
1631 xt_ematch_foreach(ematch, e) {
1632 if (j-- == 0)
1633 break;
1634 cleanup_match(ematch, net);
1635 }
1636
1637 xt_percpu_counter_free(e->counters.pcnt);
1638
1639 return ret;
1640}
1641
1642static int
1643translate_compat_table(struct net *net,
1644 const char *name,
1645 unsigned int valid_hooks,
1646 struct xt_table_info **pinfo,
1647 void **pentry0,
1648 unsigned int total_size,
1649 unsigned int number,
1650 unsigned int *hook_entries,
1651 unsigned int *underflows)
1652{
1653 unsigned int i, j;
1654 struct xt_table_info *newinfo, *info;
1655 void *pos, *entry0, *entry1;
1656 struct compat_ipt_entry *iter0;
1657 struct ipt_entry *iter1;
1658 unsigned int size;
1659 int ret;
1660
1661 info = *pinfo;
1662 entry0 = *pentry0;
1663 size = total_size;
1664 info->number = number;
1665
1666
1667 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1668 info->hook_entry[i] = 0xFFFFFFFF;
1669 info->underflow[i] = 0xFFFFFFFF;
1670 }
1671
1672 duprintf("translate_compat_table: size %u\n", info->size);
1673 j = 0;
1674 xt_compat_lock(AF_INET);
1675 xt_compat_init_offsets(AF_INET, number);
1676
1677 xt_entry_foreach(iter0, entry0, total_size) {
1678 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1679 entry0,
1680 entry0 + total_size,
1681 hook_entries,
1682 underflows,
1683 name);
1684 if (ret != 0)
1685 goto out_unlock;
1686 ++j;
1687 }
1688
1689 ret = -EINVAL;
1690 if (j != number) {
1691 duprintf("translate_compat_table: %u not %u entries\n",
1692 j, number);
1693 goto out_unlock;
1694 }
1695
1696
1697 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1698
1699 if (!(valid_hooks & (1 << i)))
1700 continue;
1701 if (info->hook_entry[i] == 0xFFFFFFFF) {
1702 duprintf("Invalid hook entry %u %u\n",
1703 i, hook_entries[i]);
1704 goto out_unlock;
1705 }
1706 if (info->underflow[i] == 0xFFFFFFFF) {
1707 duprintf("Invalid underflow %u %u\n",
1708 i, underflows[i]);
1709 goto out_unlock;
1710 }
1711 }
1712
1713 ret = -ENOMEM;
1714 newinfo = xt_alloc_table_info(size);
1715 if (!newinfo)
1716 goto out_unlock;
1717
1718 newinfo->number = number;
1719 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1720 newinfo->hook_entry[i] = info->hook_entry[i];
1721 newinfo->underflow[i] = info->underflow[i];
1722 }
1723 entry1 = newinfo->entries;
1724 pos = entry1;
1725 size = total_size;
1726 xt_entry_foreach(iter0, entry0, total_size) {
1727 ret = compat_copy_entry_from_user(iter0, &pos, &size,
1728 name, newinfo, entry1);
1729 if (ret != 0)
1730 break;
1731 }
1732 xt_compat_flush_offsets(AF_INET);
1733 xt_compat_unlock(AF_INET);
1734 if (ret)
1735 goto free_newinfo;
1736
1737 ret = -ELOOP;
1738 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1739 goto free_newinfo;
1740
1741 i = 0;
1742 xt_entry_foreach(iter1, entry1, newinfo->size) {
1743 ret = compat_check_entry(iter1, net, name);
1744 if (ret != 0)
1745 break;
1746 ++i;
1747 if (strcmp(ipt_get_target(iter1)->u.user.name,
1748 XT_ERROR_TARGET) == 0)
1749 ++newinfo->stacksize;
1750 }
1751 if (ret) {
1752
1753
1754
1755
1756
1757 int skip = i;
1758 j -= i;
1759 xt_entry_foreach(iter0, entry0, newinfo->size) {
1760 if (skip-- > 0)
1761 continue;
1762 if (j-- == 0)
1763 break;
1764 compat_release_entry(iter0);
1765 }
1766 xt_entry_foreach(iter1, entry1, newinfo->size) {
1767 if (i-- == 0)
1768 break;
1769 cleanup_entry(iter1, net);
1770 }
1771 xt_free_table_info(newinfo);
1772 return ret;
1773 }
1774
1775 *pinfo = newinfo;
1776 *pentry0 = entry1;
1777 xt_free_table_info(info);
1778 return 0;
1779
1780free_newinfo:
1781 xt_free_table_info(newinfo);
1782out:
1783 xt_entry_foreach(iter0, entry0, total_size) {
1784 if (j-- == 0)
1785 break;
1786 compat_release_entry(iter0);
1787 }
1788 return ret;
1789out_unlock:
1790 xt_compat_flush_offsets(AF_INET);
1791 xt_compat_unlock(AF_INET);
1792 goto out;
1793}
1794
1795static int
1796compat_do_replace(struct net *net, void __user *user, unsigned int len)
1797{
1798 int ret;
1799 struct compat_ipt_replace tmp;
1800 struct xt_table_info *newinfo;
1801 void *loc_cpu_entry;
1802 struct ipt_entry *iter;
1803
1804 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1805 return -EFAULT;
1806
1807
1808 if (tmp.size >= INT_MAX / num_possible_cpus())
1809 return -ENOMEM;
1810 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1811 return -ENOMEM;
1812 if (tmp.num_counters == 0)
1813 return -EINVAL;
1814
1815 tmp.name[sizeof(tmp.name)-1] = 0;
1816
1817 newinfo = xt_alloc_table_info(tmp.size);
1818 if (!newinfo)
1819 return -ENOMEM;
1820
1821 loc_cpu_entry = newinfo->entries;
1822 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1823 tmp.size) != 0) {
1824 ret = -EFAULT;
1825 goto free_newinfo;
1826 }
1827
1828 ret = translate_compat_table(net, tmp.name, tmp.valid_hooks,
1829 &newinfo, &loc_cpu_entry, tmp.size,
1830 tmp.num_entries, tmp.hook_entry,
1831 tmp.underflow);
1832 if (ret != 0)
1833 goto free_newinfo;
1834
1835 duprintf("compat_do_replace: Translated table\n");
1836
1837 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1838 tmp.num_counters, compat_ptr(tmp.counters));
1839 if (ret)
1840 goto free_newinfo_untrans;
1841 return 0;
1842
1843 free_newinfo_untrans:
1844 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1845 cleanup_entry(iter, net);
1846 free_newinfo:
1847 xt_free_table_info(newinfo);
1848 return ret;
1849}
1850
1851static int
1852compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
1853 unsigned int len)
1854{
1855 int ret;
1856
1857 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1858 return -EPERM;
1859
1860 switch (cmd) {
1861 case IPT_SO_SET_REPLACE:
1862 ret = compat_do_replace(sock_net(sk), user, len);
1863 break;
1864
1865 case IPT_SO_SET_ADD_COUNTERS:
1866 ret = do_add_counters(sock_net(sk), user, len, 1);
1867 break;
1868
1869 default:
1870 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1871 ret = -EINVAL;
1872 }
1873
1874 return ret;
1875}
1876
1877struct compat_ipt_get_entries {
1878 char name[XT_TABLE_MAXNAMELEN];
1879 compat_uint_t size;
1880 struct compat_ipt_entry entrytable[0];
1881};
1882
1883static int
1884compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1885 void __user *userptr)
1886{
1887 struct xt_counters *counters;
1888 const struct xt_table_info *private = table->private;
1889 void __user *pos;
1890 unsigned int size;
1891 int ret = 0;
1892 unsigned int i = 0;
1893 struct ipt_entry *iter;
1894
1895 counters = alloc_counters(table);
1896 if (IS_ERR(counters))
1897 return PTR_ERR(counters);
1898
1899 pos = userptr;
1900 size = total_size;
1901 xt_entry_foreach(iter, private->entries, total_size) {
1902 ret = compat_copy_entry_to_user(iter, &pos,
1903 &size, counters, i++);
1904 if (ret != 0)
1905 break;
1906 }
1907
1908 vfree(counters);
1909 return ret;
1910}
1911
1912static int
1913compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1914 int *len)
1915{
1916 int ret;
1917 struct compat_ipt_get_entries get;
1918 struct xt_table *t;
1919
1920 if (*len < sizeof(get)) {
1921 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1922 return -EINVAL;
1923 }
1924
1925 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1926 return -EFAULT;
1927
1928 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
1929 duprintf("compat_get_entries: %u != %zu\n",
1930 *len, sizeof(get) + get.size);
1931 return -EINVAL;
1932 }
1933
1934 xt_compat_lock(AF_INET);
1935 t = xt_find_table_lock(net, AF_INET, get.name);
1936 if (!IS_ERR_OR_NULL(t)) {
1937 const struct xt_table_info *private = t->private;
1938 struct xt_table_info info;
1939 duprintf("t->private->number = %u\n", private->number);
1940 ret = compat_table_info(private, &info);
1941 if (!ret && get.size == info.size) {
1942 ret = compat_copy_entries_to_user(private->size,
1943 t, uptr->entrytable);
1944 } else if (!ret) {
1945 duprintf("compat_get_entries: I've got %u not %u!\n",
1946 private->size, get.size);
1947 ret = -EAGAIN;
1948 }
1949 xt_compat_flush_offsets(AF_INET);
1950 module_put(t->me);
1951 xt_table_unlock(t);
1952 } else
1953 ret = t ? PTR_ERR(t) : -ENOENT;
1954
1955 xt_compat_unlock(AF_INET);
1956 return ret;
1957}
1958
1959static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1960
1961static int
1962compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1963{
1964 int ret;
1965
1966 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1967 return -EPERM;
1968
1969 switch (cmd) {
1970 case IPT_SO_GET_INFO:
1971 ret = get_info(sock_net(sk), user, len, 1);
1972 break;
1973 case IPT_SO_GET_ENTRIES:
1974 ret = compat_get_entries(sock_net(sk), user, len);
1975 break;
1976 default:
1977 ret = do_ipt_get_ctl(sk, cmd, user, len);
1978 }
1979 return ret;
1980}
1981#endif
1982
1983static int
1984do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1985{
1986 int ret;
1987
1988 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1989 return -EPERM;
1990
1991 switch (cmd) {
1992 case IPT_SO_SET_REPLACE:
1993 ret = do_replace(sock_net(sk), user, len);
1994 break;
1995
1996 case IPT_SO_SET_ADD_COUNTERS:
1997 ret = do_add_counters(sock_net(sk), user, len, 0);
1998 break;
1999
2000 default:
2001 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
2002 ret = -EINVAL;
2003 }
2004
2005 return ret;
2006}
2007
2008static int
2009do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2010{
2011 int ret;
2012
2013 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
2014 return -EPERM;
2015
2016 switch (cmd) {
2017 case IPT_SO_GET_INFO:
2018 ret = get_info(sock_net(sk), user, len, 0);
2019 break;
2020
2021 case IPT_SO_GET_ENTRIES:
2022 ret = get_entries(sock_net(sk), user, len);
2023 break;
2024
2025 case IPT_SO_GET_REVISION_MATCH:
2026 case IPT_SO_GET_REVISION_TARGET: {
2027 struct xt_get_revision rev;
2028 int target;
2029
2030 if (*len != sizeof(rev)) {
2031 ret = -EINVAL;
2032 break;
2033 }
2034 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2035 ret = -EFAULT;
2036 break;
2037 }
2038 rev.name[sizeof(rev.name)-1] = 0;
2039
2040 if (cmd == IPT_SO_GET_REVISION_TARGET)
2041 target = 1;
2042 else
2043 target = 0;
2044
2045 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2046 rev.revision,
2047 target, &ret),
2048 "ipt_%s", rev.name);
2049 break;
2050 }
2051
2052 default:
2053 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2054 ret = -EINVAL;
2055 }
2056
2057 return ret;
2058}
2059
2060struct xt_table *ipt_register_table(struct net *net,
2061 const struct xt_table *table,
2062 const struct ipt_replace *repl)
2063{
2064 int ret;
2065 struct xt_table_info *newinfo;
2066 struct xt_table_info bootstrap = {0};
2067 void *loc_cpu_entry;
2068 struct xt_table *new_table;
2069
2070 newinfo = xt_alloc_table_info(repl->size);
2071 if (!newinfo) {
2072 ret = -ENOMEM;
2073 goto out;
2074 }
2075
2076 loc_cpu_entry = newinfo->entries;
2077 memcpy(loc_cpu_entry, repl->entries, repl->size);
2078
2079 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
2080 if (ret != 0)
2081 goto out_free;
2082
2083 new_table = xt_register_table(net, table, &bootstrap, newinfo);
2084 if (IS_ERR(new_table)) {
2085 ret = PTR_ERR(new_table);
2086 goto out_free;
2087 }
2088
2089 return new_table;
2090
2091out_free:
2092 xt_free_table_info(newinfo);
2093out:
2094 return ERR_PTR(ret);
2095}
2096
2097void ipt_unregister_table(struct net *net, struct xt_table *table)
2098{
2099 struct xt_table_info *private;
2100 void *loc_cpu_entry;
2101 struct module *table_owner = table->me;
2102 struct ipt_entry *iter;
2103
2104 private = xt_unregister_table(table);
2105
2106
2107 loc_cpu_entry = private->entries;
2108 xt_entry_foreach(iter, loc_cpu_entry, private->size)
2109 cleanup_entry(iter, net);
2110 if (private->number > private->initial_entries)
2111 module_put(table_owner);
2112 xt_free_table_info(private);
2113}
2114
2115
2116static inline bool
2117icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2118 u_int8_t type, u_int8_t code,
2119 bool invert)
2120{
2121 return ((test_type == 0xFF) ||
2122 (type == test_type && code >= min_code && code <= max_code))
2123 ^ invert;
2124}
2125
2126static bool
2127icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
2128{
2129 const struct icmphdr *ic;
2130 struct icmphdr _icmph;
2131 const struct ipt_icmp *icmpinfo = par->matchinfo;
2132
2133
2134 if (par->fragoff != 0)
2135 return false;
2136
2137 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
2138 if (ic == NULL) {
2139
2140
2141
2142 duprintf("Dropping evil ICMP tinygram.\n");
2143 par->hotdrop = true;
2144 return false;
2145 }
2146
2147 return icmp_type_code_match(icmpinfo->type,
2148 icmpinfo->code[0],
2149 icmpinfo->code[1],
2150 ic->type, ic->code,
2151 !!(icmpinfo->invflags&IPT_ICMP_INV));
2152}
2153
2154static int icmp_checkentry(const struct xt_mtchk_param *par)
2155{
2156 const struct ipt_icmp *icmpinfo = par->matchinfo;
2157
2158
2159 return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0;
2160}
2161
2162static struct xt_target ipt_builtin_tg[] __read_mostly = {
2163 {
2164 .name = XT_STANDARD_TARGET,
2165 .targetsize = sizeof(int),
2166 .family = NFPROTO_IPV4,
2167#ifdef CONFIG_COMPAT
2168 .compatsize = sizeof(compat_int_t),
2169 .compat_from_user = compat_standard_from_user,
2170 .compat_to_user = compat_standard_to_user,
2171#endif
2172 },
2173 {
2174 .name = XT_ERROR_TARGET,
2175 .target = ipt_error,
2176 .targetsize = XT_FUNCTION_MAXNAMELEN,
2177 .family = NFPROTO_IPV4,
2178 },
2179};
2180
2181static struct nf_sockopt_ops ipt_sockopts = {
2182 .pf = PF_INET,
2183 .set_optmin = IPT_BASE_CTL,
2184 .set_optmax = IPT_SO_SET_MAX+1,
2185 .set = do_ipt_set_ctl,
2186#ifdef CONFIG_COMPAT
2187 .compat_set = compat_do_ipt_set_ctl,
2188#endif
2189 .get_optmin = IPT_BASE_CTL,
2190 .get_optmax = IPT_SO_GET_MAX+1,
2191 .get = do_ipt_get_ctl,
2192#ifdef CONFIG_COMPAT
2193 .compat_get = compat_do_ipt_get_ctl,
2194#endif
2195 .owner = THIS_MODULE,
2196};
2197
2198static struct xt_match ipt_builtin_mt[] __read_mostly = {
2199 {
2200 .name = "icmp",
2201 .match = icmp_match,
2202 .matchsize = sizeof(struct ipt_icmp),
2203 .checkentry = icmp_checkentry,
2204 .proto = IPPROTO_ICMP,
2205 .family = NFPROTO_IPV4,
2206 },
2207};
2208
2209static int __net_init ip_tables_net_init(struct net *net)
2210{
2211 return xt_proto_init(net, NFPROTO_IPV4);
2212}
2213
2214static void __net_exit ip_tables_net_exit(struct net *net)
2215{
2216 xt_proto_fini(net, NFPROTO_IPV4);
2217}
2218
2219static struct pernet_operations ip_tables_net_ops = {
2220 .init = ip_tables_net_init,
2221 .exit = ip_tables_net_exit,
2222};
2223
2224static int __init ip_tables_init(void)
2225{
2226 int ret;
2227
2228 ret = register_pernet_subsys(&ip_tables_net_ops);
2229 if (ret < 0)
2230 goto err1;
2231
2232
2233 ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
2234 if (ret < 0)
2235 goto err2;
2236 ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
2237 if (ret < 0)
2238 goto err4;
2239
2240
2241 ret = nf_register_sockopt(&ipt_sockopts);
2242 if (ret < 0)
2243 goto err5;
2244
2245 pr_info("(C) 2000-2006 Netfilter Core Team\n");
2246 return 0;
2247
2248err5:
2249 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
2250err4:
2251 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
2252err2:
2253 unregister_pernet_subsys(&ip_tables_net_ops);
2254err1:
2255 return ret;
2256}
2257
2258static void __exit ip_tables_fini(void)
2259{
2260 nf_unregister_sockopt(&ipt_sockopts);
2261
2262 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
2263 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
2264 unregister_pernet_subsys(&ip_tables_net_ops);
2265}
2266
2267EXPORT_SYMBOL(ipt_register_table);
2268EXPORT_SYMBOL(ipt_unregister_table);
2269EXPORT_SYMBOL(ipt_do_table);
2270module_init(ip_tables_init);
2271module_exit(ip_tables_fini);
2272