blob: 0f8ecf3902299edb64032132e0c5ae863af1c17c [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
97 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
98 NIPQUAD(ip->saddr),
99 NIPQUAD(ipinfo->smsk.s_addr),
100 NIPQUAD(ipinfo->src.s_addr),
101 ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
102 dprintf("DST: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
103 NIPQUAD(ip->daddr),
104 NIPQUAD(ipinfo->dmsk.s_addr),
105 NIPQUAD(ipinfo->dst.s_addr),
106 ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800107 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109
110 /* Look for ifname matches; this should unroll nicely. */
111 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
112 ret |= (((const unsigned long *)indev)[i]
113 ^ ((const unsigned long *)ipinfo->iniface)[i])
114 & ((const unsigned long *)ipinfo->iniface_mask)[i];
115 }
116
117 if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
118 dprintf("VIA in mismatch (%s vs %s).%s\n",
119 indev, ipinfo->iniface,
120 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800121 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
124 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
125 ret |= (((const unsigned long *)outdev)[i]
126 ^ ((const unsigned long *)ipinfo->outiface)[i])
127 & ((const unsigned long *)ipinfo->outiface_mask)[i];
128 }
129
130 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
131 dprintf("VIA out mismatch (%s vs %s).%s\n",
132 outdev, ipinfo->outiface,
133 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800134 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
137 /* Check specific protocol */
138 if (ipinfo->proto
139 && FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
140 dprintf("Packet protocol %hi does not match %hi.%s\n",
141 ip->protocol, ipinfo->proto,
142 ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800143 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
146 /* If we have a fragment rule but the packet is not a fragment
147 * then we return zero */
148 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
149 dprintf("Fragment rule but not fragment.%s\n",
150 ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800151 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
Patrick McHardy9c547952007-12-17 21:52:00 -0800154 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Denys Vlasenko022748a2008-01-14 23:44:05 -0800157static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158ip_checkentry(const struct ipt_ip *ip)
159{
160 if (ip->flags & ~IPT_F_MASK) {
161 duprintf("Unknown flag bits set: %08X\n",
162 ip->flags & ~IPT_F_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700163 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 if (ip->invflags & ~IPT_INV_MASK) {
166 duprintf("Unknown invflag bits set: %08X\n",
167 ip->invflags & ~IPT_INV_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700168 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700170 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200174ipt_error(struct sk_buff *skb, const struct xt_target_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 if (net_ratelimit())
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200177 printk("ip_tables: error: `%s'\n",
178 (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 return NF_DROP;
181}
182
Denys Vlasenko022748a2008-01-14 23:44:05 -0800183/* Performance critical - called for every packet */
184static inline bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200185do_match(struct ipt_entry_match *m, const struct sk_buff *skb,
186 struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200188 par->match = m->u.kernel.match;
189 par->matchinfo = m->data;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Stop iteration if it doesn't match */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200192 if (!m->u.kernel.match->match(skb, par))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700193 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 else
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700195 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Denys Vlasenko022748a2008-01-14 23:44:05 -0800198/* Performance critical */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static inline struct ipt_entry *
200get_entry(void *base, unsigned int offset)
201{
202 return (struct ipt_entry *)(base + offset);
203}
204
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700205/* All zeroes == unconditional rule. */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800206/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700207static inline int
208unconditional(const struct ipt_ip *ip)
209{
210 unsigned int i;
211
212 for (i = 0; i < sizeof(*ip)/sizeof(__u32); i++)
213 if (((__u32 *)ip)[i])
214 return 0;
215
216 return 1;
Jan Engelhardte79ec502007-12-17 22:44:06 -0800217#undef FWINV
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700218}
219
220#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
221 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
Denys Vlasenko022748a2008-01-14 23:44:05 -0800222static const char *const hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800223 [NF_INET_PRE_ROUTING] = "PREROUTING",
224 [NF_INET_LOCAL_IN] = "INPUT",
Patrick McHardy9c547952007-12-17 21:52:00 -0800225 [NF_INET_FORWARD] = "FORWARD",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800226 [NF_INET_LOCAL_OUT] = "OUTPUT",
227 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700228};
229
230enum nf_ip_trace_comments {
231 NF_IP_TRACE_COMMENT_RULE,
232 NF_IP_TRACE_COMMENT_RETURN,
233 NF_IP_TRACE_COMMENT_POLICY,
234};
235
Denys Vlasenko022748a2008-01-14 23:44:05 -0800236static const char *const comments[] = {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700237 [NF_IP_TRACE_COMMENT_RULE] = "rule",
238 [NF_IP_TRACE_COMMENT_RETURN] = "return",
239 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
240};
241
242static struct nf_loginfo trace_loginfo = {
243 .type = NF_LOG_TYPE_LOG,
244 .u = {
245 .log = {
246 .level = 4,
247 .logflags = NF_LOG_MASK,
248 },
249 },
250};
251
Denys Vlasenko022748a2008-01-14 23:44:05 -0800252/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700253static inline int
254get_chainname_rulenum(struct ipt_entry *s, struct ipt_entry *e,
255 char *hookname, char **chainname,
256 char **comment, unsigned int *rulenum)
257{
258 struct ipt_standard_target *t = (void *)ipt_get_target(s);
259
260 if (strcmp(t->target.u.kernel.target->name, IPT_ERROR_TARGET) == 0) {
261 /* Head of user chain: ERROR target with chainname */
262 *chainname = t->target.data;
263 (*rulenum) = 0;
264 } else if (s == e) {
265 (*rulenum)++;
266
267 if (s->target_offset == sizeof(struct ipt_entry)
268 && strcmp(t->target.u.kernel.target->name,
269 IPT_STANDARD_TARGET) == 0
270 && t->verdict < 0
271 && unconditional(&s->ip)) {
272 /* Tail of chains: STANDARD target (return/policy) */
273 *comment = *chainname == hookname
274 ? (char *)comments[NF_IP_TRACE_COMMENT_POLICY]
275 : (char *)comments[NF_IP_TRACE_COMMENT_RETURN];
276 }
277 return 1;
278 } else
279 (*rulenum)++;
280
281 return 0;
282}
283
284static void trace_packet(struct sk_buff *skb,
285 unsigned int hook,
286 const struct net_device *in,
287 const struct net_device *out,
Jan Engelhardtecb6f852008-01-31 03:54:47 -0800288 const char *tablename,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700289 struct xt_table_info *private,
290 struct ipt_entry *e)
291{
292 void *table_base;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200293 const struct ipt_entry *root;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700294 char *hookname, *chainname, *comment;
295 unsigned int rulenum = 0;
296
297 table_base = (void *)private->entries[smp_processor_id()];
298 root = get_entry(table_base, private->hook_entry[hook]);
299
300 hookname = chainname = (char *)hooknames[hook];
301 comment = (char *)comments[NF_IP_TRACE_COMMENT_RULE];
302
303 IPT_ENTRY_ITERATE(root,
304 private->size - private->hook_entry[hook],
305 get_chainname_rulenum,
306 e, hookname, &chainname, &comment, &rulenum);
307
308 nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo,
309 "TRACE: %s:%s:%s:%u ",
310 tablename, chainname, comment, rulenum);
311}
312#endif
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* Returns one of the generic firewall policies, like NF_ACCEPT. */
315unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700316ipt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned int hook,
318 const struct net_device *in,
319 const struct net_device *out,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800320 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Jan Engelhardt5452e422008-04-14 11:15:35 +0200323 const struct iphdr *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 u_int16_t datalen;
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700325 bool hotdrop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* Initializing verdict to NF_DROP keeps gcc happy. */
327 unsigned int verdict = NF_DROP;
328 const char *indev, *outdev;
329 void *table_base;
330 struct ipt_entry *e, *back;
Patrick McHardy83117312006-08-17 18:13:53 -0700331 struct xt_table_info *private;
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200332 struct xt_match_param mtpar;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200333 struct xt_target_param tgpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* Initialization */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700336 ip = ip_hdr(skb);
337 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 indev = in ? in->name : nulldevname;
339 outdev = out ? out->name : nulldevname;
340 /* We handle fragments by dealing with the first fragment as
341 * if it was a normal packet. All other fragments are treated
342 * normally, except that they will NEVER match rules that ask
343 * things we don't know, ie. tcp syn flag or ports). If the
344 * rule is also a fragment-specific rule, non-fragments won't
345 * match it. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200346 mtpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
347 mtpar.thoff = ip_hdrlen(skb);
348 mtpar.hotdrop = &hotdrop;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200349 mtpar.in = tgpar.in = in;
350 mtpar.out = tgpar.out = out;
351 tgpar.hooknum = hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 read_lock_bh(&table->lock);
354 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Patrick McHardy83117312006-08-17 18:13:53 -0700355 private = table->private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800356 table_base = (void *)private->entries[smp_processor_id()];
357 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* For return from builtin chain */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800360 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 do {
363 IP_NF_ASSERT(e);
364 IP_NF_ASSERT(back);
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200365 if (ip_packet_match(ip, indev, outdev,
366 &e->ip, mtpar.fragoff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct ipt_entry_target *t;
368
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200369 if (IPT_MATCH_ITERATE(e, do_match, skb, &mtpar) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 goto no_match;
371
372 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
373
374 t = ipt_get_target(e);
375 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700376
377#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
378 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
379 /* The packet is traced: log it */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700380 if (unlikely(skb->nf_trace))
381 trace_packet(skb, hook, in, out,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700382 table->name, private, e);
383#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* Standard target? */
385 if (!t->u.kernel.target->target) {
386 int v;
387
388 v = ((struct ipt_standard_target *)t)->verdict;
389 if (v < 0) {
390 /* Pop from stack? */
391 if (v != IPT_RETURN) {
392 verdict = (unsigned)(-v) - 1;
393 break;
394 }
395 e = back;
396 back = get_entry(table_base,
397 back->comefrom);
398 continue;
399 }
Patrick McHardy05465342005-08-21 23:31:43 -0700400 if (table_base + v != (void *)e + e->next_offset
401 && !(e->ip.flags & IPT_F_GOTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* Save old back ptr in next entry */
403 struct ipt_entry *next
404 = (void *)e + e->next_offset;
405 next->comefrom
406 = (void *)back - table_base;
407 /* set back pointer to next entry */
408 back = next;
409 }
410
411 e = get_entry(table_base, v);
412 } else {
413 /* Targets which reenter must return
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900414 abs. verdicts */
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200415 tgpar.target = t->u.kernel.target;
416 tgpar.targinfo = t->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#ifdef CONFIG_NETFILTER_DEBUG
418 ((struct ipt_entry *)table_base)->comefrom
419 = 0xeeeeeeec;
420#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700421 verdict = t->u.kernel.target->target(skb,
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200422 &tgpar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#ifdef CONFIG_NETFILTER_DEBUG
424 if (((struct ipt_entry *)table_base)->comefrom
425 != 0xeeeeeeec
426 && verdict == IPT_CONTINUE) {
427 printk("Target %s reentered!\n",
428 t->u.kernel.target->name);
429 verdict = NF_DROP;
430 }
431 ((struct ipt_entry *)table_base)->comefrom
432 = 0x57acc001;
433#endif
434 /* Target might have changed stuff. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700435 ip = ip_hdr(skb);
436 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (verdict == IPT_CONTINUE)
439 e = (void *)e + e->next_offset;
440 else
441 /* Verdict */
442 break;
443 }
444 } else {
445
446 no_match:
447 e = (void *)e + e->next_offset;
448 }
449 } while (!hotdrop);
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 read_unlock_bh(&table->lock);
452
453#ifdef DEBUG_ALLOW_ALL
454 return NF_ACCEPT;
455#else
456 if (hotdrop)
457 return NF_DROP;
458 else return verdict;
459#endif
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/* Figures out from what hook each rule can be called: returns 0 if
463 there are loops. Puts hook bitmask in comefrom. */
464static int
Harald Welte2e4e6a12006-01-12 13:30:04 -0800465mark_source_chains(struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800466 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 unsigned int hook;
469
470 /* No recursion; use packet counter to save back ptrs (reset
471 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800472 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800474 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if (!(valid_hooks & (1 << hook)))
477 continue;
478
479 /* Set initial back pointer. */
480 e->counters.pcnt = pos;
481
482 for (;;) {
483 struct ipt_standard_target *t
484 = (void *)ipt_get_target(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800485 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800487 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 printk("iptables: loop hook %u pos %u %08X.\n",
489 hook, pos, e->comefrom);
490 return 0;
491 }
Patrick McHardy9c547952007-12-17 21:52:00 -0800492 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Unconditional return/END. */
Al Viroe1b4b9f2006-12-12 00:29:52 -0800495 if ((e->target_offset == sizeof(struct ipt_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 && (strcmp(t->target.u.user.name,
497 IPT_STANDARD_TARGET) == 0)
498 && t->verdict < 0
Al Viroe1b4b9f2006-12-12 00:29:52 -0800499 && unconditional(&e->ip)) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 unsigned int oldpos, size;
501
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800502 if (t->verdict < -NF_MAX_VERDICT - 1) {
503 duprintf("mark_source_chains: bad "
504 "negative verdict (%i)\n",
505 t->verdict);
506 return 0;
507 }
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* Return: backtrack through the last
510 big jump. */
511 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800512 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513#ifdef DEBUG_IP_FIREWALL_USER
514 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800515 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 duprintf("Back unset "
517 "on hook %u "
518 "rule %u\n",
519 hook, pos);
520 }
521#endif
522 oldpos = pos;
523 pos = e->counters.pcnt;
524 e->counters.pcnt = 0;
525
526 /* We're at the start. */
527 if (pos == oldpos)
528 goto next;
529
530 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800531 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 } while (oldpos == pos + e->next_offset);
533
534 /* Move along one */
535 size = e->next_offset;
536 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800537 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 e->counters.pcnt = pos;
539 pos += size;
540 } else {
541 int newpos = t->verdict;
542
543 if (strcmp(t->target.u.user.name,
544 IPT_STANDARD_TARGET) == 0
545 && newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800546 if (newpos > newinfo->size -
547 sizeof(struct ipt_entry)) {
548 duprintf("mark_source_chains: "
549 "bad verdict (%i)\n",
550 newpos);
551 return 0;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* This a jump; chase it. */
554 duprintf("Jump rule %u -> %u\n",
555 pos, newpos);
556 } else {
557 /* ... this is a fallthru */
558 newpos = pos + e->next_offset;
559 }
560 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800561 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 e->counters.pcnt = pos;
563 pos = newpos;
564 }
565 }
566 next:
567 duprintf("Finished chain %u\n", hook);
568 }
569 return 1;
570}
571
Denys Vlasenko022748a2008-01-14 23:44:05 -0800572static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573cleanup_match(struct ipt_entry_match *m, unsigned int *i)
574{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200575 struct xt_mtdtor_param par;
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (i && (*i)-- == 0)
578 return 1;
579
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200580 par.match = m->u.kernel.match;
581 par.matchinfo = m->data;
582 if (par.match->destroy != NULL)
583 par.match->destroy(&par);
584 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 0;
586}
587
Denys Vlasenko022748a2008-01-14 23:44:05 -0800588static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800589check_entry(struct ipt_entry *e, const char *name)
590{
591 struct ipt_entry_target *t;
592
593 if (!ip_checkentry(&e->ip)) {
594 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
595 return -EINVAL;
596 }
597
Patrick McHardy9c547952007-12-17 21:52:00 -0800598 if (e->target_offset + sizeof(struct ipt_entry_target) >
599 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800600 return -EINVAL;
601
602 t = ipt_get_target(e);
603 if (e->target_offset + t->u.target_size > e->next_offset)
604 return -EINVAL;
605
606 return 0;
607}
608
Denys Vlasenko022748a2008-01-14 23:44:05 -0800609static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200610check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
611 unsigned int *i)
Dmitry Mishina96be242006-12-12 00:29:26 -0800612{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200613 const struct ipt_ip *ip = par->entryinfo;
Dmitry Mishina96be242006-12-12 00:29:26 -0800614 int ret;
615
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200616 par->match = m->u.kernel.match;
617 par->matchinfo = m->data;
618
619 ret = xt_check_match(par, NFPROTO_IPV4, m->u.match_size - sizeof(*m),
620 ip->proto, ip->invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200621 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800622 duprintf("ip_tables: check failed for `%s'.\n",
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200623 par.match->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200624 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800625 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200626 ++*i;
627 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800628}
629
Denys Vlasenko022748a2008-01-14 23:44:05 -0800630static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200631find_check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
Patrick McHardy4b478242007-12-17 21:46:15 -0800632 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800634 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800635 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Harald Welte2e4e6a12006-01-12 13:30:04 -0800637 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy9c547952007-12-17 21:52:00 -0800638 m->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 "ipt_%s", m->u.user.name);
640 if (IS_ERR(match) || !match) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800641 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return match ? PTR_ERR(match) : -ENOENT;
643 }
644 m->u.kernel.match = match;
645
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200646 ret = check_match(m, par, i);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800647 if (ret)
648 goto err;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800651err:
652 module_put(m->u.kernel.match->me);
653 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Denys Vlasenko022748a2008-01-14 23:44:05 -0800656static int check_target(struct ipt_entry *e, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800657{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900658 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800659 struct xt_target *target;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900660 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800661
662 t = ipt_get_target(e);
663 target = t->u.kernel.target;
664 ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
665 name, e->comefrom, e->ip.proto,
Jan Engelhardt367c6792008-10-08 11:35:17 +0200666 e->ip.invflags & IPT_INV_PROTO, e, t->data);
667 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800668 duprintf("ip_tables: check failed for `%s'.\n",
669 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200670 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800671 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200672 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800673}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Denys Vlasenko022748a2008-01-14 23:44:05 -0800675static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800676find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
Patrick McHardy4b478242007-12-17 21:46:15 -0800677 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800680 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int ret;
682 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200683 struct xt_mtchk_param mtpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Dmitry Mishina96be242006-12-12 00:29:26 -0800685 ret = check_entry(e, name);
686 if (ret)
687 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200690 mtpar.table = name;
691 mtpar.entryinfo = &e->ip;
692 mtpar.hook_mask = e->comefrom;
693 ret = IPT_MATCH_ITERATE(e, find_check_match, &mtpar, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (ret != 0)
695 goto cleanup_matches;
696
697 t = ipt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800698 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -0800699 t->u.user.name,
700 t->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 "ipt_%s", t->u.user.name);
702 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800703 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 ret = target ? PTR_ERR(target) : -ENOENT;
705 goto cleanup_matches;
706 }
707 t->u.kernel.target = target;
708
Dmitry Mishina96be242006-12-12 00:29:26 -0800709 ret = check_target(e, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800710 if (ret)
711 goto err;
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 (*i)++;
714 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800715 err:
716 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 cleanup_matches:
718 IPT_MATCH_ITERATE(e, cleanup_match, &j);
719 return ret;
720}
721
Denys Vlasenko022748a2008-01-14 23:44:05 -0800722static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800724 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 unsigned char *base,
726 unsigned char *limit,
727 const unsigned int *hook_entries,
728 const unsigned int *underflows,
729 unsigned int *i)
730{
731 unsigned int h;
732
733 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0
734 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
735 duprintf("Bad offset %p\n", e);
736 return -EINVAL;
737 }
738
739 if (e->next_offset
740 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
741 duprintf("checking: element %p size %u\n",
742 e, e->next_offset);
743 return -EINVAL;
744 }
745
746 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800747 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if ((unsigned char *)e - base == hook_entries[h])
749 newinfo->hook_entry[h] = hook_entries[h];
750 if ((unsigned char *)e - base == underflows[h])
751 newinfo->underflow[h] = underflows[h];
752 }
753
754 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900755 < 0 (not IPT_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800758 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 e->comefrom = 0;
760
761 (*i)++;
762 return 0;
763}
764
Denys Vlasenko022748a2008-01-14 23:44:05 -0800765static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766cleanup_entry(struct ipt_entry *e, unsigned int *i)
767{
768 struct ipt_entry_target *t;
769
770 if (i && (*i)-- == 0)
771 return 1;
772
773 /* Cleanup all matches */
774 IPT_MATCH_ITERATE(e, cleanup_match, NULL);
775 t = ipt_get_target(e);
776 if (t->u.kernel.target->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700777 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 module_put(t->u.kernel.target->me);
779 return 0;
780}
781
782/* Checks and translates the user-supplied table segment (held in
783 newinfo) */
784static int
785translate_table(const char *name,
786 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800787 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800788 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 unsigned int size,
790 unsigned int number,
791 const unsigned int *hook_entries,
792 const unsigned int *underflows)
793{
794 unsigned int i;
795 int ret;
796
797 newinfo->size = size;
798 newinfo->number = number;
799
800 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800801 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 newinfo->hook_entry[i] = 0xFFFFFFFF;
803 newinfo->underflow[i] = 0xFFFFFFFF;
804 }
805
806 duprintf("translate_table: size %u\n", newinfo->size);
807 i = 0;
808 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800809 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 check_entry_size_and_hooks,
811 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800812 entry0,
813 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 hook_entries, underflows, &i);
815 if (ret != 0)
816 return ret;
817
818 if (i != number) {
819 duprintf("translate_table: %u not %u entries\n",
820 i, number);
821 return -EINVAL;
822 }
823
824 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800825 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* Only hooks which are valid */
827 if (!(valid_hooks & (1 << i)))
828 continue;
829 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
830 duprintf("Invalid hook entry %u %u\n",
831 i, hook_entries[i]);
832 return -EINVAL;
833 }
834 if (newinfo->underflow[i] == 0xFFFFFFFF) {
835 duprintf("Invalid underflow %u %u\n",
836 i, underflows[i]);
837 return -EINVAL;
838 }
839 }
840
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800841 if (!mark_source_chains(newinfo, valid_hooks, entry0))
842 return -ELOOP;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* Finally, each sanity check must pass */
845 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800846 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Dmitry Mishina96be242006-12-12 00:29:26 -0800847 find_check_entry, name, size, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800849 if (ret != 0) {
850 IPT_ENTRY_ITERATE(entry0, newinfo->size,
851 cleanup_entry, &i);
852 return ret;
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700856 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800857 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
858 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 return ret;
862}
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/* Gets counters. */
865static inline int
866add_entry_to_counter(const struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800867 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 unsigned int *i)
869{
870 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
871
872 (*i)++;
873 return 0;
874}
875
Eric Dumazet31836062005-12-13 23:13:48 -0800876static inline int
877set_entry_to_counter(const struct ipt_entry *e,
878 struct ipt_counters total[],
879 unsigned int *i)
880{
881 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
882
883 (*i)++;
884 return 0;
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800888get_counters(const struct xt_table_info *t,
889 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 unsigned int cpu;
892 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800893 unsigned int curcpu;
894
895 /* Instead of clearing (by a previous call to memset())
896 * the counters and using adds, we set the counters
897 * with data used by 'current' CPU
898 * We dont care about preemption here.
899 */
900 curcpu = raw_smp_processor_id();
901
902 i = 0;
903 IPT_ENTRY_ITERATE(t->entries[curcpu],
904 t->size,
905 set_entry_to_counter,
906 counters,
907 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700909 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800910 if (cpu == curcpu)
911 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800913 IPT_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 t->size,
915 add_entry_to_counter,
916 counters,
917 &i);
918 }
919}
920
Denys Vlasenko022748a2008-01-14 23:44:05 -0800921static struct xt_counters * alloc_counters(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Dmitry Mishin27229712006-04-01 02:25:19 -0800923 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800924 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200925 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 /* We need atomic snapshot of counters: rest doesn't change
928 (other than comefrom, which userspace doesn't care
929 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800930 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet31836062005-12-13 23:13:48 -0800931 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (counters == NULL)
Dmitry Mishin27229712006-04-01 02:25:19 -0800934 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 write_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800938 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 write_unlock_bh(&table->lock);
940
Dmitry Mishin27229712006-04-01 02:25:19 -0800941 return counters;
942}
943
944static int
945copy_entries_to_user(unsigned int total_size,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800946 struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800947 void __user *userptr)
948{
949 unsigned int off, num;
950 struct ipt_entry *e;
951 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200952 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800953 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200954 const void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -0800955
956 counters = alloc_counters(table);
957 if (IS_ERR(counters))
958 return PTR_ERR(counters);
959
Eric Dumazet31836062005-12-13 23:13:48 -0800960 /* choose the copy that is on our node/cpu, ...
961 * This choice is lazy (because current thread is
962 * allowed to migrate to another cpu)
963 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800964 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800965 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 ret = -EFAULT;
967 goto free_counters;
968 }
969
970 /* FIXME: use iterator macros --RR */
971 /* ... then go back and fix counters and names */
972 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
973 unsigned int i;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200974 const struct ipt_entry_match *m;
975 const struct ipt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Eric Dumazet31836062005-12-13 23:13:48 -0800977 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (copy_to_user(userptr + off
979 + offsetof(struct ipt_entry, counters),
980 &counters[num],
981 sizeof(counters[num])) != 0) {
982 ret = -EFAULT;
983 goto free_counters;
984 }
985
986 for (i = sizeof(struct ipt_entry);
987 i < e->target_offset;
988 i += m->u.match_size) {
989 m = (void *)e + i;
990
991 if (copy_to_user(userptr + off + i
992 + offsetof(struct ipt_entry_match,
993 u.user.name),
994 m->u.kernel.match->name,
995 strlen(m->u.kernel.match->name)+1)
996 != 0) {
997 ret = -EFAULT;
998 goto free_counters;
999 }
1000 }
1001
1002 t = ipt_get_target(e);
1003 if (copy_to_user(userptr + off + e->target_offset
1004 + offsetof(struct ipt_entry_target,
1005 u.user.name),
1006 t->u.kernel.target->name,
1007 strlen(t->u.kernel.target->name)+1) != 0) {
1008 ret = -EFAULT;
1009 goto free_counters;
1010 }
1011 }
1012
1013 free_counters:
1014 vfree(counters);
1015 return ret;
1016}
1017
Dmitry Mishin27229712006-04-01 02:25:19 -08001018#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001019static void compat_standard_from_user(void *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001020{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001021 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001022
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001023 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001024 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001025 memcpy(dst, &v, sizeof(v));
1026}
1027
1028static int compat_standard_to_user(void __user *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001029{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001030 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001031
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001032 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001033 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001034 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001035}
1036
1037static inline int
Patrick McHardy4b478242007-12-17 21:46:15 -08001038compat_calc_match(struct ipt_entry_match *m, int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001039{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001040 *size += xt_compat_match_offset(m->u.kernel.match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001041 return 0;
1042}
1043
Eric Dumazet259d4e42007-12-04 23:24:56 -08001044static int compat_calc_entry(struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001045 const struct xt_table_info *info,
1046 void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001047{
1048 struct ipt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001049 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001050 int off, i, ret;
1051
Patrick McHardy30c08c42007-12-17 21:47:14 -08001052 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001053 entry_offset = (void *)e - base;
1054 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
1055 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001056 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001057 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001058 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001059 if (ret)
1060 return ret;
1061
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001062 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001063 if (info->hook_entry[i] &&
1064 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001065 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001066 if (info->underflow[i] &&
1067 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001068 newinfo->underflow[i] -= off;
1069 }
1070 return 0;
1071}
1072
Eric Dumazet259d4e42007-12-04 23:24:56 -08001073static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001074 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001075{
1076 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001077
1078 if (!newinfo || !info)
1079 return -EINVAL;
1080
Eric Dumazet259d4e42007-12-04 23:24:56 -08001081 /* we dont care about newinfo->entries[] */
1082 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1083 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001084 loc_cpu_entry = info->entries[raw_smp_processor_id()];
1085 return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001086 compat_calc_entry, info, loc_cpu_entry,
1087 newinfo);
Dmitry Mishin27229712006-04-01 02:25:19 -08001088}
1089#endif
1090
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001091static int get_info(struct net *net, void __user *user, int *len, int compat)
Dmitry Mishin27229712006-04-01 02:25:19 -08001092{
1093 char name[IPT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001094 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001095 int ret;
1096
1097 if (*len != sizeof(struct ipt_getinfo)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001098 duprintf("length %u != %zu\n", *len,
1099 sizeof(struct ipt_getinfo));
Dmitry Mishin27229712006-04-01 02:25:19 -08001100 return -EINVAL;
1101 }
1102
1103 if (copy_from_user(name, user, sizeof(name)) != 0)
1104 return -EFAULT;
1105
1106 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1107#ifdef CONFIG_COMPAT
1108 if (compat)
1109 xt_compat_lock(AF_INET);
1110#endif
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001111 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001112 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001113 if (t && !IS_ERR(t)) {
1114 struct ipt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001115 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001116
1117#ifdef CONFIG_COMPAT
1118 if (compat) {
1119 struct xt_table_info tmp;
1120 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001121 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001122 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001123 }
1124#endif
1125 info.valid_hooks = t->valid_hooks;
1126 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001127 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001128 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001129 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001130 info.num_entries = private->number;
1131 info.size = private->size;
1132 strcpy(info.name, name);
1133
1134 if (copy_to_user(user, &info, *len) != 0)
1135 ret = -EFAULT;
1136 else
1137 ret = 0;
1138
1139 xt_table_unlock(t);
1140 module_put(t->me);
1141 } else
1142 ret = t ? PTR_ERR(t) : -ENOENT;
1143#ifdef CONFIG_COMPAT
1144 if (compat)
1145 xt_compat_unlock(AF_INET);
1146#endif
1147 return ret;
1148}
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001151get_entries(struct net *net, struct ipt_get_entries __user *uptr, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001154 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001155 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Dmitry Mishin27229712006-04-01 02:25:19 -08001157 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001158 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001159 return -EINVAL;
1160 }
1161 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1162 return -EFAULT;
1163 if (*len != sizeof(struct ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001164 duprintf("get_entries: %u != %zu\n",
1165 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001166 return -EINVAL;
1167 }
1168
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001169 t = xt_find_table_lock(net, AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001171 const struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001172 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001173 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001174 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 t, uptr->entrytable);
1176 else {
1177 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001178 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001179 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001182 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 } else
1184 ret = t ? PTR_ERR(t) : -ENOENT;
1185
1186 return ret;
1187}
1188
1189static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001190__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001191 struct xt_table_info *newinfo, unsigned int num_counters,
1192 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001193{
1194 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001195 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001196 struct xt_table_info *oldinfo;
1197 struct xt_counters *counters;
1198 void *loc_cpu_old_entry;
1199
1200 ret = 0;
1201 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1202 if (!counters) {
1203 ret = -ENOMEM;
1204 goto out;
1205 }
1206
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001207 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Dmitry Mishin27229712006-04-01 02:25:19 -08001208 "iptable_%s", name);
1209 if (!t || IS_ERR(t)) {
1210 ret = t ? PTR_ERR(t) : -ENOENT;
1211 goto free_newinfo_counters_untrans;
1212 }
1213
1214 /* You lied! */
1215 if (valid_hooks != t->valid_hooks) {
1216 duprintf("Valid hook crap: %08X vs %08X\n",
1217 valid_hooks, t->valid_hooks);
1218 ret = -EINVAL;
1219 goto put_module;
1220 }
1221
1222 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1223 if (!oldinfo)
1224 goto put_module;
1225
1226 /* Update module usage count based on number of rules */
1227 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1228 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1229 if ((oldinfo->number > oldinfo->initial_entries) ||
1230 (newinfo->number <= oldinfo->initial_entries))
1231 module_put(t->me);
1232 if ((oldinfo->number > oldinfo->initial_entries) &&
1233 (newinfo->number <= oldinfo->initial_entries))
1234 module_put(t->me);
1235
1236 /* Get the old counters. */
1237 get_counters(oldinfo, counters);
1238 /* Decrease module usage counts and free resource */
1239 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Patrick McHardy4b478242007-12-17 21:46:15 -08001240 IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1241 NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001242 xt_free_table_info(oldinfo);
1243 if (copy_to_user(counters_ptr, counters,
1244 sizeof(struct xt_counters) * num_counters) != 0)
1245 ret = -EFAULT;
1246 vfree(counters);
1247 xt_table_unlock(t);
1248 return ret;
1249
1250 put_module:
1251 module_put(t->me);
1252 xt_table_unlock(t);
1253 free_newinfo_counters_untrans:
1254 vfree(counters);
1255 out:
1256 return ret;
1257}
1258
1259static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001260do_replace(struct net *net, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 int ret;
1263 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001264 struct xt_table_info *newinfo;
1265 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1268 return -EFAULT;
1269
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001270 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001271 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1272 return -ENOMEM;
1273
Harald Welte2e4e6a12006-01-12 13:30:04 -08001274 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (!newinfo)
1276 return -ENOMEM;
1277
Patrick McHardy9c547952007-12-17 21:52:00 -08001278 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001279 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1280 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 tmp.size) != 0) {
1282 ret = -EFAULT;
1283 goto free_newinfo;
1284 }
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001287 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 tmp.hook_entry, tmp.underflow);
1289 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001290 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 duprintf("ip_tables: Translated table\n");
1293
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001294 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001295 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001296 if (ret)
1297 goto free_newinfo_untrans;
1298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Dmitry Mishin27229712006-04-01 02:25:19 -08001300 free_newinfo_untrans:
Patrick McHardy9c547952007-12-17 21:52:00 -08001301 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001303 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return ret;
1305}
1306
1307/* We're lazy, and add to the first CPU; overflow works its fey magic
1308 * and everything is OK. */
Denys Vlasenko022748a2008-01-14 23:44:05 -08001309static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310add_counter_to_entry(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001311 const struct xt_counters addme[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 unsigned int *i)
1313{
1314#if 0
1315 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1316 *i,
1317 (long unsigned int)e->counters.pcnt,
1318 (long unsigned int)e->counters.bcnt,
1319 (long unsigned int)addme[*i].pcnt,
1320 (long unsigned int)addme[*i].bcnt);
1321#endif
1322
1323 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1324
1325 (*i)++;
1326 return 0;
1327}
1328
1329static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001330do_add_counters(struct net *net, void __user *user, unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 unsigned int i;
Dmitry Mishin27229712006-04-01 02:25:19 -08001333 struct xt_counters_info tmp;
1334 struct xt_counters *paddc;
1335 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001336 const char *name;
Dmitry Mishin27229712006-04-01 02:25:19 -08001337 int size;
1338 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001339 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001340 const struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001342 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001343#ifdef CONFIG_COMPAT
1344 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Dmitry Mishin27229712006-04-01 02:25:19 -08001346 if (compat) {
1347 ptmp = &compat_tmp;
1348 size = sizeof(struct compat_xt_counters_info);
1349 } else
1350#endif
1351 {
1352 ptmp = &tmp;
1353 size = sizeof(struct xt_counters_info);
1354 }
1355
1356 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -EFAULT;
1358
Dmitry Mishin27229712006-04-01 02:25:19 -08001359#ifdef CONFIG_COMPAT
1360 if (compat) {
1361 num_counters = compat_tmp.num_counters;
1362 name = compat_tmp.name;
1363 } else
1364#endif
1365 {
1366 num_counters = tmp.num_counters;
1367 name = tmp.name;
1368 }
1369
1370 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -EINVAL;
1372
Dmitry Mishin27229712006-04-01 02:25:19 -08001373 paddc = vmalloc_node(len - size, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (!paddc)
1375 return -ENOMEM;
1376
Dmitry Mishin27229712006-04-01 02:25:19 -08001377 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 ret = -EFAULT;
1379 goto free;
1380 }
1381
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001382 t = xt_find_table_lock(net, AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (!t || IS_ERR(t)) {
1384 ret = t ? PTR_ERR(t) : -ENOENT;
1385 goto free;
1386 }
1387
1388 write_lock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001389 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001390 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 ret = -EINVAL;
1392 goto unlock_up_free;
1393 }
1394
1395 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001396 /* Choose the copy that is on our node */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001397 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -08001398 IPT_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001399 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 add_counter_to_entry,
Dmitry Mishin27229712006-04-01 02:25:19 -08001401 paddc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 &i);
1403 unlock_up_free:
1404 write_unlock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001405 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 module_put(t->me);
1407 free:
1408 vfree(paddc);
1409
1410 return ret;
1411}
1412
Dmitry Mishin27229712006-04-01 02:25:19 -08001413#ifdef CONFIG_COMPAT
1414struct compat_ipt_replace {
1415 char name[IPT_TABLE_MAXNAMELEN];
1416 u32 valid_hooks;
1417 u32 num_entries;
1418 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001419 u32 hook_entry[NF_INET_NUMHOOKS];
1420 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001421 u32 num_counters;
1422 compat_uptr_t counters; /* struct ipt_counters * */
1423 struct compat_ipt_entry entries[0];
1424};
1425
Patrick McHardya18aa312007-12-12 10:35:16 -08001426static int
1427compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001428 unsigned int *size, struct xt_counters *counters,
Patrick McHardya18aa312007-12-12 10:35:16 -08001429 unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001430{
Al Viro3e597c62006-09-24 23:42:20 +01001431 struct ipt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001432 struct compat_ipt_entry __user *ce;
1433 u_int16_t target_offset, next_offset;
1434 compat_uint_t origsize;
1435 int ret;
1436
1437 ret = -EFAULT;
1438 origsize = *size;
1439 ce = (struct compat_ipt_entry __user *)*dstptr;
Patrick McHardy78000072006-05-03 23:20:27 -07001440 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
Dmitry Mishin27229712006-04-01 02:25:19 -08001441 goto out;
1442
Patrick McHardya18aa312007-12-12 10:35:16 -08001443 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1444 goto out;
1445
Dmitry Mishin27229712006-04-01 02:25:19 -08001446 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001447 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1448
Patrick McHardyac8e27f2007-12-17 21:45:52 -08001449 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001450 target_offset = e->target_offset - (origsize - *size);
1451 if (ret)
1452 goto out;
1453 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001454 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001455 if (ret)
1456 goto out;
1457 ret = -EFAULT;
1458 next_offset = e->next_offset - (origsize - *size);
Patrick McHardy78000072006-05-03 23:20:27 -07001459 if (put_user(target_offset, &ce->target_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001460 goto out;
Patrick McHardy78000072006-05-03 23:20:27 -07001461 if (put_user(next_offset, &ce->next_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001462 goto out;
Patrick McHardya18aa312007-12-12 10:35:16 -08001463
1464 (*i)++;
Dmitry Mishin27229712006-04-01 02:25:19 -08001465 return 0;
1466out:
1467 return ret;
1468}
1469
Denys Vlasenko022748a2008-01-14 23:44:05 -08001470static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001471compat_find_calc_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001472 const char *name,
1473 const struct ipt_ip *ip,
1474 unsigned int hookmask,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001475 int *size, unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001476{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001477 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001478
1479 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001480 m->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001481 "ipt_%s", m->u.user.name);
1482 if (IS_ERR(match) || !match) {
1483 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001484 m->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001485 return match ? PTR_ERR(match) : -ENOENT;
1486 }
1487 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001488 *size += xt_compat_match_offset(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001489
1490 (*i)++;
1491 return 0;
1492}
1493
Denys Vlasenko022748a2008-01-14 23:44:05 -08001494static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001495compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1496{
1497 if (i && (*i)-- == 0)
1498 return 1;
1499
1500 module_put(m->u.kernel.match->me);
1501 return 0;
1502}
1503
Denys Vlasenko022748a2008-01-14 23:44:05 -08001504static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001505compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001506{
1507 struct ipt_entry_target *t;
1508
1509 if (i && (*i)-- == 0)
1510 return 1;
1511
1512 /* Cleanup all matches */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001513 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1514 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001515 module_put(t->u.kernel.target->me);
1516 return 0;
1517}
1518
Denys Vlasenko022748a2008-01-14 23:44:05 -08001519static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001520check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001521 struct xt_table_info *newinfo,
1522 unsigned int *size,
1523 unsigned char *base,
1524 unsigned char *limit,
1525 unsigned int *hook_entries,
1526 unsigned int *underflows,
1527 unsigned int *i,
1528 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001529{
1530 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001531 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001532 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001533 unsigned int j;
1534 int ret, off, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001535
1536 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1537 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0
1538 || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1539 duprintf("Bad offset %p, limit = %p\n", e, limit);
1540 return -EINVAL;
1541 }
1542
1543 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001544 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001545 duprintf("checking: element %p size %u\n",
1546 e, e->next_offset);
1547 return -EINVAL;
1548 }
1549
Patrick McHardy73cd5982007-12-17 21:47:32 -08001550 /* For purposes of check_entry casting the compat entry is fine */
1551 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001552 if (ret)
1553 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001554
Patrick McHardy30c08c42007-12-17 21:47:14 -08001555 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001556 entry_offset = (void *)e - (void *)base;
1557 j = 0;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001558 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1559 &e->ip, e->comefrom, &off, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001560 if (ret != 0)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001561 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001562
Patrick McHardy73cd5982007-12-17 21:47:32 -08001563 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001564 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -08001565 t->u.user.name,
1566 t->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001567 "ipt_%s", t->u.user.name);
1568 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001569 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001570 t->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001571 ret = target ? PTR_ERR(target) : -ENOENT;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001572 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001573 }
1574 t->u.kernel.target = target;
1575
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001576 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001577 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001578 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001579 if (ret)
1580 goto out;
1581
1582 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001583 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001584 if ((unsigned char *)e - base == hook_entries[h])
1585 newinfo->hook_entry[h] = hook_entries[h];
1586 if ((unsigned char *)e - base == underflows[h])
1587 newinfo->underflow[h] = underflows[h];
1588 }
1589
1590 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001591 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001592 e->comefrom = 0;
1593
1594 (*i)++;
1595 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001596
Dmitry Mishin27229712006-04-01 02:25:19 -08001597out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001598 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001599release_matches:
1600 IPT_MATCH_ITERATE(e, compat_release_match, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001601 return ret;
1602}
1603
Patrick McHardy4b478242007-12-17 21:46:15 -08001604static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001605compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001606 unsigned int *size, const char *name,
1607 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001608{
1609 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001610 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001611 struct ipt_entry *de;
1612 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001613 int ret, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001614
1615 ret = 0;
1616 origsize = *size;
1617 de = (struct ipt_entry *)*dstptr;
1618 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001619 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001620
Patrick McHardy73cd5982007-12-17 21:47:32 -08001621 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001622 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1623
Patrick McHardy73cd5982007-12-17 21:47:32 -08001624 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1625 dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001626 if (ret)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001627 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001628 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001629 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001630 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001631 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001632
1633 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001634 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001635 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1636 newinfo->hook_entry[h] -= origsize - *size;
1637 if ((unsigned char *)de - base < newinfo->underflow[h])
1638 newinfo->underflow[h] -= origsize - *size;
1639 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001640 return ret;
1641}
Dmitry Mishin27229712006-04-01 02:25:19 -08001642
Denys Vlasenko022748a2008-01-14 23:44:05 -08001643static int
1644compat_check_entry(struct ipt_entry *e, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001645 unsigned int *i)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001646{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001647 struct xt_mtchk_param mtpar;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001648 unsigned int j;
1649 int ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001650
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001651 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001652 mtpar.table = name;
1653 mtpar.entryinfo = &e->ip;
1654 mtpar.hook_mask = e->comefrom;
1655 ret = IPT_MATCH_ITERATE(e, check_match, &mtpar, &j);
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001656 if (ret)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001657 goto cleanup_matches;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001658
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001659 ret = check_target(e, name);
1660 if (ret)
1661 goto cleanup_matches;
1662
1663 (*i)++;
1664 return 0;
1665
1666 cleanup_matches:
1667 IPT_MATCH_ITERATE(e, cleanup_match, &j);
1668 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001669}
1670
Dmitry Mishin27229712006-04-01 02:25:19 -08001671static int
1672translate_compat_table(const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001673 unsigned int valid_hooks,
1674 struct xt_table_info **pinfo,
1675 void **pentry0,
1676 unsigned int total_size,
1677 unsigned int number,
1678 unsigned int *hook_entries,
1679 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001680{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001681 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001682 struct xt_table_info *newinfo, *info;
1683 void *pos, *entry0, *entry1;
1684 unsigned int size;
1685 int ret;
1686
1687 info = *pinfo;
1688 entry0 = *pentry0;
1689 size = total_size;
1690 info->number = number;
1691
1692 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001693 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001694 info->hook_entry[i] = 0xFFFFFFFF;
1695 info->underflow[i] = 0xFFFFFFFF;
1696 }
1697
1698 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001699 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001700 xt_compat_lock(AF_INET);
1701 /* Walk through entries, checking offsets. */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001702 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1703 check_compat_entry_size_and_hooks,
1704 info, &size, entry0,
1705 entry0 + total_size,
1706 hook_entries, underflows, &j, name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001707 if (ret != 0)
1708 goto out_unlock;
1709
1710 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001711 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001712 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001713 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001714 goto out_unlock;
1715 }
1716
1717 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001718 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001719 /* Only hooks which are valid */
1720 if (!(valid_hooks & (1 << i)))
1721 continue;
1722 if (info->hook_entry[i] == 0xFFFFFFFF) {
1723 duprintf("Invalid hook entry %u %u\n",
1724 i, hook_entries[i]);
1725 goto out_unlock;
1726 }
1727 if (info->underflow[i] == 0xFFFFFFFF) {
1728 duprintf("Invalid underflow %u %u\n",
1729 i, underflows[i]);
1730 goto out_unlock;
1731 }
1732 }
1733
1734 ret = -ENOMEM;
1735 newinfo = xt_alloc_table_info(size);
1736 if (!newinfo)
1737 goto out_unlock;
1738
1739 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001740 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001741 newinfo->hook_entry[i] = info->hook_entry[i];
1742 newinfo->underflow[i] = info->underflow[i];
1743 }
1744 entry1 = newinfo->entries[raw_smp_processor_id()];
1745 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001746 size = total_size;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001747 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
Patrick McHardy9c547952007-12-17 21:52:00 -08001748 compat_copy_entry_from_user,
1749 &pos, &size, name, newinfo, entry1);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001750 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001751 xt_compat_unlock(AF_INET);
1752 if (ret)
1753 goto free_newinfo;
1754
1755 ret = -ELOOP;
1756 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1757 goto free_newinfo;
1758
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001759 i = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001760 ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001761 name, &i);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001762 if (ret) {
1763 j -= i;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001764 COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1765 compat_release_entry, &j);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001766 IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1767 xt_free_table_info(newinfo);
1768 return ret;
1769 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001770
Dmitry Mishin27229712006-04-01 02:25:19 -08001771 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001772 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001773 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1774 memcpy(newinfo->entries[i], entry1, newinfo->size);
1775
1776 *pinfo = newinfo;
1777 *pentry0 = entry1;
1778 xt_free_table_info(info);
1779 return 0;
1780
1781free_newinfo:
1782 xt_free_table_info(newinfo);
1783out:
Patrick McHardy73cd5982007-12-17 21:47:32 -08001784 COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001785 return ret;
1786out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001787 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001788 xt_compat_unlock(AF_INET);
1789 goto out;
1790}
1791
1792static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001793compat_do_replace(struct net *net, void __user *user, unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001794{
1795 int ret;
1796 struct compat_ipt_replace tmp;
1797 struct xt_table_info *newinfo;
1798 void *loc_cpu_entry;
1799
1800 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1801 return -EFAULT;
1802
Dmitry Mishin27229712006-04-01 02:25:19 -08001803 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001804 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001805 return -ENOMEM;
1806 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1807 return -ENOMEM;
1808
1809 newinfo = xt_alloc_table_info(tmp.size);
1810 if (!newinfo)
1811 return -ENOMEM;
1812
Patrick McHardy9c547952007-12-17 21:52:00 -08001813 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001814 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1815 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1816 tmp.size) != 0) {
1817 ret = -EFAULT;
1818 goto free_newinfo;
1819 }
1820
1821 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001822 &newinfo, &loc_cpu_entry, tmp.size,
1823 tmp.num_entries, tmp.hook_entry,
1824 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001825 if (ret != 0)
1826 goto free_newinfo;
1827
1828 duprintf("compat_do_replace: Translated table\n");
1829
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001830 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001831 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001832 if (ret)
1833 goto free_newinfo_untrans;
1834 return 0;
1835
1836 free_newinfo_untrans:
Patrick McHardy4b478242007-12-17 21:46:15 -08001837 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001838 free_newinfo:
1839 xt_free_table_info(newinfo);
1840 return ret;
1841}
1842
1843static int
1844compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001845 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001846{
1847 int ret;
1848
1849 if (!capable(CAP_NET_ADMIN))
1850 return -EPERM;
1851
1852 switch (cmd) {
1853 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001854 ret = compat_do_replace(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001855 break;
1856
1857 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001858 ret = do_add_counters(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001859 break;
1860
1861 default:
1862 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1863 ret = -EINVAL;
1864 }
1865
1866 return ret;
1867}
1868
Patrick McHardy4b478242007-12-17 21:46:15 -08001869struct compat_ipt_get_entries {
Dmitry Mishin27229712006-04-01 02:25:19 -08001870 char name[IPT_TABLE_MAXNAMELEN];
1871 compat_uint_t size;
1872 struct compat_ipt_entry entrytable[0];
1873};
1874
Patrick McHardy4b478242007-12-17 21:46:15 -08001875static int
1876compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1877 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001878{
Dmitry Mishin27229712006-04-01 02:25:19 -08001879 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001880 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001881 void __user *pos;
1882 unsigned int size;
1883 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001884 const void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001885 unsigned int i = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001886
1887 counters = alloc_counters(table);
1888 if (IS_ERR(counters))
1889 return PTR_ERR(counters);
1890
1891 /* choose the copy that is on our node/cpu, ...
1892 * This choice is lazy (because current thread is
1893 * allowed to migrate to another cpu)
1894 */
1895 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1896 pos = userptr;
1897 size = total_size;
1898 ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
Patrick McHardya18aa312007-12-12 10:35:16 -08001899 compat_copy_entry_to_user,
1900 &pos, &size, counters, &i);
Dmitry Mishin27229712006-04-01 02:25:19 -08001901
Dmitry Mishin27229712006-04-01 02:25:19 -08001902 vfree(counters);
1903 return ret;
1904}
1905
1906static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001907compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1908 int *len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001909{
1910 int ret;
1911 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001912 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001913
Dmitry Mishin27229712006-04-01 02:25:19 -08001914 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001915 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001916 return -EINVAL;
1917 }
1918
1919 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1920 return -EFAULT;
1921
1922 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001923 duprintf("compat_get_entries: %u != %zu\n",
1924 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001925 return -EINVAL;
1926 }
1927
1928 xt_compat_lock(AF_INET);
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001929 t = xt_find_table_lock(net, AF_INET, get.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001930 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001931 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001932 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08001933 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001934 ret = compat_table_info(private, &info);
1935 if (!ret && get.size == info.size) {
1936 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001937 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001938 } else if (!ret) {
1939 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001940 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001941 ret = -EAGAIN;
Dmitry Mishin27229712006-04-01 02:25:19 -08001942 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001943 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001944 module_put(t->me);
1945 xt_table_unlock(t);
1946 } else
1947 ret = t ? PTR_ERR(t) : -ENOENT;
1948
1949 xt_compat_unlock(AF_INET);
1950 return ret;
1951}
1952
Patrick McHardy79030ed2006-09-20 12:05:08 -07001953static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1954
Dmitry Mishin27229712006-04-01 02:25:19 -08001955static int
1956compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1957{
1958 int ret;
1959
Björn Steinbrink82fac052006-10-20 00:21:10 -07001960 if (!capable(CAP_NET_ADMIN))
1961 return -EPERM;
1962
Dmitry Mishin27229712006-04-01 02:25:19 -08001963 switch (cmd) {
1964 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001965 ret = get_info(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001966 break;
1967 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001968 ret = compat_get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001969 break;
1970 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001971 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001972 }
1973 return ret;
1974}
1975#endif
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001978do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 int ret;
1981
1982 if (!capable(CAP_NET_ADMIN))
1983 return -EPERM;
1984
1985 switch (cmd) {
1986 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001987 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 break;
1989
1990 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001991 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 break;
1993
1994 default:
1995 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1996 ret = -EINVAL;
1997 }
1998
1999 return ret;
2000}
2001
2002static int
2003do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2004{
2005 int ret;
2006
2007 if (!capable(CAP_NET_ADMIN))
2008 return -EPERM;
2009
2010 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002011 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002012 ret = get_info(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002014
2015 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002016 ret = get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08002017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 case IPT_SO_GET_REVISION_MATCH:
2020 case IPT_SO_GET_REVISION_TARGET: {
2021 struct ipt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002022 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 if (*len != sizeof(rev)) {
2025 ret = -EINVAL;
2026 break;
2027 }
2028 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2029 ret = -EFAULT;
2030 break;
2031 }
2032
2033 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002034 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002036 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Harald Welte2e4e6a12006-01-12 13:30:04 -08002038 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2039 rev.revision,
2040 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 "ipt_%s", rev.name);
2042 break;
2043 }
2044
2045 default:
2046 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2047 ret = -EINVAL;
2048 }
2049
2050 return ret;
2051}
2052
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002053struct xt_table *ipt_register_table(struct net *net, struct xt_table *table,
2054 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002057 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08002058 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08002060 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08002061 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Harald Welte2e4e6a12006-01-12 13:30:04 -08002063 newinfo = xt_alloc_table_info(repl->size);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002064 if (!newinfo) {
2065 ret = -ENOMEM;
2066 goto out;
2067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Patrick McHardy9c547952007-12-17 21:52:00 -08002069 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002070 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2071 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08002074 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 repl->num_entries,
2076 repl->hook_entry,
2077 repl->underflow);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002078 if (ret != 0)
2079 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002081 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08002082 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002083 ret = PTR_ERR(new_table);
2084 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002087 return new_table;
2088
2089out_free:
2090 xt_free_table_info(newinfo);
2091out:
2092 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Jan Engelhardte60a13e2007-02-07 15:12:33 -08002095void ipt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002097 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002098 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08002099 struct module *table_owner = table->me;
Eric Dumazet31836062005-12-13 23:13:48 -08002100
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002101 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002104 loc_cpu_entry = private->entries[raw_smp_processor_id()];
2105 IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
Alexey Dobriyandf200962008-01-31 04:05:34 -08002106 if (private->number > private->initial_entries)
2107 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002108 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
2111/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002112static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2114 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002115 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Patrick McHardy9c547952007-12-17 21:52:00 -08002117 return ((test_type == 0xFF) ||
2118 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 ^ invert;
2120}
2121
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002122static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002123icmp_match(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Jan Engelhardt5452e422008-04-14 11:15:35 +02002125 const struct icmphdr *ic;
2126 struct icmphdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002127 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002130 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002131 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002133 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (ic == NULL) {
2135 /* We've been asked to examine this packet, and we
2136 * can't. Hence, no choice but to drop.
2137 */
2138 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002139 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002140 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
2142
2143 return icmp_type_code_match(icmpinfo->type,
2144 icmpinfo->code[0],
2145 icmpinfo->code[1],
2146 ic->type, ic->code,
2147 !!(icmpinfo->invflags&IPT_ICMP_INV));
2148}
2149
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002150static bool icmp_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002152 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002154 /* Must specify no unknown invflags */
2155 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
2158/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07002159static struct xt_target ipt_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 .name = IPT_STANDARD_TARGET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002161 .targetsize = sizeof(int),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002162 .family = AF_INET,
Dmitry Mishin27229712006-04-01 02:25:19 -08002163#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07002164 .compatsize = sizeof(compat_int_t),
2165 .compat_from_user = compat_standard_from_user,
2166 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002167#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168};
2169
Patrick McHardy9f15c532007-07-07 22:22:02 -07002170static struct xt_target ipt_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 .name = IPT_ERROR_TARGET,
2172 .target = ipt_error,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002173 .targetsize = IPT_FUNCTION_MAXNAMELEN,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002174 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175};
2176
2177static struct nf_sockopt_ops ipt_sockopts = {
2178 .pf = PF_INET,
2179 .set_optmin = IPT_BASE_CTL,
2180 .set_optmax = IPT_SO_SET_MAX+1,
2181 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002182#ifdef CONFIG_COMPAT
2183 .compat_set = compat_do_ipt_set_ctl,
2184#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 .get_optmin = IPT_BASE_CTL,
2186 .get_optmax = IPT_SO_GET_MAX+1,
2187 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002188#ifdef CONFIG_COMPAT
2189 .compat_get = compat_do_ipt_get_ctl,
2190#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002191 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192};
2193
Patrick McHardy9f15c532007-07-07 22:22:02 -07002194static struct xt_match icmp_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 .name = "icmp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002196 .match = icmp_match,
2197 .matchsize = sizeof(struct ipt_icmp),
Patrick McHardy9c547952007-12-17 21:52:00 -08002198 .checkentry = icmp_checkentry,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002199 .proto = IPPROTO_ICMP,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002200 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201};
2202
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002203static int __net_init ip_tables_net_init(struct net *net)
2204{
2205 return xt_proto_init(net, AF_INET);
2206}
2207
2208static void __net_exit ip_tables_net_exit(struct net *net)
2209{
2210 xt_proto_fini(net, AF_INET);
2211}
2212
2213static struct pernet_operations ip_tables_net_ops = {
2214 .init = ip_tables_net_init,
2215 .exit = ip_tables_net_exit,
2216};
2217
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002218static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 int ret;
2221
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002222 ret = register_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002223 if (ret < 0)
2224 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002227 ret = xt_register_target(&ipt_standard_target);
2228 if (ret < 0)
2229 goto err2;
2230 ret = xt_register_target(&ipt_error_target);
2231 if (ret < 0)
2232 goto err3;
2233 ret = xt_register_match(&icmp_matchstruct);
2234 if (ret < 0)
2235 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 /* Register setsockopt */
2238 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002239 if (ret < 0)
2240 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Dan Aloni0236e662007-07-09 13:20:12 -07002242 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002244
2245err5:
2246 xt_unregister_match(&icmp_matchstruct);
2247err4:
2248 xt_unregister_target(&ipt_error_target);
2249err3:
2250 xt_unregister_target(&ipt_standard_target);
2251err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002252 unregister_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002253err1:
2254 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002257static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
2259 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002260
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002261 xt_unregister_match(&icmp_matchstruct);
2262 xt_unregister_target(&ipt_error_target);
2263 xt_unregister_target(&ipt_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002264
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002265 unregister_pernet_subsys(&ip_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
2268EXPORT_SYMBOL(ipt_register_table);
2269EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002271module_init(ip_tables_init);
2272module_exit(ip_tables_fini);