blob: 271f6a5d3d4c393e8a8f0031afb2b748bb1d585a [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>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080029#include <net/netfilter/nf_log.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
33MODULE_DESCRIPTION("IPv4 packet filter");
34
35/*#define DEBUG_IP_FIREWALL*/
36/*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging */
37/*#define DEBUG_IP_FIREWALL_USER*/
38
39#ifdef DEBUG_IP_FIREWALL
40#define dprintf(format, args...) printk(format , ## args)
41#else
42#define dprintf(format, args...)
43#endif
44
45#ifdef DEBUG_IP_FIREWALL_USER
46#define duprintf(format, args...) printk(format , ## args)
47#else
48#define duprintf(format, args...)
49#endif
50
51#ifdef CONFIG_NETFILTER_DEBUG
52#define IP_NF_ASSERT(x) \
53do { \
54 if (!(x)) \
55 printk("IP_NF_ASSERT: %s:%s:%u\n", \
56 __FUNCTION__, __FILE__, __LINE__); \
57} while(0)
58#else
59#define IP_NF_ASSERT(x)
60#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#if 0
63/* All the better to debug you with... */
64#define static
65#define inline
66#endif
67
68/*
69 We keep a set of rules for each CPU, so we can avoid write-locking
70 them in the softirq when updating the counters and therefore
71 only need to read-lock in the softirq; doing a write_lock_bh() in user
72 context stops packets coming through and allows user context to read
73 the counters or update the rules.
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 Hence the start of any table is given by get_table() below. */
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Returns whether matches rule or not. */
Patrick McHardy9c547952007-12-17 21:52:00 -080078static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070079ip_packet_match(const struct iphdr *ip,
80 const char *indev,
81 const char *outdev,
82 const struct ipt_ip *ipinfo,
83 int isfrag)
84{
85 size_t i;
86 unsigned long ret;
87
88#define FWINV(bool,invflg) ((bool) ^ !!(ipinfo->invflags & invflg))
89
90 if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
91 IPT_INV_SRCIP)
92 || FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
93 IPT_INV_DSTIP)) {
94 dprintf("Source or dest mismatch.\n");
95
96 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
97 NIPQUAD(ip->saddr),
98 NIPQUAD(ipinfo->smsk.s_addr),
99 NIPQUAD(ipinfo->src.s_addr),
100 ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
101 dprintf("DST: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
102 NIPQUAD(ip->daddr),
103 NIPQUAD(ipinfo->dmsk.s_addr),
104 NIPQUAD(ipinfo->dst.s_addr),
105 ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800106 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
109 /* Look for ifname matches; this should unroll nicely. */
110 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
111 ret |= (((const unsigned long *)indev)[i]
112 ^ ((const unsigned long *)ipinfo->iniface)[i])
113 & ((const unsigned long *)ipinfo->iniface_mask)[i];
114 }
115
116 if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
117 dprintf("VIA in mismatch (%s vs %s).%s\n",
118 indev, ipinfo->iniface,
119 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800120 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
123 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
124 ret |= (((const unsigned long *)outdev)[i]
125 ^ ((const unsigned long *)ipinfo->outiface)[i])
126 & ((const unsigned long *)ipinfo->outiface_mask)[i];
127 }
128
129 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
130 dprintf("VIA out mismatch (%s vs %s).%s\n",
131 outdev, ipinfo->outiface,
132 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800133 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
136 /* Check specific protocol */
137 if (ipinfo->proto
138 && FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
139 dprintf("Packet protocol %hi does not match %hi.%s\n",
140 ip->protocol, ipinfo->proto,
141 ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800142 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
145 /* If we have a fragment rule but the packet is not a fragment
146 * then we return zero */
147 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
148 dprintf("Fragment rule but not fragment.%s\n",
149 ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800150 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
Patrick McHardy9c547952007-12-17 21:52:00 -0800153 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700156static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157ip_checkentry(const struct ipt_ip *ip)
158{
159 if (ip->flags & ~IPT_F_MASK) {
160 duprintf("Unknown flag bits set: %08X\n",
161 ip->flags & ~IPT_F_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700162 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164 if (ip->invflags & ~IPT_INV_MASK) {
165 duprintf("Unknown invflag bits set: %08X\n",
166 ip->invflags & ~IPT_INV_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700167 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700169 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172static unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700173ipt_error(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 const struct net_device *in,
175 const struct net_device *out,
176 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800177 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700178 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 if (net_ratelimit())
181 printk("ip_tables: error: `%s'\n", (char *)targinfo);
182
183 return NF_DROP;
184}
185
186static inline
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700187bool do_match(struct ipt_entry_match *m,
188 const struct sk_buff *skb,
189 const struct net_device *in,
190 const struct net_device *out,
191 int offset,
192 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 /* Stop iteration if it doesn't match */
Patrick McHardy1c524832006-03-20 18:02:15 -0800195 if (!m->u.kernel.match->match(skb, in, out, m->u.kernel.match, m->data,
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300196 offset, ip_hdrlen(skb), hotdrop))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700197 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 else
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700199 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202static inline struct ipt_entry *
203get_entry(void *base, unsigned int offset)
204{
205 return (struct ipt_entry *)(base + offset);
206}
207
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700208/* All zeroes == unconditional rule. */
209static inline int
210unconditional(const struct ipt_ip *ip)
211{
212 unsigned int i;
213
214 for (i = 0; i < sizeof(*ip)/sizeof(__u32); i++)
215 if (((__u32 *)ip)[i])
216 return 0;
217
218 return 1;
219}
220
221#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
222 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
223static const char *hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800224 [NF_INET_PRE_ROUTING] = "PREROUTING",
225 [NF_INET_LOCAL_IN] = "INPUT",
Patrick McHardy9c547952007-12-17 21:52:00 -0800226 [NF_INET_FORWARD] = "FORWARD",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800227 [NF_INET_LOCAL_OUT] = "OUTPUT",
228 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700229};
230
231enum nf_ip_trace_comments {
232 NF_IP_TRACE_COMMENT_RULE,
233 NF_IP_TRACE_COMMENT_RETURN,
234 NF_IP_TRACE_COMMENT_POLICY,
235};
236
237static const char *comments[] = {
238 [NF_IP_TRACE_COMMENT_RULE] = "rule",
239 [NF_IP_TRACE_COMMENT_RETURN] = "return",
240 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
241};
242
243static struct nf_loginfo trace_loginfo = {
244 .type = NF_LOG_TYPE_LOG,
245 .u = {
246 .log = {
247 .level = 4,
248 .logflags = NF_LOG_MASK,
249 },
250 },
251};
252
253static inline int
254get_chainname_rulenum(struct ipt_entry *s, struct ipt_entry *e,
255 char *hookname, char **chainname,
256 char **comment, unsigned int *rulenum)
257{
258 struct ipt_standard_target *t = (void *)ipt_get_target(s);
259
260 if (strcmp(t->target.u.kernel.target->name, IPT_ERROR_TARGET) == 0) {
261 /* Head of user chain: ERROR target with chainname */
262 *chainname = t->target.data;
263 (*rulenum) = 0;
264 } else if (s == e) {
265 (*rulenum)++;
266
267 if (s->target_offset == sizeof(struct ipt_entry)
268 && strcmp(t->target.u.kernel.target->name,
269 IPT_STANDARD_TARGET) == 0
270 && t->verdict < 0
271 && unconditional(&s->ip)) {
272 /* Tail of chains: STANDARD target (return/policy) */
273 *comment = *chainname == hookname
274 ? (char *)comments[NF_IP_TRACE_COMMENT_POLICY]
275 : (char *)comments[NF_IP_TRACE_COMMENT_RETURN];
276 }
277 return 1;
278 } else
279 (*rulenum)++;
280
281 return 0;
282}
283
284static void trace_packet(struct sk_buff *skb,
285 unsigned int hook,
286 const struct net_device *in,
287 const struct net_device *out,
288 char *tablename,
289 struct xt_table_info *private,
290 struct ipt_entry *e)
291{
292 void *table_base;
293 struct ipt_entry *root;
294 char *hookname, *chainname, *comment;
295 unsigned int rulenum = 0;
296
297 table_base = (void *)private->entries[smp_processor_id()];
298 root = get_entry(table_base, private->hook_entry[hook]);
299
300 hookname = chainname = (char *)hooknames[hook];
301 comment = (char *)comments[NF_IP_TRACE_COMMENT_RULE];
302
303 IPT_ENTRY_ITERATE(root,
304 private->size - private->hook_entry[hook],
305 get_chainname_rulenum,
306 e, hookname, &chainname, &comment, &rulenum);
307
308 nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo,
309 "TRACE: %s:%s:%s:%u ",
310 tablename, chainname, comment, rulenum);
311}
312#endif
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* Returns one of the generic firewall policies, like NF_ACCEPT. */
315unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700316ipt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned int hook,
318 const struct net_device *in,
319 const struct net_device *out,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800320 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
323 u_int16_t offset;
324 struct iphdr *ip;
325 u_int16_t datalen;
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700326 bool hotdrop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* Initializing verdict to NF_DROP keeps gcc happy. */
328 unsigned int verdict = NF_DROP;
329 const char *indev, *outdev;
330 void *table_base;
331 struct ipt_entry *e, *back;
Patrick McHardy83117312006-08-17 18:13:53 -0700332 struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Initialization */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700335 ip = ip_hdr(skb);
336 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 indev = in ? in->name : nulldevname;
338 outdev = out ? out->name : nulldevname;
339 /* We handle fragments by dealing with the first fragment as
340 * if it was a normal packet. All other fragments are treated
341 * normally, except that they will NEVER match rules that ask
342 * things we don't know, ie. tcp syn flag or ports). If the
343 * rule is also a fragment-specific rule, non-fragments won't
344 * match it. */
345 offset = ntohs(ip->frag_off) & IP_OFFSET;
346
347 read_lock_bh(&table->lock);
348 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Patrick McHardy83117312006-08-17 18:13:53 -0700349 private = table->private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800350 table_base = (void *)private->entries[smp_processor_id()];
351 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* For return from builtin chain */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800354 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 do {
357 IP_NF_ASSERT(e);
358 IP_NF_ASSERT(back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (ip_packet_match(ip, indev, outdev, &e->ip, offset)) {
360 struct ipt_entry_target *t;
361
362 if (IPT_MATCH_ITERATE(e, do_match,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700363 skb, in, out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 offset, &hotdrop) != 0)
365 goto no_match;
366
367 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
368
369 t = ipt_get_target(e);
370 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700371
372#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
373 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
374 /* The packet is traced: log it */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700375 if (unlikely(skb->nf_trace))
376 trace_packet(skb, hook, in, out,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700377 table->name, private, e);
378#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* Standard target? */
380 if (!t->u.kernel.target->target) {
381 int v;
382
383 v = ((struct ipt_standard_target *)t)->verdict;
384 if (v < 0) {
385 /* Pop from stack? */
386 if (v != IPT_RETURN) {
387 verdict = (unsigned)(-v) - 1;
388 break;
389 }
390 e = back;
391 back = get_entry(table_base,
392 back->comefrom);
393 continue;
394 }
Patrick McHardy05465342005-08-21 23:31:43 -0700395 if (table_base + v != (void *)e + e->next_offset
396 && !(e->ip.flags & IPT_F_GOTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Save old back ptr in next entry */
398 struct ipt_entry *next
399 = (void *)e + e->next_offset;
400 next->comefrom
401 = (void *)back - table_base;
402 /* set back pointer to next entry */
403 back = next;
404 }
405
406 e = get_entry(table_base, v);
407 } else {
408 /* Targets which reenter must return
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900409 abs. verdicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410#ifdef CONFIG_NETFILTER_DEBUG
411 ((struct ipt_entry *)table_base)->comefrom
412 = 0xeeeeeeec;
413#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700414 verdict = t->u.kernel.target->target(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 in, out,
416 hook,
Patrick McHardy1c524832006-03-20 18:02:15 -0800417 t->u.kernel.target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700418 t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420#ifdef CONFIG_NETFILTER_DEBUG
421 if (((struct ipt_entry *)table_base)->comefrom
422 != 0xeeeeeeec
423 && verdict == IPT_CONTINUE) {
424 printk("Target %s reentered!\n",
425 t->u.kernel.target->name);
426 verdict = NF_DROP;
427 }
428 ((struct ipt_entry *)table_base)->comefrom
429 = 0x57acc001;
430#endif
431 /* Target might have changed stuff. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700432 ip = ip_hdr(skb);
433 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 if (verdict == IPT_CONTINUE)
436 e = (void *)e + e->next_offset;
437 else
438 /* Verdict */
439 break;
440 }
441 } else {
442
443 no_match:
444 e = (void *)e + e->next_offset;
445 }
446 } while (!hotdrop);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 read_unlock_bh(&table->lock);
449
450#ifdef DEBUG_ALLOW_ALL
451 return NF_ACCEPT;
452#else
453 if (hotdrop)
454 return NF_DROP;
455 else return verdict;
456#endif
457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/* Figures out from what hook each rule can be called: returns 0 if
460 there are loops. Puts hook bitmask in comefrom. */
461static int
Harald Welte2e4e6a12006-01-12 13:30:04 -0800462mark_source_chains(struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800463 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 unsigned int hook;
466
467 /* No recursion; use packet counter to save back ptrs (reset
468 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800469 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800471 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if (!(valid_hooks & (1 << hook)))
474 continue;
475
476 /* Set initial back pointer. */
477 e->counters.pcnt = pos;
478
479 for (;;) {
480 struct ipt_standard_target *t
481 = (void *)ipt_get_target(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800482 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800484 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 printk("iptables: loop hook %u pos %u %08X.\n",
486 hook, pos, e->comefrom);
487 return 0;
488 }
Patrick McHardy9c547952007-12-17 21:52:00 -0800489 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /* Unconditional return/END. */
Al Viroe1b4b9f2006-12-12 00:29:52 -0800492 if ((e->target_offset == sizeof(struct ipt_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 && (strcmp(t->target.u.user.name,
494 IPT_STANDARD_TARGET) == 0)
495 && t->verdict < 0
Al Viroe1b4b9f2006-12-12 00:29:52 -0800496 && unconditional(&e->ip)) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 unsigned int oldpos, size;
498
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800499 if (t->verdict < -NF_MAX_VERDICT - 1) {
500 duprintf("mark_source_chains: bad "
501 "negative verdict (%i)\n",
502 t->verdict);
503 return 0;
504 }
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* Return: backtrack through the last
507 big jump. */
508 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800509 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510#ifdef DEBUG_IP_FIREWALL_USER
511 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800512 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 duprintf("Back unset "
514 "on hook %u "
515 "rule %u\n",
516 hook, pos);
517 }
518#endif
519 oldpos = pos;
520 pos = e->counters.pcnt;
521 e->counters.pcnt = 0;
522
523 /* We're at the start. */
524 if (pos == oldpos)
525 goto next;
526
527 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800528 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 } while (oldpos == pos + e->next_offset);
530
531 /* Move along one */
532 size = e->next_offset;
533 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800534 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 e->counters.pcnt = pos;
536 pos += size;
537 } else {
538 int newpos = t->verdict;
539
540 if (strcmp(t->target.u.user.name,
541 IPT_STANDARD_TARGET) == 0
542 && newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800543 if (newpos > newinfo->size -
544 sizeof(struct ipt_entry)) {
545 duprintf("mark_source_chains: "
546 "bad verdict (%i)\n",
547 newpos);
548 return 0;
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* This a jump; chase it. */
551 duprintf("Jump rule %u -> %u\n",
552 pos, newpos);
553 } else {
554 /* ... this is a fallthru */
555 newpos = pos + e->next_offset;
556 }
557 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800558 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 e->counters.pcnt = pos;
560 pos = newpos;
561 }
562 }
563 next:
564 duprintf("Finished chain %u\n", hook);
565 }
566 return 1;
567}
568
569static inline int
570cleanup_match(struct ipt_entry_match *m, unsigned int *i)
571{
572 if (i && (*i)-- == 0)
573 return 1;
574
575 if (m->u.kernel.match->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700576 m->u.kernel.match->destroy(m->u.kernel.match, m->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 module_put(m->u.kernel.match->me);
578 return 0;
579}
580
581static inline int
Dmitry Mishina96be242006-12-12 00:29:26 -0800582check_entry(struct ipt_entry *e, const char *name)
583{
584 struct ipt_entry_target *t;
585
586 if (!ip_checkentry(&e->ip)) {
587 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
588 return -EINVAL;
589 }
590
Patrick McHardy9c547952007-12-17 21:52:00 -0800591 if (e->target_offset + sizeof(struct ipt_entry_target) >
592 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800593 return -EINVAL;
594
595 t = ipt_get_target(e);
596 if (e->target_offset + t->u.target_size > e->next_offset)
597 return -EINVAL;
598
599 return 0;
600}
601
602static inline int check_match(struct ipt_entry_match *m, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -0800603 const struct ipt_ip *ip,
604 unsigned int hookmask, unsigned int *i)
Dmitry Mishina96be242006-12-12 00:29:26 -0800605{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800606 struct xt_match *match;
Dmitry Mishina96be242006-12-12 00:29:26 -0800607 int ret;
608
609 match = m->u.kernel.match;
610 ret = xt_check_match(match, AF_INET, m->u.match_size - sizeof(*m),
611 name, hookmask, ip->proto,
612 ip->invflags & IPT_INV_PROTO);
613 if (!ret && m->u.kernel.match->checkentry
614 && !m->u.kernel.match->checkentry(name, ip, match, m->data,
615 hookmask)) {
616 duprintf("ip_tables: check failed for `%s'.\n",
617 m->u.kernel.match->name);
618 ret = -EINVAL;
619 }
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -0700620 if (!ret)
621 (*i)++;
Dmitry Mishina96be242006-12-12 00:29:26 -0800622 return ret;
623}
624
625static inline int
626find_check_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -0800627 const char *name,
628 const struct ipt_ip *ip,
629 unsigned int hookmask,
630 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800632 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800633 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Harald Welte2e4e6a12006-01-12 13:30:04 -0800635 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy9c547952007-12-17 21:52:00 -0800636 m->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 "ipt_%s", m->u.user.name);
638 if (IS_ERR(match) || !match) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800639 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return match ? PTR_ERR(match) : -ENOENT;
641 }
642 m->u.kernel.match = match;
643
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -0700644 ret = check_match(m, name, ip, hookmask, i);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800645 if (ret)
646 goto err;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800649err:
650 module_put(m->u.kernel.match->me);
651 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Dmitry Mishina96be242006-12-12 00:29:26 -0800654static inline int check_target(struct ipt_entry *e, const char *name)
655{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900656 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800657 struct xt_target *target;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900658 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800659
660 t = ipt_get_target(e);
661 target = t->u.kernel.target;
662 ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
663 name, e->comefrom, e->ip.proto,
664 e->ip.invflags & IPT_INV_PROTO);
665 if (!ret && t->u.kernel.target->checkentry
Patrick McHardy4b478242007-12-17 21:46:15 -0800666 && !t->u.kernel.target->checkentry(name, e, target, t->data,
667 e->comefrom)) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800668 duprintf("ip_tables: check failed for `%s'.\n",
669 t->u.kernel.target->name);
670 ret = -EINVAL;
671 }
672 return ret;
673}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675static inline int
Dmitry Mishina96be242006-12-12 00:29:26 -0800676find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
Patrick McHardy4b478242007-12-17 21:46:15 -0800677 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800680 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int ret;
682 unsigned int j;
683
Dmitry Mishina96be242006-12-12 00:29:26 -0800684 ret = check_entry(e, name);
685 if (ret)
686 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 j = 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800689 ret = IPT_MATCH_ITERATE(e, find_check_match, name, &e->ip,
Patrick McHardy4b478242007-12-17 21:46:15 -0800690 e->comefrom, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (ret != 0)
692 goto cleanup_matches;
693
694 t = ipt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800695 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -0800696 t->u.user.name,
697 t->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 "ipt_%s", t->u.user.name);
699 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800700 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 ret = target ? PTR_ERR(target) : -ENOENT;
702 goto cleanup_matches;
703 }
704 t->u.kernel.target = target;
705
Dmitry Mishina96be242006-12-12 00:29:26 -0800706 ret = check_target(e, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800707 if (ret)
708 goto err;
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 (*i)++;
711 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800712 err:
713 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 cleanup_matches:
715 IPT_MATCH_ITERATE(e, cleanup_match, &j);
716 return ret;
717}
718
719static inline int
720check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800721 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unsigned char *base,
723 unsigned char *limit,
724 const unsigned int *hook_entries,
725 const unsigned int *underflows,
726 unsigned int *i)
727{
728 unsigned int h;
729
730 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0
731 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
732 duprintf("Bad offset %p\n", e);
733 return -EINVAL;
734 }
735
736 if (e->next_offset
737 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
738 duprintf("checking: element %p size %u\n",
739 e, e->next_offset);
740 return -EINVAL;
741 }
742
743 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800744 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if ((unsigned char *)e - base == hook_entries[h])
746 newinfo->hook_entry[h] = hook_entries[h];
747 if ((unsigned char *)e - base == underflows[h])
748 newinfo->underflow[h] = underflows[h];
749 }
750
751 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900752 < 0 (not IPT_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800755 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 e->comefrom = 0;
757
758 (*i)++;
759 return 0;
760}
761
762static inline int
763cleanup_entry(struct ipt_entry *e, unsigned int *i)
764{
765 struct ipt_entry_target *t;
766
767 if (i && (*i)-- == 0)
768 return 1;
769
770 /* Cleanup all matches */
771 IPT_MATCH_ITERATE(e, cleanup_match, NULL);
772 t = ipt_get_target(e);
773 if (t->u.kernel.target->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700774 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 module_put(t->u.kernel.target->me);
776 return 0;
777}
778
779/* Checks and translates the user-supplied table segment (held in
780 newinfo) */
781static int
782translate_table(const char *name,
783 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800784 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800785 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 unsigned int size,
787 unsigned int number,
788 const unsigned int *hook_entries,
789 const unsigned int *underflows)
790{
791 unsigned int i;
792 int ret;
793
794 newinfo->size = size;
795 newinfo->number = number;
796
797 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800798 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 newinfo->hook_entry[i] = 0xFFFFFFFF;
800 newinfo->underflow[i] = 0xFFFFFFFF;
801 }
802
803 duprintf("translate_table: size %u\n", newinfo->size);
804 i = 0;
805 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800806 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 check_entry_size_and_hooks,
808 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800809 entry0,
810 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 hook_entries, underflows, &i);
812 if (ret != 0)
813 return ret;
814
815 if (i != number) {
816 duprintf("translate_table: %u not %u entries\n",
817 i, number);
818 return -EINVAL;
819 }
820
821 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800822 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* Only hooks which are valid */
824 if (!(valid_hooks & (1 << i)))
825 continue;
826 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
827 duprintf("Invalid hook entry %u %u\n",
828 i, hook_entries[i]);
829 return -EINVAL;
830 }
831 if (newinfo->underflow[i] == 0xFFFFFFFF) {
832 duprintf("Invalid underflow %u %u\n",
833 i, underflows[i]);
834 return -EINVAL;
835 }
836 }
837
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800838 if (!mark_source_chains(newinfo, valid_hooks, entry0))
839 return -ELOOP;
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* Finally, each sanity check must pass */
842 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800843 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Dmitry Mishina96be242006-12-12 00:29:26 -0800844 find_check_entry, name, size, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800846 if (ret != 0) {
847 IPT_ENTRY_ITERATE(entry0, newinfo->size,
848 cleanup_entry, &i);
849 return ret;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700853 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800854 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
855 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
858 return ret;
859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861/* Gets counters. */
862static inline int
863add_entry_to_counter(const struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800864 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 unsigned int *i)
866{
867 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
868
869 (*i)++;
870 return 0;
871}
872
Eric Dumazet31836062005-12-13 23:13:48 -0800873static inline int
874set_entry_to_counter(const struct ipt_entry *e,
875 struct ipt_counters total[],
876 unsigned int *i)
877{
878 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
879
880 (*i)++;
881 return 0;
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800885get_counters(const struct xt_table_info *t,
886 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 unsigned int cpu;
889 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800890 unsigned int curcpu;
891
892 /* Instead of clearing (by a previous call to memset())
893 * the counters and using adds, we set the counters
894 * with data used by 'current' CPU
895 * We dont care about preemption here.
896 */
897 curcpu = raw_smp_processor_id();
898
899 i = 0;
900 IPT_ENTRY_ITERATE(t->entries[curcpu],
901 t->size,
902 set_entry_to_counter,
903 counters,
904 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700906 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800907 if (cpu == curcpu)
908 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800910 IPT_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 t->size,
912 add_entry_to_counter,
913 counters,
914 &i);
915 }
916}
917
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800918static inline struct xt_counters * alloc_counters(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Dmitry Mishin27229712006-04-01 02:25:19 -0800920 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800921 struct xt_counters *counters;
922 struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /* We need atomic snapshot of counters: rest doesn't change
925 (other than comefrom, which userspace doesn't care
926 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800927 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet31836062005-12-13 23:13:48 -0800928 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (counters == NULL)
Dmitry Mishin27229712006-04-01 02:25:19 -0800931 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 write_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800935 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 write_unlock_bh(&table->lock);
937
Dmitry Mishin27229712006-04-01 02:25:19 -0800938 return counters;
939}
940
941static int
942copy_entries_to_user(unsigned int total_size,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800943 struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800944 void __user *userptr)
945{
946 unsigned int off, num;
947 struct ipt_entry *e;
948 struct xt_counters *counters;
949 struct xt_table_info *private = table->private;
950 int ret = 0;
951 void *loc_cpu_entry;
952
953 counters = alloc_counters(table);
954 if (IS_ERR(counters))
955 return PTR_ERR(counters);
956
Eric Dumazet31836062005-12-13 23:13:48 -0800957 /* choose the copy that is on our node/cpu, ...
958 * This choice is lazy (because current thread is
959 * allowed to migrate to another cpu)
960 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800961 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800962 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 ret = -EFAULT;
964 goto free_counters;
965 }
966
967 /* FIXME: use iterator macros --RR */
968 /* ... then go back and fix counters and names */
969 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
970 unsigned int i;
971 struct ipt_entry_match *m;
972 struct ipt_entry_target *t;
973
Eric Dumazet31836062005-12-13 23:13:48 -0800974 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (copy_to_user(userptr + off
976 + offsetof(struct ipt_entry, counters),
977 &counters[num],
978 sizeof(counters[num])) != 0) {
979 ret = -EFAULT;
980 goto free_counters;
981 }
982
983 for (i = sizeof(struct ipt_entry);
984 i < e->target_offset;
985 i += m->u.match_size) {
986 m = (void *)e + i;
987
988 if (copy_to_user(userptr + off + i
989 + offsetof(struct ipt_entry_match,
990 u.user.name),
991 m->u.kernel.match->name,
992 strlen(m->u.kernel.match->name)+1)
993 != 0) {
994 ret = -EFAULT;
995 goto free_counters;
996 }
997 }
998
999 t = ipt_get_target(e);
1000 if (copy_to_user(userptr + off + e->target_offset
1001 + offsetof(struct ipt_entry_target,
1002 u.user.name),
1003 t->u.kernel.target->name,
1004 strlen(t->u.kernel.target->name)+1) != 0) {
1005 ret = -EFAULT;
1006 goto free_counters;
1007 }
1008 }
1009
1010 free_counters:
1011 vfree(counters);
1012 return ret;
1013}
1014
Dmitry Mishin27229712006-04-01 02:25:19 -08001015#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001016static void compat_standard_from_user(void *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001017{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001018 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001019
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001020 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001021 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001022 memcpy(dst, &v, sizeof(v));
1023}
1024
1025static int compat_standard_to_user(void __user *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001026{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001027 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001028
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001029 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001030 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001031 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001032}
1033
1034static inline int
Patrick McHardy4b478242007-12-17 21:46:15 -08001035compat_calc_match(struct ipt_entry_match *m, int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001036{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001037 *size += xt_compat_match_offset(m->u.kernel.match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001038 return 0;
1039}
1040
Eric Dumazet259d4e42007-12-04 23:24:56 -08001041static int compat_calc_entry(struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001042 const struct xt_table_info *info,
1043 void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001044{
1045 struct ipt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001046 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001047 int off, i, ret;
1048
Patrick McHardy30c08c42007-12-17 21:47:14 -08001049 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001050 entry_offset = (void *)e - base;
1051 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
1052 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001053 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001054 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001055 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001056 if (ret)
1057 return ret;
1058
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001059 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001060 if (info->hook_entry[i] &&
1061 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001062 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001063 if (info->underflow[i] &&
1064 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001065 newinfo->underflow[i] -= off;
1066 }
1067 return 0;
1068}
1069
Eric Dumazet259d4e42007-12-04 23:24:56 -08001070static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001071 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001072{
1073 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001074
1075 if (!newinfo || !info)
1076 return -EINVAL;
1077
Eric Dumazet259d4e42007-12-04 23:24:56 -08001078 /* we dont care about newinfo->entries[] */
1079 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1080 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001081 loc_cpu_entry = info->entries[raw_smp_processor_id()];
1082 return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001083 compat_calc_entry, info, loc_cpu_entry,
1084 newinfo);
Dmitry Mishin27229712006-04-01 02:25:19 -08001085}
1086#endif
1087
1088static int get_info(void __user *user, int *len, int compat)
1089{
1090 char name[IPT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001091 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001092 int ret;
1093
1094 if (*len != sizeof(struct ipt_getinfo)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001095 duprintf("length %u != %zu\n", *len,
1096 sizeof(struct ipt_getinfo));
Dmitry Mishin27229712006-04-01 02:25:19 -08001097 return -EINVAL;
1098 }
1099
1100 if (copy_from_user(name, user, sizeof(name)) != 0)
1101 return -EFAULT;
1102
1103 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1104#ifdef CONFIG_COMPAT
1105 if (compat)
1106 xt_compat_lock(AF_INET);
1107#endif
1108 t = try_then_request_module(xt_find_table_lock(AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001109 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001110 if (t && !IS_ERR(t)) {
1111 struct ipt_getinfo info;
1112 struct xt_table_info *private = t->private;
1113
1114#ifdef CONFIG_COMPAT
1115 if (compat) {
1116 struct xt_table_info tmp;
1117 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001118 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001119 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001120 }
1121#endif
1122 info.valid_hooks = t->valid_hooks;
1123 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001124 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001125 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001126 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001127 info.num_entries = private->number;
1128 info.size = private->size;
1129 strcpy(info.name, name);
1130
1131 if (copy_to_user(user, &info, *len) != 0)
1132 ret = -EFAULT;
1133 else
1134 ret = 0;
1135
1136 xt_table_unlock(t);
1137 module_put(t->me);
1138 } else
1139 ret = t ? PTR_ERR(t) : -ENOENT;
1140#ifdef CONFIG_COMPAT
1141 if (compat)
1142 xt_compat_unlock(AF_INET);
1143#endif
1144 return ret;
1145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001148get_entries(struct ipt_get_entries __user *uptr, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001151 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001152 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Dmitry Mishin27229712006-04-01 02:25:19 -08001154 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001155 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001156 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) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001161 duprintf("get_entries: %u != %zu\n",
1162 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001163 return -EINVAL;
1164 }
1165
1166 t = xt_find_table_lock(AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (t && !IS_ERR(t)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001168 struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001169 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001170 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001171 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 t, uptr->entrytable);
1173 else {
1174 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001175 private->size, get.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 ret = -EINVAL;
1177 }
1178 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001179 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 } else
1181 ret = t ? PTR_ERR(t) : -ENOENT;
1182
1183 return ret;
1184}
1185
1186static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001187__do_replace(const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001188 struct xt_table_info *newinfo, unsigned int num_counters,
1189 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001190{
1191 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001192 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001193 struct xt_table_info *oldinfo;
1194 struct xt_counters *counters;
1195 void *loc_cpu_old_entry;
1196
1197 ret = 0;
1198 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1199 if (!counters) {
1200 ret = -ENOMEM;
1201 goto out;
1202 }
1203
1204 t = try_then_request_module(xt_find_table_lock(AF_INET, name),
1205 "iptable_%s", name);
1206 if (!t || IS_ERR(t)) {
1207 ret = t ? PTR_ERR(t) : -ENOENT;
1208 goto free_newinfo_counters_untrans;
1209 }
1210
1211 /* You lied! */
1212 if (valid_hooks != t->valid_hooks) {
1213 duprintf("Valid hook crap: %08X vs %08X\n",
1214 valid_hooks, t->valid_hooks);
1215 ret = -EINVAL;
1216 goto put_module;
1217 }
1218
1219 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1220 if (!oldinfo)
1221 goto put_module;
1222
1223 /* Update module usage count based on number of rules */
1224 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1225 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1226 if ((oldinfo->number > oldinfo->initial_entries) ||
1227 (newinfo->number <= oldinfo->initial_entries))
1228 module_put(t->me);
1229 if ((oldinfo->number > oldinfo->initial_entries) &&
1230 (newinfo->number <= oldinfo->initial_entries))
1231 module_put(t->me);
1232
1233 /* Get the old counters. */
1234 get_counters(oldinfo, counters);
1235 /* Decrease module usage counts and free resource */
1236 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Patrick McHardy4b478242007-12-17 21:46:15 -08001237 IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1238 NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001239 xt_free_table_info(oldinfo);
1240 if (copy_to_user(counters_ptr, counters,
1241 sizeof(struct xt_counters) * num_counters) != 0)
1242 ret = -EFAULT;
1243 vfree(counters);
1244 xt_table_unlock(t);
1245 return ret;
1246
1247 put_module:
1248 module_put(t->me);
1249 xt_table_unlock(t);
1250 free_newinfo_counters_untrans:
1251 vfree(counters);
1252 out:
1253 return ret;
1254}
1255
1256static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257do_replace(void __user *user, unsigned int len)
1258{
1259 int ret;
1260 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001261 struct xt_table_info *newinfo;
1262 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1265 return -EFAULT;
1266
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001267 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001268 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1269 return -ENOMEM;
1270
Harald Welte2e4e6a12006-01-12 13:30:04 -08001271 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (!newinfo)
1273 return -ENOMEM;
1274
Patrick McHardy9c547952007-12-17 21:52:00 -08001275 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001276 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1277 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 tmp.size) != 0) {
1279 ret = -EFAULT;
1280 goto free_newinfo;
1281 }
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001284 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 tmp.hook_entry, tmp.underflow);
1286 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001287 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 duprintf("ip_tables: Translated table\n");
1290
Patrick McHardy4b478242007-12-17 21:46:15 -08001291 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1292 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001293 if (ret)
1294 goto free_newinfo_untrans;
1295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Dmitry Mishin27229712006-04-01 02:25:19 -08001297 free_newinfo_untrans:
Patrick McHardy9c547952007-12-17 21:52:00 -08001298 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001300 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return ret;
1302}
1303
1304/* We're lazy, and add to the first CPU; overflow works its fey magic
1305 * and everything is OK. */
1306static inline int
1307add_counter_to_entry(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001308 const struct xt_counters addme[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 unsigned int *i)
1310{
1311#if 0
1312 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1313 *i,
1314 (long unsigned int)e->counters.pcnt,
1315 (long unsigned int)e->counters.bcnt,
1316 (long unsigned int)addme[*i].pcnt,
1317 (long unsigned int)addme[*i].bcnt);
1318#endif
1319
1320 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1321
1322 (*i)++;
1323 return 0;
1324}
1325
1326static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001327do_add_counters(void __user *user, unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 unsigned int i;
Dmitry Mishin27229712006-04-01 02:25:19 -08001330 struct xt_counters_info tmp;
1331 struct xt_counters *paddc;
1332 unsigned int num_counters;
1333 char *name;
1334 int size;
1335 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001336 struct xt_table *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001337 struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001339 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001340#ifdef CONFIG_COMPAT
1341 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Dmitry Mishin27229712006-04-01 02:25:19 -08001343 if (compat) {
1344 ptmp = &compat_tmp;
1345 size = sizeof(struct compat_xt_counters_info);
1346 } else
1347#endif
1348 {
1349 ptmp = &tmp;
1350 size = sizeof(struct xt_counters_info);
1351 }
1352
1353 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EFAULT;
1355
Dmitry Mishin27229712006-04-01 02:25:19 -08001356#ifdef CONFIG_COMPAT
1357 if (compat) {
1358 num_counters = compat_tmp.num_counters;
1359 name = compat_tmp.name;
1360 } else
1361#endif
1362 {
1363 num_counters = tmp.num_counters;
1364 name = tmp.name;
1365 }
1366
1367 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return -EINVAL;
1369
Dmitry Mishin27229712006-04-01 02:25:19 -08001370 paddc = vmalloc_node(len - size, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (!paddc)
1372 return -ENOMEM;
1373
Dmitry Mishin27229712006-04-01 02:25:19 -08001374 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 ret = -EFAULT;
1376 goto free;
1377 }
1378
Dmitry Mishin27229712006-04-01 02:25:19 -08001379 t = xt_find_table_lock(AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (!t || IS_ERR(t)) {
1381 ret = t ? PTR_ERR(t) : -ENOENT;
1382 goto free;
1383 }
1384
1385 write_lock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001386 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001387 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ret = -EINVAL;
1389 goto unlock_up_free;
1390 }
1391
1392 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001393 /* Choose the copy that is on our node */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001394 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -08001395 IPT_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001396 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 add_counter_to_entry,
Dmitry Mishin27229712006-04-01 02:25:19 -08001398 paddc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 &i);
1400 unlock_up_free:
1401 write_unlock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001402 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 module_put(t->me);
1404 free:
1405 vfree(paddc);
1406
1407 return ret;
1408}
1409
Dmitry Mishin27229712006-04-01 02:25:19 -08001410#ifdef CONFIG_COMPAT
1411struct compat_ipt_replace {
1412 char name[IPT_TABLE_MAXNAMELEN];
1413 u32 valid_hooks;
1414 u32 num_entries;
1415 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001416 u32 hook_entry[NF_INET_NUMHOOKS];
1417 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001418 u32 num_counters;
1419 compat_uptr_t counters; /* struct ipt_counters * */
1420 struct compat_ipt_entry entries[0];
1421};
1422
Patrick McHardya18aa312007-12-12 10:35:16 -08001423static int
1424compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
1425 compat_uint_t *size, struct xt_counters *counters,
1426 unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001427{
Al Viro3e597c62006-09-24 23:42:20 +01001428 struct ipt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001429 struct compat_ipt_entry __user *ce;
1430 u_int16_t target_offset, next_offset;
1431 compat_uint_t origsize;
1432 int ret;
1433
1434 ret = -EFAULT;
1435 origsize = *size;
1436 ce = (struct compat_ipt_entry __user *)*dstptr;
Patrick McHardy78000072006-05-03 23:20:27 -07001437 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
Dmitry Mishin27229712006-04-01 02:25:19 -08001438 goto out;
1439
Patrick McHardya18aa312007-12-12 10:35:16 -08001440 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1441 goto out;
1442
Dmitry Mishin27229712006-04-01 02:25:19 -08001443 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001444 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1445
Patrick McHardyac8e27f2007-12-17 21:45:52 -08001446 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001447 target_offset = e->target_offset - (origsize - *size);
1448 if (ret)
1449 goto out;
1450 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001451 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001452 if (ret)
1453 goto out;
1454 ret = -EFAULT;
1455 next_offset = e->next_offset - (origsize - *size);
Patrick McHardy78000072006-05-03 23:20:27 -07001456 if (put_user(target_offset, &ce->target_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001457 goto out;
Patrick McHardy78000072006-05-03 23:20:27 -07001458 if (put_user(next_offset, &ce->next_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001459 goto out;
Patrick McHardya18aa312007-12-12 10:35:16 -08001460
1461 (*i)++;
Dmitry Mishin27229712006-04-01 02:25:19 -08001462 return 0;
1463out:
1464 return ret;
1465}
1466
1467static inline int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001468compat_find_calc_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001469 const char *name,
1470 const struct ipt_ip *ip,
1471 unsigned int hookmask,
1472 int *size, int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001473{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001474 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001475
1476 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001477 m->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001478 "ipt_%s", m->u.user.name);
1479 if (IS_ERR(match) || !match) {
1480 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001481 m->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001482 return match ? PTR_ERR(match) : -ENOENT;
1483 }
1484 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001485 *size += xt_compat_match_offset(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001486
1487 (*i)++;
1488 return 0;
1489}
1490
1491static inline int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001492compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1493{
1494 if (i && (*i)-- == 0)
1495 return 1;
1496
1497 module_put(m->u.kernel.match->me);
1498 return 0;
1499}
1500
1501static inline int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001502compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001503{
1504 struct ipt_entry_target *t;
1505
1506 if (i && (*i)-- == 0)
1507 return 1;
1508
1509 /* Cleanup all matches */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001510 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1511 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001512 module_put(t->u.kernel.target->me);
1513 return 0;
1514}
1515
1516static inline int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001517check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001518 struct xt_table_info *newinfo,
1519 unsigned int *size,
1520 unsigned char *base,
1521 unsigned char *limit,
1522 unsigned int *hook_entries,
1523 unsigned int *underflows,
1524 unsigned int *i,
1525 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001526{
1527 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001528 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001529 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001530 int ret, off, h, j;
1531
1532 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1533 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0
1534 || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1535 duprintf("Bad offset %p, limit = %p\n", e, limit);
1536 return -EINVAL;
1537 }
1538
1539 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001540 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001541 duprintf("checking: element %p size %u\n",
1542 e, e->next_offset);
1543 return -EINVAL;
1544 }
1545
Patrick McHardy73cd5982007-12-17 21:47:32 -08001546 /* For purposes of check_entry casting the compat entry is fine */
1547 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001548 if (ret)
1549 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001550
Patrick McHardy30c08c42007-12-17 21:47:14 -08001551 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001552 entry_offset = (void *)e - (void *)base;
1553 j = 0;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001554 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1555 &e->ip, e->comefrom, &off, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001556 if (ret != 0)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001557 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001558
Patrick McHardy73cd5982007-12-17 21:47:32 -08001559 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001560 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -08001561 t->u.user.name,
1562 t->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001563 "ipt_%s", t->u.user.name);
1564 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001565 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001566 t->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001567 ret = target ? PTR_ERR(target) : -ENOENT;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001568 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001569 }
1570 t->u.kernel.target = target;
1571
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001572 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001573 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001574 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001575 if (ret)
1576 goto out;
1577
1578 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001579 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001580 if ((unsigned char *)e - base == hook_entries[h])
1581 newinfo->hook_entry[h] = hook_entries[h];
1582 if ((unsigned char *)e - base == underflows[h])
1583 newinfo->underflow[h] = underflows[h];
1584 }
1585
1586 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001587 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001588 e->comefrom = 0;
1589
1590 (*i)++;
1591 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001592
Dmitry Mishin27229712006-04-01 02:25:19 -08001593out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001594 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001595release_matches:
1596 IPT_MATCH_ITERATE(e, compat_release_match, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001597 return ret;
1598}
1599
Patrick McHardy4b478242007-12-17 21:46:15 -08001600static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001601compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001602 unsigned int *size, const char *name,
1603 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001604{
1605 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001606 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001607 struct ipt_entry *de;
1608 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001609 int ret, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001610
1611 ret = 0;
1612 origsize = *size;
1613 de = (struct ipt_entry *)*dstptr;
1614 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001615 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001616
Patrick McHardy73cd5982007-12-17 21:47:32 -08001617 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001618 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1619
Patrick McHardy73cd5982007-12-17 21:47:32 -08001620 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1621 dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001622 if (ret)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001623 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001624 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001625 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001626 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001627 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001628
1629 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001630 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001631 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1632 newinfo->hook_entry[h] -= origsize - *size;
1633 if ((unsigned char *)de - base < newinfo->underflow[h])
1634 newinfo->underflow[h] -= origsize - *size;
1635 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001636 return ret;
1637}
Dmitry Mishin27229712006-04-01 02:25:19 -08001638
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001639static inline int compat_check_entry(struct ipt_entry *e, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001640 unsigned int *i)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001641{
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001642 int j, ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001643
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001644 j = 0;
Patrick McHardy9c547952007-12-17 21:52:00 -08001645 ret = IPT_MATCH_ITERATE(e, check_match, name, &e->ip,
1646 e->comefrom, &j);
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001647 if (ret)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001648 goto cleanup_matches;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001649
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001650 ret = check_target(e, name);
1651 if (ret)
1652 goto cleanup_matches;
1653
1654 (*i)++;
1655 return 0;
1656
1657 cleanup_matches:
1658 IPT_MATCH_ITERATE(e, cleanup_match, &j);
1659 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001660}
1661
Dmitry Mishin27229712006-04-01 02:25:19 -08001662static int
1663translate_compat_table(const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001664 unsigned int valid_hooks,
1665 struct xt_table_info **pinfo,
1666 void **pentry0,
1667 unsigned int total_size,
1668 unsigned int number,
1669 unsigned int *hook_entries,
1670 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001671{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001672 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001673 struct xt_table_info *newinfo, *info;
1674 void *pos, *entry0, *entry1;
1675 unsigned int size;
1676 int ret;
1677
1678 info = *pinfo;
1679 entry0 = *pentry0;
1680 size = total_size;
1681 info->number = number;
1682
1683 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001684 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001685 info->hook_entry[i] = 0xFFFFFFFF;
1686 info->underflow[i] = 0xFFFFFFFF;
1687 }
1688
1689 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001690 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001691 xt_compat_lock(AF_INET);
1692 /* Walk through entries, checking offsets. */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001693 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1694 check_compat_entry_size_and_hooks,
1695 info, &size, entry0,
1696 entry0 + total_size,
1697 hook_entries, underflows, &j, name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001698 if (ret != 0)
1699 goto out_unlock;
1700
1701 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001702 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001703 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001704 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001705 goto out_unlock;
1706 }
1707
1708 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001709 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001710 /* Only hooks which are valid */
1711 if (!(valid_hooks & (1 << i)))
1712 continue;
1713 if (info->hook_entry[i] == 0xFFFFFFFF) {
1714 duprintf("Invalid hook entry %u %u\n",
1715 i, hook_entries[i]);
1716 goto out_unlock;
1717 }
1718 if (info->underflow[i] == 0xFFFFFFFF) {
1719 duprintf("Invalid underflow %u %u\n",
1720 i, underflows[i]);
1721 goto out_unlock;
1722 }
1723 }
1724
1725 ret = -ENOMEM;
1726 newinfo = xt_alloc_table_info(size);
1727 if (!newinfo)
1728 goto out_unlock;
1729
1730 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001731 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001732 newinfo->hook_entry[i] = info->hook_entry[i];
1733 newinfo->underflow[i] = info->underflow[i];
1734 }
1735 entry1 = newinfo->entries[raw_smp_processor_id()];
1736 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001737 size = total_size;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001738 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
Patrick McHardy9c547952007-12-17 21:52:00 -08001739 compat_copy_entry_from_user,
1740 &pos, &size, name, newinfo, entry1);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001741 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001742 xt_compat_unlock(AF_INET);
1743 if (ret)
1744 goto free_newinfo;
1745
1746 ret = -ELOOP;
1747 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1748 goto free_newinfo;
1749
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001750 i = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001751 ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001752 name, &i);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001753 if (ret) {
1754 j -= i;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001755 COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1756 compat_release_entry, &j);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001757 IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1758 xt_free_table_info(newinfo);
1759 return ret;
1760 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001761
Dmitry Mishin27229712006-04-01 02:25:19 -08001762 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001763 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001764 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1765 memcpy(newinfo->entries[i], entry1, newinfo->size);
1766
1767 *pinfo = newinfo;
1768 *pentry0 = entry1;
1769 xt_free_table_info(info);
1770 return 0;
1771
1772free_newinfo:
1773 xt_free_table_info(newinfo);
1774out:
Patrick McHardy73cd5982007-12-17 21:47:32 -08001775 COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001776 return ret;
1777out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001778 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001779 xt_compat_unlock(AF_INET);
1780 goto out;
1781}
1782
1783static int
1784compat_do_replace(void __user *user, unsigned int len)
1785{
1786 int ret;
1787 struct compat_ipt_replace tmp;
1788 struct xt_table_info *newinfo;
1789 void *loc_cpu_entry;
1790
1791 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1792 return -EFAULT;
1793
Dmitry Mishin27229712006-04-01 02:25:19 -08001794 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001795 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001796 return -ENOMEM;
1797 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1798 return -ENOMEM;
1799
1800 newinfo = xt_alloc_table_info(tmp.size);
1801 if (!newinfo)
1802 return -ENOMEM;
1803
Patrick McHardy9c547952007-12-17 21:52:00 -08001804 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001805 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1806 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1807 tmp.size) != 0) {
1808 ret = -EFAULT;
1809 goto free_newinfo;
1810 }
1811
1812 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001813 &newinfo, &loc_cpu_entry, tmp.size,
1814 tmp.num_entries, tmp.hook_entry,
1815 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001816 if (ret != 0)
1817 goto free_newinfo;
1818
1819 duprintf("compat_do_replace: Translated table\n");
1820
Patrick McHardy4b478242007-12-17 21:46:15 -08001821 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1822 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001823 if (ret)
1824 goto free_newinfo_untrans;
1825 return 0;
1826
1827 free_newinfo_untrans:
Patrick McHardy4b478242007-12-17 21:46:15 -08001828 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001829 free_newinfo:
1830 xt_free_table_info(newinfo);
1831 return ret;
1832}
1833
1834static int
1835compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001836 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001837{
1838 int ret;
1839
1840 if (!capable(CAP_NET_ADMIN))
1841 return -EPERM;
1842
1843 switch (cmd) {
1844 case IPT_SO_SET_REPLACE:
1845 ret = compat_do_replace(user, len);
1846 break;
1847
1848 case IPT_SO_SET_ADD_COUNTERS:
1849 ret = do_add_counters(user, len, 1);
1850 break;
1851
1852 default:
1853 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1854 ret = -EINVAL;
1855 }
1856
1857 return ret;
1858}
1859
Patrick McHardy4b478242007-12-17 21:46:15 -08001860struct compat_ipt_get_entries {
Dmitry Mishin27229712006-04-01 02:25:19 -08001861 char name[IPT_TABLE_MAXNAMELEN];
1862 compat_uint_t size;
1863 struct compat_ipt_entry entrytable[0];
1864};
1865
Patrick McHardy4b478242007-12-17 21:46:15 -08001866static int
1867compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1868 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001869{
Dmitry Mishin27229712006-04-01 02:25:19 -08001870 struct xt_counters *counters;
1871 struct xt_table_info *private = table->private;
1872 void __user *pos;
1873 unsigned int size;
1874 int ret = 0;
1875 void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001876 unsigned int i = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001877
1878 counters = alloc_counters(table);
1879 if (IS_ERR(counters))
1880 return PTR_ERR(counters);
1881
1882 /* choose the copy that is on our node/cpu, ...
1883 * This choice is lazy (because current thread is
1884 * allowed to migrate to another cpu)
1885 */
1886 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1887 pos = userptr;
1888 size = total_size;
1889 ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
Patrick McHardya18aa312007-12-12 10:35:16 -08001890 compat_copy_entry_to_user,
1891 &pos, &size, counters, &i);
Dmitry Mishin27229712006-04-01 02:25:19 -08001892
Dmitry Mishin27229712006-04-01 02:25:19 -08001893 vfree(counters);
1894 return ret;
1895}
1896
1897static int
1898compat_get_entries(struct compat_ipt_get_entries __user *uptr, int *len)
1899{
1900 int ret;
1901 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001902 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001903
Dmitry Mishin27229712006-04-01 02:25:19 -08001904 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001905 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001906 return -EINVAL;
1907 }
1908
1909 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1910 return -EFAULT;
1911
1912 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001913 duprintf("compat_get_entries: %u != %zu\n",
1914 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001915 return -EINVAL;
1916 }
1917
1918 xt_compat_lock(AF_INET);
1919 t = xt_find_table_lock(AF_INET, get.name);
1920 if (t && !IS_ERR(t)) {
1921 struct xt_table_info *private = t->private;
1922 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08001923 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001924 ret = compat_table_info(private, &info);
1925 if (!ret && get.size == info.size) {
1926 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001927 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001928 } else if (!ret) {
1929 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001930 private->size, get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001931 ret = -EINVAL;
1932 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001933 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001934 module_put(t->me);
1935 xt_table_unlock(t);
1936 } else
1937 ret = t ? PTR_ERR(t) : -ENOENT;
1938
1939 xt_compat_unlock(AF_INET);
1940 return ret;
1941}
1942
Patrick McHardy79030ed2006-09-20 12:05:08 -07001943static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1944
Dmitry Mishin27229712006-04-01 02:25:19 -08001945static int
1946compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1947{
1948 int ret;
1949
Björn Steinbrink82fac052006-10-20 00:21:10 -07001950 if (!capable(CAP_NET_ADMIN))
1951 return -EPERM;
1952
Dmitry Mishin27229712006-04-01 02:25:19 -08001953 switch (cmd) {
1954 case IPT_SO_GET_INFO:
1955 ret = get_info(user, len, 1);
1956 break;
1957 case IPT_SO_GET_ENTRIES:
1958 ret = compat_get_entries(user, len);
1959 break;
1960 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001961 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001962 }
1963 return ret;
1964}
1965#endif
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001968do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 int ret;
1971
1972 if (!capable(CAP_NET_ADMIN))
1973 return -EPERM;
1974
1975 switch (cmd) {
1976 case IPT_SO_SET_REPLACE:
1977 ret = do_replace(user, len);
1978 break;
1979
1980 case IPT_SO_SET_ADD_COUNTERS:
Dmitry Mishin27229712006-04-01 02:25:19 -08001981 ret = do_add_counters(user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 break;
1983
1984 default:
1985 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1986 ret = -EINVAL;
1987 }
1988
1989 return ret;
1990}
1991
1992static int
1993do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1994{
1995 int ret;
1996
1997 if (!capable(CAP_NET_ADMIN))
1998 return -EPERM;
1999
2000 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002001 case IPT_SO_GET_INFO:
2002 ret = get_info(user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002004
2005 case IPT_SO_GET_ENTRIES:
2006 ret = get_entries(user, len);
2007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 case IPT_SO_GET_REVISION_MATCH:
2010 case IPT_SO_GET_REVISION_TARGET: {
2011 struct ipt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002012 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 if (*len != sizeof(rev)) {
2015 ret = -EINVAL;
2016 break;
2017 }
2018 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2019 ret = -EFAULT;
2020 break;
2021 }
2022
2023 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002024 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002026 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Harald Welte2e4e6a12006-01-12 13:30:04 -08002028 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2029 rev.revision,
2030 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 "ipt_%s", rev.name);
2032 break;
2033 }
2034
2035 default:
2036 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2037 ret = -EINVAL;
2038 }
2039
2040 return ret;
2041}
2042
Harald Welte2e4e6a12006-01-12 13:30:04 -08002043int ipt_register_table(struct xt_table *table, const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002046 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08002047 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08002049 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Harald Welte2e4e6a12006-01-12 13:30:04 -08002051 newinfo = xt_alloc_table_info(repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (!newinfo)
2053 return -ENOMEM;
2054
Patrick McHardy9c547952007-12-17 21:52:00 -08002055 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002056 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2057 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08002060 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 repl->num_entries,
2062 repl->hook_entry,
2063 repl->underflow);
2064 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08002065 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return ret;
2067 }
2068
Patrick McHardyda298d32006-06-27 03:00:09 -07002069 ret = xt_register_table(table, &bootstrap, newinfo);
2070 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08002071 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return ret;
2073 }
2074
Harald Welte2e4e6a12006-01-12 13:30:04 -08002075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
Jan Engelhardte60a13e2007-02-07 15:12:33 -08002078void ipt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002080 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002081 void *loc_cpu_entry;
2082
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002083 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002086 loc_cpu_entry = private->entries[raw_smp_processor_id()];
2087 IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
2088 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
2091/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002092static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2094 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002095 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Patrick McHardy9c547952007-12-17 21:52:00 -08002097 return ((test_type == 0xFF) ||
2098 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 ^ invert;
2100}
2101
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002102static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103icmp_match(const struct sk_buff *skb,
2104 const struct net_device *in,
2105 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -08002106 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 const void *matchinfo,
2108 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -08002109 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -07002110 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
2112 struct icmphdr _icmph, *ic;
2113 const struct ipt_icmp *icmpinfo = matchinfo;
2114
2115 /* Must not be a fragment. */
2116 if (offset)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002117 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Harald Welte2e4e6a12006-01-12 13:30:04 -08002119 ic = skb_header_pointer(skb, protoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (ic == NULL) {
2121 /* We've been asked to examine this packet, and we
2122 * can't. Hence, no choice but to drop.
2123 */
2124 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -07002125 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002126 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128
2129 return icmp_type_code_match(icmpinfo->type,
2130 icmpinfo->code[0],
2131 icmpinfo->code[1],
2132 ic->type, ic->code,
2133 !!(icmpinfo->invflags&IPT_ICMP_INV));
2134}
2135
2136/* Called when user tries to insert an entry of this type. */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07002137static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138icmp_checkentry(const char *tablename,
Patrick McHardy9c547952007-12-17 21:52:00 -08002139 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -08002140 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 unsigned int hook_mask)
2143{
2144 const struct ipt_icmp *icmpinfo = matchinfo;
2145
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002146 /* Must specify no unknown invflags */
2147 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
2150/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07002151static struct xt_target ipt_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 .name = IPT_STANDARD_TARGET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002153 .targetsize = sizeof(int),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002154 .family = AF_INET,
Dmitry Mishin27229712006-04-01 02:25:19 -08002155#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07002156 .compatsize = sizeof(compat_int_t),
2157 .compat_from_user = compat_standard_from_user,
2158 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160};
2161
Patrick McHardy9f15c532007-07-07 22:22:02 -07002162static struct xt_target ipt_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 .name = IPT_ERROR_TARGET,
2164 .target = ipt_error,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002165 .targetsize = IPT_FUNCTION_MAXNAMELEN,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002166 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167};
2168
2169static struct nf_sockopt_ops ipt_sockopts = {
2170 .pf = PF_INET,
2171 .set_optmin = IPT_BASE_CTL,
2172 .set_optmax = IPT_SO_SET_MAX+1,
2173 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002174#ifdef CONFIG_COMPAT
2175 .compat_set = compat_do_ipt_set_ctl,
2176#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 .get_optmin = IPT_BASE_CTL,
2178 .get_optmax = IPT_SO_GET_MAX+1,
2179 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002180#ifdef CONFIG_COMPAT
2181 .compat_get = compat_do_ipt_get_ctl,
2182#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002183 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184};
2185
Patrick McHardy9f15c532007-07-07 22:22:02 -07002186static struct xt_match icmp_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 .name = "icmp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002188 .match = icmp_match,
2189 .matchsize = sizeof(struct ipt_icmp),
Patrick McHardy9c547952007-12-17 21:52:00 -08002190 .checkentry = icmp_checkentry,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002191 .proto = IPPROTO_ICMP,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002192 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193};
2194
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002195static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 int ret;
2198
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002199 ret = xt_proto_init(AF_INET);
2200 if (ret < 0)
2201 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002204 ret = xt_register_target(&ipt_standard_target);
2205 if (ret < 0)
2206 goto err2;
2207 ret = xt_register_target(&ipt_error_target);
2208 if (ret < 0)
2209 goto err3;
2210 ret = xt_register_match(&icmp_matchstruct);
2211 if (ret < 0)
2212 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /* Register setsockopt */
2215 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002216 if (ret < 0)
2217 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Dan Aloni0236e662007-07-09 13:20:12 -07002219 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002221
2222err5:
2223 xt_unregister_match(&icmp_matchstruct);
2224err4:
2225 xt_unregister_target(&ipt_error_target);
2226err3:
2227 xt_unregister_target(&ipt_standard_target);
2228err2:
2229 xt_proto_fini(AF_INET);
2230err1:
2231 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002234static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
2236 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002237
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002238 xt_unregister_match(&icmp_matchstruct);
2239 xt_unregister_target(&ipt_error_target);
2240 xt_unregister_target(&ipt_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002241
2242 xt_proto_fini(AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
2245EXPORT_SYMBOL(ipt_register_table);
2246EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002248module_init(ip_tables_init);
2249module_exit(ip_tables_fini);