blob: 82888bc6bc1af59b9dd679109af103b95d67f8ae [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unsigned long ret;
87
Jan Engelhardte79ec502007-12-17 22:44:06 -080088#define FWINV(bool, invflg) ((bool) ^ !!(ipinfo->invflags & (invflg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
91 IPT_INV_SRCIP)
92 || FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
93 IPT_INV_DSTIP)) {
94 dprintf("Source or dest mismatch.\n");
95
Harvey Harrisoncffee382008-10-31 00:53:08 -070096 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
97 &ip->saddr, &ipinfo->smsk.s_addr, &ipinfo->src.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
Harvey Harrisoncffee382008-10-31 00:53:08 -070099 dprintf("DST: %pI4 Mask: %pI4 Target: %pI4.%s\n",
100 &ip->daddr, &ipinfo->dmsk.s_addr, &ipinfo->dst.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800102 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
Eric Dumazetb8dfe492009-03-25 17:31:52 +0100105 ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
108 dprintf("VIA in mismatch (%s vs %s).%s\n",
109 indev, ipinfo->iniface,
110 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800111 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
Eric Dumazetb8dfe492009-03-25 17:31:52 +0100114 ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
117 dprintf("VIA out mismatch (%s vs %s).%s\n",
118 outdev, ipinfo->outiface,
119 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800120 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
123 /* Check specific protocol */
124 if (ipinfo->proto
125 && FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
126 dprintf("Packet protocol %hi does not match %hi.%s\n",
127 ip->protocol, ipinfo->proto,
128 ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800129 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131
132 /* If we have a fragment rule but the packet is not a fragment
133 * then we return zero */
134 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
135 dprintf("Fragment rule but not fragment.%s\n",
136 ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800137 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
Patrick McHardy9c547952007-12-17 21:52:00 -0800140 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Denys Vlasenko022748a2008-01-14 23:44:05 -0800143static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144ip_checkentry(const struct ipt_ip *ip)
145{
146 if (ip->flags & ~IPT_F_MASK) {
147 duprintf("Unknown flag bits set: %08X\n",
148 ip->flags & ~IPT_F_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700149 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 if (ip->invflags & ~IPT_INV_MASK) {
152 duprintf("Unknown invflag bits set: %08X\n",
153 ip->invflags & ~IPT_INV_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700154 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700156 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200160ipt_error(struct sk_buff *skb, const struct xt_target_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 if (net_ratelimit())
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200163 printk("ip_tables: error: `%s'\n",
164 (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 return NF_DROP;
167}
168
Denys Vlasenko022748a2008-01-14 23:44:05 -0800169/* Performance critical - called for every packet */
170static inline bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200171do_match(struct ipt_entry_match *m, const struct sk_buff *skb,
172 struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200174 par->match = m->u.kernel.match;
175 par->matchinfo = m->data;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /* Stop iteration if it doesn't match */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200178 if (!m->u.kernel.match->match(skb, par))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700179 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 else
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700181 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Denys Vlasenko022748a2008-01-14 23:44:05 -0800184/* Performance critical */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static inline struct ipt_entry *
186get_entry(void *base, unsigned int offset)
187{
188 return (struct ipt_entry *)(base + offset);
189}
190
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700191/* All zeroes == unconditional rule. */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800192/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700193static inline int
194unconditional(const struct ipt_ip *ip)
195{
196 unsigned int i;
197
198 for (i = 0; i < sizeof(*ip)/sizeof(__u32); i++)
199 if (((__u32 *)ip)[i])
200 return 0;
201
202 return 1;
Jan Engelhardte79ec502007-12-17 22:44:06 -0800203#undef FWINV
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700204}
205
206#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
207 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
Denys Vlasenko022748a2008-01-14 23:44:05 -0800208static const char *const hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800209 [NF_INET_PRE_ROUTING] = "PREROUTING",
210 [NF_INET_LOCAL_IN] = "INPUT",
Patrick McHardy9c547952007-12-17 21:52:00 -0800211 [NF_INET_FORWARD] = "FORWARD",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800212 [NF_INET_LOCAL_OUT] = "OUTPUT",
213 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700214};
215
216enum nf_ip_trace_comments {
217 NF_IP_TRACE_COMMENT_RULE,
218 NF_IP_TRACE_COMMENT_RETURN,
219 NF_IP_TRACE_COMMENT_POLICY,
220};
221
Denys Vlasenko022748a2008-01-14 23:44:05 -0800222static const char *const comments[] = {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700223 [NF_IP_TRACE_COMMENT_RULE] = "rule",
224 [NF_IP_TRACE_COMMENT_RETURN] = "return",
225 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
226};
227
228static struct nf_loginfo trace_loginfo = {
229 .type = NF_LOG_TYPE_LOG,
230 .u = {
231 .log = {
232 .level = 4,
233 .logflags = NF_LOG_MASK,
234 },
235 },
236};
237
Denys Vlasenko022748a2008-01-14 23:44:05 -0800238/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700239static inline int
240get_chainname_rulenum(struct ipt_entry *s, struct ipt_entry *e,
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200241 const char *hookname, const char **chainname,
242 const char **comment, unsigned int *rulenum)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700243{
244 struct ipt_standard_target *t = (void *)ipt_get_target(s);
245
246 if (strcmp(t->target.u.kernel.target->name, IPT_ERROR_TARGET) == 0) {
247 /* Head of user chain: ERROR target with chainname */
248 *chainname = t->target.data;
249 (*rulenum) = 0;
250 } else if (s == e) {
251 (*rulenum)++;
252
253 if (s->target_offset == sizeof(struct ipt_entry)
254 && strcmp(t->target.u.kernel.target->name,
255 IPT_STANDARD_TARGET) == 0
256 && t->verdict < 0
257 && unconditional(&s->ip)) {
258 /* Tail of chains: STANDARD target (return/policy) */
259 *comment = *chainname == hookname
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200260 ? comments[NF_IP_TRACE_COMMENT_POLICY]
261 : comments[NF_IP_TRACE_COMMENT_RETURN];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700262 }
263 return 1;
264 } else
265 (*rulenum)++;
266
267 return 0;
268}
269
270static void trace_packet(struct sk_buff *skb,
271 unsigned int hook,
272 const struct net_device *in,
273 const struct net_device *out,
Jan Engelhardtecb6f852008-01-31 03:54:47 -0800274 const char *tablename,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700275 struct xt_table_info *private,
276 struct ipt_entry *e)
277{
278 void *table_base;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200279 const struct ipt_entry *root;
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200280 const char *hookname, *chainname, *comment;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700281 unsigned int rulenum = 0;
282
Jan Engelhardtccf5bd82009-04-15 20:27:03 +0200283 table_base = private->entries[smp_processor_id()];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700284 root = get_entry(table_base, private->hook_entry[hook]);
285
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200286 hookname = chainname = hooknames[hook];
287 comment = comments[NF_IP_TRACE_COMMENT_RULE];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700288
289 IPT_ENTRY_ITERATE(root,
290 private->size - private->hook_entry[hook],
291 get_chainname_rulenum,
292 e, hookname, &chainname, &comment, &rulenum);
293
294 nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo,
295 "TRACE: %s:%s:%s:%u ",
296 tablename, chainname, comment, rulenum);
297}
298#endif
299
Jan Engelhardt98e86402009-04-15 21:06:05 +0200300static inline __pure
301struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
302{
303 return (void *)entry + entry->next_offset;
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/* Returns one of the generic firewall policies, like NF_ACCEPT. */
307unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700308ipt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 unsigned int hook,
310 const struct net_device *in,
311 const struct net_device *out,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800312 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Jan Engelhardt5452e422008-04-14 11:15:35 +0200315 const struct iphdr *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 u_int16_t datalen;
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700317 bool hotdrop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* Initializing verdict to NF_DROP keeps gcc happy. */
319 unsigned int verdict = NF_DROP;
320 const char *indev, *outdev;
321 void *table_base;
322 struct ipt_entry *e, *back;
Patrick McHardy83117312006-08-17 18:13:53 -0700323 struct xt_table_info *private;
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200324 struct xt_match_param mtpar;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200325 struct xt_target_param tgpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* Initialization */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700328 ip = ip_hdr(skb);
329 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 indev = in ? in->name : nulldevname;
331 outdev = out ? out->name : nulldevname;
332 /* We handle fragments by dealing with the first fragment as
333 * if it was a normal packet. All other fragments are treated
334 * normally, except that they will NEVER match rules that ask
335 * things we don't know, ie. tcp syn flag or ports). If the
336 * rule is also a fragment-specific rule, non-fragments won't
337 * match it. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200338 mtpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
339 mtpar.thoff = ip_hdrlen(skb);
340 mtpar.hotdrop = &hotdrop;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200341 mtpar.in = tgpar.in = in;
342 mtpar.out = tgpar.out = out;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200343 mtpar.family = tgpar.family = NFPROTO_IPV4;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200344 tgpar.hooknum = hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700347 xt_info_rdlock_bh();
348 private = table->private;
349 table_base = private->entries[smp_processor_id()];
Stephen Hemminger78454472009-02-20 10:35:32 +0100350
Harald Welte2e4e6a12006-01-12 13:30:04 -0800351 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* For return from builtin chain */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800354 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 do {
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200357 struct ipt_entry_target *t;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 IP_NF_ASSERT(e);
360 IP_NF_ASSERT(back);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200361 if (!ip_packet_match(ip, indev, outdev,
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200362 &e->ip, mtpar.fragoff)) {
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200363 no_match:
364 e = ipt_next_entry(e);
365 continue;
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200368 if (IPT_MATCH_ITERATE(e, do_match, skb, &mtpar) != 0)
369 goto no_match;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200371 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200373 t = ipt_get_target(e);
374 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700375
376#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
377 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200378 /* The packet is traced: log it */
379 if (unlikely(skb->nf_trace))
380 trace_packet(skb, hook, in, out,
381 table->name, private, e);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700382#endif
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200383 /* Standard target? */
384 if (!t->u.kernel.target->target) {
385 int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200387 v = ((struct ipt_standard_target *)t)->verdict;
388 if (v < 0) {
389 /* Pop from stack? */
390 if (v != IPT_RETURN) {
391 verdict = (unsigned)(-v) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 break;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200393 }
394 e = back;
395 back = get_entry(table_base, back->comefrom);
396 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200398 if (table_base + v != ipt_next_entry(e)
399 && !(e->ip.flags & IPT_F_GOTO)) {
400 /* Save old back ptr in next entry */
401 struct ipt_entry *next = ipt_next_entry(e);
402 next->comefrom = (void *)back - table_base;
403 /* set back pointer to next entry */
404 back = next;
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200407 e = get_entry(table_base, v);
408 } else {
409 /* Targets which reenter must return
410 abs. verdicts */
411 tgpar.target = t->u.kernel.target;
412 tgpar.targinfo = t->data;
413#ifdef CONFIG_NETFILTER_DEBUG
414 ((struct ipt_entry *)table_base)->comefrom
415 = 0xeeeeeeec;
416#endif
417 verdict = t->u.kernel.target->target(skb, &tgpar);
418#ifdef CONFIG_NETFILTER_DEBUG
419 if (((struct ipt_entry *)table_base)->comefrom
420 != 0xeeeeeeec
421 && verdict == IPT_CONTINUE) {
422 printk("Target %s reentered!\n",
423 t->u.kernel.target->name);
424 verdict = NF_DROP;
425 }
426 ((struct ipt_entry *)table_base)->comefrom
427 = 0x57acc001;
428#endif
429 /* Target might have changed stuff. */
430 ip = ip_hdr(skb);
431 datalen = skb->len - ip->ihl * 4;
432
433 if (verdict == IPT_CONTINUE)
434 e = ipt_next_entry(e);
435 else
436 /* Verdict */
437 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 } while (!hotdrop);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700440 xt_info_rdunlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442#ifdef DEBUG_ALLOW_ALL
443 return NF_ACCEPT;
444#else
445 if (hotdrop)
446 return NF_DROP;
447 else return verdict;
448#endif
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/* Figures out from what hook each rule can be called: returns 0 if
452 there are loops. Puts hook bitmask in comefrom. */
453static int
Harald Welte2e4e6a12006-01-12 13:30:04 -0800454mark_source_chains(struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800455 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 unsigned int hook;
458
459 /* No recursion; use packet counter to save back ptrs (reset
460 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800461 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800463 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 if (!(valid_hooks & (1 << hook)))
466 continue;
467
468 /* Set initial back pointer. */
469 e->counters.pcnt = pos;
470
471 for (;;) {
472 struct ipt_standard_target *t
473 = (void *)ipt_get_target(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800474 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800476 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 printk("iptables: loop hook %u pos %u %08X.\n",
478 hook, pos, e->comefrom);
479 return 0;
480 }
Patrick McHardy9c547952007-12-17 21:52:00 -0800481 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* Unconditional return/END. */
Al Viroe1b4b9f2006-12-12 00:29:52 -0800484 if ((e->target_offset == sizeof(struct ipt_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 && (strcmp(t->target.u.user.name,
486 IPT_STANDARD_TARGET) == 0)
487 && t->verdict < 0
Al Viroe1b4b9f2006-12-12 00:29:52 -0800488 && unconditional(&e->ip)) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 unsigned int oldpos, size;
490
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100491 if ((strcmp(t->target.u.user.name,
492 IPT_STANDARD_TARGET) == 0) &&
493 t->verdict < -NF_MAX_VERDICT - 1) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800494 duprintf("mark_source_chains: bad "
495 "negative verdict (%i)\n",
496 t->verdict);
497 return 0;
498 }
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Return: backtrack through the last
501 big jump. */
502 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800503 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504#ifdef DEBUG_IP_FIREWALL_USER
505 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800506 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 duprintf("Back unset "
508 "on hook %u "
509 "rule %u\n",
510 hook, pos);
511 }
512#endif
513 oldpos = pos;
514 pos = e->counters.pcnt;
515 e->counters.pcnt = 0;
516
517 /* We're at the start. */
518 if (pos == oldpos)
519 goto next;
520
521 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800522 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 } while (oldpos == pos + e->next_offset);
524
525 /* Move along one */
526 size = e->next_offset;
527 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800528 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 e->counters.pcnt = pos;
530 pos += size;
531 } else {
532 int newpos = t->verdict;
533
534 if (strcmp(t->target.u.user.name,
535 IPT_STANDARD_TARGET) == 0
536 && newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800537 if (newpos > newinfo->size -
538 sizeof(struct ipt_entry)) {
539 duprintf("mark_source_chains: "
540 "bad verdict (%i)\n",
541 newpos);
542 return 0;
543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* This a jump; chase it. */
545 duprintf("Jump rule %u -> %u\n",
546 pos, newpos);
547 } else {
548 /* ... this is a fallthru */
549 newpos = pos + e->next_offset;
550 }
551 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800552 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 e->counters.pcnt = pos;
554 pos = newpos;
555 }
556 }
557 next:
558 duprintf("Finished chain %u\n", hook);
559 }
560 return 1;
561}
562
Denys Vlasenko022748a2008-01-14 23:44:05 -0800563static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564cleanup_match(struct ipt_entry_match *m, unsigned int *i)
565{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200566 struct xt_mtdtor_param par;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (i && (*i)-- == 0)
569 return 1;
570
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200571 par.match = m->u.kernel.match;
572 par.matchinfo = m->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200573 par.family = NFPROTO_IPV4;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200574 if (par.match->destroy != NULL)
575 par.match->destroy(&par);
576 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578}
579
Denys Vlasenko022748a2008-01-14 23:44:05 -0800580static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800581check_entry(struct ipt_entry *e, const char *name)
582{
583 struct ipt_entry_target *t;
584
585 if (!ip_checkentry(&e->ip)) {
586 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
587 return -EINVAL;
588 }
589
Patrick McHardy9c547952007-12-17 21:52:00 -0800590 if (e->target_offset + sizeof(struct ipt_entry_target) >
591 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800592 return -EINVAL;
593
594 t = ipt_get_target(e);
595 if (e->target_offset + t->u.target_size > e->next_offset)
596 return -EINVAL;
597
598 return 0;
599}
600
Denys Vlasenko022748a2008-01-14 23:44:05 -0800601static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200602check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
603 unsigned int *i)
Dmitry Mishina96be242006-12-12 00:29:26 -0800604{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200605 const struct ipt_ip *ip = par->entryinfo;
Dmitry Mishina96be242006-12-12 00:29:26 -0800606 int ret;
607
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200608 par->match = m->u.kernel.match;
609 par->matchinfo = m->data;
610
Jan Engelhardt916a9172008-10-08 11:35:20 +0200611 ret = xt_check_match(par, m->u.match_size - sizeof(*m),
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200612 ip->proto, ip->invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200613 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800614 duprintf("ip_tables: check failed for `%s'.\n",
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200615 par.match->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200616 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800617 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200618 ++*i;
619 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800620}
621
Denys Vlasenko022748a2008-01-14 23:44:05 -0800622static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200623find_check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
Patrick McHardy4b478242007-12-17 21:46:15 -0800624 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800626 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800627 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Harald Welte2e4e6a12006-01-12 13:30:04 -0800629 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy9c547952007-12-17 21:52:00 -0800630 m->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 "ipt_%s", m->u.user.name);
632 if (IS_ERR(match) || !match) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800633 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return match ? PTR_ERR(match) : -ENOENT;
635 }
636 m->u.kernel.match = match;
637
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200638 ret = check_match(m, par, i);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800639 if (ret)
640 goto err;
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800643err:
644 module_put(m->u.kernel.match->me);
645 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Denys Vlasenko022748a2008-01-14 23:44:05 -0800648static int check_target(struct ipt_entry *e, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800649{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200650 struct ipt_entry_target *t = ipt_get_target(e);
651 struct xt_tgchk_param par = {
652 .table = name,
653 .entryinfo = e,
654 .target = t->u.kernel.target,
655 .targinfo = t->data,
656 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200657 .family = NFPROTO_IPV4,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200658 };
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900659 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800660
Jan Engelhardt916a9172008-10-08 11:35:20 +0200661 ret = xt_check_target(&par, t->u.target_size - sizeof(*t),
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200662 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200663 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800664 duprintf("ip_tables: check failed for `%s'.\n",
665 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200666 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800667 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200668 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800669}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Denys Vlasenko022748a2008-01-14 23:44:05 -0800671static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800672find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
Patrick McHardy4b478242007-12-17 21:46:15 -0800673 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800676 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int ret;
678 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200679 struct xt_mtchk_param mtpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Dmitry Mishina96be242006-12-12 00:29:26 -0800681 ret = check_entry(e, name);
682 if (ret)
683 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200686 mtpar.table = name;
687 mtpar.entryinfo = &e->ip;
688 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200689 mtpar.family = NFPROTO_IPV4;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200690 ret = IPT_MATCH_ITERATE(e, find_check_match, &mtpar, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (ret != 0)
692 goto cleanup_matches;
693
694 t = ipt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800695 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -0800696 t->u.user.name,
697 t->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 "ipt_%s", t->u.user.name);
699 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800700 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 ret = target ? PTR_ERR(target) : -ENOENT;
702 goto cleanup_matches;
703 }
704 t->u.kernel.target = target;
705
Dmitry Mishina96be242006-12-12 00:29:26 -0800706 ret = check_target(e, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800707 if (ret)
708 goto err;
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 (*i)++;
711 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800712 err:
713 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 cleanup_matches:
715 IPT_MATCH_ITERATE(e, cleanup_match, &j);
716 return ret;
717}
718
Denys Vlasenko022748a2008-01-14 23:44:05 -0800719static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800721 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unsigned char *base,
723 unsigned char *limit,
724 const unsigned int *hook_entries,
725 const unsigned int *underflows,
726 unsigned int *i)
727{
728 unsigned int h;
729
730 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0
731 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
732 duprintf("Bad offset %p\n", e);
733 return -EINVAL;
734 }
735
736 if (e->next_offset
737 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
738 duprintf("checking: element %p size %u\n",
739 e, e->next_offset);
740 return -EINVAL;
741 }
742
743 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800744 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if ((unsigned char *)e - base == hook_entries[h])
746 newinfo->hook_entry[h] = hook_entries[h];
747 if ((unsigned char *)e - base == underflows[h])
748 newinfo->underflow[h] = underflows[h];
749 }
750
751 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900752 < 0 (not IPT_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800755 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 e->comefrom = 0;
757
758 (*i)++;
759 return 0;
760}
761
Denys Vlasenko022748a2008-01-14 23:44:05 -0800762static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763cleanup_entry(struct ipt_entry *e, unsigned int *i)
764{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200765 struct xt_tgdtor_param par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct ipt_entry_target *t;
767
768 if (i && (*i)-- == 0)
769 return 1;
770
771 /* Cleanup all matches */
772 IPT_MATCH_ITERATE(e, cleanup_match, NULL);
773 t = ipt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200774
775 par.target = t->u.kernel.target;
776 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200777 par.family = NFPROTO_IPV4;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200778 if (par.target->destroy != NULL)
779 par.target->destroy(&par);
780 module_put(par.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return 0;
782}
783
784/* Checks and translates the user-supplied table segment (held in
785 newinfo) */
786static int
787translate_table(const char *name,
788 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800789 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800790 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 unsigned int size,
792 unsigned int number,
793 const unsigned int *hook_entries,
794 const unsigned int *underflows)
795{
796 unsigned int i;
797 int ret;
798
799 newinfo->size = size;
800 newinfo->number = number;
801
802 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800803 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 newinfo->hook_entry[i] = 0xFFFFFFFF;
805 newinfo->underflow[i] = 0xFFFFFFFF;
806 }
807
808 duprintf("translate_table: size %u\n", newinfo->size);
809 i = 0;
810 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800811 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 check_entry_size_and_hooks,
813 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800814 entry0,
815 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 hook_entries, underflows, &i);
817 if (ret != 0)
818 return ret;
819
820 if (i != number) {
821 duprintf("translate_table: %u not %u entries\n",
822 i, number);
823 return -EINVAL;
824 }
825
826 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800827 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* Only hooks which are valid */
829 if (!(valid_hooks & (1 << i)))
830 continue;
831 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
832 duprintf("Invalid hook entry %u %u\n",
833 i, hook_entries[i]);
834 return -EINVAL;
835 }
836 if (newinfo->underflow[i] == 0xFFFFFFFF) {
837 duprintf("Invalid underflow %u %u\n",
838 i, underflows[i]);
839 return -EINVAL;
840 }
841 }
842
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800843 if (!mark_source_chains(newinfo, valid_hooks, entry0))
844 return -ELOOP;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* Finally, each sanity check must pass */
847 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800848 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Dmitry Mishina96be242006-12-12 00:29:26 -0800849 find_check_entry, name, size, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800851 if (ret != 0) {
852 IPT_ENTRY_ITERATE(entry0, newinfo->size,
853 cleanup_entry, &i);
854 return ret;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700858 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800859 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
860 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
863 return ret;
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/* Gets counters. */
867static inline int
868add_entry_to_counter(const struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800869 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 unsigned int *i)
871{
872 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
873
874 (*i)++;
875 return 0;
876}
877
Eric Dumazet31836062005-12-13 23:13:48 -0800878static inline int
879set_entry_to_counter(const struct ipt_entry *e,
880 struct ipt_counters total[],
881 unsigned int *i)
882{
883 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
884
885 (*i)++;
886 return 0;
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800890get_counters(const struct xt_table_info *t,
891 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 unsigned int cpu;
894 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800895 unsigned int curcpu;
896
897 /* Instead of clearing (by a previous call to memset())
898 * the counters and using adds, we set the counters
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700899 * with data used by 'current' CPU.
900 *
901 * Bottom half has to be disabled to prevent deadlock
902 * if new softirq were to run and call ipt_do_table
Eric Dumazet31836062005-12-13 23:13:48 -0800903 */
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700904 local_bh_disable();
905 curcpu = smp_processor_id();
Eric Dumazet31836062005-12-13 23:13:48 -0800906
907 i = 0;
908 IPT_ENTRY_ITERATE(t->entries[curcpu],
909 t->size,
910 set_entry_to_counter,
911 counters,
912 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700914 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800915 if (cpu == curcpu)
916 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 i = 0;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700918 xt_info_wrlock(cpu);
Eric Dumazet31836062005-12-13 23:13:48 -0800919 IPT_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 t->size,
921 add_entry_to_counter,
922 counters,
923 &i);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700924 xt_info_wrunlock(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100926 local_bh_enable();
927}
928
Denys Vlasenko022748a2008-01-14 23:44:05 -0800929static struct xt_counters * alloc_counters(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Dmitry Mishin27229712006-04-01 02:25:19 -0800931 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800932 struct xt_counters *counters;
Stephen Hemminger78454472009-02-20 10:35:32 +0100933 struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* We need atomic snapshot of counters: rest doesn't change
936 (other than comefrom, which userspace doesn't care
937 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800938 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet31836062005-12-13 23:13:48 -0800939 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700942 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700944 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Dmitry Mishin27229712006-04-01 02:25:19 -0800946 return counters;
947}
948
949static int
950copy_entries_to_user(unsigned int total_size,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800951 struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800952 void __user *userptr)
953{
954 unsigned int off, num;
955 struct ipt_entry *e;
956 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200957 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800958 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200959 const void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -0800960
961 counters = alloc_counters(table);
962 if (IS_ERR(counters))
963 return PTR_ERR(counters);
964
Eric Dumazet31836062005-12-13 23:13:48 -0800965 /* choose the copy that is on our node/cpu, ...
966 * This choice is lazy (because current thread is
967 * allowed to migrate to another cpu)
968 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800969 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800970 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 ret = -EFAULT;
972 goto free_counters;
973 }
974
975 /* FIXME: use iterator macros --RR */
976 /* ... then go back and fix counters and names */
977 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
978 unsigned int i;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200979 const struct ipt_entry_match *m;
980 const struct ipt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Eric Dumazet31836062005-12-13 23:13:48 -0800982 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (copy_to_user(userptr + off
984 + offsetof(struct ipt_entry, counters),
985 &counters[num],
986 sizeof(counters[num])) != 0) {
987 ret = -EFAULT;
988 goto free_counters;
989 }
990
991 for (i = sizeof(struct ipt_entry);
992 i < e->target_offset;
993 i += m->u.match_size) {
994 m = (void *)e + i;
995
996 if (copy_to_user(userptr + off + i
997 + offsetof(struct ipt_entry_match,
998 u.user.name),
999 m->u.kernel.match->name,
1000 strlen(m->u.kernel.match->name)+1)
1001 != 0) {
1002 ret = -EFAULT;
1003 goto free_counters;
1004 }
1005 }
1006
1007 t = ipt_get_target(e);
1008 if (copy_to_user(userptr + off + e->target_offset
1009 + offsetof(struct ipt_entry_target,
1010 u.user.name),
1011 t->u.kernel.target->name,
1012 strlen(t->u.kernel.target->name)+1) != 0) {
1013 ret = -EFAULT;
1014 goto free_counters;
1015 }
1016 }
1017
1018 free_counters:
1019 vfree(counters);
1020 return ret;
1021}
1022
Dmitry Mishin27229712006-04-01 02:25:19 -08001023#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001024static void compat_standard_from_user(void *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001025{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001026 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001027
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001028 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001029 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001030 memcpy(dst, &v, sizeof(v));
1031}
1032
1033static int compat_standard_to_user(void __user *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001034{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001035 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001036
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001037 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001038 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001039 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001040}
1041
1042static inline int
Patrick McHardy4b478242007-12-17 21:46:15 -08001043compat_calc_match(struct ipt_entry_match *m, int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001044{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001045 *size += xt_compat_match_offset(m->u.kernel.match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001046 return 0;
1047}
1048
Eric Dumazet259d4e42007-12-04 23:24:56 -08001049static int compat_calc_entry(struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001050 const struct xt_table_info *info,
1051 void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001052{
1053 struct ipt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001054 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001055 int off, i, ret;
1056
Patrick McHardy30c08c42007-12-17 21:47:14 -08001057 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001058 entry_offset = (void *)e - base;
1059 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
1060 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001061 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001062 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001063 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001064 if (ret)
1065 return ret;
1066
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001067 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001068 if (info->hook_entry[i] &&
1069 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001070 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001071 if (info->underflow[i] &&
1072 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001073 newinfo->underflow[i] -= off;
1074 }
1075 return 0;
1076}
1077
Eric Dumazet259d4e42007-12-04 23:24:56 -08001078static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001079 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001080{
1081 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001082
1083 if (!newinfo || !info)
1084 return -EINVAL;
1085
Eric Dumazet259d4e42007-12-04 23:24:56 -08001086 /* we dont care about newinfo->entries[] */
1087 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1088 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001089 loc_cpu_entry = info->entries[raw_smp_processor_id()];
1090 return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001091 compat_calc_entry, info, loc_cpu_entry,
1092 newinfo);
Dmitry Mishin27229712006-04-01 02:25:19 -08001093}
1094#endif
1095
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001096static int get_info(struct net *net, void __user *user, int *len, int compat)
Dmitry Mishin27229712006-04-01 02:25:19 -08001097{
1098 char name[IPT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001099 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001100 int ret;
1101
1102 if (*len != sizeof(struct ipt_getinfo)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001103 duprintf("length %u != %zu\n", *len,
1104 sizeof(struct ipt_getinfo));
Dmitry Mishin27229712006-04-01 02:25:19 -08001105 return -EINVAL;
1106 }
1107
1108 if (copy_from_user(name, user, sizeof(name)) != 0)
1109 return -EFAULT;
1110
1111 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1112#ifdef CONFIG_COMPAT
1113 if (compat)
1114 xt_compat_lock(AF_INET);
1115#endif
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001116 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001117 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001118 if (t && !IS_ERR(t)) {
1119 struct ipt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001120 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001121
1122#ifdef CONFIG_COMPAT
1123 if (compat) {
1124 struct xt_table_info tmp;
1125 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001126 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001127 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001128 }
1129#endif
1130 info.valid_hooks = t->valid_hooks;
1131 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001132 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001133 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001134 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001135 info.num_entries = private->number;
1136 info.size = private->size;
1137 strcpy(info.name, name);
1138
1139 if (copy_to_user(user, &info, *len) != 0)
1140 ret = -EFAULT;
1141 else
1142 ret = 0;
1143
1144 xt_table_unlock(t);
1145 module_put(t->me);
1146 } else
1147 ret = t ? PTR_ERR(t) : -ENOENT;
1148#ifdef CONFIG_COMPAT
1149 if (compat)
1150 xt_compat_unlock(AF_INET);
1151#endif
1152 return ret;
1153}
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001156get_entries(struct net *net, struct ipt_get_entries __user *uptr, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001159 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001160 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Dmitry Mishin27229712006-04-01 02:25:19 -08001162 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001163 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001164 return -EINVAL;
1165 }
1166 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1167 return -EFAULT;
1168 if (*len != sizeof(struct ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001169 duprintf("get_entries: %u != %zu\n",
1170 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001171 return -EINVAL;
1172 }
1173
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001174 t = xt_find_table_lock(net, AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001176 const struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001177 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001178 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001179 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 t, uptr->entrytable);
1181 else {
1182 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001183 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001184 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001187 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 } else
1189 ret = t ? PTR_ERR(t) : -ENOENT;
1190
1191 return ret;
1192}
1193
1194static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001195__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001196 struct xt_table_info *newinfo, unsigned int num_counters,
1197 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001198{
1199 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001200 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001201 struct xt_table_info *oldinfo;
1202 struct xt_counters *counters;
1203 void *loc_cpu_old_entry;
1204
1205 ret = 0;
1206 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1207 if (!counters) {
1208 ret = -ENOMEM;
1209 goto out;
1210 }
1211
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001212 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Dmitry Mishin27229712006-04-01 02:25:19 -08001213 "iptable_%s", name);
1214 if (!t || IS_ERR(t)) {
1215 ret = t ? PTR_ERR(t) : -ENOENT;
1216 goto free_newinfo_counters_untrans;
1217 }
1218
1219 /* You lied! */
1220 if (valid_hooks != t->valid_hooks) {
1221 duprintf("Valid hook crap: %08X vs %08X\n",
1222 valid_hooks, t->valid_hooks);
1223 ret = -EINVAL;
1224 goto put_module;
1225 }
1226
1227 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1228 if (!oldinfo)
1229 goto put_module;
1230
1231 /* Update module usage count based on number of rules */
1232 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1233 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1234 if ((oldinfo->number > oldinfo->initial_entries) ||
1235 (newinfo->number <= oldinfo->initial_entries))
1236 module_put(t->me);
1237 if ((oldinfo->number > oldinfo->initial_entries) &&
1238 (newinfo->number <= oldinfo->initial_entries))
1239 module_put(t->me);
1240
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001241 /* Get the old counters, and synchronize with replace */
Dmitry Mishin27229712006-04-01 02:25:19 -08001242 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001243
Dmitry Mishin27229712006-04-01 02:25:19 -08001244 /* Decrease module usage counts and free resource */
1245 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Patrick McHardy4b478242007-12-17 21:46:15 -08001246 IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1247 NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001248 xt_free_table_info(oldinfo);
1249 if (copy_to_user(counters_ptr, counters,
1250 sizeof(struct xt_counters) * num_counters) != 0)
1251 ret = -EFAULT;
1252 vfree(counters);
1253 xt_table_unlock(t);
1254 return ret;
1255
1256 put_module:
1257 module_put(t->me);
1258 xt_table_unlock(t);
1259 free_newinfo_counters_untrans:
1260 vfree(counters);
1261 out:
1262 return ret;
1263}
1264
1265static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001266do_replace(struct net *net, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 int ret;
1269 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001270 struct xt_table_info *newinfo;
1271 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1274 return -EFAULT;
1275
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001276 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001277 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1278 return -ENOMEM;
1279
Harald Welte2e4e6a12006-01-12 13:30:04 -08001280 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (!newinfo)
1282 return -ENOMEM;
1283
Patrick McHardy9c547952007-12-17 21:52:00 -08001284 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001285 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1286 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 tmp.size) != 0) {
1288 ret = -EFAULT;
1289 goto free_newinfo;
1290 }
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001293 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 tmp.hook_entry, tmp.underflow);
1295 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001296 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 duprintf("ip_tables: Translated table\n");
1299
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001300 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001301 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001302 if (ret)
1303 goto free_newinfo_untrans;
1304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Dmitry Mishin27229712006-04-01 02:25:19 -08001306 free_newinfo_untrans:
Patrick McHardy9c547952007-12-17 21:52:00 -08001307 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001309 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return ret;
1311}
1312
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001313/* We're lazy, and add to the first CPU; overflow works its fey magic
1314 * and everything is OK. */
1315static int
1316add_counter_to_entry(struct ipt_entry *e,
1317 const struct xt_counters addme[],
1318 unsigned int *i)
1319{
1320 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1321
1322 (*i)++;
1323 return 0;
1324}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001327do_add_counters(struct net *net, void __user *user, unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001329 unsigned int i, curcpu;
Dmitry Mishin27229712006-04-01 02:25:19 -08001330 struct xt_counters_info tmp;
1331 struct xt_counters *paddc;
1332 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001333 const char *name;
Dmitry Mishin27229712006-04-01 02:25:19 -08001334 int size;
1335 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001336 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001337 const struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001339 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001340#ifdef CONFIG_COMPAT
1341 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Dmitry Mishin27229712006-04-01 02:25:19 -08001343 if (compat) {
1344 ptmp = &compat_tmp;
1345 size = sizeof(struct compat_xt_counters_info);
1346 } else
1347#endif
1348 {
1349 ptmp = &tmp;
1350 size = sizeof(struct xt_counters_info);
1351 }
1352
1353 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EFAULT;
1355
Dmitry Mishin27229712006-04-01 02:25:19 -08001356#ifdef CONFIG_COMPAT
1357 if (compat) {
1358 num_counters = compat_tmp.num_counters;
1359 name = compat_tmp.name;
1360 } else
1361#endif
1362 {
1363 num_counters = tmp.num_counters;
1364 name = tmp.name;
1365 }
1366
1367 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return -EINVAL;
1369
Dmitry Mishin27229712006-04-01 02:25:19 -08001370 paddc = vmalloc_node(len - size, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (!paddc)
1372 return -ENOMEM;
1373
Dmitry Mishin27229712006-04-01 02:25:19 -08001374 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 ret = -EFAULT;
1376 goto free;
1377 }
1378
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001379 t = xt_find_table_lock(net, AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (!t || IS_ERR(t)) {
1381 ret = t ? PTR_ERR(t) : -ENOENT;
1382 goto free;
1383 }
1384
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001385 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001386 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001387 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ret = -EINVAL;
1389 goto unlock_up_free;
1390 }
1391
1392 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001393 /* Choose the copy that is on our node */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001394 curcpu = smp_processor_id();
1395 loc_cpu_entry = private->entries[curcpu];
1396 xt_info_wrlock(curcpu);
Eric Dumazet31836062005-12-13 23:13:48 -08001397 IPT_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001398 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 add_counter_to_entry,
Dmitry Mishin27229712006-04-01 02:25:19 -08001400 paddc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 &i);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001402 xt_info_wrunlock(curcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001404 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001405 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 module_put(t->me);
1407 free:
1408 vfree(paddc);
1409
1410 return ret;
1411}
1412
Dmitry Mishin27229712006-04-01 02:25:19 -08001413#ifdef CONFIG_COMPAT
1414struct compat_ipt_replace {
1415 char name[IPT_TABLE_MAXNAMELEN];
1416 u32 valid_hooks;
1417 u32 num_entries;
1418 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001419 u32 hook_entry[NF_INET_NUMHOOKS];
1420 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001421 u32 num_counters;
1422 compat_uptr_t counters; /* struct ipt_counters * */
1423 struct compat_ipt_entry entries[0];
1424};
1425
Patrick McHardya18aa312007-12-12 10:35:16 -08001426static int
1427compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001428 unsigned int *size, struct xt_counters *counters,
Patrick McHardya18aa312007-12-12 10:35:16 -08001429 unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001430{
Al Viro3e597c62006-09-24 23:42:20 +01001431 struct ipt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001432 struct compat_ipt_entry __user *ce;
1433 u_int16_t target_offset, next_offset;
1434 compat_uint_t origsize;
1435 int ret;
1436
1437 ret = -EFAULT;
1438 origsize = *size;
1439 ce = (struct compat_ipt_entry __user *)*dstptr;
Patrick McHardy78000072006-05-03 23:20:27 -07001440 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
Dmitry Mishin27229712006-04-01 02:25:19 -08001441 goto out;
1442
Patrick McHardya18aa312007-12-12 10:35:16 -08001443 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1444 goto out;
1445
Dmitry Mishin27229712006-04-01 02:25:19 -08001446 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001447 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1448
Patrick McHardyac8e27f2007-12-17 21:45:52 -08001449 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001450 target_offset = e->target_offset - (origsize - *size);
1451 if (ret)
1452 goto out;
1453 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001454 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001455 if (ret)
1456 goto out;
1457 ret = -EFAULT;
1458 next_offset = e->next_offset - (origsize - *size);
Patrick McHardy78000072006-05-03 23:20:27 -07001459 if (put_user(target_offset, &ce->target_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001460 goto out;
Patrick McHardy78000072006-05-03 23:20:27 -07001461 if (put_user(next_offset, &ce->next_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001462 goto out;
Patrick McHardya18aa312007-12-12 10:35:16 -08001463
1464 (*i)++;
Dmitry Mishin27229712006-04-01 02:25:19 -08001465 return 0;
1466out:
1467 return ret;
1468}
1469
Denys Vlasenko022748a2008-01-14 23:44:05 -08001470static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001471compat_find_calc_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001472 const char *name,
1473 const struct ipt_ip *ip,
1474 unsigned int hookmask,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001475 int *size, unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001476{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001477 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001478
1479 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001480 m->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001481 "ipt_%s", m->u.user.name);
1482 if (IS_ERR(match) || !match) {
1483 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001484 m->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001485 return match ? PTR_ERR(match) : -ENOENT;
1486 }
1487 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001488 *size += xt_compat_match_offset(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001489
1490 (*i)++;
1491 return 0;
1492}
1493
Denys Vlasenko022748a2008-01-14 23:44:05 -08001494static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001495compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1496{
1497 if (i && (*i)-- == 0)
1498 return 1;
1499
1500 module_put(m->u.kernel.match->me);
1501 return 0;
1502}
1503
Denys Vlasenko022748a2008-01-14 23:44:05 -08001504static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001505compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001506{
1507 struct ipt_entry_target *t;
1508
1509 if (i && (*i)-- == 0)
1510 return 1;
1511
1512 /* Cleanup all matches */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001513 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1514 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001515 module_put(t->u.kernel.target->me);
1516 return 0;
1517}
1518
Denys Vlasenko022748a2008-01-14 23:44:05 -08001519static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001520check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001521 struct xt_table_info *newinfo,
1522 unsigned int *size,
1523 unsigned char *base,
1524 unsigned char *limit,
1525 unsigned int *hook_entries,
1526 unsigned int *underflows,
1527 unsigned int *i,
1528 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001529{
1530 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001531 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001532 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001533 unsigned int j;
1534 int ret, off, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001535
1536 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1537 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0
1538 || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1539 duprintf("Bad offset %p, limit = %p\n", e, limit);
1540 return -EINVAL;
1541 }
1542
1543 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001544 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001545 duprintf("checking: element %p size %u\n",
1546 e, e->next_offset);
1547 return -EINVAL;
1548 }
1549
Patrick McHardy73cd5982007-12-17 21:47:32 -08001550 /* For purposes of check_entry casting the compat entry is fine */
1551 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001552 if (ret)
1553 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001554
Patrick McHardy30c08c42007-12-17 21:47:14 -08001555 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001556 entry_offset = (void *)e - (void *)base;
1557 j = 0;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001558 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1559 &e->ip, e->comefrom, &off, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001560 if (ret != 0)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001561 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001562
Patrick McHardy73cd5982007-12-17 21:47:32 -08001563 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001564 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -08001565 t->u.user.name,
1566 t->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001567 "ipt_%s", t->u.user.name);
1568 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001569 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001570 t->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001571 ret = target ? PTR_ERR(target) : -ENOENT;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001572 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001573 }
1574 t->u.kernel.target = target;
1575
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001576 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001577 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001578 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001579 if (ret)
1580 goto out;
1581
1582 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001583 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001584 if ((unsigned char *)e - base == hook_entries[h])
1585 newinfo->hook_entry[h] = hook_entries[h];
1586 if ((unsigned char *)e - base == underflows[h])
1587 newinfo->underflow[h] = underflows[h];
1588 }
1589
1590 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001591 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001592 e->comefrom = 0;
1593
1594 (*i)++;
1595 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001596
Dmitry Mishin27229712006-04-01 02:25:19 -08001597out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001598 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001599release_matches:
1600 IPT_MATCH_ITERATE(e, compat_release_match, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001601 return ret;
1602}
1603
Patrick McHardy4b478242007-12-17 21:46:15 -08001604static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001605compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001606 unsigned int *size, const char *name,
1607 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001608{
1609 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001610 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001611 struct ipt_entry *de;
1612 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001613 int ret, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001614
1615 ret = 0;
1616 origsize = *size;
1617 de = (struct ipt_entry *)*dstptr;
1618 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001619 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001620
Patrick McHardy73cd5982007-12-17 21:47:32 -08001621 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001622 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1623
Patrick McHardy73cd5982007-12-17 21:47:32 -08001624 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1625 dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001626 if (ret)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001627 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001628 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001629 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001630 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001631 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001632
1633 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001634 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001635 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1636 newinfo->hook_entry[h] -= origsize - *size;
1637 if ((unsigned char *)de - base < newinfo->underflow[h])
1638 newinfo->underflow[h] -= origsize - *size;
1639 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001640 return ret;
1641}
Dmitry Mishin27229712006-04-01 02:25:19 -08001642
Denys Vlasenko022748a2008-01-14 23:44:05 -08001643static int
1644compat_check_entry(struct ipt_entry *e, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001645 unsigned int *i)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001646{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001647 struct xt_mtchk_param mtpar;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001648 unsigned int j;
1649 int ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001650
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001651 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001652 mtpar.table = name;
1653 mtpar.entryinfo = &e->ip;
1654 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +02001655 mtpar.family = NFPROTO_IPV4;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001656 ret = IPT_MATCH_ITERATE(e, check_match, &mtpar, &j);
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001657 if (ret)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001658 goto cleanup_matches;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001659
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001660 ret = check_target(e, name);
1661 if (ret)
1662 goto cleanup_matches;
1663
1664 (*i)++;
1665 return 0;
1666
1667 cleanup_matches:
1668 IPT_MATCH_ITERATE(e, cleanup_match, &j);
1669 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001670}
1671
Dmitry Mishin27229712006-04-01 02:25:19 -08001672static int
1673translate_compat_table(const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001674 unsigned int valid_hooks,
1675 struct xt_table_info **pinfo,
1676 void **pentry0,
1677 unsigned int total_size,
1678 unsigned int number,
1679 unsigned int *hook_entries,
1680 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001681{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001682 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001683 struct xt_table_info *newinfo, *info;
1684 void *pos, *entry0, *entry1;
1685 unsigned int size;
1686 int ret;
1687
1688 info = *pinfo;
1689 entry0 = *pentry0;
1690 size = total_size;
1691 info->number = number;
1692
1693 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001694 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001695 info->hook_entry[i] = 0xFFFFFFFF;
1696 info->underflow[i] = 0xFFFFFFFF;
1697 }
1698
1699 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001700 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001701 xt_compat_lock(AF_INET);
1702 /* Walk through entries, checking offsets. */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001703 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1704 check_compat_entry_size_and_hooks,
1705 info, &size, entry0,
1706 entry0 + total_size,
1707 hook_entries, underflows, &j, name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001708 if (ret != 0)
1709 goto out_unlock;
1710
1711 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001712 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001713 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001714 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001715 goto out_unlock;
1716 }
1717
1718 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001719 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001720 /* Only hooks which are valid */
1721 if (!(valid_hooks & (1 << i)))
1722 continue;
1723 if (info->hook_entry[i] == 0xFFFFFFFF) {
1724 duprintf("Invalid hook entry %u %u\n",
1725 i, hook_entries[i]);
1726 goto out_unlock;
1727 }
1728 if (info->underflow[i] == 0xFFFFFFFF) {
1729 duprintf("Invalid underflow %u %u\n",
1730 i, underflows[i]);
1731 goto out_unlock;
1732 }
1733 }
1734
1735 ret = -ENOMEM;
1736 newinfo = xt_alloc_table_info(size);
1737 if (!newinfo)
1738 goto out_unlock;
1739
1740 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001741 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001742 newinfo->hook_entry[i] = info->hook_entry[i];
1743 newinfo->underflow[i] = info->underflow[i];
1744 }
1745 entry1 = newinfo->entries[raw_smp_processor_id()];
1746 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001747 size = total_size;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001748 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
Patrick McHardy9c547952007-12-17 21:52:00 -08001749 compat_copy_entry_from_user,
1750 &pos, &size, name, newinfo, entry1);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001751 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001752 xt_compat_unlock(AF_INET);
1753 if (ret)
1754 goto free_newinfo;
1755
1756 ret = -ELOOP;
1757 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1758 goto free_newinfo;
1759
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001760 i = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001761 ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001762 name, &i);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001763 if (ret) {
1764 j -= i;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001765 COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1766 compat_release_entry, &j);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001767 IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1768 xt_free_table_info(newinfo);
1769 return ret;
1770 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001771
Dmitry Mishin27229712006-04-01 02:25:19 -08001772 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001773 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001774 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1775 memcpy(newinfo->entries[i], entry1, newinfo->size);
1776
1777 *pinfo = newinfo;
1778 *pentry0 = entry1;
1779 xt_free_table_info(info);
1780 return 0;
1781
1782free_newinfo:
1783 xt_free_table_info(newinfo);
1784out:
Patrick McHardy73cd5982007-12-17 21:47:32 -08001785 COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001786 return ret;
1787out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001788 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001789 xt_compat_unlock(AF_INET);
1790 goto out;
1791}
1792
1793static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001794compat_do_replace(struct net *net, void __user *user, unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001795{
1796 int ret;
1797 struct compat_ipt_replace tmp;
1798 struct xt_table_info *newinfo;
1799 void *loc_cpu_entry;
1800
1801 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1802 return -EFAULT;
1803
Dmitry Mishin27229712006-04-01 02:25:19 -08001804 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001805 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001806 return -ENOMEM;
1807 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1808 return -ENOMEM;
1809
1810 newinfo = xt_alloc_table_info(tmp.size);
1811 if (!newinfo)
1812 return -ENOMEM;
1813
Patrick McHardy9c547952007-12-17 21:52:00 -08001814 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001815 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1816 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1817 tmp.size) != 0) {
1818 ret = -EFAULT;
1819 goto free_newinfo;
1820 }
1821
1822 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001823 &newinfo, &loc_cpu_entry, tmp.size,
1824 tmp.num_entries, tmp.hook_entry,
1825 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001826 if (ret != 0)
1827 goto free_newinfo;
1828
1829 duprintf("compat_do_replace: Translated table\n");
1830
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001831 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001832 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001833 if (ret)
1834 goto free_newinfo_untrans;
1835 return 0;
1836
1837 free_newinfo_untrans:
Patrick McHardy4b478242007-12-17 21:46:15 -08001838 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001839 free_newinfo:
1840 xt_free_table_info(newinfo);
1841 return ret;
1842}
1843
1844static int
1845compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001846 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001847{
1848 int ret;
1849
1850 if (!capable(CAP_NET_ADMIN))
1851 return -EPERM;
1852
1853 switch (cmd) {
1854 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001855 ret = compat_do_replace(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001856 break;
1857
1858 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001859 ret = do_add_counters(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001860 break;
1861
1862 default:
1863 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1864 ret = -EINVAL;
1865 }
1866
1867 return ret;
1868}
1869
Patrick McHardy4b478242007-12-17 21:46:15 -08001870struct compat_ipt_get_entries {
Dmitry Mishin27229712006-04-01 02:25:19 -08001871 char name[IPT_TABLE_MAXNAMELEN];
1872 compat_uint_t size;
1873 struct compat_ipt_entry entrytable[0];
1874};
1875
Patrick McHardy4b478242007-12-17 21:46:15 -08001876static int
1877compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1878 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001879{
Dmitry Mishin27229712006-04-01 02:25:19 -08001880 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001881 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001882 void __user *pos;
1883 unsigned int size;
1884 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001885 const void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001886 unsigned int i = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001887
1888 counters = alloc_counters(table);
1889 if (IS_ERR(counters))
1890 return PTR_ERR(counters);
1891
1892 /* choose the copy that is on our node/cpu, ...
1893 * This choice is lazy (because current thread is
1894 * allowed to migrate to another cpu)
1895 */
1896 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1897 pos = userptr;
1898 size = total_size;
1899 ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
Patrick McHardya18aa312007-12-12 10:35:16 -08001900 compat_copy_entry_to_user,
1901 &pos, &size, counters, &i);
Dmitry Mishin27229712006-04-01 02:25:19 -08001902
Dmitry Mishin27229712006-04-01 02:25:19 -08001903 vfree(counters);
1904 return ret;
1905}
1906
1907static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001908compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1909 int *len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001910{
1911 int ret;
1912 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001913 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001914
Dmitry Mishin27229712006-04-01 02:25:19 -08001915 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001916 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001917 return -EINVAL;
1918 }
1919
1920 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1921 return -EFAULT;
1922
1923 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001924 duprintf("compat_get_entries: %u != %zu\n",
1925 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001926 return -EINVAL;
1927 }
1928
1929 xt_compat_lock(AF_INET);
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001930 t = xt_find_table_lock(net, AF_INET, get.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001931 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001932 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001933 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08001934 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001935 ret = compat_table_info(private, &info);
1936 if (!ret && get.size == info.size) {
1937 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001938 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001939 } else if (!ret) {
1940 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001941 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001942 ret = -EAGAIN;
Dmitry Mishin27229712006-04-01 02:25:19 -08001943 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001944 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001945 module_put(t->me);
1946 xt_table_unlock(t);
1947 } else
1948 ret = t ? PTR_ERR(t) : -ENOENT;
1949
1950 xt_compat_unlock(AF_INET);
1951 return ret;
1952}
1953
Patrick McHardy79030ed2006-09-20 12:05:08 -07001954static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1955
Dmitry Mishin27229712006-04-01 02:25:19 -08001956static int
1957compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1958{
1959 int ret;
1960
Björn Steinbrink82fac052006-10-20 00:21:10 -07001961 if (!capable(CAP_NET_ADMIN))
1962 return -EPERM;
1963
Dmitry Mishin27229712006-04-01 02:25:19 -08001964 switch (cmd) {
1965 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001966 ret = get_info(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001967 break;
1968 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001969 ret = compat_get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001970 break;
1971 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001972 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001973 }
1974 return ret;
1975}
1976#endif
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001979do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 int ret;
1982
1983 if (!capable(CAP_NET_ADMIN))
1984 return -EPERM;
1985
1986 switch (cmd) {
1987 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001988 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 break;
1990
1991 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001992 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 break;
1994
1995 default:
1996 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1997 ret = -EINVAL;
1998 }
1999
2000 return ret;
2001}
2002
2003static int
2004do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2005{
2006 int ret;
2007
2008 if (!capable(CAP_NET_ADMIN))
2009 return -EPERM;
2010
2011 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002012 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002013 ret = get_info(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002015
2016 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002017 ret = get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08002018 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 case IPT_SO_GET_REVISION_MATCH:
2021 case IPT_SO_GET_REVISION_TARGET: {
2022 struct ipt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002023 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 if (*len != sizeof(rev)) {
2026 ret = -EINVAL;
2027 break;
2028 }
2029 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2030 ret = -EFAULT;
2031 break;
2032 }
2033
2034 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002035 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002037 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Harald Welte2e4e6a12006-01-12 13:30:04 -08002039 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2040 rev.revision,
2041 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 "ipt_%s", rev.name);
2043 break;
2044 }
2045
2046 default:
2047 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2048 ret = -EINVAL;
2049 }
2050
2051 return ret;
2052}
2053
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002054struct xt_table *ipt_register_table(struct net *net, struct xt_table *table,
2055 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002058 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08002059 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08002061 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08002062 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Harald Welte2e4e6a12006-01-12 13:30:04 -08002064 newinfo = xt_alloc_table_info(repl->size);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002065 if (!newinfo) {
2066 ret = -ENOMEM;
2067 goto out;
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Patrick McHardy9c547952007-12-17 21:52:00 -08002070 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002071 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2072 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08002075 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 repl->num_entries,
2077 repl->hook_entry,
2078 repl->underflow);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002079 if (ret != 0)
2080 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002082 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08002083 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002084 ret = PTR_ERR(new_table);
2085 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002088 return new_table;
2089
2090out_free:
2091 xt_free_table_info(newinfo);
2092out:
2093 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Jan Engelhardte60a13e2007-02-07 15:12:33 -08002096void ipt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002098 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002099 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08002100 struct module *table_owner = table->me;
Eric Dumazet31836062005-12-13 23:13:48 -08002101
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002102 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002105 loc_cpu_entry = private->entries[raw_smp_processor_id()];
2106 IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
Alexey Dobriyandf200962008-01-31 04:05:34 -08002107 if (private->number > private->initial_entries)
2108 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002109 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
2112/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002113static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2115 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002116 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Patrick McHardy9c547952007-12-17 21:52:00 -08002118 return ((test_type == 0xFF) ||
2119 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 ^ invert;
2121}
2122
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002123static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002124icmp_match(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Jan Engelhardt5452e422008-04-14 11:15:35 +02002126 const struct icmphdr *ic;
2127 struct icmphdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002128 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002131 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002132 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002134 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (ic == NULL) {
2136 /* We've been asked to examine this packet, and we
2137 * can't. Hence, no choice but to drop.
2138 */
2139 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002140 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002141 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143
2144 return icmp_type_code_match(icmpinfo->type,
2145 icmpinfo->code[0],
2146 icmpinfo->code[1],
2147 ic->type, ic->code,
2148 !!(icmpinfo->invflags&IPT_ICMP_INV));
2149}
2150
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002151static bool icmp_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002153 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002155 /* Must specify no unknown invflags */
2156 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
2159/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07002160static struct xt_target ipt_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 .name = IPT_STANDARD_TARGET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002162 .targetsize = sizeof(int),
Jan Engelhardt4ba351c2009-04-14 14:28:48 +02002163 .family = NFPROTO_IPV4,
Dmitry Mishin27229712006-04-01 02:25:19 -08002164#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07002165 .compatsize = sizeof(compat_int_t),
2166 .compat_from_user = compat_standard_from_user,
2167 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002168#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169};
2170
Patrick McHardy9f15c532007-07-07 22:22:02 -07002171static struct xt_target ipt_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 .name = IPT_ERROR_TARGET,
2173 .target = ipt_error,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002174 .targetsize = IPT_FUNCTION_MAXNAMELEN,
Jan Engelhardt4ba351c2009-04-14 14:28:48 +02002175 .family = NFPROTO_IPV4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176};
2177
2178static struct nf_sockopt_ops ipt_sockopts = {
2179 .pf = PF_INET,
2180 .set_optmin = IPT_BASE_CTL,
2181 .set_optmax = IPT_SO_SET_MAX+1,
2182 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002183#ifdef CONFIG_COMPAT
2184 .compat_set = compat_do_ipt_set_ctl,
2185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 .get_optmin = IPT_BASE_CTL,
2187 .get_optmax = IPT_SO_GET_MAX+1,
2188 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002189#ifdef CONFIG_COMPAT
2190 .compat_get = compat_do_ipt_get_ctl,
2191#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002192 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193};
2194
Patrick McHardy9f15c532007-07-07 22:22:02 -07002195static struct xt_match icmp_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 .name = "icmp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002197 .match = icmp_match,
2198 .matchsize = sizeof(struct ipt_icmp),
Patrick McHardy9c547952007-12-17 21:52:00 -08002199 .checkentry = icmp_checkentry,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002200 .proto = IPPROTO_ICMP,
Jan Engelhardt4ba351c2009-04-14 14:28:48 +02002201 .family = NFPROTO_IPV4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202};
2203
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002204static int __net_init ip_tables_net_init(struct net *net)
2205{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02002206 return xt_proto_init(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002207}
2208
2209static void __net_exit ip_tables_net_exit(struct net *net)
2210{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02002211 xt_proto_fini(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002212}
2213
2214static struct pernet_operations ip_tables_net_ops = {
2215 .init = ip_tables_net_init,
2216 .exit = ip_tables_net_exit,
2217};
2218
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002219static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 int ret;
2222
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002223 ret = register_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002224 if (ret < 0)
2225 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002228 ret = xt_register_target(&ipt_standard_target);
2229 if (ret < 0)
2230 goto err2;
2231 ret = xt_register_target(&ipt_error_target);
2232 if (ret < 0)
2233 goto err3;
2234 ret = xt_register_match(&icmp_matchstruct);
2235 if (ret < 0)
2236 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 /* Register setsockopt */
2239 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002240 if (ret < 0)
2241 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Dan Aloni0236e662007-07-09 13:20:12 -07002243 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002245
2246err5:
2247 xt_unregister_match(&icmp_matchstruct);
2248err4:
2249 xt_unregister_target(&ipt_error_target);
2250err3:
2251 xt_unregister_target(&ipt_standard_target);
2252err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002253 unregister_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002254err1:
2255 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002258static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
2260 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002261
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002262 xt_unregister_match(&icmp_matchstruct);
2263 xt_unregister_target(&ipt_error_target);
2264 xt_unregister_target(&ipt_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002265
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002266 unregister_pernet_subsys(&ip_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
2269EXPORT_SYMBOL(ipt_register_table);
2270EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002272module_init(ip_tables_init);
2273module_exit(ip_tables_fini);