netfilter: xtables: move extension arguments into compound structure (1/6)
[deliverable/linux.git] / net / ipv4 / netfilter / ip_tables.c
CommitLineData
1da177e4
LT
1/*
2 * Packet matching code.
3 *
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
2e4e6a17 5 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
1da177e4
LT
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
1da177e4 10 */
1da177e4 11#include <linux/cache.h>
4fc268d2 12#include <linux/capability.h>
1da177e4
LT
13#include <linux/skbuff.h>
14#include <linux/kmod.h>
15#include <linux/vmalloc.h>
16#include <linux/netdevice.h>
17#include <linux/module.h>
1da177e4
LT
18#include <linux/icmp.h>
19#include <net/ip.h>
2722971c 20#include <net/compat.h>
1da177e4 21#include <asm/uaccess.h>
57b47a53 22#include <linux/mutex.h>
1da177e4
LT
23#include <linux/proc_fs.h>
24#include <linux/err.h>
c8923c6b 25#include <linux/cpumask.h>
1da177e4 26
2e4e6a17 27#include <linux/netfilter/x_tables.h>
1da177e4 28#include <linux/netfilter_ipv4/ip_tables.h>
f01ffbd6 29#include <net/netfilter/nf_log.h>
1da177e4
LT
30
31MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
33MODULE_DESCRIPTION("IPv4 packet filter");
34
35/*#define DEBUG_IP_FIREWALL*/
36/*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging */
37/*#define DEBUG_IP_FIREWALL_USER*/
38
39#ifdef DEBUG_IP_FIREWALL
40#define dprintf(format, args...) printk(format , ## args)
41#else
42#define dprintf(format, args...)
43#endif
44
45#ifdef DEBUG_IP_FIREWALL_USER
46#define duprintf(format, args...) printk(format , ## args)
47#else
48#define duprintf(format, args...)
49#endif
50
51#ifdef CONFIG_NETFILTER_DEBUG
52#define IP_NF_ASSERT(x) \
53do { \
54 if (!(x)) \
55 printk("IP_NF_ASSERT: %s:%s:%u\n", \
0dc47877 56 __func__, __FILE__, __LINE__); \
1da177e4
LT
57} while(0)
58#else
59#define IP_NF_ASSERT(x)
60#endif
1da177e4
LT
61
62#if 0
63/* All the better to debug you with... */
64#define static
65#define inline
66#endif
67
68/*
69 We keep a set of rules for each CPU, so we can avoid write-locking
70 them in the softirq when updating the counters and therefore
71 only need to read-lock in the softirq; doing a write_lock_bh() in user
72 context stops packets coming through and allows user context to read
73 the counters or update the rules.
74
1da177e4
LT
75 Hence the start of any table is given by get_table() below. */
76
1da177e4 77/* Returns whether matches rule or not. */
022748a9 78/* Performance critical - called for every packet */
9c547959 79static inline bool
1da177e4
LT
80ip_packet_match(const struct iphdr *ip,
81 const char *indev,
82 const char *outdev,
83 const struct ipt_ip *ipinfo,
84 int isfrag)
85{
86 size_t i;
87 unsigned long ret;
88
e79ec50b 89#define FWINV(bool, invflg) ((bool) ^ !!(ipinfo->invflags & (invflg)))
1da177e4
LT
90
91 if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
92 IPT_INV_SRCIP)
93 || FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
94 IPT_INV_DSTIP)) {
95 dprintf("Source or dest mismatch.\n");
96
97 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
98 NIPQUAD(ip->saddr),
99 NIPQUAD(ipinfo->smsk.s_addr),
100 NIPQUAD(ipinfo->src.s_addr),
101 ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
102 dprintf("DST: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
103 NIPQUAD(ip->daddr),
104 NIPQUAD(ipinfo->dmsk.s_addr),
105 NIPQUAD(ipinfo->dst.s_addr),
106 ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
9c547959 107 return false;
1da177e4
LT
108 }
109
110 /* Look for ifname matches; this should unroll nicely. */
111 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
112 ret |= (((const unsigned long *)indev)[i]
113 ^ ((const unsigned long *)ipinfo->iniface)[i])
114 & ((const unsigned long *)ipinfo->iniface_mask)[i];
115 }
116
117 if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
118 dprintf("VIA in mismatch (%s vs %s).%s\n",
119 indev, ipinfo->iniface,
120 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
9c547959 121 return false;
1da177e4
LT
122 }
123
124 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
125 ret |= (((const unsigned long *)outdev)[i]
126 ^ ((const unsigned long *)ipinfo->outiface)[i])
127 & ((const unsigned long *)ipinfo->outiface_mask)[i];
128 }
129
130 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
131 dprintf("VIA out mismatch (%s vs %s).%s\n",
132 outdev, ipinfo->outiface,
133 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
9c547959 134 return false;
1da177e4
LT
135 }
136
137 /* Check specific protocol */
138 if (ipinfo->proto
139 && FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
140 dprintf("Packet protocol %hi does not match %hi.%s\n",
141 ip->protocol, ipinfo->proto,
142 ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
9c547959 143 return false;
1da177e4
LT
144 }
145
146 /* If we have a fragment rule but the packet is not a fragment
147 * then we return zero */
148 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
149 dprintf("Fragment rule but not fragment.%s\n",
150 ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
9c547959 151 return false;
1da177e4
LT
152 }
153
9c547959 154 return true;
1da177e4
LT
155}
156
022748a9 157static bool
1da177e4
LT
158ip_checkentry(const struct ipt_ip *ip)
159{
160 if (ip->flags & ~IPT_F_MASK) {
161 duprintf("Unknown flag bits set: %08X\n",
162 ip->flags & ~IPT_F_MASK);
ccb79bdc 163 return false;
1da177e4
LT
164 }
165 if (ip->invflags & ~IPT_INV_MASK) {
166 duprintf("Unknown invflag bits set: %08X\n",
167 ip->invflags & ~IPT_INV_MASK);
ccb79bdc 168 return false;
1da177e4 169 }
ccb79bdc 170 return true;
1da177e4
LT
171}
172
173static unsigned int
3db05fea 174ipt_error(struct sk_buff *skb,
1da177e4
LT
175 const struct net_device *in,
176 const struct net_device *out,
177 unsigned int hooknum,
c4986734 178 const struct xt_target *target,
fe1cb108 179 const void *targinfo)
1da177e4
LT
180{
181 if (net_ratelimit())
182 printk("ip_tables: error: `%s'\n", (char *)targinfo);
183
184 return NF_DROP;
185}
186
022748a9
DV
187/* Performance critical - called for every packet */
188static inline bool
f7108a20
JE
189do_match(struct ipt_entry_match *m, const struct sk_buff *skb,
190 struct xt_match_param *par)
1da177e4 191{
f7108a20
JE
192 par->match = m->u.kernel.match;
193 par->matchinfo = m->data;
194
1da177e4 195 /* Stop iteration if it doesn't match */
f7108a20 196 if (!m->u.kernel.match->match(skb, par))
1d93a9cb 197 return true;
1da177e4 198 else
1d93a9cb 199 return false;
1da177e4
LT
200}
201
022748a9 202/* Performance critical */
1da177e4
LT
203static inline struct ipt_entry *
204get_entry(void *base, unsigned int offset)
205{
206 return (struct ipt_entry *)(base + offset);
207}
208
ba9dda3a 209/* All zeroes == unconditional rule. */
022748a9 210/* Mildly perf critical (only if packet tracing is on) */
ba9dda3a
JK
211static inline int
212unconditional(const struct ipt_ip *ip)
213{
214 unsigned int i;
215
216 for (i = 0; i < sizeof(*ip)/sizeof(__u32); i++)
217 if (((__u32 *)ip)[i])
218 return 0;
219
220 return 1;
e79ec50b 221#undef FWINV
ba9dda3a
JK
222}
223
224#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
225 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
022748a9 226static const char *const hooknames[] = {
6e23ae2a
PM
227 [NF_INET_PRE_ROUTING] = "PREROUTING",
228 [NF_INET_LOCAL_IN] = "INPUT",
9c547959 229 [NF_INET_FORWARD] = "FORWARD",
6e23ae2a
PM
230 [NF_INET_LOCAL_OUT] = "OUTPUT",
231 [NF_INET_POST_ROUTING] = "POSTROUTING",
ba9dda3a
JK
232};
233
234enum nf_ip_trace_comments {
235 NF_IP_TRACE_COMMENT_RULE,
236 NF_IP_TRACE_COMMENT_RETURN,
237 NF_IP_TRACE_COMMENT_POLICY,
238};
239
022748a9 240static const char *const comments[] = {
ba9dda3a
JK
241 [NF_IP_TRACE_COMMENT_RULE] = "rule",
242 [NF_IP_TRACE_COMMENT_RETURN] = "return",
243 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
244};
245
246static struct nf_loginfo trace_loginfo = {
247 .type = NF_LOG_TYPE_LOG,
248 .u = {
249 .log = {
250 .level = 4,
251 .logflags = NF_LOG_MASK,
252 },
253 },
254};
255
022748a9 256/* Mildly perf critical (only if packet tracing is on) */
ba9dda3a
JK
257static inline int
258get_chainname_rulenum(struct ipt_entry *s, struct ipt_entry *e,
259 char *hookname, char **chainname,
260 char **comment, unsigned int *rulenum)
261{
262 struct ipt_standard_target *t = (void *)ipt_get_target(s);
263
264 if (strcmp(t->target.u.kernel.target->name, IPT_ERROR_TARGET) == 0) {
265 /* Head of user chain: ERROR target with chainname */
266 *chainname = t->target.data;
267 (*rulenum) = 0;
268 } else if (s == e) {
269 (*rulenum)++;
270
271 if (s->target_offset == sizeof(struct ipt_entry)
272 && strcmp(t->target.u.kernel.target->name,
273 IPT_STANDARD_TARGET) == 0
274 && t->verdict < 0
275 && unconditional(&s->ip)) {
276 /* Tail of chains: STANDARD target (return/policy) */
277 *comment = *chainname == hookname
278 ? (char *)comments[NF_IP_TRACE_COMMENT_POLICY]
279 : (char *)comments[NF_IP_TRACE_COMMENT_RETURN];
280 }
281 return 1;
282 } else
283 (*rulenum)++;
284
285 return 0;
286}
287
288static void trace_packet(struct sk_buff *skb,
289 unsigned int hook,
290 const struct net_device *in,
291 const struct net_device *out,
ecb6f85e 292 const char *tablename,
ba9dda3a
JK
293 struct xt_table_info *private,
294 struct ipt_entry *e)
295{
296 void *table_base;
5452e425 297 const struct ipt_entry *root;
ba9dda3a
JK
298 char *hookname, *chainname, *comment;
299 unsigned int rulenum = 0;
300
301 table_base = (void *)private->entries[smp_processor_id()];
302 root = get_entry(table_base, private->hook_entry[hook]);
303
304 hookname = chainname = (char *)hooknames[hook];
305 comment = (char *)comments[NF_IP_TRACE_COMMENT_RULE];
306
307 IPT_ENTRY_ITERATE(root,
308 private->size - private->hook_entry[hook],
309 get_chainname_rulenum,
310 e, hookname, &chainname, &comment, &rulenum);
311
312 nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo,
313 "TRACE: %s:%s:%s:%u ",
314 tablename, chainname, comment, rulenum);
315}
316#endif
317
1da177e4
LT
318/* Returns one of the generic firewall policies, like NF_ACCEPT. */
319unsigned int
3db05fea 320ipt_do_table(struct sk_buff *skb,
1da177e4
LT
321 unsigned int hook,
322 const struct net_device *in,
323 const struct net_device *out,
e60a13e0 324 struct xt_table *table)
1da177e4
LT
325{
326 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
5452e425 327 const struct iphdr *ip;
1da177e4 328 u_int16_t datalen;
cff533ac 329 bool hotdrop = false;
1da177e4
LT
330 /* Initializing verdict to NF_DROP keeps gcc happy. */
331 unsigned int verdict = NF_DROP;
332 const char *indev, *outdev;
333 void *table_base;
334 struct ipt_entry *e, *back;
8311731a 335 struct xt_table_info *private;
f7108a20 336 struct xt_match_param mtpar;
1da177e4
LT
337
338 /* Initialization */
3db05fea
HX
339 ip = ip_hdr(skb);
340 datalen = skb->len - ip->ihl * 4;
1da177e4
LT
341 indev = in ? in->name : nulldevname;
342 outdev = out ? out->name : nulldevname;
343 /* We handle fragments by dealing with the first fragment as
344 * if it was a normal packet. All other fragments are treated
345 * normally, except that they will NEVER match rules that ask
346 * things we don't know, ie. tcp syn flag or ports). If the
347 * rule is also a fragment-specific rule, non-fragments won't
348 * match it. */
f7108a20
JE
349 mtpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
350 mtpar.thoff = ip_hdrlen(skb);
351 mtpar.hotdrop = &hotdrop;
352 mtpar.in = in;
353 mtpar.out = out;
1da177e4
LT
354
355 read_lock_bh(&table->lock);
356 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
8311731a 357 private = table->private;
2e4e6a17
HW
358 table_base = (void *)private->entries[smp_processor_id()];
359 e = get_entry(table_base, private->hook_entry[hook]);
1da177e4
LT
360
361 /* For return from builtin chain */
2e4e6a17 362 back = get_entry(table_base, private->underflow[hook]);
1da177e4
LT
363
364 do {
365 IP_NF_ASSERT(e);
366 IP_NF_ASSERT(back);
f7108a20
JE
367 if (ip_packet_match(ip, indev, outdev,
368 &e->ip, mtpar.fragoff)) {
1da177e4
LT
369 struct ipt_entry_target *t;
370
f7108a20 371 if (IPT_MATCH_ITERATE(e, do_match, skb, &mtpar) != 0)
1da177e4
LT
372 goto no_match;
373
374 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
375
376 t = ipt_get_target(e);
377 IP_NF_ASSERT(t->u.kernel.target);
ba9dda3a
JK
378
379#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
380 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
381 /* The packet is traced: log it */
3db05fea
HX
382 if (unlikely(skb->nf_trace))
383 trace_packet(skb, hook, in, out,
ba9dda3a
JK
384 table->name, private, e);
385#endif
1da177e4
LT
386 /* Standard target? */
387 if (!t->u.kernel.target->target) {
388 int v;
389
390 v = ((struct ipt_standard_target *)t)->verdict;
391 if (v < 0) {
392 /* Pop from stack? */
393 if (v != IPT_RETURN) {
394 verdict = (unsigned)(-v) - 1;
395 break;
396 }
397 e = back;
398 back = get_entry(table_base,
399 back->comefrom);
400 continue;
401 }
05465343
PM
402 if (table_base + v != (void *)e + e->next_offset
403 && !(e->ip.flags & IPT_F_GOTO)) {
1da177e4
LT
404 /* Save old back ptr in next entry */
405 struct ipt_entry *next
406 = (void *)e + e->next_offset;
407 next->comefrom
408 = (void *)back - table_base;
409 /* set back pointer to next entry */
410 back = next;
411 }
412
413 e = get_entry(table_base, v);
414 } else {
415 /* Targets which reenter must return
e905a9ed 416 abs. verdicts */
1da177e4
LT
417#ifdef CONFIG_NETFILTER_DEBUG
418 ((struct ipt_entry *)table_base)->comefrom
419 = 0xeeeeeeec;
420#endif
3db05fea 421 verdict = t->u.kernel.target->target(skb,
1da177e4
LT
422 in, out,
423 hook,
1c524830 424 t->u.kernel.target,
fe1cb108 425 t->data);
1da177e4
LT
426
427#ifdef CONFIG_NETFILTER_DEBUG
428 if (((struct ipt_entry *)table_base)->comefrom
429 != 0xeeeeeeec
430 && verdict == IPT_CONTINUE) {
431 printk("Target %s reentered!\n",
432 t->u.kernel.target->name);
433 verdict = NF_DROP;
434 }
435 ((struct ipt_entry *)table_base)->comefrom
436 = 0x57acc001;
437#endif
438 /* Target might have changed stuff. */
3db05fea
HX
439 ip = ip_hdr(skb);
440 datalen = skb->len - ip->ihl * 4;
1da177e4
LT
441
442 if (verdict == IPT_CONTINUE)
443 e = (void *)e + e->next_offset;
444 else
445 /* Verdict */
446 break;
447 }
448 } else {
449
450 no_match:
451 e = (void *)e + e->next_offset;
452 }
453 } while (!hotdrop);
454
1da177e4
LT
455 read_unlock_bh(&table->lock);
456
457#ifdef DEBUG_ALLOW_ALL
458 return NF_ACCEPT;
459#else
460 if (hotdrop)
461 return NF_DROP;
462 else return verdict;
463#endif
464}
465
1da177e4
LT
466/* Figures out from what hook each rule can be called: returns 0 if
467 there are loops. Puts hook bitmask in comefrom. */
468static int
2e4e6a17 469mark_source_chains(struct xt_table_info *newinfo,
31836064 470 unsigned int valid_hooks, void *entry0)
1da177e4
LT
471{
472 unsigned int hook;
473
474 /* No recursion; use packet counter to save back ptrs (reset
475 to 0 as we leave), and comefrom to save source hook bitmask */
6e23ae2a 476 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
1da177e4 477 unsigned int pos = newinfo->hook_entry[hook];
9c547959 478 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
1da177e4
LT
479
480 if (!(valid_hooks & (1 << hook)))
481 continue;
482
483 /* Set initial back pointer. */
484 e->counters.pcnt = pos;
485
486 for (;;) {
487 struct ipt_standard_target *t
488 = (void *)ipt_get_target(e);
e1b4b9f3 489 int visited = e->comefrom & (1 << hook);
1da177e4 490
6e23ae2a 491 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
1da177e4
LT
492 printk("iptables: loop hook %u pos %u %08X.\n",
493 hook, pos, e->comefrom);
494 return 0;
495 }
9c547959 496 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
1da177e4
LT
497
498 /* Unconditional return/END. */
e1b4b9f3 499 if ((e->target_offset == sizeof(struct ipt_entry)
1da177e4
LT
500 && (strcmp(t->target.u.user.name,
501 IPT_STANDARD_TARGET) == 0)
502 && t->verdict < 0
e1b4b9f3 503 && unconditional(&e->ip)) || visited) {
1da177e4
LT
504 unsigned int oldpos, size;
505
74c9c0c1
DM
506 if (t->verdict < -NF_MAX_VERDICT - 1) {
507 duprintf("mark_source_chains: bad "
508 "negative verdict (%i)\n",
509 t->verdict);
510 return 0;
511 }
512
1da177e4
LT
513 /* Return: backtrack through the last
514 big jump. */
515 do {
6e23ae2a 516 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
1da177e4
LT
517#ifdef DEBUG_IP_FIREWALL_USER
518 if (e->comefrom
6e23ae2a 519 & (1 << NF_INET_NUMHOOKS)) {
1da177e4
LT
520 duprintf("Back unset "
521 "on hook %u "
522 "rule %u\n",
523 hook, pos);
524 }
525#endif
526 oldpos = pos;
527 pos = e->counters.pcnt;
528 e->counters.pcnt = 0;
529
530 /* We're at the start. */
531 if (pos == oldpos)
532 goto next;
533
534 e = (struct ipt_entry *)
31836064 535 (entry0 + pos);
1da177e4
LT
536 } while (oldpos == pos + e->next_offset);
537
538 /* Move along one */
539 size = e->next_offset;
540 e = (struct ipt_entry *)
31836064 541 (entry0 + pos + size);
1da177e4
LT
542 e->counters.pcnt = pos;
543 pos += size;
544 } else {
545 int newpos = t->verdict;
546
547 if (strcmp(t->target.u.user.name,
548 IPT_STANDARD_TARGET) == 0
549 && newpos >= 0) {
74c9c0c1
DM
550 if (newpos > newinfo->size -
551 sizeof(struct ipt_entry)) {
552 duprintf("mark_source_chains: "
553 "bad verdict (%i)\n",
554 newpos);
555 return 0;
556 }
1da177e4
LT
557 /* This a jump; chase it. */
558 duprintf("Jump rule %u -> %u\n",
559 pos, newpos);
560 } else {
561 /* ... this is a fallthru */
562 newpos = pos + e->next_offset;
563 }
564 e = (struct ipt_entry *)
31836064 565 (entry0 + newpos);
1da177e4
LT
566 e->counters.pcnt = pos;
567 pos = newpos;
568 }
569 }
570 next:
571 duprintf("Finished chain %u\n", hook);
572 }
573 return 1;
574}
575
022748a9 576static int
1da177e4
LT
577cleanup_match(struct ipt_entry_match *m, unsigned int *i)
578{
579 if (i && (*i)-- == 0)
580 return 1;
581
582 if (m->u.kernel.match->destroy)
efa74165 583 m->u.kernel.match->destroy(m->u.kernel.match, m->data);
1da177e4
LT
584 module_put(m->u.kernel.match->me);
585 return 0;
586}
587
022748a9 588static int
a96be246
DM
589check_entry(struct ipt_entry *e, const char *name)
590{
591 struct ipt_entry_target *t;
592
593 if (!ip_checkentry(&e->ip)) {
594 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
595 return -EINVAL;
596 }
597
9c547959
PM
598 if (e->target_offset + sizeof(struct ipt_entry_target) >
599 e->next_offset)
a96be246
DM
600 return -EINVAL;
601
602 t = ipt_get_target(e);
603 if (e->target_offset + t->u.target_size > e->next_offset)
604 return -EINVAL;
605
606 return 0;
607}
608
022748a9
DV
609static int
610check_match(struct ipt_entry_match *m, const char *name,
4b478248
PM
611 const struct ipt_ip *ip,
612 unsigned int hookmask, unsigned int *i)
a96be246 613{
6709dbbb 614 struct xt_match *match;
a96be246
DM
615 int ret;
616
617 match = m->u.kernel.match;
618 ret = xt_check_match(match, AF_INET, m->u.match_size - sizeof(*m),
619 name, hookmask, ip->proto,
367c6790
JE
620 ip->invflags & IPT_INV_PROTO, ip, m->data);
621 if (ret < 0) {
a96be246
DM
622 duprintf("ip_tables: check failed for `%s'.\n",
623 m->u.kernel.match->name);
367c6790 624 return ret;
a96be246 625 }
367c6790
JE
626 ++*i;
627 return 0;
a96be246
DM
628}
629
022748a9 630static int
a96be246 631find_check_match(struct ipt_entry_match *m,
4b478248
PM
632 const char *name,
633 const struct ipt_ip *ip,
634 unsigned int hookmask,
635 unsigned int *i)
1da177e4 636{
6709dbbb 637 struct xt_match *match;
3cdc7c95 638 int ret;
1da177e4 639
2e4e6a17 640 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
9c547959 641 m->u.user.revision),
1da177e4
LT
642 "ipt_%s", m->u.user.name);
643 if (IS_ERR(match) || !match) {
a96be246 644 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
1da177e4
LT
645 return match ? PTR_ERR(match) : -ENOENT;
646 }
647 m->u.kernel.match = match;
648
4c1b52bc 649 ret = check_match(m, name, ip, hookmask, i);
3cdc7c95
PM
650 if (ret)
651 goto err;
652
1da177e4 653 return 0;
3cdc7c95
PM
654err:
655 module_put(m->u.kernel.match->me);
656 return ret;
1da177e4
LT
657}
658
022748a9 659static int check_target(struct ipt_entry *e, const char *name)
a96be246 660{
e905a9ed 661 struct ipt_entry_target *t;
6709dbbb 662 struct xt_target *target;
e905a9ed 663 int ret;
a96be246
DM
664
665 t = ipt_get_target(e);
666 target = t->u.kernel.target;
667 ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
668 name, e->comefrom, e->ip.proto,
367c6790
JE
669 e->ip.invflags & IPT_INV_PROTO, e, t->data);
670 if (ret < 0) {
a96be246
DM
671 duprintf("ip_tables: check failed for `%s'.\n",
672 t->u.kernel.target->name);
367c6790 673 return ret;
a96be246 674 }
367c6790 675 return 0;
a96be246 676}
1da177e4 677
022748a9 678static int
a96be246 679find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
4b478248 680 unsigned int *i)
1da177e4
LT
681{
682 struct ipt_entry_target *t;
6709dbbb 683 struct xt_target *target;
1da177e4
LT
684 int ret;
685 unsigned int j;
686
a96be246
DM
687 ret = check_entry(e, name);
688 if (ret)
689 return ret;
590bdf7f 690
1da177e4 691 j = 0;
a96be246 692 ret = IPT_MATCH_ITERATE(e, find_check_match, name, &e->ip,
4b478248 693 e->comefrom, &j);
1da177e4
LT
694 if (ret != 0)
695 goto cleanup_matches;
696
697 t = ipt_get_target(e);
2e4e6a17 698 target = try_then_request_module(xt_find_target(AF_INET,
4b478248
PM
699 t->u.user.name,
700 t->u.user.revision),
1da177e4
LT
701 "ipt_%s", t->u.user.name);
702 if (IS_ERR(target) || !target) {
a96be246 703 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
1da177e4
LT
704 ret = target ? PTR_ERR(target) : -ENOENT;
705 goto cleanup_matches;
706 }
707 t->u.kernel.target = target;
708
a96be246 709 ret = check_target(e, name);
3cdc7c95
PM
710 if (ret)
711 goto err;
712
1da177e4
LT
713 (*i)++;
714 return 0;
3cdc7c95
PM
715 err:
716 module_put(t->u.kernel.target->me);
1da177e4
LT
717 cleanup_matches:
718 IPT_MATCH_ITERATE(e, cleanup_match, &j);
719 return ret;
720}
721
022748a9 722static int
1da177e4 723check_entry_size_and_hooks(struct ipt_entry *e,
2e4e6a17 724 struct xt_table_info *newinfo,
1da177e4
LT
725 unsigned char *base,
726 unsigned char *limit,
727 const unsigned int *hook_entries,
728 const unsigned int *underflows,
729 unsigned int *i)
730{
731 unsigned int h;
732
733 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0
734 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
735 duprintf("Bad offset %p\n", e);
736 return -EINVAL;
737 }
738
739 if (e->next_offset
740 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
741 duprintf("checking: element %p size %u\n",
742 e, e->next_offset);
743 return -EINVAL;
744 }
745
746 /* Check hooks & underflows */
6e23ae2a 747 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1da177e4
LT
748 if ((unsigned char *)e - base == hook_entries[h])
749 newinfo->hook_entry[h] = hook_entries[h];
750 if ((unsigned char *)e - base == underflows[h])
751 newinfo->underflow[h] = underflows[h];
752 }
753
754 /* FIXME: underflows must be unconditional, standard verdicts
e905a9ed 755 < 0 (not IPT_RETURN). --RR */
1da177e4
LT
756
757 /* Clear counters and comefrom */
2e4e6a17 758 e->counters = ((struct xt_counters) { 0, 0 });
1da177e4
LT
759 e->comefrom = 0;
760
761 (*i)++;
762 return 0;
763}
764
022748a9 765static int
1da177e4
LT
766cleanup_entry(struct ipt_entry *e, unsigned int *i)
767{
768 struct ipt_entry_target *t;
769
770 if (i && (*i)-- == 0)
771 return 1;
772
773 /* Cleanup all matches */
774 IPT_MATCH_ITERATE(e, cleanup_match, NULL);
775 t = ipt_get_target(e);
776 if (t->u.kernel.target->destroy)
efa74165 777 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
1da177e4
LT
778 module_put(t->u.kernel.target->me);
779 return 0;
780}
781
782/* Checks and translates the user-supplied table segment (held in
783 newinfo) */
784static int
785translate_table(const char *name,
786 unsigned int valid_hooks,
2e4e6a17 787 struct xt_table_info *newinfo,
31836064 788 void *entry0,
1da177e4
LT
789 unsigned int size,
790 unsigned int number,
791 const unsigned int *hook_entries,
792 const unsigned int *underflows)
793{
794 unsigned int i;
795 int ret;
796
797 newinfo->size = size;
798 newinfo->number = number;
799
800 /* Init all hooks to impossible value. */
6e23ae2a 801 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1da177e4
LT
802 newinfo->hook_entry[i] = 0xFFFFFFFF;
803 newinfo->underflow[i] = 0xFFFFFFFF;
804 }
805
806 duprintf("translate_table: size %u\n", newinfo->size);
807 i = 0;
808 /* Walk through entries, checking offsets. */
31836064 809 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
1da177e4
LT
810 check_entry_size_and_hooks,
811 newinfo,
31836064
ED
812 entry0,
813 entry0 + size,
1da177e4
LT
814 hook_entries, underflows, &i);
815 if (ret != 0)
816 return ret;
817
818 if (i != number) {
819 duprintf("translate_table: %u not %u entries\n",
820 i, number);
821 return -EINVAL;
822 }
823
824 /* Check hooks all assigned */
6e23ae2a 825 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1da177e4
LT
826 /* Only hooks which are valid */
827 if (!(valid_hooks & (1 << i)))
828 continue;
829 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
830 duprintf("Invalid hook entry %u %u\n",
831 i, hook_entries[i]);
832 return -EINVAL;
833 }
834 if (newinfo->underflow[i] == 0xFFFFFFFF) {
835 duprintf("Invalid underflow %u %u\n",
836 i, underflows[i]);
837 return -EINVAL;
838 }
839 }
840
74c9c0c1
DM
841 if (!mark_source_chains(newinfo, valid_hooks, entry0))
842 return -ELOOP;
843
1da177e4
LT
844 /* Finally, each sanity check must pass */
845 i = 0;
31836064 846 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
a96be246 847 find_check_entry, name, size, &i);
1da177e4 848
74c9c0c1
DM
849 if (ret != 0) {
850 IPT_ENTRY_ITERATE(entry0, newinfo->size,
851 cleanup_entry, &i);
852 return ret;
853 }
1da177e4
LT
854
855 /* And one copy for every other CPU */
6f912042 856 for_each_possible_cpu(i) {
31836064
ED
857 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
858 memcpy(newinfo->entries[i], entry0, newinfo->size);
1da177e4
LT
859 }
860
861 return ret;
862}
863
1da177e4
LT
864/* Gets counters. */
865static inline int
866add_entry_to_counter(const struct ipt_entry *e,
2e4e6a17 867 struct xt_counters total[],
1da177e4
LT
868 unsigned int *i)
869{
870 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
871
872 (*i)++;
873 return 0;
874}
875
31836064
ED
876static inline int
877set_entry_to_counter(const struct ipt_entry *e,
878 struct ipt_counters total[],
879 unsigned int *i)
880{
881 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
882
883 (*i)++;
884 return 0;
885}
886
1da177e4 887static void
2e4e6a17
HW
888get_counters(const struct xt_table_info *t,
889 struct xt_counters counters[])
1da177e4
LT
890{
891 unsigned int cpu;
892 unsigned int i;
31836064
ED
893 unsigned int curcpu;
894
895 /* Instead of clearing (by a previous call to memset())
896 * the counters and using adds, we set the counters
897 * with data used by 'current' CPU
898 * We dont care about preemption here.
899 */
900 curcpu = raw_smp_processor_id();
901
902 i = 0;
903 IPT_ENTRY_ITERATE(t->entries[curcpu],
904 t->size,
905 set_entry_to_counter,
906 counters,
907 &i);
1da177e4 908
6f912042 909 for_each_possible_cpu(cpu) {
31836064
ED
910 if (cpu == curcpu)
911 continue;
1da177e4 912 i = 0;
31836064 913 IPT_ENTRY_ITERATE(t->entries[cpu],
1da177e4
LT
914 t->size,
915 add_entry_to_counter,
916 counters,
917 &i);
918 }
919}
920
022748a9 921static struct xt_counters * alloc_counters(struct xt_table *table)
1da177e4 922{
2722971c 923 unsigned int countersize;
2e4e6a17 924 struct xt_counters *counters;
5452e425 925 const struct xt_table_info *private = table->private;
1da177e4
LT
926
927 /* We need atomic snapshot of counters: rest doesn't change
928 (other than comefrom, which userspace doesn't care
929 about). */
2e4e6a17 930 countersize = sizeof(struct xt_counters) * private->number;
31836064 931 counters = vmalloc_node(countersize, numa_node_id());
1da177e4
LT
932
933 if (counters == NULL)
2722971c 934 return ERR_PTR(-ENOMEM);
1da177e4
LT
935
936 /* First, sum counters... */
1da177e4 937 write_lock_bh(&table->lock);
2e4e6a17 938 get_counters(private, counters);
1da177e4
LT
939 write_unlock_bh(&table->lock);
940
2722971c
DM
941 return counters;
942}
943
944static int
945copy_entries_to_user(unsigned int total_size,
e60a13e0 946 struct xt_table *table,
2722971c
DM
947 void __user *userptr)
948{
949 unsigned int off, num;
950 struct ipt_entry *e;
951 struct xt_counters *counters;
5452e425 952 const struct xt_table_info *private = table->private;
2722971c 953 int ret = 0;
5452e425 954 const void *loc_cpu_entry;
2722971c
DM
955
956 counters = alloc_counters(table);
957 if (IS_ERR(counters))
958 return PTR_ERR(counters);
959
31836064
ED
960 /* choose the copy that is on our node/cpu, ...
961 * This choice is lazy (because current thread is
962 * allowed to migrate to another cpu)
963 */
2e4e6a17 964 loc_cpu_entry = private->entries[raw_smp_processor_id()];
31836064 965 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
1da177e4
LT
966 ret = -EFAULT;
967 goto free_counters;
968 }
969
970 /* FIXME: use iterator macros --RR */
971 /* ... then go back and fix counters and names */
972 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
973 unsigned int i;
5452e425
JE
974 const struct ipt_entry_match *m;
975 const struct ipt_entry_target *t;
1da177e4 976
31836064 977 e = (struct ipt_entry *)(loc_cpu_entry + off);
1da177e4
LT
978 if (copy_to_user(userptr + off
979 + offsetof(struct ipt_entry, counters),
980 &counters[num],
981 sizeof(counters[num])) != 0) {
982 ret = -EFAULT;
983 goto free_counters;
984 }
985
986 for (i = sizeof(struct ipt_entry);
987 i < e->target_offset;
988 i += m->u.match_size) {
989 m = (void *)e + i;
990
991 if (copy_to_user(userptr + off + i
992 + offsetof(struct ipt_entry_match,
993 u.user.name),
994 m->u.kernel.match->name,
995 strlen(m->u.kernel.match->name)+1)
996 != 0) {
997 ret = -EFAULT;
998 goto free_counters;
999 }
1000 }
1001
1002 t = ipt_get_target(e);
1003 if (copy_to_user(userptr + off + e->target_offset
1004 + offsetof(struct ipt_entry_target,
1005 u.user.name),
1006 t->u.kernel.target->name,
1007 strlen(t->u.kernel.target->name)+1) != 0) {
1008 ret = -EFAULT;
1009 goto free_counters;
1010 }
1011 }
1012
1013 free_counters:
1014 vfree(counters);
1015 return ret;
1016}
1017
2722971c 1018#ifdef CONFIG_COMPAT
9fa492cd 1019static void compat_standard_from_user(void *dst, void *src)
2722971c 1020{
9fa492cd 1021 int v = *(compat_int_t *)src;
2722971c 1022
9fa492cd 1023 if (v > 0)
b386d9f5 1024 v += xt_compat_calc_jump(AF_INET, v);
9fa492cd
PM
1025 memcpy(dst, &v, sizeof(v));
1026}
46c5ea3c 1027
9fa492cd 1028static int compat_standard_to_user(void __user *dst, void *src)
2722971c 1029{
9fa492cd 1030 compat_int_t cv = *(int *)src;
2722971c 1031
9fa492cd 1032 if (cv > 0)
b386d9f5 1033 cv -= xt_compat_calc_jump(AF_INET, cv);
9fa492cd 1034 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
2722971c
DM
1035}
1036
1037static inline int
4b478248 1038compat_calc_match(struct ipt_entry_match *m, int *size)
2722971c 1039{
9fa492cd 1040 *size += xt_compat_match_offset(m->u.kernel.match);
2722971c
DM
1041 return 0;
1042}
1043
259d4e41 1044static int compat_calc_entry(struct ipt_entry *e,
4b478248
PM
1045 const struct xt_table_info *info,
1046 void *base, struct xt_table_info *newinfo)
2722971c
DM
1047{
1048 struct ipt_entry_target *t;
e5b5ef7d 1049 unsigned int entry_offset;
2722971c
DM
1050 int off, i, ret;
1051
30c08c41 1052 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
2722971c
DM
1053 entry_offset = (void *)e - base;
1054 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
1055 t = ipt_get_target(e);
9fa492cd 1056 off += xt_compat_target_offset(t->u.kernel.target);
2722971c 1057 newinfo->size -= off;
b386d9f5 1058 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
2722971c
DM
1059 if (ret)
1060 return ret;
1061
6e23ae2a 1062 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
4b478248
PM
1063 if (info->hook_entry[i] &&
1064 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
2722971c 1065 newinfo->hook_entry[i] -= off;
4b478248
PM
1066 if (info->underflow[i] &&
1067 (e < (struct ipt_entry *)(base + info->underflow[i])))
2722971c
DM
1068 newinfo->underflow[i] -= off;
1069 }
1070 return 0;
1071}
1072
259d4e41 1073static int compat_table_info(const struct xt_table_info *info,
4b478248 1074 struct xt_table_info *newinfo)
2722971c
DM
1075{
1076 void *loc_cpu_entry;
2722971c
DM
1077
1078 if (!newinfo || !info)
1079 return -EINVAL;
1080
259d4e41
ED
1081 /* we dont care about newinfo->entries[] */
1082 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1083 newinfo->initial_entries = 0;
2722971c
DM
1084 loc_cpu_entry = info->entries[raw_smp_processor_id()];
1085 return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
4b478248
PM
1086 compat_calc_entry, info, loc_cpu_entry,
1087 newinfo);
2722971c
DM
1088}
1089#endif
1090
34bd137b 1091static int get_info(struct net *net, void __user *user, int *len, int compat)
2722971c
DM
1092{
1093 char name[IPT_TABLE_MAXNAMELEN];
e60a13e0 1094 struct xt_table *t;
2722971c
DM
1095 int ret;
1096
1097 if (*len != sizeof(struct ipt_getinfo)) {
c9d8fe13
PM
1098 duprintf("length %u != %zu\n", *len,
1099 sizeof(struct ipt_getinfo));
2722971c
DM
1100 return -EINVAL;
1101 }
1102
1103 if (copy_from_user(name, user, sizeof(name)) != 0)
1104 return -EFAULT;
1105
1106 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1107#ifdef CONFIG_COMPAT
1108 if (compat)
1109 xt_compat_lock(AF_INET);
1110#endif
34bd137b 1111 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
4b478248 1112 "iptable_%s", name);
2722971c
DM
1113 if (t && !IS_ERR(t)) {
1114 struct ipt_getinfo info;
5452e425 1115 const struct xt_table_info *private = t->private;
2722971c
DM
1116
1117#ifdef CONFIG_COMPAT
1118 if (compat) {
1119 struct xt_table_info tmp;
1120 ret = compat_table_info(private, &tmp);
b386d9f5 1121 xt_compat_flush_offsets(AF_INET);
4b478248 1122 private = &tmp;
2722971c
DM
1123 }
1124#endif
1125 info.valid_hooks = t->valid_hooks;
1126 memcpy(info.hook_entry, private->hook_entry,
4b478248 1127 sizeof(info.hook_entry));
2722971c 1128 memcpy(info.underflow, private->underflow,
4b478248 1129 sizeof(info.underflow));
2722971c
DM
1130 info.num_entries = private->number;
1131 info.size = private->size;
1132 strcpy(info.name, name);
1133
1134 if (copy_to_user(user, &info, *len) != 0)
1135 ret = -EFAULT;
1136 else
1137 ret = 0;
1138
1139 xt_table_unlock(t);
1140 module_put(t->me);
1141 } else
1142 ret = t ? PTR_ERR(t) : -ENOENT;
1143#ifdef CONFIG_COMPAT
1144 if (compat)
1145 xt_compat_unlock(AF_INET);
1146#endif
1147 return ret;
1148}
1149
1150static int
34bd137b 1151get_entries(struct net *net, struct ipt_get_entries __user *uptr, int *len)
2722971c
DM
1152{
1153 int ret;
1154 struct ipt_get_entries get;
e60a13e0 1155 struct xt_table *t;
2722971c
DM
1156
1157 if (*len < sizeof(get)) {
c9d8fe13 1158 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
2722971c
DM
1159 return -EINVAL;
1160 }
1161 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1162 return -EFAULT;
1163 if (*len != sizeof(struct ipt_get_entries) + get.size) {
c9d8fe13
PM
1164 duprintf("get_entries: %u != %zu\n",
1165 *len, sizeof(get) + get.size);
2722971c
DM
1166 return -EINVAL;
1167 }
1168
34bd137b 1169 t = xt_find_table_lock(net, AF_INET, get.name);
2722971c 1170 if (t && !IS_ERR(t)) {
5452e425 1171 const struct xt_table_info *private = t->private;
9c547959 1172 duprintf("t->private->number = %u\n", private->number);
2722971c
DM
1173 if (get.size == private->size)
1174 ret = copy_entries_to_user(private->size,
1175 t, uptr->entrytable);
1176 else {
1177 duprintf("get_entries: I've got %u not %u!\n",
9c547959 1178 private->size, get.size);
544473c1 1179 ret = -EAGAIN;
2722971c
DM
1180 }
1181 module_put(t->me);
1182 xt_table_unlock(t);
1183 } else
1184 ret = t ? PTR_ERR(t) : -ENOENT;
1185
1186 return ret;
1187}
1188
1189static int
34bd137b 1190__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
4b478248
PM
1191 struct xt_table_info *newinfo, unsigned int num_counters,
1192 void __user *counters_ptr)
2722971c
DM
1193{
1194 int ret;
e60a13e0 1195 struct xt_table *t;
2722971c
DM
1196 struct xt_table_info *oldinfo;
1197 struct xt_counters *counters;
1198 void *loc_cpu_old_entry;
1199
1200 ret = 0;
1201 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1202 if (!counters) {
1203 ret = -ENOMEM;
1204 goto out;
1205 }
1206
34bd137b 1207 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
2722971c
DM
1208 "iptable_%s", name);
1209 if (!t || IS_ERR(t)) {
1210 ret = t ? PTR_ERR(t) : -ENOENT;
1211 goto free_newinfo_counters_untrans;
1212 }
1213
1214 /* You lied! */
1215 if (valid_hooks != t->valid_hooks) {
1216 duprintf("Valid hook crap: %08X vs %08X\n",
1217 valid_hooks, t->valid_hooks);
1218 ret = -EINVAL;
1219 goto put_module;
1220 }
1221
1222 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1223 if (!oldinfo)
1224 goto put_module;
1225
1226 /* Update module usage count based on number of rules */
1227 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1228 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1229 if ((oldinfo->number > oldinfo->initial_entries) ||
1230 (newinfo->number <= oldinfo->initial_entries))
1231 module_put(t->me);
1232 if ((oldinfo->number > oldinfo->initial_entries) &&
1233 (newinfo->number <= oldinfo->initial_entries))
1234 module_put(t->me);
1235
1236 /* Get the old counters. */
1237 get_counters(oldinfo, counters);
1238 /* Decrease module usage counts and free resource */
1239 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
4b478248
PM
1240 IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1241 NULL);
2722971c
DM
1242 xt_free_table_info(oldinfo);
1243 if (copy_to_user(counters_ptr, counters,
1244 sizeof(struct xt_counters) * num_counters) != 0)
1245 ret = -EFAULT;
1246 vfree(counters);
1247 xt_table_unlock(t);
1248 return ret;
1249
1250 put_module:
1251 module_put(t->me);
1252 xt_table_unlock(t);
1253 free_newinfo_counters_untrans:
1254 vfree(counters);
1255 out:
1256 return ret;
1257}
1258
1259static int
34bd137b 1260do_replace(struct net *net, void __user *user, unsigned int len)
2722971c
DM
1261{
1262 int ret;
1263 struct ipt_replace tmp;
1264 struct xt_table_info *newinfo;
1265 void *loc_cpu_entry;
1266
1267 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1268 return -EFAULT;
1269
2722971c 1270 /* overflow check */
2722971c
DM
1271 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1272 return -ENOMEM;
1273
1274 newinfo = xt_alloc_table_info(tmp.size);
1275 if (!newinfo)
1276 return -ENOMEM;
1277
9c547959 1278 /* choose the copy that is on our node/cpu */
2722971c
DM
1279 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1280 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1281 tmp.size) != 0) {
1282 ret = -EFAULT;
1283 goto free_newinfo;
1284 }
1285
1286 ret = translate_table(tmp.name, tmp.valid_hooks,
1287 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
1288 tmp.hook_entry, tmp.underflow);
1289 if (ret != 0)
1290 goto free_newinfo;
1291
1292 duprintf("ip_tables: Translated table\n");
1293
34bd137b 1294 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
4b478248 1295 tmp.num_counters, tmp.counters);
2722971c
DM
1296 if (ret)
1297 goto free_newinfo_untrans;
1298 return 0;
1299
1300 free_newinfo_untrans:
9c547959 1301 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
2722971c
DM
1302 free_newinfo:
1303 xt_free_table_info(newinfo);
1304 return ret;
1305}
1306
1307/* We're lazy, and add to the first CPU; overflow works its fey magic
1308 * and everything is OK. */
022748a9 1309static int
2722971c
DM
1310add_counter_to_entry(struct ipt_entry *e,
1311 const struct xt_counters addme[],
1312 unsigned int *i)
1313{
1314#if 0
1315 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1316 *i,
1317 (long unsigned int)e->counters.pcnt,
1318 (long unsigned int)e->counters.bcnt,
1319 (long unsigned int)addme[*i].pcnt,
1320 (long unsigned int)addme[*i].bcnt);
1321#endif
1322
1323 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1324
1325 (*i)++;
1326 return 0;
1327}
1328
1329static int
34bd137b 1330do_add_counters(struct net *net, void __user *user, unsigned int len, int compat)
2722971c
DM
1331{
1332 unsigned int i;
1333 struct xt_counters_info tmp;
1334 struct xt_counters *paddc;
1335 unsigned int num_counters;
5452e425 1336 const char *name;
2722971c
DM
1337 int size;
1338 void *ptmp;
e60a13e0 1339 struct xt_table *t;
5452e425 1340 const struct xt_table_info *private;
2722971c
DM
1341 int ret = 0;
1342 void *loc_cpu_entry;
1343#ifdef CONFIG_COMPAT
1344 struct compat_xt_counters_info compat_tmp;
1345
1346 if (compat) {
1347 ptmp = &compat_tmp;
1348 size = sizeof(struct compat_xt_counters_info);
1349 } else
1350#endif
1351 {
1352 ptmp = &tmp;
1353 size = sizeof(struct xt_counters_info);
1354 }
1355
1356 if (copy_from_user(ptmp, user, size) != 0)
1357 return -EFAULT;
1358
1359#ifdef CONFIG_COMPAT
1360 if (compat) {
1361 num_counters = compat_tmp.num_counters;
1362 name = compat_tmp.name;
1363 } else
1364#endif
1365 {
1366 num_counters = tmp.num_counters;
1367 name = tmp.name;
1368 }
1369
1370 if (len != size + num_counters * sizeof(struct xt_counters))
1371 return -EINVAL;
1372
1373 paddc = vmalloc_node(len - size, numa_node_id());
1374 if (!paddc)
1375 return -ENOMEM;
1376
1377 if (copy_from_user(paddc, user + size, len - size) != 0) {
1378 ret = -EFAULT;
1379 goto free;
1380 }
1381
34bd137b 1382 t = xt_find_table_lock(net, AF_INET, name);
2722971c
DM
1383 if (!t || IS_ERR(t)) {
1384 ret = t ? PTR_ERR(t) : -ENOENT;
1385 goto free;
1386 }
1387
1388 write_lock_bh(&t->lock);
1389 private = t->private;
1390 if (private->number != num_counters) {
1391 ret = -EINVAL;
1392 goto unlock_up_free;
1393 }
1394
1395 i = 0;
1396 /* Choose the copy that is on our node */
1397 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1398 IPT_ENTRY_ITERATE(loc_cpu_entry,
1399 private->size,
1400 add_counter_to_entry,
1401 paddc,
1402 &i);
1403 unlock_up_free:
1404 write_unlock_bh(&t->lock);
1405 xt_table_unlock(t);
1406 module_put(t->me);
1407 free:
1408 vfree(paddc);
1409
1410 return ret;
1411}
1412
1413#ifdef CONFIG_COMPAT
1414struct compat_ipt_replace {
1415 char name[IPT_TABLE_MAXNAMELEN];
1416 u32 valid_hooks;
1417 u32 num_entries;
1418 u32 size;
6e23ae2a
PM
1419 u32 hook_entry[NF_INET_NUMHOOKS];
1420 u32 underflow[NF_INET_NUMHOOKS];
2722971c
DM
1421 u32 num_counters;
1422 compat_uptr_t counters; /* struct ipt_counters * */
1423 struct compat_ipt_entry entries[0];
1424};
1425
a18aa31b
PM
1426static int
1427compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
b0a6363c 1428 unsigned int *size, struct xt_counters *counters,
a18aa31b 1429 unsigned int *i)
2722971c 1430{
3e597c60 1431 struct ipt_entry_target *t;
2722971c
DM
1432 struct compat_ipt_entry __user *ce;
1433 u_int16_t target_offset, next_offset;
1434 compat_uint_t origsize;
1435 int ret;
1436
1437 ret = -EFAULT;
1438 origsize = *size;
1439 ce = (struct compat_ipt_entry __user *)*dstptr;
7800007c 1440 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
2722971c
DM
1441 goto out;
1442
a18aa31b
PM
1443 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1444 goto out;
1445
2722971c 1446 *dstptr += sizeof(struct compat_ipt_entry);
30c08c41
PM
1447 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1448
ac8e27fd 1449 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
2722971c
DM
1450 target_offset = e->target_offset - (origsize - *size);
1451 if (ret)
1452 goto out;
1453 t = ipt_get_target(e);
9fa492cd 1454 ret = xt_compat_target_to_user(t, dstptr, size);
2722971c
DM
1455 if (ret)
1456 goto out;
1457 ret = -EFAULT;
1458 next_offset = e->next_offset - (origsize - *size);
7800007c 1459 if (put_user(target_offset, &ce->target_offset))
2722971c 1460 goto out;
7800007c 1461 if (put_user(next_offset, &ce->next_offset))
2722971c 1462 goto out;
a18aa31b
PM
1463
1464 (*i)++;
2722971c
DM
1465 return 0;
1466out:
1467 return ret;
1468}
1469
022748a9 1470static int
4c1b52bc 1471compat_find_calc_match(struct ipt_entry_match *m,
4b478248
PM
1472 const char *name,
1473 const struct ipt_ip *ip,
1474 unsigned int hookmask,
b0a6363c 1475 int *size, unsigned int *i)
2722971c 1476{
6709dbbb 1477 struct xt_match *match;
2722971c
DM
1478
1479 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
4b478248 1480 m->u.user.revision),
2722971c
DM
1481 "ipt_%s", m->u.user.name);
1482 if (IS_ERR(match) || !match) {
1483 duprintf("compat_check_calc_match: `%s' not found\n",
4b478248 1484 m->u.user.name);
2722971c
DM
1485 return match ? PTR_ERR(match) : -ENOENT;
1486 }
1487 m->u.kernel.match = match;
9fa492cd 1488 *size += xt_compat_match_offset(match);
2722971c
DM
1489
1490 (*i)++;
1491 return 0;
1492}
1493
022748a9 1494static int
4c1b52bc
DM
1495compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1496{
1497 if (i && (*i)-- == 0)
1498 return 1;
1499
1500 module_put(m->u.kernel.match->me);
1501 return 0;
1502}
1503
022748a9 1504static int
73cd598d 1505compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
4c1b52bc
DM
1506{
1507 struct ipt_entry_target *t;
1508
1509 if (i && (*i)-- == 0)
1510 return 1;
1511
1512 /* Cleanup all matches */
73cd598d
PM
1513 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1514 t = compat_ipt_get_target(e);
4c1b52bc
DM
1515 module_put(t->u.kernel.target->me);
1516 return 0;
1517}
1518
022748a9 1519static int
73cd598d 1520check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
4b478248
PM
1521 struct xt_table_info *newinfo,
1522 unsigned int *size,
1523 unsigned char *base,
1524 unsigned char *limit,
1525 unsigned int *hook_entries,
1526 unsigned int *underflows,
1527 unsigned int *i,
1528 const char *name)
2722971c
DM
1529{
1530 struct ipt_entry_target *t;
6709dbbb 1531 struct xt_target *target;
e5b5ef7d 1532 unsigned int entry_offset;
b0a6363c
PM
1533 unsigned int j;
1534 int ret, off, h;
2722971c
DM
1535
1536 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1537 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0
1538 || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1539 duprintf("Bad offset %p, limit = %p\n", e, limit);
1540 return -EINVAL;
1541 }
1542
1543 if (e->next_offset < sizeof(struct compat_ipt_entry) +
4b478248 1544 sizeof(struct compat_xt_entry_target)) {
2722971c
DM
1545 duprintf("checking: element %p size %u\n",
1546 e, e->next_offset);
1547 return -EINVAL;
1548 }
1549
73cd598d
PM
1550 /* For purposes of check_entry casting the compat entry is fine */
1551 ret = check_entry((struct ipt_entry *)e, name);
a96be246
DM
1552 if (ret)
1553 return ret;
590bdf7f 1554
30c08c41 1555 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
2722971c
DM
1556 entry_offset = (void *)e - (void *)base;
1557 j = 0;
73cd598d
PM
1558 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1559 &e->ip, e->comefrom, &off, &j);
2722971c 1560 if (ret != 0)
4c1b52bc 1561 goto release_matches;
2722971c 1562
73cd598d 1563 t = compat_ipt_get_target(e);
2722971c 1564 target = try_then_request_module(xt_find_target(AF_INET,
4b478248
PM
1565 t->u.user.name,
1566 t->u.user.revision),
2722971c
DM
1567 "ipt_%s", t->u.user.name);
1568 if (IS_ERR(target) || !target) {
a96be246 1569 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
4b478248 1570 t->u.user.name);
2722971c 1571 ret = target ? PTR_ERR(target) : -ENOENT;
4c1b52bc 1572 goto release_matches;
2722971c
DM
1573 }
1574 t->u.kernel.target = target;
1575
9fa492cd 1576 off += xt_compat_target_offset(target);
2722971c 1577 *size += off;
b386d9f5 1578 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
2722971c
DM
1579 if (ret)
1580 goto out;
1581
1582 /* Check hooks & underflows */
6e23ae2a 1583 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
2722971c
DM
1584 if ((unsigned char *)e - base == hook_entries[h])
1585 newinfo->hook_entry[h] = hook_entries[h];
1586 if ((unsigned char *)e - base == underflows[h])
1587 newinfo->underflow[h] = underflows[h];
1588 }
1589
1590 /* Clear counters and comefrom */
73cd598d 1591 memset(&e->counters, 0, sizeof(e->counters));
2722971c
DM
1592 e->comefrom = 0;
1593
1594 (*i)++;
1595 return 0;
bec71b16 1596
2722971c 1597out:
bec71b16 1598 module_put(t->u.kernel.target->me);
4c1b52bc
DM
1599release_matches:
1600 IPT_MATCH_ITERATE(e, compat_release_match, &j);
2722971c
DM
1601 return ret;
1602}
1603
4b478248 1604static int
73cd598d 1605compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
4b478248
PM
1606 unsigned int *size, const char *name,
1607 struct xt_table_info *newinfo, unsigned char *base)
2722971c
DM
1608{
1609 struct ipt_entry_target *t;
6709dbbb 1610 struct xt_target *target;
2722971c
DM
1611 struct ipt_entry *de;
1612 unsigned int origsize;
920b868a 1613 int ret, h;
2722971c
DM
1614
1615 ret = 0;
1616 origsize = *size;
1617 de = (struct ipt_entry *)*dstptr;
1618 memcpy(de, e, sizeof(struct ipt_entry));
73cd598d 1619 memcpy(&de->counters, &e->counters, sizeof(e->counters));
2722971c 1620
73cd598d 1621 *dstptr += sizeof(struct ipt_entry);
30c08c41
PM
1622 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1623
73cd598d
PM
1624 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1625 dstptr, size);
2722971c 1626 if (ret)
f6677f43 1627 return ret;
2722971c 1628 de->target_offset = e->target_offset - (origsize - *size);
73cd598d 1629 t = compat_ipt_get_target(e);
2722971c 1630 target = t->u.kernel.target;
9fa492cd 1631 xt_compat_target_from_user(t, dstptr, size);
2722971c
DM
1632
1633 de->next_offset = e->next_offset - (origsize - *size);
6e23ae2a 1634 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
2722971c
DM
1635 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1636 newinfo->hook_entry[h] -= origsize - *size;
1637 if ((unsigned char *)de - base < newinfo->underflow[h])
1638 newinfo->underflow[h] -= origsize - *size;
1639 }
f6677f43
DM
1640 return ret;
1641}
1642
022748a9
DV
1643static int
1644compat_check_entry(struct ipt_entry *e, const char *name,
4b478248 1645 unsigned int *i)
f6677f43 1646{
b0a6363c
PM
1647 unsigned int j;
1648 int ret;
f6677f43 1649
4c1b52bc 1650 j = 0;
9c547959
PM
1651 ret = IPT_MATCH_ITERATE(e, check_match, name, &e->ip,
1652 e->comefrom, &j);
f6677f43 1653 if (ret)
4c1b52bc
DM
1654 goto cleanup_matches;
1655
1656 ret = check_target(e, name);
1657 if (ret)
1658 goto cleanup_matches;
f6677f43 1659
4c1b52bc
DM
1660 (*i)++;
1661 return 0;
1662
1663 cleanup_matches:
1664 IPT_MATCH_ITERATE(e, cleanup_match, &j);
1665 return ret;
f6677f43
DM
1666}
1667
1da177e4 1668static int
2722971c 1669translate_compat_table(const char *name,
4b478248
PM
1670 unsigned int valid_hooks,
1671 struct xt_table_info **pinfo,
1672 void **pentry0,
1673 unsigned int total_size,
1674 unsigned int number,
1675 unsigned int *hook_entries,
1676 unsigned int *underflows)
1da177e4 1677{
920b868a 1678 unsigned int i, j;
2722971c
DM
1679 struct xt_table_info *newinfo, *info;
1680 void *pos, *entry0, *entry1;
1681 unsigned int size;
1da177e4 1682 int ret;
1da177e4 1683
2722971c
DM
1684 info = *pinfo;
1685 entry0 = *pentry0;
1686 size = total_size;
1687 info->number = number;
1688
1689 /* Init all hooks to impossible value. */
6e23ae2a 1690 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
2722971c
DM
1691 info->hook_entry[i] = 0xFFFFFFFF;
1692 info->underflow[i] = 0xFFFFFFFF;
1693 }
1694
1695 duprintf("translate_compat_table: size %u\n", info->size);
920b868a 1696 j = 0;
2722971c
DM
1697 xt_compat_lock(AF_INET);
1698 /* Walk through entries, checking offsets. */
73cd598d
PM
1699 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1700 check_compat_entry_size_and_hooks,
1701 info, &size, entry0,
1702 entry0 + total_size,
1703 hook_entries, underflows, &j, name);
2722971c
DM
1704 if (ret != 0)
1705 goto out_unlock;
1706
1707 ret = -EINVAL;
920b868a 1708 if (j != number) {
2722971c 1709 duprintf("translate_compat_table: %u not %u entries\n",
920b868a 1710 j, number);
2722971c
DM
1711 goto out_unlock;
1712 }
1713
1714 /* Check hooks all assigned */
6e23ae2a 1715 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
2722971c
DM
1716 /* Only hooks which are valid */
1717 if (!(valid_hooks & (1 << i)))
1718 continue;
1719 if (info->hook_entry[i] == 0xFFFFFFFF) {
1720 duprintf("Invalid hook entry %u %u\n",
1721 i, hook_entries[i]);
1722 goto out_unlock;
1da177e4 1723 }
2722971c
DM
1724 if (info->underflow[i] == 0xFFFFFFFF) {
1725 duprintf("Invalid underflow %u %u\n",
1726 i, underflows[i]);
1727 goto out_unlock;
1728 }
1729 }
1730
1731 ret = -ENOMEM;
1732 newinfo = xt_alloc_table_info(size);
1733 if (!newinfo)
1734 goto out_unlock;
1735
1736 newinfo->number = number;
6e23ae2a 1737 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
2722971c
DM
1738 newinfo->hook_entry[i] = info->hook_entry[i];
1739 newinfo->underflow[i] = info->underflow[i];
1740 }
1741 entry1 = newinfo->entries[raw_smp_processor_id()];
1742 pos = entry1;
4b478248 1743 size = total_size;
73cd598d 1744 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
9c547959
PM
1745 compat_copy_entry_from_user,
1746 &pos, &size, name, newinfo, entry1);
b386d9f5 1747 xt_compat_flush_offsets(AF_INET);
2722971c
DM
1748 xt_compat_unlock(AF_INET);
1749 if (ret)
1750 goto free_newinfo;
1751
1752 ret = -ELOOP;
1753 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1754 goto free_newinfo;
1755
4c1b52bc 1756 i = 0;
f6677f43 1757 ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
4b478248 1758 name, &i);
4c1b52bc
DM
1759 if (ret) {
1760 j -= i;
73cd598d
PM
1761 COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1762 compat_release_entry, &j);
4c1b52bc
DM
1763 IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1764 xt_free_table_info(newinfo);
1765 return ret;
1766 }
f6677f43 1767
2722971c 1768 /* And one copy for every other CPU */
fb1bb34d 1769 for_each_possible_cpu(i)
2722971c
DM
1770 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1771 memcpy(newinfo->entries[i], entry1, newinfo->size);
1772
1773 *pinfo = newinfo;
1774 *pentry0 = entry1;
1775 xt_free_table_info(info);
1776 return 0;
1da177e4 1777
2722971c
DM
1778free_newinfo:
1779 xt_free_table_info(newinfo);
1780out:
73cd598d 1781 COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
1da177e4 1782 return ret;
2722971c 1783out_unlock:
b386d9f5 1784 xt_compat_flush_offsets(AF_INET);
2722971c
DM
1785 xt_compat_unlock(AF_INET);
1786 goto out;
1da177e4
LT
1787}
1788
1789static int
34bd137b 1790compat_do_replace(struct net *net, void __user *user, unsigned int len)
1da177e4
LT
1791{
1792 int ret;
2722971c
DM
1793 struct compat_ipt_replace tmp;
1794 struct xt_table_info *newinfo;
1795 void *loc_cpu_entry;
1da177e4
LT
1796
1797 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1798 return -EFAULT;
1799
ee4bb818 1800 /* overflow check */
259d4e41 1801 if (tmp.size >= INT_MAX / num_possible_cpus())
ee4bb818
KK
1802 return -ENOMEM;
1803 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1804 return -ENOMEM;
1805
2e4e6a17 1806 newinfo = xt_alloc_table_info(tmp.size);
1da177e4
LT
1807 if (!newinfo)
1808 return -ENOMEM;
1809
9c547959 1810 /* choose the copy that is on our node/cpu */
31836064
ED
1811 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1812 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1da177e4
LT
1813 tmp.size) != 0) {
1814 ret = -EFAULT;
1815 goto free_newinfo;
1816 }
1817
2722971c 1818 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
4b478248
PM
1819 &newinfo, &loc_cpu_entry, tmp.size,
1820 tmp.num_entries, tmp.hook_entry,
1821 tmp.underflow);
2722971c 1822 if (ret != 0)
1da177e4 1823 goto free_newinfo;
1da177e4 1824
2722971c 1825 duprintf("compat_do_replace: Translated table\n");
1da177e4 1826
34bd137b 1827 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
4b478248 1828 tmp.num_counters, compat_ptr(tmp.counters));
2722971c
DM
1829 if (ret)
1830 goto free_newinfo_untrans;
1831 return 0;
1da177e4 1832
2722971c 1833 free_newinfo_untrans:
4b478248 1834 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
2722971c
DM
1835 free_newinfo:
1836 xt_free_table_info(newinfo);
1837 return ret;
1838}
1da177e4 1839
2722971c
DM
1840static int
1841compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
4b478248 1842 unsigned int len)
2722971c
DM
1843{
1844 int ret;
1da177e4 1845
2722971c
DM
1846 if (!capable(CAP_NET_ADMIN))
1847 return -EPERM;
1da177e4 1848
2722971c
DM
1849 switch (cmd) {
1850 case IPT_SO_SET_REPLACE:
3b1e0a65 1851 ret = compat_do_replace(sock_net(sk), user, len);
2722971c 1852 break;
1da177e4 1853
2722971c 1854 case IPT_SO_SET_ADD_COUNTERS:
3b1e0a65 1855 ret = do_add_counters(sock_net(sk), user, len, 1);
2722971c
DM
1856 break;
1857
1858 default:
1859 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1860 ret = -EINVAL;
1861 }
1da177e4 1862
1da177e4
LT
1863 return ret;
1864}
1865
4b478248 1866struct compat_ipt_get_entries {
2722971c
DM
1867 char name[IPT_TABLE_MAXNAMELEN];
1868 compat_uint_t size;
1869 struct compat_ipt_entry entrytable[0];
1870};
1da177e4 1871
4b478248
PM
1872static int
1873compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1874 void __user *userptr)
2722971c 1875{
2722971c 1876 struct xt_counters *counters;
5452e425 1877 const struct xt_table_info *private = table->private;
2722971c
DM
1878 void __user *pos;
1879 unsigned int size;
1880 int ret = 0;
5452e425 1881 const void *loc_cpu_entry;
a18aa31b 1882 unsigned int i = 0;
1da177e4 1883
2722971c
DM
1884 counters = alloc_counters(table);
1885 if (IS_ERR(counters))
1886 return PTR_ERR(counters);
1887
1888 /* choose the copy that is on our node/cpu, ...
1889 * This choice is lazy (because current thread is
1890 * allowed to migrate to another cpu)
1891 */
1892 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1893 pos = userptr;
1894 size = total_size;
1895 ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
a18aa31b
PM
1896 compat_copy_entry_to_user,
1897 &pos, &size, counters, &i);
2722971c 1898
2722971c
DM
1899 vfree(counters);
1900 return ret;
1da177e4
LT
1901}
1902
1903static int
34bd137b
AD
1904compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1905 int *len)
1da177e4 1906{
2722971c
DM
1907 int ret;
1908 struct compat_ipt_get_entries get;
e60a13e0 1909 struct xt_table *t;
1da177e4 1910
2722971c 1911 if (*len < sizeof(get)) {
c9d8fe13 1912 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1da177e4 1913 return -EINVAL;
2722971c 1914 }
1da177e4 1915
2722971c
DM
1916 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1917 return -EFAULT;
1da177e4 1918
2722971c 1919 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
c9d8fe13
PM
1920 duprintf("compat_get_entries: %u != %zu\n",
1921 *len, sizeof(get) + get.size);
2722971c 1922 return -EINVAL;
1da177e4
LT
1923 }
1924
2722971c 1925 xt_compat_lock(AF_INET);
34bd137b 1926 t = xt_find_table_lock(net, AF_INET, get.name);
2722971c 1927 if (t && !IS_ERR(t)) {
5452e425 1928 const struct xt_table_info *private = t->private;
2722971c 1929 struct xt_table_info info;
9c547959 1930 duprintf("t->private->number = %u\n", private->number);
2722971c
DM
1931 ret = compat_table_info(private, &info);
1932 if (!ret && get.size == info.size) {
1933 ret = compat_copy_entries_to_user(private->size,
4b478248 1934 t, uptr->entrytable);
2722971c
DM
1935 } else if (!ret) {
1936 duprintf("compat_get_entries: I've got %u not %u!\n",
9c547959 1937 private->size, get.size);
544473c1 1938 ret = -EAGAIN;
2722971c 1939 }
b386d9f5 1940 xt_compat_flush_offsets(AF_INET);
2722971c
DM
1941 module_put(t->me);
1942 xt_table_unlock(t);
1943 } else
1da177e4 1944 ret = t ? PTR_ERR(t) : -ENOENT;
1da177e4 1945
2722971c
DM
1946 xt_compat_unlock(AF_INET);
1947 return ret;
1948}
1da177e4 1949
79030ed0
PM
1950static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1951
2722971c
DM
1952static int
1953compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1954{
1955 int ret;
1da177e4 1956
82fac054
BS
1957 if (!capable(CAP_NET_ADMIN))
1958 return -EPERM;
1959
2722971c
DM
1960 switch (cmd) {
1961 case IPT_SO_GET_INFO:
3b1e0a65 1962 ret = get_info(sock_net(sk), user, len, 1);
2722971c
DM
1963 break;
1964 case IPT_SO_GET_ENTRIES:
3b1e0a65 1965 ret = compat_get_entries(sock_net(sk), user, len);
2722971c
DM
1966 break;
1967 default:
79030ed0 1968 ret = do_ipt_get_ctl(sk, cmd, user, len);
2722971c 1969 }
1da177e4
LT
1970 return ret;
1971}
2722971c 1972#endif
1da177e4
LT
1973
1974static int
9c547959 1975do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1da177e4
LT
1976{
1977 int ret;
1978
1979 if (!capable(CAP_NET_ADMIN))
1980 return -EPERM;
1981
1982 switch (cmd) {
1983 case IPT_SO_SET_REPLACE:
3b1e0a65 1984 ret = do_replace(sock_net(sk), user, len);
1da177e4
LT
1985 break;
1986
1987 case IPT_SO_SET_ADD_COUNTERS:
3b1e0a65 1988 ret = do_add_counters(sock_net(sk), user, len, 0);
1da177e4
LT
1989 break;
1990
1991 default:
1992 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1993 ret = -EINVAL;
1994 }
1995
1996 return ret;
1997}
1998
1999static int
2000do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2001{
2002 int ret;
2003
2004 if (!capable(CAP_NET_ADMIN))
2005 return -EPERM;
2006
2007 switch (cmd) {
2722971c 2008 case IPT_SO_GET_INFO:
3b1e0a65 2009 ret = get_info(sock_net(sk), user, len, 0);
2722971c 2010 break;
1da177e4 2011
2722971c 2012 case IPT_SO_GET_ENTRIES:
3b1e0a65 2013 ret = get_entries(sock_net(sk), user, len);
1da177e4 2014 break;
1da177e4
LT
2015
2016 case IPT_SO_GET_REVISION_MATCH:
2017 case IPT_SO_GET_REVISION_TARGET: {
2018 struct ipt_get_revision rev;
2e4e6a17 2019 int target;
1da177e4
LT
2020
2021 if (*len != sizeof(rev)) {
2022 ret = -EINVAL;
2023 break;
2024 }
2025 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2026 ret = -EFAULT;
2027 break;
2028 }
2029
2030 if (cmd == IPT_SO_GET_REVISION_TARGET)
2e4e6a17 2031 target = 1;
1da177e4 2032 else
2e4e6a17 2033 target = 0;
1da177e4 2034
2e4e6a17
HW
2035 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2036 rev.revision,
2037 target, &ret),
1da177e4
LT
2038 "ipt_%s", rev.name);
2039 break;
2040 }
2041
2042 default:
2043 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2044 ret = -EINVAL;
2045 }
2046
2047 return ret;
2048}
2049
44d34e72
AD
2050struct xt_table *ipt_register_table(struct net *net, struct xt_table *table,
2051 const struct ipt_replace *repl)
1da177e4
LT
2052{
2053 int ret;
2e4e6a17 2054 struct xt_table_info *newinfo;
259d4e41 2055 struct xt_table_info bootstrap
1da177e4 2056 = { 0, 0, 0, { 0 }, { 0 }, { } };
31836064 2057 void *loc_cpu_entry;
a98da11d 2058 struct xt_table *new_table;
1da177e4 2059
2e4e6a17 2060 newinfo = xt_alloc_table_info(repl->size);
44d34e72
AD
2061 if (!newinfo) {
2062 ret = -ENOMEM;
2063 goto out;
2064 }
1da177e4 2065
9c547959 2066 /* choose the copy on our node/cpu, but dont care about preemption */
31836064
ED
2067 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2068 memcpy(loc_cpu_entry, repl->entries, repl->size);
1da177e4
LT
2069
2070 ret = translate_table(table->name, table->valid_hooks,
31836064 2071 newinfo, loc_cpu_entry, repl->size,
1da177e4
LT
2072 repl->num_entries,
2073 repl->hook_entry,
2074 repl->underflow);
44d34e72
AD
2075 if (ret != 0)
2076 goto out_free;
1da177e4 2077
44d34e72 2078 new_table = xt_register_table(net, table, &bootstrap, newinfo);
a98da11d 2079 if (IS_ERR(new_table)) {
44d34e72
AD
2080 ret = PTR_ERR(new_table);
2081 goto out_free;
1da177e4
LT
2082 }
2083
44d34e72
AD
2084 return new_table;
2085
2086out_free:
2087 xt_free_table_info(newinfo);
2088out:
2089 return ERR_PTR(ret);
1da177e4
LT
2090}
2091
e60a13e0 2092void ipt_unregister_table(struct xt_table *table)
1da177e4 2093{
2e4e6a17 2094 struct xt_table_info *private;
31836064 2095 void *loc_cpu_entry;
df200969 2096 struct module *table_owner = table->me;
31836064 2097
e905a9ed 2098 private = xt_unregister_table(table);
1da177e4
LT
2099
2100 /* Decrease module usage counts and free resources */
2e4e6a17
HW
2101 loc_cpu_entry = private->entries[raw_smp_processor_id()];
2102 IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
df200969
AD
2103 if (private->number > private->initial_entries)
2104 module_put(table_owner);
2e4e6a17 2105 xt_free_table_info(private);
1da177e4
LT
2106}
2107
2108/* Returns 1 if the type and code is matched by the range, 0 otherwise */
1d93a9cb 2109static inline bool
1da177e4
LT
2110icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2111 u_int8_t type, u_int8_t code,
1d93a9cb 2112 bool invert)
1da177e4 2113{
9c547959
PM
2114 return ((test_type == 0xFF) ||
2115 (type == test_type && code >= min_code && code <= max_code))
1da177e4
LT
2116 ^ invert;
2117}
2118
1d93a9cb 2119static bool
f7108a20 2120icmp_match(const struct sk_buff *skb, const struct xt_match_param *par)
1da177e4 2121{
5452e425
JE
2122 const struct icmphdr *ic;
2123 struct icmphdr _icmph;
f7108a20 2124 const struct ipt_icmp *icmpinfo = par->matchinfo;
1da177e4
LT
2125
2126 /* Must not be a fragment. */
f7108a20 2127 if (par->fragoff != 0)
1d93a9cb 2128 return false;
1da177e4 2129
f7108a20 2130 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
1da177e4
LT
2131 if (ic == NULL) {
2132 /* We've been asked to examine this packet, and we
2133 * can't. Hence, no choice but to drop.
2134 */
2135 duprintf("Dropping evil ICMP tinygram.\n");
f7108a20 2136 *par->hotdrop = true;
1d93a9cb 2137 return false;
1da177e4
LT
2138 }
2139
2140 return icmp_type_code_match(icmpinfo->type,
2141 icmpinfo->code[0],
2142 icmpinfo->code[1],
2143 ic->type, ic->code,
2144 !!(icmpinfo->invflags&IPT_ICMP_INV));
2145}
2146
2147/* Called when user tries to insert an entry of this type. */
ccb79bdc 2148static bool
1da177e4 2149icmp_checkentry(const char *tablename,
9c547959 2150 const void *entry,
c4986734 2151 const struct xt_match *match,
1da177e4 2152 void *matchinfo,
1da177e4
LT
2153 unsigned int hook_mask)
2154{
2155 const struct ipt_icmp *icmpinfo = matchinfo;
2156
1d5cd909
PM
2157 /* Must specify no unknown invflags */
2158 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
1da177e4
LT
2159}
2160
2161/* The built-in targets: standard (NULL) and error. */
9f15c530 2162static struct xt_target ipt_standard_target __read_mostly = {
1da177e4 2163 .name = IPT_STANDARD_TARGET,
1d5cd909 2164 .targetsize = sizeof(int),
a45049c5 2165 .family = AF_INET,
2722971c 2166#ifdef CONFIG_COMPAT
9fa492cd
PM
2167 .compatsize = sizeof(compat_int_t),
2168 .compat_from_user = compat_standard_from_user,
2169 .compat_to_user = compat_standard_to_user,
2722971c 2170#endif
1da177e4
LT
2171};
2172
9f15c530 2173static struct xt_target ipt_error_target __read_mostly = {
1da177e4
LT
2174 .name = IPT_ERROR_TARGET,
2175 .target = ipt_error,
1d5cd909 2176 .targetsize = IPT_FUNCTION_MAXNAMELEN,
a45049c5 2177 .family = AF_INET,
1da177e4
LT
2178};
2179
2180static struct nf_sockopt_ops ipt_sockopts = {
2181 .pf = PF_INET,
2182 .set_optmin = IPT_BASE_CTL,
2183 .set_optmax = IPT_SO_SET_MAX+1,
2184 .set = do_ipt_set_ctl,
2722971c
DM
2185#ifdef CONFIG_COMPAT
2186 .compat_set = compat_do_ipt_set_ctl,
2187#endif
1da177e4
LT
2188 .get_optmin = IPT_BASE_CTL,
2189 .get_optmax = IPT_SO_GET_MAX+1,
2190 .get = do_ipt_get_ctl,
2722971c
DM
2191#ifdef CONFIG_COMPAT
2192 .compat_get = compat_do_ipt_get_ctl,
2193#endif
16fcec35 2194 .owner = THIS_MODULE,
1da177e4
LT
2195};
2196
9f15c530 2197static struct xt_match icmp_matchstruct __read_mostly = {
1da177e4 2198 .name = "icmp",
1d5cd909
PM
2199 .match = icmp_match,
2200 .matchsize = sizeof(struct ipt_icmp),
9c547959 2201 .checkentry = icmp_checkentry,
1d5cd909 2202 .proto = IPPROTO_ICMP,
a45049c5 2203 .family = AF_INET,
1da177e4
LT
2204};
2205
3cb609d5
AD
2206static int __net_init ip_tables_net_init(struct net *net)
2207{
2208 return xt_proto_init(net, AF_INET);
2209}
2210
2211static void __net_exit ip_tables_net_exit(struct net *net)
2212{
2213 xt_proto_fini(net, AF_INET);
2214}
2215
2216static struct pernet_operations ip_tables_net_ops = {
2217 .init = ip_tables_net_init,
2218 .exit = ip_tables_net_exit,
2219};
2220
65b4b4e8 2221static int __init ip_tables_init(void)
1da177e4
LT
2222{
2223 int ret;
2224
3cb609d5 2225 ret = register_pernet_subsys(&ip_tables_net_ops);
0eff66e6
PM
2226 if (ret < 0)
2227 goto err1;
2e4e6a17 2228
1da177e4 2229 /* Noone else will be downing sem now, so we won't sleep */
0eff66e6
PM
2230 ret = xt_register_target(&ipt_standard_target);
2231 if (ret < 0)
2232 goto err2;
2233 ret = xt_register_target(&ipt_error_target);
2234 if (ret < 0)
2235 goto err3;
2236 ret = xt_register_match(&icmp_matchstruct);
2237 if (ret < 0)
2238 goto err4;
1da177e4
LT
2239
2240 /* Register setsockopt */
2241 ret = nf_register_sockopt(&ipt_sockopts);
0eff66e6
PM
2242 if (ret < 0)
2243 goto err5;
1da177e4 2244
0236e667 2245 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
1da177e4 2246 return 0;
0eff66e6
PM
2247
2248err5:
2249 xt_unregister_match(&icmp_matchstruct);
2250err4:
2251 xt_unregister_target(&ipt_error_target);
2252err3:
2253 xt_unregister_target(&ipt_standard_target);
2254err2:
3cb609d5 2255 unregister_pernet_subsys(&ip_tables_net_ops);
0eff66e6
PM
2256err1:
2257 return ret;
1da177e4
LT
2258}
2259
65b4b4e8 2260static void __exit ip_tables_fini(void)
1da177e4
LT
2261{
2262 nf_unregister_sockopt(&ipt_sockopts);
2e4e6a17 2263
a45049c5
PNA
2264 xt_unregister_match(&icmp_matchstruct);
2265 xt_unregister_target(&ipt_error_target);
2266 xt_unregister_target(&ipt_standard_target);
2e4e6a17 2267
3cb609d5 2268 unregister_pernet_subsys(&ip_tables_net_ops);
1da177e4
LT
2269}
2270
2271EXPORT_SYMBOL(ipt_register_table);
2272EXPORT_SYMBOL(ipt_unregister_table);
1da177e4 2273EXPORT_SYMBOL(ipt_do_table);
65b4b4e8
AM
2274module_init(ip_tables_init);
2275module_exit(ip_tables_fini);
This page took 0.55649 seconds and 5 git commands to generate.