blob: 07be12cc3fe35179a709506f700bb979347ca0a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Packet matching code.
3 *
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
Harald Welte2e4e6a12006-01-12 13:30:04 -08005 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/cache.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/skbuff.h>
14#include <linux/kmod.h>
15#include <linux/vmalloc.h>
16#include <linux/netdevice.h>
17#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/icmp.h>
19#include <net/ip.h>
Dmitry Mishin27229712006-04-01 02:25:19 -080020#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -080022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/proc_fs.h>
24#include <linux/err.h>
David S. Millerc8923c62005-10-13 14:41:23 -070025#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Harald Welte2e4e6a12006-01-12 13:30:04 -080027#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/netfilter_ipv4/ip_tables.h>
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
32MODULE_DESCRIPTION("IPv4 packet filter");
33
34/*#define DEBUG_IP_FIREWALL*/
35/*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging */
36/*#define DEBUG_IP_FIREWALL_USER*/
37
38#ifdef DEBUG_IP_FIREWALL
39#define dprintf(format, args...) printk(format , ## args)
40#else
41#define dprintf(format, args...)
42#endif
43
44#ifdef DEBUG_IP_FIREWALL_USER
45#define duprintf(format, args...) printk(format , ## args)
46#else
47#define duprintf(format, args...)
48#endif
49
50#ifdef CONFIG_NETFILTER_DEBUG
51#define IP_NF_ASSERT(x) \
52do { \
53 if (!(x)) \
54 printk("IP_NF_ASSERT: %s:%s:%u\n", \
55 __FUNCTION__, __FILE__, __LINE__); \
56} while(0)
57#else
58#define IP_NF_ASSERT(x)
59#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#if 0
62/* All the better to debug you with... */
63#define static
64#define inline
65#endif
66
67/*
68 We keep a set of rules for each CPU, so we can avoid write-locking
69 them in the softirq when updating the counters and therefore
70 only need to read-lock in the softirq; doing a write_lock_bh() in user
71 context stops packets coming through and allows user context to read
72 the counters or update the rules.
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 Hence the start of any table is given by get_table() below. */
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* Returns whether matches rule or not. */
77static inline int
78ip_packet_match(const struct iphdr *ip,
79 const char *indev,
80 const char *outdev,
81 const struct ipt_ip *ipinfo,
82 int isfrag)
83{
84 size_t i;
85 unsigned long ret;
86
87#define FWINV(bool,invflg) ((bool) ^ !!(ipinfo->invflags & invflg))
88
89 if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
90 IPT_INV_SRCIP)
91 || FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
92 IPT_INV_DSTIP)) {
93 dprintf("Source or dest mismatch.\n");
94
95 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
96 NIPQUAD(ip->saddr),
97 NIPQUAD(ipinfo->smsk.s_addr),
98 NIPQUAD(ipinfo->src.s_addr),
99 ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
100 dprintf("DST: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
101 NIPQUAD(ip->daddr),
102 NIPQUAD(ipinfo->dmsk.s_addr),
103 NIPQUAD(ipinfo->dst.s_addr),
104 ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
105 return 0;
106 }
107
108 /* Look for ifname matches; this should unroll nicely. */
109 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
110 ret |= (((const unsigned long *)indev)[i]
111 ^ ((const unsigned long *)ipinfo->iniface)[i])
112 & ((const unsigned long *)ipinfo->iniface_mask)[i];
113 }
114
115 if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
116 dprintf("VIA in mismatch (%s vs %s).%s\n",
117 indev, ipinfo->iniface,
118 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
119 return 0;
120 }
121
122 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
123 ret |= (((const unsigned long *)outdev)[i]
124 ^ ((const unsigned long *)ipinfo->outiface)[i])
125 & ((const unsigned long *)ipinfo->outiface_mask)[i];
126 }
127
128 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
129 dprintf("VIA out mismatch (%s vs %s).%s\n",
130 outdev, ipinfo->outiface,
131 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
132 return 0;
133 }
134
135 /* Check specific protocol */
136 if (ipinfo->proto
137 && FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
138 dprintf("Packet protocol %hi does not match %hi.%s\n",
139 ip->protocol, ipinfo->proto,
140 ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
141 return 0;
142 }
143
144 /* If we have a fragment rule but the packet is not a fragment
145 * then we return zero */
146 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
147 dprintf("Fragment rule but not fragment.%s\n",
148 ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
149 return 0;
150 }
151
152 return 1;
153}
154
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700155static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156ip_checkentry(const struct ipt_ip *ip)
157{
158 if (ip->flags & ~IPT_F_MASK) {
159 duprintf("Unknown flag bits set: %08X\n",
160 ip->flags & ~IPT_F_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700161 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 if (ip->invflags & ~IPT_INV_MASK) {
164 duprintf("Unknown invflag bits set: %08X\n",
165 ip->invflags & ~IPT_INV_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700166 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700168 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171static unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700172ipt_error(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 const struct net_device *in,
174 const struct net_device *out,
175 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800176 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700177 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 if (net_ratelimit())
180 printk("ip_tables: error: `%s'\n", (char *)targinfo);
181
182 return NF_DROP;
183}
184
185static inline
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700186bool do_match(struct ipt_entry_match *m,
187 const struct sk_buff *skb,
188 const struct net_device *in,
189 const struct net_device *out,
190 int offset,
191 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 /* Stop iteration if it doesn't match */
Patrick McHardy1c524832006-03-20 18:02:15 -0800194 if (!m->u.kernel.match->match(skb, in, out, m->u.kernel.match, m->data,
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300195 offset, ip_hdrlen(skb), hotdrop))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700196 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 else
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700198 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201static inline struct ipt_entry *
202get_entry(void *base, unsigned int offset)
203{
204 return (struct ipt_entry *)(base + offset);
205}
206
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700207/* All zeroes == unconditional rule. */
208static inline int
209unconditional(const struct ipt_ip *ip)
210{
211 unsigned int i;
212
213 for (i = 0; i < sizeof(*ip)/sizeof(__u32); i++)
214 if (((__u32 *)ip)[i])
215 return 0;
216
217 return 1;
218}
219
220#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
221 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
222static const char *hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800223 [NF_INET_PRE_ROUTING] = "PREROUTING",
224 [NF_INET_LOCAL_IN] = "INPUT",
225 [NF_INET_FORWARD] = "FORWARD",
226 [NF_INET_LOCAL_OUT] = "OUTPUT",
227 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700228};
229
230enum nf_ip_trace_comments {
231 NF_IP_TRACE_COMMENT_RULE,
232 NF_IP_TRACE_COMMENT_RETURN,
233 NF_IP_TRACE_COMMENT_POLICY,
234};
235
236static const char *comments[] = {
237 [NF_IP_TRACE_COMMENT_RULE] = "rule",
238 [NF_IP_TRACE_COMMENT_RETURN] = "return",
239 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
240};
241
242static struct nf_loginfo trace_loginfo = {
243 .type = NF_LOG_TYPE_LOG,
244 .u = {
245 .log = {
246 .level = 4,
247 .logflags = NF_LOG_MASK,
248 },
249 },
250};
251
252static inline int
253get_chainname_rulenum(struct ipt_entry *s, struct ipt_entry *e,
254 char *hookname, char **chainname,
255 char **comment, unsigned int *rulenum)
256{
257 struct ipt_standard_target *t = (void *)ipt_get_target(s);
258
259 if (strcmp(t->target.u.kernel.target->name, IPT_ERROR_TARGET) == 0) {
260 /* Head of user chain: ERROR target with chainname */
261 *chainname = t->target.data;
262 (*rulenum) = 0;
263 } else if (s == e) {
264 (*rulenum)++;
265
266 if (s->target_offset == sizeof(struct ipt_entry)
267 && strcmp(t->target.u.kernel.target->name,
268 IPT_STANDARD_TARGET) == 0
269 && t->verdict < 0
270 && unconditional(&s->ip)) {
271 /* Tail of chains: STANDARD target (return/policy) */
272 *comment = *chainname == hookname
273 ? (char *)comments[NF_IP_TRACE_COMMENT_POLICY]
274 : (char *)comments[NF_IP_TRACE_COMMENT_RETURN];
275 }
276 return 1;
277 } else
278 (*rulenum)++;
279
280 return 0;
281}
282
283static void trace_packet(struct sk_buff *skb,
284 unsigned int hook,
285 const struct net_device *in,
286 const struct net_device *out,
287 char *tablename,
288 struct xt_table_info *private,
289 struct ipt_entry *e)
290{
291 void *table_base;
292 struct ipt_entry *root;
293 char *hookname, *chainname, *comment;
294 unsigned int rulenum = 0;
295
296 table_base = (void *)private->entries[smp_processor_id()];
297 root = get_entry(table_base, private->hook_entry[hook]);
298
299 hookname = chainname = (char *)hooknames[hook];
300 comment = (char *)comments[NF_IP_TRACE_COMMENT_RULE];
301
302 IPT_ENTRY_ITERATE(root,
303 private->size - private->hook_entry[hook],
304 get_chainname_rulenum,
305 e, hookname, &chainname, &comment, &rulenum);
306
307 nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo,
308 "TRACE: %s:%s:%s:%u ",
309 tablename, chainname, comment, rulenum);
310}
311#endif
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/* Returns one of the generic firewall policies, like NF_ACCEPT. */
314unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700315ipt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 unsigned int hook,
317 const struct net_device *in,
318 const struct net_device *out,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800319 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
322 u_int16_t offset;
323 struct iphdr *ip;
324 u_int16_t datalen;
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700325 bool hotdrop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* Initializing verdict to NF_DROP keeps gcc happy. */
327 unsigned int verdict = NF_DROP;
328 const char *indev, *outdev;
329 void *table_base;
330 struct ipt_entry *e, *back;
Patrick McHardy83117312006-08-17 18:13:53 -0700331 struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* Initialization */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700334 ip = ip_hdr(skb);
335 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 indev = in ? in->name : nulldevname;
337 outdev = out ? out->name : nulldevname;
338 /* We handle fragments by dealing with the first fragment as
339 * if it was a normal packet. All other fragments are treated
340 * normally, except that they will NEVER match rules that ask
341 * things we don't know, ie. tcp syn flag or ports). If the
342 * rule is also a fragment-specific rule, non-fragments won't
343 * match it. */
344 offset = ntohs(ip->frag_off) & IP_OFFSET;
345
346 read_lock_bh(&table->lock);
347 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Patrick McHardy83117312006-08-17 18:13:53 -0700348 private = table->private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800349 table_base = (void *)private->entries[smp_processor_id()];
350 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* For return from builtin chain */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800353 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 do {
356 IP_NF_ASSERT(e);
357 IP_NF_ASSERT(back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (ip_packet_match(ip, indev, outdev, &e->ip, offset)) {
359 struct ipt_entry_target *t;
360
361 if (IPT_MATCH_ITERATE(e, do_match,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700362 skb, in, out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 offset, &hotdrop) != 0)
364 goto no_match;
365
366 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
367
368 t = ipt_get_target(e);
369 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700370
371#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
372 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
373 /* The packet is traced: log it */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700374 if (unlikely(skb->nf_trace))
375 trace_packet(skb, hook, in, out,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700376 table->name, private, e);
377#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Standard target? */
379 if (!t->u.kernel.target->target) {
380 int v;
381
382 v = ((struct ipt_standard_target *)t)->verdict;
383 if (v < 0) {
384 /* Pop from stack? */
385 if (v != IPT_RETURN) {
386 verdict = (unsigned)(-v) - 1;
387 break;
388 }
389 e = back;
390 back = get_entry(table_base,
391 back->comefrom);
392 continue;
393 }
Patrick McHardy05465342005-08-21 23:31:43 -0700394 if (table_base + v != (void *)e + e->next_offset
395 && !(e->ip.flags & IPT_F_GOTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* Save old back ptr in next entry */
397 struct ipt_entry *next
398 = (void *)e + e->next_offset;
399 next->comefrom
400 = (void *)back - table_base;
401 /* set back pointer to next entry */
402 back = next;
403 }
404
405 e = get_entry(table_base, v);
406 } else {
407 /* Targets which reenter must return
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900408 abs. verdicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#ifdef CONFIG_NETFILTER_DEBUG
410 ((struct ipt_entry *)table_base)->comefrom
411 = 0xeeeeeeec;
412#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700413 verdict = t->u.kernel.target->target(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 in, out,
415 hook,
Patrick McHardy1c524832006-03-20 18:02:15 -0800416 t->u.kernel.target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700417 t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419#ifdef CONFIG_NETFILTER_DEBUG
420 if (((struct ipt_entry *)table_base)->comefrom
421 != 0xeeeeeeec
422 && verdict == IPT_CONTINUE) {
423 printk("Target %s reentered!\n",
424 t->u.kernel.target->name);
425 verdict = NF_DROP;
426 }
427 ((struct ipt_entry *)table_base)->comefrom
428 = 0x57acc001;
429#endif
430 /* Target might have changed stuff. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700431 ip = ip_hdr(skb);
432 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 if (verdict == IPT_CONTINUE)
435 e = (void *)e + e->next_offset;
436 else
437 /* Verdict */
438 break;
439 }
440 } else {
441
442 no_match:
443 e = (void *)e + e->next_offset;
444 }
445 } while (!hotdrop);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 read_unlock_bh(&table->lock);
448
449#ifdef DEBUG_ALLOW_ALL
450 return NF_ACCEPT;
451#else
452 if (hotdrop)
453 return NF_DROP;
454 else return verdict;
455#endif
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/* Figures out from what hook each rule can be called: returns 0 if
459 there are loops. Puts hook bitmask in comefrom. */
460static int
Harald Welte2e4e6a12006-01-12 13:30:04 -0800461mark_source_chains(struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800462 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 unsigned int hook;
465
466 /* No recursion; use packet counter to save back ptrs (reset
467 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800468 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 unsigned int pos = newinfo->hook_entry[hook];
470 struct ipt_entry *e
Eric Dumazet31836062005-12-13 23:13:48 -0800471 = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if (!(valid_hooks & (1 << hook)))
474 continue;
475
476 /* Set initial back pointer. */
477 e->counters.pcnt = pos;
478
479 for (;;) {
480 struct ipt_standard_target *t
481 = (void *)ipt_get_target(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800482 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800484 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 printk("iptables: loop hook %u pos %u %08X.\n",
486 hook, pos, e->comefrom);
487 return 0;
488 }
489 e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800490 |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* Unconditional return/END. */
Al Viroe1b4b9f2006-12-12 00:29:52 -0800493 if ((e->target_offset == sizeof(struct ipt_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 && (strcmp(t->target.u.user.name,
495 IPT_STANDARD_TARGET) == 0)
496 && t->verdict < 0
Al Viroe1b4b9f2006-12-12 00:29:52 -0800497 && unconditional(&e->ip)) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 unsigned int oldpos, size;
499
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800500 if (t->verdict < -NF_MAX_VERDICT - 1) {
501 duprintf("mark_source_chains: bad "
502 "negative verdict (%i)\n",
503 t->verdict);
504 return 0;
505 }
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Return: backtrack through the last
508 big jump. */
509 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800510 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511#ifdef DEBUG_IP_FIREWALL_USER
512 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800513 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 duprintf("Back unset "
515 "on hook %u "
516 "rule %u\n",
517 hook, pos);
518 }
519#endif
520 oldpos = pos;
521 pos = e->counters.pcnt;
522 e->counters.pcnt = 0;
523
524 /* We're at the start. */
525 if (pos == oldpos)
526 goto next;
527
528 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800529 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 } while (oldpos == pos + e->next_offset);
531
532 /* Move along one */
533 size = e->next_offset;
534 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800535 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 e->counters.pcnt = pos;
537 pos += size;
538 } else {
539 int newpos = t->verdict;
540
541 if (strcmp(t->target.u.user.name,
542 IPT_STANDARD_TARGET) == 0
543 && newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800544 if (newpos > newinfo->size -
545 sizeof(struct ipt_entry)) {
546 duprintf("mark_source_chains: "
547 "bad verdict (%i)\n",
548 newpos);
549 return 0;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* This a jump; chase it. */
552 duprintf("Jump rule %u -> %u\n",
553 pos, newpos);
554 } else {
555 /* ... this is a fallthru */
556 newpos = pos + e->next_offset;
557 }
558 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800559 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 e->counters.pcnt = pos;
561 pos = newpos;
562 }
563 }
564 next:
565 duprintf("Finished chain %u\n", hook);
566 }
567 return 1;
568}
569
570static inline int
571cleanup_match(struct ipt_entry_match *m, unsigned int *i)
572{
573 if (i && (*i)-- == 0)
574 return 1;
575
576 if (m->u.kernel.match->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700577 m->u.kernel.match->destroy(m->u.kernel.match, m->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 module_put(m->u.kernel.match->me);
579 return 0;
580}
581
582static inline int
Dmitry Mishina96be242006-12-12 00:29:26 -0800583check_entry(struct ipt_entry *e, const char *name)
584{
585 struct ipt_entry_target *t;
586
587 if (!ip_checkentry(&e->ip)) {
588 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
589 return -EINVAL;
590 }
591
592 if (e->target_offset + sizeof(struct ipt_entry_target) > e->next_offset)
593 return -EINVAL;
594
595 t = ipt_get_target(e);
596 if (e->target_offset + t->u.target_size > e->next_offset)
597 return -EINVAL;
598
599 return 0;
600}
601
602static inline int check_match(struct ipt_entry_match *m, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -0800603 const struct ipt_ip *ip,
604 unsigned int hookmask, unsigned int *i)
Dmitry Mishina96be242006-12-12 00:29:26 -0800605{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800606 struct xt_match *match;
Dmitry Mishina96be242006-12-12 00:29:26 -0800607 int ret;
608
609 match = m->u.kernel.match;
610 ret = xt_check_match(match, AF_INET, m->u.match_size - sizeof(*m),
611 name, hookmask, ip->proto,
612 ip->invflags & IPT_INV_PROTO);
613 if (!ret && m->u.kernel.match->checkentry
614 && !m->u.kernel.match->checkentry(name, ip, match, m->data,
615 hookmask)) {
616 duprintf("ip_tables: check failed for `%s'.\n",
617 m->u.kernel.match->name);
618 ret = -EINVAL;
619 }
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -0700620 if (!ret)
621 (*i)++;
Dmitry Mishina96be242006-12-12 00:29:26 -0800622 return ret;
623}
624
625static inline int
626find_check_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -0800627 const char *name,
628 const struct ipt_ip *ip,
629 unsigned int hookmask,
630 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800632 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800633 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Harald Welte2e4e6a12006-01-12 13:30:04 -0800635 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 m->u.user.revision),
637 "ipt_%s", m->u.user.name);
638 if (IS_ERR(match) || !match) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800639 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return match ? PTR_ERR(match) : -ENOENT;
641 }
642 m->u.kernel.match = match;
643
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -0700644 ret = check_match(m, name, ip, hookmask, i);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800645 if (ret)
646 goto err;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800649err:
650 module_put(m->u.kernel.match->me);
651 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Dmitry Mishina96be242006-12-12 00:29:26 -0800654static inline int check_target(struct ipt_entry *e, const char *name)
655{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900656 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800657 struct xt_target *target;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900658 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800659
660 t = ipt_get_target(e);
661 target = t->u.kernel.target;
662 ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
663 name, e->comefrom, e->ip.proto,
664 e->ip.invflags & IPT_INV_PROTO);
665 if (!ret && t->u.kernel.target->checkentry
Patrick McHardy4b478242007-12-17 21:46:15 -0800666 && !t->u.kernel.target->checkentry(name, e, target, t->data,
667 e->comefrom)) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800668 duprintf("ip_tables: check failed for `%s'.\n",
669 t->u.kernel.target->name);
670 ret = -EINVAL;
671 }
672 return ret;
673}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675static inline int
Dmitry Mishina96be242006-12-12 00:29:26 -0800676find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
Patrick McHardy4b478242007-12-17 21:46:15 -0800677 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800680 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int ret;
682 unsigned int j;
683
Dmitry Mishina96be242006-12-12 00:29:26 -0800684 ret = check_entry(e, name);
685 if (ret)
686 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 j = 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800689 ret = IPT_MATCH_ITERATE(e, find_check_match, name, &e->ip,
Patrick McHardy4b478242007-12-17 21:46:15 -0800690 e->comefrom, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (ret != 0)
692 goto cleanup_matches;
693
694 t = ipt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800695 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -0800696 t->u.user.name,
697 t->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 "ipt_%s", t->u.user.name);
699 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800700 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 ret = target ? PTR_ERR(target) : -ENOENT;
702 goto cleanup_matches;
703 }
704 t->u.kernel.target = target;
705
Dmitry Mishina96be242006-12-12 00:29:26 -0800706 ret = check_target(e, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800707 if (ret)
708 goto err;
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 (*i)++;
711 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800712 err:
713 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 cleanup_matches:
715 IPT_MATCH_ITERATE(e, cleanup_match, &j);
716 return ret;
717}
718
719static inline int
720check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800721 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unsigned char *base,
723 unsigned char *limit,
724 const unsigned int *hook_entries,
725 const unsigned int *underflows,
726 unsigned int *i)
727{
728 unsigned int h;
729
730 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0
731 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
732 duprintf("Bad offset %p\n", e);
733 return -EINVAL;
734 }
735
736 if (e->next_offset
737 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
738 duprintf("checking: element %p size %u\n",
739 e, e->next_offset);
740 return -EINVAL;
741 }
742
743 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800744 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if ((unsigned char *)e - base == hook_entries[h])
746 newinfo->hook_entry[h] = hook_entries[h];
747 if ((unsigned char *)e - base == underflows[h])
748 newinfo->underflow[h] = underflows[h];
749 }
750
751 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900752 < 0 (not IPT_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800755 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 e->comefrom = 0;
757
758 (*i)++;
759 return 0;
760}
761
762static inline int
763cleanup_entry(struct ipt_entry *e, unsigned int *i)
764{
765 struct ipt_entry_target *t;
766
767 if (i && (*i)-- == 0)
768 return 1;
769
770 /* Cleanup all matches */
771 IPT_MATCH_ITERATE(e, cleanup_match, NULL);
772 t = ipt_get_target(e);
773 if (t->u.kernel.target->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700774 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 module_put(t->u.kernel.target->me);
776 return 0;
777}
778
779/* Checks and translates the user-supplied table segment (held in
780 newinfo) */
781static int
782translate_table(const char *name,
783 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800784 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800785 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 unsigned int size,
787 unsigned int number,
788 const unsigned int *hook_entries,
789 const unsigned int *underflows)
790{
791 unsigned int i;
792 int ret;
793
794 newinfo->size = size;
795 newinfo->number = number;
796
797 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800798 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 newinfo->hook_entry[i] = 0xFFFFFFFF;
800 newinfo->underflow[i] = 0xFFFFFFFF;
801 }
802
803 duprintf("translate_table: size %u\n", newinfo->size);
804 i = 0;
805 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800806 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 check_entry_size_and_hooks,
808 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800809 entry0,
810 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 hook_entries, underflows, &i);
812 if (ret != 0)
813 return ret;
814
815 if (i != number) {
816 duprintf("translate_table: %u not %u entries\n",
817 i, number);
818 return -EINVAL;
819 }
820
821 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800822 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* Only hooks which are valid */
824 if (!(valid_hooks & (1 << i)))
825 continue;
826 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
827 duprintf("Invalid hook entry %u %u\n",
828 i, hook_entries[i]);
829 return -EINVAL;
830 }
831 if (newinfo->underflow[i] == 0xFFFFFFFF) {
832 duprintf("Invalid underflow %u %u\n",
833 i, underflows[i]);
834 return -EINVAL;
835 }
836 }
837
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800838 if (!mark_source_chains(newinfo, valid_hooks, entry0))
839 return -ELOOP;
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* Finally, each sanity check must pass */
842 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800843 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Dmitry Mishina96be242006-12-12 00:29:26 -0800844 find_check_entry, name, size, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800846 if (ret != 0) {
847 IPT_ENTRY_ITERATE(entry0, newinfo->size,
848 cleanup_entry, &i);
849 return ret;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700853 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800854 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
855 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
858 return ret;
859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861/* Gets counters. */
862static inline int
863add_entry_to_counter(const struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800864 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 unsigned int *i)
866{
867 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
868
869 (*i)++;
870 return 0;
871}
872
Eric Dumazet31836062005-12-13 23:13:48 -0800873static inline int
874set_entry_to_counter(const struct ipt_entry *e,
875 struct ipt_counters total[],
876 unsigned int *i)
877{
878 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
879
880 (*i)++;
881 return 0;
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800885get_counters(const struct xt_table_info *t,
886 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 unsigned int cpu;
889 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800890 unsigned int curcpu;
891
892 /* Instead of clearing (by a previous call to memset())
893 * the counters and using adds, we set the counters
894 * with data used by 'current' CPU
895 * We dont care about preemption here.
896 */
897 curcpu = raw_smp_processor_id();
898
899 i = 0;
900 IPT_ENTRY_ITERATE(t->entries[curcpu],
901 t->size,
902 set_entry_to_counter,
903 counters,
904 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700906 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800907 if (cpu == curcpu)
908 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800910 IPT_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 t->size,
912 add_entry_to_counter,
913 counters,
914 &i);
915 }
916}
917
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800918static inline struct xt_counters * alloc_counters(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Dmitry Mishin27229712006-04-01 02:25:19 -0800920 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800921 struct xt_counters *counters;
922 struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /* We need atomic snapshot of counters: rest doesn't change
925 (other than comefrom, which userspace doesn't care
926 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800927 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet31836062005-12-13 23:13:48 -0800928 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (counters == NULL)
Dmitry Mishin27229712006-04-01 02:25:19 -0800931 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 write_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800935 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 write_unlock_bh(&table->lock);
937
Dmitry Mishin27229712006-04-01 02:25:19 -0800938 return counters;
939}
940
941static int
942copy_entries_to_user(unsigned int total_size,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800943 struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800944 void __user *userptr)
945{
946 unsigned int off, num;
947 struct ipt_entry *e;
948 struct xt_counters *counters;
949 struct xt_table_info *private = table->private;
950 int ret = 0;
951 void *loc_cpu_entry;
952
953 counters = alloc_counters(table);
954 if (IS_ERR(counters))
955 return PTR_ERR(counters);
956
Eric Dumazet31836062005-12-13 23:13:48 -0800957 /* choose the copy that is on our node/cpu, ...
958 * This choice is lazy (because current thread is
959 * allowed to migrate to another cpu)
960 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800961 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800962 /* ... then copy entire thing ... */
963 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 ret = -EFAULT;
965 goto free_counters;
966 }
967
968 /* FIXME: use iterator macros --RR */
969 /* ... then go back and fix counters and names */
970 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
971 unsigned int i;
972 struct ipt_entry_match *m;
973 struct ipt_entry_target *t;
974
Eric Dumazet31836062005-12-13 23:13:48 -0800975 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (copy_to_user(userptr + off
977 + offsetof(struct ipt_entry, counters),
978 &counters[num],
979 sizeof(counters[num])) != 0) {
980 ret = -EFAULT;
981 goto free_counters;
982 }
983
984 for (i = sizeof(struct ipt_entry);
985 i < e->target_offset;
986 i += m->u.match_size) {
987 m = (void *)e + i;
988
989 if (copy_to_user(userptr + off + i
990 + offsetof(struct ipt_entry_match,
991 u.user.name),
992 m->u.kernel.match->name,
993 strlen(m->u.kernel.match->name)+1)
994 != 0) {
995 ret = -EFAULT;
996 goto free_counters;
997 }
998 }
999
1000 t = ipt_get_target(e);
1001 if (copy_to_user(userptr + off + e->target_offset
1002 + offsetof(struct ipt_entry_target,
1003 u.user.name),
1004 t->u.kernel.target->name,
1005 strlen(t->u.kernel.target->name)+1) != 0) {
1006 ret = -EFAULT;
1007 goto free_counters;
1008 }
1009 }
1010
1011 free_counters:
1012 vfree(counters);
1013 return ret;
1014}
1015
Dmitry Mishin27229712006-04-01 02:25:19 -08001016#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001017static void compat_standard_from_user(void *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001018{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001019 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001020
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001021 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001022 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001023 memcpy(dst, &v, sizeof(v));
1024}
1025
1026static int compat_standard_to_user(void __user *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001027{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001028 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001029
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001030 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001031 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001032 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001033}
1034
1035static inline int
Patrick McHardy4b478242007-12-17 21:46:15 -08001036compat_calc_match(struct ipt_entry_match *m, int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001037{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001038 *size += xt_compat_match_offset(m->u.kernel.match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001039 return 0;
1040}
1041
Eric Dumazet259d4e42007-12-04 23:24:56 -08001042static int compat_calc_entry(struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001043 const struct xt_table_info *info,
1044 void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001045{
1046 struct ipt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001047 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001048 int off, i, ret;
1049
Patrick McHardy30c08c42007-12-17 21:47:14 -08001050 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001051 entry_offset = (void *)e - base;
1052 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
1053 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001054 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001055 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001056 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001057 if (ret)
1058 return ret;
1059
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001060 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001061 if (info->hook_entry[i] &&
1062 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001063 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001064 if (info->underflow[i] &&
1065 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001066 newinfo->underflow[i] -= off;
1067 }
1068 return 0;
1069}
1070
Eric Dumazet259d4e42007-12-04 23:24:56 -08001071static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001072 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001073{
1074 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001075
1076 if (!newinfo || !info)
1077 return -EINVAL;
1078
Eric Dumazet259d4e42007-12-04 23:24:56 -08001079 /* we dont care about newinfo->entries[] */
1080 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1081 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001082 loc_cpu_entry = info->entries[raw_smp_processor_id()];
1083 return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001084 compat_calc_entry, info, loc_cpu_entry,
1085 newinfo);
Dmitry Mishin27229712006-04-01 02:25:19 -08001086}
1087#endif
1088
1089static int get_info(void __user *user, int *len, int compat)
1090{
1091 char name[IPT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001092 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001093 int ret;
1094
1095 if (*len != sizeof(struct ipt_getinfo)) {
1096 duprintf("length %u != %u\n", *len,
1097 (unsigned int)sizeof(struct ipt_getinfo));
1098 return -EINVAL;
1099 }
1100
1101 if (copy_from_user(name, user, sizeof(name)) != 0)
1102 return -EFAULT;
1103
1104 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1105#ifdef CONFIG_COMPAT
1106 if (compat)
1107 xt_compat_lock(AF_INET);
1108#endif
1109 t = try_then_request_module(xt_find_table_lock(AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001110 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001111 if (t && !IS_ERR(t)) {
1112 struct ipt_getinfo info;
1113 struct xt_table_info *private = t->private;
1114
1115#ifdef CONFIG_COMPAT
1116 if (compat) {
1117 struct xt_table_info tmp;
1118 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001119 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001120 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001121 }
1122#endif
1123 info.valid_hooks = t->valid_hooks;
1124 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001125 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001126 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001127 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001128 info.num_entries = private->number;
1129 info.size = private->size;
1130 strcpy(info.name, name);
1131
1132 if (copy_to_user(user, &info, *len) != 0)
1133 ret = -EFAULT;
1134 else
1135 ret = 0;
1136
1137 xt_table_unlock(t);
1138 module_put(t->me);
1139 } else
1140 ret = t ? PTR_ERR(t) : -ENOENT;
1141#ifdef CONFIG_COMPAT
1142 if (compat)
1143 xt_compat_unlock(AF_INET);
1144#endif
1145 return ret;
1146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001149get_entries(struct ipt_get_entries __user *uptr, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001152 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001153 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Dmitry Mishin27229712006-04-01 02:25:19 -08001155 if (*len < sizeof(get)) {
1156 duprintf("get_entries: %u < %d\n", *len,
1157 (unsigned int)sizeof(get));
1158 return -EINVAL;
1159 }
1160 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1161 return -EFAULT;
1162 if (*len != sizeof(struct ipt_get_entries) + get.size) {
1163 duprintf("get_entries: %u != %u\n", *len,
1164 (unsigned int)(sizeof(struct ipt_get_entries) +
1165 get.size));
1166 return -EINVAL;
1167 }
1168
1169 t = xt_find_table_lock(AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (t && !IS_ERR(t)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001171 struct xt_table_info *private = t->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 duprintf("t->private->number = %u\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -08001173 private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001174 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001175 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 t, uptr->entrytable);
1177 else {
1178 duprintf("get_entries: I've got %u not %u!\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -08001179 private->size,
Dmitry Mishin27229712006-04-01 02:25:19 -08001180 get.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 ret = -EINVAL;
1182 }
1183 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001184 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 } else
1186 ret = t ? PTR_ERR(t) : -ENOENT;
1187
1188 return ret;
1189}
1190
1191static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001192__do_replace(const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001193 struct xt_table_info *newinfo, unsigned int num_counters,
1194 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001195{
1196 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001197 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001198 struct xt_table_info *oldinfo;
1199 struct xt_counters *counters;
1200 void *loc_cpu_old_entry;
1201
1202 ret = 0;
1203 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1204 if (!counters) {
1205 ret = -ENOMEM;
1206 goto out;
1207 }
1208
1209 t = try_then_request_module(xt_find_table_lock(AF_INET, name),
1210 "iptable_%s", name);
1211 if (!t || IS_ERR(t)) {
1212 ret = t ? PTR_ERR(t) : -ENOENT;
1213 goto free_newinfo_counters_untrans;
1214 }
1215
1216 /* You lied! */
1217 if (valid_hooks != t->valid_hooks) {
1218 duprintf("Valid hook crap: %08X vs %08X\n",
1219 valid_hooks, t->valid_hooks);
1220 ret = -EINVAL;
1221 goto put_module;
1222 }
1223
1224 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1225 if (!oldinfo)
1226 goto put_module;
1227
1228 /* Update module usage count based on number of rules */
1229 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1230 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1231 if ((oldinfo->number > oldinfo->initial_entries) ||
1232 (newinfo->number <= oldinfo->initial_entries))
1233 module_put(t->me);
1234 if ((oldinfo->number > oldinfo->initial_entries) &&
1235 (newinfo->number <= oldinfo->initial_entries))
1236 module_put(t->me);
1237
1238 /* Get the old counters. */
1239 get_counters(oldinfo, counters);
1240 /* Decrease module usage counts and free resource */
1241 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Patrick McHardy4b478242007-12-17 21:46:15 -08001242 IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1243 NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001244 xt_free_table_info(oldinfo);
1245 if (copy_to_user(counters_ptr, counters,
1246 sizeof(struct xt_counters) * num_counters) != 0)
1247 ret = -EFAULT;
1248 vfree(counters);
1249 xt_table_unlock(t);
1250 return ret;
1251
1252 put_module:
1253 module_put(t->me);
1254 xt_table_unlock(t);
1255 free_newinfo_counters_untrans:
1256 vfree(counters);
1257 out:
1258 return ret;
1259}
1260
1261static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262do_replace(void __user *user, unsigned int len)
1263{
1264 int ret;
1265 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001266 struct xt_table_info *newinfo;
1267 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1270 return -EFAULT;
1271
1272 /* Hack: Causes ipchains to give correct error msg --RR */
1273 if (len != sizeof(tmp) + tmp.size)
1274 return -ENOPROTOOPT;
1275
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001276 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001277 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1278 return -ENOMEM;
1279
Harald Welte2e4e6a12006-01-12 13:30:04 -08001280 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (!newinfo)
1282 return -ENOMEM;
1283
Eric Dumazet31836062005-12-13 23:13:48 -08001284 /* choose the copy that is our node/cpu */
1285 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1286 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 tmp.size) != 0) {
1288 ret = -EFAULT;
1289 goto free_newinfo;
1290 }
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001293 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 tmp.hook_entry, tmp.underflow);
1295 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001296 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 duprintf("ip_tables: Translated table\n");
1299
Patrick McHardy4b478242007-12-17 21:46:15 -08001300 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1301 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001302 if (ret)
1303 goto free_newinfo_untrans;
1304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Dmitry Mishin27229712006-04-01 02:25:19 -08001306 free_newinfo_untrans:
Eric Dumazet31836062005-12-13 23:13:48 -08001307 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001309 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return ret;
1311}
1312
1313/* We're lazy, and add to the first CPU; overflow works its fey magic
1314 * and everything is OK. */
1315static inline int
1316add_counter_to_entry(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001317 const struct xt_counters addme[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 unsigned int *i)
1319{
1320#if 0
1321 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1322 *i,
1323 (long unsigned int)e->counters.pcnt,
1324 (long unsigned int)e->counters.bcnt,
1325 (long unsigned int)addme[*i].pcnt,
1326 (long unsigned int)addme[*i].bcnt);
1327#endif
1328
1329 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1330
1331 (*i)++;
1332 return 0;
1333}
1334
1335static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001336do_add_counters(void __user *user, unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 unsigned int i;
Dmitry Mishin27229712006-04-01 02:25:19 -08001339 struct xt_counters_info tmp;
1340 struct xt_counters *paddc;
1341 unsigned int num_counters;
1342 char *name;
1343 int size;
1344 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001345 struct xt_table *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001346 struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001348 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001349#ifdef CONFIG_COMPAT
1350 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Dmitry Mishin27229712006-04-01 02:25:19 -08001352 if (compat) {
1353 ptmp = &compat_tmp;
1354 size = sizeof(struct compat_xt_counters_info);
1355 } else
1356#endif
1357 {
1358 ptmp = &tmp;
1359 size = sizeof(struct xt_counters_info);
1360 }
1361
1362 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -EFAULT;
1364
Dmitry Mishin27229712006-04-01 02:25:19 -08001365#ifdef CONFIG_COMPAT
1366 if (compat) {
1367 num_counters = compat_tmp.num_counters;
1368 name = compat_tmp.name;
1369 } else
1370#endif
1371 {
1372 num_counters = tmp.num_counters;
1373 name = tmp.name;
1374 }
1375
1376 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return -EINVAL;
1378
Dmitry Mishin27229712006-04-01 02:25:19 -08001379 paddc = vmalloc_node(len - size, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (!paddc)
1381 return -ENOMEM;
1382
Dmitry Mishin27229712006-04-01 02:25:19 -08001383 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 ret = -EFAULT;
1385 goto free;
1386 }
1387
Dmitry Mishin27229712006-04-01 02:25:19 -08001388 t = xt_find_table_lock(AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (!t || IS_ERR(t)) {
1390 ret = t ? PTR_ERR(t) : -ENOENT;
1391 goto free;
1392 }
1393
1394 write_lock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001395 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001396 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 ret = -EINVAL;
1398 goto unlock_up_free;
1399 }
1400
1401 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001402 /* Choose the copy that is on our node */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001403 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -08001404 IPT_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001405 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 add_counter_to_entry,
Dmitry Mishin27229712006-04-01 02:25:19 -08001407 paddc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 &i);
1409 unlock_up_free:
1410 write_unlock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001411 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 module_put(t->me);
1413 free:
1414 vfree(paddc);
1415
1416 return ret;
1417}
1418
Dmitry Mishin27229712006-04-01 02:25:19 -08001419#ifdef CONFIG_COMPAT
1420struct compat_ipt_replace {
1421 char name[IPT_TABLE_MAXNAMELEN];
1422 u32 valid_hooks;
1423 u32 num_entries;
1424 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001425 u32 hook_entry[NF_INET_NUMHOOKS];
1426 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001427 u32 num_counters;
1428 compat_uptr_t counters; /* struct ipt_counters * */
1429 struct compat_ipt_entry entries[0];
1430};
1431
Patrick McHardya18aa312007-12-12 10:35:16 -08001432static int
1433compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
1434 compat_uint_t *size, struct xt_counters *counters,
1435 unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001436{
Al Viro3e597c62006-09-24 23:42:20 +01001437 struct ipt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001438 struct compat_ipt_entry __user *ce;
1439 u_int16_t target_offset, next_offset;
1440 compat_uint_t origsize;
1441 int ret;
1442
1443 ret = -EFAULT;
1444 origsize = *size;
1445 ce = (struct compat_ipt_entry __user *)*dstptr;
Patrick McHardy78000072006-05-03 23:20:27 -07001446 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
Dmitry Mishin27229712006-04-01 02:25:19 -08001447 goto out;
1448
Patrick McHardya18aa312007-12-12 10:35:16 -08001449 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1450 goto out;
1451
Dmitry Mishin27229712006-04-01 02:25:19 -08001452 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001453 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1454
Patrick McHardyac8e27f2007-12-17 21:45:52 -08001455 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001456 target_offset = e->target_offset - (origsize - *size);
1457 if (ret)
1458 goto out;
1459 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001460 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001461 if (ret)
1462 goto out;
1463 ret = -EFAULT;
1464 next_offset = e->next_offset - (origsize - *size);
Patrick McHardy78000072006-05-03 23:20:27 -07001465 if (put_user(target_offset, &ce->target_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001466 goto out;
Patrick McHardy78000072006-05-03 23:20:27 -07001467 if (put_user(next_offset, &ce->next_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001468 goto out;
Patrick McHardya18aa312007-12-12 10:35:16 -08001469
1470 (*i)++;
Dmitry Mishin27229712006-04-01 02:25:19 -08001471 return 0;
1472out:
1473 return ret;
1474}
1475
1476static inline int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001477compat_find_calc_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001478 const char *name,
1479 const struct ipt_ip *ip,
1480 unsigned int hookmask,
1481 int *size, int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001482{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001483 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001484
1485 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001486 m->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001487 "ipt_%s", m->u.user.name);
1488 if (IS_ERR(match) || !match) {
1489 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001490 m->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001491 return match ? PTR_ERR(match) : -ENOENT;
1492 }
1493 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001494 *size += xt_compat_match_offset(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001495
1496 (*i)++;
1497 return 0;
1498}
1499
1500static inline int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001501compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1502{
1503 if (i && (*i)-- == 0)
1504 return 1;
1505
1506 module_put(m->u.kernel.match->me);
1507 return 0;
1508}
1509
1510static inline int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001511compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001512{
1513 struct ipt_entry_target *t;
1514
1515 if (i && (*i)-- == 0)
1516 return 1;
1517
1518 /* Cleanup all matches */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001519 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1520 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001521 module_put(t->u.kernel.target->me);
1522 return 0;
1523}
1524
1525static inline int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001526check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001527 struct xt_table_info *newinfo,
1528 unsigned int *size,
1529 unsigned char *base,
1530 unsigned char *limit,
1531 unsigned int *hook_entries,
1532 unsigned int *underflows,
1533 unsigned int *i,
1534 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001535{
1536 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001537 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001538 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001539 int ret, off, h, j;
1540
1541 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1542 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0
1543 || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1544 duprintf("Bad offset %p, limit = %p\n", e, limit);
1545 return -EINVAL;
1546 }
1547
1548 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001549 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001550 duprintf("checking: element %p size %u\n",
1551 e, e->next_offset);
1552 return -EINVAL;
1553 }
1554
Patrick McHardy73cd5982007-12-17 21:47:32 -08001555 /* For purposes of check_entry casting the compat entry is fine */
1556 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001557 if (ret)
1558 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001559
Patrick McHardy30c08c42007-12-17 21:47:14 -08001560 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001561 entry_offset = (void *)e - (void *)base;
1562 j = 0;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001563 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1564 &e->ip, e->comefrom, &off, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001565 if (ret != 0)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001566 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001567
Patrick McHardy73cd5982007-12-17 21:47:32 -08001568 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001569 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -08001570 t->u.user.name,
1571 t->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001572 "ipt_%s", t->u.user.name);
1573 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001574 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001575 t->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001576 ret = target ? PTR_ERR(target) : -ENOENT;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001577 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001578 }
1579 t->u.kernel.target = target;
1580
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001581 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001582 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001583 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001584 if (ret)
1585 goto out;
1586
1587 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001588 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001589 if ((unsigned char *)e - base == hook_entries[h])
1590 newinfo->hook_entry[h] = hook_entries[h];
1591 if ((unsigned char *)e - base == underflows[h])
1592 newinfo->underflow[h] = underflows[h];
1593 }
1594
1595 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001596 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001597 e->comefrom = 0;
1598
1599 (*i)++;
1600 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001601
Dmitry Mishin27229712006-04-01 02:25:19 -08001602out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001603 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001604release_matches:
1605 IPT_MATCH_ITERATE(e, compat_release_match, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001606 return ret;
1607}
1608
Patrick McHardy4b478242007-12-17 21:46:15 -08001609static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001610compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001611 unsigned int *size, const char *name,
1612 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001613{
1614 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001615 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001616 struct ipt_entry *de;
1617 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001618 int ret, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001619
1620 ret = 0;
1621 origsize = *size;
1622 de = (struct ipt_entry *)*dstptr;
1623 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001624 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001625
Patrick McHardy73cd5982007-12-17 21:47:32 -08001626 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001627 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1628
Patrick McHardy73cd5982007-12-17 21:47:32 -08001629 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1630 dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001631 if (ret)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001632 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001633 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001634 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001635 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001636 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001637
1638 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001639 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001640 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1641 newinfo->hook_entry[h] -= origsize - *size;
1642 if ((unsigned char *)de - base < newinfo->underflow[h])
1643 newinfo->underflow[h] -= origsize - *size;
1644 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001645 return ret;
1646}
Dmitry Mishin27229712006-04-01 02:25:19 -08001647
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001648static inline int compat_check_entry(struct ipt_entry *e, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001649 unsigned int *i)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001650{
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001651 int j, ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001652
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001653 j = 0;
1654 ret = IPT_MATCH_ITERATE(e, check_match, name, &e->ip, e->comefrom, &j);
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001655 if (ret)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001656 goto cleanup_matches;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001657
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001658 ret = check_target(e, name);
1659 if (ret)
1660 goto cleanup_matches;
1661
1662 (*i)++;
1663 return 0;
1664
1665 cleanup_matches:
1666 IPT_MATCH_ITERATE(e, cleanup_match, &j);
1667 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001668}
1669
Dmitry Mishin27229712006-04-01 02:25:19 -08001670static int
1671translate_compat_table(const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001672 unsigned int valid_hooks,
1673 struct xt_table_info **pinfo,
1674 void **pentry0,
1675 unsigned int total_size,
1676 unsigned int number,
1677 unsigned int *hook_entries,
1678 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001679{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001680 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001681 struct xt_table_info *newinfo, *info;
1682 void *pos, *entry0, *entry1;
1683 unsigned int size;
1684 int ret;
1685
1686 info = *pinfo;
1687 entry0 = *pentry0;
1688 size = total_size;
1689 info->number = number;
1690
1691 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001692 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001693 info->hook_entry[i] = 0xFFFFFFFF;
1694 info->underflow[i] = 0xFFFFFFFF;
1695 }
1696
1697 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001698 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001699 xt_compat_lock(AF_INET);
1700 /* Walk through entries, checking offsets. */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001701 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1702 check_compat_entry_size_and_hooks,
1703 info, &size, entry0,
1704 entry0 + total_size,
1705 hook_entries, underflows, &j, name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001706 if (ret != 0)
1707 goto out_unlock;
1708
1709 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001710 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001711 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001712 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001713 goto out_unlock;
1714 }
1715
1716 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001717 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001718 /* Only hooks which are valid */
1719 if (!(valid_hooks & (1 << i)))
1720 continue;
1721 if (info->hook_entry[i] == 0xFFFFFFFF) {
1722 duprintf("Invalid hook entry %u %u\n",
1723 i, hook_entries[i]);
1724 goto out_unlock;
1725 }
1726 if (info->underflow[i] == 0xFFFFFFFF) {
1727 duprintf("Invalid underflow %u %u\n",
1728 i, underflows[i]);
1729 goto out_unlock;
1730 }
1731 }
1732
1733 ret = -ENOMEM;
1734 newinfo = xt_alloc_table_info(size);
1735 if (!newinfo)
1736 goto out_unlock;
1737
1738 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001739 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001740 newinfo->hook_entry[i] = info->hook_entry[i];
1741 newinfo->underflow[i] = info->underflow[i];
1742 }
1743 entry1 = newinfo->entries[raw_smp_processor_id()];
1744 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001745 size = total_size;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001746 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1747 compat_copy_entry_from_user, &pos, &size,
1748 name, newinfo, entry1);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001749 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001750 xt_compat_unlock(AF_INET);
1751 if (ret)
1752 goto free_newinfo;
1753
1754 ret = -ELOOP;
1755 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1756 goto free_newinfo;
1757
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001758 i = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001759 ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001760 name, &i);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001761 if (ret) {
1762 j -= i;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001763 COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1764 compat_release_entry, &j);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001765 IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1766 xt_free_table_info(newinfo);
1767 return ret;
1768 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001769
Dmitry Mishin27229712006-04-01 02:25:19 -08001770 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001771 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001772 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1773 memcpy(newinfo->entries[i], entry1, newinfo->size);
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:
Patrick McHardy73cd5982007-12-17 21:47:32 -08001783 COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001784 return ret;
1785out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001786 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001787 xt_compat_unlock(AF_INET);
1788 goto out;
1789}
1790
1791static int
1792compat_do_replace(void __user *user, unsigned int len)
1793{
1794 int ret;
1795 struct compat_ipt_replace tmp;
1796 struct xt_table_info *newinfo;
1797 void *loc_cpu_entry;
1798
1799 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1800 return -EFAULT;
1801
1802 /* Hack: Causes ipchains to give correct error msg --RR */
1803 if (len != sizeof(tmp) + tmp.size)
1804 return -ENOPROTOOPT;
1805
1806 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001807 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001808 return -ENOMEM;
1809 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1810 return -ENOMEM;
1811
1812 newinfo = xt_alloc_table_info(tmp.size);
1813 if (!newinfo)
1814 return -ENOMEM;
1815
1816 /* choose the copy that is our node/cpu */
1817 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1818 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1819 tmp.size) != 0) {
1820 ret = -EFAULT;
1821 goto free_newinfo;
1822 }
1823
1824 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001825 &newinfo, &loc_cpu_entry, tmp.size,
1826 tmp.num_entries, tmp.hook_entry,
1827 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001828 if (ret != 0)
1829 goto free_newinfo;
1830
1831 duprintf("compat_do_replace: Translated table\n");
1832
Patrick McHardy4b478242007-12-17 21:46:15 -08001833 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1834 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001835 if (ret)
1836 goto free_newinfo_untrans;
1837 return 0;
1838
1839 free_newinfo_untrans:
Patrick McHardy4b478242007-12-17 21:46:15 -08001840 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001841 free_newinfo:
1842 xt_free_table_info(newinfo);
1843 return ret;
1844}
1845
1846static int
1847compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001848 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001849{
1850 int ret;
1851
1852 if (!capable(CAP_NET_ADMIN))
1853 return -EPERM;
1854
1855 switch (cmd) {
1856 case IPT_SO_SET_REPLACE:
1857 ret = compat_do_replace(user, len);
1858 break;
1859
1860 case IPT_SO_SET_ADD_COUNTERS:
1861 ret = do_add_counters(user, len, 1);
1862 break;
1863
1864 default:
1865 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1866 ret = -EINVAL;
1867 }
1868
1869 return ret;
1870}
1871
Patrick McHardy4b478242007-12-17 21:46:15 -08001872struct compat_ipt_get_entries {
Dmitry Mishin27229712006-04-01 02:25:19 -08001873 char name[IPT_TABLE_MAXNAMELEN];
1874 compat_uint_t size;
1875 struct compat_ipt_entry entrytable[0];
1876};
1877
Patrick McHardy4b478242007-12-17 21:46:15 -08001878static int
1879compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1880 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001881{
Dmitry Mishin27229712006-04-01 02:25:19 -08001882 struct xt_counters *counters;
1883 struct xt_table_info *private = table->private;
1884 void __user *pos;
1885 unsigned int size;
1886 int ret = 0;
1887 void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001888 unsigned int i = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001889
1890 counters = alloc_counters(table);
1891 if (IS_ERR(counters))
1892 return PTR_ERR(counters);
1893
1894 /* choose the copy that is on our node/cpu, ...
1895 * This choice is lazy (because current thread is
1896 * allowed to migrate to another cpu)
1897 */
1898 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1899 pos = userptr;
1900 size = total_size;
1901 ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
Patrick McHardya18aa312007-12-12 10:35:16 -08001902 compat_copy_entry_to_user,
1903 &pos, &size, counters, &i);
Dmitry Mishin27229712006-04-01 02:25:19 -08001904
Dmitry Mishin27229712006-04-01 02:25:19 -08001905 vfree(counters);
1906 return ret;
1907}
1908
1909static int
1910compat_get_entries(struct compat_ipt_get_entries __user *uptr, int *len)
1911{
1912 int ret;
1913 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001914 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001915
Dmitry Mishin27229712006-04-01 02:25:19 -08001916 if (*len < sizeof(get)) {
1917 duprintf("compat_get_entries: %u < %u\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001918 *len, (unsigned int)sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001919 return -EINVAL;
1920 }
1921
1922 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1923 return -EFAULT;
1924
1925 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
1926 duprintf("compat_get_entries: %u != %u\n", *len,
Patrick McHardy4b478242007-12-17 21:46:15 -08001927 (unsigned int)(sizeof(struct compat_ipt_get_entries) +
1928 get.size));
Dmitry Mishin27229712006-04-01 02:25:19 -08001929 return -EINVAL;
1930 }
1931
1932 xt_compat_lock(AF_INET);
1933 t = xt_find_table_lock(AF_INET, get.name);
1934 if (t && !IS_ERR(t)) {
1935 struct xt_table_info *private = t->private;
1936 struct xt_table_info info;
1937 duprintf("t->private->number = %u\n",
1938 private->number);
1939 ret = compat_table_info(private, &info);
1940 if (!ret && get.size == info.size) {
1941 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001942 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001943 } else if (!ret) {
1944 duprintf("compat_get_entries: I've got %u not %u!\n",
1945 private->size,
1946 get.size);
1947 ret = -EINVAL;
1948 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001949 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001950 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
Patrick McHardy79030ed2006-09-20 12:05:08 -07001959static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1960
Dmitry Mishin27229712006-04-01 02:25:19 -08001961static int
1962compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1963{
1964 int ret;
1965
Björn Steinbrink82fac052006-10-20 00:21:10 -07001966 if (!capable(CAP_NET_ADMIN))
1967 return -EPERM;
1968
Dmitry Mishin27229712006-04-01 02:25:19 -08001969 switch (cmd) {
1970 case IPT_SO_GET_INFO:
1971 ret = get_info(user, len, 1);
1972 break;
1973 case IPT_SO_GET_ENTRIES:
1974 ret = compat_get_entries(user, len);
1975 break;
1976 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001977 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001978 }
1979 return ret;
1980}
1981#endif
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983static int
1984do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1985{
1986 int ret;
1987
1988 if (!capable(CAP_NET_ADMIN))
1989 return -EPERM;
1990
1991 switch (cmd) {
1992 case IPT_SO_SET_REPLACE:
1993 ret = do_replace(user, len);
1994 break;
1995
1996 case IPT_SO_SET_ADD_COUNTERS:
Dmitry Mishin27229712006-04-01 02:25:19 -08001997 ret = do_add_counters(user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 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 (!capable(CAP_NET_ADMIN))
2014 return -EPERM;
2015
2016 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002017 case IPT_SO_GET_INFO:
2018 ret = get_info(user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002020
2021 case IPT_SO_GET_ENTRIES:
2022 ret = get_entries(user, len);
2023 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 case IPT_SO_GET_REVISION_MATCH:
2026 case IPT_SO_GET_REVISION_TARGET: {
2027 struct ipt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002028 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
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
2039 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002040 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002042 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Harald Welte2e4e6a12006-01-12 13:30:04 -08002044 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2045 rev.revision,
2046 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 "ipt_%s", rev.name);
2048 break;
2049 }
2050
2051 default:
2052 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2053 ret = -EINVAL;
2054 }
2055
2056 return ret;
2057}
2058
Harald Welte2e4e6a12006-01-12 13:30:04 -08002059int ipt_register_table(struct xt_table *table, const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002062 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08002063 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08002065 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Harald Welte2e4e6a12006-01-12 13:30:04 -08002067 newinfo = xt_alloc_table_info(repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (!newinfo)
2069 return -ENOMEM;
2070
Eric Dumazet31836062005-12-13 23:13:48 -08002071 /* choose the copy on our node/cpu
2072 * but dont care of preemption
2073 */
2074 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2075 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08002078 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 repl->num_entries,
2080 repl->hook_entry,
2081 repl->underflow);
2082 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08002083 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return ret;
2085 }
2086
Patrick McHardyda298d32006-06-27 03:00:09 -07002087 ret = xt_register_table(table, &bootstrap, newinfo);
2088 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08002089 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return ret;
2091 }
2092
Harald Welte2e4e6a12006-01-12 13:30:04 -08002093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Jan Engelhardte60a13e2007-02-07 15:12:33 -08002096void ipt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002098 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002099 void *loc_cpu_entry;
2100
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002101 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002104 loc_cpu_entry = private->entries[raw_smp_processor_id()];
2105 IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
2106 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
2109/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002110static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2112 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002113 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 return ((test_type == 0xFF) || (type == test_type && code >= min_code && code <= max_code))
2116 ^ invert;
2117}
2118
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002119static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120icmp_match(const struct sk_buff *skb,
2121 const struct net_device *in,
2122 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -08002123 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 const void *matchinfo,
2125 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -08002126 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -07002127 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 struct icmphdr _icmph, *ic;
2130 const struct ipt_icmp *icmpinfo = matchinfo;
2131
2132 /* Must not be a fragment. */
2133 if (offset)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002134 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Harald Welte2e4e6a12006-01-12 13:30:04 -08002136 ic = skb_header_pointer(skb, protoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (ic == NULL) {
2138 /* We've been asked to examine this packet, and we
2139 * can't. Hence, no choice but to drop.
2140 */
2141 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -07002142 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002143 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
2146 return icmp_type_code_match(icmpinfo->type,
2147 icmpinfo->code[0],
2148 icmpinfo->code[1],
2149 ic->type, ic->code,
2150 !!(icmpinfo->invflags&IPT_ICMP_INV));
2151}
2152
2153/* Called when user tries to insert an entry of this type. */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07002154static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155icmp_checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -08002156 const void *info,
Patrick McHardyc4986732006-03-20 18:02:56 -08002157 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 unsigned int hook_mask)
2160{
2161 const struct ipt_icmp *icmpinfo = matchinfo;
2162
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002163 /* Must specify no unknown invflags */
2164 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
2167/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07002168static struct xt_target ipt_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 .name = IPT_STANDARD_TARGET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002170 .targetsize = sizeof(int),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002171 .family = AF_INET,
Dmitry Mishin27229712006-04-01 02:25:19 -08002172#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07002173 .compatsize = sizeof(compat_int_t),
2174 .compat_from_user = compat_standard_from_user,
2175 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002176#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177};
2178
Patrick McHardy9f15c532007-07-07 22:22:02 -07002179static struct xt_target ipt_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 .name = IPT_ERROR_TARGET,
2181 .target = ipt_error,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002182 .targetsize = IPT_FUNCTION_MAXNAMELEN,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002183 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184};
2185
2186static struct nf_sockopt_ops ipt_sockopts = {
2187 .pf = PF_INET,
2188 .set_optmin = IPT_BASE_CTL,
2189 .set_optmax = IPT_SO_SET_MAX+1,
2190 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002191#ifdef CONFIG_COMPAT
2192 .compat_set = compat_do_ipt_set_ctl,
2193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 .get_optmin = IPT_BASE_CTL,
2195 .get_optmax = IPT_SO_GET_MAX+1,
2196 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002197#ifdef CONFIG_COMPAT
2198 .compat_get = compat_do_ipt_get_ctl,
2199#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002200 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201};
2202
Patrick McHardy9f15c532007-07-07 22:22:02 -07002203static struct xt_match icmp_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 .name = "icmp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002205 .match = icmp_match,
2206 .matchsize = sizeof(struct ipt_icmp),
2207 .proto = IPPROTO_ICMP,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002208 .family = AF_INET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002209 .checkentry = icmp_checkentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210};
2211
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002212static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 int ret;
2215
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002216 ret = xt_proto_init(AF_INET);
2217 if (ret < 0)
2218 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002221 ret = xt_register_target(&ipt_standard_target);
2222 if (ret < 0)
2223 goto err2;
2224 ret = xt_register_target(&ipt_error_target);
2225 if (ret < 0)
2226 goto err3;
2227 ret = xt_register_match(&icmp_matchstruct);
2228 if (ret < 0)
2229 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 /* Register setsockopt */
2232 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002233 if (ret < 0)
2234 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Dan Aloni0236e662007-07-09 13:20:12 -07002236 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002238
2239err5:
2240 xt_unregister_match(&icmp_matchstruct);
2241err4:
2242 xt_unregister_target(&ipt_error_target);
2243err3:
2244 xt_unregister_target(&ipt_standard_target);
2245err2:
2246 xt_proto_fini(AF_INET);
2247err1:
2248 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002251static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002254
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002255 xt_unregister_match(&icmp_matchstruct);
2256 xt_unregister_target(&ipt_error_target);
2257 xt_unregister_target(&ipt_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002258
2259 xt_proto_fini(AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
2262EXPORT_SYMBOL(ipt_register_table);
2263EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002265module_init(ip_tables_init);
2266module_exit(ip_tables_fini);