blob: f5b66ec18b0d6a9ccff81bb90b7f6c3253fbe735 [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", \
56 __FUNCTION__, __FILE__, __LINE__); \
57} 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. */
Patrick McHardy9c547952007-12-17 21:52:00 -080078static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070079ip_packet_match(const struct iphdr *ip,
80 const char *indev,
81 const char *outdev,
82 const struct ipt_ip *ipinfo,
83 int isfrag)
84{
85 size_t i;
86 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
96 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
97 NIPQUAD(ip->saddr),
98 NIPQUAD(ipinfo->smsk.s_addr),
99 NIPQUAD(ipinfo->src.s_addr),
100 ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
101 dprintf("DST: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
102 NIPQUAD(ip->daddr),
103 NIPQUAD(ipinfo->dmsk.s_addr),
104 NIPQUAD(ipinfo->dst.s_addr),
105 ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800106 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
109 /* Look for ifname matches; this should unroll nicely. */
110 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
111 ret |= (((const unsigned long *)indev)[i]
112 ^ ((const unsigned long *)ipinfo->iniface)[i])
113 & ((const unsigned long *)ipinfo->iniface_mask)[i];
114 }
115
116 if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
117 dprintf("VIA in mismatch (%s vs %s).%s\n",
118 indev, ipinfo->iniface,
119 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800120 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
123 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
124 ret |= (((const unsigned long *)outdev)[i]
125 ^ ((const unsigned long *)ipinfo->outiface)[i])
126 & ((const unsigned long *)ipinfo->outiface_mask)[i];
127 }
128
129 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
130 dprintf("VIA out mismatch (%s vs %s).%s\n",
131 outdev, ipinfo->outiface,
132 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800133 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
136 /* Check specific protocol */
137 if (ipinfo->proto
138 && FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
139 dprintf("Packet protocol %hi does not match %hi.%s\n",
140 ip->protocol, ipinfo->proto,
141 ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800142 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
145 /* If we have a fragment rule but the packet is not a fragment
146 * then we return zero */
147 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
148 dprintf("Fragment rule but not fragment.%s\n",
149 ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800150 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
Patrick McHardy9c547952007-12-17 21:52:00 -0800153 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700156static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157ip_checkentry(const struct ipt_ip *ip)
158{
159 if (ip->flags & ~IPT_F_MASK) {
160 duprintf("Unknown flag bits set: %08X\n",
161 ip->flags & ~IPT_F_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700162 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164 if (ip->invflags & ~IPT_INV_MASK) {
165 duprintf("Unknown invflag bits set: %08X\n",
166 ip->invflags & ~IPT_INV_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700167 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700169 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172static unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700173ipt_error(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 const struct net_device *in,
175 const struct net_device *out,
176 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800177 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700178 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 if (net_ratelimit())
181 printk("ip_tables: error: `%s'\n", (char *)targinfo);
182
183 return NF_DROP;
184}
185
186static inline
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700187bool do_match(struct ipt_entry_match *m,
188 const struct sk_buff *skb,
189 const struct net_device *in,
190 const struct net_device *out,
191 int offset,
192 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 /* Stop iteration if it doesn't match */
Patrick McHardy1c524832006-03-20 18:02:15 -0800195 if (!m->u.kernel.match->match(skb, in, out, m->u.kernel.match, m->data,
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300196 offset, ip_hdrlen(skb), hotdrop))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700197 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 else
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700199 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202static inline struct ipt_entry *
203get_entry(void *base, unsigned int offset)
204{
205 return (struct ipt_entry *)(base + offset);
206}
207
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700208/* All zeroes == unconditional rule. */
209static inline int
210unconditional(const struct ipt_ip *ip)
211{
212 unsigned int i;
213
214 for (i = 0; i < sizeof(*ip)/sizeof(__u32); i++)
215 if (((__u32 *)ip)[i])
216 return 0;
217
218 return 1;
Jan Engelhardte79ec502007-12-17 22:44:06 -0800219#undef FWINV
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700220}
221
222#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
223 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
224static const char *hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800225 [NF_INET_PRE_ROUTING] = "PREROUTING",
226 [NF_INET_LOCAL_IN] = "INPUT",
Patrick McHardy9c547952007-12-17 21:52:00 -0800227 [NF_INET_FORWARD] = "FORWARD",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800228 [NF_INET_LOCAL_OUT] = "OUTPUT",
229 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700230};
231
232enum nf_ip_trace_comments {
233 NF_IP_TRACE_COMMENT_RULE,
234 NF_IP_TRACE_COMMENT_RETURN,
235 NF_IP_TRACE_COMMENT_POLICY,
236};
237
238static const char *comments[] = {
239 [NF_IP_TRACE_COMMENT_RULE] = "rule",
240 [NF_IP_TRACE_COMMENT_RETURN] = "return",
241 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
242};
243
244static struct nf_loginfo trace_loginfo = {
245 .type = NF_LOG_TYPE_LOG,
246 .u = {
247 .log = {
248 .level = 4,
249 .logflags = NF_LOG_MASK,
250 },
251 },
252};
253
254static inline int
255get_chainname_rulenum(struct ipt_entry *s, struct ipt_entry *e,
256 char *hookname, char **chainname,
257 char **comment, unsigned int *rulenum)
258{
259 struct ipt_standard_target *t = (void *)ipt_get_target(s);
260
261 if (strcmp(t->target.u.kernel.target->name, IPT_ERROR_TARGET) == 0) {
262 /* Head of user chain: ERROR target with chainname */
263 *chainname = t->target.data;
264 (*rulenum) = 0;
265 } else if (s == e) {
266 (*rulenum)++;
267
268 if (s->target_offset == sizeof(struct ipt_entry)
269 && strcmp(t->target.u.kernel.target->name,
270 IPT_STANDARD_TARGET) == 0
271 && t->verdict < 0
272 && unconditional(&s->ip)) {
273 /* Tail of chains: STANDARD target (return/policy) */
274 *comment = *chainname == hookname
275 ? (char *)comments[NF_IP_TRACE_COMMENT_POLICY]
276 : (char *)comments[NF_IP_TRACE_COMMENT_RETURN];
277 }
278 return 1;
279 } else
280 (*rulenum)++;
281
282 return 0;
283}
284
285static void trace_packet(struct sk_buff *skb,
286 unsigned int hook,
287 const struct net_device *in,
288 const struct net_device *out,
289 char *tablename,
290 struct xt_table_info *private,
291 struct ipt_entry *e)
292{
293 void *table_base;
294 struct ipt_entry *root;
295 char *hookname, *chainname, *comment;
296 unsigned int rulenum = 0;
297
298 table_base = (void *)private->entries[smp_processor_id()];
299 root = get_entry(table_base, private->hook_entry[hook]);
300
301 hookname = chainname = (char *)hooknames[hook];
302 comment = (char *)comments[NF_IP_TRACE_COMMENT_RULE];
303
304 IPT_ENTRY_ITERATE(root,
305 private->size - private->hook_entry[hook],
306 get_chainname_rulenum,
307 e, hookname, &chainname, &comment, &rulenum);
308
309 nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo,
310 "TRACE: %s:%s:%s:%u ",
311 tablename, chainname, comment, rulenum);
312}
313#endif
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/* Returns one of the generic firewall policies, like NF_ACCEPT. */
316unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700317ipt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unsigned int hook,
319 const struct net_device *in,
320 const struct net_device *out,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800321 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
324 u_int16_t offset;
325 struct iphdr *ip;
326 u_int16_t datalen;
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700327 bool hotdrop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* Initializing verdict to NF_DROP keeps gcc happy. */
329 unsigned int verdict = NF_DROP;
330 const char *indev, *outdev;
331 void *table_base;
332 struct ipt_entry *e, *back;
Patrick McHardy83117312006-08-17 18:13:53 -0700333 struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* Initialization */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700336 ip = ip_hdr(skb);
337 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 indev = in ? in->name : nulldevname;
339 outdev = out ? out->name : nulldevname;
340 /* We handle fragments by dealing with the first fragment as
341 * if it was a normal packet. All other fragments are treated
342 * normally, except that they will NEVER match rules that ask
343 * things we don't know, ie. tcp syn flag or ports). If the
344 * rule is also a fragment-specific rule, non-fragments won't
345 * match it. */
346 offset = ntohs(ip->frag_off) & IP_OFFSET;
347
348 read_lock_bh(&table->lock);
349 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Patrick McHardy83117312006-08-17 18:13:53 -0700350 private = table->private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800351 table_base = (void *)private->entries[smp_processor_id()];
352 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* For return from builtin chain */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800355 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 do {
358 IP_NF_ASSERT(e);
359 IP_NF_ASSERT(back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (ip_packet_match(ip, indev, outdev, &e->ip, offset)) {
361 struct ipt_entry_target *t;
362
363 if (IPT_MATCH_ITERATE(e, do_match,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700364 skb, in, out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 offset, &hotdrop) != 0)
366 goto no_match;
367
368 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
369
370 t = ipt_get_target(e);
371 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700372
373#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
374 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
375 /* The packet is traced: log it */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700376 if (unlikely(skb->nf_trace))
377 trace_packet(skb, hook, in, out,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700378 table->name, private, e);
379#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* Standard target? */
381 if (!t->u.kernel.target->target) {
382 int v;
383
384 v = ((struct ipt_standard_target *)t)->verdict;
385 if (v < 0) {
386 /* Pop from stack? */
387 if (v != IPT_RETURN) {
388 verdict = (unsigned)(-v) - 1;
389 break;
390 }
391 e = back;
392 back = get_entry(table_base,
393 back->comefrom);
394 continue;
395 }
Patrick McHardy05465342005-08-21 23:31:43 -0700396 if (table_base + v != (void *)e + e->next_offset
397 && !(e->ip.flags & IPT_F_GOTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* Save old back ptr in next entry */
399 struct ipt_entry *next
400 = (void *)e + e->next_offset;
401 next->comefrom
402 = (void *)back - table_base;
403 /* set back pointer to next entry */
404 back = next;
405 }
406
407 e = get_entry(table_base, v);
408 } else {
409 /* Targets which reenter must return
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900410 abs. verdicts */
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 in, out,
417 hook,
Patrick McHardy1c524832006-03-20 18:02:15 -0800418 t->u.kernel.target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700419 t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421#ifdef CONFIG_NETFILTER_DEBUG
422 if (((struct ipt_entry *)table_base)->comefrom
423 != 0xeeeeeeec
424 && verdict == IPT_CONTINUE) {
425 printk("Target %s reentered!\n",
426 t->u.kernel.target->name);
427 verdict = NF_DROP;
428 }
429 ((struct ipt_entry *)table_base)->comefrom
430 = 0x57acc001;
431#endif
432 /* Target might have changed stuff. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700433 ip = ip_hdr(skb);
434 datalen = skb->len - ip->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (verdict == IPT_CONTINUE)
437 e = (void *)e + e->next_offset;
438 else
439 /* Verdict */
440 break;
441 }
442 } else {
443
444 no_match:
445 e = (void *)e + e->next_offset;
446 }
447 } while (!hotdrop);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 read_unlock_bh(&table->lock);
450
451#ifdef DEBUG_ALLOW_ALL
452 return NF_ACCEPT;
453#else
454 if (hotdrop)
455 return NF_DROP;
456 else return verdict;
457#endif
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/* Figures out from what hook each rule can be called: returns 0 if
461 there are loops. Puts hook bitmask in comefrom. */
462static int
Harald Welte2e4e6a12006-01-12 13:30:04 -0800463mark_source_chains(struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800464 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 unsigned int hook;
467
468 /* No recursion; use packet counter to save back ptrs (reset
469 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800470 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800472 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 if (!(valid_hooks & (1 << hook)))
475 continue;
476
477 /* Set initial back pointer. */
478 e->counters.pcnt = pos;
479
480 for (;;) {
481 struct ipt_standard_target *t
482 = (void *)ipt_get_target(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800483 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800485 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 printk("iptables: loop hook %u pos %u %08X.\n",
487 hook, pos, e->comefrom);
488 return 0;
489 }
Patrick McHardy9c547952007-12-17 21:52:00 -0800490 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* Unconditional return/END. */
Al Viroe1b4b9f2006-12-12 00:29:52 -0800493 if ((e->target_offset == sizeof(struct ipt_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 && (strcmp(t->target.u.user.name,
495 IPT_STANDARD_TARGET) == 0)
496 && t->verdict < 0
Al Viroe1b4b9f2006-12-12 00:29:52 -0800497 && unconditional(&e->ip)) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 unsigned int oldpos, size;
499
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800500 if (t->verdict < -NF_MAX_VERDICT - 1) {
501 duprintf("mark_source_chains: bad "
502 "negative verdict (%i)\n",
503 t->verdict);
504 return 0;
505 }
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Return: backtrack through the last
508 big jump. */
509 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800510 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511#ifdef DEBUG_IP_FIREWALL_USER
512 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800513 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 duprintf("Back unset "
515 "on hook %u "
516 "rule %u\n",
517 hook, pos);
518 }
519#endif
520 oldpos = pos;
521 pos = e->counters.pcnt;
522 e->counters.pcnt = 0;
523
524 /* We're at the start. */
525 if (pos == oldpos)
526 goto next;
527
528 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800529 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 } while (oldpos == pos + e->next_offset);
531
532 /* Move along one */
533 size = e->next_offset;
534 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800535 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 e->counters.pcnt = pos;
537 pos += size;
538 } else {
539 int newpos = t->verdict;
540
541 if (strcmp(t->target.u.user.name,
542 IPT_STANDARD_TARGET) == 0
543 && newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800544 if (newpos > newinfo->size -
545 sizeof(struct ipt_entry)) {
546 duprintf("mark_source_chains: "
547 "bad verdict (%i)\n",
548 newpos);
549 return 0;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* This a jump; chase it. */
552 duprintf("Jump rule %u -> %u\n",
553 pos, newpos);
554 } else {
555 /* ... this is a fallthru */
556 newpos = pos + e->next_offset;
557 }
558 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800559 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 e->counters.pcnt = pos;
561 pos = newpos;
562 }
563 }
564 next:
565 duprintf("Finished chain %u\n", hook);
566 }
567 return 1;
568}
569
570static inline int
571cleanup_match(struct ipt_entry_match *m, unsigned int *i)
572{
573 if (i && (*i)-- == 0)
574 return 1;
575
576 if (m->u.kernel.match->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700577 m->u.kernel.match->destroy(m->u.kernel.match, m->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 module_put(m->u.kernel.match->me);
579 return 0;
580}
581
582static inline int
Dmitry Mishina96be242006-12-12 00:29:26 -0800583check_entry(struct ipt_entry *e, const char *name)
584{
585 struct ipt_entry_target *t;
586
587 if (!ip_checkentry(&e->ip)) {
588 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
589 return -EINVAL;
590 }
591
Patrick McHardy9c547952007-12-17 21:52:00 -0800592 if (e->target_offset + sizeof(struct ipt_entry_target) >
593 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800594 return -EINVAL;
595
596 t = ipt_get_target(e);
597 if (e->target_offset + t->u.target_size > e->next_offset)
598 return -EINVAL;
599
600 return 0;
601}
602
603static inline int check_match(struct ipt_entry_match *m, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -0800604 const struct ipt_ip *ip,
605 unsigned int hookmask, unsigned int *i)
Dmitry Mishina96be242006-12-12 00:29:26 -0800606{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800607 struct xt_match *match;
Dmitry Mishina96be242006-12-12 00:29:26 -0800608 int ret;
609
610 match = m->u.kernel.match;
611 ret = xt_check_match(match, AF_INET, m->u.match_size - sizeof(*m),
612 name, hookmask, ip->proto,
613 ip->invflags & IPT_INV_PROTO);
614 if (!ret && m->u.kernel.match->checkentry
615 && !m->u.kernel.match->checkentry(name, ip, match, m->data,
616 hookmask)) {
617 duprintf("ip_tables: check failed for `%s'.\n",
618 m->u.kernel.match->name);
619 ret = -EINVAL;
620 }
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -0700621 if (!ret)
622 (*i)++;
Dmitry Mishina96be242006-12-12 00:29:26 -0800623 return ret;
624}
625
626static inline int
627find_check_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -0800628 const char *name,
629 const struct ipt_ip *ip,
630 unsigned int hookmask,
631 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800633 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800634 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Harald Welte2e4e6a12006-01-12 13:30:04 -0800636 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy9c547952007-12-17 21:52:00 -0800637 m->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 "ipt_%s", m->u.user.name);
639 if (IS_ERR(match) || !match) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800640 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return match ? PTR_ERR(match) : -ENOENT;
642 }
643 m->u.kernel.match = match;
644
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -0700645 ret = check_match(m, name, ip, hookmask, i);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800646 if (ret)
647 goto err;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800650err:
651 module_put(m->u.kernel.match->me);
652 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Dmitry Mishina96be242006-12-12 00:29:26 -0800655static inline int check_target(struct ipt_entry *e, const char *name)
656{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900657 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800658 struct xt_target *target;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900659 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800660
661 t = ipt_get_target(e);
662 target = t->u.kernel.target;
663 ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
664 name, e->comefrom, e->ip.proto,
665 e->ip.invflags & IPT_INV_PROTO);
666 if (!ret && t->u.kernel.target->checkentry
Patrick McHardy4b478242007-12-17 21:46:15 -0800667 && !t->u.kernel.target->checkentry(name, e, target, t->data,
668 e->comefrom)) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800669 duprintf("ip_tables: check failed for `%s'.\n",
670 t->u.kernel.target->name);
671 ret = -EINVAL;
672 }
673 return ret;
674}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676static inline int
Dmitry Mishina96be242006-12-12 00:29:26 -0800677find_check_entry(struct ipt_entry *e, const char *name, unsigned int size,
Patrick McHardy4b478242007-12-17 21:46:15 -0800678 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800681 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int ret;
683 unsigned int j;
684
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;
Dmitry Mishina96be242006-12-12 00:29:26 -0800690 ret = IPT_MATCH_ITERATE(e, find_check_match, name, &e->ip,
Patrick McHardy4b478242007-12-17 21:46:15 -0800691 e->comefrom, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (ret != 0)
693 goto cleanup_matches;
694
695 t = ipt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800696 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -0800697 t->u.user.name,
698 t->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 "ipt_%s", t->u.user.name);
700 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800701 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 ret = target ? PTR_ERR(target) : -ENOENT;
703 goto cleanup_matches;
704 }
705 t->u.kernel.target = target;
706
Dmitry Mishina96be242006-12-12 00:29:26 -0800707 ret = check_target(e, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800708 if (ret)
709 goto err;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 (*i)++;
712 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800713 err:
714 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 cleanup_matches:
716 IPT_MATCH_ITERATE(e, cleanup_match, &j);
717 return ret;
718}
719
720static inline int
721check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800722 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 unsigned char *base,
724 unsigned char *limit,
725 const unsigned int *hook_entries,
726 const unsigned int *underflows,
727 unsigned int *i)
728{
729 unsigned int h;
730
731 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0
732 || (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
733 duprintf("Bad offset %p\n", e);
734 return -EINVAL;
735 }
736
737 if (e->next_offset
738 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
739 duprintf("checking: element %p size %u\n",
740 e, e->next_offset);
741 return -EINVAL;
742 }
743
744 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800745 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if ((unsigned char *)e - base == hook_entries[h])
747 newinfo->hook_entry[h] = hook_entries[h];
748 if ((unsigned char *)e - base == underflows[h])
749 newinfo->underflow[h] = underflows[h];
750 }
751
752 /* FIXME: underflows must be unconditional, standard verdicts
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900753 < 0 (not IPT_RETURN). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800756 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 e->comefrom = 0;
758
759 (*i)++;
760 return 0;
761}
762
763static inline int
764cleanup_entry(struct ipt_entry *e, unsigned int *i)
765{
766 struct ipt_entry_target *t;
767
768 if (i && (*i)-- == 0)
769 return 1;
770
771 /* Cleanup all matches */
772 IPT_MATCH_ITERATE(e, cleanup_match, NULL);
773 t = ipt_get_target(e);
774 if (t->u.kernel.target->destroy)
Patrick McHardyefa74162006-08-22 00:36:37 -0700775 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 module_put(t->u.kernel.target->me);
777 return 0;
778}
779
780/* Checks and translates the user-supplied table segment (held in
781 newinfo) */
782static int
783translate_table(const char *name,
784 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800785 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800786 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 unsigned int size,
788 unsigned int number,
789 const unsigned int *hook_entries,
790 const unsigned int *underflows)
791{
792 unsigned int i;
793 int ret;
794
795 newinfo->size = size;
796 newinfo->number = number;
797
798 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800799 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 newinfo->hook_entry[i] = 0xFFFFFFFF;
801 newinfo->underflow[i] = 0xFFFFFFFF;
802 }
803
804 duprintf("translate_table: size %u\n", newinfo->size);
805 i = 0;
806 /* Walk through entries, checking offsets. */
Eric Dumazet31836062005-12-13 23:13:48 -0800807 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 check_entry_size_and_hooks,
809 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800810 entry0,
811 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 hook_entries, underflows, &i);
813 if (ret != 0)
814 return ret;
815
816 if (i != number) {
817 duprintf("translate_table: %u not %u entries\n",
818 i, number);
819 return -EINVAL;
820 }
821
822 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800823 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* Only hooks which are valid */
825 if (!(valid_hooks & (1 << i)))
826 continue;
827 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
828 duprintf("Invalid hook entry %u %u\n",
829 i, hook_entries[i]);
830 return -EINVAL;
831 }
832 if (newinfo->underflow[i] == 0xFFFFFFFF) {
833 duprintf("Invalid underflow %u %u\n",
834 i, underflows[i]);
835 return -EINVAL;
836 }
837 }
838
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800839 if (!mark_source_chains(newinfo, valid_hooks, entry0))
840 return -ELOOP;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /* Finally, each sanity check must pass */
843 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800844 ret = IPT_ENTRY_ITERATE(entry0, newinfo->size,
Dmitry Mishina96be242006-12-12 00:29:26 -0800845 find_check_entry, name, size, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800847 if (ret != 0) {
848 IPT_ENTRY_ITERATE(entry0, newinfo->size,
849 cleanup_entry, &i);
850 return ret;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700854 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800855 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
856 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
859 return ret;
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/* Gets counters. */
863static inline int
864add_entry_to_counter(const struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800865 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 unsigned int *i)
867{
868 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
869
870 (*i)++;
871 return 0;
872}
873
Eric Dumazet31836062005-12-13 23:13:48 -0800874static inline int
875set_entry_to_counter(const struct ipt_entry *e,
876 struct ipt_counters total[],
877 unsigned int *i)
878{
879 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
880
881 (*i)++;
882 return 0;
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800886get_counters(const struct xt_table_info *t,
887 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 unsigned int cpu;
890 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800891 unsigned int curcpu;
892
893 /* Instead of clearing (by a previous call to memset())
894 * the counters and using adds, we set the counters
895 * with data used by 'current' CPU
896 * We dont care about preemption here.
897 */
898 curcpu = raw_smp_processor_id();
899
900 i = 0;
901 IPT_ENTRY_ITERATE(t->entries[curcpu],
902 t->size,
903 set_entry_to_counter,
904 counters,
905 &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700907 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800908 if (cpu == curcpu)
909 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800911 IPT_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 t->size,
913 add_entry_to_counter,
914 counters,
915 &i);
916 }
917}
918
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800919static inline struct xt_counters * alloc_counters(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Dmitry Mishin27229712006-04-01 02:25:19 -0800921 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800922 struct xt_counters *counters;
923 struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 /* We need atomic snapshot of counters: rest doesn't change
926 (other than comefrom, which userspace doesn't care
927 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800928 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet31836062005-12-13 23:13:48 -0800929 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 if (counters == NULL)
Dmitry Mishin27229712006-04-01 02:25:19 -0800932 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 write_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800936 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 write_unlock_bh(&table->lock);
938
Dmitry Mishin27229712006-04-01 02:25:19 -0800939 return counters;
940}
941
942static int
943copy_entries_to_user(unsigned int total_size,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800944 struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800945 void __user *userptr)
946{
947 unsigned int off, num;
948 struct ipt_entry *e;
949 struct xt_counters *counters;
950 struct xt_table_info *private = table->private;
951 int ret = 0;
952 void *loc_cpu_entry;
953
954 counters = alloc_counters(table);
955 if (IS_ERR(counters))
956 return PTR_ERR(counters);
957
Eric Dumazet31836062005-12-13 23:13:48 -0800958 /* choose the copy that is on our node/cpu, ...
959 * This choice is lazy (because current thread is
960 * allowed to migrate to another cpu)
961 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800962 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800963 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 ret = -EFAULT;
965 goto free_counters;
966 }
967
968 /* FIXME: use iterator macros --RR */
969 /* ... then go back and fix counters and names */
970 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
971 unsigned int i;
972 struct ipt_entry_match *m;
973 struct ipt_entry_target *t;
974
Eric Dumazet31836062005-12-13 23:13:48 -0800975 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (copy_to_user(userptr + off
977 + offsetof(struct ipt_entry, counters),
978 &counters[num],
979 sizeof(counters[num])) != 0) {
980 ret = -EFAULT;
981 goto free_counters;
982 }
983
984 for (i = sizeof(struct ipt_entry);
985 i < e->target_offset;
986 i += m->u.match_size) {
987 m = (void *)e + i;
988
989 if (copy_to_user(userptr + off + i
990 + offsetof(struct ipt_entry_match,
991 u.user.name),
992 m->u.kernel.match->name,
993 strlen(m->u.kernel.match->name)+1)
994 != 0) {
995 ret = -EFAULT;
996 goto free_counters;
997 }
998 }
999
1000 t = ipt_get_target(e);
1001 if (copy_to_user(userptr + off + e->target_offset
1002 + offsetof(struct ipt_entry_target,
1003 u.user.name),
1004 t->u.kernel.target->name,
1005 strlen(t->u.kernel.target->name)+1) != 0) {
1006 ret = -EFAULT;
1007 goto free_counters;
1008 }
1009 }
1010
1011 free_counters:
1012 vfree(counters);
1013 return ret;
1014}
1015
Dmitry Mishin27229712006-04-01 02:25:19 -08001016#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001017static void compat_standard_from_user(void *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001018{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001019 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001020
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001021 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001022 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001023 memcpy(dst, &v, sizeof(v));
1024}
1025
1026static int compat_standard_to_user(void __user *dst, void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001027{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001028 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001029
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001030 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001031 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001032 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001033}
1034
1035static inline int
Patrick McHardy4b478242007-12-17 21:46:15 -08001036compat_calc_match(struct ipt_entry_match *m, int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001037{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001038 *size += xt_compat_match_offset(m->u.kernel.match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001039 return 0;
1040}
1041
Eric Dumazet259d4e42007-12-04 23:24:56 -08001042static int compat_calc_entry(struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001043 const struct xt_table_info *info,
1044 void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001045{
1046 struct ipt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001047 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001048 int off, i, ret;
1049
Patrick McHardy30c08c42007-12-17 21:47:14 -08001050 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001051 entry_offset = (void *)e - base;
1052 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
1053 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001054 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001055 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001056 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001057 if (ret)
1058 return ret;
1059
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001060 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001061 if (info->hook_entry[i] &&
1062 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001063 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001064 if (info->underflow[i] &&
1065 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001066 newinfo->underflow[i] -= off;
1067 }
1068 return 0;
1069}
1070
Eric Dumazet259d4e42007-12-04 23:24:56 -08001071static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001072 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001073{
1074 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001075
1076 if (!newinfo || !info)
1077 return -EINVAL;
1078
Eric Dumazet259d4e42007-12-04 23:24:56 -08001079 /* we dont care about newinfo->entries[] */
1080 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1081 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001082 loc_cpu_entry = info->entries[raw_smp_processor_id()];
1083 return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001084 compat_calc_entry, info, loc_cpu_entry,
1085 newinfo);
Dmitry Mishin27229712006-04-01 02:25:19 -08001086}
1087#endif
1088
1089static int get_info(void __user *user, int *len, int compat)
1090{
1091 char name[IPT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001092 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001093 int ret;
1094
1095 if (*len != sizeof(struct ipt_getinfo)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001096 duprintf("length %u != %zu\n", *len,
1097 sizeof(struct ipt_getinfo));
Dmitry Mishin27229712006-04-01 02:25:19 -08001098 return -EINVAL;
1099 }
1100
1101 if (copy_from_user(name, user, sizeof(name)) != 0)
1102 return -EFAULT;
1103
1104 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1105#ifdef CONFIG_COMPAT
1106 if (compat)
1107 xt_compat_lock(AF_INET);
1108#endif
1109 t = try_then_request_module(xt_find_table_lock(AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001110 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001111 if (t && !IS_ERR(t)) {
1112 struct ipt_getinfo info;
1113 struct xt_table_info *private = t->private;
1114
1115#ifdef CONFIG_COMPAT
1116 if (compat) {
1117 struct xt_table_info tmp;
1118 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001119 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001120 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001121 }
1122#endif
1123 info.valid_hooks = t->valid_hooks;
1124 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001125 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001126 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001127 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001128 info.num_entries = private->number;
1129 info.size = private->size;
1130 strcpy(info.name, name);
1131
1132 if (copy_to_user(user, &info, *len) != 0)
1133 ret = -EFAULT;
1134 else
1135 ret = 0;
1136
1137 xt_table_unlock(t);
1138 module_put(t->me);
1139 } else
1140 ret = t ? PTR_ERR(t) : -ENOENT;
1141#ifdef CONFIG_COMPAT
1142 if (compat)
1143 xt_compat_unlock(AF_INET);
1144#endif
1145 return ret;
1146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001149get_entries(struct ipt_get_entries __user *uptr, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001152 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001153 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Dmitry Mishin27229712006-04-01 02:25:19 -08001155 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001156 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001157 return -EINVAL;
1158 }
1159 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1160 return -EFAULT;
1161 if (*len != sizeof(struct ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001162 duprintf("get_entries: %u != %zu\n",
1163 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001164 return -EINVAL;
1165 }
1166
1167 t = xt_find_table_lock(AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (t && !IS_ERR(t)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001169 struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001170 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001171 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001172 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 t, uptr->entrytable);
1174 else {
1175 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001176 private->size, get.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 ret = -EINVAL;
1178 }
1179 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001180 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 } else
1182 ret = t ? PTR_ERR(t) : -ENOENT;
1183
1184 return ret;
1185}
1186
1187static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001188__do_replace(const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001189 struct xt_table_info *newinfo, unsigned int num_counters,
1190 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001191{
1192 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001193 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001194 struct xt_table_info *oldinfo;
1195 struct xt_counters *counters;
1196 void *loc_cpu_old_entry;
1197
1198 ret = 0;
1199 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1200 if (!counters) {
1201 ret = -ENOMEM;
1202 goto out;
1203 }
1204
1205 t = try_then_request_module(xt_find_table_lock(AF_INET, name),
1206 "iptable_%s", name);
1207 if (!t || IS_ERR(t)) {
1208 ret = t ? PTR_ERR(t) : -ENOENT;
1209 goto free_newinfo_counters_untrans;
1210 }
1211
1212 /* You lied! */
1213 if (valid_hooks != t->valid_hooks) {
1214 duprintf("Valid hook crap: %08X vs %08X\n",
1215 valid_hooks, t->valid_hooks);
1216 ret = -EINVAL;
1217 goto put_module;
1218 }
1219
1220 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1221 if (!oldinfo)
1222 goto put_module;
1223
1224 /* Update module usage count based on number of rules */
1225 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1226 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1227 if ((oldinfo->number > oldinfo->initial_entries) ||
1228 (newinfo->number <= oldinfo->initial_entries))
1229 module_put(t->me);
1230 if ((oldinfo->number > oldinfo->initial_entries) &&
1231 (newinfo->number <= oldinfo->initial_entries))
1232 module_put(t->me);
1233
1234 /* Get the old counters. */
1235 get_counters(oldinfo, counters);
1236 /* Decrease module usage counts and free resource */
1237 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Patrick McHardy4b478242007-12-17 21:46:15 -08001238 IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1239 NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001240 xt_free_table_info(oldinfo);
1241 if (copy_to_user(counters_ptr, counters,
1242 sizeof(struct xt_counters) * num_counters) != 0)
1243 ret = -EFAULT;
1244 vfree(counters);
1245 xt_table_unlock(t);
1246 return ret;
1247
1248 put_module:
1249 module_put(t->me);
1250 xt_table_unlock(t);
1251 free_newinfo_counters_untrans:
1252 vfree(counters);
1253 out:
1254 return ret;
1255}
1256
1257static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258do_replace(void __user *user, unsigned int len)
1259{
1260 int ret;
1261 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001262 struct xt_table_info *newinfo;
1263 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1266 return -EFAULT;
1267
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001268 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001269 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1270 return -ENOMEM;
1271
Harald Welte2e4e6a12006-01-12 13:30:04 -08001272 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (!newinfo)
1274 return -ENOMEM;
1275
Patrick McHardy9c547952007-12-17 21:52:00 -08001276 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001277 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1278 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 tmp.size) != 0) {
1280 ret = -EFAULT;
1281 goto free_newinfo;
1282 }
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001285 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 tmp.hook_entry, tmp.underflow);
1287 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001288 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 duprintf("ip_tables: Translated table\n");
1291
Patrick McHardy4b478242007-12-17 21:46:15 -08001292 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1293 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001294 if (ret)
1295 goto free_newinfo_untrans;
1296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Dmitry Mishin27229712006-04-01 02:25:19 -08001298 free_newinfo_untrans:
Patrick McHardy9c547952007-12-17 21:52:00 -08001299 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001301 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return ret;
1303}
1304
1305/* We're lazy, and add to the first CPU; overflow works its fey magic
1306 * and everything is OK. */
1307static inline int
1308add_counter_to_entry(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001309 const struct xt_counters addme[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 unsigned int *i)
1311{
1312#if 0
1313 duprintf("add_counter: Entry %u %lu/%lu + %lu/%lu\n",
1314 *i,
1315 (long unsigned int)e->counters.pcnt,
1316 (long unsigned int)e->counters.bcnt,
1317 (long unsigned int)addme[*i].pcnt,
1318 (long unsigned int)addme[*i].bcnt);
1319#endif
1320
1321 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1322
1323 (*i)++;
1324 return 0;
1325}
1326
1327static int
Dmitry Mishin27229712006-04-01 02:25:19 -08001328do_add_counters(void __user *user, unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 unsigned int i;
Dmitry Mishin27229712006-04-01 02:25:19 -08001331 struct xt_counters_info tmp;
1332 struct xt_counters *paddc;
1333 unsigned int num_counters;
1334 char *name;
1335 int size;
1336 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001337 struct xt_table *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001338 struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001340 void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -08001341#ifdef CONFIG_COMPAT
1342 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Dmitry Mishin27229712006-04-01 02:25:19 -08001344 if (compat) {
1345 ptmp = &compat_tmp;
1346 size = sizeof(struct compat_xt_counters_info);
1347 } else
1348#endif
1349 {
1350 ptmp = &tmp;
1351 size = sizeof(struct xt_counters_info);
1352 }
1353
1354 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -EFAULT;
1356
Dmitry Mishin27229712006-04-01 02:25:19 -08001357#ifdef CONFIG_COMPAT
1358 if (compat) {
1359 num_counters = compat_tmp.num_counters;
1360 name = compat_tmp.name;
1361 } else
1362#endif
1363 {
1364 num_counters = tmp.num_counters;
1365 name = tmp.name;
1366 }
1367
1368 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EINVAL;
1370
Dmitry Mishin27229712006-04-01 02:25:19 -08001371 paddc = vmalloc_node(len - size, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (!paddc)
1373 return -ENOMEM;
1374
Dmitry Mishin27229712006-04-01 02:25:19 -08001375 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 ret = -EFAULT;
1377 goto free;
1378 }
1379
Dmitry Mishin27229712006-04-01 02:25:19 -08001380 t = xt_find_table_lock(AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (!t || IS_ERR(t)) {
1382 ret = t ? PTR_ERR(t) : -ENOENT;
1383 goto free;
1384 }
1385
1386 write_lock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001387 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001388 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 ret = -EINVAL;
1390 goto unlock_up_free;
1391 }
1392
1393 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001394 /* Choose the copy that is on our node */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001395 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -08001396 IPT_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001397 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 add_counter_to_entry,
Dmitry Mishin27229712006-04-01 02:25:19 -08001399 paddc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 &i);
1401 unlock_up_free:
1402 write_unlock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001403 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 module_put(t->me);
1405 free:
1406 vfree(paddc);
1407
1408 return ret;
1409}
1410
Dmitry Mishin27229712006-04-01 02:25:19 -08001411#ifdef CONFIG_COMPAT
1412struct compat_ipt_replace {
1413 char name[IPT_TABLE_MAXNAMELEN];
1414 u32 valid_hooks;
1415 u32 num_entries;
1416 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001417 u32 hook_entry[NF_INET_NUMHOOKS];
1418 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001419 u32 num_counters;
1420 compat_uptr_t counters; /* struct ipt_counters * */
1421 struct compat_ipt_entry entries[0];
1422};
1423
Patrick McHardya18aa312007-12-12 10:35:16 -08001424static int
1425compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
1426 compat_uint_t *size, struct xt_counters *counters,
1427 unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001428{
Al Viro3e597c62006-09-24 23:42:20 +01001429 struct ipt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001430 struct compat_ipt_entry __user *ce;
1431 u_int16_t target_offset, next_offset;
1432 compat_uint_t origsize;
1433 int ret;
1434
1435 ret = -EFAULT;
1436 origsize = *size;
1437 ce = (struct compat_ipt_entry __user *)*dstptr;
Patrick McHardy78000072006-05-03 23:20:27 -07001438 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
Dmitry Mishin27229712006-04-01 02:25:19 -08001439 goto out;
1440
Patrick McHardya18aa312007-12-12 10:35:16 -08001441 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1442 goto out;
1443
Dmitry Mishin27229712006-04-01 02:25:19 -08001444 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001445 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1446
Patrick McHardyac8e27f2007-12-17 21:45:52 -08001447 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001448 target_offset = e->target_offset - (origsize - *size);
1449 if (ret)
1450 goto out;
1451 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001452 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001453 if (ret)
1454 goto out;
1455 ret = -EFAULT;
1456 next_offset = e->next_offset - (origsize - *size);
Patrick McHardy78000072006-05-03 23:20:27 -07001457 if (put_user(target_offset, &ce->target_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001458 goto out;
Patrick McHardy78000072006-05-03 23:20:27 -07001459 if (put_user(next_offset, &ce->next_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001460 goto out;
Patrick McHardya18aa312007-12-12 10:35:16 -08001461
1462 (*i)++;
Dmitry Mishin27229712006-04-01 02:25:19 -08001463 return 0;
1464out:
1465 return ret;
1466}
1467
1468static inline int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001469compat_find_calc_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001470 const char *name,
1471 const struct ipt_ip *ip,
1472 unsigned int hookmask,
1473 int *size, int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001474{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001475 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001476
1477 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001478 m->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001479 "ipt_%s", m->u.user.name);
1480 if (IS_ERR(match) || !match) {
1481 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001482 m->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001483 return match ? PTR_ERR(match) : -ENOENT;
1484 }
1485 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001486 *size += xt_compat_match_offset(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001487
1488 (*i)++;
1489 return 0;
1490}
1491
1492static inline int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001493compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1494{
1495 if (i && (*i)-- == 0)
1496 return 1;
1497
1498 module_put(m->u.kernel.match->me);
1499 return 0;
1500}
1501
1502static inline int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001503compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001504{
1505 struct ipt_entry_target *t;
1506
1507 if (i && (*i)-- == 0)
1508 return 1;
1509
1510 /* Cleanup all matches */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001511 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1512 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001513 module_put(t->u.kernel.target->me);
1514 return 0;
1515}
1516
1517static inline int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001518check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001519 struct xt_table_info *newinfo,
1520 unsigned int *size,
1521 unsigned char *base,
1522 unsigned char *limit,
1523 unsigned int *hook_entries,
1524 unsigned int *underflows,
1525 unsigned int *i,
1526 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001527{
1528 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001529 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001530 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001531 int ret, off, h, j;
1532
1533 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1534 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0
1535 || (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1536 duprintf("Bad offset %p, limit = %p\n", e, limit);
1537 return -EINVAL;
1538 }
1539
1540 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001541 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001542 duprintf("checking: element %p size %u\n",
1543 e, e->next_offset);
1544 return -EINVAL;
1545 }
1546
Patrick McHardy73cd5982007-12-17 21:47:32 -08001547 /* For purposes of check_entry casting the compat entry is fine */
1548 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001549 if (ret)
1550 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001551
Patrick McHardy30c08c42007-12-17 21:47:14 -08001552 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001553 entry_offset = (void *)e - (void *)base;
1554 j = 0;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001555 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1556 &e->ip, e->comefrom, &off, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001557 if (ret != 0)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001558 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001559
Patrick McHardy73cd5982007-12-17 21:47:32 -08001560 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001561 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -08001562 t->u.user.name,
1563 t->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001564 "ipt_%s", t->u.user.name);
1565 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001566 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001567 t->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001568 ret = target ? PTR_ERR(target) : -ENOENT;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001569 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001570 }
1571 t->u.kernel.target = target;
1572
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001573 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001574 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001575 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001576 if (ret)
1577 goto out;
1578
1579 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001580 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001581 if ((unsigned char *)e - base == hook_entries[h])
1582 newinfo->hook_entry[h] = hook_entries[h];
1583 if ((unsigned char *)e - base == underflows[h])
1584 newinfo->underflow[h] = underflows[h];
1585 }
1586
1587 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001588 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001589 e->comefrom = 0;
1590
1591 (*i)++;
1592 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001593
Dmitry Mishin27229712006-04-01 02:25:19 -08001594out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001595 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001596release_matches:
1597 IPT_MATCH_ITERATE(e, compat_release_match, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001598 return ret;
1599}
1600
Patrick McHardy4b478242007-12-17 21:46:15 -08001601static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001602compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001603 unsigned int *size, const char *name,
1604 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001605{
1606 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001607 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001608 struct ipt_entry *de;
1609 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001610 int ret, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001611
1612 ret = 0;
1613 origsize = *size;
1614 de = (struct ipt_entry *)*dstptr;
1615 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001616 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001617
Patrick McHardy73cd5982007-12-17 21:47:32 -08001618 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001619 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1620
Patrick McHardy73cd5982007-12-17 21:47:32 -08001621 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1622 dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001623 if (ret)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001624 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001625 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001626 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001627 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001628 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001629
1630 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001631 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001632 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1633 newinfo->hook_entry[h] -= origsize - *size;
1634 if ((unsigned char *)de - base < newinfo->underflow[h])
1635 newinfo->underflow[h] -= origsize - *size;
1636 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001637 return ret;
1638}
Dmitry Mishin27229712006-04-01 02:25:19 -08001639
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001640static inline int compat_check_entry(struct ipt_entry *e, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001641 unsigned int *i)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001642{
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001643 int j, ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001644
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001645 j = 0;
Patrick McHardy9c547952007-12-17 21:52:00 -08001646 ret = IPT_MATCH_ITERATE(e, check_match, name, &e->ip,
1647 e->comefrom, &j);
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001648 if (ret)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001649 goto cleanup_matches;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001650
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001651 ret = check_target(e, name);
1652 if (ret)
1653 goto cleanup_matches;
1654
1655 (*i)++;
1656 return 0;
1657
1658 cleanup_matches:
1659 IPT_MATCH_ITERATE(e, cleanup_match, &j);
1660 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001661}
1662
Dmitry Mishin27229712006-04-01 02:25:19 -08001663static int
1664translate_compat_table(const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001665 unsigned int valid_hooks,
1666 struct xt_table_info **pinfo,
1667 void **pentry0,
1668 unsigned int total_size,
1669 unsigned int number,
1670 unsigned int *hook_entries,
1671 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001672{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001673 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001674 struct xt_table_info *newinfo, *info;
1675 void *pos, *entry0, *entry1;
1676 unsigned int size;
1677 int ret;
1678
1679 info = *pinfo;
1680 entry0 = *pentry0;
1681 size = total_size;
1682 info->number = number;
1683
1684 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001685 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001686 info->hook_entry[i] = 0xFFFFFFFF;
1687 info->underflow[i] = 0xFFFFFFFF;
1688 }
1689
1690 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001691 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001692 xt_compat_lock(AF_INET);
1693 /* Walk through entries, checking offsets. */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001694 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
1695 check_compat_entry_size_and_hooks,
1696 info, &size, entry0,
1697 entry0 + total_size,
1698 hook_entries, underflows, &j, name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001699 if (ret != 0)
1700 goto out_unlock;
1701
1702 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001703 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001704 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001705 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001706 goto out_unlock;
1707 }
1708
1709 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001710 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001711 /* Only hooks which are valid */
1712 if (!(valid_hooks & (1 << i)))
1713 continue;
1714 if (info->hook_entry[i] == 0xFFFFFFFF) {
1715 duprintf("Invalid hook entry %u %u\n",
1716 i, hook_entries[i]);
1717 goto out_unlock;
1718 }
1719 if (info->underflow[i] == 0xFFFFFFFF) {
1720 duprintf("Invalid underflow %u %u\n",
1721 i, underflows[i]);
1722 goto out_unlock;
1723 }
1724 }
1725
1726 ret = -ENOMEM;
1727 newinfo = xt_alloc_table_info(size);
1728 if (!newinfo)
1729 goto out_unlock;
1730
1731 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001732 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001733 newinfo->hook_entry[i] = info->hook_entry[i];
1734 newinfo->underflow[i] = info->underflow[i];
1735 }
1736 entry1 = newinfo->entries[raw_smp_processor_id()];
1737 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001738 size = total_size;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001739 ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size,
Patrick McHardy9c547952007-12-17 21:52:00 -08001740 compat_copy_entry_from_user,
1741 &pos, &size, name, newinfo, entry1);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001742 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001743 xt_compat_unlock(AF_INET);
1744 if (ret)
1745 goto free_newinfo;
1746
1747 ret = -ELOOP;
1748 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1749 goto free_newinfo;
1750
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001751 i = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001752 ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001753 name, &i);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001754 if (ret) {
1755 j -= i;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001756 COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1757 compat_release_entry, &j);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001758 IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1759 xt_free_table_info(newinfo);
1760 return ret;
1761 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001762
Dmitry Mishin27229712006-04-01 02:25:19 -08001763 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001764 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001765 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1766 memcpy(newinfo->entries[i], entry1, newinfo->size);
1767
1768 *pinfo = newinfo;
1769 *pentry0 = entry1;
1770 xt_free_table_info(info);
1771 return 0;
1772
1773free_newinfo:
1774 xt_free_table_info(newinfo);
1775out:
Patrick McHardy73cd5982007-12-17 21:47:32 -08001776 COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001777 return ret;
1778out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001779 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001780 xt_compat_unlock(AF_INET);
1781 goto out;
1782}
1783
1784static int
1785compat_do_replace(void __user *user, unsigned int len)
1786{
1787 int ret;
1788 struct compat_ipt_replace tmp;
1789 struct xt_table_info *newinfo;
1790 void *loc_cpu_entry;
1791
1792 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1793 return -EFAULT;
1794
Dmitry Mishin27229712006-04-01 02:25:19 -08001795 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001796 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001797 return -ENOMEM;
1798 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1799 return -ENOMEM;
1800
1801 newinfo = xt_alloc_table_info(tmp.size);
1802 if (!newinfo)
1803 return -ENOMEM;
1804
Patrick McHardy9c547952007-12-17 21:52:00 -08001805 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001806 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1807 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1808 tmp.size) != 0) {
1809 ret = -EFAULT;
1810 goto free_newinfo;
1811 }
1812
1813 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001814 &newinfo, &loc_cpu_entry, tmp.size,
1815 tmp.num_entries, tmp.hook_entry,
1816 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001817 if (ret != 0)
1818 goto free_newinfo;
1819
1820 duprintf("compat_do_replace: Translated table\n");
1821
Patrick McHardy4b478242007-12-17 21:46:15 -08001822 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1823 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001824 if (ret)
1825 goto free_newinfo_untrans;
1826 return 0;
1827
1828 free_newinfo_untrans:
Patrick McHardy4b478242007-12-17 21:46:15 -08001829 IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Dmitry Mishin27229712006-04-01 02:25:19 -08001830 free_newinfo:
1831 xt_free_table_info(newinfo);
1832 return ret;
1833}
1834
1835static int
1836compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001837 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001838{
1839 int ret;
1840
1841 if (!capable(CAP_NET_ADMIN))
1842 return -EPERM;
1843
1844 switch (cmd) {
1845 case IPT_SO_SET_REPLACE:
1846 ret = compat_do_replace(user, len);
1847 break;
1848
1849 case IPT_SO_SET_ADD_COUNTERS:
1850 ret = do_add_counters(user, len, 1);
1851 break;
1852
1853 default:
1854 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1855 ret = -EINVAL;
1856 }
1857
1858 return ret;
1859}
1860
Patrick McHardy4b478242007-12-17 21:46:15 -08001861struct compat_ipt_get_entries {
Dmitry Mishin27229712006-04-01 02:25:19 -08001862 char name[IPT_TABLE_MAXNAMELEN];
1863 compat_uint_t size;
1864 struct compat_ipt_entry entrytable[0];
1865};
1866
Patrick McHardy4b478242007-12-17 21:46:15 -08001867static int
1868compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1869 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001870{
Dmitry Mishin27229712006-04-01 02:25:19 -08001871 struct xt_counters *counters;
1872 struct xt_table_info *private = table->private;
1873 void __user *pos;
1874 unsigned int size;
1875 int ret = 0;
1876 void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001877 unsigned int i = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001878
1879 counters = alloc_counters(table);
1880 if (IS_ERR(counters))
1881 return PTR_ERR(counters);
1882
1883 /* choose the copy that is on our node/cpu, ...
1884 * This choice is lazy (because current thread is
1885 * allowed to migrate to another cpu)
1886 */
1887 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1888 pos = userptr;
1889 size = total_size;
1890 ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
Patrick McHardya18aa312007-12-12 10:35:16 -08001891 compat_copy_entry_to_user,
1892 &pos, &size, counters, &i);
Dmitry Mishin27229712006-04-01 02:25:19 -08001893
Dmitry Mishin27229712006-04-01 02:25:19 -08001894 vfree(counters);
1895 return ret;
1896}
1897
1898static int
1899compat_get_entries(struct compat_ipt_get_entries __user *uptr, int *len)
1900{
1901 int ret;
1902 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001903 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001904
Dmitry Mishin27229712006-04-01 02:25:19 -08001905 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001906 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001907 return -EINVAL;
1908 }
1909
1910 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1911 return -EFAULT;
1912
1913 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001914 duprintf("compat_get_entries: %u != %zu\n",
1915 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001916 return -EINVAL;
1917 }
1918
1919 xt_compat_lock(AF_INET);
1920 t = xt_find_table_lock(AF_INET, get.name);
1921 if (t && !IS_ERR(t)) {
1922 struct xt_table_info *private = t->private;
1923 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08001924 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001925 ret = compat_table_info(private, &info);
1926 if (!ret && get.size == info.size) {
1927 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001928 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001929 } else if (!ret) {
1930 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001931 private->size, get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001932 ret = -EINVAL;
1933 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001934 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001935 module_put(t->me);
1936 xt_table_unlock(t);
1937 } else
1938 ret = t ? PTR_ERR(t) : -ENOENT;
1939
1940 xt_compat_unlock(AF_INET);
1941 return ret;
1942}
1943
Patrick McHardy79030ed2006-09-20 12:05:08 -07001944static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1945
Dmitry Mishin27229712006-04-01 02:25:19 -08001946static int
1947compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1948{
1949 int ret;
1950
Björn Steinbrink82fac052006-10-20 00:21:10 -07001951 if (!capable(CAP_NET_ADMIN))
1952 return -EPERM;
1953
Dmitry Mishin27229712006-04-01 02:25:19 -08001954 switch (cmd) {
1955 case IPT_SO_GET_INFO:
1956 ret = get_info(user, len, 1);
1957 break;
1958 case IPT_SO_GET_ENTRIES:
1959 ret = compat_get_entries(user, len);
1960 break;
1961 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001962 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001963 }
1964 return ret;
1965}
1966#endif
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001969do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 int ret;
1972
1973 if (!capable(CAP_NET_ADMIN))
1974 return -EPERM;
1975
1976 switch (cmd) {
1977 case IPT_SO_SET_REPLACE:
1978 ret = do_replace(user, len);
1979 break;
1980
1981 case IPT_SO_SET_ADD_COUNTERS:
Dmitry Mishin27229712006-04-01 02:25:19 -08001982 ret = do_add_counters(user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 break;
1984
1985 default:
1986 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1987 ret = -EINVAL;
1988 }
1989
1990 return ret;
1991}
1992
1993static int
1994do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1995{
1996 int ret;
1997
1998 if (!capable(CAP_NET_ADMIN))
1999 return -EPERM;
2000
2001 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002002 case IPT_SO_GET_INFO:
2003 ret = get_info(user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002005
2006 case IPT_SO_GET_ENTRIES:
2007 ret = get_entries(user, len);
2008 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 case IPT_SO_GET_REVISION_MATCH:
2011 case IPT_SO_GET_REVISION_TARGET: {
2012 struct ipt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002013 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 if (*len != sizeof(rev)) {
2016 ret = -EINVAL;
2017 break;
2018 }
2019 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2020 ret = -EFAULT;
2021 break;
2022 }
2023
2024 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002025 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002027 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Harald Welte2e4e6a12006-01-12 13:30:04 -08002029 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2030 rev.revision,
2031 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 "ipt_%s", rev.name);
2033 break;
2034 }
2035
2036 default:
2037 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2038 ret = -EINVAL;
2039 }
2040
2041 return ret;
2042}
2043
Harald Welte2e4e6a12006-01-12 13:30:04 -08002044int ipt_register_table(struct xt_table *table, const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002047 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08002048 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08002050 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Harald Welte2e4e6a12006-01-12 13:30:04 -08002052 newinfo = xt_alloc_table_info(repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (!newinfo)
2054 return -ENOMEM;
2055
Patrick McHardy9c547952007-12-17 21:52:00 -08002056 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002057 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2058 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08002061 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 repl->num_entries,
2063 repl->hook_entry,
2064 repl->underflow);
2065 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08002066 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return ret;
2068 }
2069
Patrick McHardyda298d32006-06-27 03:00:09 -07002070 ret = xt_register_table(table, &bootstrap, newinfo);
2071 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08002072 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return ret;
2074 }
2075
Harald Welte2e4e6a12006-01-12 13:30:04 -08002076 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077}
2078
Jan Engelhardte60a13e2007-02-07 15:12:33 -08002079void ipt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002081 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002082 void *loc_cpu_entry;
2083
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002084 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002087 loc_cpu_entry = private->entries[raw_smp_processor_id()];
2088 IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, NULL);
2089 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
2092/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002093static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2095 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002096 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Patrick McHardy9c547952007-12-17 21:52:00 -08002098 return ((test_type == 0xFF) ||
2099 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 ^ invert;
2101}
2102
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002103static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104icmp_match(const struct sk_buff *skb,
2105 const struct net_device *in,
2106 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -08002107 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 const void *matchinfo,
2109 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -08002110 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -07002111 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 struct icmphdr _icmph, *ic;
2114 const struct ipt_icmp *icmpinfo = matchinfo;
2115
2116 /* Must not be a fragment. */
2117 if (offset)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002118 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Harald Welte2e4e6a12006-01-12 13:30:04 -08002120 ic = skb_header_pointer(skb, protoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (ic == NULL) {
2122 /* We've been asked to examine this packet, and we
2123 * can't. Hence, no choice but to drop.
2124 */
2125 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -07002126 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002127 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
2129
2130 return icmp_type_code_match(icmpinfo->type,
2131 icmpinfo->code[0],
2132 icmpinfo->code[1],
2133 ic->type, ic->code,
2134 !!(icmpinfo->invflags&IPT_ICMP_INV));
2135}
2136
2137/* Called when user tries to insert an entry of this type. */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07002138static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139icmp_checkentry(const char *tablename,
Patrick McHardy9c547952007-12-17 21:52:00 -08002140 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -08002141 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 unsigned int hook_mask)
2144{
2145 const struct ipt_icmp *icmpinfo = matchinfo;
2146
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002147 /* Must specify no unknown invflags */
2148 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
2151/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07002152static struct xt_target ipt_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 .name = IPT_STANDARD_TARGET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002154 .targetsize = sizeof(int),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002155 .family = AF_INET,
Dmitry Mishin27229712006-04-01 02:25:19 -08002156#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07002157 .compatsize = sizeof(compat_int_t),
2158 .compat_from_user = compat_standard_from_user,
2159 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002160#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161};
2162
Patrick McHardy9f15c532007-07-07 22:22:02 -07002163static struct xt_target ipt_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 .name = IPT_ERROR_TARGET,
2165 .target = ipt_error,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002166 .targetsize = IPT_FUNCTION_MAXNAMELEN,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002167 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168};
2169
2170static struct nf_sockopt_ops ipt_sockopts = {
2171 .pf = PF_INET,
2172 .set_optmin = IPT_BASE_CTL,
2173 .set_optmax = IPT_SO_SET_MAX+1,
2174 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002175#ifdef CONFIG_COMPAT
2176 .compat_set = compat_do_ipt_set_ctl,
2177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 .get_optmin = IPT_BASE_CTL,
2179 .get_optmax = IPT_SO_GET_MAX+1,
2180 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002181#ifdef CONFIG_COMPAT
2182 .compat_get = compat_do_ipt_get_ctl,
2183#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002184 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185};
2186
Patrick McHardy9f15c532007-07-07 22:22:02 -07002187static struct xt_match icmp_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 .name = "icmp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002189 .match = icmp_match,
2190 .matchsize = sizeof(struct ipt_icmp),
Patrick McHardy9c547952007-12-17 21:52:00 -08002191 .checkentry = icmp_checkentry,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002192 .proto = IPPROTO_ICMP,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002193 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194};
2195
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002196static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 int ret;
2199
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002200 ret = xt_proto_init(AF_INET);
2201 if (ret < 0)
2202 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002205 ret = xt_register_target(&ipt_standard_target);
2206 if (ret < 0)
2207 goto err2;
2208 ret = xt_register_target(&ipt_error_target);
2209 if (ret < 0)
2210 goto err3;
2211 ret = xt_register_match(&icmp_matchstruct);
2212 if (ret < 0)
2213 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 /* Register setsockopt */
2216 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002217 if (ret < 0)
2218 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Dan Aloni0236e662007-07-09 13:20:12 -07002220 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002222
2223err5:
2224 xt_unregister_match(&icmp_matchstruct);
2225err4:
2226 xt_unregister_target(&ipt_error_target);
2227err3:
2228 xt_unregister_target(&ipt_standard_target);
2229err2:
2230 xt_proto_fini(AF_INET);
2231err1:
2232 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002235static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002238
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002239 xt_unregister_match(&icmp_matchstruct);
2240 xt_unregister_target(&ipt_error_target);
2241 xt_unregister_target(&ipt_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002242
2243 xt_proto_fini(AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
2246EXPORT_SYMBOL(ipt_register_table);
2247EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002249module_init(ip_tables_init);
2250module_exit(ip_tables_fini);