blob: ef8b6ca068b280aabe538d58af8da7c580d09b2b [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", \
Harvey Harrison0dc47872008-03-05 20:47:47 -080056 __func__, __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070057} 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. */
Denys Vlasenko022748a2008-01-14 23:44:05 -080078/* Performance critical - called for every packet */
Patrick McHardy9c547952007-12-17 21:52:00 -080079static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070080ip_packet_match(const struct iphdr *ip,
81 const char *indev,
82 const char *outdev,
83 const struct ipt_ip *ipinfo,
84 int isfrag)
85{
86 size_t i;
87 unsigned long ret;
88
Jan Engelhardte79ec502007-12-17 22:44:06 -080089#define FWINV(bool, invflg) ((bool) ^ !!(ipinfo->invflags & (invflg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
92 IPT_INV_SRCIP)
93 || FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
94 IPT_INV_DSTIP)) {
95 dprintf("Source or dest mismatch.\n");
96
Harvey Harrisoncffee382008-10-31 00:53:08 -070097 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
98 &ip->saddr, &ipinfo->smsk.s_addr, &ipinfo->src.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
Harvey Harrisoncffee382008-10-31 00:53:08 -0700100 dprintf("DST: %pI4 Mask: %pI4 Target: %pI4.%s\n",
101 &ip->daddr, &ipinfo->dmsk.s_addr, &ipinfo->dst.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800103 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
106 /* Look for ifname matches; this should unroll nicely. */
107 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
108 ret |= (((const unsigned long *)indev)[i]
109 ^ ((const unsigned long *)ipinfo->iniface)[i])
110 & ((const unsigned long *)ipinfo->iniface_mask)[i];
111 }
112
113 if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
114 dprintf("VIA in mismatch (%s vs %s).%s\n",
115 indev, ipinfo->iniface,
116 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800117 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119
120 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
121 ret |= (((const unsigned long *)outdev)[i]
122 ^ ((const unsigned long *)ipinfo->outiface)[i])
123 & ((const unsigned long *)ipinfo->outiface_mask)[i];
124 }
125
126 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
127 dprintf("VIA out mismatch (%s vs %s).%s\n",
128 outdev, ipinfo->outiface,
129 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800130 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132
133 /* Check specific protocol */
134 if (ipinfo->proto
135 && FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
136 dprintf("Packet protocol %hi does not match %hi.%s\n",
137 ip->protocol, ipinfo->proto,
138 ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800139 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141
142 /* If we have a fragment rule but the packet is not a fragment
143 * then we return zero */
144 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
145 dprintf("Fragment rule but not fragment.%s\n",
146 ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800147 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149
Patrick McHardy9c547952007-12-17 21:52:00 -0800150 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Denys Vlasenko022748a2008-01-14 23:44:05 -0800153static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154ip_checkentry(const struct ipt_ip *ip)
155{
156 if (ip->flags & ~IPT_F_MASK) {
157 duprintf("Unknown flag bits set: %08X\n",
158 ip->flags & ~IPT_F_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700159 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161 if (ip->invflags & ~IPT_INV_MASK) {
162 duprintf("Unknown invflag bits set: %08X\n",
163 ip->invflags & ~IPT_INV_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700164 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700166 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200170ipt_error(struct sk_buff *skb, const struct xt_target_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 if (net_ratelimit())
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200173 printk("ip_tables: error: `%s'\n",
174 (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 return NF_DROP;
177}
178
Denys Vlasenko022748a2008-01-14 23:44:05 -0800179/* Performance critical - called for every packet */
180static inline bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200181do_match(struct ipt_entry_match *m, const struct sk_buff *skb,
182 struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200184 par->match = m->u.kernel.match;
185 par->matchinfo = m->data;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* Stop iteration if it doesn't match */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200188 if (!m->u.kernel.match->match(skb, par))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700189 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 else
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700191 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Denys Vlasenko022748a2008-01-14 23:44:05 -0800194/* Performance critical */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static inline struct ipt_entry *
196get_entry(void *base, unsigned int offset)
197{
198 return (struct ipt_entry *)(base + offset);
199}
200
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700201/* All zeroes == unconditional rule. */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800202/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700203static inline int
204unconditional(const struct ipt_ip *ip)
205{
206 unsigned int i;
207
208 for (i = 0; i < sizeof(*ip)/sizeof(__u32); i++)
209 if (((__u32 *)ip)[i])
210 return 0;
211
212 return 1;
Jan Engelhardte79ec502007-12-17 22:44:06 -0800213#undef FWINV
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700214}
215
216#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
217 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
Denys Vlasenko022748a2008-01-14 23:44:05 -0800218static const char *const hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800219 [NF_INET_PRE_ROUTING] = "PREROUTING",
220 [NF_INET_LOCAL_IN] = "INPUT",
Patrick McHardy9c547952007-12-17 21:52:00 -0800221 [NF_INET_FORWARD] = "FORWARD",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800222 [NF_INET_LOCAL_OUT] = "OUTPUT",
223 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700224};
225
226enum nf_ip_trace_comments {
227 NF_IP_TRACE_COMMENT_RULE,
228 NF_IP_TRACE_COMMENT_RETURN,
229 NF_IP_TRACE_COMMENT_POLICY,
230};
231
Denys Vlasenko022748a2008-01-14 23:44:05 -0800232static const char *const comments[] = {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700233 [NF_IP_TRACE_COMMENT_RULE] = "rule",
234 [NF_IP_TRACE_COMMENT_RETURN] = "return",
235 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
236};
237
238static struct nf_loginfo trace_loginfo = {
239 .type = NF_LOG_TYPE_LOG,
240 .u = {
241 .log = {
242 .level = 4,
243 .logflags = NF_LOG_MASK,
244 },
245 },
246};
247
Denys Vlasenko022748a2008-01-14 23:44:05 -0800248/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700249static inline int
250get_chainname_rulenum(struct ipt_entry *s, struct ipt_entry *e,
251 char *hookname, char **chainname,
252 char **comment, unsigned int *rulenum)
253{
254 struct ipt_standard_target *t = (void *)ipt_get_target(s);
255
256 if (strcmp(t->target.u.kernel.target->name, IPT_ERROR_TARGET) == 0) {
257 /* Head of user chain: ERROR target with chainname */
258 *chainname = t->target.data;
259 (*rulenum) = 0;
260 } else if (s == e) {
261 (*rulenum)++;
262
263 if (s->target_offset == sizeof(struct ipt_entry)
264 && strcmp(t->target.u.kernel.target->name,
265 IPT_STANDARD_TARGET) == 0
266 && t->verdict < 0
267 && unconditional(&s->ip)) {
268 /* Tail of chains: STANDARD target (return/policy) */
269 *comment = *chainname == hookname
270 ? (char *)comments[NF_IP_TRACE_COMMENT_POLICY]
271 : (char *)comments[NF_IP_TRACE_COMMENT_RETURN];
272 }
273 return 1;
274 } else
275 (*rulenum)++;
276
277 return 0;
278}
279
280static void trace_packet(struct sk_buff *skb,
281 unsigned int hook,
282 const struct net_device *in,
283 const struct net_device *out,
Jan Engelhardtecb6f852008-01-31 03:54:47 -0800284 const char *tablename,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700285 struct xt_table_info *private,
286 struct ipt_entry *e)
287{
288 void *table_base;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200289 const struct ipt_entry *root;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700290 char *hookname, *chainname, *comment;
291 unsigned int rulenum = 0;
292
293 table_base = (void *)private->entries[smp_processor_id()];
294 root = get_entry(table_base, private->hook_entry[hook]);
295
296 hookname = chainname = (char *)hooknames[hook];
297 comment = (char *)comments[NF_IP_TRACE_COMMENT_RULE];
298
299 IPT_ENTRY_ITERATE(root,
300 private->size - private->hook_entry[hook],
301 get_chainname_rulenum,
302 e, hookname, &chainname, &comment, &rulenum);
303
304 nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo,
305 "TRACE: %s:%s:%s:%u ",
306 tablename, chainname, comment, rulenum);
307}
308#endif
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/* Returns one of the generic firewall policies, like NF_ACCEPT. */
311unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700312ipt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned int hook,
314 const struct net_device *in,
315 const struct net_device *out,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800316 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Jan Engelhardt5452e422008-04-14 11:15:35 +0200319 const struct iphdr *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 u_int16_t datalen;
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700321 bool hotdrop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* Initializing verdict to NF_DROP keeps gcc happy. */
323 unsigned int verdict = NF_DROP;
324 const char *indev, *outdev;
325 void *table_base;
326 struct ipt_entry *e, *back;
Patrick McHardy83117312006-08-17 18:13:53 -0700327 struct xt_table_info *private;
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200328 struct xt_match_param mtpar;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200329 struct xt_target_param tgpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* Initialization */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700332 ip = ip_hdr(skb);
333 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 indev = in ? in->name : nulldevname;
335 outdev = out ? out->name : nulldevname;
336 /* We handle fragments by dealing with the first fragment as
337 * if it was a normal packet. All other fragments are treated
338 * normally, except that they will NEVER match rules that ask
339 * things we don't know, ie. tcp syn flag or ports). If the
340 * rule is also a fragment-specific rule, non-fragments won't
341 * match it. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200342 mtpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
343 mtpar.thoff = ip_hdrlen(skb);
344 mtpar.hotdrop = &hotdrop;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200345 mtpar.in = tgpar.in = in;
346 mtpar.out = tgpar.out = out;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200347 mtpar.family = tgpar.family = NFPROTO_IPV4;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200348 tgpar.hooknum = hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 read_lock_bh(&table->lock);
351 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Patrick McHardy83117312006-08-17 18:13:53 -0700352 private = table->private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800353 table_base = (void *)private->entries[smp_processor_id()];
354 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* For return from builtin chain */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800357 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 do {
360 IP_NF_ASSERT(e);
361 IP_NF_ASSERT(back);
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200362 if (ip_packet_match(ip, indev, outdev,
363 &e->ip, mtpar.fragoff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct ipt_entry_target *t;
365
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200366 if (IPT_MATCH_ITERATE(e, do_match, skb, &mtpar) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto no_match;
368
369 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
370
371 t = ipt_get_target(e);
372 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700373
374#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
375 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
376 /* The packet is traced: log it */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700377 if (unlikely(skb->nf_trace))
378 trace_packet(skb, hook, in, out,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700379 table->name, private, e);
380#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /* Standard target? */
382 if (!t->u.kernel.target->target) {
383 int v;
384
385 v = ((struct ipt_standard_target *)t)->verdict;
386 if (v < 0) {
387 /* Pop from stack? */
388 if (v != IPT_RETURN) {
389 verdict = (unsigned)(-v) - 1;
390 break;
391 }
392 e = back;
393 back = get_entry(table_base,
394 back->comefrom);
395 continue;
396 }
Patrick McHardy05465342005-08-21 23:31:43 -0700397 if (table_base + v != (void *)e + e->next_offset
398 && !(e->ip.flags & IPT_F_GOTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* Save old back ptr in next entry */
400 struct ipt_entry *next
401 = (void *)e + e->next_offset;
402 next->comefrom
403 = (void *)back - table_base;
404 /* set back pointer to next entry */
405 back = next;
406 }
407
408 e = get_entry(table_base, v);
409 } else {
410 /* Targets which reenter must return
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900411 abs. verdicts */
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200412 tgpar.target = t->u.kernel.target;
413 tgpar.targinfo = t->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#ifdef CONFIG_NETFILTER_DEBUG
415 ((struct ipt_entry *)table_base)->comefrom
416 = 0xeeeeeeec;
417#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700418 verdict = t->u.kernel.target->target(skb,
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200419 &tgpar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420#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
Denys Vlasenko022748a2008-01-14 23:44:05 -0800569static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570cleanup_match(struct ipt_entry_match *m, unsigned int *i)
571{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200572 struct xt_mtdtor_param par;
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (i && (*i)-- == 0)
575 return 1;
576
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200577 par.match = m->u.kernel.match;
578 par.matchinfo = m->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200579 par.family = NFPROTO_IPV4;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200580 if (par.match->destroy != NULL)
581 par.match->destroy(&par);
582 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
584}
585
Denys Vlasenko022748a2008-01-14 23:44:05 -0800586static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800587check_entry(struct ipt_entry *e, const char *name)
588{
589 struct ipt_entry_target *t;
590
591 if (!ip_checkentry(&e->ip)) {
592 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
593 return -EINVAL;
594 }
595
Patrick McHardy9c547952007-12-17 21:52:00 -0800596 if (e->target_offset + sizeof(struct ipt_entry_target) >
597 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800598 return -EINVAL;
599
600 t = ipt_get_target(e);
601 if (e->target_offset + t->u.target_size > e->next_offset)
602 return -EINVAL;
603
604 return 0;
605}
606
Denys Vlasenko022748a2008-01-14 23:44:05 -0800607static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200608check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
609 unsigned int *i)
Dmitry Mishina96be242006-12-12 00:29:26 -0800610{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200611 const struct ipt_ip *ip = par->entryinfo;
Dmitry Mishina96be242006-12-12 00:29:26 -0800612 int ret;
613
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200614 par->match = m->u.kernel.match;
615 par->matchinfo = m->data;
616
Jan Engelhardt916a9172008-10-08 11:35:20 +0200617 ret = xt_check_match(par, m->u.match_size - sizeof(*m),
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200618 ip->proto, ip->invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200619 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800620 duprintf("ip_tables: check failed for `%s'.\n",
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200621 par.match->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200622 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800623 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200624 ++*i;
625 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800626}
627
Denys Vlasenko022748a2008-01-14 23:44:05 -0800628static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200629find_check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
Patrick McHardy4b478242007-12-17 21:46:15 -0800630 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
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200644 ret = check_match(m, par, 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
Denys Vlasenko022748a2008-01-14 23:44:05 -0800654static int check_target(struct ipt_entry *e, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800655{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200656 struct ipt_entry_target *t = ipt_get_target(e);
657 struct xt_tgchk_param par = {
658 .table = name,
659 .entryinfo = e,
660 .target = t->u.kernel.target,
661 .targinfo = t->data,
662 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200663 .family = NFPROTO_IPV4,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200664 };
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900665 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800666
Jan Engelhardt916a9172008-10-08 11:35:20 +0200667 ret = xt_check_target(&par, t->u.target_size - sizeof(*t),
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200668 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200669 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800670 duprintf("ip_tables: check failed for `%s'.\n",
671 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200672 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800673 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200674 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800675}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Denys Vlasenko022748a2008-01-14 23:44:05 -0800677static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800678find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
Patrick McHardy4b478242007-12-17 21:46:15 -0800679 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800682 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int ret;
684 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200685 struct xt_mtchk_param mtpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Dmitry Mishina96be242006-12-12 00:29:26 -0800687 ret = check_entry(e, name);
688 if (ret)
689 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200692 mtpar.table = name;
693 mtpar.entryinfo = &e->ip;
694 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200695 mtpar.family = NFPROTO_IPV4;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200696 ret = IPT_MATCH_ITERATE(e, find_check_match, &mtpar, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (ret != 0)
698 goto cleanup_matches;
699
700 t = ipt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800701 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -0800702 t->u.user.name,
703 t->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 "ipt_%s", t->u.user.name);
705 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800706 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 ret = target ? PTR_ERR(target) : -ENOENT;
708 goto cleanup_matches;
709 }
710 t->u.kernel.target = target;
711
Dmitry Mishina96be242006-12-12 00:29:26 -0800712 ret = check_target(e, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800713 if (ret)
714 goto err;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 (*i)++;
717 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800718 err:
719 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 cleanup_matches:
721 IPT_MATCH_ITERATE(e, cleanup_match, &j);
722 return ret;
723}
724
Denys Vlasenko022748a2008-01-14 23:44:05 -0800725static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800727 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned char *base,
729 unsigned char *limit,
730 const unsigned int *hook_entries,
731 const unsigned int *underflows,
732 unsigned int *i)
733{
734 unsigned int h;
735
736 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0
737 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
738 duprintf("Bad offset %p\n", e);
739 return -EINVAL;
740 }
741
742 if (e->next_offset
743 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
744 duprintf("checking: element %p size %u\n",
745 e, e->next_offset);
746 return -EINVAL;
747 }
748
749 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800750 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if ((unsigned char *)e - base == hook_entries[h])
752 newinfo->hook_entry[h] = hook_entries[h];
753 if ((unsigned char *)e - base == underflows[h])
754 newinfo->underflow[h] = underflows[h];
755 }
756
757 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900758 < 0 (not IPT_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800761 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 e->comefrom = 0;
763
764 (*i)++;
765 return 0;
766}
767
Denys Vlasenko022748a2008-01-14 23:44:05 -0800768static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769cleanup_entry(struct ipt_entry *e, unsigned int *i)
770{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200771 struct xt_tgdtor_param par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct ipt_entry_target *t;
773
774 if (i && (*i)-- == 0)
775 return 1;
776
777 /* Cleanup all matches */
778 IPT_MATCH_ITERATE(e, cleanup_match, NULL);
779 t = ipt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200780
781 par.target = t->u.kernel.target;
782 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200783 par.family = NFPROTO_IPV4;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200784 if (par.target->destroy != NULL)
785 par.target->destroy(&par);
786 module_put(par.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return 0;
788}
789
790/* Checks and translates the user-supplied table segment (held in
791 newinfo) */
792static int
793translate_table(const char *name,
794 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800795 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800796 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 unsigned int size,
798 unsigned int number,
799 const unsigned int *hook_entries,
800 const unsigned int *underflows)
801{
802 unsigned int i;
803 int ret;
804
805 newinfo->size = size;
806 newinfo->number = number;
807
808 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800809 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 newinfo->hook_entry[i] = 0xFFFFFFFF;
811 newinfo->underflow[i] = 0xFFFFFFFF;
812 }
813
814 duprintf("translate_table: size %u\n", newinfo->size);
815 i = 0;
816 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800817 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 check_entry_size_and_hooks,
819 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800820 entry0,
821 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 hook_entries, underflows, &i);
823 if (ret != 0)
824 return ret;
825
826 if (i != number) {
827 duprintf("translate_table: %u not %u entries\n",
828 i, number);
829 return -EINVAL;
830 }
831
832 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800833 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* Only hooks which are valid */
835 if (!(valid_hooks & (1 << i)))
836 continue;
837 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
838 duprintf("Invalid hook entry %u %u\n",
839 i, hook_entries[i]);
840 return -EINVAL;
841 }
842 if (newinfo->underflow[i] == 0xFFFFFFFF) {
843 duprintf("Invalid underflow %u %u\n",
844 i, underflows[i]);
845 return -EINVAL;
846 }
847 }
848
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800849 if (!mark_source_chains(newinfo, valid_hooks, entry0))
850 return -ELOOP;
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* Finally, each sanity check must pass */
853 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800854 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Dmitry Mishina96be242006-12-12 00:29:26 -0800855 find_check_entry, name, size, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800857 if (ret != 0) {
858 IPT_ENTRY_ITERATE(entry0, newinfo->size,
859 cleanup_entry, &i);
860 return ret;
861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700864 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800865 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
866 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
869 return ret;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/* Gets counters. */
873static inline int
874add_entry_to_counter(const struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800875 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned int *i)
877{
878 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
879
880 (*i)++;
881 return 0;
882}
883
Eric Dumazet31836062005-12-13 23:13:48 -0800884static inline int
885set_entry_to_counter(const struct ipt_entry *e,
886 struct ipt_counters total[],
887 unsigned int *i)
888{
889 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
890
891 (*i)++;
892 return 0;
893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800896get_counters(const struct xt_table_info *t,
897 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 unsigned int cpu;
900 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800901 unsigned int curcpu;
902
903 /* Instead of clearing (by a previous call to memset())
904 * the counters and using adds, we set the counters
905 * with data used by 'current' CPU
906 * We dont care about preemption here.
907 */
908 curcpu = raw_smp_processor_id();
909
910 i = 0;
911 IPT_ENTRY_ITERATE(t->entries[curcpu],
912 t->size,
913 set_entry_to_counter,
914 counters,
915 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700917 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800918 if (cpu == curcpu)
919 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800921 IPT_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 t->size,
923 add_entry_to_counter,
924 counters,
925 &i);
926 }
927}
928
Denys Vlasenko022748a2008-01-14 23:44:05 -0800929static struct xt_counters * alloc_counters(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Dmitry Mishin27229712006-04-01 02:25:19 -0800931 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800932 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200933 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* We need atomic snapshot of counters: rest doesn't change
936 (other than comefrom, which userspace doesn't care
937 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800938 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet31836062005-12-13 23:13:48 -0800939 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 if (counters == NULL)
Dmitry Mishin27229712006-04-01 02:25:19 -0800942 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 write_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800946 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 write_unlock_bh(&table->lock);
948
Dmitry Mishin27229712006-04-01 02:25:19 -0800949 return counters;
950}
951
952static int
953copy_entries_to_user(unsigned int total_size,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800954 struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800955 void __user *userptr)
956{
957 unsigned int off, num;
958 struct ipt_entry *e;
959 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200960 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800961 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200962 const void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -0800963
964 counters = alloc_counters(table);
965 if (IS_ERR(counters))
966 return PTR_ERR(counters);
967
Eric Dumazet31836062005-12-13 23:13:48 -0800968 /* choose the copy that is on our node/cpu, ...
969 * This choice is lazy (because current thread is
970 * allowed to migrate to another cpu)
971 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800972 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800973 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 ret = -EFAULT;
975 goto free_counters;
976 }
977
978 /* FIXME: use iterator macros --RR */
979 /* ... then go back and fix counters and names */
980 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
981 unsigned int i;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200982 const struct ipt_entry_match *m;
983 const struct ipt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Eric Dumazet31836062005-12-13 23:13:48 -0800985 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (copy_to_user(userptr + off
987 + offsetof(struct ipt_entry, counters),
988 &counters[num],
989 sizeof(counters[num])) != 0) {
990 ret = -EFAULT;
991 goto free_counters;
992 }
993
994 for (i = sizeof(struct ipt_entry);
995 i < e->target_offset;
996 i += m->u.match_size) {
997 m = (void *)e + i;
998
999 if (copy_to_user(userptr + off + i
1000 + offsetof(struct ipt_entry_match,
1001 u.user.name),
1002 m->u.kernel.match->name,
1003 strlen(m->u.kernel.match->name)+1)
1004 != 0) {
1005 ret = -EFAULT;
1006 goto free_counters;
1007 }
1008 }
1009
1010 t = ipt_get_target(e);
1011 if (copy_to_user(userptr + off + e->target_offset
1012 + offsetof(struct ipt_entry_target,
1013 u.user.name),
1014 t->u.kernel.target->name,
1015 strlen(t->u.kernel.target->name)+1) != 0) {
1016 ret = -EFAULT;
1017 goto free_counters;
1018 }
1019 }
1020
1021 free_counters:
1022 vfree(counters);
1023 return ret;
1024}
1025
Dmitry Mishin27229712006-04-01 02:25:19 -08001026#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001027static void compat_standard_from_user(void *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001028{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001029 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001030
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001031 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001032 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001033 memcpy(dst, &v, sizeof(v));
1034}
1035
1036static int compat_standard_to_user(void __user *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001037{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001038 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001039
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001040 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001041 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001042 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001043}
1044
1045static inline int
Patrick McHardy4b478242007-12-17 21:46:15 -08001046compat_calc_match(struct ipt_entry_match *m, int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001047{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001048 *size += xt_compat_match_offset(m->u.kernel.match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001049 return 0;
1050}
1051
Eric Dumazet259d4e42007-12-04 23:24:56 -08001052static int compat_calc_entry(struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001053 const struct xt_table_info *info,
1054 void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001055{
1056 struct ipt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001057 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001058 int off, i, ret;
1059
Patrick McHardy30c08c42007-12-17 21:47:14 -08001060 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001061 entry_offset = (void *)e - base;
1062 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
1063 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001064 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001065 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001066 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001067 if (ret)
1068 return ret;
1069
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001070 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001071 if (info->hook_entry[i] &&
1072 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001073 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001074 if (info->underflow[i] &&
1075 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001076 newinfo->underflow[i] -= off;
1077 }
1078 return 0;
1079}
1080
Eric Dumazet259d4e42007-12-04 23:24:56 -08001081static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001082 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001083{
1084 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001085
1086 if (!newinfo || !info)
1087 return -EINVAL;
1088
Eric Dumazet259d4e42007-12-04 23:24:56 -08001089 /* we dont care about newinfo->entries[] */
1090 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1091 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001092 loc_cpu_entry = info->entries[raw_smp_processor_id()];
1093 return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001094 compat_calc_entry, info, loc_cpu_entry,
1095 newinfo);
Dmitry Mishin27229712006-04-01 02:25:19 -08001096}
1097#endif
1098
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001099static int get_info(struct net *net, void __user *user, int *len, int compat)
Dmitry Mishin27229712006-04-01 02:25:19 -08001100{
1101 char name[IPT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001102 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001103 int ret;
1104
1105 if (*len != sizeof(struct ipt_getinfo)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001106 duprintf("length %u != %zu\n", *len,
1107 sizeof(struct ipt_getinfo));
Dmitry Mishin27229712006-04-01 02:25:19 -08001108 return -EINVAL;
1109 }
1110
1111 if (copy_from_user(name, user, sizeof(name)) != 0)
1112 return -EFAULT;
1113
1114 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1115#ifdef CONFIG_COMPAT
1116 if (compat)
1117 xt_compat_lock(AF_INET);
1118#endif
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001119 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001120 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001121 if (t && !IS_ERR(t)) {
1122 struct ipt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001123 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001124
1125#ifdef CONFIG_COMPAT
1126 if (compat) {
1127 struct xt_table_info tmp;
1128 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001129 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001130 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001131 }
1132#endif
1133 info.valid_hooks = t->valid_hooks;
1134 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001135 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001136 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001137 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001138 info.num_entries = private->number;
1139 info.size = private->size;
1140 strcpy(info.name, name);
1141
1142 if (copy_to_user(user, &info, *len) != 0)
1143 ret = -EFAULT;
1144 else
1145 ret = 0;
1146
1147 xt_table_unlock(t);
1148 module_put(t->me);
1149 } else
1150 ret = t ? PTR_ERR(t) : -ENOENT;
1151#ifdef CONFIG_COMPAT
1152 if (compat)
1153 xt_compat_unlock(AF_INET);
1154#endif
1155 return ret;
1156}
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001159get_entries(struct net *net, struct ipt_get_entries __user *uptr, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001162 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001163 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Dmitry Mishin27229712006-04-01 02:25:19 -08001165 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001166 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001167 return -EINVAL;
1168 }
1169 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1170 return -EFAULT;
1171 if (*len != sizeof(struct ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001172 duprintf("get_entries: %u != %zu\n",
1173 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001174 return -EINVAL;
1175 }
1176
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001177 t = xt_find_table_lock(net, AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001179 const struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001180 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001181 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001182 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 t, uptr->entrytable);
1184 else {
1185 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001186 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001187 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001190 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 } else
1192 ret = t ? PTR_ERR(t) : -ENOENT;
1193
1194 return ret;
1195}
1196
1197static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001198__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001199 struct xt_table_info *newinfo, unsigned int num_counters,
1200 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001201{
1202 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001203 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001204 struct xt_table_info *oldinfo;
1205 struct xt_counters *counters;
1206 void *loc_cpu_old_entry;
1207
1208 ret = 0;
1209 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1210 if (!counters) {
1211 ret = -ENOMEM;
1212 goto out;
1213 }
1214
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001215 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Dmitry Mishin27229712006-04-01 02:25:19 -08001216 "iptable_%s", name);
1217 if (!t || IS_ERR(t)) {
1218 ret = t ? PTR_ERR(t) : -ENOENT;
1219 goto free_newinfo_counters_untrans;
1220 }
1221
1222 /* You lied! */
1223 if (valid_hooks != t->valid_hooks) {
1224 duprintf("Valid hook crap: %08X vs %08X\n",
1225 valid_hooks, t->valid_hooks);
1226 ret = -EINVAL;
1227 goto put_module;
1228 }
1229
1230 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1231 if (!oldinfo)
1232 goto put_module;
1233
1234 /* Update module usage count based on number of rules */
1235 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1236 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1237 if ((oldinfo->number > oldinfo->initial_entries) ||
1238 (newinfo->number <= oldinfo->initial_entries))
1239 module_put(t->me);
1240 if ((oldinfo->number > oldinfo->initial_entries) &&
1241 (newinfo->number <= oldinfo->initial_entries))
1242 module_put(t->me);
1243
1244 /* Get the old counters. */
1245 get_counters(oldinfo, counters);
1246 /* Decrease module usage counts and free resource */
1247 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Patrick McHardy4b478242007-12-17 21:46:15 -08001248 IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1249 NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001250 xt_free_table_info(oldinfo);
1251 if (copy_to_user(counters_ptr, counters,
1252 sizeof(struct xt_counters) * num_counters) != 0)
1253 ret = -EFAULT;
1254 vfree(counters);
1255 xt_table_unlock(t);
1256 return ret;
1257
1258 put_module:
1259 module_put(t->me);
1260 xt_table_unlock(t);
1261 free_newinfo_counters_untrans:
1262 vfree(counters);
1263 out:
1264 return ret;
1265}
1266
1267static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001268do_replace(struct net *net, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 int ret;
1271 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001272 struct xt_table_info *newinfo;
1273 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1276 return -EFAULT;
1277
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001278 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001279 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1280 return -ENOMEM;
1281
Harald Welte2e4e6a12006-01-12 13:30:04 -08001282 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (!newinfo)
1284 return -ENOMEM;
1285
Patrick McHardy9c547952007-12-17 21:52:00 -08001286 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001287 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1288 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 tmp.size) != 0) {
1290 ret = -EFAULT;
1291 goto free_newinfo;
1292 }
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001295 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 tmp.hook_entry, tmp.underflow);
1297 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001298 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 duprintf("ip_tables: Translated table\n");
1301
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001302 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001303 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001304 if (ret)
1305 goto free_newinfo_untrans;
1306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Dmitry Mishin27229712006-04-01 02:25:19 -08001308 free_newinfo_untrans:
Patrick McHardy9c547952007-12-17 21:52:00 -08001309 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001311 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return ret;
1313}
1314
1315/* We're lazy, and add to the first CPU; overflow works its fey magic
1316 * and everything is OK. */
Denys Vlasenko022748a2008-01-14 23:44:05 -08001317static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318add_counter_to_entry(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001319 const struct xt_counters addme[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 unsigned int *i)
1321{
1322#if 0
1323 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1324 *i,
1325 (long unsigned int)e->counters.pcnt,
1326 (long unsigned int)e->counters.bcnt,
1327 (long unsigned int)addme[*i].pcnt,
1328 (long unsigned int)addme[*i].bcnt);
1329#endif
1330
1331 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1332
1333 (*i)++;
1334 return 0;
1335}
1336
1337static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001338do_add_counters(struct net *net, void __user *user, unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 unsigned int i;
Dmitry Mishin27229712006-04-01 02:25:19 -08001341 struct xt_counters_info tmp;
1342 struct xt_counters *paddc;
1343 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001344 const char *name;
Dmitry Mishin27229712006-04-01 02:25:19 -08001345 int size;
1346 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001347 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001348 const struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001350 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001351#ifdef CONFIG_COMPAT
1352 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Dmitry Mishin27229712006-04-01 02:25:19 -08001354 if (compat) {
1355 ptmp = &compat_tmp;
1356 size = sizeof(struct compat_xt_counters_info);
1357 } else
1358#endif
1359 {
1360 ptmp = &tmp;
1361 size = sizeof(struct xt_counters_info);
1362 }
1363
1364 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return -EFAULT;
1366
Dmitry Mishin27229712006-04-01 02:25:19 -08001367#ifdef CONFIG_COMPAT
1368 if (compat) {
1369 num_counters = compat_tmp.num_counters;
1370 name = compat_tmp.name;
1371 } else
1372#endif
1373 {
1374 num_counters = tmp.num_counters;
1375 name = tmp.name;
1376 }
1377
1378 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return -EINVAL;
1380
Dmitry Mishin27229712006-04-01 02:25:19 -08001381 paddc = vmalloc_node(len - size, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (!paddc)
1383 return -ENOMEM;
1384
Dmitry Mishin27229712006-04-01 02:25:19 -08001385 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 ret = -EFAULT;
1387 goto free;
1388 }
1389
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001390 t = xt_find_table_lock(net, AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (!t || IS_ERR(t)) {
1392 ret = t ? PTR_ERR(t) : -ENOENT;
1393 goto free;
1394 }
1395
1396 write_lock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001397 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001398 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 ret = -EINVAL;
1400 goto unlock_up_free;
1401 }
1402
1403 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001404 /* Choose the copy that is on our node */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001405 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -08001406 IPT_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001407 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 add_counter_to_entry,
Dmitry Mishin27229712006-04-01 02:25:19 -08001409 paddc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 &i);
1411 unlock_up_free:
1412 write_unlock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001413 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 module_put(t->me);
1415 free:
1416 vfree(paddc);
1417
1418 return ret;
1419}
1420
Dmitry Mishin27229712006-04-01 02:25:19 -08001421#ifdef CONFIG_COMPAT
1422struct compat_ipt_replace {
1423 char name[IPT_TABLE_MAXNAMELEN];
1424 u32 valid_hooks;
1425 u32 num_entries;
1426 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001427 u32 hook_entry[NF_INET_NUMHOOKS];
1428 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001429 u32 num_counters;
1430 compat_uptr_t counters; /* struct ipt_counters * */
1431 struct compat_ipt_entry entries[0];
1432};
1433
Patrick McHardya18aa312007-12-12 10:35:16 -08001434static int
1435compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001436 unsigned int *size, struct xt_counters *counters,
Patrick McHardya18aa312007-12-12 10:35:16 -08001437 unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001438{
Al Viro3e597c62006-09-24 23:42:20 +01001439 struct ipt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001440 struct compat_ipt_entry __user *ce;
1441 u_int16_t target_offset, next_offset;
1442 compat_uint_t origsize;
1443 int ret;
1444
1445 ret = -EFAULT;
1446 origsize = *size;
1447 ce = (struct compat_ipt_entry __user *)*dstptr;
Patrick McHardy78000072006-05-03 23:20:27 -07001448 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
Dmitry Mishin27229712006-04-01 02:25:19 -08001449 goto out;
1450
Patrick McHardya18aa312007-12-12 10:35:16 -08001451 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1452 goto out;
1453
Dmitry Mishin27229712006-04-01 02:25:19 -08001454 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001455 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1456
Patrick McHardyac8e27f2007-12-17 21:45:52 -08001457 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001458 target_offset = e->target_offset - (origsize - *size);
1459 if (ret)
1460 goto out;
1461 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001462 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001463 if (ret)
1464 goto out;
1465 ret = -EFAULT;
1466 next_offset = e->next_offset - (origsize - *size);
Patrick McHardy78000072006-05-03 23:20:27 -07001467 if (put_user(target_offset, &ce->target_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001468 goto out;
Patrick McHardy78000072006-05-03 23:20:27 -07001469 if (put_user(next_offset, &ce->next_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001470 goto out;
Patrick McHardya18aa312007-12-12 10:35:16 -08001471
1472 (*i)++;
Dmitry Mishin27229712006-04-01 02:25:19 -08001473 return 0;
1474out:
1475 return ret;
1476}
1477
Denys Vlasenko022748a2008-01-14 23:44:05 -08001478static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001479compat_find_calc_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001480 const char *name,
1481 const struct ipt_ip *ip,
1482 unsigned int hookmask,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001483 int *size, unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001484{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001485 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001486
1487 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001488 m->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001489 "ipt_%s", m->u.user.name);
1490 if (IS_ERR(match) || !match) {
1491 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001492 m->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001493 return match ? PTR_ERR(match) : -ENOENT;
1494 }
1495 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001496 *size += xt_compat_match_offset(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001497
1498 (*i)++;
1499 return 0;
1500}
1501
Denys Vlasenko022748a2008-01-14 23:44:05 -08001502static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001503compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1504{
1505 if (i && (*i)-- == 0)
1506 return 1;
1507
1508 module_put(m->u.kernel.match->me);
1509 return 0;
1510}
1511
Denys Vlasenko022748a2008-01-14 23:44:05 -08001512static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001513compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001514{
1515 struct ipt_entry_target *t;
1516
1517 if (i && (*i)-- == 0)
1518 return 1;
1519
1520 /* Cleanup all matches */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001521 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1522 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001523 module_put(t->u.kernel.target->me);
1524 return 0;
1525}
1526
Denys Vlasenko022748a2008-01-14 23:44:05 -08001527static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001528check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001529 struct xt_table_info *newinfo,
1530 unsigned int *size,
1531 unsigned char *base,
1532 unsigned char *limit,
1533 unsigned int *hook_entries,
1534 unsigned int *underflows,
1535 unsigned int *i,
1536 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001537{
1538 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001539 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001540 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001541 unsigned int j;
1542 int ret, off, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001543
1544 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1545 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0
1546 || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1547 duprintf("Bad offset %p, limit = %p\n", e, limit);
1548 return -EINVAL;
1549 }
1550
1551 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001552 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001553 duprintf("checking: element %p size %u\n",
1554 e, e->next_offset);
1555 return -EINVAL;
1556 }
1557
Patrick McHardy73cd5982007-12-17 21:47:32 -08001558 /* For purposes of check_entry casting the compat entry is fine */
1559 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001560 if (ret)
1561 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001562
Patrick McHardy30c08c42007-12-17 21:47:14 -08001563 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001564 entry_offset = (void *)e - (void *)base;
1565 j = 0;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001566 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1567 &e->ip, e->comefrom, &off, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001568 if (ret != 0)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001569 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001570
Patrick McHardy73cd5982007-12-17 21:47:32 -08001571 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001572 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -08001573 t->u.user.name,
1574 t->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001575 "ipt_%s", t->u.user.name);
1576 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001577 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001578 t->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001579 ret = target ? PTR_ERR(target) : -ENOENT;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001580 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001581 }
1582 t->u.kernel.target = target;
1583
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001584 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001585 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001586 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001587 if (ret)
1588 goto out;
1589
1590 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001591 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001592 if ((unsigned char *)e - base == hook_entries[h])
1593 newinfo->hook_entry[h] = hook_entries[h];
1594 if ((unsigned char *)e - base == underflows[h])
1595 newinfo->underflow[h] = underflows[h];
1596 }
1597
1598 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001599 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001600 e->comefrom = 0;
1601
1602 (*i)++;
1603 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001604
Dmitry Mishin27229712006-04-01 02:25:19 -08001605out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001606 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001607release_matches:
1608 IPT_MATCH_ITERATE(e, compat_release_match, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001609 return ret;
1610}
1611
Patrick McHardy4b478242007-12-17 21:46:15 -08001612static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001613compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001614 unsigned int *size, const char *name,
1615 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001616{
1617 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001618 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001619 struct ipt_entry *de;
1620 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001621 int ret, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001622
1623 ret = 0;
1624 origsize = *size;
1625 de = (struct ipt_entry *)*dstptr;
1626 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001627 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001628
Patrick McHardy73cd5982007-12-17 21:47:32 -08001629 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001630 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1631
Patrick McHardy73cd5982007-12-17 21:47:32 -08001632 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1633 dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001634 if (ret)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001635 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001636 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001637 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001638 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001639 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001640
1641 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001642 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001643 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1644 newinfo->hook_entry[h] -= origsize - *size;
1645 if ((unsigned char *)de - base < newinfo->underflow[h])
1646 newinfo->underflow[h] -= origsize - *size;
1647 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001648 return ret;
1649}
Dmitry Mishin27229712006-04-01 02:25:19 -08001650
Denys Vlasenko022748a2008-01-14 23:44:05 -08001651static int
1652compat_check_entry(struct ipt_entry *e, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001653 unsigned int *i)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001654{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001655 struct xt_mtchk_param mtpar;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001656 unsigned int j;
1657 int ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001658
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001659 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001660 mtpar.table = name;
1661 mtpar.entryinfo = &e->ip;
1662 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +02001663 mtpar.family = NFPROTO_IPV4;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001664 ret = IPT_MATCH_ITERATE(e, check_match, &mtpar, &j);
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001665 if (ret)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001666 goto cleanup_matches;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001667
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001668 ret = check_target(e, name);
1669 if (ret)
1670 goto cleanup_matches;
1671
1672 (*i)++;
1673 return 0;
1674
1675 cleanup_matches:
1676 IPT_MATCH_ITERATE(e, cleanup_match, &j);
1677 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001678}
1679
Dmitry Mishin27229712006-04-01 02:25:19 -08001680static int
1681translate_compat_table(const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001682 unsigned int valid_hooks,
1683 struct xt_table_info **pinfo,
1684 void **pentry0,
1685 unsigned int total_size,
1686 unsigned int number,
1687 unsigned int *hook_entries,
1688 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001689{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001690 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001691 struct xt_table_info *newinfo, *info;
1692 void *pos, *entry0, *entry1;
1693 unsigned int size;
1694 int ret;
1695
1696 info = *pinfo;
1697 entry0 = *pentry0;
1698 size = total_size;
1699 info->number = number;
1700
1701 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001702 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001703 info->hook_entry[i] = 0xFFFFFFFF;
1704 info->underflow[i] = 0xFFFFFFFF;
1705 }
1706
1707 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001708 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001709 xt_compat_lock(AF_INET);
1710 /* Walk through entries, checking offsets. */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001711 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1712 check_compat_entry_size_and_hooks,
1713 info, &size, entry0,
1714 entry0 + total_size,
1715 hook_entries, underflows, &j, name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001716 if (ret != 0)
1717 goto out_unlock;
1718
1719 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001720 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001721 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001722 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001723 goto out_unlock;
1724 }
1725
1726 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001727 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001728 /* Only hooks which are valid */
1729 if (!(valid_hooks & (1 << i)))
1730 continue;
1731 if (info->hook_entry[i] == 0xFFFFFFFF) {
1732 duprintf("Invalid hook entry %u %u\n",
1733 i, hook_entries[i]);
1734 goto out_unlock;
1735 }
1736 if (info->underflow[i] == 0xFFFFFFFF) {
1737 duprintf("Invalid underflow %u %u\n",
1738 i, underflows[i]);
1739 goto out_unlock;
1740 }
1741 }
1742
1743 ret = -ENOMEM;
1744 newinfo = xt_alloc_table_info(size);
1745 if (!newinfo)
1746 goto out_unlock;
1747
1748 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001749 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001750 newinfo->hook_entry[i] = info->hook_entry[i];
1751 newinfo->underflow[i] = info->underflow[i];
1752 }
1753 entry1 = newinfo->entries[raw_smp_processor_id()];
1754 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001755 size = total_size;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001756 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
Patrick McHardy9c547952007-12-17 21:52:00 -08001757 compat_copy_entry_from_user,
1758 &pos, &size, name, newinfo, entry1);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001759 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001760 xt_compat_unlock(AF_INET);
1761 if (ret)
1762 goto free_newinfo;
1763
1764 ret = -ELOOP;
1765 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1766 goto free_newinfo;
1767
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001768 i = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001769 ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001770 name, &i);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001771 if (ret) {
1772 j -= i;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001773 COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1774 compat_release_entry, &j);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001775 IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1776 xt_free_table_info(newinfo);
1777 return ret;
1778 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001779
Dmitry Mishin27229712006-04-01 02:25:19 -08001780 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001781 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001782 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1783 memcpy(newinfo->entries[i], entry1, newinfo->size);
1784
1785 *pinfo = newinfo;
1786 *pentry0 = entry1;
1787 xt_free_table_info(info);
1788 return 0;
1789
1790free_newinfo:
1791 xt_free_table_info(newinfo);
1792out:
Patrick McHardy73cd5982007-12-17 21:47:32 -08001793 COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001794 return ret;
1795out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001796 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001797 xt_compat_unlock(AF_INET);
1798 goto out;
1799}
1800
1801static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001802compat_do_replace(struct net *net, void __user *user, unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001803{
1804 int ret;
1805 struct compat_ipt_replace tmp;
1806 struct xt_table_info *newinfo;
1807 void *loc_cpu_entry;
1808
1809 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1810 return -EFAULT;
1811
Dmitry Mishin27229712006-04-01 02:25:19 -08001812 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001813 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001814 return -ENOMEM;
1815 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1816 return -ENOMEM;
1817
1818 newinfo = xt_alloc_table_info(tmp.size);
1819 if (!newinfo)
1820 return -ENOMEM;
1821
Patrick McHardy9c547952007-12-17 21:52:00 -08001822 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001823 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1824 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1825 tmp.size) != 0) {
1826 ret = -EFAULT;
1827 goto free_newinfo;
1828 }
1829
1830 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001831 &newinfo, &loc_cpu_entry, tmp.size,
1832 tmp.num_entries, tmp.hook_entry,
1833 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001834 if (ret != 0)
1835 goto free_newinfo;
1836
1837 duprintf("compat_do_replace: Translated table\n");
1838
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001839 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001840 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001841 if (ret)
1842 goto free_newinfo_untrans;
1843 return 0;
1844
1845 free_newinfo_untrans:
Patrick McHardy4b478242007-12-17 21:46:15 -08001846 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001847 free_newinfo:
1848 xt_free_table_info(newinfo);
1849 return ret;
1850}
1851
1852static int
1853compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001854 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001855{
1856 int ret;
1857
1858 if (!capable(CAP_NET_ADMIN))
1859 return -EPERM;
1860
1861 switch (cmd) {
1862 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001863 ret = compat_do_replace(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001864 break;
1865
1866 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001867 ret = do_add_counters(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001868 break;
1869
1870 default:
1871 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1872 ret = -EINVAL;
1873 }
1874
1875 return ret;
1876}
1877
Patrick McHardy4b478242007-12-17 21:46:15 -08001878struct compat_ipt_get_entries {
Dmitry Mishin27229712006-04-01 02:25:19 -08001879 char name[IPT_TABLE_MAXNAMELEN];
1880 compat_uint_t size;
1881 struct compat_ipt_entry entrytable[0];
1882};
1883
Patrick McHardy4b478242007-12-17 21:46:15 -08001884static int
1885compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1886 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001887{
Dmitry Mishin27229712006-04-01 02:25:19 -08001888 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001889 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001890 void __user *pos;
1891 unsigned int size;
1892 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001893 const void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001894 unsigned int i = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001895
1896 counters = alloc_counters(table);
1897 if (IS_ERR(counters))
1898 return PTR_ERR(counters);
1899
1900 /* choose the copy that is on our node/cpu, ...
1901 * This choice is lazy (because current thread is
1902 * allowed to migrate to another cpu)
1903 */
1904 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1905 pos = userptr;
1906 size = total_size;
1907 ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
Patrick McHardya18aa312007-12-12 10:35:16 -08001908 compat_copy_entry_to_user,
1909 &pos, &size, counters, &i);
Dmitry Mishin27229712006-04-01 02:25:19 -08001910
Dmitry Mishin27229712006-04-01 02:25:19 -08001911 vfree(counters);
1912 return ret;
1913}
1914
1915static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001916compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1917 int *len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001918{
1919 int ret;
1920 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001921 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001922
Dmitry Mishin27229712006-04-01 02:25:19 -08001923 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001924 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001925 return -EINVAL;
1926 }
1927
1928 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1929 return -EFAULT;
1930
1931 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001932 duprintf("compat_get_entries: %u != %zu\n",
1933 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001934 return -EINVAL;
1935 }
1936
1937 xt_compat_lock(AF_INET);
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001938 t = xt_find_table_lock(net, AF_INET, get.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001939 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001940 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001941 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08001942 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001943 ret = compat_table_info(private, &info);
1944 if (!ret && get.size == info.size) {
1945 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001946 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001947 } else if (!ret) {
1948 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001949 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001950 ret = -EAGAIN;
Dmitry Mishin27229712006-04-01 02:25:19 -08001951 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001952 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001953 module_put(t->me);
1954 xt_table_unlock(t);
1955 } else
1956 ret = t ? PTR_ERR(t) : -ENOENT;
1957
1958 xt_compat_unlock(AF_INET);
1959 return ret;
1960}
1961
Patrick McHardy79030ed2006-09-20 12:05:08 -07001962static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1963
Dmitry Mishin27229712006-04-01 02:25:19 -08001964static int
1965compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1966{
1967 int ret;
1968
Björn Steinbrink82fac052006-10-20 00:21:10 -07001969 if (!capable(CAP_NET_ADMIN))
1970 return -EPERM;
1971
Dmitry Mishin27229712006-04-01 02:25:19 -08001972 switch (cmd) {
1973 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001974 ret = get_info(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001975 break;
1976 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001977 ret = compat_get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001978 break;
1979 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001980 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001981 }
1982 return ret;
1983}
1984#endif
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001987do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 int ret;
1990
1991 if (!capable(CAP_NET_ADMIN))
1992 return -EPERM;
1993
1994 switch (cmd) {
1995 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001996 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
1998
1999 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002000 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 break;
2002
2003 default:
2004 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
2005 ret = -EINVAL;
2006 }
2007
2008 return ret;
2009}
2010
2011static int
2012do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2013{
2014 int ret;
2015
2016 if (!capable(CAP_NET_ADMIN))
2017 return -EPERM;
2018
2019 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002020 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002021 ret = get_info(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002023
2024 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002025 ret = get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08002026 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 case IPT_SO_GET_REVISION_MATCH:
2029 case IPT_SO_GET_REVISION_TARGET: {
2030 struct ipt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002031 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 if (*len != sizeof(rev)) {
2034 ret = -EINVAL;
2035 break;
2036 }
2037 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2038 ret = -EFAULT;
2039 break;
2040 }
2041
2042 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002043 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002045 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Harald Welte2e4e6a12006-01-12 13:30:04 -08002047 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2048 rev.revision,
2049 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 "ipt_%s", rev.name);
2051 break;
2052 }
2053
2054 default:
2055 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2056 ret = -EINVAL;
2057 }
2058
2059 return ret;
2060}
2061
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002062struct xt_table *ipt_register_table(struct net *net, struct xt_table *table,
2063 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002066 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08002067 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08002069 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08002070 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Harald Welte2e4e6a12006-01-12 13:30:04 -08002072 newinfo = xt_alloc_table_info(repl->size);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002073 if (!newinfo) {
2074 ret = -ENOMEM;
2075 goto out;
2076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Patrick McHardy9c547952007-12-17 21:52:00 -08002078 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002079 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2080 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08002083 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 repl->num_entries,
2085 repl->hook_entry,
2086 repl->underflow);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002087 if (ret != 0)
2088 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002090 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08002091 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002092 ret = PTR_ERR(new_table);
2093 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002096 return new_table;
2097
2098out_free:
2099 xt_free_table_info(newinfo);
2100out:
2101 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Jan Engelhardte60a13e2007-02-07 15:12:33 -08002104void ipt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002106 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002107 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08002108 struct module *table_owner = table->me;
Eric Dumazet31836062005-12-13 23:13:48 -08002109
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002110 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002113 loc_cpu_entry = private->entries[raw_smp_processor_id()];
2114 IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
Alexey Dobriyandf200962008-01-31 04:05:34 -08002115 if (private->number > private->initial_entries)
2116 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002117 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
2120/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002121static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2123 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002124 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Patrick McHardy9c547952007-12-17 21:52:00 -08002126 return ((test_type == 0xFF) ||
2127 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 ^ invert;
2129}
2130
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002131static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002132icmp_match(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Jan Engelhardt5452e422008-04-14 11:15:35 +02002134 const struct icmphdr *ic;
2135 struct icmphdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002136 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002139 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002140 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002142 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (ic == NULL) {
2144 /* We've been asked to examine this packet, and we
2145 * can't. Hence, no choice but to drop.
2146 */
2147 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002148 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002149 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151
2152 return icmp_type_code_match(icmpinfo->type,
2153 icmpinfo->code[0],
2154 icmpinfo->code[1],
2155 ic->type, ic->code,
2156 !!(icmpinfo->invflags&IPT_ICMP_INV));
2157}
2158
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002159static bool icmp_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002161 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002163 /* Must specify no unknown invflags */
2164 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
2167/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07002168static struct xt_target ipt_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 .name = IPT_STANDARD_TARGET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002170 .targetsize = sizeof(int),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002171 .family = AF_INET,
Dmitry Mishin27229712006-04-01 02:25:19 -08002172#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07002173 .compatsize = sizeof(compat_int_t),
2174 .compat_from_user = compat_standard_from_user,
2175 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002176#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177};
2178
Patrick McHardy9f15c532007-07-07 22:22:02 -07002179static struct xt_target ipt_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 .name = IPT_ERROR_TARGET,
2181 .target = ipt_error,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002182 .targetsize = IPT_FUNCTION_MAXNAMELEN,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002183 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184};
2185
2186static struct nf_sockopt_ops ipt_sockopts = {
2187 .pf = PF_INET,
2188 .set_optmin = IPT_BASE_CTL,
2189 .set_optmax = IPT_SO_SET_MAX+1,
2190 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002191#ifdef CONFIG_COMPAT
2192 .compat_set = compat_do_ipt_set_ctl,
2193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 .get_optmin = IPT_BASE_CTL,
2195 .get_optmax = IPT_SO_GET_MAX+1,
2196 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002197#ifdef CONFIG_COMPAT
2198 .compat_get = compat_do_ipt_get_ctl,
2199#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002200 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201};
2202
Patrick McHardy9f15c532007-07-07 22:22:02 -07002203static struct xt_match icmp_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 .name = "icmp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002205 .match = icmp_match,
2206 .matchsize = sizeof(struct ipt_icmp),
Patrick McHardy9c547952007-12-17 21:52:00 -08002207 .checkentry = icmp_checkentry,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002208 .proto = IPPROTO_ICMP,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002209 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210};
2211
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002212static int __net_init ip_tables_net_init(struct net *net)
2213{
2214 return xt_proto_init(net, AF_INET);
2215}
2216
2217static void __net_exit ip_tables_net_exit(struct net *net)
2218{
2219 xt_proto_fini(net, AF_INET);
2220}
2221
2222static struct pernet_operations ip_tables_net_ops = {
2223 .init = ip_tables_net_init,
2224 .exit = ip_tables_net_exit,
2225};
2226
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002227static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 int ret;
2230
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002231 ret = register_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002232 if (ret < 0)
2233 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002236 ret = xt_register_target(&ipt_standard_target);
2237 if (ret < 0)
2238 goto err2;
2239 ret = xt_register_target(&ipt_error_target);
2240 if (ret < 0)
2241 goto err3;
2242 ret = xt_register_match(&icmp_matchstruct);
2243 if (ret < 0)
2244 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 /* Register setsockopt */
2247 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002248 if (ret < 0)
2249 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Dan Aloni0236e662007-07-09 13:20:12 -07002251 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002253
2254err5:
2255 xt_unregister_match(&icmp_matchstruct);
2256err4:
2257 xt_unregister_target(&ipt_error_target);
2258err3:
2259 xt_unregister_target(&ipt_standard_target);
2260err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002261 unregister_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002262err1:
2263 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002266static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002269
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002270 xt_unregister_match(&icmp_matchstruct);
2271 xt_unregister_target(&ipt_error_target);
2272 xt_unregister_target(&ipt_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002273
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002274 unregister_pernet_subsys(&ip_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
2277EXPORT_SYMBOL(ipt_register_table);
2278EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002280module_init(ip_tables_init);
2281module_exit(ip_tables_fini);