blob: 7ec4e40927555e45f715b7447b10f2f25dcf36f9 [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 {
357 IP_NF_ASSERT(e);
358 IP_NF_ASSERT(back);
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200359 if (ip_packet_match(ip, indev, outdev,
360 &e->ip, mtpar.fragoff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct ipt_entry_target *t;
362
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200363 if (IPT_MATCH_ITERATE(e, do_match, skb, &mtpar) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 goto no_match;
365
366 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
367
368 t = ipt_get_target(e);
369 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700370
371#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
372 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
373 /* The packet is traced: log it */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700374 if (unlikely(skb->nf_trace))
375 trace_packet(skb, hook, in, out,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700376 table->name, private, e);
377#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Standard target? */
379 if (!t->u.kernel.target->target) {
380 int v;
381
382 v = ((struct ipt_standard_target *)t)->verdict;
383 if (v < 0) {
384 /* Pop from stack? */
385 if (v != IPT_RETURN) {
386 verdict = (unsigned)(-v) - 1;
387 break;
388 }
389 e = back;
390 back = get_entry(table_base,
391 back->comefrom);
392 continue;
393 }
Jan Engelhardt98e86402009-04-15 21:06:05 +0200394 if (table_base + v != ipt_next_entry(e)
Patrick McHardy05465342005-08-21 23:31:43 -0700395 && !(e->ip.flags & IPT_F_GOTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* Save old back ptr in next entry */
397 struct ipt_entry *next
Jan Engelhardt98e86402009-04-15 21:06:05 +0200398 = ipt_next_entry(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 next->comefrom
400 = (void *)back - table_base;
401 /* set back pointer to next entry */
402 back = next;
403 }
404
405 e = get_entry(table_base, v);
406 } else {
407 /* Targets which reenter must return
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900408 abs. verdicts */
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200409 tgpar.target = t->u.kernel.target;
410 tgpar.targinfo = t->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#ifdef CONFIG_NETFILTER_DEBUG
412 ((struct ipt_entry *)table_base)->comefrom
413 = 0xeeeeeeec;
414#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700415 verdict = t->u.kernel.target->target(skb,
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200416 &tgpar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#ifdef CONFIG_NETFILTER_DEBUG
418 if (((struct ipt_entry *)table_base)->comefrom
419 != 0xeeeeeeec
420 && verdict == IPT_CONTINUE) {
421 printk("Target %s reentered!\n",
422 t->u.kernel.target->name);
423 verdict = NF_DROP;
424 }
425 ((struct ipt_entry *)table_base)->comefrom
426 = 0x57acc001;
427#endif
428 /* Target might have changed stuff. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700429 ip = ip_hdr(skb);
430 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (verdict == IPT_CONTINUE)
Jan Engelhardt98e86402009-04-15 21:06:05 +0200433 e = ipt_next_entry(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 else
435 /* Verdict */
436 break;
437 }
438 } else {
439
440 no_match:
Jan Engelhardt98e86402009-04-15 21:06:05 +0200441 e = ipt_next_entry(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 } while (!hotdrop);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700444 xt_info_rdunlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446#ifdef DEBUG_ALLOW_ALL
447 return NF_ACCEPT;
448#else
449 if (hotdrop)
450 return NF_DROP;
451 else return verdict;
452#endif
453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/* Figures out from what hook each rule can be called: returns 0 if
456 there are loops. Puts hook bitmask in comefrom. */
457static int
Harald Welte2e4e6a12006-01-12 13:30:04 -0800458mark_source_chains(struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800459 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 unsigned int hook;
462
463 /* No recursion; use packet counter to save back ptrs (reset
464 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800465 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800467 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 if (!(valid_hooks & (1 << hook)))
470 continue;
471
472 /* Set initial back pointer. */
473 e->counters.pcnt = pos;
474
475 for (;;) {
476 struct ipt_standard_target *t
477 = (void *)ipt_get_target(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800478 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800480 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 printk("iptables: loop hook %u pos %u %08X.\n",
482 hook, pos, e->comefrom);
483 return 0;
484 }
Patrick McHardy9c547952007-12-17 21:52:00 -0800485 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* Unconditional return/END. */
Al Viroe1b4b9f2006-12-12 00:29:52 -0800488 if ((e->target_offset == sizeof(struct ipt_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 && (strcmp(t->target.u.user.name,
490 IPT_STANDARD_TARGET) == 0)
491 && t->verdict < 0
Al Viroe1b4b9f2006-12-12 00:29:52 -0800492 && unconditional(&e->ip)) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 unsigned int oldpos, size;
494
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100495 if ((strcmp(t->target.u.user.name,
496 IPT_STANDARD_TARGET) == 0) &&
497 t->verdict < -NF_MAX_VERDICT - 1) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800498 duprintf("mark_source_chains: bad "
499 "negative verdict (%i)\n",
500 t->verdict);
501 return 0;
502 }
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* Return: backtrack through the last
505 big jump. */
506 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800507 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508#ifdef DEBUG_IP_FIREWALL_USER
509 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800510 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 duprintf("Back unset "
512 "on hook %u "
513 "rule %u\n",
514 hook, pos);
515 }
516#endif
517 oldpos = pos;
518 pos = e->counters.pcnt;
519 e->counters.pcnt = 0;
520
521 /* We're at the start. */
522 if (pos == oldpos)
523 goto next;
524
525 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800526 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 } while (oldpos == pos + e->next_offset);
528
529 /* Move along one */
530 size = e->next_offset;
531 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800532 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 e->counters.pcnt = pos;
534 pos += size;
535 } else {
536 int newpos = t->verdict;
537
538 if (strcmp(t->target.u.user.name,
539 IPT_STANDARD_TARGET) == 0
540 && newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800541 if (newpos > newinfo->size -
542 sizeof(struct ipt_entry)) {
543 duprintf("mark_source_chains: "
544 "bad verdict (%i)\n",
545 newpos);
546 return 0;
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* This a jump; chase it. */
549 duprintf("Jump rule %u -> %u\n",
550 pos, newpos);
551 } else {
552 /* ... this is a fallthru */
553 newpos = pos + e->next_offset;
554 }
555 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800556 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 e->counters.pcnt = pos;
558 pos = newpos;
559 }
560 }
561 next:
562 duprintf("Finished chain %u\n", hook);
563 }
564 return 1;
565}
566
Denys Vlasenko022748a2008-01-14 23:44:05 -0800567static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568cleanup_match(struct ipt_entry_match *m, unsigned int *i)
569{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200570 struct xt_mtdtor_param par;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (i && (*i)-- == 0)
573 return 1;
574
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200575 par.match = m->u.kernel.match;
576 par.matchinfo = m->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200577 par.family = NFPROTO_IPV4;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200578 if (par.match->destroy != NULL)
579 par.match->destroy(&par);
580 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0;
582}
583
Denys Vlasenko022748a2008-01-14 23:44:05 -0800584static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800585check_entry(struct ipt_entry *e, const char *name)
586{
587 struct ipt_entry_target *t;
588
589 if (!ip_checkentry(&e->ip)) {
590 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
591 return -EINVAL;
592 }
593
Patrick McHardy9c547952007-12-17 21:52:00 -0800594 if (e->target_offset + sizeof(struct ipt_entry_target) >
595 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800596 return -EINVAL;
597
598 t = ipt_get_target(e);
599 if (e->target_offset + t->u.target_size > e->next_offset)
600 return -EINVAL;
601
602 return 0;
603}
604
Denys Vlasenko022748a2008-01-14 23:44:05 -0800605static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200606check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
607 unsigned int *i)
Dmitry Mishina96be242006-12-12 00:29:26 -0800608{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200609 const struct ipt_ip *ip = par->entryinfo;
Dmitry Mishina96be242006-12-12 00:29:26 -0800610 int ret;
611
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200612 par->match = m->u.kernel.match;
613 par->matchinfo = m->data;
614
Jan Engelhardt916a9172008-10-08 11:35:20 +0200615 ret = xt_check_match(par, m->u.match_size - sizeof(*m),
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200616 ip->proto, ip->invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200617 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800618 duprintf("ip_tables: check failed for `%s'.\n",
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200619 par.match->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200620 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800621 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200622 ++*i;
623 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800624}
625
Denys Vlasenko022748a2008-01-14 23:44:05 -0800626static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200627find_check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
Patrick McHardy4b478242007-12-17 21:46:15 -0800628 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800630 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800631 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Harald Welte2e4e6a12006-01-12 13:30:04 -0800633 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy9c547952007-12-17 21:52:00 -0800634 m->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 "ipt_%s", m->u.user.name);
636 if (IS_ERR(match) || !match) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800637 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return match ? PTR_ERR(match) : -ENOENT;
639 }
640 m->u.kernel.match = match;
641
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200642 ret = check_match(m, par, i);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800643 if (ret)
644 goto err;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800647err:
648 module_put(m->u.kernel.match->me);
649 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Denys Vlasenko022748a2008-01-14 23:44:05 -0800652static int check_target(struct ipt_entry *e, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800653{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200654 struct ipt_entry_target *t = ipt_get_target(e);
655 struct xt_tgchk_param par = {
656 .table = name,
657 .entryinfo = e,
658 .target = t->u.kernel.target,
659 .targinfo = t->data,
660 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200661 .family = NFPROTO_IPV4,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200662 };
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900663 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800664
Jan Engelhardt916a9172008-10-08 11:35:20 +0200665 ret = xt_check_target(&par, t->u.target_size - sizeof(*t),
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200666 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200667 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800668 duprintf("ip_tables: check failed for `%s'.\n",
669 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200670 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800671 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200672 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800673}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Denys Vlasenko022748a2008-01-14 23:44:05 -0800675static int
Dmitry Mishina96be242006-12-12 00:29:26 -0800676find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
Patrick McHardy4b478242007-12-17 21:46:15 -0800677 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800680 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int ret;
682 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200683 struct xt_mtchk_param mtpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Dmitry Mishina96be242006-12-12 00:29:26 -0800685 ret = check_entry(e, name);
686 if (ret)
687 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200690 mtpar.table = name;
691 mtpar.entryinfo = &e->ip;
692 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200693 mtpar.family = NFPROTO_IPV4;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200694 ret = IPT_MATCH_ITERATE(e, find_check_match, &mtpar, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (ret != 0)
696 goto cleanup_matches;
697
698 t = ipt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800699 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -0800700 t->u.user.name,
701 t->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 "ipt_%s", t->u.user.name);
703 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800704 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 ret = target ? PTR_ERR(target) : -ENOENT;
706 goto cleanup_matches;
707 }
708 t->u.kernel.target = target;
709
Dmitry Mishina96be242006-12-12 00:29:26 -0800710 ret = check_target(e, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800711 if (ret)
712 goto err;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 (*i)++;
715 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800716 err:
717 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 cleanup_matches:
719 IPT_MATCH_ITERATE(e, cleanup_match, &j);
720 return ret;
721}
722
Denys Vlasenko022748a2008-01-14 23:44:05 -0800723static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800725 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 unsigned char *base,
727 unsigned char *limit,
728 const unsigned int *hook_entries,
729 const unsigned int *underflows,
730 unsigned int *i)
731{
732 unsigned int h;
733
734 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0
735 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
736 duprintf("Bad offset %p\n", e);
737 return -EINVAL;
738 }
739
740 if (e->next_offset
741 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
742 duprintf("checking: element %p size %u\n",
743 e, e->next_offset);
744 return -EINVAL;
745 }
746
747 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800748 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if ((unsigned char *)e - base == hook_entries[h])
750 newinfo->hook_entry[h] = hook_entries[h];
751 if ((unsigned char *)e - base == underflows[h])
752 newinfo->underflow[h] = underflows[h];
753 }
754
755 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900756 < 0 (not IPT_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800759 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 e->comefrom = 0;
761
762 (*i)++;
763 return 0;
764}
765
Denys Vlasenko022748a2008-01-14 23:44:05 -0800766static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767cleanup_entry(struct ipt_entry *e, unsigned int *i)
768{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200769 struct xt_tgdtor_param par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 struct ipt_entry_target *t;
771
772 if (i && (*i)-- == 0)
773 return 1;
774
775 /* Cleanup all matches */
776 IPT_MATCH_ITERATE(e, cleanup_match, NULL);
777 t = ipt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200778
779 par.target = t->u.kernel.target;
780 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200781 par.family = NFPROTO_IPV4;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200782 if (par.target->destroy != NULL)
783 par.target->destroy(&par);
784 module_put(par.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786}
787
788/* Checks and translates the user-supplied table segment (held in
789 newinfo) */
790static int
791translate_table(const char *name,
792 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800793 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800794 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 unsigned int size,
796 unsigned int number,
797 const unsigned int *hook_entries,
798 const unsigned int *underflows)
799{
800 unsigned int i;
801 int ret;
802
803 newinfo->size = size;
804 newinfo->number = number;
805
806 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800807 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 newinfo->hook_entry[i] = 0xFFFFFFFF;
809 newinfo->underflow[i] = 0xFFFFFFFF;
810 }
811
812 duprintf("translate_table: size %u\n", newinfo->size);
813 i = 0;
814 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800815 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 check_entry_size_and_hooks,
817 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800818 entry0,
819 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 hook_entries, underflows, &i);
821 if (ret != 0)
822 return ret;
823
824 if (i != number) {
825 duprintf("translate_table: %u not %u entries\n",
826 i, number);
827 return -EINVAL;
828 }
829
830 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800831 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* Only hooks which are valid */
833 if (!(valid_hooks & (1 << i)))
834 continue;
835 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
836 duprintf("Invalid hook entry %u %u\n",
837 i, hook_entries[i]);
838 return -EINVAL;
839 }
840 if (newinfo->underflow[i] == 0xFFFFFFFF) {
841 duprintf("Invalid underflow %u %u\n",
842 i, underflows[i]);
843 return -EINVAL;
844 }
845 }
846
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800847 if (!mark_source_chains(newinfo, valid_hooks, entry0))
848 return -ELOOP;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* Finally, each sanity check must pass */
851 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800852 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Dmitry Mishina96be242006-12-12 00:29:26 -0800853 find_check_entry, name, size, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800855 if (ret != 0) {
856 IPT_ENTRY_ITERATE(entry0, newinfo->size,
857 cleanup_entry, &i);
858 return ret;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700862 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800863 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
864 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
867 return ret;
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870/* Gets counters. */
871static inline int
872add_entry_to_counter(const struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800873 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 unsigned int *i)
875{
876 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
877
878 (*i)++;
879 return 0;
880}
881
Eric Dumazet31836062005-12-13 23:13:48 -0800882static inline int
883set_entry_to_counter(const struct ipt_entry *e,
884 struct ipt_counters total[],
885 unsigned int *i)
886{
887 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
888
889 (*i)++;
890 return 0;
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800894get_counters(const struct xt_table_info *t,
895 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 unsigned int cpu;
898 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800899 unsigned int curcpu;
900
901 /* Instead of clearing (by a previous call to memset())
902 * the counters and using adds, we set the counters
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700903 * with data used by 'current' CPU.
904 *
905 * Bottom half has to be disabled to prevent deadlock
906 * if new softirq were to run and call ipt_do_table
Eric Dumazet31836062005-12-13 23:13:48 -0800907 */
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700908 local_bh_disable();
909 curcpu = smp_processor_id();
Eric Dumazet31836062005-12-13 23:13:48 -0800910
911 i = 0;
912 IPT_ENTRY_ITERATE(t->entries[curcpu],
913 t->size,
914 set_entry_to_counter,
915 counters,
916 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700918 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800919 if (cpu == curcpu)
920 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 i = 0;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700922 xt_info_wrlock(cpu);
Eric Dumazet31836062005-12-13 23:13:48 -0800923 IPT_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 t->size,
925 add_entry_to_counter,
926 counters,
927 &i);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700928 xt_info_wrunlock(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100930 local_bh_enable();
931}
932
Denys Vlasenko022748a2008-01-14 23:44:05 -0800933static struct xt_counters * alloc_counters(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Dmitry Mishin27229712006-04-01 02:25:19 -0800935 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800936 struct xt_counters *counters;
Stephen Hemminger78454472009-02-20 10:35:32 +0100937 struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /* We need atomic snapshot of counters: rest doesn't change
940 (other than comefrom, which userspace doesn't care
941 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800942 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet31836062005-12-13 23:13:48 -0800943 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700946 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700948 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Dmitry Mishin27229712006-04-01 02:25:19 -0800950 return counters;
951}
952
953static int
954copy_entries_to_user(unsigned int total_size,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800955 struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800956 void __user *userptr)
957{
958 unsigned int off, num;
959 struct ipt_entry *e;
960 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200961 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800962 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200963 const void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -0800964
965 counters = alloc_counters(table);
966 if (IS_ERR(counters))
967 return PTR_ERR(counters);
968
Eric Dumazet31836062005-12-13 23:13:48 -0800969 /* choose the copy that is on our node/cpu, ...
970 * This choice is lazy (because current thread is
971 * allowed to migrate to another cpu)
972 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800973 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800974 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 ret = -EFAULT;
976 goto free_counters;
977 }
978
979 /* FIXME: use iterator macros --RR */
980 /* ... then go back and fix counters and names */
981 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
982 unsigned int i;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200983 const struct ipt_entry_match *m;
984 const struct ipt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Eric Dumazet31836062005-12-13 23:13:48 -0800986 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (copy_to_user(userptr + off
988 + offsetof(struct ipt_entry, counters),
989 &counters[num],
990 sizeof(counters[num])) != 0) {
991 ret = -EFAULT;
992 goto free_counters;
993 }
994
995 for (i = sizeof(struct ipt_entry);
996 i < e->target_offset;
997 i += m->u.match_size) {
998 m = (void *)e + i;
999
1000 if (copy_to_user(userptr + off + i
1001 + offsetof(struct ipt_entry_match,
1002 u.user.name),
1003 m->u.kernel.match->name,
1004 strlen(m->u.kernel.match->name)+1)
1005 != 0) {
1006 ret = -EFAULT;
1007 goto free_counters;
1008 }
1009 }
1010
1011 t = ipt_get_target(e);
1012 if (copy_to_user(userptr + off + e->target_offset
1013 + offsetof(struct ipt_entry_target,
1014 u.user.name),
1015 t->u.kernel.target->name,
1016 strlen(t->u.kernel.target->name)+1) != 0) {
1017 ret = -EFAULT;
1018 goto free_counters;
1019 }
1020 }
1021
1022 free_counters:
1023 vfree(counters);
1024 return ret;
1025}
1026
Dmitry Mishin27229712006-04-01 02:25:19 -08001027#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001028static void compat_standard_from_user(void *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001029{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001030 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001031
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001032 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001033 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001034 memcpy(dst, &v, sizeof(v));
1035}
1036
1037static int compat_standard_to_user(void __user *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001038{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001039 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001040
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001041 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001042 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001043 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001044}
1045
1046static inline int
Patrick McHardy4b478242007-12-17 21:46:15 -08001047compat_calc_match(struct ipt_entry_match *m, int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001048{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001049 *size += xt_compat_match_offset(m->u.kernel.match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001050 return 0;
1051}
1052
Eric Dumazet259d4e42007-12-04 23:24:56 -08001053static int compat_calc_entry(struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001054 const struct xt_table_info *info,
1055 void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001056{
1057 struct ipt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001058 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001059 int off, i, ret;
1060
Patrick McHardy30c08c42007-12-17 21:47:14 -08001061 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001062 entry_offset = (void *)e - base;
1063 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
1064 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001065 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001066 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001067 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001068 if (ret)
1069 return ret;
1070
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001071 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001072 if (info->hook_entry[i] &&
1073 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001074 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001075 if (info->underflow[i] &&
1076 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001077 newinfo->underflow[i] -= off;
1078 }
1079 return 0;
1080}
1081
Eric Dumazet259d4e42007-12-04 23:24:56 -08001082static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001083 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001084{
1085 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001086
1087 if (!newinfo || !info)
1088 return -EINVAL;
1089
Eric Dumazet259d4e42007-12-04 23:24:56 -08001090 /* we dont care about newinfo->entries[] */
1091 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1092 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001093 loc_cpu_entry = info->entries[raw_smp_processor_id()];
1094 return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001095 compat_calc_entry, info, loc_cpu_entry,
1096 newinfo);
Dmitry Mishin27229712006-04-01 02:25:19 -08001097}
1098#endif
1099
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001100static int get_info(struct net *net, void __user *user, int *len, int compat)
Dmitry Mishin27229712006-04-01 02:25:19 -08001101{
1102 char name[IPT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001103 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001104 int ret;
1105
1106 if (*len != sizeof(struct ipt_getinfo)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001107 duprintf("length %u != %zu\n", *len,
1108 sizeof(struct ipt_getinfo));
Dmitry Mishin27229712006-04-01 02:25:19 -08001109 return -EINVAL;
1110 }
1111
1112 if (copy_from_user(name, user, sizeof(name)) != 0)
1113 return -EFAULT;
1114
1115 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1116#ifdef CONFIG_COMPAT
1117 if (compat)
1118 xt_compat_lock(AF_INET);
1119#endif
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001120 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001121 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001122 if (t && !IS_ERR(t)) {
1123 struct ipt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001124 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001125
1126#ifdef CONFIG_COMPAT
1127 if (compat) {
1128 struct xt_table_info tmp;
1129 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001130 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001131 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001132 }
1133#endif
1134 info.valid_hooks = t->valid_hooks;
1135 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001136 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001137 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001138 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001139 info.num_entries = private->number;
1140 info.size = private->size;
1141 strcpy(info.name, name);
1142
1143 if (copy_to_user(user, &info, *len) != 0)
1144 ret = -EFAULT;
1145 else
1146 ret = 0;
1147
1148 xt_table_unlock(t);
1149 module_put(t->me);
1150 } else
1151 ret = t ? PTR_ERR(t) : -ENOENT;
1152#ifdef CONFIG_COMPAT
1153 if (compat)
1154 xt_compat_unlock(AF_INET);
1155#endif
1156 return ret;
1157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001160get_entries(struct net *net, struct ipt_get_entries __user *uptr, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001163 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001164 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Dmitry Mishin27229712006-04-01 02:25:19 -08001166 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001167 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001168 return -EINVAL;
1169 }
1170 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1171 return -EFAULT;
1172 if (*len != sizeof(struct ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001173 duprintf("get_entries: %u != %zu\n",
1174 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001175 return -EINVAL;
1176 }
1177
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001178 t = xt_find_table_lock(net, AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001180 const struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001181 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001182 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001183 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 t, uptr->entrytable);
1185 else {
1186 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001187 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001188 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001191 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 } else
1193 ret = t ? PTR_ERR(t) : -ENOENT;
1194
1195 return ret;
1196}
1197
1198static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001199__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001200 struct xt_table_info *newinfo, unsigned int num_counters,
1201 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001202{
1203 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001204 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001205 struct xt_table_info *oldinfo;
1206 struct xt_counters *counters;
1207 void *loc_cpu_old_entry;
1208
1209 ret = 0;
1210 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1211 if (!counters) {
1212 ret = -ENOMEM;
1213 goto out;
1214 }
1215
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001216 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Dmitry Mishin27229712006-04-01 02:25:19 -08001217 "iptable_%s", name);
1218 if (!t || IS_ERR(t)) {
1219 ret = t ? PTR_ERR(t) : -ENOENT;
1220 goto free_newinfo_counters_untrans;
1221 }
1222
1223 /* You lied! */
1224 if (valid_hooks != t->valid_hooks) {
1225 duprintf("Valid hook crap: %08X vs %08X\n",
1226 valid_hooks, t->valid_hooks);
1227 ret = -EINVAL;
1228 goto put_module;
1229 }
1230
1231 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1232 if (!oldinfo)
1233 goto put_module;
1234
1235 /* Update module usage count based on number of rules */
1236 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1237 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1238 if ((oldinfo->number > oldinfo->initial_entries) ||
1239 (newinfo->number <= oldinfo->initial_entries))
1240 module_put(t->me);
1241 if ((oldinfo->number > oldinfo->initial_entries) &&
1242 (newinfo->number <= oldinfo->initial_entries))
1243 module_put(t->me);
1244
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001245 /* Get the old counters, and synchronize with replace */
Dmitry Mishin27229712006-04-01 02:25:19 -08001246 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001247
Dmitry Mishin27229712006-04-01 02:25:19 -08001248 /* Decrease module usage counts and free resource */
1249 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Patrick McHardy4b478242007-12-17 21:46:15 -08001250 IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1251 NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001252 xt_free_table_info(oldinfo);
1253 if (copy_to_user(counters_ptr, counters,
1254 sizeof(struct xt_counters) * num_counters) != 0)
1255 ret = -EFAULT;
1256 vfree(counters);
1257 xt_table_unlock(t);
1258 return ret;
1259
1260 put_module:
1261 module_put(t->me);
1262 xt_table_unlock(t);
1263 free_newinfo_counters_untrans:
1264 vfree(counters);
1265 out:
1266 return ret;
1267}
1268
1269static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001270do_replace(struct net *net, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 int ret;
1273 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001274 struct xt_table_info *newinfo;
1275 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1278 return -EFAULT;
1279
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001280 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001281 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1282 return -ENOMEM;
1283
Harald Welte2e4e6a12006-01-12 13:30:04 -08001284 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (!newinfo)
1286 return -ENOMEM;
1287
Patrick McHardy9c547952007-12-17 21:52:00 -08001288 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001289 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1290 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 tmp.size) != 0) {
1292 ret = -EFAULT;
1293 goto free_newinfo;
1294 }
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001297 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 tmp.hook_entry, tmp.underflow);
1299 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001300 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 duprintf("ip_tables: Translated table\n");
1303
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001304 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001305 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001306 if (ret)
1307 goto free_newinfo_untrans;
1308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Dmitry Mishin27229712006-04-01 02:25:19 -08001310 free_newinfo_untrans:
Patrick McHardy9c547952007-12-17 21:52:00 -08001311 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001313 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return ret;
1315}
1316
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001317/* We're lazy, and add to the first CPU; overflow works its fey magic
1318 * and everything is OK. */
1319static int
1320add_counter_to_entry(struct ipt_entry *e,
1321 const struct xt_counters addme[],
1322 unsigned int *i)
1323{
1324 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1325
1326 (*i)++;
1327 return 0;
1328}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001331do_add_counters(struct net *net, void __user *user, unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001333 unsigned int i, curcpu;
Dmitry Mishin27229712006-04-01 02:25:19 -08001334 struct xt_counters_info tmp;
1335 struct xt_counters *paddc;
1336 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001337 const char *name;
Dmitry Mishin27229712006-04-01 02:25:19 -08001338 int size;
1339 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001340 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001341 const struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001343 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001344#ifdef CONFIG_COMPAT
1345 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Dmitry Mishin27229712006-04-01 02:25:19 -08001347 if (compat) {
1348 ptmp = &compat_tmp;
1349 size = sizeof(struct compat_xt_counters_info);
1350 } else
1351#endif
1352 {
1353 ptmp = &tmp;
1354 size = sizeof(struct xt_counters_info);
1355 }
1356
1357 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return -EFAULT;
1359
Dmitry Mishin27229712006-04-01 02:25:19 -08001360#ifdef CONFIG_COMPAT
1361 if (compat) {
1362 num_counters = compat_tmp.num_counters;
1363 name = compat_tmp.name;
1364 } else
1365#endif
1366 {
1367 num_counters = tmp.num_counters;
1368 name = tmp.name;
1369 }
1370
1371 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return -EINVAL;
1373
Dmitry Mishin27229712006-04-01 02:25:19 -08001374 paddc = vmalloc_node(len - size, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!paddc)
1376 return -ENOMEM;
1377
Dmitry Mishin27229712006-04-01 02:25:19 -08001378 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 ret = -EFAULT;
1380 goto free;
1381 }
1382
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001383 t = xt_find_table_lock(net, AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (!t || IS_ERR(t)) {
1385 ret = t ? PTR_ERR(t) : -ENOENT;
1386 goto free;
1387 }
1388
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001389 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001390 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001391 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 ret = -EINVAL;
1393 goto unlock_up_free;
1394 }
1395
1396 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001397 /* Choose the copy that is on our node */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001398 curcpu = smp_processor_id();
1399 loc_cpu_entry = private->entries[curcpu];
1400 xt_info_wrlock(curcpu);
Eric Dumazet31836062005-12-13 23:13:48 -08001401 IPT_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001402 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 add_counter_to_entry,
Dmitry Mishin27229712006-04-01 02:25:19 -08001404 paddc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 &i);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001406 xt_info_wrunlock(curcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001408 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001409 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 module_put(t->me);
1411 free:
1412 vfree(paddc);
1413
1414 return ret;
1415}
1416
Dmitry Mishin27229712006-04-01 02:25:19 -08001417#ifdef CONFIG_COMPAT
1418struct compat_ipt_replace {
1419 char name[IPT_TABLE_MAXNAMELEN];
1420 u32 valid_hooks;
1421 u32 num_entries;
1422 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001423 u32 hook_entry[NF_INET_NUMHOOKS];
1424 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001425 u32 num_counters;
1426 compat_uptr_t counters; /* struct ipt_counters * */
1427 struct compat_ipt_entry entries[0];
1428};
1429
Patrick McHardya18aa312007-12-12 10:35:16 -08001430static int
1431compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001432 unsigned int *size, struct xt_counters *counters,
Patrick McHardya18aa312007-12-12 10:35:16 -08001433 unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001434{
Al Viro3e597c62006-09-24 23:42:20 +01001435 struct ipt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001436 struct compat_ipt_entry __user *ce;
1437 u_int16_t target_offset, next_offset;
1438 compat_uint_t origsize;
1439 int ret;
1440
1441 ret = -EFAULT;
1442 origsize = *size;
1443 ce = (struct compat_ipt_entry __user *)*dstptr;
Patrick McHardy78000072006-05-03 23:20:27 -07001444 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
Dmitry Mishin27229712006-04-01 02:25:19 -08001445 goto out;
1446
Patrick McHardya18aa312007-12-12 10:35:16 -08001447 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1448 goto out;
1449
Dmitry Mishin27229712006-04-01 02:25:19 -08001450 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001451 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1452
Patrick McHardyac8e27f2007-12-17 21:45:52 -08001453 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001454 target_offset = e->target_offset - (origsize - *size);
1455 if (ret)
1456 goto out;
1457 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001458 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001459 if (ret)
1460 goto out;
1461 ret = -EFAULT;
1462 next_offset = e->next_offset - (origsize - *size);
Patrick McHardy78000072006-05-03 23:20:27 -07001463 if (put_user(target_offset, &ce->target_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001464 goto out;
Patrick McHardy78000072006-05-03 23:20:27 -07001465 if (put_user(next_offset, &ce->next_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001466 goto out;
Patrick McHardya18aa312007-12-12 10:35:16 -08001467
1468 (*i)++;
Dmitry Mishin27229712006-04-01 02:25:19 -08001469 return 0;
1470out:
1471 return ret;
1472}
1473
Denys Vlasenko022748a2008-01-14 23:44:05 -08001474static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001475compat_find_calc_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001476 const char *name,
1477 const struct ipt_ip *ip,
1478 unsigned int hookmask,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001479 int *size, unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001480{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001481 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001482
1483 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001484 m->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001485 "ipt_%s", m->u.user.name);
1486 if (IS_ERR(match) || !match) {
1487 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001488 m->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001489 return match ? PTR_ERR(match) : -ENOENT;
1490 }
1491 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001492 *size += xt_compat_match_offset(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001493
1494 (*i)++;
1495 return 0;
1496}
1497
Denys Vlasenko022748a2008-01-14 23:44:05 -08001498static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001499compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1500{
1501 if (i && (*i)-- == 0)
1502 return 1;
1503
1504 module_put(m->u.kernel.match->me);
1505 return 0;
1506}
1507
Denys Vlasenko022748a2008-01-14 23:44:05 -08001508static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001509compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001510{
1511 struct ipt_entry_target *t;
1512
1513 if (i && (*i)-- == 0)
1514 return 1;
1515
1516 /* Cleanup all matches */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001517 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1518 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001519 module_put(t->u.kernel.target->me);
1520 return 0;
1521}
1522
Denys Vlasenko022748a2008-01-14 23:44:05 -08001523static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001524check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001525 struct xt_table_info *newinfo,
1526 unsigned int *size,
1527 unsigned char *base,
1528 unsigned char *limit,
1529 unsigned int *hook_entries,
1530 unsigned int *underflows,
1531 unsigned int *i,
1532 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001533{
1534 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001535 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001536 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001537 unsigned int j;
1538 int ret, off, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001539
1540 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1541 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0
1542 || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1543 duprintf("Bad offset %p, limit = %p\n", e, limit);
1544 return -EINVAL;
1545 }
1546
1547 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001548 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001549 duprintf("checking: element %p size %u\n",
1550 e, e->next_offset);
1551 return -EINVAL;
1552 }
1553
Patrick McHardy73cd5982007-12-17 21:47:32 -08001554 /* For purposes of check_entry casting the compat entry is fine */
1555 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001556 if (ret)
1557 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001558
Patrick McHardy30c08c42007-12-17 21:47:14 -08001559 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001560 entry_offset = (void *)e - (void *)base;
1561 j = 0;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001562 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1563 &e->ip, e->comefrom, &off, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001564 if (ret != 0)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001565 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001566
Patrick McHardy73cd5982007-12-17 21:47:32 -08001567 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001568 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -08001569 t->u.user.name,
1570 t->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001571 "ipt_%s", t->u.user.name);
1572 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001573 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001574 t->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001575 ret = target ? PTR_ERR(target) : -ENOENT;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001576 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001577 }
1578 t->u.kernel.target = target;
1579
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001580 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001581 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001582 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001583 if (ret)
1584 goto out;
1585
1586 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001587 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001588 if ((unsigned char *)e - base == hook_entries[h])
1589 newinfo->hook_entry[h] = hook_entries[h];
1590 if ((unsigned char *)e - base == underflows[h])
1591 newinfo->underflow[h] = underflows[h];
1592 }
1593
1594 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001595 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001596 e->comefrom = 0;
1597
1598 (*i)++;
1599 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001600
Dmitry Mishin27229712006-04-01 02:25:19 -08001601out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001602 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001603release_matches:
1604 IPT_MATCH_ITERATE(e, compat_release_match, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001605 return ret;
1606}
1607
Patrick McHardy4b478242007-12-17 21:46:15 -08001608static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001609compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001610 unsigned int *size, const char *name,
1611 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001612{
1613 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001614 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001615 struct ipt_entry *de;
1616 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001617 int ret, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001618
1619 ret = 0;
1620 origsize = *size;
1621 de = (struct ipt_entry *)*dstptr;
1622 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001623 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001624
Patrick McHardy73cd5982007-12-17 21:47:32 -08001625 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001626 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1627
Patrick McHardy73cd5982007-12-17 21:47:32 -08001628 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1629 dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001630 if (ret)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001631 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001632 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001633 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001634 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001635 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001636
1637 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001638 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001639 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1640 newinfo->hook_entry[h] -= origsize - *size;
1641 if ((unsigned char *)de - base < newinfo->underflow[h])
1642 newinfo->underflow[h] -= origsize - *size;
1643 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001644 return ret;
1645}
Dmitry Mishin27229712006-04-01 02:25:19 -08001646
Denys Vlasenko022748a2008-01-14 23:44:05 -08001647static int
1648compat_check_entry(struct ipt_entry *e, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001649 unsigned int *i)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001650{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001651 struct xt_mtchk_param mtpar;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001652 unsigned int j;
1653 int ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001654
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001655 j = 0;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001656 mtpar.table = name;
1657 mtpar.entryinfo = &e->ip;
1658 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +02001659 mtpar.family = NFPROTO_IPV4;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001660 ret = IPT_MATCH_ITERATE(e, check_match, &mtpar, &j);
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001661 if (ret)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001662 goto cleanup_matches;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001663
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001664 ret = check_target(e, name);
1665 if (ret)
1666 goto cleanup_matches;
1667
1668 (*i)++;
1669 return 0;
1670
1671 cleanup_matches:
1672 IPT_MATCH_ITERATE(e, cleanup_match, &j);
1673 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001674}
1675
Dmitry Mishin27229712006-04-01 02:25:19 -08001676static int
1677translate_compat_table(const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001678 unsigned int valid_hooks,
1679 struct xt_table_info **pinfo,
1680 void **pentry0,
1681 unsigned int total_size,
1682 unsigned int number,
1683 unsigned int *hook_entries,
1684 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001685{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001686 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001687 struct xt_table_info *newinfo, *info;
1688 void *pos, *entry0, *entry1;
1689 unsigned int size;
1690 int ret;
1691
1692 info = *pinfo;
1693 entry0 = *pentry0;
1694 size = total_size;
1695 info->number = number;
1696
1697 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001698 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001699 info->hook_entry[i] = 0xFFFFFFFF;
1700 info->underflow[i] = 0xFFFFFFFF;
1701 }
1702
1703 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001704 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001705 xt_compat_lock(AF_INET);
1706 /* Walk through entries, checking offsets. */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001707 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1708 check_compat_entry_size_and_hooks,
1709 info, &size, entry0,
1710 entry0 + total_size,
1711 hook_entries, underflows, &j, name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001712 if (ret != 0)
1713 goto out_unlock;
1714
1715 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001716 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001717 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001718 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001719 goto out_unlock;
1720 }
1721
1722 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001723 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001724 /* Only hooks which are valid */
1725 if (!(valid_hooks & (1 << i)))
1726 continue;
1727 if (info->hook_entry[i] == 0xFFFFFFFF) {
1728 duprintf("Invalid hook entry %u %u\n",
1729 i, hook_entries[i]);
1730 goto out_unlock;
1731 }
1732 if (info->underflow[i] == 0xFFFFFFFF) {
1733 duprintf("Invalid underflow %u %u\n",
1734 i, underflows[i]);
1735 goto out_unlock;
1736 }
1737 }
1738
1739 ret = -ENOMEM;
1740 newinfo = xt_alloc_table_info(size);
1741 if (!newinfo)
1742 goto out_unlock;
1743
1744 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001745 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001746 newinfo->hook_entry[i] = info->hook_entry[i];
1747 newinfo->underflow[i] = info->underflow[i];
1748 }
1749 entry1 = newinfo->entries[raw_smp_processor_id()];
1750 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001751 size = total_size;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001752 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
Patrick McHardy9c547952007-12-17 21:52:00 -08001753 compat_copy_entry_from_user,
1754 &pos, &size, name, newinfo, entry1);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001755 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001756 xt_compat_unlock(AF_INET);
1757 if (ret)
1758 goto free_newinfo;
1759
1760 ret = -ELOOP;
1761 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1762 goto free_newinfo;
1763
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001764 i = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001765 ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001766 name, &i);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001767 if (ret) {
1768 j -= i;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001769 COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1770 compat_release_entry, &j);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001771 IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1772 xt_free_table_info(newinfo);
1773 return ret;
1774 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001775
Dmitry Mishin27229712006-04-01 02:25:19 -08001776 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001777 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001778 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1779 memcpy(newinfo->entries[i], entry1, newinfo->size);
1780
1781 *pinfo = newinfo;
1782 *pentry0 = entry1;
1783 xt_free_table_info(info);
1784 return 0;
1785
1786free_newinfo:
1787 xt_free_table_info(newinfo);
1788out:
Patrick McHardy73cd5982007-12-17 21:47:32 -08001789 COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001790 return ret;
1791out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001792 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001793 xt_compat_unlock(AF_INET);
1794 goto out;
1795}
1796
1797static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001798compat_do_replace(struct net *net, void __user *user, unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001799{
1800 int ret;
1801 struct compat_ipt_replace tmp;
1802 struct xt_table_info *newinfo;
1803 void *loc_cpu_entry;
1804
1805 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1806 return -EFAULT;
1807
Dmitry Mishin27229712006-04-01 02:25:19 -08001808 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001809 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001810 return -ENOMEM;
1811 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1812 return -ENOMEM;
1813
1814 newinfo = xt_alloc_table_info(tmp.size);
1815 if (!newinfo)
1816 return -ENOMEM;
1817
Patrick McHardy9c547952007-12-17 21:52:00 -08001818 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001819 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1820 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1821 tmp.size) != 0) {
1822 ret = -EFAULT;
1823 goto free_newinfo;
1824 }
1825
1826 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001827 &newinfo, &loc_cpu_entry, tmp.size,
1828 tmp.num_entries, tmp.hook_entry,
1829 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001830 if (ret != 0)
1831 goto free_newinfo;
1832
1833 duprintf("compat_do_replace: Translated table\n");
1834
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001835 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001836 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001837 if (ret)
1838 goto free_newinfo_untrans;
1839 return 0;
1840
1841 free_newinfo_untrans:
Patrick McHardy4b478242007-12-17 21:46:15 -08001842 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001843 free_newinfo:
1844 xt_free_table_info(newinfo);
1845 return ret;
1846}
1847
1848static int
1849compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001850 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001851{
1852 int ret;
1853
1854 if (!capable(CAP_NET_ADMIN))
1855 return -EPERM;
1856
1857 switch (cmd) {
1858 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001859 ret = compat_do_replace(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001860 break;
1861
1862 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001863 ret = do_add_counters(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001864 break;
1865
1866 default:
1867 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1868 ret = -EINVAL;
1869 }
1870
1871 return ret;
1872}
1873
Patrick McHardy4b478242007-12-17 21:46:15 -08001874struct compat_ipt_get_entries {
Dmitry Mishin27229712006-04-01 02:25:19 -08001875 char name[IPT_TABLE_MAXNAMELEN];
1876 compat_uint_t size;
1877 struct compat_ipt_entry entrytable[0];
1878};
1879
Patrick McHardy4b478242007-12-17 21:46:15 -08001880static int
1881compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1882 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001883{
Dmitry Mishin27229712006-04-01 02:25:19 -08001884 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001885 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001886 void __user *pos;
1887 unsigned int size;
1888 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001889 const void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001890 unsigned int i = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001891
1892 counters = alloc_counters(table);
1893 if (IS_ERR(counters))
1894 return PTR_ERR(counters);
1895
1896 /* choose the copy that is on our node/cpu, ...
1897 * This choice is lazy (because current thread is
1898 * allowed to migrate to another cpu)
1899 */
1900 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1901 pos = userptr;
1902 size = total_size;
1903 ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
Patrick McHardya18aa312007-12-12 10:35:16 -08001904 compat_copy_entry_to_user,
1905 &pos, &size, counters, &i);
Dmitry Mishin27229712006-04-01 02:25:19 -08001906
Dmitry Mishin27229712006-04-01 02:25:19 -08001907 vfree(counters);
1908 return ret;
1909}
1910
1911static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001912compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1913 int *len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001914{
1915 int ret;
1916 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001917 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001918
Dmitry Mishin27229712006-04-01 02:25:19 -08001919 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001920 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001921 return -EINVAL;
1922 }
1923
1924 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1925 return -EFAULT;
1926
1927 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001928 duprintf("compat_get_entries: %u != %zu\n",
1929 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001930 return -EINVAL;
1931 }
1932
1933 xt_compat_lock(AF_INET);
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001934 t = xt_find_table_lock(net, AF_INET, get.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001935 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001936 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001937 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08001938 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001939 ret = compat_table_info(private, &info);
1940 if (!ret && get.size == info.size) {
1941 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001942 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001943 } else if (!ret) {
1944 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001945 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001946 ret = -EAGAIN;
Dmitry Mishin27229712006-04-01 02:25:19 -08001947 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001948 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001949 module_put(t->me);
1950 xt_table_unlock(t);
1951 } else
1952 ret = t ? PTR_ERR(t) : -ENOENT;
1953
1954 xt_compat_unlock(AF_INET);
1955 return ret;
1956}
1957
Patrick McHardy79030ed2006-09-20 12:05:08 -07001958static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1959
Dmitry Mishin27229712006-04-01 02:25:19 -08001960static int
1961compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1962{
1963 int ret;
1964
Björn Steinbrink82fac052006-10-20 00:21:10 -07001965 if (!capable(CAP_NET_ADMIN))
1966 return -EPERM;
1967
Dmitry Mishin27229712006-04-01 02:25:19 -08001968 switch (cmd) {
1969 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001970 ret = get_info(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001971 break;
1972 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001973 ret = compat_get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001974 break;
1975 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001976 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001977 }
1978 return ret;
1979}
1980#endif
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001983do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 int ret;
1986
1987 if (!capable(CAP_NET_ADMIN))
1988 return -EPERM;
1989
1990 switch (cmd) {
1991 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001992 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 break;
1994
1995 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001996 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
1998
1999 default:
2000 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
2001 ret = -EINVAL;
2002 }
2003
2004 return ret;
2005}
2006
2007static int
2008do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2009{
2010 int ret;
2011
2012 if (!capable(CAP_NET_ADMIN))
2013 return -EPERM;
2014
2015 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002016 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002017 ret = get_info(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002019
2020 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002021 ret = get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08002022 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 case IPT_SO_GET_REVISION_MATCH:
2025 case IPT_SO_GET_REVISION_TARGET: {
2026 struct ipt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002027 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 if (*len != sizeof(rev)) {
2030 ret = -EINVAL;
2031 break;
2032 }
2033 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2034 ret = -EFAULT;
2035 break;
2036 }
2037
2038 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002039 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002041 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Harald Welte2e4e6a12006-01-12 13:30:04 -08002043 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2044 rev.revision,
2045 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 "ipt_%s", rev.name);
2047 break;
2048 }
2049
2050 default:
2051 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2052 ret = -EINVAL;
2053 }
2054
2055 return ret;
2056}
2057
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002058struct xt_table *ipt_register_table(struct net *net, struct xt_table *table,
2059 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002062 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08002063 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08002065 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08002066 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Harald Welte2e4e6a12006-01-12 13:30:04 -08002068 newinfo = xt_alloc_table_info(repl->size);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002069 if (!newinfo) {
2070 ret = -ENOMEM;
2071 goto out;
2072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Patrick McHardy9c547952007-12-17 21:52:00 -08002074 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002075 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2076 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08002079 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 repl->num_entries,
2081 repl->hook_entry,
2082 repl->underflow);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002083 if (ret != 0)
2084 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002086 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08002087 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002088 ret = PTR_ERR(new_table);
2089 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002092 return new_table;
2093
2094out_free:
2095 xt_free_table_info(newinfo);
2096out:
2097 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
Jan Engelhardte60a13e2007-02-07 15:12:33 -08002100void ipt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002102 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002103 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08002104 struct module *table_owner = table->me;
Eric Dumazet31836062005-12-13 23:13:48 -08002105
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002106 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002109 loc_cpu_entry = private->entries[raw_smp_processor_id()];
2110 IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
Alexey Dobriyandf200962008-01-31 04:05:34 -08002111 if (private->number > private->initial_entries)
2112 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002113 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
2116/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002117static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2119 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002120 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Patrick McHardy9c547952007-12-17 21:52:00 -08002122 return ((test_type == 0xFF) ||
2123 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 ^ invert;
2125}
2126
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002127static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002128icmp_match(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Jan Engelhardt5452e422008-04-14 11:15:35 +02002130 const struct icmphdr *ic;
2131 struct icmphdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002132 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002135 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002136 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002138 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (ic == NULL) {
2140 /* We've been asked to examine this packet, and we
2141 * can't. Hence, no choice but to drop.
2142 */
2143 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002144 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002145 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147
2148 return icmp_type_code_match(icmpinfo->type,
2149 icmpinfo->code[0],
2150 icmpinfo->code[1],
2151 ic->type, ic->code,
2152 !!(icmpinfo->invflags&IPT_ICMP_INV));
2153}
2154
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002155static bool icmp_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002157 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002159 /* Must specify no unknown invflags */
2160 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
2163/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07002164static struct xt_target ipt_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 .name = IPT_STANDARD_TARGET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002166 .targetsize = sizeof(int),
Jan Engelhardt4ba351c2009-04-14 14:28:48 +02002167 .family = NFPROTO_IPV4,
Dmitry Mishin27229712006-04-01 02:25:19 -08002168#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07002169 .compatsize = sizeof(compat_int_t),
2170 .compat_from_user = compat_standard_from_user,
2171 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173};
2174
Patrick McHardy9f15c532007-07-07 22:22:02 -07002175static struct xt_target ipt_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 .name = IPT_ERROR_TARGET,
2177 .target = ipt_error,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002178 .targetsize = IPT_FUNCTION_MAXNAMELEN,
Jan Engelhardt4ba351c2009-04-14 14:28:48 +02002179 .family = NFPROTO_IPV4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180};
2181
2182static struct nf_sockopt_ops ipt_sockopts = {
2183 .pf = PF_INET,
2184 .set_optmin = IPT_BASE_CTL,
2185 .set_optmax = IPT_SO_SET_MAX+1,
2186 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002187#ifdef CONFIG_COMPAT
2188 .compat_set = compat_do_ipt_set_ctl,
2189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 .get_optmin = IPT_BASE_CTL,
2191 .get_optmax = IPT_SO_GET_MAX+1,
2192 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002193#ifdef CONFIG_COMPAT
2194 .compat_get = compat_do_ipt_get_ctl,
2195#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002196 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197};
2198
Patrick McHardy9f15c532007-07-07 22:22:02 -07002199static struct xt_match icmp_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 .name = "icmp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002201 .match = icmp_match,
2202 .matchsize = sizeof(struct ipt_icmp),
Patrick McHardy9c547952007-12-17 21:52:00 -08002203 .checkentry = icmp_checkentry,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002204 .proto = IPPROTO_ICMP,
Jan Engelhardt4ba351c2009-04-14 14:28:48 +02002205 .family = NFPROTO_IPV4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206};
2207
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002208static int __net_init ip_tables_net_init(struct net *net)
2209{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02002210 return xt_proto_init(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002211}
2212
2213static void __net_exit ip_tables_net_exit(struct net *net)
2214{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02002215 xt_proto_fini(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002216}
2217
2218static struct pernet_operations ip_tables_net_ops = {
2219 .init = ip_tables_net_init,
2220 .exit = ip_tables_net_exit,
2221};
2222
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002223static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
2225 int ret;
2226
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002227 ret = register_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002228 if (ret < 0)
2229 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002232 ret = xt_register_target(&ipt_standard_target);
2233 if (ret < 0)
2234 goto err2;
2235 ret = xt_register_target(&ipt_error_target);
2236 if (ret < 0)
2237 goto err3;
2238 ret = xt_register_match(&icmp_matchstruct);
2239 if (ret < 0)
2240 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 /* Register setsockopt */
2243 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002244 if (ret < 0)
2245 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Dan Aloni0236e662007-07-09 13:20:12 -07002247 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002249
2250err5:
2251 xt_unregister_match(&icmp_matchstruct);
2252err4:
2253 xt_unregister_target(&ipt_error_target);
2254err3:
2255 xt_unregister_target(&ipt_standard_target);
2256err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002257 unregister_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002258err1:
2259 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002262static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002265
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002266 xt_unregister_match(&icmp_matchstruct);
2267 xt_unregister_target(&ipt_error_target);
2268 xt_unregister_target(&ipt_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002269
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002270 unregister_pernet_subsys(&ip_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
2273EXPORT_SYMBOL(ipt_register_table);
2274EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002276module_init(ip_tables_init);
2277module_exit(ip_tables_fini);