blob: 213fb27debc1e583adb548b4628ad0ac782d3492 [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;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200351 mtpar.family = tgpar.family = NFPROTO_IPV4;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200352 tgpar.hooknum = hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 read_lock_bh(&table->lock);
355 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Patrick McHardy83117312006-08-17 18:13:53 -0700356 private = table->private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800357 table_base = (void *)private->entries[smp_processor_id()];
358 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* For return from builtin chain */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800361 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 do {
364 IP_NF_ASSERT(e);
365 IP_NF_ASSERT(back);
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200366 if (ip_packet_match(ip, indev, outdev,
367 &e->ip, mtpar.fragoff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct ipt_entry_target *t;
369
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200370 if (IPT_MATCH_ITERATE(e, do_match, skb, &mtpar) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 goto no_match;
372
373 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
374
375 t = ipt_get_target(e);
376 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700377
378#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
379 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
380 /* The packet is traced: log it */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700381 if (unlikely(skb->nf_trace))
382 trace_packet(skb, hook, in, out,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700383 table->name, private, e);
384#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* Standard target? */
386 if (!t->u.kernel.target->target) {
387 int v;
388
389 v = ((struct ipt_standard_target *)t)->verdict;
390 if (v < 0) {
391 /* Pop from stack? */
392 if (v != IPT_RETURN) {
393 verdict = (unsigned)(-v) - 1;
394 break;
395 }
396 e = back;
397 back = get_entry(table_base,
398 back->comefrom);
399 continue;
400 }
Patrick McHardy05465342005-08-21 23:31:43 -0700401 if (table_base + v != (void *)e + e->next_offset
402 && !(e->ip.flags & IPT_F_GOTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* Save old back ptr in next entry */
404 struct ipt_entry *next
405 = (void *)e + e->next_offset;
406 next->comefrom
407 = (void *)back - table_base;
408 /* set back pointer to next entry */
409 back = next;
410 }
411
412 e = get_entry(table_base, v);
413 } else {
414 /* Targets which reenter must return
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900415 abs. verdicts */
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200416 tgpar.target = t->u.kernel.target;
417 tgpar.targinfo = t->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#ifdef CONFIG_NETFILTER_DEBUG
419 ((struct ipt_entry *)table_base)->comefrom
420 = 0xeeeeeeec;
421#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700422 verdict = t->u.kernel.target->target(skb,
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200423 &tgpar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#ifdef CONFIG_NETFILTER_DEBUG
425 if (((struct ipt_entry *)table_base)->comefrom
426 != 0xeeeeeeec
427 && verdict == IPT_CONTINUE) {
428 printk("Target %s reentered!\n",
429 t->u.kernel.target->name);
430 verdict = NF_DROP;
431 }
432 ((struct ipt_entry *)table_base)->comefrom
433 = 0x57acc001;
434#endif
435 /* Target might have changed stuff. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700436 ip = ip_hdr(skb);
437 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (verdict == IPT_CONTINUE)
440 e = (void *)e + e->next_offset;
441 else
442 /* Verdict */
443 break;
444 }
445 } else {
446
447 no_match:
448 e = (void *)e + e->next_offset;
449 }
450 } while (!hotdrop);
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 read_unlock_bh(&table->lock);
453
454#ifdef DEBUG_ALLOW_ALL
455 return NF_ACCEPT;
456#else
457 if (hotdrop)
458 return NF_DROP;
459 else return verdict;
460#endif
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/* Figures out from what hook each rule can be called: returns 0 if
464 there are loops. Puts hook bitmask in comefrom. */
465static int
Harald Welte2e4e6a12006-01-12 13:30:04 -0800466mark_source_chains(struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800467 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 unsigned int hook;
470
471 /* No recursion; use packet counter to save back ptrs (reset
472 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800473 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800475 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 if (!(valid_hooks & (1 << hook)))
478 continue;
479
480 /* Set initial back pointer. */
481 e->counters.pcnt = pos;
482
483 for (;;) {
484 struct ipt_standard_target *t
485 = (void *)ipt_get_target(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800486 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800488 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 printk("iptables: loop hook %u pos %u %08X.\n",
490 hook, pos, e->comefrom);
491 return 0;
492 }
Patrick McHardy9c547952007-12-17 21:52:00 -0800493 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* Unconditional return/END. */
Al Viroe1b4b9f2006-12-12 00:29:52 -0800496 if ((e->target_offset == sizeof(struct ipt_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 && (strcmp(t->target.u.user.name,
498 IPT_STANDARD_TARGET) == 0)
499 && t->verdict < 0
Al Viroe1b4b9f2006-12-12 00:29:52 -0800500 && unconditional(&e->ip)) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned int oldpos, size;
502
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800503 if (t->verdict < -NF_MAX_VERDICT - 1) {
504 duprintf("mark_source_chains: bad "
505 "negative verdict (%i)\n",
506 t->verdict);
507 return 0;
508 }
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Return: backtrack through the last
511 big jump. */
512 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800513 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514#ifdef DEBUG_IP_FIREWALL_USER
515 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800516 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 duprintf("Back unset "
518 "on hook %u "
519 "rule %u\n",
520 hook, pos);
521 }
522#endif
523 oldpos = pos;
524 pos = e->counters.pcnt;
525 e->counters.pcnt = 0;
526
527 /* We're at the start. */
528 if (pos == oldpos)
529 goto next;
530
531 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800532 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } while (oldpos == pos + e->next_offset);
534
535 /* Move along one */
536 size = e->next_offset;
537 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800538 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 e->counters.pcnt = pos;
540 pos += size;
541 } else {
542 int newpos = t->verdict;
543
544 if (strcmp(t->target.u.user.name,
545 IPT_STANDARD_TARGET) == 0
546 && newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800547 if (newpos > newinfo->size -
548 sizeof(struct ipt_entry)) {
549 duprintf("mark_source_chains: "
550 "bad verdict (%i)\n",
551 newpos);
552 return 0;
553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* This a jump; chase it. */
555 duprintf("Jump rule %u -> %u\n",
556 pos, newpos);
557 } else {
558 /* ... this is a fallthru */
559 newpos = pos + e->next_offset;
560 }
561 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800562 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 e->counters.pcnt = pos;
564 pos = newpos;
565 }
566 }
567 next:
568 duprintf("Finished chain %u\n", hook);
569 }
570 return 1;
571}
572
Denys Vlasenko022748a2008-01-14 23:44:05 -0800573static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574cleanup_match(struct ipt_entry_match *m, unsigned int *i)
575{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200576 struct xt_mtdtor_param par;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (i && (*i)-- == 0)
579 return 1;
580
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200581 par.match = m->u.kernel.match;
582 par.matchinfo = m->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200583 par.family = NFPROTO_IPV4;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200584 if (par.match->destroy != NULL)
585 par.match->destroy(&par);
586 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 0;
588}
589
Denys Vlasenko022748a2008-01-14 23:44:05 -0800590static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800591check_entry(struct ipt_entry *e, const char *name)
592{
593 struct ipt_entry_target *t;
594
595 if (!ip_checkentry(&e->ip)) {
596 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
597 return -EINVAL;
598 }
599
Patrick McHardy9c547952007-12-17 21:52:00 -0800600 if (e->target_offset + sizeof(struct ipt_entry_target) >
601 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800602 return -EINVAL;
603
604 t = ipt_get_target(e);
605 if (e->target_offset + t->u.target_size > e->next_offset)
606 return -EINVAL;
607
608 return 0;
609}
610
Denys Vlasenko022748a2008-01-14 23:44:05 -0800611static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200612check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
613 unsigned int *i)
Dmitry Mishina96be242006-12-12 00:29:26 -0800614{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200615 const struct ipt_ip *ip = par->entryinfo;
Dmitry Mishina96be242006-12-12 00:29:26 -0800616 int ret;
617
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200618 par->match = m->u.kernel.match;
619 par->matchinfo = m->data;
620
Jan Engelhardt916a9172008-10-08 11:35:20 +0200621 ret = xt_check_match(par, m->u.match_size - sizeof(*m),
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200622 ip->proto, ip->invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200623 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800624 duprintf("ip_tables: check failed for `%s'.\n",
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200625 par.match->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200626 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800627 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200628 ++*i;
629 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800630}
631
Denys Vlasenko022748a2008-01-14 23:44:05 -0800632static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200633find_check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
Patrick McHardy4b478242007-12-17 21:46:15 -0800634 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800636 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800637 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Harald Welte2e4e6a12006-01-12 13:30:04 -0800639 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy9c547952007-12-17 21:52:00 -0800640 m->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 "ipt_%s", m->u.user.name);
642 if (IS_ERR(match) || !match) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800643 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return match ? PTR_ERR(match) : -ENOENT;
645 }
646 m->u.kernel.match = match;
647
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200648 ret = check_match(m, par, i);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800649 if (ret)
650 goto err;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800653err:
654 module_put(m->u.kernel.match->me);
655 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Denys Vlasenko022748a2008-01-14 23:44:05 -0800658static int check_target(struct ipt_entry *e, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800659{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200660 struct ipt_entry_target *t = ipt_get_target(e);
661 struct xt_tgchk_param par = {
662 .table = name,
663 .entryinfo = e,
664 .target = t->u.kernel.target,
665 .targinfo = t->data,
666 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200667 .family = NFPROTO_IPV4,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200668 };
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900669 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800670
Jan Engelhardt916a9172008-10-08 11:35:20 +0200671 ret = xt_check_target(&par, t->u.target_size - sizeof(*t),
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200672 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200673 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800674 duprintf("ip_tables: check failed for `%s'.\n",
675 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200676 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800677 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200678 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800679}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Denys Vlasenko022748a2008-01-14 23:44:05 -0800681static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800682find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
Patrick McHardy4b478242007-12-17 21:46:15 -0800683 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800686 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int ret;
688 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200689 struct xt_mtchk_param mtpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Dmitry Mishina96be242006-12-12 00:29:26 -0800691 ret = check_entry(e, name);
692 if (ret)
693 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200696 mtpar.table = name;
697 mtpar.entryinfo = &e->ip;
698 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200699 mtpar.family = NFPROTO_IPV4;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200700 ret = IPT_MATCH_ITERATE(e, find_check_match, &mtpar, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (ret != 0)
702 goto cleanup_matches;
703
704 t = ipt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800705 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -0800706 t->u.user.name,
707 t->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 "ipt_%s", t->u.user.name);
709 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800710 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 ret = target ? PTR_ERR(target) : -ENOENT;
712 goto cleanup_matches;
713 }
714 t->u.kernel.target = target;
715
Dmitry Mishina96be242006-12-12 00:29:26 -0800716 ret = check_target(e, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800717 if (ret)
718 goto err;
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 (*i)++;
721 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800722 err:
723 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 cleanup_matches:
725 IPT_MATCH_ITERATE(e, cleanup_match, &j);
726 return ret;
727}
728
Denys Vlasenko022748a2008-01-14 23:44:05 -0800729static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800731 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 unsigned char *base,
733 unsigned char *limit,
734 const unsigned int *hook_entries,
735 const unsigned int *underflows,
736 unsigned int *i)
737{
738 unsigned int h;
739
740 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0
741 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
742 duprintf("Bad offset %p\n", e);
743 return -EINVAL;
744 }
745
746 if (e->next_offset
747 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
748 duprintf("checking: element %p size %u\n",
749 e, e->next_offset);
750 return -EINVAL;
751 }
752
753 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800754 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if ((unsigned char *)e - base == hook_entries[h])
756 newinfo->hook_entry[h] = hook_entries[h];
757 if ((unsigned char *)e - base == underflows[h])
758 newinfo->underflow[h] = underflows[h];
759 }
760
761 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900762 < 0 (not IPT_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800765 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 e->comefrom = 0;
767
768 (*i)++;
769 return 0;
770}
771
Denys Vlasenko022748a2008-01-14 23:44:05 -0800772static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773cleanup_entry(struct ipt_entry *e, unsigned int *i)
774{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200775 struct xt_tgdtor_param par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct ipt_entry_target *t;
777
778 if (i && (*i)-- == 0)
779 return 1;
780
781 /* Cleanup all matches */
782 IPT_MATCH_ITERATE(e, cleanup_match, NULL);
783 t = ipt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200784
785 par.target = t->u.kernel.target;
786 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200787 par.family = NFPROTO_IPV4;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200788 if (par.target->destroy != NULL)
789 par.target->destroy(&par);
790 module_put(par.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return 0;
792}
793
794/* Checks and translates the user-supplied table segment (held in
795 newinfo) */
796static int
797translate_table(const char *name,
798 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800799 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800800 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 unsigned int size,
802 unsigned int number,
803 const unsigned int *hook_entries,
804 const unsigned int *underflows)
805{
806 unsigned int i;
807 int ret;
808
809 newinfo->size = size;
810 newinfo->number = number;
811
812 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800813 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 newinfo->hook_entry[i] = 0xFFFFFFFF;
815 newinfo->underflow[i] = 0xFFFFFFFF;
816 }
817
818 duprintf("translate_table: size %u\n", newinfo->size);
819 i = 0;
820 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800821 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 check_entry_size_and_hooks,
823 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800824 entry0,
825 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 hook_entries, underflows, &i);
827 if (ret != 0)
828 return ret;
829
830 if (i != number) {
831 duprintf("translate_table: %u not %u entries\n",
832 i, number);
833 return -EINVAL;
834 }
835
836 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800837 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* Only hooks which are valid */
839 if (!(valid_hooks & (1 << i)))
840 continue;
841 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
842 duprintf("Invalid hook entry %u %u\n",
843 i, hook_entries[i]);
844 return -EINVAL;
845 }
846 if (newinfo->underflow[i] == 0xFFFFFFFF) {
847 duprintf("Invalid underflow %u %u\n",
848 i, underflows[i]);
849 return -EINVAL;
850 }
851 }
852
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800853 if (!mark_source_chains(newinfo, valid_hooks, entry0))
854 return -ELOOP;
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* Finally, each sanity check must pass */
857 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800858 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Dmitry Mishina96be242006-12-12 00:29:26 -0800859 find_check_entry, name, size, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800861 if (ret != 0) {
862 IPT_ENTRY_ITERATE(entry0, newinfo->size,
863 cleanup_entry, &i);
864 return ret;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700868 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800869 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
870 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
873 return ret;
874}
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876/* Gets counters. */
877static inline int
878add_entry_to_counter(const struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800879 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 unsigned int *i)
881{
882 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
883
884 (*i)++;
885 return 0;
886}
887
Eric Dumazet31836062005-12-13 23:13:48 -0800888static inline int
889set_entry_to_counter(const struct ipt_entry *e,
890 struct ipt_counters total[],
891 unsigned int *i)
892{
893 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
894
895 (*i)++;
896 return 0;
897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800900get_counters(const struct xt_table_info *t,
901 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 unsigned int cpu;
904 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800905 unsigned int curcpu;
906
907 /* Instead of clearing (by a previous call to memset())
908 * the counters and using adds, we set the counters
909 * with data used by 'current' CPU
910 * We dont care about preemption here.
911 */
912 curcpu = raw_smp_processor_id();
913
914 i = 0;
915 IPT_ENTRY_ITERATE(t->entries[curcpu],
916 t->size,
917 set_entry_to_counter,
918 counters,
919 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700921 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800922 if (cpu == curcpu)
923 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800925 IPT_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 t->size,
927 add_entry_to_counter,
928 counters,
929 &i);
930 }
931}
932
Denys Vlasenko022748a2008-01-14 23:44:05 -0800933static struct xt_counters * alloc_counters(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Dmitry Mishin27229712006-04-01 02:25:19 -0800935 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800936 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200937 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /* We need atomic snapshot of counters: rest doesn't change
940 (other than comefrom, which userspace doesn't care
941 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800942 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet31836062005-12-13 23:13:48 -0800943 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (counters == NULL)
Dmitry Mishin27229712006-04-01 02:25:19 -0800946 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 write_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800950 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 write_unlock_bh(&table->lock);
952
Dmitry Mishin27229712006-04-01 02:25:19 -0800953 return counters;
954}
955
956static int
957copy_entries_to_user(unsigned int total_size,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800958 struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800959 void __user *userptr)
960{
961 unsigned int off, num;
962 struct ipt_entry *e;
963 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200964 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800965 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200966 const void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -0800967
968 counters = alloc_counters(table);
969 if (IS_ERR(counters))
970 return PTR_ERR(counters);
971
Eric Dumazet31836062005-12-13 23:13:48 -0800972 /* choose the copy that is on our node/cpu, ...
973 * This choice is lazy (because current thread is
974 * allowed to migrate to another cpu)
975 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800976 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800977 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 ret = -EFAULT;
979 goto free_counters;
980 }
981
982 /* FIXME: use iterator macros --RR */
983 /* ... then go back and fix counters and names */
984 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
985 unsigned int i;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200986 const struct ipt_entry_match *m;
987 const struct ipt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Eric Dumazet31836062005-12-13 23:13:48 -0800989 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (copy_to_user(userptr + off
991 + offsetof(struct ipt_entry, counters),
992 &counters[num],
993 sizeof(counters[num])) != 0) {
994 ret = -EFAULT;
995 goto free_counters;
996 }
997
998 for (i = sizeof(struct ipt_entry);
999 i < e->target_offset;
1000 i += m->u.match_size) {
1001 m = (void *)e + i;
1002
1003 if (copy_to_user(userptr + off + i
1004 + offsetof(struct ipt_entry_match,
1005 u.user.name),
1006 m->u.kernel.match->name,
1007 strlen(m->u.kernel.match->name)+1)
1008 != 0) {
1009 ret = -EFAULT;
1010 goto free_counters;
1011 }
1012 }
1013
1014 t = ipt_get_target(e);
1015 if (copy_to_user(userptr + off + e->target_offset
1016 + offsetof(struct ipt_entry_target,
1017 u.user.name),
1018 t->u.kernel.target->name,
1019 strlen(t->u.kernel.target->name)+1) != 0) {
1020 ret = -EFAULT;
1021 goto free_counters;
1022 }
1023 }
1024
1025 free_counters:
1026 vfree(counters);
1027 return ret;
1028}
1029
Dmitry Mishin27229712006-04-01 02:25:19 -08001030#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001031static void compat_standard_from_user(void *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001032{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001033 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001034
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001035 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001036 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001037 memcpy(dst, &v, sizeof(v));
1038}
1039
1040static int compat_standard_to_user(void __user *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001041{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001042 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001043
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001044 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001045 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001046 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001047}
1048
1049static inline int
Patrick McHardy4b478242007-12-17 21:46:15 -08001050compat_calc_match(struct ipt_entry_match *m, int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001051{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001052 *size += xt_compat_match_offset(m->u.kernel.match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001053 return 0;
1054}
1055
Eric Dumazet259d4e42007-12-04 23:24:56 -08001056static int compat_calc_entry(struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001057 const struct xt_table_info *info,
1058 void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001059{
1060 struct ipt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001061 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001062 int off, i, ret;
1063
Patrick McHardy30c08c42007-12-17 21:47:14 -08001064 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001065 entry_offset = (void *)e - base;
1066 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
1067 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001068 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001069 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001070 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001071 if (ret)
1072 return ret;
1073
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001074 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001075 if (info->hook_entry[i] &&
1076 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001077 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001078 if (info->underflow[i] &&
1079 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001080 newinfo->underflow[i] -= off;
1081 }
1082 return 0;
1083}
1084
Eric Dumazet259d4e42007-12-04 23:24:56 -08001085static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001086 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001087{
1088 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001089
1090 if (!newinfo || !info)
1091 return -EINVAL;
1092
Eric Dumazet259d4e42007-12-04 23:24:56 -08001093 /* we dont care about newinfo->entries[] */
1094 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1095 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001096 loc_cpu_entry = info->entries[raw_smp_processor_id()];
1097 return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001098 compat_calc_entry, info, loc_cpu_entry,
1099 newinfo);
Dmitry Mishin27229712006-04-01 02:25:19 -08001100}
1101#endif
1102
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001103static int get_info(struct net *net, void __user *user, int *len, int compat)
Dmitry Mishin27229712006-04-01 02:25:19 -08001104{
1105 char name[IPT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001106 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001107 int ret;
1108
1109 if (*len != sizeof(struct ipt_getinfo)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001110 duprintf("length %u != %zu\n", *len,
1111 sizeof(struct ipt_getinfo));
Dmitry Mishin27229712006-04-01 02:25:19 -08001112 return -EINVAL;
1113 }
1114
1115 if (copy_from_user(name, user, sizeof(name)) != 0)
1116 return -EFAULT;
1117
1118 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1119#ifdef CONFIG_COMPAT
1120 if (compat)
1121 xt_compat_lock(AF_INET);
1122#endif
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001123 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001124 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001125 if (t && !IS_ERR(t)) {
1126 struct ipt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001127 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001128
1129#ifdef CONFIG_COMPAT
1130 if (compat) {
1131 struct xt_table_info tmp;
1132 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001133 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001134 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001135 }
1136#endif
1137 info.valid_hooks = t->valid_hooks;
1138 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001139 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001140 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001141 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001142 info.num_entries = private->number;
1143 info.size = private->size;
1144 strcpy(info.name, name);
1145
1146 if (copy_to_user(user, &info, *len) != 0)
1147 ret = -EFAULT;
1148 else
1149 ret = 0;
1150
1151 xt_table_unlock(t);
1152 module_put(t->me);
1153 } else
1154 ret = t ? PTR_ERR(t) : -ENOENT;
1155#ifdef CONFIG_COMPAT
1156 if (compat)
1157 xt_compat_unlock(AF_INET);
1158#endif
1159 return ret;
1160}
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001163get_entries(struct net *net, struct ipt_get_entries __user *uptr, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001166 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001167 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Dmitry Mishin27229712006-04-01 02:25:19 -08001169 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001170 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001171 return -EINVAL;
1172 }
1173 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1174 return -EFAULT;
1175 if (*len != sizeof(struct ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001176 duprintf("get_entries: %u != %zu\n",
1177 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001178 return -EINVAL;
1179 }
1180
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001181 t = xt_find_table_lock(net, AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001183 const struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001184 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001185 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001186 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 t, uptr->entrytable);
1188 else {
1189 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001190 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001191 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001194 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 } else
1196 ret = t ? PTR_ERR(t) : -ENOENT;
1197
1198 return ret;
1199}
1200
1201static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001202__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001203 struct xt_table_info *newinfo, unsigned int num_counters,
1204 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001205{
1206 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001207 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001208 struct xt_table_info *oldinfo;
1209 struct xt_counters *counters;
1210 void *loc_cpu_old_entry;
1211
1212 ret = 0;
1213 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1214 if (!counters) {
1215 ret = -ENOMEM;
1216 goto out;
1217 }
1218
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001219 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Dmitry Mishin27229712006-04-01 02:25:19 -08001220 "iptable_%s", name);
1221 if (!t || IS_ERR(t)) {
1222 ret = t ? PTR_ERR(t) : -ENOENT;
1223 goto free_newinfo_counters_untrans;
1224 }
1225
1226 /* You lied! */
1227 if (valid_hooks != t->valid_hooks) {
1228 duprintf("Valid hook crap: %08X vs %08X\n",
1229 valid_hooks, t->valid_hooks);
1230 ret = -EINVAL;
1231 goto put_module;
1232 }
1233
1234 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1235 if (!oldinfo)
1236 goto put_module;
1237
1238 /* Update module usage count based on number of rules */
1239 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1240 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1241 if ((oldinfo->number > oldinfo->initial_entries) ||
1242 (newinfo->number <= oldinfo->initial_entries))
1243 module_put(t->me);
1244 if ((oldinfo->number > oldinfo->initial_entries) &&
1245 (newinfo->number <= oldinfo->initial_entries))
1246 module_put(t->me);
1247
1248 /* Get the old counters. */
1249 get_counters(oldinfo, counters);
1250 /* Decrease module usage counts and free resource */
1251 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Patrick McHardy4b478242007-12-17 21:46:15 -08001252 IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1253 NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001254 xt_free_table_info(oldinfo);
1255 if (copy_to_user(counters_ptr, counters,
1256 sizeof(struct xt_counters) * num_counters) != 0)
1257 ret = -EFAULT;
1258 vfree(counters);
1259 xt_table_unlock(t);
1260 return ret;
1261
1262 put_module:
1263 module_put(t->me);
1264 xt_table_unlock(t);
1265 free_newinfo_counters_untrans:
1266 vfree(counters);
1267 out:
1268 return ret;
1269}
1270
1271static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001272do_replace(struct net *net, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 int ret;
1275 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001276 struct xt_table_info *newinfo;
1277 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1280 return -EFAULT;
1281
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001282 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001283 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1284 return -ENOMEM;
1285
Harald Welte2e4e6a12006-01-12 13:30:04 -08001286 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (!newinfo)
1288 return -ENOMEM;
1289
Patrick McHardy9c547952007-12-17 21:52:00 -08001290 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001291 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1292 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 tmp.size) != 0) {
1294 ret = -EFAULT;
1295 goto free_newinfo;
1296 }
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001299 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 tmp.hook_entry, tmp.underflow);
1301 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001302 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 duprintf("ip_tables: Translated table\n");
1305
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001306 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001307 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001308 if (ret)
1309 goto free_newinfo_untrans;
1310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Dmitry Mishin27229712006-04-01 02:25:19 -08001312 free_newinfo_untrans:
Patrick McHardy9c547952007-12-17 21:52:00 -08001313 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001315 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return ret;
1317}
1318
1319/* We're lazy, and add to the first CPU; overflow works its fey magic
1320 * and everything is OK. */
Denys Vlasenko022748a2008-01-14 23:44:05 -08001321static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322add_counter_to_entry(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001323 const struct xt_counters addme[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 unsigned int *i)
1325{
1326#if 0
1327 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1328 *i,
1329 (long unsigned int)e->counters.pcnt,
1330 (long unsigned int)e->counters.bcnt,
1331 (long unsigned int)addme[*i].pcnt,
1332 (long unsigned int)addme[*i].bcnt);
1333#endif
1334
1335 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1336
1337 (*i)++;
1338 return 0;
1339}
1340
1341static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001342do_add_counters(struct net *net, void __user *user, unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 unsigned int i;
Dmitry Mishin27229712006-04-01 02:25:19 -08001345 struct xt_counters_info tmp;
1346 struct xt_counters *paddc;
1347 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001348 const char *name;
Dmitry Mishin27229712006-04-01 02:25:19 -08001349 int size;
1350 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001351 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001352 const struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001354 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001355#ifdef CONFIG_COMPAT
1356 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Dmitry Mishin27229712006-04-01 02:25:19 -08001358 if (compat) {
1359 ptmp = &compat_tmp;
1360 size = sizeof(struct compat_xt_counters_info);
1361 } else
1362#endif
1363 {
1364 ptmp = &tmp;
1365 size = sizeof(struct xt_counters_info);
1366 }
1367
1368 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EFAULT;
1370
Dmitry Mishin27229712006-04-01 02:25:19 -08001371#ifdef CONFIG_COMPAT
1372 if (compat) {
1373 num_counters = compat_tmp.num_counters;
1374 name = compat_tmp.name;
1375 } else
1376#endif
1377 {
1378 num_counters = tmp.num_counters;
1379 name = tmp.name;
1380 }
1381
1382 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return -EINVAL;
1384
Dmitry Mishin27229712006-04-01 02:25:19 -08001385 paddc = vmalloc_node(len - size, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (!paddc)
1387 return -ENOMEM;
1388
Dmitry Mishin27229712006-04-01 02:25:19 -08001389 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 ret = -EFAULT;
1391 goto free;
1392 }
1393
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001394 t = xt_find_table_lock(net, AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (!t || IS_ERR(t)) {
1396 ret = t ? PTR_ERR(t) : -ENOENT;
1397 goto free;
1398 }
1399
1400 write_lock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001401 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001402 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 ret = -EINVAL;
1404 goto unlock_up_free;
1405 }
1406
1407 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001408 /* Choose the copy that is on our node */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001409 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -08001410 IPT_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001411 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 add_counter_to_entry,
Dmitry Mishin27229712006-04-01 02:25:19 -08001413 paddc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 &i);
1415 unlock_up_free:
1416 write_unlock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001417 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 module_put(t->me);
1419 free:
1420 vfree(paddc);
1421
1422 return ret;
1423}
1424
Dmitry Mishin27229712006-04-01 02:25:19 -08001425#ifdef CONFIG_COMPAT
1426struct compat_ipt_replace {
1427 char name[IPT_TABLE_MAXNAMELEN];
1428 u32 valid_hooks;
1429 u32 num_entries;
1430 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001431 u32 hook_entry[NF_INET_NUMHOOKS];
1432 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001433 u32 num_counters;
1434 compat_uptr_t counters; /* struct ipt_counters * */
1435 struct compat_ipt_entry entries[0];
1436};
1437
Patrick McHardya18aa312007-12-12 10:35:16 -08001438static int
1439compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001440 unsigned int *size, struct xt_counters *counters,
Patrick McHardya18aa312007-12-12 10:35:16 -08001441 unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001442{
Al Viro3e597c62006-09-24 23:42:20 +01001443 struct ipt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001444 struct compat_ipt_entry __user *ce;
1445 u_int16_t target_offset, next_offset;
1446 compat_uint_t origsize;
1447 int ret;
1448
1449 ret = -EFAULT;
1450 origsize = *size;
1451 ce = (struct compat_ipt_entry __user *)*dstptr;
Patrick McHardy78000072006-05-03 23:20:27 -07001452 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
Dmitry Mishin27229712006-04-01 02:25:19 -08001453 goto out;
1454
Patrick McHardya18aa312007-12-12 10:35:16 -08001455 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1456 goto out;
1457
Dmitry Mishin27229712006-04-01 02:25:19 -08001458 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001459 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1460
Patrick McHardyac8e27f2007-12-17 21:45:52 -08001461 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001462 target_offset = e->target_offset - (origsize - *size);
1463 if (ret)
1464 goto out;
1465 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001466 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001467 if (ret)
1468 goto out;
1469 ret = -EFAULT;
1470 next_offset = e->next_offset - (origsize - *size);
Patrick McHardy78000072006-05-03 23:20:27 -07001471 if (put_user(target_offset, &ce->target_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001472 goto out;
Patrick McHardy78000072006-05-03 23:20:27 -07001473 if (put_user(next_offset, &ce->next_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001474 goto out;
Patrick McHardya18aa312007-12-12 10:35:16 -08001475
1476 (*i)++;
Dmitry Mishin27229712006-04-01 02:25:19 -08001477 return 0;
1478out:
1479 return ret;
1480}
1481
Denys Vlasenko022748a2008-01-14 23:44:05 -08001482static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001483compat_find_calc_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001484 const char *name,
1485 const struct ipt_ip *ip,
1486 unsigned int hookmask,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001487 int *size, unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001488{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001489 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001490
1491 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001492 m->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001493 "ipt_%s", m->u.user.name);
1494 if (IS_ERR(match) || !match) {
1495 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001496 m->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001497 return match ? PTR_ERR(match) : -ENOENT;
1498 }
1499 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001500 *size += xt_compat_match_offset(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001501
1502 (*i)++;
1503 return 0;
1504}
1505
Denys Vlasenko022748a2008-01-14 23:44:05 -08001506static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001507compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1508{
1509 if (i && (*i)-- == 0)
1510 return 1;
1511
1512 module_put(m->u.kernel.match->me);
1513 return 0;
1514}
1515
Denys Vlasenko022748a2008-01-14 23:44:05 -08001516static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001517compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001518{
1519 struct ipt_entry_target *t;
1520
1521 if (i && (*i)-- == 0)
1522 return 1;
1523
1524 /* Cleanup all matches */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001525 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1526 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001527 module_put(t->u.kernel.target->me);
1528 return 0;
1529}
1530
Denys Vlasenko022748a2008-01-14 23:44:05 -08001531static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001532check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001533 struct xt_table_info *newinfo,
1534 unsigned int *size,
1535 unsigned char *base,
1536 unsigned char *limit,
1537 unsigned int *hook_entries,
1538 unsigned int *underflows,
1539 unsigned int *i,
1540 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001541{
1542 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001543 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001544 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001545 unsigned int j;
1546 int ret, off, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001547
1548 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1549 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0
1550 || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1551 duprintf("Bad offset %p, limit = %p\n", e, limit);
1552 return -EINVAL;
1553 }
1554
1555 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001556 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001557 duprintf("checking: element %p size %u\n",
1558 e, e->next_offset);
1559 return -EINVAL;
1560 }
1561
Patrick McHardy73cd5982007-12-17 21:47:32 -08001562 /* For purposes of check_entry casting the compat entry is fine */
1563 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001564 if (ret)
1565 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001566
Patrick McHardy30c08c42007-12-17 21:47:14 -08001567 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001568 entry_offset = (void *)e - (void *)base;
1569 j = 0;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001570 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1571 &e->ip, e->comefrom, &off, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001572 if (ret != 0)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001573 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001574
Patrick McHardy73cd5982007-12-17 21:47:32 -08001575 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001576 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -08001577 t->u.user.name,
1578 t->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001579 "ipt_%s", t->u.user.name);
1580 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001581 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001582 t->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001583 ret = target ? PTR_ERR(target) : -ENOENT;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001584 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001585 }
1586 t->u.kernel.target = target;
1587
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001588 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001589 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001590 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001591 if (ret)
1592 goto out;
1593
1594 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001595 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001596 if ((unsigned char *)e - base == hook_entries[h])
1597 newinfo->hook_entry[h] = hook_entries[h];
1598 if ((unsigned char *)e - base == underflows[h])
1599 newinfo->underflow[h] = underflows[h];
1600 }
1601
1602 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001603 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001604 e->comefrom = 0;
1605
1606 (*i)++;
1607 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001608
Dmitry Mishin27229712006-04-01 02:25:19 -08001609out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001610 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001611release_matches:
1612 IPT_MATCH_ITERATE(e, compat_release_match, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001613 return ret;
1614}
1615
Patrick McHardy4b478242007-12-17 21:46:15 -08001616static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001617compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001618 unsigned int *size, const char *name,
1619 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001620{
1621 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001622 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001623 struct ipt_entry *de;
1624 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001625 int ret, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001626
1627 ret = 0;
1628 origsize = *size;
1629 de = (struct ipt_entry *)*dstptr;
1630 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001631 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001632
Patrick McHardy73cd5982007-12-17 21:47:32 -08001633 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001634 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1635
Patrick McHardy73cd5982007-12-17 21:47:32 -08001636 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1637 dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001638 if (ret)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001639 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001640 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001641 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001642 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001643 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001644
1645 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001646 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001647 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1648 newinfo->hook_entry[h] -= origsize - *size;
1649 if ((unsigned char *)de - base < newinfo->underflow[h])
1650 newinfo->underflow[h] -= origsize - *size;
1651 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001652 return ret;
1653}
Dmitry Mishin27229712006-04-01 02:25:19 -08001654
Denys Vlasenko022748a2008-01-14 23:44:05 -08001655static int
1656compat_check_entry(struct ipt_entry *e, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001657 unsigned int *i)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001658{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001659 struct xt_mtchk_param mtpar;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001660 unsigned int j;
1661 int ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001662
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001663 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001664 mtpar.table = name;
1665 mtpar.entryinfo = &e->ip;
1666 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +02001667 mtpar.family = NFPROTO_IPV4;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001668 ret = IPT_MATCH_ITERATE(e, check_match, &mtpar, &j);
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001669 if (ret)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001670 goto cleanup_matches;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001671
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001672 ret = check_target(e, name);
1673 if (ret)
1674 goto cleanup_matches;
1675
1676 (*i)++;
1677 return 0;
1678
1679 cleanup_matches:
1680 IPT_MATCH_ITERATE(e, cleanup_match, &j);
1681 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001682}
1683
Dmitry Mishin27229712006-04-01 02:25:19 -08001684static int
1685translate_compat_table(const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001686 unsigned int valid_hooks,
1687 struct xt_table_info **pinfo,
1688 void **pentry0,
1689 unsigned int total_size,
1690 unsigned int number,
1691 unsigned int *hook_entries,
1692 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001693{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001694 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001695 struct xt_table_info *newinfo, *info;
1696 void *pos, *entry0, *entry1;
1697 unsigned int size;
1698 int ret;
1699
1700 info = *pinfo;
1701 entry0 = *pentry0;
1702 size = total_size;
1703 info->number = number;
1704
1705 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001706 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001707 info->hook_entry[i] = 0xFFFFFFFF;
1708 info->underflow[i] = 0xFFFFFFFF;
1709 }
1710
1711 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001712 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001713 xt_compat_lock(AF_INET);
1714 /* Walk through entries, checking offsets. */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001715 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1716 check_compat_entry_size_and_hooks,
1717 info, &size, entry0,
1718 entry0 + total_size,
1719 hook_entries, underflows, &j, name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001720 if (ret != 0)
1721 goto out_unlock;
1722
1723 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001724 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001725 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001726 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001727 goto out_unlock;
1728 }
1729
1730 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001731 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001732 /* Only hooks which are valid */
1733 if (!(valid_hooks & (1 << i)))
1734 continue;
1735 if (info->hook_entry[i] == 0xFFFFFFFF) {
1736 duprintf("Invalid hook entry %u %u\n",
1737 i, hook_entries[i]);
1738 goto out_unlock;
1739 }
1740 if (info->underflow[i] == 0xFFFFFFFF) {
1741 duprintf("Invalid underflow %u %u\n",
1742 i, underflows[i]);
1743 goto out_unlock;
1744 }
1745 }
1746
1747 ret = -ENOMEM;
1748 newinfo = xt_alloc_table_info(size);
1749 if (!newinfo)
1750 goto out_unlock;
1751
1752 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001753 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001754 newinfo->hook_entry[i] = info->hook_entry[i];
1755 newinfo->underflow[i] = info->underflow[i];
1756 }
1757 entry1 = newinfo->entries[raw_smp_processor_id()];
1758 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001759 size = total_size;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001760 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
Patrick McHardy9c547952007-12-17 21:52:00 -08001761 compat_copy_entry_from_user,
1762 &pos, &size, name, newinfo, entry1);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001763 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001764 xt_compat_unlock(AF_INET);
1765 if (ret)
1766 goto free_newinfo;
1767
1768 ret = -ELOOP;
1769 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1770 goto free_newinfo;
1771
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001772 i = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001773 ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001774 name, &i);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001775 if (ret) {
1776 j -= i;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001777 COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1778 compat_release_entry, &j);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001779 IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1780 xt_free_table_info(newinfo);
1781 return ret;
1782 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001783
Dmitry Mishin27229712006-04-01 02:25:19 -08001784 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001785 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001786 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1787 memcpy(newinfo->entries[i], entry1, newinfo->size);
1788
1789 *pinfo = newinfo;
1790 *pentry0 = entry1;
1791 xt_free_table_info(info);
1792 return 0;
1793
1794free_newinfo:
1795 xt_free_table_info(newinfo);
1796out:
Patrick McHardy73cd5982007-12-17 21:47:32 -08001797 COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001798 return ret;
1799out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001800 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001801 xt_compat_unlock(AF_INET);
1802 goto out;
1803}
1804
1805static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001806compat_do_replace(struct net *net, void __user *user, unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001807{
1808 int ret;
1809 struct compat_ipt_replace tmp;
1810 struct xt_table_info *newinfo;
1811 void *loc_cpu_entry;
1812
1813 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1814 return -EFAULT;
1815
Dmitry Mishin27229712006-04-01 02:25:19 -08001816 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001817 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001818 return -ENOMEM;
1819 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1820 return -ENOMEM;
1821
1822 newinfo = xt_alloc_table_info(tmp.size);
1823 if (!newinfo)
1824 return -ENOMEM;
1825
Patrick McHardy9c547952007-12-17 21:52:00 -08001826 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001827 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1828 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1829 tmp.size) != 0) {
1830 ret = -EFAULT;
1831 goto free_newinfo;
1832 }
1833
1834 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001835 &newinfo, &loc_cpu_entry, tmp.size,
1836 tmp.num_entries, tmp.hook_entry,
1837 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001838 if (ret != 0)
1839 goto free_newinfo;
1840
1841 duprintf("compat_do_replace: Translated table\n");
1842
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001843 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001844 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001845 if (ret)
1846 goto free_newinfo_untrans;
1847 return 0;
1848
1849 free_newinfo_untrans:
Patrick McHardy4b478242007-12-17 21:46:15 -08001850 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001851 free_newinfo:
1852 xt_free_table_info(newinfo);
1853 return ret;
1854}
1855
1856static int
1857compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001858 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001859{
1860 int ret;
1861
1862 if (!capable(CAP_NET_ADMIN))
1863 return -EPERM;
1864
1865 switch (cmd) {
1866 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001867 ret = compat_do_replace(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001868 break;
1869
1870 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001871 ret = do_add_counters(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001872 break;
1873
1874 default:
1875 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1876 ret = -EINVAL;
1877 }
1878
1879 return ret;
1880}
1881
Patrick McHardy4b478242007-12-17 21:46:15 -08001882struct compat_ipt_get_entries {
Dmitry Mishin27229712006-04-01 02:25:19 -08001883 char name[IPT_TABLE_MAXNAMELEN];
1884 compat_uint_t size;
1885 struct compat_ipt_entry entrytable[0];
1886};
1887
Patrick McHardy4b478242007-12-17 21:46:15 -08001888static int
1889compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1890 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001891{
Dmitry Mishin27229712006-04-01 02:25:19 -08001892 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001893 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001894 void __user *pos;
1895 unsigned int size;
1896 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001897 const void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001898 unsigned int i = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001899
1900 counters = alloc_counters(table);
1901 if (IS_ERR(counters))
1902 return PTR_ERR(counters);
1903
1904 /* choose the copy that is on our node/cpu, ...
1905 * This choice is lazy (because current thread is
1906 * allowed to migrate to another cpu)
1907 */
1908 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1909 pos = userptr;
1910 size = total_size;
1911 ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
Patrick McHardya18aa312007-12-12 10:35:16 -08001912 compat_copy_entry_to_user,
1913 &pos, &size, counters, &i);
Dmitry Mishin27229712006-04-01 02:25:19 -08001914
Dmitry Mishin27229712006-04-01 02:25:19 -08001915 vfree(counters);
1916 return ret;
1917}
1918
1919static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001920compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1921 int *len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001922{
1923 int ret;
1924 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001925 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001926
Dmitry Mishin27229712006-04-01 02:25:19 -08001927 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001928 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001929 return -EINVAL;
1930 }
1931
1932 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1933 return -EFAULT;
1934
1935 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001936 duprintf("compat_get_entries: %u != %zu\n",
1937 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001938 return -EINVAL;
1939 }
1940
1941 xt_compat_lock(AF_INET);
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001942 t = xt_find_table_lock(net, AF_INET, get.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001943 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001944 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001945 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08001946 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001947 ret = compat_table_info(private, &info);
1948 if (!ret && get.size == info.size) {
1949 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001950 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001951 } else if (!ret) {
1952 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001953 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001954 ret = -EAGAIN;
Dmitry Mishin27229712006-04-01 02:25:19 -08001955 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001956 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001957 module_put(t->me);
1958 xt_table_unlock(t);
1959 } else
1960 ret = t ? PTR_ERR(t) : -ENOENT;
1961
1962 xt_compat_unlock(AF_INET);
1963 return ret;
1964}
1965
Patrick McHardy79030ed2006-09-20 12:05:08 -07001966static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1967
Dmitry Mishin27229712006-04-01 02:25:19 -08001968static int
1969compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1970{
1971 int ret;
1972
Björn Steinbrink82fac052006-10-20 00:21:10 -07001973 if (!capable(CAP_NET_ADMIN))
1974 return -EPERM;
1975
Dmitry Mishin27229712006-04-01 02:25:19 -08001976 switch (cmd) {
1977 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001978 ret = get_info(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001979 break;
1980 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001981 ret = compat_get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001982 break;
1983 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001984 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001985 }
1986 return ret;
1987}
1988#endif
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001991do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 int ret;
1994
1995 if (!capable(CAP_NET_ADMIN))
1996 return -EPERM;
1997
1998 switch (cmd) {
1999 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002000 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 break;
2002
2003 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002004 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 break;
2006
2007 default:
2008 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
2009 ret = -EINVAL;
2010 }
2011
2012 return ret;
2013}
2014
2015static int
2016do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2017{
2018 int ret;
2019
2020 if (!capable(CAP_NET_ADMIN))
2021 return -EPERM;
2022
2023 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002024 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002025 ret = get_info(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002027
2028 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002029 ret = get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08002030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 case IPT_SO_GET_REVISION_MATCH:
2033 case IPT_SO_GET_REVISION_TARGET: {
2034 struct ipt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002035 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 if (*len != sizeof(rev)) {
2038 ret = -EINVAL;
2039 break;
2040 }
2041 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2042 ret = -EFAULT;
2043 break;
2044 }
2045
2046 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002047 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002049 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Harald Welte2e4e6a12006-01-12 13:30:04 -08002051 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2052 rev.revision,
2053 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 "ipt_%s", rev.name);
2055 break;
2056 }
2057
2058 default:
2059 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2060 ret = -EINVAL;
2061 }
2062
2063 return ret;
2064}
2065
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002066struct xt_table *ipt_register_table(struct net *net, struct xt_table *table,
2067 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002070 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08002071 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08002073 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08002074 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Harald Welte2e4e6a12006-01-12 13:30:04 -08002076 newinfo = xt_alloc_table_info(repl->size);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002077 if (!newinfo) {
2078 ret = -ENOMEM;
2079 goto out;
2080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Patrick McHardy9c547952007-12-17 21:52:00 -08002082 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002083 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2084 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08002087 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 repl->num_entries,
2089 repl->hook_entry,
2090 repl->underflow);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002091 if (ret != 0)
2092 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002094 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08002095 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002096 ret = PTR_ERR(new_table);
2097 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002100 return new_table;
2101
2102out_free:
2103 xt_free_table_info(newinfo);
2104out:
2105 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
Jan Engelhardte60a13e2007-02-07 15:12:33 -08002108void ipt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002110 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002111 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08002112 struct module *table_owner = table->me;
Eric Dumazet31836062005-12-13 23:13:48 -08002113
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002114 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002117 loc_cpu_entry = private->entries[raw_smp_processor_id()];
2118 IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
Alexey Dobriyandf200962008-01-31 04:05:34 -08002119 if (private->number > private->initial_entries)
2120 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002121 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
2124/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002125static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2127 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002128 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Patrick McHardy9c547952007-12-17 21:52:00 -08002130 return ((test_type == 0xFF) ||
2131 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 ^ invert;
2133}
2134
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002135static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002136icmp_match(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Jan Engelhardt5452e422008-04-14 11:15:35 +02002138 const struct icmphdr *ic;
2139 struct icmphdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002140 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002143 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002144 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002146 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (ic == NULL) {
2148 /* We've been asked to examine this packet, and we
2149 * can't. Hence, no choice but to drop.
2150 */
2151 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002152 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002153 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155
2156 return icmp_type_code_match(icmpinfo->type,
2157 icmpinfo->code[0],
2158 icmpinfo->code[1],
2159 ic->type, ic->code,
2160 !!(icmpinfo->invflags&IPT_ICMP_INV));
2161}
2162
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002163static bool icmp_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002165 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002167 /* Must specify no unknown invflags */
2168 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
2171/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07002172static struct xt_target ipt_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 .name = IPT_STANDARD_TARGET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002174 .targetsize = sizeof(int),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002175 .family = AF_INET,
Dmitry Mishin27229712006-04-01 02:25:19 -08002176#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07002177 .compatsize = sizeof(compat_int_t),
2178 .compat_from_user = compat_standard_from_user,
2179 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181};
2182
Patrick McHardy9f15c532007-07-07 22:22:02 -07002183static struct xt_target ipt_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 .name = IPT_ERROR_TARGET,
2185 .target = ipt_error,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002186 .targetsize = IPT_FUNCTION_MAXNAMELEN,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002187 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188};
2189
2190static struct nf_sockopt_ops ipt_sockopts = {
2191 .pf = PF_INET,
2192 .set_optmin = IPT_BASE_CTL,
2193 .set_optmax = IPT_SO_SET_MAX+1,
2194 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002195#ifdef CONFIG_COMPAT
2196 .compat_set = compat_do_ipt_set_ctl,
2197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 .get_optmin = IPT_BASE_CTL,
2199 .get_optmax = IPT_SO_GET_MAX+1,
2200 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002201#ifdef CONFIG_COMPAT
2202 .compat_get = compat_do_ipt_get_ctl,
2203#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002204 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205};
2206
Patrick McHardy9f15c532007-07-07 22:22:02 -07002207static struct xt_match icmp_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 .name = "icmp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002209 .match = icmp_match,
2210 .matchsize = sizeof(struct ipt_icmp),
Patrick McHardy9c547952007-12-17 21:52:00 -08002211 .checkentry = icmp_checkentry,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002212 .proto = IPPROTO_ICMP,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002213 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214};
2215
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002216static int __net_init ip_tables_net_init(struct net *net)
2217{
2218 return xt_proto_init(net, AF_INET);
2219}
2220
2221static void __net_exit ip_tables_net_exit(struct net *net)
2222{
2223 xt_proto_fini(net, AF_INET);
2224}
2225
2226static struct pernet_operations ip_tables_net_ops = {
2227 .init = ip_tables_net_init,
2228 .exit = ip_tables_net_exit,
2229};
2230
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002231static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
2233 int ret;
2234
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002235 ret = register_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002236 if (ret < 0)
2237 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002240 ret = xt_register_target(&ipt_standard_target);
2241 if (ret < 0)
2242 goto err2;
2243 ret = xt_register_target(&ipt_error_target);
2244 if (ret < 0)
2245 goto err3;
2246 ret = xt_register_match(&icmp_matchstruct);
2247 if (ret < 0)
2248 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 /* Register setsockopt */
2251 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002252 if (ret < 0)
2253 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Dan Aloni0236e662007-07-09 13:20:12 -07002255 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002257
2258err5:
2259 xt_unregister_match(&icmp_matchstruct);
2260err4:
2261 xt_unregister_target(&ipt_error_target);
2262err3:
2263 xt_unregister_target(&ipt_standard_target);
2264err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002265 unregister_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002266err1:
2267 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002270static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002273
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002274 xt_unregister_match(&icmp_matchstruct);
2275 xt_unregister_target(&ipt_error_target);
2276 xt_unregister_target(&ipt_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002277
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002278 unregister_pernet_subsys(&ip_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
2281EXPORT_SYMBOL(ipt_register_table);
2282EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002284module_init(ip_tables_init);
2285module_exit(ip_tables_fini);