blob: 231f5d290755b8202273b4b323438b5a07e9fe42 [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. */
Patrick McHardy9c547952007-12-17 21:52:00 -080077static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070078ip_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)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800105 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
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)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800119 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
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)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800132 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
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)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800141 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
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)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800149 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
Patrick McHardy9c547952007-12-17 21:52:00 -0800152 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
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",
Patrick McHardy9c547952007-12-17 21:52:00 -0800225 [NF_INET_FORWARD] = "FORWARD",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800226 [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];
Patrick McHardy9c547952007-12-17 21:52:00 -0800470 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (!(valid_hooks & (1 << hook)))
473 continue;
474
475 /* Set initial back pointer. */
476 e->counters.pcnt = pos;
477
478 for (;;) {
479 struct ipt_standard_target *t
480 = (void *)ipt_get_target(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800481 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800483 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 printk("iptables: loop hook %u pos %u %08X.\n",
485 hook, pos, e->comefrom);
486 return 0;
487 }
Patrick McHardy9c547952007-12-17 21:52:00 -0800488 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* Unconditional return/END. */
Al Viroe1b4b9f2006-12-12 00:29:52 -0800491 if ((e->target_offset == sizeof(struct ipt_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 && (strcmp(t->target.u.user.name,
493 IPT_STANDARD_TARGET) == 0)
494 && t->verdict < 0
Al Viroe1b4b9f2006-12-12 00:29:52 -0800495 && unconditional(&e->ip)) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 unsigned int oldpos, size;
497
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800498 if (t->verdict < -NF_MAX_VERDICT - 1) {
499 duprintf("mark_source_chains: bad "
500 "negative verdict (%i)\n",
501 t->verdict);
502 return 0;
503 }
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* Return: backtrack through the last
506 big jump. */
507 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800508 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509#ifdef DEBUG_IP_FIREWALL_USER
510 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800511 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 duprintf("Back unset "
513 "on hook %u "
514 "rule %u\n",
515 hook, pos);
516 }
517#endif
518 oldpos = pos;
519 pos = e->counters.pcnt;
520 e->counters.pcnt = 0;
521
522 /* We're at the start. */
523 if (pos == oldpos)
524 goto next;
525
526 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800527 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 } while (oldpos == pos + e->next_offset);
529
530 /* Move along one */
531 size = e->next_offset;
532 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800533 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 e->counters.pcnt = pos;
535 pos += size;
536 } else {
537 int newpos = t->verdict;
538
539 if (strcmp(t->target.u.user.name,
540 IPT_STANDARD_TARGET) == 0
541 && newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800542 if (newpos > newinfo->size -
543 sizeof(struct ipt_entry)) {
544 duprintf("mark_source_chains: "
545 "bad verdict (%i)\n",
546 newpos);
547 return 0;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* This a jump; chase it. */
550 duprintf("Jump rule %u -> %u\n",
551 pos, newpos);
552 } else {
553 /* ... this is a fallthru */
554 newpos = pos + e->next_offset;
555 }
556 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800557 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 e->counters.pcnt = pos;
559 pos = newpos;
560 }
561 }
562 next:
563 duprintf("Finished chain %u\n", hook);
564 }
565 return 1;
566}
567
568static inline int
569cleanup_match(struct ipt_entry_match *m, unsigned int *i)
570{
571 if (i && (*i)-- == 0)
572 return 1;
573
574 if (m->u.kernel.match->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700575 m->u.kernel.match->destroy(m->u.kernel.match, m->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 module_put(m->u.kernel.match->me);
577 return 0;
578}
579
580static inline int
Dmitry Mishina96be242006-12-12 00:29:26 -0800581check_entry(struct ipt_entry *e, const char *name)
582{
583 struct ipt_entry_target *t;
584
585 if (!ip_checkentry(&e->ip)) {
586 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
587 return -EINVAL;
588 }
589
Patrick McHardy9c547952007-12-17 21:52:00 -0800590 if (e->target_offset + sizeof(struct ipt_entry_target) >
591 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800592 return -EINVAL;
593
594 t = ipt_get_target(e);
595 if (e->target_offset + t->u.target_size > e->next_offset)
596 return -EINVAL;
597
598 return 0;
599}
600
601static inline int check_match(struct ipt_entry_match *m, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -0800602 const struct ipt_ip *ip,
603 unsigned int hookmask, unsigned int *i)
Dmitry Mishina96be242006-12-12 00:29:26 -0800604{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800605 struct xt_match *match;
Dmitry Mishina96be242006-12-12 00:29:26 -0800606 int ret;
607
608 match = m->u.kernel.match;
609 ret = xt_check_match(match, AF_INET, m->u.match_size - sizeof(*m),
610 name, hookmask, ip->proto,
611 ip->invflags & IPT_INV_PROTO);
612 if (!ret && m->u.kernel.match->checkentry
613 && !m->u.kernel.match->checkentry(name, ip, match, m->data,
614 hookmask)) {
615 duprintf("ip_tables: check failed for `%s'.\n",
616 m->u.kernel.match->name);
617 ret = -EINVAL;
618 }
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -0700619 if (!ret)
620 (*i)++;
Dmitry Mishina96be242006-12-12 00:29:26 -0800621 return ret;
622}
623
624static inline int
625find_check_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -0800626 const char *name,
627 const struct ipt_ip *ip,
628 unsigned int hookmask,
629 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800631 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800632 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Harald Welte2e4e6a12006-01-12 13:30:04 -0800634 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy9c547952007-12-17 21:52:00 -0800635 m->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 "ipt_%s", m->u.user.name);
637 if (IS_ERR(match) || !match) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800638 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return match ? PTR_ERR(match) : -ENOENT;
640 }
641 m->u.kernel.match = match;
642
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -0700643 ret = check_match(m, name, ip, hookmask, i);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800644 if (ret)
645 goto err;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800648err:
649 module_put(m->u.kernel.match->me);
650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Dmitry Mishina96be242006-12-12 00:29:26 -0800653static inline int check_target(struct ipt_entry *e, const char *name)
654{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900655 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800656 struct xt_target *target;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900657 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800658
659 t = ipt_get_target(e);
660 target = t->u.kernel.target;
661 ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
662 name, e->comefrom, e->ip.proto,
663 e->ip.invflags & IPT_INV_PROTO);
664 if (!ret && t->u.kernel.target->checkentry
Patrick McHardy4b478242007-12-17 21:46:15 -0800665 && !t->u.kernel.target->checkentry(name, e, target, t->data,
666 e->comefrom)) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800667 duprintf("ip_tables: check failed for `%s'.\n",
668 t->u.kernel.target->name);
669 ret = -EINVAL;
670 }
671 return ret;
672}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674static inline int
Dmitry Mishina96be242006-12-12 00:29:26 -0800675find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
Patrick McHardy4b478242007-12-17 21:46:15 -0800676 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800679 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int ret;
681 unsigned int j;
682
Dmitry Mishina96be242006-12-12 00:29:26 -0800683 ret = check_entry(e, name);
684 if (ret)
685 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 j = 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800688 ret = IPT_MATCH_ITERATE(e, find_check_match, name, &e->ip,
Patrick McHardy4b478242007-12-17 21:46:15 -0800689 e->comefrom, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (ret != 0)
691 goto cleanup_matches;
692
693 t = ipt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800694 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -0800695 t->u.user.name,
696 t->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 "ipt_%s", t->u.user.name);
698 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800699 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 ret = target ? PTR_ERR(target) : -ENOENT;
701 goto cleanup_matches;
702 }
703 t->u.kernel.target = target;
704
Dmitry Mishina96be242006-12-12 00:29:26 -0800705 ret = check_target(e, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800706 if (ret)
707 goto err;
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 (*i)++;
710 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800711 err:
712 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 cleanup_matches:
714 IPT_MATCH_ITERATE(e, cleanup_match, &j);
715 return ret;
716}
717
718static inline int
719check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800720 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 unsigned char *base,
722 unsigned char *limit,
723 const unsigned int *hook_entries,
724 const unsigned int *underflows,
725 unsigned int *i)
726{
727 unsigned int h;
728
729 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0
730 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
731 duprintf("Bad offset %p\n", e);
732 return -EINVAL;
733 }
734
735 if (e->next_offset
736 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
737 duprintf("checking: element %p size %u\n",
738 e, e->next_offset);
739 return -EINVAL;
740 }
741
742 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800743 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if ((unsigned char *)e - base == hook_entries[h])
745 newinfo->hook_entry[h] = hook_entries[h];
746 if ((unsigned char *)e - base == underflows[h])
747 newinfo->underflow[h] = underflows[h];
748 }
749
750 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900751 < 0 (not IPT_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800754 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 e->comefrom = 0;
756
757 (*i)++;
758 return 0;
759}
760
761static inline int
762cleanup_entry(struct ipt_entry *e, unsigned int *i)
763{
764 struct ipt_entry_target *t;
765
766 if (i && (*i)-- == 0)
767 return 1;
768
769 /* Cleanup all matches */
770 IPT_MATCH_ITERATE(e, cleanup_match, NULL);
771 t = ipt_get_target(e);
772 if (t->u.kernel.target->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700773 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 module_put(t->u.kernel.target->me);
775 return 0;
776}
777
778/* Checks and translates the user-supplied table segment (held in
779 newinfo) */
780static int
781translate_table(const char *name,
782 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800783 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800784 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 unsigned int size,
786 unsigned int number,
787 const unsigned int *hook_entries,
788 const unsigned int *underflows)
789{
790 unsigned int i;
791 int ret;
792
793 newinfo->size = size;
794 newinfo->number = number;
795
796 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800797 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 newinfo->hook_entry[i] = 0xFFFFFFFF;
799 newinfo->underflow[i] = 0xFFFFFFFF;
800 }
801
802 duprintf("translate_table: size %u\n", newinfo->size);
803 i = 0;
804 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800805 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 check_entry_size_and_hooks,
807 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800808 entry0,
809 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 hook_entries, underflows, &i);
811 if (ret != 0)
812 return ret;
813
814 if (i != number) {
815 duprintf("translate_table: %u not %u entries\n",
816 i, number);
817 return -EINVAL;
818 }
819
820 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800821 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /* Only hooks which are valid */
823 if (!(valid_hooks & (1 << i)))
824 continue;
825 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
826 duprintf("Invalid hook entry %u %u\n",
827 i, hook_entries[i]);
828 return -EINVAL;
829 }
830 if (newinfo->underflow[i] == 0xFFFFFFFF) {
831 duprintf("Invalid underflow %u %u\n",
832 i, underflows[i]);
833 return -EINVAL;
834 }
835 }
836
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800837 if (!mark_source_chains(newinfo, valid_hooks, entry0))
838 return -ELOOP;
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /* Finally, each sanity check must pass */
841 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800842 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Dmitry Mishina96be242006-12-12 00:29:26 -0800843 find_check_entry, name, size, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800845 if (ret != 0) {
846 IPT_ENTRY_ITERATE(entry0, newinfo->size,
847 cleanup_entry, &i);
848 return ret;
849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700852 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800853 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
854 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 return ret;
858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860/* Gets counters. */
861static inline int
862add_entry_to_counter(const struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800863 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 unsigned int *i)
865{
866 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
867
868 (*i)++;
869 return 0;
870}
871
Eric Dumazet31836062005-12-13 23:13:48 -0800872static inline int
873set_entry_to_counter(const struct ipt_entry *e,
874 struct ipt_counters total[],
875 unsigned int *i)
876{
877 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
878
879 (*i)++;
880 return 0;
881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800884get_counters(const struct xt_table_info *t,
885 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 unsigned int cpu;
888 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800889 unsigned int curcpu;
890
891 /* Instead of clearing (by a previous call to memset())
892 * the counters and using adds, we set the counters
893 * with data used by 'current' CPU
894 * We dont care about preemption here.
895 */
896 curcpu = raw_smp_processor_id();
897
898 i = 0;
899 IPT_ENTRY_ITERATE(t->entries[curcpu],
900 t->size,
901 set_entry_to_counter,
902 counters,
903 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700905 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800906 if (cpu == curcpu)
907 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800909 IPT_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 t->size,
911 add_entry_to_counter,
912 counters,
913 &i);
914 }
915}
916
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800917static inline struct xt_counters * alloc_counters(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Dmitry Mishin27229712006-04-01 02:25:19 -0800919 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800920 struct xt_counters *counters;
921 struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* We need atomic snapshot of counters: rest doesn't change
924 (other than comefrom, which userspace doesn't care
925 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800926 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet31836062005-12-13 23:13:48 -0800927 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (counters == NULL)
Dmitry Mishin27229712006-04-01 02:25:19 -0800930 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 write_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800934 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 write_unlock_bh(&table->lock);
936
Dmitry Mishin27229712006-04-01 02:25:19 -0800937 return counters;
938}
939
940static int
941copy_entries_to_user(unsigned int total_size,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800942 struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800943 void __user *userptr)
944{
945 unsigned int off, num;
946 struct ipt_entry *e;
947 struct xt_counters *counters;
948 struct xt_table_info *private = table->private;
949 int ret = 0;
950 void *loc_cpu_entry;
951
952 counters = alloc_counters(table);
953 if (IS_ERR(counters))
954 return PTR_ERR(counters);
955
Eric Dumazet31836062005-12-13 23:13:48 -0800956 /* choose the copy that is on our node/cpu, ...
957 * This choice is lazy (because current thread is
958 * allowed to migrate to another cpu)
959 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800960 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800961 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 ret = -EFAULT;
963 goto free_counters;
964 }
965
966 /* FIXME: use iterator macros --RR */
967 /* ... then go back and fix counters and names */
968 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
969 unsigned int i;
970 struct ipt_entry_match *m;
971 struct ipt_entry_target *t;
972
Eric Dumazet31836062005-12-13 23:13:48 -0800973 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (copy_to_user(userptr + off
975 + offsetof(struct ipt_entry, counters),
976 &counters[num],
977 sizeof(counters[num])) != 0) {
978 ret = -EFAULT;
979 goto free_counters;
980 }
981
982 for (i = sizeof(struct ipt_entry);
983 i < e->target_offset;
984 i += m->u.match_size) {
985 m = (void *)e + i;
986
987 if (copy_to_user(userptr + off + i
988 + offsetof(struct ipt_entry_match,
989 u.user.name),
990 m->u.kernel.match->name,
991 strlen(m->u.kernel.match->name)+1)
992 != 0) {
993 ret = -EFAULT;
994 goto free_counters;
995 }
996 }
997
998 t = ipt_get_target(e);
999 if (copy_to_user(userptr + off + e->target_offset
1000 + offsetof(struct ipt_entry_target,
1001 u.user.name),
1002 t->u.kernel.target->name,
1003 strlen(t->u.kernel.target->name)+1) != 0) {
1004 ret = -EFAULT;
1005 goto free_counters;
1006 }
1007 }
1008
1009 free_counters:
1010 vfree(counters);
1011 return ret;
1012}
1013
Dmitry Mishin27229712006-04-01 02:25:19 -08001014#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001015static void compat_standard_from_user(void *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001016{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001017 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001018
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001019 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001020 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001021 memcpy(dst, &v, sizeof(v));
1022}
1023
1024static int compat_standard_to_user(void __user *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001025{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001026 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001027
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001028 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001029 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001030 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001031}
1032
1033static inline int
Patrick McHardy4b478242007-12-17 21:46:15 -08001034compat_calc_match(struct ipt_entry_match *m, int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001035{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001036 *size += xt_compat_match_offset(m->u.kernel.match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001037 return 0;
1038}
1039
Eric Dumazet259d4e42007-12-04 23:24:56 -08001040static int compat_calc_entry(struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001041 const struct xt_table_info *info,
1042 void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001043{
1044 struct ipt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001045 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001046 int off, i, ret;
1047
Patrick McHardy30c08c42007-12-17 21:47:14 -08001048 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001049 entry_offset = (void *)e - base;
1050 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
1051 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001052 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001053 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001054 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001055 if (ret)
1056 return ret;
1057
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001058 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001059 if (info->hook_entry[i] &&
1060 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001061 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001062 if (info->underflow[i] &&
1063 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001064 newinfo->underflow[i] -= off;
1065 }
1066 return 0;
1067}
1068
Eric Dumazet259d4e42007-12-04 23:24:56 -08001069static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001070 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001071{
1072 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001073
1074 if (!newinfo || !info)
1075 return -EINVAL;
1076
Eric Dumazet259d4e42007-12-04 23:24:56 -08001077 /* we dont care about newinfo->entries[] */
1078 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1079 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001080 loc_cpu_entry = info->entries[raw_smp_processor_id()];
1081 return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001082 compat_calc_entry, info, loc_cpu_entry,
1083 newinfo);
Dmitry Mishin27229712006-04-01 02:25:19 -08001084}
1085#endif
1086
1087static int get_info(void __user *user, int *len, int compat)
1088{
1089 char name[IPT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001090 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001091 int ret;
1092
1093 if (*len != sizeof(struct ipt_getinfo)) {
1094 duprintf("length %u != %u\n", *len,
1095 (unsigned int)sizeof(struct ipt_getinfo));
1096 return -EINVAL;
1097 }
1098
1099 if (copy_from_user(name, user, sizeof(name)) != 0)
1100 return -EFAULT;
1101
1102 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1103#ifdef CONFIG_COMPAT
1104 if (compat)
1105 xt_compat_lock(AF_INET);
1106#endif
1107 t = try_then_request_module(xt_find_table_lock(AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001108 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001109 if (t && !IS_ERR(t)) {
1110 struct ipt_getinfo info;
1111 struct xt_table_info *private = t->private;
1112
1113#ifdef CONFIG_COMPAT
1114 if (compat) {
1115 struct xt_table_info tmp;
1116 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001117 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001118 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001119 }
1120#endif
1121 info.valid_hooks = t->valid_hooks;
1122 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001123 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001124 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001125 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001126 info.num_entries = private->number;
1127 info.size = private->size;
1128 strcpy(info.name, name);
1129
1130 if (copy_to_user(user, &info, *len) != 0)
1131 ret = -EFAULT;
1132 else
1133 ret = 0;
1134
1135 xt_table_unlock(t);
1136 module_put(t->me);
1137 } else
1138 ret = t ? PTR_ERR(t) : -ENOENT;
1139#ifdef CONFIG_COMPAT
1140 if (compat)
1141 xt_compat_unlock(AF_INET);
1142#endif
1143 return ret;
1144}
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001147get_entries(struct ipt_get_entries __user *uptr, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001150 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001151 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Dmitry Mishin27229712006-04-01 02:25:19 -08001153 if (*len < sizeof(get)) {
1154 duprintf("get_entries: %u < %d\n", *len,
1155 (unsigned int)sizeof(get));
1156 return -EINVAL;
1157 }
1158 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1159 return -EFAULT;
1160 if (*len != sizeof(struct ipt_get_entries) + get.size) {
1161 duprintf("get_entries: %u != %u\n", *len,
1162 (unsigned int)(sizeof(struct ipt_get_entries) +
1163 get.size));
1164 return -EINVAL;
1165 }
1166
1167 t = xt_find_table_lock(AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (t && !IS_ERR(t)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001169 struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001170 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001171 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001172 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 t, uptr->entrytable);
1174 else {
1175 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001176 private->size, get.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 ret = -EINVAL;
1178 }
1179 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001180 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 } else
1182 ret = t ? PTR_ERR(t) : -ENOENT;
1183
1184 return ret;
1185}
1186
1187static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001188__do_replace(const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001189 struct xt_table_info *newinfo, unsigned int num_counters,
1190 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001191{
1192 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001193 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001194 struct xt_table_info *oldinfo;
1195 struct xt_counters *counters;
1196 void *loc_cpu_old_entry;
1197
1198 ret = 0;
1199 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1200 if (!counters) {
1201 ret = -ENOMEM;
1202 goto out;
1203 }
1204
1205 t = try_then_request_module(xt_find_table_lock(AF_INET, name),
1206 "iptable_%s", name);
1207 if (!t || IS_ERR(t)) {
1208 ret = t ? PTR_ERR(t) : -ENOENT;
1209 goto free_newinfo_counters_untrans;
1210 }
1211
1212 /* You lied! */
1213 if (valid_hooks != t->valid_hooks) {
1214 duprintf("Valid hook crap: %08X vs %08X\n",
1215 valid_hooks, t->valid_hooks);
1216 ret = -EINVAL;
1217 goto put_module;
1218 }
1219
1220 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1221 if (!oldinfo)
1222 goto put_module;
1223
1224 /* Update module usage count based on number of rules */
1225 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1226 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1227 if ((oldinfo->number > oldinfo->initial_entries) ||
1228 (newinfo->number <= oldinfo->initial_entries))
1229 module_put(t->me);
1230 if ((oldinfo->number > oldinfo->initial_entries) &&
1231 (newinfo->number <= oldinfo->initial_entries))
1232 module_put(t->me);
1233
1234 /* Get the old counters. */
1235 get_counters(oldinfo, counters);
1236 /* Decrease module usage counts and free resource */
1237 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Patrick McHardy4b478242007-12-17 21:46:15 -08001238 IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1239 NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001240 xt_free_table_info(oldinfo);
1241 if (copy_to_user(counters_ptr, counters,
1242 sizeof(struct xt_counters) * num_counters) != 0)
1243 ret = -EFAULT;
1244 vfree(counters);
1245 xt_table_unlock(t);
1246 return ret;
1247
1248 put_module:
1249 module_put(t->me);
1250 xt_table_unlock(t);
1251 free_newinfo_counters_untrans:
1252 vfree(counters);
1253 out:
1254 return ret;
1255}
1256
1257static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258do_replace(void __user *user, unsigned int len)
1259{
1260 int ret;
1261 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001262 struct xt_table_info *newinfo;
1263 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1266 return -EFAULT;
1267
1268 /* Hack: Causes ipchains to give correct error msg --RR */
1269 if (len != sizeof(tmp) + tmp.size)
1270 return -ENOPROTOOPT;
1271
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001272 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001273 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1274 return -ENOMEM;
1275
Harald Welte2e4e6a12006-01-12 13:30:04 -08001276 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (!newinfo)
1278 return -ENOMEM;
1279
Patrick McHardy9c547952007-12-17 21:52:00 -08001280 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001281 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1282 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 tmp.size) != 0) {
1284 ret = -EFAULT;
1285 goto free_newinfo;
1286 }
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001289 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 tmp.hook_entry, tmp.underflow);
1291 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001292 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 duprintf("ip_tables: Translated table\n");
1295
Patrick McHardy4b478242007-12-17 21:46:15 -08001296 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1297 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001298 if (ret)
1299 goto free_newinfo_untrans;
1300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Dmitry Mishin27229712006-04-01 02:25:19 -08001302 free_newinfo_untrans:
Patrick McHardy9c547952007-12-17 21:52:00 -08001303 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001305 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return ret;
1307}
1308
1309/* We're lazy, and add to the first CPU; overflow works its fey magic
1310 * and everything is OK. */
1311static inline int
1312add_counter_to_entry(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001313 const struct xt_counters addme[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 unsigned int *i)
1315{
1316#if 0
1317 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1318 *i,
1319 (long unsigned int)e->counters.pcnt,
1320 (long unsigned int)e->counters.bcnt,
1321 (long unsigned int)addme[*i].pcnt,
1322 (long unsigned int)addme[*i].bcnt);
1323#endif
1324
1325 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1326
1327 (*i)++;
1328 return 0;
1329}
1330
1331static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001332do_add_counters(void __user *user, unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 unsigned int i;
Dmitry Mishin27229712006-04-01 02:25:19 -08001335 struct xt_counters_info tmp;
1336 struct xt_counters *paddc;
1337 unsigned int num_counters;
1338 char *name;
1339 int size;
1340 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001341 struct xt_table *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001342 struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001344 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001345#ifdef CONFIG_COMPAT
1346 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Dmitry Mishin27229712006-04-01 02:25:19 -08001348 if (compat) {
1349 ptmp = &compat_tmp;
1350 size = sizeof(struct compat_xt_counters_info);
1351 } else
1352#endif
1353 {
1354 ptmp = &tmp;
1355 size = sizeof(struct xt_counters_info);
1356 }
1357
1358 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return -EFAULT;
1360
Dmitry Mishin27229712006-04-01 02:25:19 -08001361#ifdef CONFIG_COMPAT
1362 if (compat) {
1363 num_counters = compat_tmp.num_counters;
1364 name = compat_tmp.name;
1365 } else
1366#endif
1367 {
1368 num_counters = tmp.num_counters;
1369 name = tmp.name;
1370 }
1371
1372 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return -EINVAL;
1374
Dmitry Mishin27229712006-04-01 02:25:19 -08001375 paddc = vmalloc_node(len - size, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (!paddc)
1377 return -ENOMEM;
1378
Dmitry Mishin27229712006-04-01 02:25:19 -08001379 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 ret = -EFAULT;
1381 goto free;
1382 }
1383
Dmitry Mishin27229712006-04-01 02:25:19 -08001384 t = xt_find_table_lock(AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (!t || IS_ERR(t)) {
1386 ret = t ? PTR_ERR(t) : -ENOENT;
1387 goto free;
1388 }
1389
1390 write_lock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001391 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001392 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 ret = -EINVAL;
1394 goto unlock_up_free;
1395 }
1396
1397 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001398 /* Choose the copy that is on our node */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001399 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -08001400 IPT_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001401 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 add_counter_to_entry,
Dmitry Mishin27229712006-04-01 02:25:19 -08001403 paddc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 &i);
1405 unlock_up_free:
1406 write_unlock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001407 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 module_put(t->me);
1409 free:
1410 vfree(paddc);
1411
1412 return ret;
1413}
1414
Dmitry Mishin27229712006-04-01 02:25:19 -08001415#ifdef CONFIG_COMPAT
1416struct compat_ipt_replace {
1417 char name[IPT_TABLE_MAXNAMELEN];
1418 u32 valid_hooks;
1419 u32 num_entries;
1420 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001421 u32 hook_entry[NF_INET_NUMHOOKS];
1422 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001423 u32 num_counters;
1424 compat_uptr_t counters; /* struct ipt_counters * */
1425 struct compat_ipt_entry entries[0];
1426};
1427
Patrick McHardya18aa312007-12-12 10:35:16 -08001428static int
1429compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
1430 compat_uint_t *size, struct xt_counters *counters,
1431 unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001432{
Al Viro3e597c62006-09-24 23:42:20 +01001433 struct ipt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001434 struct compat_ipt_entry __user *ce;
1435 u_int16_t target_offset, next_offset;
1436 compat_uint_t origsize;
1437 int ret;
1438
1439 ret = -EFAULT;
1440 origsize = *size;
1441 ce = (struct compat_ipt_entry __user *)*dstptr;
Patrick McHardy78000072006-05-03 23:20:27 -07001442 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
Dmitry Mishin27229712006-04-01 02:25:19 -08001443 goto out;
1444
Patrick McHardya18aa312007-12-12 10:35:16 -08001445 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1446 goto out;
1447
Dmitry Mishin27229712006-04-01 02:25:19 -08001448 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001449 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1450
Patrick McHardyac8e27f2007-12-17 21:45:52 -08001451 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001452 target_offset = e->target_offset - (origsize - *size);
1453 if (ret)
1454 goto out;
1455 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001456 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001457 if (ret)
1458 goto out;
1459 ret = -EFAULT;
1460 next_offset = e->next_offset - (origsize - *size);
Patrick McHardy78000072006-05-03 23:20:27 -07001461 if (put_user(target_offset, &ce->target_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001462 goto out;
Patrick McHardy78000072006-05-03 23:20:27 -07001463 if (put_user(next_offset, &ce->next_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001464 goto out;
Patrick McHardya18aa312007-12-12 10:35:16 -08001465
1466 (*i)++;
Dmitry Mishin27229712006-04-01 02:25:19 -08001467 return 0;
1468out:
1469 return ret;
1470}
1471
1472static inline int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001473compat_find_calc_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001474 const char *name,
1475 const struct ipt_ip *ip,
1476 unsigned int hookmask,
1477 int *size, int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001478{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001479 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001480
1481 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001482 m->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001483 "ipt_%s", m->u.user.name);
1484 if (IS_ERR(match) || !match) {
1485 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001486 m->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001487 return match ? PTR_ERR(match) : -ENOENT;
1488 }
1489 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001490 *size += xt_compat_match_offset(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001491
1492 (*i)++;
1493 return 0;
1494}
1495
1496static inline int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001497compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1498{
1499 if (i && (*i)-- == 0)
1500 return 1;
1501
1502 module_put(m->u.kernel.match->me);
1503 return 0;
1504}
1505
1506static inline int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001507compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001508{
1509 struct ipt_entry_target *t;
1510
1511 if (i && (*i)-- == 0)
1512 return 1;
1513
1514 /* Cleanup all matches */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001515 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1516 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001517 module_put(t->u.kernel.target->me);
1518 return 0;
1519}
1520
1521static inline int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001522check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001523 struct xt_table_info *newinfo,
1524 unsigned int *size,
1525 unsigned char *base,
1526 unsigned char *limit,
1527 unsigned int *hook_entries,
1528 unsigned int *underflows,
1529 unsigned int *i,
1530 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001531{
1532 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001533 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001534 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001535 int ret, off, h, j;
1536
1537 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1538 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0
1539 || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1540 duprintf("Bad offset %p, limit = %p\n", e, limit);
1541 return -EINVAL;
1542 }
1543
1544 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001545 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001546 duprintf("checking: element %p size %u\n",
1547 e, e->next_offset);
1548 return -EINVAL;
1549 }
1550
Patrick McHardy73cd5982007-12-17 21:47:32 -08001551 /* For purposes of check_entry casting the compat entry is fine */
1552 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001553 if (ret)
1554 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001555
Patrick McHardy30c08c42007-12-17 21:47:14 -08001556 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001557 entry_offset = (void *)e - (void *)base;
1558 j = 0;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001559 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1560 &e->ip, e->comefrom, &off, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001561 if (ret != 0)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001562 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001563
Patrick McHardy73cd5982007-12-17 21:47:32 -08001564 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001565 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -08001566 t->u.user.name,
1567 t->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001568 "ipt_%s", t->u.user.name);
1569 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001570 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001571 t->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001572 ret = target ? PTR_ERR(target) : -ENOENT;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001573 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001574 }
1575 t->u.kernel.target = target;
1576
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001577 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001578 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001579 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001580 if (ret)
1581 goto out;
1582
1583 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001584 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001585 if ((unsigned char *)e - base == hook_entries[h])
1586 newinfo->hook_entry[h] = hook_entries[h];
1587 if ((unsigned char *)e - base == underflows[h])
1588 newinfo->underflow[h] = underflows[h];
1589 }
1590
1591 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001592 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001593 e->comefrom = 0;
1594
1595 (*i)++;
1596 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001597
Dmitry Mishin27229712006-04-01 02:25:19 -08001598out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001599 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001600release_matches:
1601 IPT_MATCH_ITERATE(e, compat_release_match, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001602 return ret;
1603}
1604
Patrick McHardy4b478242007-12-17 21:46:15 -08001605static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001606compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001607 unsigned int *size, const char *name,
1608 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001609{
1610 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001611 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001612 struct ipt_entry *de;
1613 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001614 int ret, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001615
1616 ret = 0;
1617 origsize = *size;
1618 de = (struct ipt_entry *)*dstptr;
1619 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001620 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001621
Patrick McHardy73cd5982007-12-17 21:47:32 -08001622 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001623 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1624
Patrick McHardy73cd5982007-12-17 21:47:32 -08001625 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1626 dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001627 if (ret)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001628 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001629 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001630 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001631 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001632 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001633
1634 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001635 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001636 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1637 newinfo->hook_entry[h] -= origsize - *size;
1638 if ((unsigned char *)de - base < newinfo->underflow[h])
1639 newinfo->underflow[h] -= origsize - *size;
1640 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001641 return ret;
1642}
Dmitry Mishin27229712006-04-01 02:25:19 -08001643
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001644static inline int compat_check_entry(struct ipt_entry *e, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001645 unsigned int *i)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001646{
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001647 int j, ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001648
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001649 j = 0;
Patrick McHardy9c547952007-12-17 21:52:00 -08001650 ret = IPT_MATCH_ITERATE(e, check_match, name, &e->ip,
1651 e->comefrom, &j);
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001652 if (ret)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001653 goto cleanup_matches;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001654
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001655 ret = check_target(e, name);
1656 if (ret)
1657 goto cleanup_matches;
1658
1659 (*i)++;
1660 return 0;
1661
1662 cleanup_matches:
1663 IPT_MATCH_ITERATE(e, cleanup_match, &j);
1664 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001665}
1666
Dmitry Mishin27229712006-04-01 02:25:19 -08001667static int
1668translate_compat_table(const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001669 unsigned int valid_hooks,
1670 struct xt_table_info **pinfo,
1671 void **pentry0,
1672 unsigned int total_size,
1673 unsigned int number,
1674 unsigned int *hook_entries,
1675 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001676{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001677 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001678 struct xt_table_info *newinfo, *info;
1679 void *pos, *entry0, *entry1;
1680 unsigned int size;
1681 int ret;
1682
1683 info = *pinfo;
1684 entry0 = *pentry0;
1685 size = total_size;
1686 info->number = number;
1687
1688 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001689 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001690 info->hook_entry[i] = 0xFFFFFFFF;
1691 info->underflow[i] = 0xFFFFFFFF;
1692 }
1693
1694 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001695 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001696 xt_compat_lock(AF_INET);
1697 /* Walk through entries, checking offsets. */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001698 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1699 check_compat_entry_size_and_hooks,
1700 info, &size, entry0,
1701 entry0 + total_size,
1702 hook_entries, underflows, &j, name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001703 if (ret != 0)
1704 goto out_unlock;
1705
1706 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001707 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001708 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001709 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001710 goto out_unlock;
1711 }
1712
1713 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001714 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001715 /* Only hooks which are valid */
1716 if (!(valid_hooks & (1 << i)))
1717 continue;
1718 if (info->hook_entry[i] == 0xFFFFFFFF) {
1719 duprintf("Invalid hook entry %u %u\n",
1720 i, hook_entries[i]);
1721 goto out_unlock;
1722 }
1723 if (info->underflow[i] == 0xFFFFFFFF) {
1724 duprintf("Invalid underflow %u %u\n",
1725 i, underflows[i]);
1726 goto out_unlock;
1727 }
1728 }
1729
1730 ret = -ENOMEM;
1731 newinfo = xt_alloc_table_info(size);
1732 if (!newinfo)
1733 goto out_unlock;
1734
1735 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001736 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001737 newinfo->hook_entry[i] = info->hook_entry[i];
1738 newinfo->underflow[i] = info->underflow[i];
1739 }
1740 entry1 = newinfo->entries[raw_smp_processor_id()];
1741 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001742 size = total_size;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001743 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
Patrick McHardy9c547952007-12-17 21:52:00 -08001744 compat_copy_entry_from_user,
1745 &pos, &size, name, newinfo, entry1);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001746 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001747 xt_compat_unlock(AF_INET);
1748 if (ret)
1749 goto free_newinfo;
1750
1751 ret = -ELOOP;
1752 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1753 goto free_newinfo;
1754
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001755 i = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001756 ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001757 name, &i);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001758 if (ret) {
1759 j -= i;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001760 COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1761 compat_release_entry, &j);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001762 IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1763 xt_free_table_info(newinfo);
1764 return ret;
1765 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001766
Dmitry Mishin27229712006-04-01 02:25:19 -08001767 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001768 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001769 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1770 memcpy(newinfo->entries[i], entry1, newinfo->size);
1771
1772 *pinfo = newinfo;
1773 *pentry0 = entry1;
1774 xt_free_table_info(info);
1775 return 0;
1776
1777free_newinfo:
1778 xt_free_table_info(newinfo);
1779out:
Patrick McHardy73cd5982007-12-17 21:47:32 -08001780 COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001781 return ret;
1782out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001783 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001784 xt_compat_unlock(AF_INET);
1785 goto out;
1786}
1787
1788static int
1789compat_do_replace(void __user *user, unsigned int len)
1790{
1791 int ret;
1792 struct compat_ipt_replace tmp;
1793 struct xt_table_info *newinfo;
1794 void *loc_cpu_entry;
1795
1796 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1797 return -EFAULT;
1798
1799 /* Hack: Causes ipchains to give correct error msg --RR */
1800 if (len != sizeof(tmp) + tmp.size)
1801 return -ENOPROTOOPT;
1802
1803 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001804 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001805 return -ENOMEM;
1806 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1807 return -ENOMEM;
1808
1809 newinfo = xt_alloc_table_info(tmp.size);
1810 if (!newinfo)
1811 return -ENOMEM;
1812
Patrick McHardy9c547952007-12-17 21:52:00 -08001813 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001814 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1815 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1816 tmp.size) != 0) {
1817 ret = -EFAULT;
1818 goto free_newinfo;
1819 }
1820
1821 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001822 &newinfo, &loc_cpu_entry, tmp.size,
1823 tmp.num_entries, tmp.hook_entry,
1824 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001825 if (ret != 0)
1826 goto free_newinfo;
1827
1828 duprintf("compat_do_replace: Translated table\n");
1829
Patrick McHardy4b478242007-12-17 21:46:15 -08001830 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1831 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001832 if (ret)
1833 goto free_newinfo_untrans;
1834 return 0;
1835
1836 free_newinfo_untrans:
Patrick McHardy4b478242007-12-17 21:46:15 -08001837 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001838 free_newinfo:
1839 xt_free_table_info(newinfo);
1840 return ret;
1841}
1842
1843static int
1844compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001845 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001846{
1847 int ret;
1848
1849 if (!capable(CAP_NET_ADMIN))
1850 return -EPERM;
1851
1852 switch (cmd) {
1853 case IPT_SO_SET_REPLACE:
1854 ret = compat_do_replace(user, len);
1855 break;
1856
1857 case IPT_SO_SET_ADD_COUNTERS:
1858 ret = do_add_counters(user, len, 1);
1859 break;
1860
1861 default:
1862 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1863 ret = -EINVAL;
1864 }
1865
1866 return ret;
1867}
1868
Patrick McHardy4b478242007-12-17 21:46:15 -08001869struct compat_ipt_get_entries {
Dmitry Mishin27229712006-04-01 02:25:19 -08001870 char name[IPT_TABLE_MAXNAMELEN];
1871 compat_uint_t size;
1872 struct compat_ipt_entry entrytable[0];
1873};
1874
Patrick McHardy4b478242007-12-17 21:46:15 -08001875static int
1876compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1877 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001878{
Dmitry Mishin27229712006-04-01 02:25:19 -08001879 struct xt_counters *counters;
1880 struct xt_table_info *private = table->private;
1881 void __user *pos;
1882 unsigned int size;
1883 int ret = 0;
1884 void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001885 unsigned int i = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001886
1887 counters = alloc_counters(table);
1888 if (IS_ERR(counters))
1889 return PTR_ERR(counters);
1890
1891 /* choose the copy that is on our node/cpu, ...
1892 * This choice is lazy (because current thread is
1893 * allowed to migrate to another cpu)
1894 */
1895 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1896 pos = userptr;
1897 size = total_size;
1898 ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
Patrick McHardya18aa312007-12-12 10:35:16 -08001899 compat_copy_entry_to_user,
1900 &pos, &size, counters, &i);
Dmitry Mishin27229712006-04-01 02:25:19 -08001901
Dmitry Mishin27229712006-04-01 02:25:19 -08001902 vfree(counters);
1903 return ret;
1904}
1905
1906static int
1907compat_get_entries(struct compat_ipt_get_entries __user *uptr, int *len)
1908{
1909 int ret;
1910 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001911 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001912
Dmitry Mishin27229712006-04-01 02:25:19 -08001913 if (*len < sizeof(get)) {
1914 duprintf("compat_get_entries: %u < %u\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001915 *len, (unsigned int)sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001916 return -EINVAL;
1917 }
1918
1919 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1920 return -EFAULT;
1921
1922 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
1923 duprintf("compat_get_entries: %u != %u\n", *len,
Patrick McHardy4b478242007-12-17 21:46:15 -08001924 (unsigned int)(sizeof(struct compat_ipt_get_entries) +
1925 get.size));
Dmitry Mishin27229712006-04-01 02:25:19 -08001926 return -EINVAL;
1927 }
1928
1929 xt_compat_lock(AF_INET);
1930 t = xt_find_table_lock(AF_INET, get.name);
1931 if (t && !IS_ERR(t)) {
1932 struct xt_table_info *private = t->private;
1933 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08001934 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001935 ret = compat_table_info(private, &info);
1936 if (!ret && get.size == info.size) {
1937 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001938 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001939 } else if (!ret) {
1940 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001941 private->size, get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001942 ret = -EINVAL;
1943 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001944 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001945 module_put(t->me);
1946 xt_table_unlock(t);
1947 } else
1948 ret = t ? PTR_ERR(t) : -ENOENT;
1949
1950 xt_compat_unlock(AF_INET);
1951 return ret;
1952}
1953
Patrick McHardy79030ed2006-09-20 12:05:08 -07001954static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1955
Dmitry Mishin27229712006-04-01 02:25:19 -08001956static int
1957compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1958{
1959 int ret;
1960
Björn Steinbrink82fac052006-10-20 00:21:10 -07001961 if (!capable(CAP_NET_ADMIN))
1962 return -EPERM;
1963
Dmitry Mishin27229712006-04-01 02:25:19 -08001964 switch (cmd) {
1965 case IPT_SO_GET_INFO:
1966 ret = get_info(user, len, 1);
1967 break;
1968 case IPT_SO_GET_ENTRIES:
1969 ret = compat_get_entries(user, len);
1970 break;
1971 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001972 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001973 }
1974 return ret;
1975}
1976#endif
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001979do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 int ret;
1982
1983 if (!capable(CAP_NET_ADMIN))
1984 return -EPERM;
1985
1986 switch (cmd) {
1987 case IPT_SO_SET_REPLACE:
1988 ret = do_replace(user, len);
1989 break;
1990
1991 case IPT_SO_SET_ADD_COUNTERS:
Dmitry Mishin27229712006-04-01 02:25:19 -08001992 ret = do_add_counters(user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 break;
1994
1995 default:
1996 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1997 ret = -EINVAL;
1998 }
1999
2000 return ret;
2001}
2002
2003static int
2004do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2005{
2006 int ret;
2007
2008 if (!capable(CAP_NET_ADMIN))
2009 return -EPERM;
2010
2011 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002012 case IPT_SO_GET_INFO:
2013 ret = get_info(user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002015
2016 case IPT_SO_GET_ENTRIES:
2017 ret = get_entries(user, len);
2018 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 case IPT_SO_GET_REVISION_MATCH:
2021 case IPT_SO_GET_REVISION_TARGET: {
2022 struct ipt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002023 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 if (*len != sizeof(rev)) {
2026 ret = -EINVAL;
2027 break;
2028 }
2029 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2030 ret = -EFAULT;
2031 break;
2032 }
2033
2034 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002035 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002037 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Harald Welte2e4e6a12006-01-12 13:30:04 -08002039 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2040 rev.revision,
2041 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 "ipt_%s", rev.name);
2043 break;
2044 }
2045
2046 default:
2047 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2048 ret = -EINVAL;
2049 }
2050
2051 return ret;
2052}
2053
Harald Welte2e4e6a12006-01-12 13:30:04 -08002054int ipt_register_table(struct xt_table *table, const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002057 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08002058 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08002060 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Harald Welte2e4e6a12006-01-12 13:30:04 -08002062 newinfo = xt_alloc_table_info(repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (!newinfo)
2064 return -ENOMEM;
2065
Patrick McHardy9c547952007-12-17 21:52:00 -08002066 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002067 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2068 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08002071 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 repl->num_entries,
2073 repl->hook_entry,
2074 repl->underflow);
2075 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08002076 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return ret;
2078 }
2079
Patrick McHardyda298d32006-06-27 03:00:09 -07002080 ret = xt_register_table(table, &bootstrap, newinfo);
2081 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08002082 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return ret;
2084 }
2085
Harald Welte2e4e6a12006-01-12 13:30:04 -08002086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Jan Engelhardte60a13e2007-02-07 15:12:33 -08002089void ipt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002091 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002092 void *loc_cpu_entry;
2093
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002094 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002097 loc_cpu_entry = private->entries[raw_smp_processor_id()];
2098 IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
2099 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
2102/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002103static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2105 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002106 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Patrick McHardy9c547952007-12-17 21:52:00 -08002108 return ((test_type == 0xFF) ||
2109 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 ^ invert;
2111}
2112
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002113static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114icmp_match(const struct sk_buff *skb,
2115 const struct net_device *in,
2116 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -08002117 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 const void *matchinfo,
2119 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -08002120 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -07002121 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 struct icmphdr _icmph, *ic;
2124 const struct ipt_icmp *icmpinfo = matchinfo;
2125
2126 /* Must not be a fragment. */
2127 if (offset)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002128 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Harald Welte2e4e6a12006-01-12 13:30:04 -08002130 ic = skb_header_pointer(skb, protoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 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");
Jan Engelhardtcff533a2007-07-07 22:15:12 -07002136 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002137 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
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. */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07002148static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149icmp_checkentry(const char *tablename,
Patrick McHardy9c547952007-12-17 21:52:00 -08002150 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -08002151 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 unsigned int hook_mask)
2154{
2155 const struct ipt_icmp *icmpinfo = matchinfo;
2156
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002157 /* Must specify no unknown invflags */
2158 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
2161/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07002162static struct xt_target ipt_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 .name = IPT_STANDARD_TARGET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002164 .targetsize = sizeof(int),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002165 .family = AF_INET,
Dmitry Mishin27229712006-04-01 02:25:19 -08002166#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07002167 .compatsize = sizeof(compat_int_t),
2168 .compat_from_user = compat_standard_from_user,
2169 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171};
2172
Patrick McHardy9f15c532007-07-07 22:22:02 -07002173static struct xt_target ipt_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 .name = IPT_ERROR_TARGET,
2175 .target = ipt_error,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002176 .targetsize = IPT_FUNCTION_MAXNAMELEN,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002177 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178};
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,
Dmitry Mishin27229712006-04-01 02:25:19 -08002185#ifdef CONFIG_COMPAT
2186 .compat_set = compat_do_ipt_set_ctl,
2187#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 .get_optmin = IPT_BASE_CTL,
2189 .get_optmax = IPT_SO_GET_MAX+1,
2190 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002191#ifdef CONFIG_COMPAT
2192 .compat_get = compat_do_ipt_get_ctl,
2193#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002194 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195};
2196
Patrick McHardy9f15c532007-07-07 22:22:02 -07002197static struct xt_match icmp_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 .name = "icmp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002199 .match = icmp_match,
2200 .matchsize = sizeof(struct ipt_icmp),
Patrick McHardy9c547952007-12-17 21:52:00 -08002201 .checkentry = icmp_checkentry,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002202 .proto = IPPROTO_ICMP,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002203 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204};
2205
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002206static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
2208 int ret;
2209
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002210 ret = xt_proto_init(AF_INET);
2211 if (ret < 0)
2212 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002215 ret = xt_register_target(&ipt_standard_target);
2216 if (ret < 0)
2217 goto err2;
2218 ret = xt_register_target(&ipt_error_target);
2219 if (ret < 0)
2220 goto err3;
2221 ret = xt_register_match(&icmp_matchstruct);
2222 if (ret < 0)
2223 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 /* Register setsockopt */
2226 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002227 if (ret < 0)
2228 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Dan Aloni0236e662007-07-09 13:20:12 -07002230 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002232
2233err5:
2234 xt_unregister_match(&icmp_matchstruct);
2235err4:
2236 xt_unregister_target(&ipt_error_target);
2237err3:
2238 xt_unregister_target(&ipt_standard_target);
2239err2:
2240 xt_proto_fini(AF_INET);
2241err1:
2242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002245static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002248
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002249 xt_unregister_match(&icmp_matchstruct);
2250 xt_unregister_target(&ipt_error_target);
2251 xt_unregister_target(&ipt_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002252
2253 xt_proto_fini(AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
2256EXPORT_SYMBOL(ipt_register_table);
2257EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002259module_init(ip_tables_init);
2260module_exit(ip_tables_fini);