blob: 50b9a6c34c38038cbd4142ba8c3f33afcfd2d327 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Packet matching code.
3 *
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
Harald Welte2e4e6a12006-01-12 13:30:04 -08005 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/cache.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/skbuff.h>
14#include <linux/kmod.h>
15#include <linux/vmalloc.h>
16#include <linux/netdevice.h>
17#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/icmp.h>
19#include <net/ip.h>
Dmitry Mishin27229712006-04-01 02:25:19 -080020#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -080022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/proc_fs.h>
24#include <linux/err.h>
David S. Millerc8923c62005-10-13 14:41:23 -070025#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Harald Welte2e4e6a12006-01-12 13:30:04 -080027#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/netfilter_ipv4/ip_tables.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080029#include <net/netfilter/nf_log.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
33MODULE_DESCRIPTION("IPv4 packet filter");
34
35/*#define DEBUG_IP_FIREWALL*/
36/*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging */
37/*#define DEBUG_IP_FIREWALL_USER*/
38
39#ifdef DEBUG_IP_FIREWALL
40#define dprintf(format, args...) printk(format , ## args)
41#else
42#define dprintf(format, args...)
43#endif
44
45#ifdef DEBUG_IP_FIREWALL_USER
46#define duprintf(format, args...) printk(format , ## args)
47#else
48#define duprintf(format, args...)
49#endif
50
51#ifdef CONFIG_NETFILTER_DEBUG
52#define IP_NF_ASSERT(x) \
53do { \
54 if (!(x)) \
55 printk("IP_NF_ASSERT: %s:%s:%u\n", \
Harvey Harrison0dc47872008-03-05 20:47:47 -080056 __func__, __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070057} while(0)
58#else
59#define IP_NF_ASSERT(x)
60#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#if 0
63/* All the better to debug you with... */
64#define static
65#define inline
66#endif
67
68/*
69 We keep a set of rules for each CPU, so we can avoid write-locking
70 them in the softirq when updating the counters and therefore
71 only need to read-lock in the softirq; doing a write_lock_bh() in user
72 context stops packets coming through and allows user context to read
73 the counters or update the rules.
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 Hence the start of any table is given by get_table() below. */
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Returns whether matches rule or not. */
Denys Vlasenko022748a2008-01-14 23:44:05 -080078/* Performance critical - called for every packet */
Patrick McHardy9c547952007-12-17 21:52:00 -080079static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070080ip_packet_match(const struct iphdr *ip,
81 const char *indev,
82 const char *outdev,
83 const struct ipt_ip *ipinfo,
84 int isfrag)
85{
86 size_t i;
87 unsigned long ret;
88
Jan Engelhardte79ec502007-12-17 22:44:06 -080089#define FWINV(bool, invflg) ((bool) ^ !!(ipinfo->invflags & (invflg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
92 IPT_INV_SRCIP)
93 || FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
94 IPT_INV_DSTIP)) {
95 dprintf("Source or dest mismatch.\n");
96
97 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
98 NIPQUAD(ip->saddr),
99 NIPQUAD(ipinfo->smsk.s_addr),
100 NIPQUAD(ipinfo->src.s_addr),
101 ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
102 dprintf("DST: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
103 NIPQUAD(ip->daddr),
104 NIPQUAD(ipinfo->dmsk.s_addr),
105 NIPQUAD(ipinfo->dst.s_addr),
106 ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800107 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109
110 /* Look for ifname matches; this should unroll nicely. */
111 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
112 ret |= (((const unsigned long *)indev)[i]
113 ^ ((const unsigned long *)ipinfo->iniface)[i])
114 & ((const unsigned long *)ipinfo->iniface_mask)[i];
115 }
116
117 if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
118 dprintf("VIA in mismatch (%s vs %s).%s\n",
119 indev, ipinfo->iniface,
120 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800121 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
124 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
125 ret |= (((const unsigned long *)outdev)[i]
126 ^ ((const unsigned long *)ipinfo->outiface)[i])
127 & ((const unsigned long *)ipinfo->outiface_mask)[i];
128 }
129
130 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
131 dprintf("VIA out mismatch (%s vs %s).%s\n",
132 outdev, ipinfo->outiface,
133 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800134 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
137 /* Check specific protocol */
138 if (ipinfo->proto
139 && FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
140 dprintf("Packet protocol %hi does not match %hi.%s\n",
141 ip->protocol, ipinfo->proto,
142 ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800143 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
146 /* If we have a fragment rule but the packet is not a fragment
147 * then we return zero */
148 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
149 dprintf("Fragment rule but not fragment.%s\n",
150 ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800151 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
Patrick McHardy9c547952007-12-17 21:52:00 -0800154 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Denys Vlasenko022748a2008-01-14 23:44:05 -0800157static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158ip_checkentry(const struct ipt_ip *ip)
159{
160 if (ip->flags & ~IPT_F_MASK) {
161 duprintf("Unknown flag bits set: %08X\n",
162 ip->flags & ~IPT_F_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700163 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 if (ip->invflags & ~IPT_INV_MASK) {
166 duprintf("Unknown invflag bits set: %08X\n",
167 ip->invflags & ~IPT_INV_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700168 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700170 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200174ipt_error(struct sk_buff *skb, const struct xt_target_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 if (net_ratelimit())
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200177 printk("ip_tables: error: `%s'\n",
178 (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 return NF_DROP;
181}
182
Denys Vlasenko022748a2008-01-14 23:44:05 -0800183/* Performance critical - called for every packet */
184static inline bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200185do_match(struct ipt_entry_match *m, const struct sk_buff *skb,
186 struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200188 par->match = m->u.kernel.match;
189 par->matchinfo = m->data;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Stop iteration if it doesn't match */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200192 if (!m->u.kernel.match->match(skb, par))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700193 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 else
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700195 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Denys Vlasenko022748a2008-01-14 23:44:05 -0800198/* Performance critical */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static inline struct ipt_entry *
200get_entry(void *base, unsigned int offset)
201{
202 return (struct ipt_entry *)(base + offset);
203}
204
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700205/* All zeroes == unconditional rule. */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800206/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700207static inline int
208unconditional(const struct ipt_ip *ip)
209{
210 unsigned int i;
211
212 for (i = 0; i < sizeof(*ip)/sizeof(__u32); i++)
213 if (((__u32 *)ip)[i])
214 return 0;
215
216 return 1;
Jan Engelhardte79ec502007-12-17 22:44:06 -0800217#undef FWINV
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700218}
219
220#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
221 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
Denys Vlasenko022748a2008-01-14 23:44:05 -0800222static const char *const hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800223 [NF_INET_PRE_ROUTING] = "PREROUTING",
224 [NF_INET_LOCAL_IN] = "INPUT",
Patrick McHardy9c547952007-12-17 21:52:00 -0800225 [NF_INET_FORWARD] = "FORWARD",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800226 [NF_INET_LOCAL_OUT] = "OUTPUT",
227 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700228};
229
230enum nf_ip_trace_comments {
231 NF_IP_TRACE_COMMENT_RULE,
232 NF_IP_TRACE_COMMENT_RETURN,
233 NF_IP_TRACE_COMMENT_POLICY,
234};
235
Denys Vlasenko022748a2008-01-14 23:44:05 -0800236static const char *const comments[] = {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700237 [NF_IP_TRACE_COMMENT_RULE] = "rule",
238 [NF_IP_TRACE_COMMENT_RETURN] = "return",
239 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
240};
241
242static struct nf_loginfo trace_loginfo = {
243 .type = NF_LOG_TYPE_LOG,
244 .u = {
245 .log = {
246 .level = 4,
247 .logflags = NF_LOG_MASK,
248 },
249 },
250};
251
Denys Vlasenko022748a2008-01-14 23:44:05 -0800252/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700253static inline int
254get_chainname_rulenum(struct ipt_entry *s, struct ipt_entry *e,
255 char *hookname, char **chainname,
256 char **comment, unsigned int *rulenum)
257{
258 struct ipt_standard_target *t = (void *)ipt_get_target(s);
259
260 if (strcmp(t->target.u.kernel.target->name, IPT_ERROR_TARGET) == 0) {
261 /* Head of user chain: ERROR target with chainname */
262 *chainname = t->target.data;
263 (*rulenum) = 0;
264 } else if (s == e) {
265 (*rulenum)++;
266
267 if (s->target_offset == sizeof(struct ipt_entry)
268 && strcmp(t->target.u.kernel.target->name,
269 IPT_STANDARD_TARGET) == 0
270 && t->verdict < 0
271 && unconditional(&s->ip)) {
272 /* Tail of chains: STANDARD target (return/policy) */
273 *comment = *chainname == hookname
274 ? (char *)comments[NF_IP_TRACE_COMMENT_POLICY]
275 : (char *)comments[NF_IP_TRACE_COMMENT_RETURN];
276 }
277 return 1;
278 } else
279 (*rulenum)++;
280
281 return 0;
282}
283
284static void trace_packet(struct sk_buff *skb,
285 unsigned int hook,
286 const struct net_device *in,
287 const struct net_device *out,
Jan Engelhardtecb6f852008-01-31 03:54:47 -0800288 const char *tablename,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700289 struct xt_table_info *private,
290 struct ipt_entry *e)
291{
292 void *table_base;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200293 const struct ipt_entry *root;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700294 char *hookname, *chainname, *comment;
295 unsigned int rulenum = 0;
296
297 table_base = (void *)private->entries[smp_processor_id()];
298 root = get_entry(table_base, private->hook_entry[hook]);
299
300 hookname = chainname = (char *)hooknames[hook];
301 comment = (char *)comments[NF_IP_TRACE_COMMENT_RULE];
302
303 IPT_ENTRY_ITERATE(root,
304 private->size - private->hook_entry[hook],
305 get_chainname_rulenum,
306 e, hookname, &chainname, &comment, &rulenum);
307
308 nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo,
309 "TRACE: %s:%s:%s:%u ",
310 tablename, chainname, comment, rulenum);
311}
312#endif
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* Returns one of the generic firewall policies, like NF_ACCEPT. */
315unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700316ipt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned int hook,
318 const struct net_device *in,
319 const struct net_device *out,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800320 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Jan Engelhardt5452e422008-04-14 11:15:35 +0200323 const struct iphdr *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 u_int16_t datalen;
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700325 bool hotdrop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* Initializing verdict to NF_DROP keeps gcc happy. */
327 unsigned int verdict = NF_DROP;
328 const char *indev, *outdev;
329 void *table_base;
330 struct ipt_entry *e, *back;
Patrick McHardy83117312006-08-17 18:13:53 -0700331 struct xt_table_info *private;
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200332 struct xt_match_param mtpar;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200333 struct xt_target_param tgpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* Initialization */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700336 ip = ip_hdr(skb);
337 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 indev = in ? in->name : nulldevname;
339 outdev = out ? out->name : nulldevname;
340 /* We handle fragments by dealing with the first fragment as
341 * if it was a normal packet. All other fragments are treated
342 * normally, except that they will NEVER match rules that ask
343 * things we don't know, ie. tcp syn flag or ports). If the
344 * rule is also a fragment-specific rule, non-fragments won't
345 * match it. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200346 mtpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
347 mtpar.thoff = ip_hdrlen(skb);
348 mtpar.hotdrop = &hotdrop;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200349 mtpar.in = tgpar.in = in;
350 mtpar.out = tgpar.out = out;
351 tgpar.hooknum = hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 read_lock_bh(&table->lock);
354 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Patrick McHardy83117312006-08-17 18:13:53 -0700355 private = table->private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800356 table_base = (void *)private->entries[smp_processor_id()];
357 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* For return from builtin chain */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800360 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 do {
363 IP_NF_ASSERT(e);
364 IP_NF_ASSERT(back);
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200365 if (ip_packet_match(ip, indev, outdev,
366 &e->ip, mtpar.fragoff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct ipt_entry_target *t;
368
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200369 if (IPT_MATCH_ITERATE(e, do_match, skb, &mtpar) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 goto no_match;
371
372 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
373
374 t = ipt_get_target(e);
375 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700376
377#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
378 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
379 /* The packet is traced: log it */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700380 if (unlikely(skb->nf_trace))
381 trace_packet(skb, hook, in, out,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700382 table->name, private, e);
383#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* Standard target? */
385 if (!t->u.kernel.target->target) {
386 int v;
387
388 v = ((struct ipt_standard_target *)t)->verdict;
389 if (v < 0) {
390 /* Pop from stack? */
391 if (v != IPT_RETURN) {
392 verdict = (unsigned)(-v) - 1;
393 break;
394 }
395 e = back;
396 back = get_entry(table_base,
397 back->comefrom);
398 continue;
399 }
Patrick McHardy05465342005-08-21 23:31:43 -0700400 if (table_base + v != (void *)e + e->next_offset
401 && !(e->ip.flags & IPT_F_GOTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* Save old back ptr in next entry */
403 struct ipt_entry *next
404 = (void *)e + e->next_offset;
405 next->comefrom
406 = (void *)back - table_base;
407 /* set back pointer to next entry */
408 back = next;
409 }
410
411 e = get_entry(table_base, v);
412 } else {
413 /* Targets which reenter must return
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900414 abs. verdicts */
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200415 tgpar.target = t->u.kernel.target;
416 tgpar.targinfo = t->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#ifdef CONFIG_NETFILTER_DEBUG
418 ((struct ipt_entry *)table_base)->comefrom
419 = 0xeeeeeeec;
420#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700421 verdict = t->u.kernel.target->target(skb,
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200422 &tgpar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#ifdef CONFIG_NETFILTER_DEBUG
424 if (((struct ipt_entry *)table_base)->comefrom
425 != 0xeeeeeeec
426 && verdict == IPT_CONTINUE) {
427 printk("Target %s reentered!\n",
428 t->u.kernel.target->name);
429 verdict = NF_DROP;
430 }
431 ((struct ipt_entry *)table_base)->comefrom
432 = 0x57acc001;
433#endif
434 /* Target might have changed stuff. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700435 ip = ip_hdr(skb);
436 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (verdict == IPT_CONTINUE)
439 e = (void *)e + e->next_offset;
440 else
441 /* Verdict */
442 break;
443 }
444 } else {
445
446 no_match:
447 e = (void *)e + e->next_offset;
448 }
449 } while (!hotdrop);
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 read_unlock_bh(&table->lock);
452
453#ifdef DEBUG_ALLOW_ALL
454 return NF_ACCEPT;
455#else
456 if (hotdrop)
457 return NF_DROP;
458 else return verdict;
459#endif
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/* Figures out from what hook each rule can be called: returns 0 if
463 there are loops. Puts hook bitmask in comefrom. */
464static int
Harald Welte2e4e6a12006-01-12 13:30:04 -0800465mark_source_chains(struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800466 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 unsigned int hook;
469
470 /* No recursion; use packet counter to save back ptrs (reset
471 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800472 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800474 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if (!(valid_hooks & (1 << hook)))
477 continue;
478
479 /* Set initial back pointer. */
480 e->counters.pcnt = pos;
481
482 for (;;) {
483 struct ipt_standard_target *t
484 = (void *)ipt_get_target(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800485 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800487 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 printk("iptables: loop hook %u pos %u %08X.\n",
489 hook, pos, e->comefrom);
490 return 0;
491 }
Patrick McHardy9c547952007-12-17 21:52:00 -0800492 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Unconditional return/END. */
Al Viroe1b4b9f2006-12-12 00:29:52 -0800495 if ((e->target_offset == sizeof(struct ipt_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 && (strcmp(t->target.u.user.name,
497 IPT_STANDARD_TARGET) == 0)
498 && t->verdict < 0
Al Viroe1b4b9f2006-12-12 00:29:52 -0800499 && unconditional(&e->ip)) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 unsigned int oldpos, size;
501
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800502 if (t->verdict < -NF_MAX_VERDICT - 1) {
503 duprintf("mark_source_chains: bad "
504 "negative verdict (%i)\n",
505 t->verdict);
506 return 0;
507 }
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* Return: backtrack through the last
510 big jump. */
511 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800512 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513#ifdef DEBUG_IP_FIREWALL_USER
514 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800515 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 duprintf("Back unset "
517 "on hook %u "
518 "rule %u\n",
519 hook, pos);
520 }
521#endif
522 oldpos = pos;
523 pos = e->counters.pcnt;
524 e->counters.pcnt = 0;
525
526 /* We're at the start. */
527 if (pos == oldpos)
528 goto next;
529
530 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800531 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 } while (oldpos == pos + e->next_offset);
533
534 /* Move along one */
535 size = e->next_offset;
536 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800537 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 e->counters.pcnt = pos;
539 pos += size;
540 } else {
541 int newpos = t->verdict;
542
543 if (strcmp(t->target.u.user.name,
544 IPT_STANDARD_TARGET) == 0
545 && newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800546 if (newpos > newinfo->size -
547 sizeof(struct ipt_entry)) {
548 duprintf("mark_source_chains: "
549 "bad verdict (%i)\n",
550 newpos);
551 return 0;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* This a jump; chase it. */
554 duprintf("Jump rule %u -> %u\n",
555 pos, newpos);
556 } else {
557 /* ... this is a fallthru */
558 newpos = pos + e->next_offset;
559 }
560 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800561 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 e->counters.pcnt = pos;
563 pos = newpos;
564 }
565 }
566 next:
567 duprintf("Finished chain %u\n", hook);
568 }
569 return 1;
570}
571
Denys Vlasenko022748a2008-01-14 23:44:05 -0800572static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573cleanup_match(struct ipt_entry_match *m, unsigned int *i)
574{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200575 struct xt_mtdtor_param par;
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (i && (*i)-- == 0)
578 return 1;
579
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200580 par.match = m->u.kernel.match;
581 par.matchinfo = m->data;
582 if (par.match->destroy != NULL)
583 par.match->destroy(&par);
584 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 0;
586}
587
Denys Vlasenko022748a2008-01-14 23:44:05 -0800588static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800589check_entry(struct ipt_entry *e, const char *name)
590{
591 struct ipt_entry_target *t;
592
593 if (!ip_checkentry(&e->ip)) {
594 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
595 return -EINVAL;
596 }
597
Patrick McHardy9c547952007-12-17 21:52:00 -0800598 if (e->target_offset + sizeof(struct ipt_entry_target) >
599 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800600 return -EINVAL;
601
602 t = ipt_get_target(e);
603 if (e->target_offset + t->u.target_size > e->next_offset)
604 return -EINVAL;
605
606 return 0;
607}
608
Denys Vlasenko022748a2008-01-14 23:44:05 -0800609static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200610check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
611 unsigned int *i)
Dmitry Mishina96be242006-12-12 00:29:26 -0800612{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200613 const struct ipt_ip *ip = par->entryinfo;
Dmitry Mishina96be242006-12-12 00:29:26 -0800614 int ret;
615
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200616 par->match = m->u.kernel.match;
617 par->matchinfo = m->data;
618
619 ret = xt_check_match(par, NFPROTO_IPV4, m->u.match_size - sizeof(*m),
620 ip->proto, ip->invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200621 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800622 duprintf("ip_tables: check failed for `%s'.\n",
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200623 par.match->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200624 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800625 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200626 ++*i;
627 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800628}
629
Denys Vlasenko022748a2008-01-14 23:44:05 -0800630static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200631find_check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
Patrick McHardy4b478242007-12-17 21:46:15 -0800632 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800634 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800635 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Harald Welte2e4e6a12006-01-12 13:30:04 -0800637 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy9c547952007-12-17 21:52:00 -0800638 m->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 "ipt_%s", m->u.user.name);
640 if (IS_ERR(match) || !match) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800641 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return match ? PTR_ERR(match) : -ENOENT;
643 }
644 m->u.kernel.match = match;
645
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200646 ret = check_match(m, par, i);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800647 if (ret)
648 goto err;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800651err:
652 module_put(m->u.kernel.match->me);
653 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Denys Vlasenko022748a2008-01-14 23:44:05 -0800656static int check_target(struct ipt_entry *e, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800657{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200658 struct ipt_entry_target *t = ipt_get_target(e);
659 struct xt_tgchk_param par = {
660 .table = name,
661 .entryinfo = e,
662 .target = t->u.kernel.target,
663 .targinfo = t->data,
664 .hook_mask = e->comefrom,
665 };
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900666 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800667
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200668 ret = xt_check_target(&par, NFPROTO_IPV4, t->u.target_size - sizeof(*t),
669 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200670 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800671 duprintf("ip_tables: check failed for `%s'.\n",
672 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200673 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800674 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200675 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800676}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Denys Vlasenko022748a2008-01-14 23:44:05 -0800678static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800679find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
Patrick McHardy4b478242007-12-17 21:46:15 -0800680 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800683 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int ret;
685 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200686 struct xt_mtchk_param mtpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Dmitry Mishina96be242006-12-12 00:29:26 -0800688 ret = check_entry(e, name);
689 if (ret)
690 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200693 mtpar.table = name;
694 mtpar.entryinfo = &e->ip;
695 mtpar.hook_mask = e->comefrom;
696 ret = IPT_MATCH_ITERATE(e, find_check_match, &mtpar, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (ret != 0)
698 goto cleanup_matches;
699
700 t = ipt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800701 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -0800702 t->u.user.name,
703 t->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 "ipt_%s", t->u.user.name);
705 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800706 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 ret = target ? PTR_ERR(target) : -ENOENT;
708 goto cleanup_matches;
709 }
710 t->u.kernel.target = target;
711
Dmitry Mishina96be242006-12-12 00:29:26 -0800712 ret = check_target(e, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800713 if (ret)
714 goto err;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 (*i)++;
717 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800718 err:
719 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 cleanup_matches:
721 IPT_MATCH_ITERATE(e, cleanup_match, &j);
722 return ret;
723}
724
Denys Vlasenko022748a2008-01-14 23:44:05 -0800725static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800727 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned char *base,
729 unsigned char *limit,
730 const unsigned int *hook_entries,
731 const unsigned int *underflows,
732 unsigned int *i)
733{
734 unsigned int h;
735
736 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0
737 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
738 duprintf("Bad offset %p\n", e);
739 return -EINVAL;
740 }
741
742 if (e->next_offset
743 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
744 duprintf("checking: element %p size %u\n",
745 e, e->next_offset);
746 return -EINVAL;
747 }
748
749 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800750 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if ((unsigned char *)e - base == hook_entries[h])
752 newinfo->hook_entry[h] = hook_entries[h];
753 if ((unsigned char *)e - base == underflows[h])
754 newinfo->underflow[h] = underflows[h];
755 }
756
757 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900758 < 0 (not IPT_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800761 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 e->comefrom = 0;
763
764 (*i)++;
765 return 0;
766}
767
Denys Vlasenko022748a2008-01-14 23:44:05 -0800768static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769cleanup_entry(struct ipt_entry *e, unsigned int *i)
770{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200771 struct xt_tgdtor_param par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct ipt_entry_target *t;
773
774 if (i && (*i)-- == 0)
775 return 1;
776
777 /* Cleanup all matches */
778 IPT_MATCH_ITERATE(e, cleanup_match, NULL);
779 t = ipt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200780
781 par.target = t->u.kernel.target;
782 par.targinfo = t->data;
783 if (par.target->destroy != NULL)
784 par.target->destroy(&par);
785 module_put(par.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return 0;
787}
788
789/* Checks and translates the user-supplied table segment (held in
790 newinfo) */
791static int
792translate_table(const char *name,
793 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800794 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800795 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 unsigned int size,
797 unsigned int number,
798 const unsigned int *hook_entries,
799 const unsigned int *underflows)
800{
801 unsigned int i;
802 int ret;
803
804 newinfo->size = size;
805 newinfo->number = number;
806
807 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800808 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 newinfo->hook_entry[i] = 0xFFFFFFFF;
810 newinfo->underflow[i] = 0xFFFFFFFF;
811 }
812
813 duprintf("translate_table: size %u\n", newinfo->size);
814 i = 0;
815 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800816 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 check_entry_size_and_hooks,
818 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800819 entry0,
820 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 hook_entries, underflows, &i);
822 if (ret != 0)
823 return ret;
824
825 if (i != number) {
826 duprintf("translate_table: %u not %u entries\n",
827 i, number);
828 return -EINVAL;
829 }
830
831 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800832 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* Only hooks which are valid */
834 if (!(valid_hooks & (1 << i)))
835 continue;
836 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
837 duprintf("Invalid hook entry %u %u\n",
838 i, hook_entries[i]);
839 return -EINVAL;
840 }
841 if (newinfo->underflow[i] == 0xFFFFFFFF) {
842 duprintf("Invalid underflow %u %u\n",
843 i, underflows[i]);
844 return -EINVAL;
845 }
846 }
847
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800848 if (!mark_source_chains(newinfo, valid_hooks, entry0))
849 return -ELOOP;
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* Finally, each sanity check must pass */
852 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800853 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Dmitry Mishina96be242006-12-12 00:29:26 -0800854 find_check_entry, name, size, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800856 if (ret != 0) {
857 IPT_ENTRY_ITERATE(entry0, newinfo->size,
858 cleanup_entry, &i);
859 return ret;
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700863 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800864 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
865 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
868 return ret;
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/* Gets counters. */
872static inline int
873add_entry_to_counter(const struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800874 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 unsigned int *i)
876{
877 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
878
879 (*i)++;
880 return 0;
881}
882
Eric Dumazet31836062005-12-13 23:13:48 -0800883static inline int
884set_entry_to_counter(const struct ipt_entry *e,
885 struct ipt_counters total[],
886 unsigned int *i)
887{
888 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
889
890 (*i)++;
891 return 0;
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800895get_counters(const struct xt_table_info *t,
896 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 unsigned int cpu;
899 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800900 unsigned int curcpu;
901
902 /* Instead of clearing (by a previous call to memset())
903 * the counters and using adds, we set the counters
904 * with data used by 'current' CPU
905 * We dont care about preemption here.
906 */
907 curcpu = raw_smp_processor_id();
908
909 i = 0;
910 IPT_ENTRY_ITERATE(t->entries[curcpu],
911 t->size,
912 set_entry_to_counter,
913 counters,
914 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700916 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800917 if (cpu == curcpu)
918 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800920 IPT_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 t->size,
922 add_entry_to_counter,
923 counters,
924 &i);
925 }
926}
927
Denys Vlasenko022748a2008-01-14 23:44:05 -0800928static struct xt_counters * alloc_counters(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Dmitry Mishin27229712006-04-01 02:25:19 -0800930 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800931 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200932 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /* We need atomic snapshot of counters: rest doesn't change
935 (other than comefrom, which userspace doesn't care
936 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800937 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet31836062005-12-13 23:13:48 -0800938 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (counters == NULL)
Dmitry Mishin27229712006-04-01 02:25:19 -0800941 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 write_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800945 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 write_unlock_bh(&table->lock);
947
Dmitry Mishin27229712006-04-01 02:25:19 -0800948 return counters;
949}
950
951static int
952copy_entries_to_user(unsigned int total_size,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800953 struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800954 void __user *userptr)
955{
956 unsigned int off, num;
957 struct ipt_entry *e;
958 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200959 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800960 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200961 const void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -0800962
963 counters = alloc_counters(table);
964 if (IS_ERR(counters))
965 return PTR_ERR(counters);
966
Eric Dumazet31836062005-12-13 23:13:48 -0800967 /* choose the copy that is on our node/cpu, ...
968 * This choice is lazy (because current thread is
969 * allowed to migrate to another cpu)
970 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800971 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800972 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 ret = -EFAULT;
974 goto free_counters;
975 }
976
977 /* FIXME: use iterator macros --RR */
978 /* ... then go back and fix counters and names */
979 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
980 unsigned int i;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200981 const struct ipt_entry_match *m;
982 const struct ipt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Eric Dumazet31836062005-12-13 23:13:48 -0800984 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (copy_to_user(userptr + off
986 + offsetof(struct ipt_entry, counters),
987 &counters[num],
988 sizeof(counters[num])) != 0) {
989 ret = -EFAULT;
990 goto free_counters;
991 }
992
993 for (i = sizeof(struct ipt_entry);
994 i < e->target_offset;
995 i += m->u.match_size) {
996 m = (void *)e + i;
997
998 if (copy_to_user(userptr + off + i
999 + offsetof(struct ipt_entry_match,
1000 u.user.name),
1001 m->u.kernel.match->name,
1002 strlen(m->u.kernel.match->name)+1)
1003 != 0) {
1004 ret = -EFAULT;
1005 goto free_counters;
1006 }
1007 }
1008
1009 t = ipt_get_target(e);
1010 if (copy_to_user(userptr + off + e->target_offset
1011 + offsetof(struct ipt_entry_target,
1012 u.user.name),
1013 t->u.kernel.target->name,
1014 strlen(t->u.kernel.target->name)+1) != 0) {
1015 ret = -EFAULT;
1016 goto free_counters;
1017 }
1018 }
1019
1020 free_counters:
1021 vfree(counters);
1022 return ret;
1023}
1024
Dmitry Mishin27229712006-04-01 02:25:19 -08001025#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001026static void compat_standard_from_user(void *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001027{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001028 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001029
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001030 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001031 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001032 memcpy(dst, &v, sizeof(v));
1033}
1034
1035static int compat_standard_to_user(void __user *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001036{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001037 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001038
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001039 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001040 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001041 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001042}
1043
1044static inline int
Patrick McHardy4b478242007-12-17 21:46:15 -08001045compat_calc_match(struct ipt_entry_match *m, int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001046{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001047 *size += xt_compat_match_offset(m->u.kernel.match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001048 return 0;
1049}
1050
Eric Dumazet259d4e42007-12-04 23:24:56 -08001051static int compat_calc_entry(struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001052 const struct xt_table_info *info,
1053 void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001054{
1055 struct ipt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001056 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001057 int off, i, ret;
1058
Patrick McHardy30c08c42007-12-17 21:47:14 -08001059 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001060 entry_offset = (void *)e - base;
1061 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
1062 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001063 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001064 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001065 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001066 if (ret)
1067 return ret;
1068
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001069 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001070 if (info->hook_entry[i] &&
1071 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001072 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001073 if (info->underflow[i] &&
1074 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001075 newinfo->underflow[i] -= off;
1076 }
1077 return 0;
1078}
1079
Eric Dumazet259d4e42007-12-04 23:24:56 -08001080static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001081 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001082{
1083 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001084
1085 if (!newinfo || !info)
1086 return -EINVAL;
1087
Eric Dumazet259d4e42007-12-04 23:24:56 -08001088 /* we dont care about newinfo->entries[] */
1089 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1090 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001091 loc_cpu_entry = info->entries[raw_smp_processor_id()];
1092 return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001093 compat_calc_entry, info, loc_cpu_entry,
1094 newinfo);
Dmitry Mishin27229712006-04-01 02:25:19 -08001095}
1096#endif
1097
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001098static int get_info(struct net *net, void __user *user, int *len, int compat)
Dmitry Mishin27229712006-04-01 02:25:19 -08001099{
1100 char name[IPT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001101 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001102 int ret;
1103
1104 if (*len != sizeof(struct ipt_getinfo)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001105 duprintf("length %u != %zu\n", *len,
1106 sizeof(struct ipt_getinfo));
Dmitry Mishin27229712006-04-01 02:25:19 -08001107 return -EINVAL;
1108 }
1109
1110 if (copy_from_user(name, user, sizeof(name)) != 0)
1111 return -EFAULT;
1112
1113 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1114#ifdef CONFIG_COMPAT
1115 if (compat)
1116 xt_compat_lock(AF_INET);
1117#endif
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001118 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001119 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001120 if (t && !IS_ERR(t)) {
1121 struct ipt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001122 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001123
1124#ifdef CONFIG_COMPAT
1125 if (compat) {
1126 struct xt_table_info tmp;
1127 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001128 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001129 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001130 }
1131#endif
1132 info.valid_hooks = t->valid_hooks;
1133 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001134 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001135 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001136 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001137 info.num_entries = private->number;
1138 info.size = private->size;
1139 strcpy(info.name, name);
1140
1141 if (copy_to_user(user, &info, *len) != 0)
1142 ret = -EFAULT;
1143 else
1144 ret = 0;
1145
1146 xt_table_unlock(t);
1147 module_put(t->me);
1148 } else
1149 ret = t ? PTR_ERR(t) : -ENOENT;
1150#ifdef CONFIG_COMPAT
1151 if (compat)
1152 xt_compat_unlock(AF_INET);
1153#endif
1154 return ret;
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001158get_entries(struct net *net, struct ipt_get_entries __user *uptr, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001161 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001162 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Dmitry Mishin27229712006-04-01 02:25:19 -08001164 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001165 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001166 return -EINVAL;
1167 }
1168 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1169 return -EFAULT;
1170 if (*len != sizeof(struct ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001171 duprintf("get_entries: %u != %zu\n",
1172 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001173 return -EINVAL;
1174 }
1175
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001176 t = xt_find_table_lock(net, AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001178 const struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001179 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001180 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001181 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 t, uptr->entrytable);
1183 else {
1184 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001185 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001186 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001189 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 } else
1191 ret = t ? PTR_ERR(t) : -ENOENT;
1192
1193 return ret;
1194}
1195
1196static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001197__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001198 struct xt_table_info *newinfo, unsigned int num_counters,
1199 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001200{
1201 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001202 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001203 struct xt_table_info *oldinfo;
1204 struct xt_counters *counters;
1205 void *loc_cpu_old_entry;
1206
1207 ret = 0;
1208 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1209 if (!counters) {
1210 ret = -ENOMEM;
1211 goto out;
1212 }
1213
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001214 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Dmitry Mishin27229712006-04-01 02:25:19 -08001215 "iptable_%s", name);
1216 if (!t || IS_ERR(t)) {
1217 ret = t ? PTR_ERR(t) : -ENOENT;
1218 goto free_newinfo_counters_untrans;
1219 }
1220
1221 /* You lied! */
1222 if (valid_hooks != t->valid_hooks) {
1223 duprintf("Valid hook crap: %08X vs %08X\n",
1224 valid_hooks, t->valid_hooks);
1225 ret = -EINVAL;
1226 goto put_module;
1227 }
1228
1229 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1230 if (!oldinfo)
1231 goto put_module;
1232
1233 /* Update module usage count based on number of rules */
1234 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1235 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1236 if ((oldinfo->number > oldinfo->initial_entries) ||
1237 (newinfo->number <= oldinfo->initial_entries))
1238 module_put(t->me);
1239 if ((oldinfo->number > oldinfo->initial_entries) &&
1240 (newinfo->number <= oldinfo->initial_entries))
1241 module_put(t->me);
1242
1243 /* Get the old counters. */
1244 get_counters(oldinfo, counters);
1245 /* Decrease module usage counts and free resource */
1246 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Patrick McHardy4b478242007-12-17 21:46:15 -08001247 IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1248 NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001249 xt_free_table_info(oldinfo);
1250 if (copy_to_user(counters_ptr, counters,
1251 sizeof(struct xt_counters) * num_counters) != 0)
1252 ret = -EFAULT;
1253 vfree(counters);
1254 xt_table_unlock(t);
1255 return ret;
1256
1257 put_module:
1258 module_put(t->me);
1259 xt_table_unlock(t);
1260 free_newinfo_counters_untrans:
1261 vfree(counters);
1262 out:
1263 return ret;
1264}
1265
1266static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001267do_replace(struct net *net, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 int ret;
1270 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001271 struct xt_table_info *newinfo;
1272 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1275 return -EFAULT;
1276
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001277 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001278 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1279 return -ENOMEM;
1280
Harald Welte2e4e6a12006-01-12 13:30:04 -08001281 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (!newinfo)
1283 return -ENOMEM;
1284
Patrick McHardy9c547952007-12-17 21:52:00 -08001285 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001286 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1287 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 tmp.size) != 0) {
1289 ret = -EFAULT;
1290 goto free_newinfo;
1291 }
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001294 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 tmp.hook_entry, tmp.underflow);
1296 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001297 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 duprintf("ip_tables: Translated table\n");
1300
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001301 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001302 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001303 if (ret)
1304 goto free_newinfo_untrans;
1305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Dmitry Mishin27229712006-04-01 02:25:19 -08001307 free_newinfo_untrans:
Patrick McHardy9c547952007-12-17 21:52:00 -08001308 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001310 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return ret;
1312}
1313
1314/* We're lazy, and add to the first CPU; overflow works its fey magic
1315 * and everything is OK. */
Denys Vlasenko022748a2008-01-14 23:44:05 -08001316static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317add_counter_to_entry(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001318 const struct xt_counters addme[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 unsigned int *i)
1320{
1321#if 0
1322 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1323 *i,
1324 (long unsigned int)e->counters.pcnt,
1325 (long unsigned int)e->counters.bcnt,
1326 (long unsigned int)addme[*i].pcnt,
1327 (long unsigned int)addme[*i].bcnt);
1328#endif
1329
1330 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1331
1332 (*i)++;
1333 return 0;
1334}
1335
1336static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001337do_add_counters(struct net *net, void __user *user, unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 unsigned int i;
Dmitry Mishin27229712006-04-01 02:25:19 -08001340 struct xt_counters_info tmp;
1341 struct xt_counters *paddc;
1342 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001343 const char *name;
Dmitry Mishin27229712006-04-01 02:25:19 -08001344 int size;
1345 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001346 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001347 const struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001349 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001350#ifdef CONFIG_COMPAT
1351 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Dmitry Mishin27229712006-04-01 02:25:19 -08001353 if (compat) {
1354 ptmp = &compat_tmp;
1355 size = sizeof(struct compat_xt_counters_info);
1356 } else
1357#endif
1358 {
1359 ptmp = &tmp;
1360 size = sizeof(struct xt_counters_info);
1361 }
1362
1363 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -EFAULT;
1365
Dmitry Mishin27229712006-04-01 02:25:19 -08001366#ifdef CONFIG_COMPAT
1367 if (compat) {
1368 num_counters = compat_tmp.num_counters;
1369 name = compat_tmp.name;
1370 } else
1371#endif
1372 {
1373 num_counters = tmp.num_counters;
1374 name = tmp.name;
1375 }
1376
1377 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return -EINVAL;
1379
Dmitry Mishin27229712006-04-01 02:25:19 -08001380 paddc = vmalloc_node(len - size, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (!paddc)
1382 return -ENOMEM;
1383
Dmitry Mishin27229712006-04-01 02:25:19 -08001384 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 ret = -EFAULT;
1386 goto free;
1387 }
1388
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001389 t = xt_find_table_lock(net, AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (!t || IS_ERR(t)) {
1391 ret = t ? PTR_ERR(t) : -ENOENT;
1392 goto free;
1393 }
1394
1395 write_lock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001396 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001397 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 ret = -EINVAL;
1399 goto unlock_up_free;
1400 }
1401
1402 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001403 /* Choose the copy that is on our node */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001404 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -08001405 IPT_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001406 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 add_counter_to_entry,
Dmitry Mishin27229712006-04-01 02:25:19 -08001408 paddc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 &i);
1410 unlock_up_free:
1411 write_unlock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001412 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 module_put(t->me);
1414 free:
1415 vfree(paddc);
1416
1417 return ret;
1418}
1419
Dmitry Mishin27229712006-04-01 02:25:19 -08001420#ifdef CONFIG_COMPAT
1421struct compat_ipt_replace {
1422 char name[IPT_TABLE_MAXNAMELEN];
1423 u32 valid_hooks;
1424 u32 num_entries;
1425 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001426 u32 hook_entry[NF_INET_NUMHOOKS];
1427 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001428 u32 num_counters;
1429 compat_uptr_t counters; /* struct ipt_counters * */
1430 struct compat_ipt_entry entries[0];
1431};
1432
Patrick McHardya18aa312007-12-12 10:35:16 -08001433static int
1434compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001435 unsigned int *size, struct xt_counters *counters,
Patrick McHardya18aa312007-12-12 10:35:16 -08001436 unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001437{
Al Viro3e597c62006-09-24 23:42:20 +01001438 struct ipt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001439 struct compat_ipt_entry __user *ce;
1440 u_int16_t target_offset, next_offset;
1441 compat_uint_t origsize;
1442 int ret;
1443
1444 ret = -EFAULT;
1445 origsize = *size;
1446 ce = (struct compat_ipt_entry __user *)*dstptr;
Patrick McHardy78000072006-05-03 23:20:27 -07001447 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
Dmitry Mishin27229712006-04-01 02:25:19 -08001448 goto out;
1449
Patrick McHardya18aa312007-12-12 10:35:16 -08001450 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1451 goto out;
1452
Dmitry Mishin27229712006-04-01 02:25:19 -08001453 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001454 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1455
Patrick McHardyac8e27f2007-12-17 21:45:52 -08001456 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001457 target_offset = e->target_offset - (origsize - *size);
1458 if (ret)
1459 goto out;
1460 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001461 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001462 if (ret)
1463 goto out;
1464 ret = -EFAULT;
1465 next_offset = e->next_offset - (origsize - *size);
Patrick McHardy78000072006-05-03 23:20:27 -07001466 if (put_user(target_offset, &ce->target_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001467 goto out;
Patrick McHardy78000072006-05-03 23:20:27 -07001468 if (put_user(next_offset, &ce->next_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001469 goto out;
Patrick McHardya18aa312007-12-12 10:35:16 -08001470
1471 (*i)++;
Dmitry Mishin27229712006-04-01 02:25:19 -08001472 return 0;
1473out:
1474 return ret;
1475}
1476
Denys Vlasenko022748a2008-01-14 23:44:05 -08001477static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001478compat_find_calc_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001479 const char *name,
1480 const struct ipt_ip *ip,
1481 unsigned int hookmask,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001482 int *size, unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001483{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001484 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001485
1486 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001487 m->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001488 "ipt_%s", m->u.user.name);
1489 if (IS_ERR(match) || !match) {
1490 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001491 m->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001492 return match ? PTR_ERR(match) : -ENOENT;
1493 }
1494 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001495 *size += xt_compat_match_offset(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001496
1497 (*i)++;
1498 return 0;
1499}
1500
Denys Vlasenko022748a2008-01-14 23:44:05 -08001501static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001502compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1503{
1504 if (i && (*i)-- == 0)
1505 return 1;
1506
1507 module_put(m->u.kernel.match->me);
1508 return 0;
1509}
1510
Denys Vlasenko022748a2008-01-14 23:44:05 -08001511static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001512compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001513{
1514 struct ipt_entry_target *t;
1515
1516 if (i && (*i)-- == 0)
1517 return 1;
1518
1519 /* Cleanup all matches */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001520 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1521 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001522 module_put(t->u.kernel.target->me);
1523 return 0;
1524}
1525
Denys Vlasenko022748a2008-01-14 23:44:05 -08001526static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001527check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001528 struct xt_table_info *newinfo,
1529 unsigned int *size,
1530 unsigned char *base,
1531 unsigned char *limit,
1532 unsigned int *hook_entries,
1533 unsigned int *underflows,
1534 unsigned int *i,
1535 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001536{
1537 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001538 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001539 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001540 unsigned int j;
1541 int ret, off, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001542
1543 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1544 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0
1545 || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1546 duprintf("Bad offset %p, limit = %p\n", e, limit);
1547 return -EINVAL;
1548 }
1549
1550 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001551 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001552 duprintf("checking: element %p size %u\n",
1553 e, e->next_offset);
1554 return -EINVAL;
1555 }
1556
Patrick McHardy73cd5982007-12-17 21:47:32 -08001557 /* For purposes of check_entry casting the compat entry is fine */
1558 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001559 if (ret)
1560 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001561
Patrick McHardy30c08c42007-12-17 21:47:14 -08001562 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001563 entry_offset = (void *)e - (void *)base;
1564 j = 0;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001565 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1566 &e->ip, e->comefrom, &off, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001567 if (ret != 0)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001568 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001569
Patrick McHardy73cd5982007-12-17 21:47:32 -08001570 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001571 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -08001572 t->u.user.name,
1573 t->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001574 "ipt_%s", t->u.user.name);
1575 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001576 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001577 t->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001578 ret = target ? PTR_ERR(target) : -ENOENT;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001579 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001580 }
1581 t->u.kernel.target = target;
1582
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001583 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001584 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001585 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001586 if (ret)
1587 goto out;
1588
1589 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001590 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001591 if ((unsigned char *)e - base == hook_entries[h])
1592 newinfo->hook_entry[h] = hook_entries[h];
1593 if ((unsigned char *)e - base == underflows[h])
1594 newinfo->underflow[h] = underflows[h];
1595 }
1596
1597 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001598 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001599 e->comefrom = 0;
1600
1601 (*i)++;
1602 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001603
Dmitry Mishin27229712006-04-01 02:25:19 -08001604out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001605 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001606release_matches:
1607 IPT_MATCH_ITERATE(e, compat_release_match, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001608 return ret;
1609}
1610
Patrick McHardy4b478242007-12-17 21:46:15 -08001611static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001612compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001613 unsigned int *size, const char *name,
1614 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001615{
1616 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001617 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001618 struct ipt_entry *de;
1619 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001620 int ret, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001621
1622 ret = 0;
1623 origsize = *size;
1624 de = (struct ipt_entry *)*dstptr;
1625 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001626 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001627
Patrick McHardy73cd5982007-12-17 21:47:32 -08001628 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001629 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1630
Patrick McHardy73cd5982007-12-17 21:47:32 -08001631 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1632 dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001633 if (ret)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001634 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001635 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001636 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001637 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001638 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001639
1640 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001641 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001642 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1643 newinfo->hook_entry[h] -= origsize - *size;
1644 if ((unsigned char *)de - base < newinfo->underflow[h])
1645 newinfo->underflow[h] -= origsize - *size;
1646 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001647 return ret;
1648}
Dmitry Mishin27229712006-04-01 02:25:19 -08001649
Denys Vlasenko022748a2008-01-14 23:44:05 -08001650static int
1651compat_check_entry(struct ipt_entry *e, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001652 unsigned int *i)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001653{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001654 struct xt_mtchk_param mtpar;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001655 unsigned int j;
1656 int ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001657
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001658 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001659 mtpar.table = name;
1660 mtpar.entryinfo = &e->ip;
1661 mtpar.hook_mask = e->comefrom;
1662 ret = IPT_MATCH_ITERATE(e, check_match, &mtpar, &j);
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001663 if (ret)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001664 goto cleanup_matches;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001665
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001666 ret = check_target(e, name);
1667 if (ret)
1668 goto cleanup_matches;
1669
1670 (*i)++;
1671 return 0;
1672
1673 cleanup_matches:
1674 IPT_MATCH_ITERATE(e, cleanup_match, &j);
1675 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001676}
1677
Dmitry Mishin27229712006-04-01 02:25:19 -08001678static int
1679translate_compat_table(const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001680 unsigned int valid_hooks,
1681 struct xt_table_info **pinfo,
1682 void **pentry0,
1683 unsigned int total_size,
1684 unsigned int number,
1685 unsigned int *hook_entries,
1686 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001687{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001688 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001689 struct xt_table_info *newinfo, *info;
1690 void *pos, *entry0, *entry1;
1691 unsigned int size;
1692 int ret;
1693
1694 info = *pinfo;
1695 entry0 = *pentry0;
1696 size = total_size;
1697 info->number = number;
1698
1699 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001700 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001701 info->hook_entry[i] = 0xFFFFFFFF;
1702 info->underflow[i] = 0xFFFFFFFF;
1703 }
1704
1705 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001706 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001707 xt_compat_lock(AF_INET);
1708 /* Walk through entries, checking offsets. */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001709 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1710 check_compat_entry_size_and_hooks,
1711 info, &size, entry0,
1712 entry0 + total_size,
1713 hook_entries, underflows, &j, name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001714 if (ret != 0)
1715 goto out_unlock;
1716
1717 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001718 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001719 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001720 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001721 goto out_unlock;
1722 }
1723
1724 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001725 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001726 /* Only hooks which are valid */
1727 if (!(valid_hooks & (1 << i)))
1728 continue;
1729 if (info->hook_entry[i] == 0xFFFFFFFF) {
1730 duprintf("Invalid hook entry %u %u\n",
1731 i, hook_entries[i]);
1732 goto out_unlock;
1733 }
1734 if (info->underflow[i] == 0xFFFFFFFF) {
1735 duprintf("Invalid underflow %u %u\n",
1736 i, underflows[i]);
1737 goto out_unlock;
1738 }
1739 }
1740
1741 ret = -ENOMEM;
1742 newinfo = xt_alloc_table_info(size);
1743 if (!newinfo)
1744 goto out_unlock;
1745
1746 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001747 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001748 newinfo->hook_entry[i] = info->hook_entry[i];
1749 newinfo->underflow[i] = info->underflow[i];
1750 }
1751 entry1 = newinfo->entries[raw_smp_processor_id()];
1752 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001753 size = total_size;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001754 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
Patrick McHardy9c547952007-12-17 21:52:00 -08001755 compat_copy_entry_from_user,
1756 &pos, &size, name, newinfo, entry1);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001757 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001758 xt_compat_unlock(AF_INET);
1759 if (ret)
1760 goto free_newinfo;
1761
1762 ret = -ELOOP;
1763 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1764 goto free_newinfo;
1765
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001766 i = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001767 ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001768 name, &i);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001769 if (ret) {
1770 j -= i;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001771 COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1772 compat_release_entry, &j);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001773 IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1774 xt_free_table_info(newinfo);
1775 return ret;
1776 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001777
Dmitry Mishin27229712006-04-01 02:25:19 -08001778 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001779 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001780 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1781 memcpy(newinfo->entries[i], entry1, newinfo->size);
1782
1783 *pinfo = newinfo;
1784 *pentry0 = entry1;
1785 xt_free_table_info(info);
1786 return 0;
1787
1788free_newinfo:
1789 xt_free_table_info(newinfo);
1790out:
Patrick McHardy73cd5982007-12-17 21:47:32 -08001791 COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001792 return ret;
1793out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001794 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001795 xt_compat_unlock(AF_INET);
1796 goto out;
1797}
1798
1799static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001800compat_do_replace(struct net *net, void __user *user, unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001801{
1802 int ret;
1803 struct compat_ipt_replace tmp;
1804 struct xt_table_info *newinfo;
1805 void *loc_cpu_entry;
1806
1807 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1808 return -EFAULT;
1809
Dmitry Mishin27229712006-04-01 02:25:19 -08001810 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001811 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001812 return -ENOMEM;
1813 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1814 return -ENOMEM;
1815
1816 newinfo = xt_alloc_table_info(tmp.size);
1817 if (!newinfo)
1818 return -ENOMEM;
1819
Patrick McHardy9c547952007-12-17 21:52:00 -08001820 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001821 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1822 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1823 tmp.size) != 0) {
1824 ret = -EFAULT;
1825 goto free_newinfo;
1826 }
1827
1828 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001829 &newinfo, &loc_cpu_entry, tmp.size,
1830 tmp.num_entries, tmp.hook_entry,
1831 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001832 if (ret != 0)
1833 goto free_newinfo;
1834
1835 duprintf("compat_do_replace: Translated table\n");
1836
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001837 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001838 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001839 if (ret)
1840 goto free_newinfo_untrans;
1841 return 0;
1842
1843 free_newinfo_untrans:
Patrick McHardy4b478242007-12-17 21:46:15 -08001844 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001845 free_newinfo:
1846 xt_free_table_info(newinfo);
1847 return ret;
1848}
1849
1850static int
1851compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001852 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001853{
1854 int ret;
1855
1856 if (!capable(CAP_NET_ADMIN))
1857 return -EPERM;
1858
1859 switch (cmd) {
1860 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001861 ret = compat_do_replace(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001862 break;
1863
1864 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001865 ret = do_add_counters(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001866 break;
1867
1868 default:
1869 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1870 ret = -EINVAL;
1871 }
1872
1873 return ret;
1874}
1875
Patrick McHardy4b478242007-12-17 21:46:15 -08001876struct compat_ipt_get_entries {
Dmitry Mishin27229712006-04-01 02:25:19 -08001877 char name[IPT_TABLE_MAXNAMELEN];
1878 compat_uint_t size;
1879 struct compat_ipt_entry entrytable[0];
1880};
1881
Patrick McHardy4b478242007-12-17 21:46:15 -08001882static int
1883compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1884 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001885{
Dmitry Mishin27229712006-04-01 02:25:19 -08001886 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001887 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001888 void __user *pos;
1889 unsigned int size;
1890 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001891 const void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001892 unsigned int i = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001893
1894 counters = alloc_counters(table);
1895 if (IS_ERR(counters))
1896 return PTR_ERR(counters);
1897
1898 /* choose the copy that is on our node/cpu, ...
1899 * This choice is lazy (because current thread is
1900 * allowed to migrate to another cpu)
1901 */
1902 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1903 pos = userptr;
1904 size = total_size;
1905 ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
Patrick McHardya18aa312007-12-12 10:35:16 -08001906 compat_copy_entry_to_user,
1907 &pos, &size, counters, &i);
Dmitry Mishin27229712006-04-01 02:25:19 -08001908
Dmitry Mishin27229712006-04-01 02:25:19 -08001909 vfree(counters);
1910 return ret;
1911}
1912
1913static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001914compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1915 int *len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001916{
1917 int ret;
1918 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001919 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001920
Dmitry Mishin27229712006-04-01 02:25:19 -08001921 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001922 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001923 return -EINVAL;
1924 }
1925
1926 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1927 return -EFAULT;
1928
1929 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001930 duprintf("compat_get_entries: %u != %zu\n",
1931 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001932 return -EINVAL;
1933 }
1934
1935 xt_compat_lock(AF_INET);
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001936 t = xt_find_table_lock(net, AF_INET, get.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001937 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001938 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001939 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08001940 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001941 ret = compat_table_info(private, &info);
1942 if (!ret && get.size == info.size) {
1943 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001944 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001945 } else if (!ret) {
1946 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001947 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001948 ret = -EAGAIN;
Dmitry Mishin27229712006-04-01 02:25:19 -08001949 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001950 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001951 module_put(t->me);
1952 xt_table_unlock(t);
1953 } else
1954 ret = t ? PTR_ERR(t) : -ENOENT;
1955
1956 xt_compat_unlock(AF_INET);
1957 return ret;
1958}
1959
Patrick McHardy79030ed2006-09-20 12:05:08 -07001960static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1961
Dmitry Mishin27229712006-04-01 02:25:19 -08001962static int
1963compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1964{
1965 int ret;
1966
Björn Steinbrink82fac052006-10-20 00:21:10 -07001967 if (!capable(CAP_NET_ADMIN))
1968 return -EPERM;
1969
Dmitry Mishin27229712006-04-01 02:25:19 -08001970 switch (cmd) {
1971 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001972 ret = get_info(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001973 break;
1974 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001975 ret = compat_get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001976 break;
1977 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001978 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001979 }
1980 return ret;
1981}
1982#endif
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001985do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 int ret;
1988
1989 if (!capable(CAP_NET_ADMIN))
1990 return -EPERM;
1991
1992 switch (cmd) {
1993 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001994 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 break;
1996
1997 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001998 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 break;
2000
2001 default:
2002 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
2003 ret = -EINVAL;
2004 }
2005
2006 return ret;
2007}
2008
2009static int
2010do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2011{
2012 int ret;
2013
2014 if (!capable(CAP_NET_ADMIN))
2015 return -EPERM;
2016
2017 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002018 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002019 ret = get_info(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002021
2022 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002023 ret = get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08002024 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 case IPT_SO_GET_REVISION_MATCH:
2027 case IPT_SO_GET_REVISION_TARGET: {
2028 struct ipt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002029 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 if (*len != sizeof(rev)) {
2032 ret = -EINVAL;
2033 break;
2034 }
2035 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2036 ret = -EFAULT;
2037 break;
2038 }
2039
2040 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002041 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002043 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Harald Welte2e4e6a12006-01-12 13:30:04 -08002045 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2046 rev.revision,
2047 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 "ipt_%s", rev.name);
2049 break;
2050 }
2051
2052 default:
2053 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2054 ret = -EINVAL;
2055 }
2056
2057 return ret;
2058}
2059
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002060struct xt_table *ipt_register_table(struct net *net, struct xt_table *table,
2061 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002064 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08002065 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08002067 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08002068 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Harald Welte2e4e6a12006-01-12 13:30:04 -08002070 newinfo = xt_alloc_table_info(repl->size);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002071 if (!newinfo) {
2072 ret = -ENOMEM;
2073 goto out;
2074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Patrick McHardy9c547952007-12-17 21:52:00 -08002076 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002077 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2078 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08002081 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 repl->num_entries,
2083 repl->hook_entry,
2084 repl->underflow);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002085 if (ret != 0)
2086 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002088 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08002089 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002090 ret = PTR_ERR(new_table);
2091 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002094 return new_table;
2095
2096out_free:
2097 xt_free_table_info(newinfo);
2098out:
2099 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Jan Engelhardte60a13e2007-02-07 15:12:33 -08002102void ipt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002104 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002105 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08002106 struct module *table_owner = table->me;
Eric Dumazet31836062005-12-13 23:13:48 -08002107
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002108 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002111 loc_cpu_entry = private->entries[raw_smp_processor_id()];
2112 IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
Alexey Dobriyandf200962008-01-31 04:05:34 -08002113 if (private->number > private->initial_entries)
2114 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002115 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
2118/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002119static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2121 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002122 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Patrick McHardy9c547952007-12-17 21:52:00 -08002124 return ((test_type == 0xFF) ||
2125 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 ^ invert;
2127}
2128
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002129static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002130icmp_match(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Jan Engelhardt5452e422008-04-14 11:15:35 +02002132 const struct icmphdr *ic;
2133 struct icmphdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002134 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002137 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002138 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002140 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (ic == NULL) {
2142 /* We've been asked to examine this packet, and we
2143 * can't. Hence, no choice but to drop.
2144 */
2145 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002146 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002147 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149
2150 return icmp_type_code_match(icmpinfo->type,
2151 icmpinfo->code[0],
2152 icmpinfo->code[1],
2153 ic->type, ic->code,
2154 !!(icmpinfo->invflags&IPT_ICMP_INV));
2155}
2156
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002157static bool icmp_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002159 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002161 /* Must specify no unknown invflags */
2162 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
2165/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07002166static struct xt_target ipt_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 .name = IPT_STANDARD_TARGET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002168 .targetsize = sizeof(int),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002169 .family = AF_INET,
Dmitry Mishin27229712006-04-01 02:25:19 -08002170#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07002171 .compatsize = sizeof(compat_int_t),
2172 .compat_from_user = compat_standard_from_user,
2173 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175};
2176
Patrick McHardy9f15c532007-07-07 22:22:02 -07002177static struct xt_target ipt_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 .name = IPT_ERROR_TARGET,
2179 .target = ipt_error,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002180 .targetsize = IPT_FUNCTION_MAXNAMELEN,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002181 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182};
2183
2184static struct nf_sockopt_ops ipt_sockopts = {
2185 .pf = PF_INET,
2186 .set_optmin = IPT_BASE_CTL,
2187 .set_optmax = IPT_SO_SET_MAX+1,
2188 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002189#ifdef CONFIG_COMPAT
2190 .compat_set = compat_do_ipt_set_ctl,
2191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 .get_optmin = IPT_BASE_CTL,
2193 .get_optmax = IPT_SO_GET_MAX+1,
2194 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002195#ifdef CONFIG_COMPAT
2196 .compat_get = compat_do_ipt_get_ctl,
2197#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002198 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199};
2200
Patrick McHardy9f15c532007-07-07 22:22:02 -07002201static struct xt_match icmp_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 .name = "icmp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002203 .match = icmp_match,
2204 .matchsize = sizeof(struct ipt_icmp),
Patrick McHardy9c547952007-12-17 21:52:00 -08002205 .checkentry = icmp_checkentry,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002206 .proto = IPPROTO_ICMP,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002207 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208};
2209
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002210static int __net_init ip_tables_net_init(struct net *net)
2211{
2212 return xt_proto_init(net, AF_INET);
2213}
2214
2215static void __net_exit ip_tables_net_exit(struct net *net)
2216{
2217 xt_proto_fini(net, AF_INET);
2218}
2219
2220static struct pernet_operations ip_tables_net_ops = {
2221 .init = ip_tables_net_init,
2222 .exit = ip_tables_net_exit,
2223};
2224
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002225static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
2227 int ret;
2228
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002229 ret = register_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002230 if (ret < 0)
2231 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002234 ret = xt_register_target(&ipt_standard_target);
2235 if (ret < 0)
2236 goto err2;
2237 ret = xt_register_target(&ipt_error_target);
2238 if (ret < 0)
2239 goto err3;
2240 ret = xt_register_match(&icmp_matchstruct);
2241 if (ret < 0)
2242 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 /* Register setsockopt */
2245 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002246 if (ret < 0)
2247 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Dan Aloni0236e662007-07-09 13:20:12 -07002249 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002251
2252err5:
2253 xt_unregister_match(&icmp_matchstruct);
2254err4:
2255 xt_unregister_target(&ipt_error_target);
2256err3:
2257 xt_unregister_target(&ipt_standard_target);
2258err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002259 unregister_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002260err1:
2261 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002264static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002267
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002268 xt_unregister_match(&icmp_matchstruct);
2269 xt_unregister_target(&ipt_error_target);
2270 xt_unregister_target(&ipt_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002271
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002272 unregister_pernet_subsys(&ip_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
2275EXPORT_SYMBOL(ipt_register_table);
2276EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002278module_init(ip_tables_init);
2279module_exit(ip_tables_fini);