blob: b43280aad8a2d27e0552efcf1751bd838e185dd3 [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 */
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +020011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/cache.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080013#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/skbuff.h>
15#include <linux/kmod.h>
16#include <linux/vmalloc.h>
17#include <linux/netdevice.h>
18#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/icmp.h>
20#include <net/ip.h>
Dmitry Mishin27229712006-04-01 02:25:19 -080021#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/uaccess.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -080023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/proc_fs.h>
25#include <linux/err.h>
David S. Millerc8923c62005-10-13 14:41:23 -070026#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Harald Welte2e4e6a12006-01-12 13:30:04 -080028#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/netfilter_ipv4/ip_tables.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080030#include <net/netfilter/nf_log.h>
Jan Engelhardte3eaa992009-06-17 22:14:54 +020031#include "../../netfilter/xt_repldata.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33MODULE_LICENSE("GPL");
34MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
35MODULE_DESCRIPTION("IPv4 packet filter");
36
37/*#define DEBUG_IP_FIREWALL*/
38/*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging */
39/*#define DEBUG_IP_FIREWALL_USER*/
40
41#ifdef DEBUG_IP_FIREWALL
42#define dprintf(format, args...) printk(format , ## args)
43#else
44#define dprintf(format, args...)
45#endif
46
47#ifdef DEBUG_IP_FIREWALL_USER
48#define duprintf(format, args...) printk(format , ## args)
49#else
50#define duprintf(format, args...)
51#endif
52
53#ifdef CONFIG_NETFILTER_DEBUG
54#define IP_NF_ASSERT(x) \
55do { \
56 if (!(x)) \
57 printk("IP_NF_ASSERT: %s:%s:%u\n", \
Harvey Harrison0dc47872008-03-05 20:47:47 -080058 __func__, __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070059} while(0)
60#else
61#define IP_NF_ASSERT(x)
62#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#if 0
65/* All the better to debug you with... */
66#define static
67#define inline
68#endif
69
Jan Engelhardte3eaa992009-06-17 22:14:54 +020070void *ipt_alloc_initial_table(const struct xt_table *info)
71{
72 return xt_alloc_initial_table(ipt, IPT);
73}
74EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
77 We keep a set of rules for each CPU, so we can avoid write-locking
78 them in the softirq when updating the counters and therefore
79 only need to read-lock in the softirq; doing a write_lock_bh() in user
80 context stops packets coming through and allows user context to read
81 the counters or update the rules.
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 Hence the start of any table is given by get_table() below. */
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Returns whether matches rule or not. */
Denys Vlasenko022748a2008-01-14 23:44:05 -080086/* Performance critical - called for every packet */
Patrick McHardy9c547952007-12-17 21:52:00 -080087static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070088ip_packet_match(const struct iphdr *ip,
89 const char *indev,
90 const char *outdev,
91 const struct ipt_ip *ipinfo,
92 int isfrag)
93{
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 unsigned long ret;
95
Jan Engelhardte79ec502007-12-17 22:44:06 -080096#define FWINV(bool, invflg) ((bool) ^ !!(ipinfo->invflags & (invflg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
Joe Perches3666ed12009-11-23 23:17:06 +010099 IPT_INV_SRCIP) ||
100 FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
101 IPT_INV_DSTIP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dprintf("Source or dest mismatch.\n");
103
Harvey Harrisoncffee382008-10-31 00:53:08 -0700104 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
105 &ip->saddr, &ipinfo->smsk.s_addr, &ipinfo->src.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
Harvey Harrisoncffee382008-10-31 00:53:08 -0700107 dprintf("DST: %pI4 Mask: %pI4 Target: %pI4.%s\n",
108 &ip->daddr, &ipinfo->dmsk.s_addr, &ipinfo->dst.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800110 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
Eric Dumazetb8dfe492009-03-25 17:31:52 +0100113 ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
116 dprintf("VIA in mismatch (%s vs %s).%s\n",
117 indev, ipinfo->iniface,
118 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800119 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
Eric Dumazetb8dfe492009-03-25 17:31:52 +0100122 ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
125 dprintf("VIA out mismatch (%s vs %s).%s\n",
126 outdev, ipinfo->outiface,
127 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800128 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130
131 /* Check specific protocol */
Joe Perches3666ed12009-11-23 23:17:06 +0100132 if (ipinfo->proto &&
133 FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 dprintf("Packet protocol %hi does not match %hi.%s\n",
135 ip->protocol, ipinfo->proto,
136 ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800137 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
140 /* If we have a fragment rule but the packet is not a fragment
141 * then we return zero */
142 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
143 dprintf("Fragment rule but not fragment.%s\n",
144 ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800145 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
Patrick McHardy9c547952007-12-17 21:52:00 -0800148 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Denys Vlasenko022748a2008-01-14 23:44:05 -0800151static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152ip_checkentry(const struct ipt_ip *ip)
153{
154 if (ip->flags & ~IPT_F_MASK) {
155 duprintf("Unknown flag bits set: %08X\n",
156 ip->flags & ~IPT_F_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700157 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159 if (ip->invflags & ~IPT_INV_MASK) {
160 duprintf("Unknown invflag bits set: %08X\n",
161 ip->invflags & ~IPT_INV_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700162 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700164 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200168ipt_error(struct sk_buff *skb, const struct xt_target_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 if (net_ratelimit())
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200171 printk("ip_tables: error: `%s'\n",
172 (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 return NF_DROP;
175}
176
Denys Vlasenko022748a2008-01-14 23:44:05 -0800177/* Performance critical - called for every packet */
178static inline bool
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200179do_match(const struct ipt_entry_match *m, const struct sk_buff *skb,
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200180 struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200182 par->match = m->u.kernel.match;
183 par->matchinfo = m->data;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* Stop iteration if it doesn't match */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200186 if (!m->u.kernel.match->match(skb, par))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700187 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 else
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700189 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Denys Vlasenko022748a2008-01-14 23:44:05 -0800192/* Performance critical */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static inline struct ipt_entry *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200194get_entry(const void *base, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 return (struct ipt_entry *)(base + offset);
197}
198
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700199/* All zeroes == unconditional rule. */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800200/* Mildly perf critical (only if packet tracing is on) */
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200201static inline bool unconditional(const struct ipt_ip *ip)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700202{
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200203 static const struct ipt_ip uncond;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700204
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200205 return memcmp(ip, &uncond, sizeof(uncond)) == 0;
Jan Engelhardte79ec502007-12-17 22:44:06 -0800206#undef FWINV
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700207}
208
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200209/* for const-correctness */
210static inline const struct ipt_entry_target *
211ipt_get_target_c(const struct ipt_entry *e)
212{
213 return ipt_get_target((struct ipt_entry *)e);
214}
215
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700216#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
217 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
Denys Vlasenko022748a2008-01-14 23:44:05 -0800218static const char *const hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800219 [NF_INET_PRE_ROUTING] = "PREROUTING",
220 [NF_INET_LOCAL_IN] = "INPUT",
Patrick McHardy9c547952007-12-17 21:52:00 -0800221 [NF_INET_FORWARD] = "FORWARD",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800222 [NF_INET_LOCAL_OUT] = "OUTPUT",
223 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700224};
225
226enum nf_ip_trace_comments {
227 NF_IP_TRACE_COMMENT_RULE,
228 NF_IP_TRACE_COMMENT_RETURN,
229 NF_IP_TRACE_COMMENT_POLICY,
230};
231
Denys Vlasenko022748a2008-01-14 23:44:05 -0800232static const char *const comments[] = {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700233 [NF_IP_TRACE_COMMENT_RULE] = "rule",
234 [NF_IP_TRACE_COMMENT_RETURN] = "return",
235 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
236};
237
238static struct nf_loginfo trace_loginfo = {
239 .type = NF_LOG_TYPE_LOG,
240 .u = {
241 .log = {
242 .level = 4,
243 .logflags = NF_LOG_MASK,
244 },
245 },
246};
247
Denys Vlasenko022748a2008-01-14 23:44:05 -0800248/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700249static inline int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200250get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e,
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200251 const char *hookname, const char **chainname,
252 const char **comment, unsigned int *rulenum)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700253{
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200254 const struct ipt_standard_target *t = (void *)ipt_get_target_c(s);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700255
256 if (strcmp(t->target.u.kernel.target->name, IPT_ERROR_TARGET) == 0) {
257 /* Head of user chain: ERROR target with chainname */
258 *chainname = t->target.data;
259 (*rulenum) = 0;
260 } else if (s == e) {
261 (*rulenum)++;
262
Joe Perches3666ed12009-11-23 23:17:06 +0100263 if (s->target_offset == sizeof(struct ipt_entry) &&
264 strcmp(t->target.u.kernel.target->name,
265 IPT_STANDARD_TARGET) == 0 &&
266 t->verdict < 0 &&
267 unconditional(&s->ip)) {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700268 /* Tail of chains: STANDARD target (return/policy) */
269 *comment = *chainname == hookname
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200270 ? comments[NF_IP_TRACE_COMMENT_POLICY]
271 : comments[NF_IP_TRACE_COMMENT_RETURN];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700272 }
273 return 1;
274 } else
275 (*rulenum)++;
276
277 return 0;
278}
279
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200280static void trace_packet(const struct sk_buff *skb,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700281 unsigned int hook,
282 const struct net_device *in,
283 const struct net_device *out,
Jan Engelhardtecb6f852008-01-31 03:54:47 -0800284 const char *tablename,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200285 const struct xt_table_info *private,
286 const struct ipt_entry *e)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700287{
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200288 const void *table_base;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200289 const struct ipt_entry *root;
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200290 const char *hookname, *chainname, *comment;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100291 const struct ipt_entry *iter;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700292 unsigned int rulenum = 0;
293
Jan Engelhardtccf5bd82009-04-15 20:27:03 +0200294 table_base = private->entries[smp_processor_id()];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700295 root = get_entry(table_base, private->hook_entry[hook]);
296
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200297 hookname = chainname = hooknames[hook];
298 comment = comments[NF_IP_TRACE_COMMENT_RULE];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700299
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100300 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
301 if (get_chainname_rulenum(iter, e, hookname,
302 &chainname, &comment, &rulenum) != 0)
303 break;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700304
305 nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo,
306 "TRACE: %s:%s:%s:%u ",
307 tablename, chainname, comment, rulenum);
308}
309#endif
310
Jan Engelhardt98e86402009-04-15 21:06:05 +0200311static inline __pure
312struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
313{
314 return (void *)entry + entry->next_offset;
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/* Returns one of the generic firewall policies, like NF_ACCEPT. */
318unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700319ipt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 unsigned int hook,
321 const struct net_device *in,
322 const struct net_device *out,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800323 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Jan Engelhardtbb70dfa2009-04-15 21:40:13 +0200325#define tb_comefrom ((struct ipt_entry *)table_base)->comefrom
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Jan Engelhardt5452e422008-04-14 11:15:35 +0200328 const struct iphdr *ip;
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700329 bool hotdrop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /* Initializing verdict to NF_DROP keeps gcc happy. */
331 unsigned int verdict = NF_DROP;
332 const char *indev, *outdev;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200333 const void *table_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct ipt_entry *e, *back;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200335 const struct xt_table_info *private;
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200336 struct xt_match_param mtpar;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200337 struct xt_target_param tgpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* Initialization */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700340 ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 indev = in ? in->name : nulldevname;
342 outdev = out ? out->name : nulldevname;
343 /* We handle fragments by dealing with the first fragment as
344 * if it was a normal packet. All other fragments are treated
345 * normally, except that they will NEVER match rules that ask
346 * things we don't know, ie. tcp syn flag or ports). If the
347 * rule is also a fragment-specific rule, non-fragments won't
348 * match it. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200349 mtpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
350 mtpar.thoff = ip_hdrlen(skb);
351 mtpar.hotdrop = &hotdrop;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200352 mtpar.in = tgpar.in = in;
353 mtpar.out = tgpar.out = out;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200354 mtpar.family = tgpar.family = NFPROTO_IPV4;
Evgeniy Polyakova5e78822009-06-04 16:54:42 +0200355 mtpar.hooknum = tgpar.hooknum = hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700358 xt_info_rdlock_bh();
359 private = table->private;
360 table_base = private->entries[smp_processor_id()];
Stephen Hemminger78454472009-02-20 10:35:32 +0100361
Harald Welte2e4e6a12006-01-12 13:30:04 -0800362 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /* For return from builtin chain */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800365 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 do {
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200368 const struct ipt_entry_target *t;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 IP_NF_ASSERT(e);
371 IP_NF_ASSERT(back);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200372 if (!ip_packet_match(ip, indev, outdev,
Jan Engelhardt94522582009-04-15 21:31:32 +0200373 &e->ip, mtpar.fragoff) ||
374 IPT_MATCH_ITERATE(e, do_match, skb, &mtpar) != 0) {
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200375 e = ipt_next_entry(e);
376 continue;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200379 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200381 t = ipt_get_target(e);
382 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700383
384#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
385 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200386 /* The packet is traced: log it */
387 if (unlikely(skb->nf_trace))
388 trace_packet(skb, hook, in, out,
389 table->name, private, e);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700390#endif
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200391 /* Standard target? */
392 if (!t->u.kernel.target->target) {
393 int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200395 v = ((struct ipt_standard_target *)t)->verdict;
396 if (v < 0) {
397 /* Pop from stack? */
398 if (v != IPT_RETURN) {
399 verdict = (unsigned)(-v) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200401 }
402 e = back;
403 back = get_entry(table_base, back->comefrom);
404 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Joe Perches3666ed12009-11-23 23:17:06 +0100406 if (table_base + v != ipt_next_entry(e) &&
407 !(e->ip.flags & IPT_F_GOTO)) {
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200408 /* Save old back ptr in next entry */
409 struct ipt_entry *next = ipt_next_entry(e);
410 next->comefrom = (void *)back - table_base;
411 /* set back pointer to next entry */
412 back = next;
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200415 e = get_entry(table_base, v);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200416 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200418
419 /* Targets which reenter must return
420 abs. verdicts */
421 tgpar.target = t->u.kernel.target;
422 tgpar.targinfo = t->data;
Jan Engelhardtbb70dfa2009-04-15 21:40:13 +0200423
424
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200425#ifdef CONFIG_NETFILTER_DEBUG
Jan Engelhardtbb70dfa2009-04-15 21:40:13 +0200426 tb_comefrom = 0xeeeeeeec;
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200427#endif
428 verdict = t->u.kernel.target->target(skb, &tgpar);
429#ifdef CONFIG_NETFILTER_DEBUG
Patrick McHardy24992ea2009-06-12 01:53:09 +0200430 if (tb_comefrom != 0xeeeeeeec && verdict == IPT_CONTINUE) {
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200431 printk("Target %s reentered!\n",
432 t->u.kernel.target->name);
433 verdict = NF_DROP;
434 }
Jan Engelhardtbb70dfa2009-04-15 21:40:13 +0200435 tb_comefrom = 0x57acc001;
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200436#endif
437 /* Target might have changed stuff. */
438 ip = ip_hdr(skb);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200439 if (verdict == IPT_CONTINUE)
440 e = ipt_next_entry(e);
441 else
442 /* Verdict */
443 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 } while (!hotdrop);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700445 xt_info_rdunlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447#ifdef DEBUG_ALLOW_ALL
448 return NF_ACCEPT;
449#else
450 if (hotdrop)
451 return NF_DROP;
452 else return verdict;
453#endif
Jan Engelhardtbb70dfa2009-04-15 21:40:13 +0200454
455#undef tb_comefrom
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/* Figures out from what hook each rule can be called: returns 0 if
459 there are loops. Puts hook bitmask in comefrom. */
460static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200461mark_source_chains(const struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800462 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 unsigned int hook;
465
466 /* No recursion; use packet counter to save back ptrs (reset
467 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800468 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800470 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (!(valid_hooks & (1 << hook)))
473 continue;
474
475 /* Set initial back pointer. */
476 e->counters.pcnt = pos;
477
478 for (;;) {
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200479 const struct ipt_standard_target *t
480 = (void *)ipt_get_target_c(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800481 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800483 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 printk("iptables: loop hook %u pos %u %08X.\n",
485 hook, pos, e->comefrom);
486 return 0;
487 }
Patrick McHardy9c547952007-12-17 21:52:00 -0800488 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* Unconditional return/END. */
Joe Perches3666ed12009-11-23 23:17:06 +0100491 if ((e->target_offset == sizeof(struct ipt_entry) &&
492 (strcmp(t->target.u.user.name,
493 IPT_STANDARD_TARGET) == 0) &&
494 t->verdict < 0 && unconditional(&e->ip)) ||
495 visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 unsigned int oldpos, size;
497
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100498 if ((strcmp(t->target.u.user.name,
499 IPT_STANDARD_TARGET) == 0) &&
500 t->verdict < -NF_MAX_VERDICT - 1) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800501 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,
Joe Perches3666ed12009-11-23 23:17:06 +0100542 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
Denys Vlasenko022748a2008-01-14 23:44:05 -0800570static int
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100571cleanup_match(struct ipt_entry_match *m, struct net *net, unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200573 struct xt_mtdtor_param par;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (i && (*i)-- == 0)
576 return 1;
577
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100578 par.net = net;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200579 par.match = m->u.kernel.match;
580 par.matchinfo = m->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200581 par.family = NFPROTO_IPV4;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200582 if (par.match->destroy != NULL)
583 par.match->destroy(&par);
584 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 0;
586}
587
Denys Vlasenko022748a2008-01-14 23:44:05 -0800588static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200589check_entry(const struct ipt_entry *e, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800590{
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200591 const struct ipt_entry_target *t;
Dmitry Mishina96be242006-12-12 00:29:26 -0800592
593 if (!ip_checkentry(&e->ip)) {
594 duprintf("ip_tables: ip check failed %p %s.\n", e, name);
595 return -EINVAL;
596 }
597
Patrick McHardy9c547952007-12-17 21:52:00 -0800598 if (e->target_offset + sizeof(struct ipt_entry_target) >
599 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800600 return -EINVAL;
601
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200602 t = ipt_get_target_c(e);
Dmitry Mishina96be242006-12-12 00:29:26 -0800603 if (e->target_offset + t->u.target_size > e->next_offset)
604 return -EINVAL;
605
606 return 0;
607}
608
Denys Vlasenko022748a2008-01-14 23:44:05 -0800609static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200610check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
611 unsigned int *i)
Dmitry Mishina96be242006-12-12 00:29:26 -0800612{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200613 const struct ipt_ip *ip = par->entryinfo;
Dmitry Mishina96be242006-12-12 00:29:26 -0800614 int ret;
615
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200616 par->match = m->u.kernel.match;
617 par->matchinfo = m->data;
618
Jan Engelhardt916a9172008-10-08 11:35:20 +0200619 ret = xt_check_match(par, m->u.match_size - sizeof(*m),
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200620 ip->proto, ip->invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200621 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800622 duprintf("ip_tables: check failed for `%s'.\n",
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200623 par.match->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200624 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800625 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200626 ++*i;
627 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800628}
629
Denys Vlasenko022748a2008-01-14 23:44:05 -0800630static int
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200631find_check_match(struct ipt_entry_match *m, struct xt_mtchk_param *par,
Patrick McHardy4b478242007-12-17 21:46:15 -0800632 unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800634 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800635 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Harald Welte2e4e6a12006-01-12 13:30:04 -0800637 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy9c547952007-12-17 21:52:00 -0800638 m->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 "ipt_%s", m->u.user.name);
640 if (IS_ERR(match) || !match) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800641 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return match ? PTR_ERR(match) : -ENOENT;
643 }
644 m->u.kernel.match = match;
645
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200646 ret = check_match(m, par, i);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800647 if (ret)
648 goto err;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800651err:
652 module_put(m->u.kernel.match->me);
653 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Patrick McHardyadd67462010-02-03 13:45:12 +0100656static int check_target(struct ipt_entry *e, struct net *net, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800657{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200658 struct ipt_entry_target *t = ipt_get_target(e);
659 struct xt_tgchk_param par = {
Patrick McHardyadd67462010-02-03 13:45:12 +0100660 .net = net,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200661 .table = name,
662 .entryinfo = e,
663 .target = t->u.kernel.target,
664 .targinfo = t->data,
665 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200666 .family = NFPROTO_IPV4,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200667 };
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900668 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800669
Jan Engelhardt916a9172008-10-08 11:35:20 +0200670 ret = xt_check_target(&par, t->u.target_size - sizeof(*t),
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200671 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200672 if (ret < 0) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800673 duprintf("ip_tables: check failed for `%s'.\n",
674 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200675 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800676 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200677 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800678}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Denys Vlasenko022748a2008-01-14 23:44:05 -0800680static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100681find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
682 unsigned int size, unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800685 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 int ret;
687 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200688 struct xt_mtchk_param mtpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Dmitry Mishina96be242006-12-12 00:29:26 -0800690 ret = check_entry(e, name);
691 if (ret)
692 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 j = 0;
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100695 mtpar.net = net;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200696 mtpar.table = name;
697 mtpar.entryinfo = &e->ip;
698 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200699 mtpar.family = NFPROTO_IPV4;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200700 ret = IPT_MATCH_ITERATE(e, find_check_match, &mtpar, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (ret != 0)
702 goto cleanup_matches;
703
704 t = ipt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800705 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -0800706 t->u.user.name,
707 t->u.user.revision),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 "ipt_%s", t->u.user.name);
709 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800710 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 ret = target ? PTR_ERR(target) : -ENOENT;
712 goto cleanup_matches;
713 }
714 t->u.kernel.target = target;
715
Patrick McHardyadd67462010-02-03 13:45:12 +0100716 ret = check_target(e, net, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800717 if (ret)
718 goto err;
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 (*i)++;
721 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800722 err:
723 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 cleanup_matches:
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100725 IPT_MATCH_ITERATE(e, cleanup_match, net, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return ret;
727}
728
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200729static bool check_underflow(const struct ipt_entry *e)
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200730{
731 const struct ipt_entry_target *t;
732 unsigned int verdict;
733
734 if (!unconditional(&e->ip))
735 return false;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200736 t = ipt_get_target_c(e);
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200737 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
738 return false;
739 verdict = ((struct ipt_standard_target *)t)->verdict;
740 verdict = -verdict - 1;
741 return verdict == NF_DROP || verdict == NF_ACCEPT;
742}
743
Denys Vlasenko022748a2008-01-14 23:44:05 -0800744static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800746 struct xt_table_info *newinfo,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200747 const unsigned char *base,
748 const unsigned char *limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 const unsigned int *hook_entries,
750 const unsigned int *underflows,
Jan Engelhardta7d51732009-07-18 14:52:58 +0200751 unsigned int valid_hooks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 unsigned int *i)
753{
754 unsigned int h;
755
Joe Perches3666ed12009-11-23 23:17:06 +0100756 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
757 (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 duprintf("Bad offset %p\n", e);
759 return -EINVAL;
760 }
761
762 if (e->next_offset
763 < sizeof(struct ipt_entry) + sizeof(struct ipt_entry_target)) {
764 duprintf("checking: element %p size %u\n",
765 e, e->next_offset);
766 return -EINVAL;
767 }
768
769 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800770 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Jan Engelhardta7d51732009-07-18 14:52:58 +0200771 if (!(valid_hooks & (1 << h)))
772 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if ((unsigned char *)e - base == hook_entries[h])
774 newinfo->hook_entry[h] = hook_entries[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200775 if ((unsigned char *)e - base == underflows[h]) {
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200776 if (!check_underflow(e)) {
777 pr_err("Underflows must be unconditional and "
778 "use the STANDARD target with "
779 "ACCEPT/DROP\n");
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200780 return -EINVAL;
781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 newinfo->underflow[h] = underflows[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800787 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 e->comefrom = 0;
789
790 (*i)++;
791 return 0;
792}
793
Denys Vlasenko022748a2008-01-14 23:44:05 -0800794static int
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100795cleanup_entry(struct ipt_entry *e, struct net *net, unsigned int *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200797 struct xt_tgdtor_param par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct ipt_entry_target *t;
799
800 if (i && (*i)-- == 0)
801 return 1;
802
803 /* Cleanup all matches */
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100804 IPT_MATCH_ITERATE(e, cleanup_match, net, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 t = ipt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200806
Patrick McHardyadd67462010-02-03 13:45:12 +0100807 par.net = net;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200808 par.target = t->u.kernel.target;
809 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200810 par.family = NFPROTO_IPV4;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200811 if (par.target->destroy != NULL)
812 par.target->destroy(&par);
813 module_put(par.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return 0;
815}
816
817/* Checks and translates the user-supplied table segment (held in
818 newinfo) */
819static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100820translate_table(struct net *net,
821 const char *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 unsigned int valid_hooks,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800823 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800824 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 unsigned int size,
826 unsigned int number,
827 const unsigned int *hook_entries,
828 const unsigned int *underflows)
829{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100830 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 unsigned int i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100832 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 newinfo->size = size;
835 newinfo->number = number;
836
837 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800838 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 newinfo->hook_entry[i] = 0xFFFFFFFF;
840 newinfo->underflow[i] = 0xFFFFFFFF;
841 }
842
843 duprintf("translate_table: size %u\n", newinfo->size);
844 i = 0;
845 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100846 xt_entry_foreach(iter, entry0, newinfo->size) {
847 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
848 entry0 + size, hook_entries, underflows,
849 valid_hooks, &i);
850 if (ret != 0)
851 break;
852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (ret != 0)
854 return ret;
855
856 if (i != number) {
857 duprintf("translate_table: %u not %u entries\n",
858 i, number);
859 return -EINVAL;
860 }
861
862 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800863 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /* Only hooks which are valid */
865 if (!(valid_hooks & (1 << i)))
866 continue;
867 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
868 duprintf("Invalid hook entry %u %u\n",
869 i, hook_entries[i]);
870 return -EINVAL;
871 }
872 if (newinfo->underflow[i] == 0xFFFFFFFF) {
873 duprintf("Invalid underflow %u %u\n",
874 i, underflows[i]);
875 return -EINVAL;
876 }
877 }
878
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800879 if (!mark_source_chains(newinfo, valid_hooks, entry0))
880 return -ELOOP;
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /* Finally, each sanity check must pass */
883 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100884 xt_entry_foreach(iter, entry0, newinfo->size) {
885 ret = find_check_entry(iter, net, name, size, &i);
886 if (ret != 0)
887 break;
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800890 if (ret != 0) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100891 xt_entry_foreach(iter, entry0, newinfo->size)
892 if (cleanup_entry(iter, net, &i) != 0)
893 break;
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800894 return ret;
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700898 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800899 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
900 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
903 return ret;
904}
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/* Gets counters. */
907static inline int
908add_entry_to_counter(const struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800909 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 unsigned int *i)
911{
912 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
913
914 (*i)++;
915 return 0;
916}
917
Eric Dumazet31836062005-12-13 23:13:48 -0800918static inline int
919set_entry_to_counter(const struct ipt_entry *e,
920 struct ipt_counters total[],
921 unsigned int *i)
922{
923 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
924
925 (*i)++;
926 return 0;
927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800930get_counters(const struct xt_table_info *t,
931 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100933 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 unsigned int cpu;
935 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800936 unsigned int curcpu;
937
938 /* Instead of clearing (by a previous call to memset())
939 * the counters and using adds, we set the counters
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700940 * with data used by 'current' CPU.
941 *
942 * Bottom half has to be disabled to prevent deadlock
943 * if new softirq were to run and call ipt_do_table
Eric Dumazet31836062005-12-13 23:13:48 -0800944 */
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700945 local_bh_disable();
946 curcpu = smp_processor_id();
Eric Dumazet31836062005-12-13 23:13:48 -0800947
948 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100949 xt_entry_foreach(iter, t->entries[curcpu], t->size)
950 if (set_entry_to_counter(iter, counters, &i) != 0)
951 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700953 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800954 if (cpu == curcpu)
955 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 i = 0;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700957 xt_info_wrlock(cpu);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100958 xt_entry_foreach(iter, t->entries[cpu], t->size)
959 if (add_entry_to_counter(iter, counters, &i) != 0)
960 break;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700961 xt_info_wrunlock(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100963 local_bh_enable();
964}
965
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200966static struct xt_counters *alloc_counters(const struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Dmitry Mishin27229712006-04-01 02:25:19 -0800968 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800969 struct xt_counters *counters;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200970 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 /* We need atomic snapshot of counters: rest doesn't change
973 (other than comefrom, which userspace doesn't care
974 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800975 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet31836062005-12-13 23:13:48 -0800976 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700979 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700981 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Dmitry Mishin27229712006-04-01 02:25:19 -0800983 return counters;
984}
985
986static int
987copy_entries_to_user(unsigned int total_size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200988 const struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800989 void __user *userptr)
990{
991 unsigned int off, num;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200992 const struct ipt_entry *e;
Dmitry Mishin27229712006-04-01 02:25:19 -0800993 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200994 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800995 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200996 const void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -0800997
998 counters = alloc_counters(table);
999 if (IS_ERR(counters))
1000 return PTR_ERR(counters);
1001
Eric Dumazet31836062005-12-13 23:13:48 -08001002 /* choose the copy that is on our node/cpu, ...
1003 * This choice is lazy (because current thread is
1004 * allowed to migrate to another cpu)
1005 */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001006 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -08001007 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 ret = -EFAULT;
1009 goto free_counters;
1010 }
1011
1012 /* FIXME: use iterator macros --RR */
1013 /* ... then go back and fix counters and names */
1014 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
1015 unsigned int i;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001016 const struct ipt_entry_match *m;
1017 const struct ipt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Eric Dumazet31836062005-12-13 23:13:48 -08001019 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (copy_to_user(userptr + off
1021 + offsetof(struct ipt_entry, counters),
1022 &counters[num],
1023 sizeof(counters[num])) != 0) {
1024 ret = -EFAULT;
1025 goto free_counters;
1026 }
1027
1028 for (i = sizeof(struct ipt_entry);
1029 i < e->target_offset;
1030 i += m->u.match_size) {
1031 m = (void *)e + i;
1032
1033 if (copy_to_user(userptr + off + i
1034 + offsetof(struct ipt_entry_match,
1035 u.user.name),
1036 m->u.kernel.match->name,
1037 strlen(m->u.kernel.match->name)+1)
1038 != 0) {
1039 ret = -EFAULT;
1040 goto free_counters;
1041 }
1042 }
1043
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001044 t = ipt_get_target_c(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (copy_to_user(userptr + off + e->target_offset
1046 + offsetof(struct ipt_entry_target,
1047 u.user.name),
1048 t->u.kernel.target->name,
1049 strlen(t->u.kernel.target->name)+1) != 0) {
1050 ret = -EFAULT;
1051 goto free_counters;
1052 }
1053 }
1054
1055 free_counters:
1056 vfree(counters);
1057 return ret;
1058}
1059
Dmitry Mishin27229712006-04-01 02:25:19 -08001060#ifdef CONFIG_COMPAT
Jan Engelhardt739674f2009-06-26 08:23:19 +02001061static void compat_standard_from_user(void *dst, const void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001062{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001063 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001064
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001065 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001066 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001067 memcpy(dst, &v, sizeof(v));
1068}
1069
Jan Engelhardt739674f2009-06-26 08:23:19 +02001070static int compat_standard_to_user(void __user *dst, const void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001071{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001072 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001073
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001074 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001075 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001076 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001077}
1078
1079static inline int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001080compat_calc_match(const struct ipt_entry_match *m, int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001081{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001082 *size += xt_compat_match_offset(m->u.kernel.match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001083 return 0;
1084}
1085
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001086static int compat_calc_entry(const struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001087 const struct xt_table_info *info,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001088 const void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001089{
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001090 const struct ipt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001091 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001092 int off, i, ret;
1093
Patrick McHardy30c08c42007-12-17 21:47:14 -08001094 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001095 entry_offset = (void *)e - base;
1096 IPT_MATCH_ITERATE(e, compat_calc_match, &off);
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001097 t = ipt_get_target_c(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001098 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001099 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001100 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001101 if (ret)
1102 return ret;
1103
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001104 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001105 if (info->hook_entry[i] &&
1106 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001107 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001108 if (info->underflow[i] &&
1109 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001110 newinfo->underflow[i] -= off;
1111 }
1112 return 0;
1113}
1114
Eric Dumazet259d4e42007-12-04 23:24:56 -08001115static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001116 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001117{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001118 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001119 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001120 int ret = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001121
1122 if (!newinfo || !info)
1123 return -EINVAL;
1124
Eric Dumazet259d4e42007-12-04 23:24:56 -08001125 /* we dont care about newinfo->entries[] */
1126 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1127 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001128 loc_cpu_entry = info->entries[raw_smp_processor_id()];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001129 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
1130 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
1131 if (ret != 0)
1132 break;
1133 }
1134 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001135}
1136#endif
1137
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001138static int get_info(struct net *net, void __user *user,
1139 const int *len, int compat)
Dmitry Mishin27229712006-04-01 02:25:19 -08001140{
1141 char name[IPT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001142 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001143 int ret;
1144
1145 if (*len != sizeof(struct ipt_getinfo)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001146 duprintf("length %u != %zu\n", *len,
1147 sizeof(struct ipt_getinfo));
Dmitry Mishin27229712006-04-01 02:25:19 -08001148 return -EINVAL;
1149 }
1150
1151 if (copy_from_user(name, user, sizeof(name)) != 0)
1152 return -EFAULT;
1153
1154 name[IPT_TABLE_MAXNAMELEN-1] = '\0';
1155#ifdef CONFIG_COMPAT
1156 if (compat)
1157 xt_compat_lock(AF_INET);
1158#endif
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001159 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001160 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001161 if (t && !IS_ERR(t)) {
1162 struct ipt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001163 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001164#ifdef CONFIG_COMPAT
Alexey Dobriyan14c7dbe2010-02-08 11:17:43 -08001165 struct xt_table_info tmp;
1166
Dmitry Mishin27229712006-04-01 02:25:19 -08001167 if (compat) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001168 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001169 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001170 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001171 }
1172#endif
1173 info.valid_hooks = t->valid_hooks;
1174 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001175 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001176 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001177 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001178 info.num_entries = private->number;
1179 info.size = private->size;
1180 strcpy(info.name, name);
1181
1182 if (copy_to_user(user, &info, *len) != 0)
1183 ret = -EFAULT;
1184 else
1185 ret = 0;
1186
1187 xt_table_unlock(t);
1188 module_put(t->me);
1189 } else
1190 ret = t ? PTR_ERR(t) : -ENOENT;
1191#ifdef CONFIG_COMPAT
1192 if (compat)
1193 xt_compat_unlock(AF_INET);
1194#endif
1195 return ret;
1196}
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001199get_entries(struct net *net, struct ipt_get_entries __user *uptr,
1200 const int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001203 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001204 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Dmitry Mishin27229712006-04-01 02:25:19 -08001206 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001207 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001208 return -EINVAL;
1209 }
1210 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1211 return -EFAULT;
1212 if (*len != sizeof(struct ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001213 duprintf("get_entries: %u != %zu\n",
1214 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001215 return -EINVAL;
1216 }
1217
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001218 t = xt_find_table_lock(net, AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001220 const struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001221 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001222 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001223 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 t, uptr->entrytable);
1225 else {
1226 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001227 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001228 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001231 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 } else
1233 ret = t ? PTR_ERR(t) : -ENOENT;
1234
1235 return ret;
1236}
1237
1238static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001239__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001240 struct xt_table_info *newinfo, unsigned int num_counters,
1241 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001242{
1243 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001244 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001245 struct xt_table_info *oldinfo;
1246 struct xt_counters *counters;
1247 void *loc_cpu_old_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001248 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001249
1250 ret = 0;
1251 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1252 if (!counters) {
1253 ret = -ENOMEM;
1254 goto out;
1255 }
1256
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001257 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Dmitry Mishin27229712006-04-01 02:25:19 -08001258 "iptable_%s", name);
1259 if (!t || IS_ERR(t)) {
1260 ret = t ? PTR_ERR(t) : -ENOENT;
1261 goto free_newinfo_counters_untrans;
1262 }
1263
1264 /* You lied! */
1265 if (valid_hooks != t->valid_hooks) {
1266 duprintf("Valid hook crap: %08X vs %08X\n",
1267 valid_hooks, t->valid_hooks);
1268 ret = -EINVAL;
1269 goto put_module;
1270 }
1271
1272 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1273 if (!oldinfo)
1274 goto put_module;
1275
1276 /* Update module usage count based on number of rules */
1277 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1278 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1279 if ((oldinfo->number > oldinfo->initial_entries) ||
1280 (newinfo->number <= oldinfo->initial_entries))
1281 module_put(t->me);
1282 if ((oldinfo->number > oldinfo->initial_entries) &&
1283 (newinfo->number <= oldinfo->initial_entries))
1284 module_put(t->me);
1285
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001286 /* Get the old counters, and synchronize with replace */
Dmitry Mishin27229712006-04-01 02:25:19 -08001287 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001288
Dmitry Mishin27229712006-04-01 02:25:19 -08001289 /* Decrease module usage counts and free resource */
1290 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001291 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
1292 if (cleanup_entry(iter, net, NULL) != 0)
1293 break;
1294
Dmitry Mishin27229712006-04-01 02:25:19 -08001295 xt_free_table_info(oldinfo);
1296 if (copy_to_user(counters_ptr, counters,
1297 sizeof(struct xt_counters) * num_counters) != 0)
1298 ret = -EFAULT;
1299 vfree(counters);
1300 xt_table_unlock(t);
1301 return ret;
1302
1303 put_module:
1304 module_put(t->me);
1305 xt_table_unlock(t);
1306 free_newinfo_counters_untrans:
1307 vfree(counters);
1308 out:
1309 return ret;
1310}
1311
1312static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001313do_replace(struct net *net, const void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 int ret;
1316 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001317 struct xt_table_info *newinfo;
1318 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001319 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1322 return -EFAULT;
1323
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001324 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001325 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1326 return -ENOMEM;
1327
Harald Welte2e4e6a12006-01-12 13:30:04 -08001328 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (!newinfo)
1330 return -ENOMEM;
1331
Patrick McHardy9c547952007-12-17 21:52:00 -08001332 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001333 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1334 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 tmp.size) != 0) {
1336 ret = -EFAULT;
1337 goto free_newinfo;
1338 }
1339
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001340 ret = translate_table(net, tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001341 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 tmp.hook_entry, tmp.underflow);
1343 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001344 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 duprintf("ip_tables: Translated table\n");
1347
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001348 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001349 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001350 if (ret)
1351 goto free_newinfo_untrans;
1352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Dmitry Mishin27229712006-04-01 02:25:19 -08001354 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001355 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1356 if (cleanup_entry(iter, net, NULL) != 0)
1357 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001359 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return ret;
1361}
1362
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001363/* We're lazy, and add to the first CPU; overflow works its fey magic
1364 * and everything is OK. */
1365static int
1366add_counter_to_entry(struct ipt_entry *e,
1367 const struct xt_counters addme[],
1368 unsigned int *i)
1369{
1370 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1371
1372 (*i)++;
1373 return 0;
1374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001377do_add_counters(struct net *net, const void __user *user,
1378 unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001380 unsigned int i, curcpu;
Dmitry Mishin27229712006-04-01 02:25:19 -08001381 struct xt_counters_info tmp;
1382 struct xt_counters *paddc;
1383 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001384 const char *name;
Dmitry Mishin27229712006-04-01 02:25:19 -08001385 int size;
1386 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001387 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001388 const struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001390 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001391 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001392#ifdef CONFIG_COMPAT
1393 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Dmitry Mishin27229712006-04-01 02:25:19 -08001395 if (compat) {
1396 ptmp = &compat_tmp;
1397 size = sizeof(struct compat_xt_counters_info);
1398 } else
1399#endif
1400 {
1401 ptmp = &tmp;
1402 size = sizeof(struct xt_counters_info);
1403 }
1404
1405 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return -EFAULT;
1407
Dmitry Mishin27229712006-04-01 02:25:19 -08001408#ifdef CONFIG_COMPAT
1409 if (compat) {
1410 num_counters = compat_tmp.num_counters;
1411 name = compat_tmp.name;
1412 } else
1413#endif
1414 {
1415 num_counters = tmp.num_counters;
1416 name = tmp.name;
1417 }
1418
1419 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EINVAL;
1421
Dmitry Mishin27229712006-04-01 02:25:19 -08001422 paddc = vmalloc_node(len - size, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!paddc)
1424 return -ENOMEM;
1425
Dmitry Mishin27229712006-04-01 02:25:19 -08001426 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 ret = -EFAULT;
1428 goto free;
1429 }
1430
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001431 t = xt_find_table_lock(net, AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (!t || IS_ERR(t)) {
1433 ret = t ? PTR_ERR(t) : -ENOENT;
1434 goto free;
1435 }
1436
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001437 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001438 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001439 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 ret = -EINVAL;
1441 goto unlock_up_free;
1442 }
1443
1444 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001445 /* Choose the copy that is on our node */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001446 curcpu = smp_processor_id();
1447 loc_cpu_entry = private->entries[curcpu];
1448 xt_info_wrlock(curcpu);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001449 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1450 if (add_counter_to_entry(iter, paddc, &i) != 0)
1451 break;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001452 xt_info_wrunlock(curcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001454 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001455 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 module_put(t->me);
1457 free:
1458 vfree(paddc);
1459
1460 return ret;
1461}
1462
Dmitry Mishin27229712006-04-01 02:25:19 -08001463#ifdef CONFIG_COMPAT
1464struct compat_ipt_replace {
1465 char name[IPT_TABLE_MAXNAMELEN];
1466 u32 valid_hooks;
1467 u32 num_entries;
1468 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001469 u32 hook_entry[NF_INET_NUMHOOKS];
1470 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001471 u32 num_counters;
1472 compat_uptr_t counters; /* struct ipt_counters * */
1473 struct compat_ipt_entry entries[0];
1474};
1475
Patrick McHardya18aa312007-12-12 10:35:16 -08001476static int
1477compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001478 unsigned int *size, struct xt_counters *counters,
Patrick McHardya18aa312007-12-12 10:35:16 -08001479 unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001480{
Al Viro3e597c62006-09-24 23:42:20 +01001481 struct ipt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001482 struct compat_ipt_entry __user *ce;
1483 u_int16_t target_offset, next_offset;
1484 compat_uint_t origsize;
1485 int ret;
1486
1487 ret = -EFAULT;
1488 origsize = *size;
1489 ce = (struct compat_ipt_entry __user *)*dstptr;
Patrick McHardy78000072006-05-03 23:20:27 -07001490 if (copy_to_user(ce, e, sizeof(struct ipt_entry)))
Dmitry Mishin27229712006-04-01 02:25:19 -08001491 goto out;
1492
Patrick McHardya18aa312007-12-12 10:35:16 -08001493 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1494 goto out;
1495
Dmitry Mishin27229712006-04-01 02:25:19 -08001496 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001497 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1498
Patrick McHardyac8e27f2007-12-17 21:45:52 -08001499 ret = IPT_MATCH_ITERATE(e, xt_compat_match_to_user, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001500 target_offset = e->target_offset - (origsize - *size);
1501 if (ret)
1502 goto out;
1503 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001504 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001505 if (ret)
1506 goto out;
1507 ret = -EFAULT;
1508 next_offset = e->next_offset - (origsize - *size);
Patrick McHardy78000072006-05-03 23:20:27 -07001509 if (put_user(target_offset, &ce->target_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001510 goto out;
Patrick McHardy78000072006-05-03 23:20:27 -07001511 if (put_user(next_offset, &ce->next_offset))
Dmitry Mishin27229712006-04-01 02:25:19 -08001512 goto out;
Patrick McHardya18aa312007-12-12 10:35:16 -08001513
1514 (*i)++;
Dmitry Mishin27229712006-04-01 02:25:19 -08001515 return 0;
1516out:
1517 return ret;
1518}
1519
Denys Vlasenko022748a2008-01-14 23:44:05 -08001520static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001521compat_find_calc_match(struct ipt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001522 const char *name,
1523 const struct ipt_ip *ip,
1524 unsigned int hookmask,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001525 int *size, unsigned int *i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001526{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001527 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001528
1529 match = try_then_request_module(xt_find_match(AF_INET, m->u.user.name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001530 m->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001531 "ipt_%s", m->u.user.name);
1532 if (IS_ERR(match) || !match) {
1533 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001534 m->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001535 return match ? PTR_ERR(match) : -ENOENT;
1536 }
1537 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001538 *size += xt_compat_match_offset(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001539
1540 (*i)++;
1541 return 0;
1542}
1543
Denys Vlasenko022748a2008-01-14 23:44:05 -08001544static int
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001545compat_release_match(struct ipt_entry_match *m, unsigned int *i)
1546{
1547 if (i && (*i)-- == 0)
1548 return 1;
1549
1550 module_put(m->u.kernel.match->me);
1551 return 0;
1552}
1553
Denys Vlasenko022748a2008-01-14 23:44:05 -08001554static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001555compat_release_entry(struct compat_ipt_entry *e, unsigned int *i)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001556{
1557 struct ipt_entry_target *t;
1558
1559 if (i && (*i)-- == 0)
1560 return 1;
1561
1562 /* Cleanup all matches */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001563 COMPAT_IPT_MATCH_ITERATE(e, compat_release_match, NULL);
1564 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001565 module_put(t->u.kernel.target->me);
1566 return 0;
1567}
1568
Denys Vlasenko022748a2008-01-14 23:44:05 -08001569static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001570check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001571 struct xt_table_info *newinfo,
1572 unsigned int *size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001573 const unsigned char *base,
1574 const unsigned char *limit,
1575 const unsigned int *hook_entries,
1576 const unsigned int *underflows,
Patrick McHardy4b478242007-12-17 21:46:15 -08001577 unsigned int *i,
1578 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001579{
1580 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001581 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001582 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001583 unsigned int j;
1584 int ret, off, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001585
1586 duprintf("check_compat_entry_size_and_hooks %p\n", e);
Joe Perches3666ed12009-11-23 23:17:06 +01001587 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
1588 (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001589 duprintf("Bad offset %p, limit = %p\n", e, limit);
1590 return -EINVAL;
1591 }
1592
1593 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001594 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001595 duprintf("checking: element %p size %u\n",
1596 e, e->next_offset);
1597 return -EINVAL;
1598 }
1599
Patrick McHardy73cd5982007-12-17 21:47:32 -08001600 /* For purposes of check_entry casting the compat entry is fine */
1601 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001602 if (ret)
1603 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001604
Patrick McHardy30c08c42007-12-17 21:47:14 -08001605 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001606 entry_offset = (void *)e - (void *)base;
1607 j = 0;
Patrick McHardy73cd5982007-12-17 21:47:32 -08001608 ret = COMPAT_IPT_MATCH_ITERATE(e, compat_find_calc_match, name,
1609 &e->ip, e->comefrom, &off, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001610 if (ret != 0)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001611 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001612
Patrick McHardy73cd5982007-12-17 21:47:32 -08001613 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001614 target = try_then_request_module(xt_find_target(AF_INET,
Patrick McHardy4b478242007-12-17 21:46:15 -08001615 t->u.user.name,
1616 t->u.user.revision),
Dmitry Mishin27229712006-04-01 02:25:19 -08001617 "ipt_%s", t->u.user.name);
1618 if (IS_ERR(target) || !target) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001619 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001620 t->u.user.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001621 ret = target ? PTR_ERR(target) : -ENOENT;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001622 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001623 }
1624 t->u.kernel.target = target;
1625
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001626 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001627 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001628 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001629 if (ret)
1630 goto out;
1631
1632 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001633 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001634 if ((unsigned char *)e - base == hook_entries[h])
1635 newinfo->hook_entry[h] = hook_entries[h];
1636 if ((unsigned char *)e - base == underflows[h])
1637 newinfo->underflow[h] = underflows[h];
1638 }
1639
1640 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001641 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001642 e->comefrom = 0;
1643
1644 (*i)++;
1645 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001646
Dmitry Mishin27229712006-04-01 02:25:19 -08001647out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001648 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001649release_matches:
1650 IPT_MATCH_ITERATE(e, compat_release_match, &j);
Dmitry Mishin27229712006-04-01 02:25:19 -08001651 return ret;
1652}
1653
Patrick McHardy4b478242007-12-17 21:46:15 -08001654static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001655compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001656 unsigned int *size, const char *name,
1657 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001658{
1659 struct ipt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001660 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001661 struct ipt_entry *de;
1662 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001663 int ret, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001664
1665 ret = 0;
1666 origsize = *size;
1667 de = (struct ipt_entry *)*dstptr;
1668 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001669 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001670
Patrick McHardy73cd5982007-12-17 21:47:32 -08001671 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001672 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1673
Patrick McHardy73cd5982007-12-17 21:47:32 -08001674 ret = COMPAT_IPT_MATCH_ITERATE(e, xt_compat_match_from_user,
1675 dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001676 if (ret)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001677 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001678 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001679 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001680 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001681 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001682
1683 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001684 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001685 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1686 newinfo->hook_entry[h] -= origsize - *size;
1687 if ((unsigned char *)de - base < newinfo->underflow[h])
1688 newinfo->underflow[h] -= origsize - *size;
1689 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001690 return ret;
1691}
Dmitry Mishin27229712006-04-01 02:25:19 -08001692
Denys Vlasenko022748a2008-01-14 23:44:05 -08001693static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001694compat_check_entry(struct ipt_entry *e, struct net *net, const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001695 unsigned int *i)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001696{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001697 struct xt_mtchk_param mtpar;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001698 unsigned int j;
1699 int ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001700
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001701 j = 0;
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001702 mtpar.net = net;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001703 mtpar.table = name;
1704 mtpar.entryinfo = &e->ip;
1705 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +02001706 mtpar.family = NFPROTO_IPV4;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001707 ret = IPT_MATCH_ITERATE(e, check_match, &mtpar, &j);
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001708 if (ret)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001709 goto cleanup_matches;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001710
Patrick McHardyadd67462010-02-03 13:45:12 +01001711 ret = check_target(e, net, name);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001712 if (ret)
1713 goto cleanup_matches;
1714
1715 (*i)++;
1716 return 0;
1717
1718 cleanup_matches:
Alexey Dobriyanf54e9362010-01-18 08:25:47 +01001719 IPT_MATCH_ITERATE(e, cleanup_match, net, &j);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001720 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001721}
1722
Dmitry Mishin27229712006-04-01 02:25:19 -08001723static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001724translate_compat_table(struct net *net,
1725 const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001726 unsigned int valid_hooks,
1727 struct xt_table_info **pinfo,
1728 void **pentry0,
1729 unsigned int total_size,
1730 unsigned int number,
1731 unsigned int *hook_entries,
1732 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001733{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001734 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001735 struct xt_table_info *newinfo, *info;
1736 void *pos, *entry0, *entry1;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001737 struct compat_ipt_entry *iter0;
1738 struct ipt_entry *iter1;
Dmitry Mishin27229712006-04-01 02:25:19 -08001739 unsigned int size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001740 int ret = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001741
1742 info = *pinfo;
1743 entry0 = *pentry0;
1744 size = total_size;
1745 info->number = number;
1746
1747 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001748 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001749 info->hook_entry[i] = 0xFFFFFFFF;
1750 info->underflow[i] = 0xFFFFFFFF;
1751 }
1752
1753 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001754 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001755 xt_compat_lock(AF_INET);
1756 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001757 xt_entry_foreach(iter0, entry0, total_size) {
1758 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1759 entry0, entry0 + total_size, hook_entries, underflows,
1760 &j, name);
1761 if (ret != 0)
1762 break;
1763 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001764 if (ret != 0)
1765 goto out_unlock;
1766
1767 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001768 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001769 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001770 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001771 goto out_unlock;
1772 }
1773
1774 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001775 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001776 /* Only hooks which are valid */
1777 if (!(valid_hooks & (1 << i)))
1778 continue;
1779 if (info->hook_entry[i] == 0xFFFFFFFF) {
1780 duprintf("Invalid hook entry %u %u\n",
1781 i, hook_entries[i]);
1782 goto out_unlock;
1783 }
1784 if (info->underflow[i] == 0xFFFFFFFF) {
1785 duprintf("Invalid underflow %u %u\n",
1786 i, underflows[i]);
1787 goto out_unlock;
1788 }
1789 }
1790
1791 ret = -ENOMEM;
1792 newinfo = xt_alloc_table_info(size);
1793 if (!newinfo)
1794 goto out_unlock;
1795
1796 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001797 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001798 newinfo->hook_entry[i] = info->hook_entry[i];
1799 newinfo->underflow[i] = info->underflow[i];
1800 }
1801 entry1 = newinfo->entries[raw_smp_processor_id()];
1802 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001803 size = total_size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001804 xt_entry_foreach(iter0, entry0, total_size) {
1805 ret = compat_copy_entry_from_user(iter0, &pos,
1806 &size, name, newinfo, entry1);
1807 if (ret != 0)
1808 break;
1809 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001810 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001811 xt_compat_unlock(AF_INET);
1812 if (ret)
1813 goto free_newinfo;
1814
1815 ret = -ELOOP;
1816 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1817 goto free_newinfo;
1818
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001819 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001820 xt_entry_foreach(iter1, entry1, newinfo->size) {
1821 ret = compat_check_entry(iter1, net, name, &i);
1822 if (ret != 0)
1823 break;
1824 }
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001825 if (ret) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001826 /*
1827 * The first i matches need cleanup_entry (calls ->destroy)
1828 * because they had called ->check already. The other j-i
1829 * entries need only release.
1830 */
1831 int skip = i;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001832 j -= i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001833 xt_entry_foreach(iter0, entry0, newinfo->size) {
1834 if (skip-- > 0)
1835 continue;
1836 if (compat_release_entry(iter0, &i) != 0)
1837 break;
1838 }
1839 xt_entry_foreach(iter1, entry1, newinfo->size)
1840 if (cleanup_entry(iter1, net, &i) != 0)
1841 break;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001842 xt_free_table_info(newinfo);
1843 return ret;
1844 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001845
Dmitry Mishin27229712006-04-01 02:25:19 -08001846 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001847 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001848 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1849 memcpy(newinfo->entries[i], entry1, newinfo->size);
1850
1851 *pinfo = newinfo;
1852 *pentry0 = entry1;
1853 xt_free_table_info(info);
1854 return 0;
1855
1856free_newinfo:
1857 xt_free_table_info(newinfo);
1858out:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001859 xt_entry_foreach(iter0, entry0, total_size)
1860 if (compat_release_entry(iter0, &j) != 0)
1861 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08001862 return ret;
1863out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001864 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001865 xt_compat_unlock(AF_INET);
1866 goto out;
1867}
1868
1869static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001870compat_do_replace(struct net *net, void __user *user, unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001871{
1872 int ret;
1873 struct compat_ipt_replace tmp;
1874 struct xt_table_info *newinfo;
1875 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001876 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001877
1878 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1879 return -EFAULT;
1880
Dmitry Mishin27229712006-04-01 02:25:19 -08001881 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001882 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001883 return -ENOMEM;
1884 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1885 return -ENOMEM;
1886
1887 newinfo = xt_alloc_table_info(tmp.size);
1888 if (!newinfo)
1889 return -ENOMEM;
1890
Patrick McHardy9c547952007-12-17 21:52:00 -08001891 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001892 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1893 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1894 tmp.size) != 0) {
1895 ret = -EFAULT;
1896 goto free_newinfo;
1897 }
1898
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001899 ret = translate_compat_table(net, tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001900 &newinfo, &loc_cpu_entry, tmp.size,
1901 tmp.num_entries, tmp.hook_entry,
1902 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001903 if (ret != 0)
1904 goto free_newinfo;
1905
1906 duprintf("compat_do_replace: Translated table\n");
1907
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001908 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001909 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001910 if (ret)
1911 goto free_newinfo_untrans;
1912 return 0;
1913
1914 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001915 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1916 if (cleanup_entry(iter, net, NULL) != 0)
1917 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08001918 free_newinfo:
1919 xt_free_table_info(newinfo);
1920 return ret;
1921}
1922
1923static int
1924compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001925 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001926{
1927 int ret;
1928
1929 if (!capable(CAP_NET_ADMIN))
1930 return -EPERM;
1931
1932 switch (cmd) {
1933 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001934 ret = compat_do_replace(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001935 break;
1936
1937 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001938 ret = do_add_counters(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001939 break;
1940
1941 default:
1942 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1943 ret = -EINVAL;
1944 }
1945
1946 return ret;
1947}
1948
Patrick McHardy4b478242007-12-17 21:46:15 -08001949struct compat_ipt_get_entries {
Dmitry Mishin27229712006-04-01 02:25:19 -08001950 char name[IPT_TABLE_MAXNAMELEN];
1951 compat_uint_t size;
1952 struct compat_ipt_entry entrytable[0];
1953};
1954
Patrick McHardy4b478242007-12-17 21:46:15 -08001955static int
1956compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1957 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001958{
Dmitry Mishin27229712006-04-01 02:25:19 -08001959 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001960 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001961 void __user *pos;
1962 unsigned int size;
1963 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001964 const void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001965 unsigned int i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001966 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001967
1968 counters = alloc_counters(table);
1969 if (IS_ERR(counters))
1970 return PTR_ERR(counters);
1971
1972 /* choose the copy that is on our node/cpu, ...
1973 * This choice is lazy (because current thread is
1974 * allowed to migrate to another cpu)
1975 */
1976 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1977 pos = userptr;
1978 size = total_size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001979 xt_entry_foreach(iter, loc_cpu_entry, total_size) {
1980 ret = compat_copy_entry_to_user(iter, &pos,
1981 &size, counters, &i);
1982 if (ret != 0)
1983 break;
1984 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001985
Dmitry Mishin27229712006-04-01 02:25:19 -08001986 vfree(counters);
1987 return ret;
1988}
1989
1990static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001991compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1992 int *len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001993{
1994 int ret;
1995 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001996 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001997
Dmitry Mishin27229712006-04-01 02:25:19 -08001998 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001999 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08002000 return -EINVAL;
2001 }
2002
2003 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
2004 return -EFAULT;
2005
2006 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08002007 duprintf("compat_get_entries: %u != %zu\n",
2008 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08002009 return -EINVAL;
2010 }
2011
2012 xt_compat_lock(AF_INET);
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08002013 t = xt_find_table_lock(net, AF_INET, get.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08002014 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02002015 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08002016 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08002017 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08002018 ret = compat_table_info(private, &info);
2019 if (!ret && get.size == info.size) {
2020 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08002021 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08002022 } else if (!ret) {
2023 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08002024 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02002025 ret = -EAGAIN;
Dmitry Mishin27229712006-04-01 02:25:19 -08002026 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08002027 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08002028 module_put(t->me);
2029 xt_table_unlock(t);
2030 } else
2031 ret = t ? PTR_ERR(t) : -ENOENT;
2032
2033 xt_compat_unlock(AF_INET);
2034 return ret;
2035}
2036
Patrick McHardy79030ed2006-09-20 12:05:08 -07002037static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
2038
Dmitry Mishin27229712006-04-01 02:25:19 -08002039static int
2040compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2041{
2042 int ret;
2043
Björn Steinbrink82fac052006-10-20 00:21:10 -07002044 if (!capable(CAP_NET_ADMIN))
2045 return -EPERM;
2046
Dmitry Mishin27229712006-04-01 02:25:19 -08002047 switch (cmd) {
2048 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002049 ret = get_info(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08002050 break;
2051 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002052 ret = compat_get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08002053 break;
2054 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07002055 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08002056 }
2057 return ret;
2058}
2059#endif
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061static int
Patrick McHardy9c547952007-12-17 21:52:00 -08002062do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 int ret;
2065
2066 if (!capable(CAP_NET_ADMIN))
2067 return -EPERM;
2068
2069 switch (cmd) {
2070 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002071 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 break;
2073
2074 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002075 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 break;
2077
2078 default:
2079 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
2080 ret = -EINVAL;
2081 }
2082
2083 return ret;
2084}
2085
2086static int
2087do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2088{
2089 int ret;
2090
2091 if (!capable(CAP_NET_ADMIN))
2092 return -EPERM;
2093
2094 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002095 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002096 ret = get_info(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002098
2099 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002100 ret = get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08002101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 case IPT_SO_GET_REVISION_MATCH:
2104 case IPT_SO_GET_REVISION_TARGET: {
2105 struct ipt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002106 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 if (*len != sizeof(rev)) {
2109 ret = -EINVAL;
2110 break;
2111 }
2112 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2113 ret = -EFAULT;
2114 break;
2115 }
2116
2117 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002118 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002120 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Harald Welte2e4e6a12006-01-12 13:30:04 -08002122 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2123 rev.revision,
2124 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 "ipt_%s", rev.name);
2126 break;
2127 }
2128
2129 default:
2130 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2131 ret = -EINVAL;
2132 }
2133
2134 return ret;
2135}
2136
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02002137struct xt_table *ipt_register_table(struct net *net,
2138 const struct xt_table *table,
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002139 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002142 struct xt_table_info *newinfo;
Eric Dumazet259d4e42007-12-04 23:24:56 -08002143 struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08002145 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08002146 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Harald Welte2e4e6a12006-01-12 13:30:04 -08002148 newinfo = xt_alloc_table_info(repl->size);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002149 if (!newinfo) {
2150 ret = -ENOMEM;
2151 goto out;
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Patrick McHardy9c547952007-12-17 21:52:00 -08002154 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002155 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2156 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01002158 ret = translate_table(net, table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08002159 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 repl->num_entries,
2161 repl->hook_entry,
2162 repl->underflow);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002163 if (ret != 0)
2164 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002166 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08002167 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002168 ret = PTR_ERR(new_table);
2169 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002172 return new_table;
2173
2174out_free:
2175 xt_free_table_info(newinfo);
2176out:
2177 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
Alexey Dobriyanf54e9362010-01-18 08:25:47 +01002180void ipt_unregister_table(struct net *net, struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002182 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002183 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08002184 struct module *table_owner = table->me;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01002185 struct ipt_entry *iter;
Eric Dumazet31836062005-12-13 23:13:48 -08002186
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002187 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002190 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01002191 xt_entry_foreach(iter, loc_cpu_entry, private->size)
2192 if (cleanup_entry(iter, net, NULL) != 0)
2193 break;
Alexey Dobriyandf200962008-01-31 04:05:34 -08002194 if (private->number > private->initial_entries)
2195 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002196 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
2199/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002200static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2202 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002203 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Patrick McHardy9c547952007-12-17 21:52:00 -08002205 return ((test_type == 0xFF) ||
2206 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 ^ invert;
2208}
2209
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002210static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002211icmp_match(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Jan Engelhardt5452e422008-04-14 11:15:35 +02002213 const struct icmphdr *ic;
2214 struct icmphdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002215 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002218 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002219 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002221 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (ic == NULL) {
2223 /* We've been asked to examine this packet, and we
2224 * can't. Hence, no choice but to drop.
2225 */
2226 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002227 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002228 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
2230
2231 return icmp_type_code_match(icmpinfo->type,
2232 icmpinfo->code[0],
2233 icmpinfo->code[1],
2234 ic->type, ic->code,
2235 !!(icmpinfo->invflags&IPT_ICMP_INV));
2236}
2237
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002238static bool icmp_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002240 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002242 /* Must specify no unknown invflags */
2243 return !(icmpinfo->invflags & ~IPT_ICMP_INV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
2246/* The built-in targets: standard (NULL) and error. */
Patrick McHardy9f15c532007-07-07 22:22:02 -07002247static struct xt_target ipt_standard_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 .name = IPT_STANDARD_TARGET,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002249 .targetsize = sizeof(int),
Jan Engelhardt4ba351c2009-04-14 14:28:48 +02002250 .family = NFPROTO_IPV4,
Dmitry Mishin27229712006-04-01 02:25:19 -08002251#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -07002252 .compatsize = sizeof(compat_int_t),
2253 .compat_from_user = compat_standard_from_user,
2254 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002255#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256};
2257
Patrick McHardy9f15c532007-07-07 22:22:02 -07002258static struct xt_target ipt_error_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 .name = IPT_ERROR_TARGET,
2260 .target = ipt_error,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002261 .targetsize = IPT_FUNCTION_MAXNAMELEN,
Jan Engelhardt4ba351c2009-04-14 14:28:48 +02002262 .family = NFPROTO_IPV4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263};
2264
2265static struct nf_sockopt_ops ipt_sockopts = {
2266 .pf = PF_INET,
2267 .set_optmin = IPT_BASE_CTL,
2268 .set_optmax = IPT_SO_SET_MAX+1,
2269 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002270#ifdef CONFIG_COMPAT
2271 .compat_set = compat_do_ipt_set_ctl,
2272#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 .get_optmin = IPT_BASE_CTL,
2274 .get_optmax = IPT_SO_GET_MAX+1,
2275 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002276#ifdef CONFIG_COMPAT
2277 .compat_get = compat_do_ipt_get_ctl,
2278#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002279 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280};
2281
Patrick McHardy9f15c532007-07-07 22:22:02 -07002282static struct xt_match icmp_matchstruct __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 .name = "icmp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002284 .match = icmp_match,
2285 .matchsize = sizeof(struct ipt_icmp),
Patrick McHardy9c547952007-12-17 21:52:00 -08002286 .checkentry = icmp_checkentry,
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002287 .proto = IPPROTO_ICMP,
Jan Engelhardt4ba351c2009-04-14 14:28:48 +02002288 .family = NFPROTO_IPV4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289};
2290
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002291static int __net_init ip_tables_net_init(struct net *net)
2292{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02002293 return xt_proto_init(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002294}
2295
2296static void __net_exit ip_tables_net_exit(struct net *net)
2297{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02002298 xt_proto_fini(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002299}
2300
2301static struct pernet_operations ip_tables_net_ops = {
2302 .init = ip_tables_net_init,
2303 .exit = ip_tables_net_exit,
2304};
2305
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002306static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 int ret;
2309
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002310 ret = register_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002311 if (ret < 0)
2312 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 /* Noone else will be downing sem now, so we won't sleep */
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002315 ret = xt_register_target(&ipt_standard_target);
2316 if (ret < 0)
2317 goto err2;
2318 ret = xt_register_target(&ipt_error_target);
2319 if (ret < 0)
2320 goto err3;
2321 ret = xt_register_match(&icmp_matchstruct);
2322 if (ret < 0)
2323 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 /* Register setsockopt */
2326 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002327 if (ret < 0)
2328 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Dan Aloni0236e662007-07-09 13:20:12 -07002330 printk(KERN_INFO "ip_tables: (C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002332
2333err5:
2334 xt_unregister_match(&icmp_matchstruct);
2335err4:
2336 xt_unregister_target(&ipt_error_target);
2337err3:
2338 xt_unregister_target(&ipt_standard_target);
2339err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002340 unregister_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002341err1:
2342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002345static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002348
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08002349 xt_unregister_match(&icmp_matchstruct);
2350 xt_unregister_target(&ipt_error_target);
2351 xt_unregister_target(&ipt_standard_target);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002352
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002353 unregister_pernet_subsys(&ip_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
2356EXPORT_SYMBOL(ipt_register_table);
2357EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002359module_init(ip_tables_init);
2360module_exit(ip_tables_fini);