blob: 17c5e06da6628b7ec12495471d3bfcc943acf969 [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
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010042#define dprintf(format, args...) pr_info(format , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#else
44#define dprintf(format, args...)
45#endif
46
47#ifdef DEBUG_IP_FIREWALL_USER
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010048#define duprintf(format, args...) pr_info(format , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#else
50#define duprintf(format, args...)
51#endif
52
53#ifdef CONFIG_NETFILTER_DEBUG
Stephen Hemmingeraf567602010-05-13 15:00:20 +020054#define IP_NF_ASSERT(x) WARN_ON(!(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#else
56#define IP_NF_ASSERT(x)
57#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#if 0
60/* All the better to debug you with... */
61#define static
62#define inline
63#endif
64
Jan Engelhardte3eaa992009-06-17 22:14:54 +020065void *ipt_alloc_initial_table(const struct xt_table *info)
66{
67 return xt_alloc_initial_table(ipt, IPT);
68}
69EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Returns whether matches rule or not. */
Denys Vlasenko022748a2008-01-14 23:44:05 -080072/* Performance critical - called for every packet */
Patrick McHardy9c547952007-12-17 21:52:00 -080073static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070074ip_packet_match(const struct iphdr *ip,
75 const char *indev,
76 const char *outdev,
77 const struct ipt_ip *ipinfo,
78 int isfrag)
79{
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 unsigned long ret;
81
Jan Engelhardte79ec502007-12-17 22:44:06 -080082#define FWINV(bool, invflg) ((bool) ^ !!(ipinfo->invflags & (invflg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
Joe Perches3666ed12009-11-23 23:17:06 +010085 IPT_INV_SRCIP) ||
86 FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
87 IPT_INV_DSTIP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 dprintf("Source or dest mismatch.\n");
89
Harvey Harrisoncffee382008-10-31 00:53:08 -070090 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
91 &ip->saddr, &ipinfo->smsk.s_addr, &ipinfo->src.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
Harvey Harrisoncffee382008-10-31 00:53:08 -070093 dprintf("DST: %pI4 Mask: %pI4 Target: %pI4.%s\n",
94 &ip->daddr, &ipinfo->dmsk.s_addr, &ipinfo->dst.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -080096 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
Eric Dumazetb8dfe492009-03-25 17:31:52 +010099 ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
102 dprintf("VIA in mismatch (%s vs %s).%s\n",
103 indev, ipinfo->iniface,
104 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800105 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
Eric Dumazetb8dfe492009-03-25 17:31:52 +0100108 ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
111 dprintf("VIA out mismatch (%s vs %s).%s\n",
112 outdev, ipinfo->outiface,
113 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800114 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
117 /* Check specific protocol */
Joe Perches3666ed12009-11-23 23:17:06 +0100118 if (ipinfo->proto &&
119 FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 dprintf("Packet protocol %hi does not match %hi.%s\n",
121 ip->protocol, ipinfo->proto,
122 ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800123 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125
126 /* If we have a fragment rule but the packet is not a fragment
127 * then we return zero */
128 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
129 dprintf("Fragment rule but not fragment.%s\n",
130 ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800131 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
Patrick McHardy9c547952007-12-17 21:52:00 -0800134 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Denys Vlasenko022748a2008-01-14 23:44:05 -0800137static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138ip_checkentry(const struct ipt_ip *ip)
139{
140 if (ip->flags & ~IPT_F_MASK) {
141 duprintf("Unknown flag bits set: %08X\n",
142 ip->flags & ~IPT_F_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700143 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 if (ip->invflags & ~IPT_INV_MASK) {
146 duprintf("Unknown invflag bits set: %08X\n",
147 ip->invflags & ~IPT_INV_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700148 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700150 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200154ipt_error(struct sk_buff *skb, const struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Joe Perchese87cc472012-05-13 21:56:26 +0000156 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 return NF_DROP;
159}
160
Denys Vlasenko022748a2008-01-14 23:44:05 -0800161/* Performance critical */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static inline struct ipt_entry *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200163get_entry(const void *base, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 return (struct ipt_entry *)(base + offset);
166}
167
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700168/* All zeroes == unconditional rule. */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800169/* Mildly perf critical (only if packet tracing is on) */
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200170static inline bool unconditional(const struct ipt_ip *ip)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700171{
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200172 static const struct ipt_ip uncond;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700173
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200174 return memcmp(ip, &uncond, sizeof(uncond)) == 0;
Jan Engelhardte79ec502007-12-17 22:44:06 -0800175#undef FWINV
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700176}
177
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200178/* for const-correctness */
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200179static inline const struct xt_entry_target *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200180ipt_get_target_c(const struct ipt_entry *e)
181{
182 return ipt_get_target((struct ipt_entry *)e);
183}
184
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700185#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
186 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
Denys Vlasenko022748a2008-01-14 23:44:05 -0800187static const char *const hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800188 [NF_INET_PRE_ROUTING] = "PREROUTING",
189 [NF_INET_LOCAL_IN] = "INPUT",
Patrick McHardy9c547952007-12-17 21:52:00 -0800190 [NF_INET_FORWARD] = "FORWARD",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800191 [NF_INET_LOCAL_OUT] = "OUTPUT",
192 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700193};
194
195enum nf_ip_trace_comments {
196 NF_IP_TRACE_COMMENT_RULE,
197 NF_IP_TRACE_COMMENT_RETURN,
198 NF_IP_TRACE_COMMENT_POLICY,
199};
200
Denys Vlasenko022748a2008-01-14 23:44:05 -0800201static const char *const comments[] = {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700202 [NF_IP_TRACE_COMMENT_RULE] = "rule",
203 [NF_IP_TRACE_COMMENT_RETURN] = "return",
204 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
205};
206
207static struct nf_loginfo trace_loginfo = {
208 .type = NF_LOG_TYPE_LOG,
209 .u = {
210 .log = {
211 .level = 4,
212 .logflags = NF_LOG_MASK,
213 },
214 },
215};
216
Denys Vlasenko022748a2008-01-14 23:44:05 -0800217/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700218static inline int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200219get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e,
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200220 const char *hookname, const char **chainname,
221 const char **comment, unsigned int *rulenum)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700222{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200223 const struct xt_standard_target *t = (void *)ipt_get_target_c(s);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700224
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200225 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700226 /* Head of user chain: ERROR target with chainname */
227 *chainname = t->target.data;
228 (*rulenum) = 0;
229 } else if (s == e) {
230 (*rulenum)++;
231
Joe Perches3666ed12009-11-23 23:17:06 +0100232 if (s->target_offset == sizeof(struct ipt_entry) &&
233 strcmp(t->target.u.kernel.target->name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200234 XT_STANDARD_TARGET) == 0 &&
Joe Perches3666ed12009-11-23 23:17:06 +0100235 t->verdict < 0 &&
236 unconditional(&s->ip)) {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700237 /* Tail of chains: STANDARD target (return/policy) */
238 *comment = *chainname == hookname
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200239 ? comments[NF_IP_TRACE_COMMENT_POLICY]
240 : comments[NF_IP_TRACE_COMMENT_RETURN];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700241 }
242 return 1;
243 } else
244 (*rulenum)++;
245
246 return 0;
247}
248
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200249static void trace_packet(const struct sk_buff *skb,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700250 unsigned int hook,
251 const struct net_device *in,
252 const struct net_device *out,
Jan Engelhardtecb6f852008-01-31 03:54:47 -0800253 const char *tablename,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200254 const struct xt_table_info *private,
255 const struct ipt_entry *e)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700256{
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200257 const void *table_base;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200258 const struct ipt_entry *root;
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200259 const char *hookname, *chainname, *comment;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100260 const struct ipt_entry *iter;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700261 unsigned int rulenum = 0;
262
Jan Engelhardtccf5bd82009-04-15 20:27:03 +0200263 table_base = private->entries[smp_processor_id()];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700264 root = get_entry(table_base, private->hook_entry[hook]);
265
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200266 hookname = chainname = hooknames[hook];
267 comment = comments[NF_IP_TRACE_COMMENT_RULE];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700268
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100269 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
270 if (get_chainname_rulenum(iter, e, hookname,
271 &chainname, &comment, &rulenum) != 0)
272 break;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700273
274 nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo,
275 "TRACE: %s:%s:%s:%u ",
276 tablename, chainname, comment, rulenum);
277}
278#endif
279
Jan Engelhardt98e86402009-04-15 21:06:05 +0200280static inline __pure
281struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
282{
283 return (void *)entry + entry->next_offset;
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/* Returns one of the generic firewall policies, like NF_ACCEPT. */
287unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700288ipt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 unsigned int hook,
290 const struct net_device *in,
291 const struct net_device *out,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800292 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Jan Engelhardt5452e422008-04-14 11:15:35 +0200295 const struct iphdr *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* Initializing verdict to NF_DROP keeps gcc happy. */
297 unsigned int verdict = NF_DROP;
298 const char *indev, *outdev;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200299 const void *table_base;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200300 struct ipt_entry *e, **jumpstack;
301 unsigned int *stackptr, origptr, cpu;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200302 const struct xt_table_info *private;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200303 struct xt_action_param acpar;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200304 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* Initialization */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700307 ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 indev = in ? in->name : nulldevname;
309 outdev = out ? out->name : nulldevname;
310 /* We handle fragments by dealing with the first fragment as
311 * if it was a normal packet. All other fragments are treated
312 * normally, except that they will NEVER match rules that ask
313 * things we don't know, ie. tcp syn flag or ports). If the
314 * rule is also a fragment-specific rule, non-fragments won't
315 * match it. */
Jan Engelhardtde74c162009-07-05 18:26:37 +0200316 acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
317 acpar.thoff = ip_hdrlen(skb);
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200318 acpar.hotdrop = false;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200319 acpar.in = in;
320 acpar.out = out;
321 acpar.family = NFPROTO_IPV4;
322 acpar.hooknum = hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200325 local_bh_disable();
326 addend = xt_write_recseq_begin();
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700327 private = table->private;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200328 cpu = smp_processor_id();
329 table_base = private->entries[cpu];
330 jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
Eric Dumazet7489aec2010-05-31 16:41:35 +0200331 stackptr = per_cpu_ptr(private->stackptr, cpu);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200332 origptr = *stackptr;
Stephen Hemminger78454472009-02-20 10:35:32 +0100333
Harald Welte2e4e6a12006-01-12 13:30:04 -0800334 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Patrick McHardycecc74d2010-04-22 13:03:24 +0200336 pr_debug("Entering %s(hook %u); sp at %u (UF %p)\n",
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200337 table->name, hook, origptr,
338 get_entry(table_base, private->underflow[hook]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 do {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200341 const struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100342 const struct xt_entry_match *ematch;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 IP_NF_ASSERT(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200345 if (!ip_packet_match(ip, indev, outdev,
Jan Engelhardtde74c162009-07-05 18:26:37 +0200346 &e->ip, acpar.fragoff)) {
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100347 no_match:
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200348 e = ipt_next_entry(e);
349 continue;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Jan Engelhardtef53d702009-07-09 19:14:18 +0200352 xt_ematch_foreach(ematch, e) {
Jan Engelhardtde74c162009-07-05 18:26:37 +0200353 acpar.match = ematch->u.kernel.match;
354 acpar.matchinfo = ematch->data;
355 if (!acpar.match->match(skb, &acpar))
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100356 goto no_match;
Jan Engelhardtef53d702009-07-09 19:14:18 +0200357 }
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100358
Changli Gao7df08842010-07-23 16:25:11 +0200359 ADD_COUNTER(e->counters, skb->len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200361 t = ipt_get_target(e);
362 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700363
364#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
365 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200366 /* The packet is traced: log it */
367 if (unlikely(skb->nf_trace))
368 trace_packet(skb, hook, in, out,
369 table->name, private, e);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700370#endif
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200371 /* Standard target? */
372 if (!t->u.kernel.target->target) {
373 int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200375 v = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200376 if (v < 0) {
377 /* Pop from stack? */
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200378 if (v != XT_RETURN) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000379 verdict = (unsigned int)(-v) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 break;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200381 }
Eric Dumazetdb856672011-03-20 15:40:06 +0100382 if (*stackptr <= origptr) {
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200383 e = get_entry(table_base,
384 private->underflow[hook]);
Patrick McHardycecc74d2010-04-22 13:03:24 +0200385 pr_debug("Underflow (this is normal) "
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200386 "to %p\n", e);
387 } else {
388 e = jumpstack[--*stackptr];
Patrick McHardycecc74d2010-04-22 13:03:24 +0200389 pr_debug("Pulled %p out from pos %u\n",
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200390 e, *stackptr);
391 e = ipt_next_entry(e);
392 }
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200393 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Joe Perches3666ed12009-11-23 23:17:06 +0100395 if (table_base + v != ipt_next_entry(e) &&
396 !(e->ip.flags & IPT_F_GOTO)) {
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200397 if (*stackptr >= private->stacksize) {
398 verdict = NF_DROP;
399 break;
400 }
401 jumpstack[(*stackptr)++] = e;
Patrick McHardycecc74d2010-04-22 13:03:24 +0200402 pr_debug("Pushed %p into pos %u\n",
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200403 e, *stackptr - 1);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200406 e = get_entry(table_base, v);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200407 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200409
Jan Engelhardtde74c162009-07-05 18:26:37 +0200410 acpar.target = t->u.kernel.target;
411 acpar.targinfo = t->data;
Jan Engelhardtbb70dfa2009-04-15 21:40:13 +0200412
Jan Engelhardtde74c162009-07-05 18:26:37 +0200413 verdict = t->u.kernel.target->target(skb, &acpar);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200414 /* Target might have changed stuff. */
415 ip = ip_hdr(skb);
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200416 if (verdict == XT_CONTINUE)
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200417 e = ipt_next_entry(e);
418 else
419 /* Verdict */
420 break;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200421 } while (!acpar.hotdrop);
Patrick McHardycecc74d2010-04-22 13:03:24 +0200422 pr_debug("Exiting %s; resetting sp from %u to %u\n",
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200423 __func__, *stackptr, origptr);
424 *stackptr = origptr;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200425 xt_write_recseq_end(addend);
426 local_bh_enable();
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428#ifdef DEBUG_ALLOW_ALL
429 return NF_ACCEPT;
430#else
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200431 if (acpar.hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return NF_DROP;
433 else return verdict;
434#endif
435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/* Figures out from what hook each rule can be called: returns 0 if
438 there are loops. Puts hook bitmask in comefrom. */
439static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200440mark_source_chains(const struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800441 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 unsigned int hook;
444
445 /* No recursion; use packet counter to save back ptrs (reset
446 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800447 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800449 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (!(valid_hooks & (1 << hook)))
452 continue;
453
454 /* Set initial back pointer. */
455 e->counters.pcnt = pos;
456
457 for (;;) {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200458 const struct xt_standard_target *t
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200459 = (void *)ipt_get_target_c(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800460 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800462 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200463 pr_err("iptables: loop hook %u pos %u %08X.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 hook, pos, e->comefrom);
465 return 0;
466 }
Patrick McHardy9c547952007-12-17 21:52:00 -0800467 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 /* Unconditional return/END. */
Joe Perches3666ed12009-11-23 23:17:06 +0100470 if ((e->target_offset == sizeof(struct ipt_entry) &&
471 (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200472 XT_STANDARD_TARGET) == 0) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100473 t->verdict < 0 && unconditional(&e->ip)) ||
474 visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 unsigned int oldpos, size;
476
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100477 if ((strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200478 XT_STANDARD_TARGET) == 0) &&
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100479 t->verdict < -NF_MAX_VERDICT - 1) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800480 duprintf("mark_source_chains: bad "
481 "negative verdict (%i)\n",
482 t->verdict);
483 return 0;
484 }
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* Return: backtrack through the last
487 big jump. */
488 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800489 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490#ifdef DEBUG_IP_FIREWALL_USER
491 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800492 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 duprintf("Back unset "
494 "on hook %u "
495 "rule %u\n",
496 hook, pos);
497 }
498#endif
499 oldpos = pos;
500 pos = e->counters.pcnt;
501 e->counters.pcnt = 0;
502
503 /* We're at the start. */
504 if (pos == oldpos)
505 goto next;
506
507 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800508 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 } while (oldpos == pos + e->next_offset);
510
511 /* Move along one */
512 size = e->next_offset;
513 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800514 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 e->counters.pcnt = pos;
516 pos += size;
517 } else {
518 int newpos = t->verdict;
519
520 if (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200521 XT_STANDARD_TARGET) == 0 &&
Joe Perches3666ed12009-11-23 23:17:06 +0100522 newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800523 if (newpos > newinfo->size -
524 sizeof(struct ipt_entry)) {
525 duprintf("mark_source_chains: "
526 "bad verdict (%i)\n",
527 newpos);
528 return 0;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* This a jump; chase it. */
531 duprintf("Jump rule %u -> %u\n",
532 pos, newpos);
533 } else {
534 /* ... this is a fallthru */
535 newpos = pos + e->next_offset;
536 }
537 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800538 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 e->counters.pcnt = pos;
540 pos = newpos;
541 }
542 }
543 next:
544 duprintf("Finished chain %u\n", hook);
545 }
546 return 1;
547}
548
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200549static void cleanup_match(struct xt_entry_match *m, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200551 struct xt_mtdtor_param par;
552
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100553 par.net = net;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200554 par.match = m->u.kernel.match;
555 par.matchinfo = m->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200556 par.family = NFPROTO_IPV4;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200557 if (par.match->destroy != NULL)
558 par.match->destroy(&par);
559 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Denys Vlasenko022748a2008-01-14 23:44:05 -0800562static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200563check_entry(const struct ipt_entry *e, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800564{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200565 const struct xt_entry_target *t;
Dmitry Mishina96be242006-12-12 00:29:26 -0800566
567 if (!ip_checkentry(&e->ip)) {
Sebastian Andrzej Siewior63f6fe92011-06-16 17:16:37 +0200568 duprintf("ip check failed %p %s.\n", e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -0800569 return -EINVAL;
570 }
571
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200572 if (e->target_offset + sizeof(struct xt_entry_target) >
Patrick McHardy9c547952007-12-17 21:52:00 -0800573 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800574 return -EINVAL;
575
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200576 t = ipt_get_target_c(e);
Dmitry Mishina96be242006-12-12 00:29:26 -0800577 if (e->target_offset + t->u.target_size > e->next_offset)
578 return -EINVAL;
579
580 return 0;
581}
582
Denys Vlasenko022748a2008-01-14 23:44:05 -0800583static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200584check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Dmitry Mishina96be242006-12-12 00:29:26 -0800585{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200586 const struct ipt_ip *ip = par->entryinfo;
Dmitry Mishina96be242006-12-12 00:29:26 -0800587 int ret;
588
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200589 par->match = m->u.kernel.match;
590 par->matchinfo = m->data;
591
Jan Engelhardt916a9172008-10-08 11:35:20 +0200592 ret = xt_check_match(par, m->u.match_size - sizeof(*m),
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200593 ip->proto, ip->invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200594 if (ret < 0) {
Jan Engelhardtb5cad0d2010-05-02 13:55:21 +0200595 duprintf("check failed for `%s'.\n", par->match->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200596 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800597 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200598 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800599}
600
Denys Vlasenko022748a2008-01-14 23:44:05 -0800601static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200602find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800604 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800605 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200607 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
608 m->u.user.revision);
609 if (IS_ERR(match)) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800610 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200611 return PTR_ERR(match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613 m->u.kernel.match = match;
614
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100615 ret = check_match(m, par);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800616 if (ret)
617 goto err;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800620err:
621 module_put(m->u.kernel.match->me);
622 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Patrick McHardyadd67462010-02-03 13:45:12 +0100625static int check_target(struct ipt_entry *e, struct net *net, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800626{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200627 struct xt_entry_target *t = ipt_get_target(e);
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200628 struct xt_tgchk_param par = {
Patrick McHardyadd67462010-02-03 13:45:12 +0100629 .net = net,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200630 .table = name,
631 .entryinfo = e,
632 .target = t->u.kernel.target,
633 .targinfo = t->data,
634 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200635 .family = NFPROTO_IPV4,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200636 };
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900637 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800638
Jan Engelhardt916a9172008-10-08 11:35:20 +0200639 ret = xt_check_target(&par, t->u.target_size - sizeof(*t),
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200640 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200641 if (ret < 0) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100642 duprintf("check failed for `%s'.\n",
Dmitry Mishina96be242006-12-12 00:29:26 -0800643 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200644 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800645 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200646 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800647}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Denys Vlasenko022748a2008-01-14 23:44:05 -0800649static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100650find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
Jan Engelhardt05595182010-02-24 18:33:43 +0100651 unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200653 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800654 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 int ret;
656 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200657 struct xt_mtchk_param mtpar;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100658 struct xt_entry_match *ematch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Dmitry Mishina96be242006-12-12 00:29:26 -0800660 ret = check_entry(e, name);
661 if (ret)
662 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 j = 0;
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100665 mtpar.net = net;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200666 mtpar.table = name;
667 mtpar.entryinfo = &e->ip;
668 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200669 mtpar.family = NFPROTO_IPV4;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100670 xt_ematch_foreach(ematch, e) {
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100671 ret = find_check_match(ematch, &mtpar);
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100672 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100673 goto cleanup_matches;
674 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 t = ipt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200678 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
679 t->u.user.revision);
680 if (IS_ERR(target)) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800681 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200682 ret = PTR_ERR(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto cleanup_matches;
684 }
685 t->u.kernel.target = target;
686
Patrick McHardyadd67462010-02-03 13:45:12 +0100687 ret = check_target(e, net, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800688 if (ret)
689 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800691 err:
692 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 cleanup_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100694 xt_ematch_foreach(ematch, e) {
695 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100696 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100697 cleanup_match(ematch, net);
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return ret;
700}
701
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200702static bool check_underflow(const struct ipt_entry *e)
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200703{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200704 const struct xt_entry_target *t;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200705 unsigned int verdict;
706
707 if (!unconditional(&e->ip))
708 return false;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200709 t = ipt_get_target_c(e);
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200710 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
711 return false;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200712 verdict = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200713 verdict = -verdict - 1;
714 return verdict == NF_DROP || verdict == NF_ACCEPT;
715}
716
Denys Vlasenko022748a2008-01-14 23:44:05 -0800717static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800719 struct xt_table_info *newinfo,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200720 const unsigned char *base,
721 const unsigned char *limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 const unsigned int *hook_entries,
723 const unsigned int *underflows,
Jan Engelhardt05595182010-02-24 18:33:43 +0100724 unsigned int valid_hooks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 unsigned int h;
727
Joe Perches3666ed12009-11-23 23:17:06 +0100728 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
729 (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 duprintf("Bad offset %p\n", e);
731 return -EINVAL;
732 }
733
734 if (e->next_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200735 < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 duprintf("checking: element %p size %u\n",
737 e, e->next_offset);
738 return -EINVAL;
739 }
740
741 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800742 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Jan Engelhardta7d51732009-07-18 14:52:58 +0200743 if (!(valid_hooks & (1 << h)))
744 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if ((unsigned char *)e - base == hook_entries[h])
746 newinfo->hook_entry[h] = hook_entries[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200747 if ((unsigned char *)e - base == underflows[h]) {
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200748 if (!check_underflow(e)) {
749 pr_err("Underflows must be unconditional and "
750 "use the STANDARD target with "
751 "ACCEPT/DROP\n");
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200752 return -EINVAL;
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 newinfo->underflow[h] = underflows[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800759 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 e->comefrom = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 0;
762}
763
Jan Engelhardt05595182010-02-24 18:33:43 +0100764static void
765cleanup_entry(struct ipt_entry *e, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200767 struct xt_tgdtor_param par;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200768 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100769 struct xt_entry_match *ematch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100772 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100773 cleanup_match(ematch, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 t = ipt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200775
Patrick McHardyadd67462010-02-03 13:45:12 +0100776 par.net = net;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200777 par.target = t->u.kernel.target;
778 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200779 par.family = NFPROTO_IPV4;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200780 if (par.target->destroy != NULL)
781 par.target->destroy(&par);
782 module_put(par.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785/* Checks and translates the user-supplied table segment (held in
786 newinfo) */
787static int
Jan Engelhardt0f234212010-02-24 18:36:04 +0100788translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
789 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100791 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 unsigned int i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100793 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Jan Engelhardt0f234212010-02-24 18:36:04 +0100795 newinfo->size = repl->size;
796 newinfo->number = repl->num_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800799 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 newinfo->hook_entry[i] = 0xFFFFFFFF;
801 newinfo->underflow[i] = 0xFFFFFFFF;
802 }
803
804 duprintf("translate_table: size %u\n", newinfo->size);
805 i = 0;
806 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100807 xt_entry_foreach(iter, entry0, newinfo->size) {
808 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +0100809 entry0 + repl->size,
810 repl->hook_entry,
811 repl->underflow,
812 repl->valid_hooks);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100813 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +0100814 return ret;
815 ++i;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200816 if (strcmp(ipt_get_target(iter)->u.user.name,
817 XT_ERROR_TARGET) == 0)
818 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Jan Engelhardt0f234212010-02-24 18:36:04 +0100821 if (i != repl->num_entries) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 duprintf("translate_table: %u not %u entries\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100823 i, repl->num_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -EINVAL;
825 }
826
827 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800828 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* Only hooks which are valid */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100830 if (!(repl->valid_hooks & (1 << i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 continue;
832 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
833 duprintf("Invalid hook entry %u %u\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100834 i, repl->hook_entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return -EINVAL;
836 }
837 if (newinfo->underflow[i] == 0xFFFFFFFF) {
838 duprintf("Invalid underflow %u %u\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100839 i, repl->underflow[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return -EINVAL;
841 }
842 }
843
Jan Engelhardt0f234212010-02-24 18:36:04 +0100844 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0))
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800845 return -ELOOP;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* Finally, each sanity check must pass */
848 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100849 xt_entry_foreach(iter, entry0, newinfo->size) {
Jan Engelhardt0f234212010-02-24 18:36:04 +0100850 ret = find_check_entry(iter, net, repl->name, repl->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100851 if (ret != 0)
852 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100853 ++i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800856 if (ret != 0) {
Jan Engelhardt05595182010-02-24 18:33:43 +0100857 xt_entry_foreach(iter, entry0, newinfo->size) {
858 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100859 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100860 cleanup_entry(iter, net);
861 }
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800862 return ret;
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700866 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800867 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
868 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
871 return ret;
872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800875get_counters(const struct xt_table_info *t,
876 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100878 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 unsigned int cpu;
880 unsigned int i;
881
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700882 for_each_possible_cpu(cpu) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200883 seqcount_t *s = &per_cpu(xt_recseq, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 i = 0;
Jan Engelhardt05595182010-02-24 18:33:43 +0100886 xt_entry_foreach(iter, t->entries[cpu], t->size) {
Eric Dumazet83723d62011-01-10 20:11:38 +0100887 u64 bcnt, pcnt;
888 unsigned int start;
889
890 do {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200891 start = read_seqcount_begin(s);
Eric Dumazet83723d62011-01-10 20:11:38 +0100892 bcnt = iter->counters.bcnt;
893 pcnt = iter->counters.pcnt;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200894 } while (read_seqcount_retry(s, start));
Eric Dumazet83723d62011-01-10 20:11:38 +0100895
896 ADD_COUNTER(counters[i], bcnt, pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +0100897 ++i; /* macro does multi eval of i */
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100900}
901
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200902static struct xt_counters *alloc_counters(const struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Dmitry Mishin27229712006-04-01 02:25:19 -0800904 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800905 struct xt_counters *counters;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200906 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 /* We need atomic snapshot of counters: rest doesn't change
909 (other than comefrom, which userspace doesn't care
910 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800911 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet83723d62011-01-10 20:11:38 +0100912 counters = vzalloc(countersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700915 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700917 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Dmitry Mishin27229712006-04-01 02:25:19 -0800919 return counters;
920}
921
922static int
923copy_entries_to_user(unsigned int total_size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200924 const struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800925 void __user *userptr)
926{
927 unsigned int off, num;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200928 const struct ipt_entry *e;
Dmitry Mishin27229712006-04-01 02:25:19 -0800929 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200930 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800931 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200932 const void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -0800933
934 counters = alloc_counters(table);
935 if (IS_ERR(counters))
936 return PTR_ERR(counters);
937
Eric Dumazet31836062005-12-13 23:13:48 -0800938 /* choose the copy that is on our node/cpu, ...
939 * This choice is lazy (because current thread is
940 * allowed to migrate to another cpu)
941 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800942 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800943 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 ret = -EFAULT;
945 goto free_counters;
946 }
947
948 /* FIXME: use iterator macros --RR */
949 /* ... then go back and fix counters and names */
950 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
951 unsigned int i;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200952 const struct xt_entry_match *m;
953 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Eric Dumazet31836062005-12-13 23:13:48 -0800955 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (copy_to_user(userptr + off
957 + offsetof(struct ipt_entry, counters),
958 &counters[num],
959 sizeof(counters[num])) != 0) {
960 ret = -EFAULT;
961 goto free_counters;
962 }
963
964 for (i = sizeof(struct ipt_entry);
965 i < e->target_offset;
966 i += m->u.match_size) {
967 m = (void *)e + i;
968
969 if (copy_to_user(userptr + off + i
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200970 + offsetof(struct xt_entry_match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 u.user.name),
972 m->u.kernel.match->name,
973 strlen(m->u.kernel.match->name)+1)
974 != 0) {
975 ret = -EFAULT;
976 goto free_counters;
977 }
978 }
979
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200980 t = ipt_get_target_c(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (copy_to_user(userptr + off + e->target_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200982 + offsetof(struct xt_entry_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 u.user.name),
984 t->u.kernel.target->name,
985 strlen(t->u.kernel.target->name)+1) != 0) {
986 ret = -EFAULT;
987 goto free_counters;
988 }
989 }
990
991 free_counters:
992 vfree(counters);
993 return ret;
994}
995
Dmitry Mishin27229712006-04-01 02:25:19 -0800996#ifdef CONFIG_COMPAT
Jan Engelhardt739674f2009-06-26 08:23:19 +0200997static void compat_standard_from_user(void *dst, const void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -0800998{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700999 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001000
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001001 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001002 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001003 memcpy(dst, &v, sizeof(v));
1004}
1005
Jan Engelhardt739674f2009-06-26 08:23:19 +02001006static int compat_standard_to_user(void __user *dst, const void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001007{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001008 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001009
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001010 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001011 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001012 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001013}
1014
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001015static int compat_calc_entry(const struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001016 const struct xt_table_info *info,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001017 const void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001018{
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001019 const struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001020 const struct xt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001021 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001022 int off, i, ret;
1023
Patrick McHardy30c08c42007-12-17 21:47:14 -08001024 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001025 entry_offset = (void *)e - base;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001026 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001027 off += xt_compat_match_offset(ematch->u.kernel.match);
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001028 t = ipt_get_target_c(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001029 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001030 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001031 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001032 if (ret)
1033 return ret;
1034
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001035 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001036 if (info->hook_entry[i] &&
1037 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001038 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001039 if (info->underflow[i] &&
1040 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001041 newinfo->underflow[i] -= off;
1042 }
1043 return 0;
1044}
1045
Eric Dumazet259d4e42007-12-04 23:24:56 -08001046static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001047 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001048{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001049 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001050 void *loc_cpu_entry;
Jan Engelhardt05595182010-02-24 18:33:43 +01001051 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001052
1053 if (!newinfo || !info)
1054 return -EINVAL;
1055
Eric Dumazet259d4e42007-12-04 23:24:56 -08001056 /* we dont care about newinfo->entries[] */
1057 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1058 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001059 loc_cpu_entry = info->entries[raw_smp_processor_id()];
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001060 xt_compat_init_offsets(AF_INET, info->number);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001061 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
1062 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
1063 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001064 return ret;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001065 }
Jan Engelhardt05595182010-02-24 18:33:43 +01001066 return 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001067}
1068#endif
1069
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001070static int get_info(struct net *net, void __user *user,
1071 const int *len, int compat)
Dmitry Mishin27229712006-04-01 02:25:19 -08001072{
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001073 char name[XT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001074 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001075 int ret;
1076
1077 if (*len != sizeof(struct ipt_getinfo)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001078 duprintf("length %u != %zu\n", *len,
1079 sizeof(struct ipt_getinfo));
Dmitry Mishin27229712006-04-01 02:25:19 -08001080 return -EINVAL;
1081 }
1082
1083 if (copy_from_user(name, user, sizeof(name)) != 0)
1084 return -EFAULT;
1085
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001086 name[XT_TABLE_MAXNAMELEN-1] = '\0';
Dmitry Mishin27229712006-04-01 02:25:19 -08001087#ifdef CONFIG_COMPAT
1088 if (compat)
1089 xt_compat_lock(AF_INET);
1090#endif
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001091 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001092 "iptable_%s", name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001093 if (t && !IS_ERR(t)) {
1094 struct ipt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001095 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001096#ifdef CONFIG_COMPAT
Alexey Dobriyan14c7dbe2010-02-08 11:17:43 -08001097 struct xt_table_info tmp;
1098
Dmitry Mishin27229712006-04-01 02:25:19 -08001099 if (compat) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001100 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001101 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001102 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001103 }
1104#endif
Vasiliy Kulikovb5f15ac2010-11-03 08:45:06 +01001105 memset(&info, 0, sizeof(info));
Dmitry Mishin27229712006-04-01 02:25:19 -08001106 info.valid_hooks = t->valid_hooks;
1107 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001108 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001109 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001110 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001111 info.num_entries = private->number;
1112 info.size = private->size;
1113 strcpy(info.name, name);
1114
1115 if (copy_to_user(user, &info, *len) != 0)
1116 ret = -EFAULT;
1117 else
1118 ret = 0;
1119
1120 xt_table_unlock(t);
1121 module_put(t->me);
1122 } else
1123 ret = t ? PTR_ERR(t) : -ENOENT;
1124#ifdef CONFIG_COMPAT
1125 if (compat)
1126 xt_compat_unlock(AF_INET);
1127#endif
1128 return ret;
1129}
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001132get_entries(struct net *net, struct ipt_get_entries __user *uptr,
1133 const int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001136 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001137 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Dmitry Mishin27229712006-04-01 02:25:19 -08001139 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001140 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001141 return -EINVAL;
1142 }
1143 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1144 return -EFAULT;
1145 if (*len != sizeof(struct ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001146 duprintf("get_entries: %u != %zu\n",
1147 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001148 return -EINVAL;
1149 }
1150
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001151 t = xt_find_table_lock(net, AF_INET, get.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001153 const struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001154 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001155 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001156 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 t, uptr->entrytable);
1158 else {
1159 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001160 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001161 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001164 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 } else
1166 ret = t ? PTR_ERR(t) : -ENOENT;
1167
1168 return ret;
1169}
1170
1171static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001172__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001173 struct xt_table_info *newinfo, unsigned int num_counters,
1174 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001175{
1176 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001177 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001178 struct xt_table_info *oldinfo;
1179 struct xt_counters *counters;
1180 void *loc_cpu_old_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001181 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001182
1183 ret = 0;
Eric Dumazet83723d62011-01-10 20:11:38 +01001184 counters = vzalloc(num_counters * sizeof(struct xt_counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001185 if (!counters) {
1186 ret = -ENOMEM;
1187 goto out;
1188 }
1189
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001190 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Dmitry Mishin27229712006-04-01 02:25:19 -08001191 "iptable_%s", name);
1192 if (!t || IS_ERR(t)) {
1193 ret = t ? PTR_ERR(t) : -ENOENT;
1194 goto free_newinfo_counters_untrans;
1195 }
1196
1197 /* You lied! */
1198 if (valid_hooks != t->valid_hooks) {
1199 duprintf("Valid hook crap: %08X vs %08X\n",
1200 valid_hooks, t->valid_hooks);
1201 ret = -EINVAL;
1202 goto put_module;
1203 }
1204
1205 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1206 if (!oldinfo)
1207 goto put_module;
1208
1209 /* Update module usage count based on number of rules */
1210 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1211 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1212 if ((oldinfo->number > oldinfo->initial_entries) ||
1213 (newinfo->number <= oldinfo->initial_entries))
1214 module_put(t->me);
1215 if ((oldinfo->number > oldinfo->initial_entries) &&
1216 (newinfo->number <= oldinfo->initial_entries))
1217 module_put(t->me);
1218
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001219 /* Get the old counters, and synchronize with replace */
Dmitry Mishin27229712006-04-01 02:25:19 -08001220 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001221
Dmitry Mishin27229712006-04-01 02:25:19 -08001222 /* Decrease module usage counts and free resource */
1223 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001224 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001225 cleanup_entry(iter, net);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001226
Dmitry Mishin27229712006-04-01 02:25:19 -08001227 xt_free_table_info(oldinfo);
1228 if (copy_to_user(counters_ptr, counters,
1229 sizeof(struct xt_counters) * num_counters) != 0)
1230 ret = -EFAULT;
1231 vfree(counters);
1232 xt_table_unlock(t);
1233 return ret;
1234
1235 put_module:
1236 module_put(t->me);
1237 xt_table_unlock(t);
1238 free_newinfo_counters_untrans:
1239 vfree(counters);
1240 out:
1241 return ret;
1242}
1243
1244static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001245do_replace(struct net *net, const void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 int ret;
1248 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001249 struct xt_table_info *newinfo;
1250 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001251 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1254 return -EFAULT;
1255
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001256 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001257 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1258 return -ENOMEM;
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01001259 tmp.name[sizeof(tmp.name)-1] = 0;
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001260
Harald Welte2e4e6a12006-01-12 13:30:04 -08001261 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (!newinfo)
1263 return -ENOMEM;
1264
Patrick McHardy9c547952007-12-17 21:52:00 -08001265 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001266 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1267 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 tmp.size) != 0) {
1269 ret = -EFAULT;
1270 goto free_newinfo;
1271 }
1272
Jan Engelhardt0f234212010-02-24 18:36:04 +01001273 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001275 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01001277 duprintf("Translated table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001279 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001280 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001281 if (ret)
1282 goto free_newinfo_untrans;
1283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Dmitry Mishin27229712006-04-01 02:25:19 -08001285 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001286 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001287 cleanup_entry(iter, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001289 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return ret;
1291}
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001294do_add_counters(struct net *net, const void __user *user,
1295 unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001297 unsigned int i, curcpu;
Dmitry Mishin27229712006-04-01 02:25:19 -08001298 struct xt_counters_info tmp;
1299 struct xt_counters *paddc;
1300 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001301 const char *name;
Dmitry Mishin27229712006-04-01 02:25:19 -08001302 int size;
1303 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001304 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001305 const struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001307 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001308 struct ipt_entry *iter;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001309 unsigned int addend;
Dmitry Mishin27229712006-04-01 02:25:19 -08001310#ifdef CONFIG_COMPAT
1311 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Dmitry Mishin27229712006-04-01 02:25:19 -08001313 if (compat) {
1314 ptmp = &compat_tmp;
1315 size = sizeof(struct compat_xt_counters_info);
1316 } else
1317#endif
1318 {
1319 ptmp = &tmp;
1320 size = sizeof(struct xt_counters_info);
1321 }
1322
1323 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return -EFAULT;
1325
Dmitry Mishin27229712006-04-01 02:25:19 -08001326#ifdef CONFIG_COMPAT
1327 if (compat) {
1328 num_counters = compat_tmp.num_counters;
1329 name = compat_tmp.name;
1330 } else
1331#endif
1332 {
1333 num_counters = tmp.num_counters;
1334 name = tmp.name;
1335 }
1336
1337 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -EINVAL;
1339
Eric Dumazete12f8e22010-06-04 13:31:29 +02001340 paddc = vmalloc(len - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (!paddc)
1342 return -ENOMEM;
1343
Dmitry Mishin27229712006-04-01 02:25:19 -08001344 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 ret = -EFAULT;
1346 goto free;
1347 }
1348
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001349 t = xt_find_table_lock(net, AF_INET, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (!t || IS_ERR(t)) {
1351 ret = t ? PTR_ERR(t) : -ENOENT;
1352 goto free;
1353 }
1354
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001355 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001356 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001357 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 ret = -EINVAL;
1359 goto unlock_up_free;
1360 }
1361
1362 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001363 /* Choose the copy that is on our node */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001364 curcpu = smp_processor_id();
1365 loc_cpu_entry = private->entries[curcpu];
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001366 addend = xt_write_recseq_begin();
Jan Engelhardt05595182010-02-24 18:33:43 +01001367 xt_entry_foreach(iter, loc_cpu_entry, private->size) {
1368 ADD_COUNTER(iter->counters, paddc[i].bcnt, paddc[i].pcnt);
1369 ++i;
1370 }
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001371 xt_write_recseq_end(addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001373 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001374 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 module_put(t->me);
1376 free:
1377 vfree(paddc);
1378
1379 return ret;
1380}
1381
Dmitry Mishin27229712006-04-01 02:25:19 -08001382#ifdef CONFIG_COMPAT
1383struct compat_ipt_replace {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001384 char name[XT_TABLE_MAXNAMELEN];
Dmitry Mishin27229712006-04-01 02:25:19 -08001385 u32 valid_hooks;
1386 u32 num_entries;
1387 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001388 u32 hook_entry[NF_INET_NUMHOOKS];
1389 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001390 u32 num_counters;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001391 compat_uptr_t counters; /* struct xt_counters * */
Dmitry Mishin27229712006-04-01 02:25:19 -08001392 struct compat_ipt_entry entries[0];
1393};
1394
Patrick McHardya18aa312007-12-12 10:35:16 -08001395static int
1396compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001397 unsigned int *size, struct xt_counters *counters,
Jan Engelhardt05595182010-02-24 18:33:43 +01001398 unsigned int i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001399{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001400 struct xt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001401 struct compat_ipt_entry __user *ce;
1402 u_int16_t target_offset, next_offset;
1403 compat_uint_t origsize;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001404 const struct xt_entry_match *ematch;
1405 int ret = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001406
Dmitry Mishin27229712006-04-01 02:25:19 -08001407 origsize = *size;
1408 ce = (struct compat_ipt_entry __user *)*dstptr;
Jan Engelhardt05595182010-02-24 18:33:43 +01001409 if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
1410 copy_to_user(&ce->counters, &counters[i],
1411 sizeof(counters[i])) != 0)
1412 return -EFAULT;
Patrick McHardya18aa312007-12-12 10:35:16 -08001413
Dmitry Mishin27229712006-04-01 02:25:19 -08001414 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001415 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1416
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001417 xt_ematch_foreach(ematch, e) {
1418 ret = xt_compat_match_to_user(ematch, dstptr, size);
1419 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001420 return ret;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001421 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001422 target_offset = e->target_offset - (origsize - *size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001423 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001424 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001425 if (ret)
Jan Engelhardt05595182010-02-24 18:33:43 +01001426 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001427 next_offset = e->next_offset - (origsize - *size);
Jan Engelhardt05595182010-02-24 18:33:43 +01001428 if (put_user(target_offset, &ce->target_offset) != 0 ||
1429 put_user(next_offset, &ce->next_offset) != 0)
1430 return -EFAULT;
Dmitry Mishin27229712006-04-01 02:25:19 -08001431 return 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001432}
1433
Denys Vlasenko022748a2008-01-14 23:44:05 -08001434static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001435compat_find_calc_match(struct xt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001436 const char *name,
1437 const struct ipt_ip *ip,
1438 unsigned int hookmask,
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001439 int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001440{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001441 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001442
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001443 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
1444 m->u.user.revision);
1445 if (IS_ERR(match)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001446 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001447 m->u.user.name);
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001448 return PTR_ERR(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001449 }
1450 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001451 *size += xt_compat_match_offset(match);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001452 return 0;
1453}
1454
Jan Engelhardt05595182010-02-24 18:33:43 +01001455static void compat_release_entry(struct compat_ipt_entry *e)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001456{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001457 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001458 struct xt_entry_match *ematch;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001459
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001460 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001461 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001462 module_put(ematch->u.kernel.match->me);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001463 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001464 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001465}
1466
Denys Vlasenko022748a2008-01-14 23:44:05 -08001467static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001468check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001469 struct xt_table_info *newinfo,
1470 unsigned int *size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001471 const unsigned char *base,
1472 const unsigned char *limit,
1473 const unsigned int *hook_entries,
1474 const unsigned int *underflows,
Patrick McHardy4b478242007-12-17 21:46:15 -08001475 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001476{
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001477 struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001478 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001479 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001480 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001481 unsigned int j;
1482 int ret, off, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001483
1484 duprintf("check_compat_entry_size_and_hooks %p\n", e);
Joe Perches3666ed12009-11-23 23:17:06 +01001485 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
1486 (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001487 duprintf("Bad offset %p, limit = %p\n", e, limit);
1488 return -EINVAL;
1489 }
1490
1491 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001492 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001493 duprintf("checking: element %p size %u\n",
1494 e, e->next_offset);
1495 return -EINVAL;
1496 }
1497
Patrick McHardy73cd5982007-12-17 21:47:32 -08001498 /* For purposes of check_entry casting the compat entry is fine */
1499 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001500 if (ret)
1501 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001502
Patrick McHardy30c08c42007-12-17 21:47:14 -08001503 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001504 entry_offset = (void *)e - (void *)base;
1505 j = 0;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001506 xt_ematch_foreach(ematch, e) {
1507 ret = compat_find_calc_match(ematch, name,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001508 &e->ip, e->comefrom, &off);
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001509 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001510 goto release_matches;
1511 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001512 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001513
Patrick McHardy73cd5982007-12-17 21:47:32 -08001514 t = compat_ipt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001515 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
1516 t->u.user.revision);
1517 if (IS_ERR(target)) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001518 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001519 t->u.user.name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001520 ret = PTR_ERR(target);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001521 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001522 }
1523 t->u.kernel.target = target;
1524
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001525 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001526 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001527 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001528 if (ret)
1529 goto out;
1530
1531 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001532 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001533 if ((unsigned char *)e - base == hook_entries[h])
1534 newinfo->hook_entry[h] = hook_entries[h];
1535 if ((unsigned char *)e - base == underflows[h])
1536 newinfo->underflow[h] = underflows[h];
1537 }
1538
1539 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001540 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001541 e->comefrom = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001542 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001543
Dmitry Mishin27229712006-04-01 02:25:19 -08001544out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001545 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001546release_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001547 xt_ematch_foreach(ematch, e) {
1548 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001549 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001550 module_put(ematch->u.kernel.match->me);
1551 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001552 return ret;
1553}
1554
Patrick McHardy4b478242007-12-17 21:46:15 -08001555static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001556compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001557 unsigned int *size, const char *name,
1558 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001559{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001560 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001561 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001562 struct ipt_entry *de;
1563 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001564 int ret, h;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001565 struct xt_entry_match *ematch;
Dmitry Mishin27229712006-04-01 02:25:19 -08001566
1567 ret = 0;
1568 origsize = *size;
1569 de = (struct ipt_entry *)*dstptr;
1570 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001571 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001572
Patrick McHardy73cd5982007-12-17 21:47:32 -08001573 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001574 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1575
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001576 xt_ematch_foreach(ematch, e) {
1577 ret = xt_compat_match_from_user(ematch, dstptr, size);
1578 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001579 return ret;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001580 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001581 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001582 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001583 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001584 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001585
1586 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001587 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001588 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1589 newinfo->hook_entry[h] -= origsize - *size;
1590 if ((unsigned char *)de - base < newinfo->underflow[h])
1591 newinfo->underflow[h] -= origsize - *size;
1592 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001593 return ret;
1594}
Dmitry Mishin27229712006-04-01 02:25:19 -08001595
Denys Vlasenko022748a2008-01-14 23:44:05 -08001596static int
Jan Engelhardt05595182010-02-24 18:33:43 +01001597compat_check_entry(struct ipt_entry *e, struct net *net, const char *name)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001598{
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001599 struct xt_entry_match *ematch;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001600 struct xt_mtchk_param mtpar;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001601 unsigned int j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001602 int ret = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001603
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001604 j = 0;
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001605 mtpar.net = net;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001606 mtpar.table = name;
1607 mtpar.entryinfo = &e->ip;
1608 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +02001609 mtpar.family = NFPROTO_IPV4;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001610 xt_ematch_foreach(ematch, e) {
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001611 ret = check_match(ematch, &mtpar);
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001612 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001613 goto cleanup_matches;
1614 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001615 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001616
Patrick McHardyadd67462010-02-03 13:45:12 +01001617 ret = check_target(e, net, name);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001618 if (ret)
1619 goto cleanup_matches;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001620 return 0;
1621
1622 cleanup_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001623 xt_ematch_foreach(ematch, e) {
1624 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001625 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001626 cleanup_match(ematch, net);
1627 }
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001628 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001629}
1630
Dmitry Mishin27229712006-04-01 02:25:19 -08001631static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001632translate_compat_table(struct net *net,
1633 const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001634 unsigned int valid_hooks,
1635 struct xt_table_info **pinfo,
1636 void **pentry0,
1637 unsigned int total_size,
1638 unsigned int number,
1639 unsigned int *hook_entries,
1640 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001641{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001642 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001643 struct xt_table_info *newinfo, *info;
1644 void *pos, *entry0, *entry1;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001645 struct compat_ipt_entry *iter0;
1646 struct ipt_entry *iter1;
Dmitry Mishin27229712006-04-01 02:25:19 -08001647 unsigned int size;
Jan Engelhardt05595182010-02-24 18:33:43 +01001648 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001649
1650 info = *pinfo;
1651 entry0 = *pentry0;
1652 size = total_size;
1653 info->number = number;
1654
1655 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001656 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001657 info->hook_entry[i] = 0xFFFFFFFF;
1658 info->underflow[i] = 0xFFFFFFFF;
1659 }
1660
1661 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001662 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001663 xt_compat_lock(AF_INET);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001664 xt_compat_init_offsets(AF_INET, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001665 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001666 xt_entry_foreach(iter0, entry0, total_size) {
1667 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001668 entry0,
1669 entry0 + total_size,
1670 hook_entries,
1671 underflows,
1672 name);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001673 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001674 goto out_unlock;
1675 ++j;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001676 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001677
1678 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001679 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001680 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001681 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001682 goto out_unlock;
1683 }
1684
1685 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001686 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001687 /* Only hooks which are valid */
1688 if (!(valid_hooks & (1 << i)))
1689 continue;
1690 if (info->hook_entry[i] == 0xFFFFFFFF) {
1691 duprintf("Invalid hook entry %u %u\n",
1692 i, hook_entries[i]);
1693 goto out_unlock;
1694 }
1695 if (info->underflow[i] == 0xFFFFFFFF) {
1696 duprintf("Invalid underflow %u %u\n",
1697 i, underflows[i]);
1698 goto out_unlock;
1699 }
1700 }
1701
1702 ret = -ENOMEM;
1703 newinfo = xt_alloc_table_info(size);
1704 if (!newinfo)
1705 goto out_unlock;
1706
1707 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001708 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001709 newinfo->hook_entry[i] = info->hook_entry[i];
1710 newinfo->underflow[i] = info->underflow[i];
1711 }
1712 entry1 = newinfo->entries[raw_smp_processor_id()];
1713 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001714 size = total_size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001715 xt_entry_foreach(iter0, entry0, total_size) {
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001716 ret = compat_copy_entry_from_user(iter0, &pos, &size,
1717 name, newinfo, entry1);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001718 if (ret != 0)
1719 break;
1720 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001721 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001722 xt_compat_unlock(AF_INET);
1723 if (ret)
1724 goto free_newinfo;
1725
1726 ret = -ELOOP;
1727 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1728 goto free_newinfo;
1729
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001730 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001731 xt_entry_foreach(iter1, entry1, newinfo->size) {
Jan Engelhardt05595182010-02-24 18:33:43 +01001732 ret = compat_check_entry(iter1, net, name);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001733 if (ret != 0)
1734 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001735 ++i;
Florian Westphalcca77b72010-08-23 14:41:22 -07001736 if (strcmp(ipt_get_target(iter1)->u.user.name,
1737 XT_ERROR_TARGET) == 0)
1738 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001739 }
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001740 if (ret) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001741 /*
1742 * The first i matches need cleanup_entry (calls ->destroy)
1743 * because they had called ->check already. The other j-i
1744 * entries need only release.
1745 */
1746 int skip = i;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001747 j -= i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001748 xt_entry_foreach(iter0, entry0, newinfo->size) {
1749 if (skip-- > 0)
1750 continue;
Jan Engelhardt05595182010-02-24 18:33:43 +01001751 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001752 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001753 compat_release_entry(iter0);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001754 }
Jan Engelhardt05595182010-02-24 18:33:43 +01001755 xt_entry_foreach(iter1, entry1, newinfo->size) {
1756 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001757 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001758 cleanup_entry(iter1, net);
1759 }
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001760 xt_free_table_info(newinfo);
1761 return ret;
1762 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001763
Dmitry Mishin27229712006-04-01 02:25:19 -08001764 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001765 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001766 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1767 memcpy(newinfo->entries[i], entry1, newinfo->size);
1768
1769 *pinfo = newinfo;
1770 *pentry0 = entry1;
1771 xt_free_table_info(info);
1772 return 0;
1773
1774free_newinfo:
1775 xt_free_table_info(newinfo);
1776out:
Jan Engelhardt05595182010-02-24 18:33:43 +01001777 xt_entry_foreach(iter0, entry0, total_size) {
1778 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001779 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001780 compat_release_entry(iter0);
1781 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001782 return ret;
1783out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001784 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001785 xt_compat_unlock(AF_INET);
1786 goto out;
1787}
1788
1789static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001790compat_do_replace(struct net *net, void __user *user, unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001791{
1792 int ret;
1793 struct compat_ipt_replace tmp;
1794 struct xt_table_info *newinfo;
1795 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001796 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001797
1798 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1799 return -EFAULT;
1800
Dmitry Mishin27229712006-04-01 02:25:19 -08001801 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001802 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001803 return -ENOMEM;
1804 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1805 return -ENOMEM;
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01001806 tmp.name[sizeof(tmp.name)-1] = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001807
1808 newinfo = xt_alloc_table_info(tmp.size);
1809 if (!newinfo)
1810 return -ENOMEM;
1811
Patrick McHardy9c547952007-12-17 21:52:00 -08001812 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001813 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1814 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1815 tmp.size) != 0) {
1816 ret = -EFAULT;
1817 goto free_newinfo;
1818 }
1819
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001820 ret = translate_compat_table(net, tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001821 &newinfo, &loc_cpu_entry, tmp.size,
1822 tmp.num_entries, tmp.hook_entry,
1823 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001824 if (ret != 0)
1825 goto free_newinfo;
1826
1827 duprintf("compat_do_replace: Translated table\n");
1828
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001829 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001830 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001831 if (ret)
1832 goto free_newinfo_untrans;
1833 return 0;
1834
1835 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001836 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001837 cleanup_entry(iter, net);
Dmitry Mishin27229712006-04-01 02:25:19 -08001838 free_newinfo:
1839 xt_free_table_info(newinfo);
1840 return ret;
1841}
1842
1843static int
1844compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001845 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001846{
1847 int ret;
1848
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001849 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Dmitry Mishin27229712006-04-01 02:25:19 -08001850 return -EPERM;
1851
1852 switch (cmd) {
1853 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001854 ret = compat_do_replace(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001855 break;
1856
1857 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001858 ret = do_add_counters(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001859 break;
1860
1861 default:
1862 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1863 ret = -EINVAL;
1864 }
1865
1866 return ret;
1867}
1868
Patrick McHardy4b478242007-12-17 21:46:15 -08001869struct compat_ipt_get_entries {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001870 char name[XT_TABLE_MAXNAMELEN];
Dmitry Mishin27229712006-04-01 02:25:19 -08001871 compat_uint_t size;
1872 struct compat_ipt_entry entrytable[0];
1873};
1874
Patrick McHardy4b478242007-12-17 21:46:15 -08001875static int
1876compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1877 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001878{
Dmitry Mishin27229712006-04-01 02:25:19 -08001879 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001880 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001881 void __user *pos;
1882 unsigned int size;
1883 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001884 const void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001885 unsigned int i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001886 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001887
1888 counters = alloc_counters(table);
1889 if (IS_ERR(counters))
1890 return PTR_ERR(counters);
1891
1892 /* choose the copy that is on our node/cpu, ...
1893 * This choice is lazy (because current thread is
1894 * allowed to migrate to another cpu)
1895 */
1896 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1897 pos = userptr;
1898 size = total_size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001899 xt_entry_foreach(iter, loc_cpu_entry, total_size) {
1900 ret = compat_copy_entry_to_user(iter, &pos,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001901 &size, counters, i++);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001902 if (ret != 0)
1903 break;
1904 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001905
Dmitry Mishin27229712006-04-01 02:25:19 -08001906 vfree(counters);
1907 return ret;
1908}
1909
1910static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001911compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1912 int *len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001913{
1914 int ret;
1915 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001916 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001917
Dmitry Mishin27229712006-04-01 02:25:19 -08001918 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001919 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001920 return -EINVAL;
1921 }
1922
1923 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1924 return -EFAULT;
1925
1926 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001927 duprintf("compat_get_entries: %u != %zu\n",
1928 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001929 return -EINVAL;
1930 }
1931
1932 xt_compat_lock(AF_INET);
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001933 t = xt_find_table_lock(net, AF_INET, get.name);
Dmitry Mishin27229712006-04-01 02:25:19 -08001934 if (t && !IS_ERR(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001935 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001936 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08001937 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001938 ret = compat_table_info(private, &info);
1939 if (!ret && get.size == info.size) {
1940 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001941 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001942 } else if (!ret) {
1943 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001944 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001945 ret = -EAGAIN;
Dmitry Mishin27229712006-04-01 02:25:19 -08001946 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001947 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001948 module_put(t->me);
1949 xt_table_unlock(t);
1950 } else
1951 ret = t ? PTR_ERR(t) : -ENOENT;
1952
1953 xt_compat_unlock(AF_INET);
1954 return ret;
1955}
1956
Patrick McHardy79030ed2006-09-20 12:05:08 -07001957static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1958
Dmitry Mishin27229712006-04-01 02:25:19 -08001959static int
1960compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1961{
1962 int ret;
1963
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001964 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Björn Steinbrink82fac052006-10-20 00:21:10 -07001965 return -EPERM;
1966
Dmitry Mishin27229712006-04-01 02:25:19 -08001967 switch (cmd) {
1968 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001969 ret = get_info(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001970 break;
1971 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001972 ret = compat_get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001973 break;
1974 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001975 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001976 }
1977 return ret;
1978}
1979#endif
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001982do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 int ret;
1985
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001986 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return -EPERM;
1988
1989 switch (cmd) {
1990 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001991 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 break;
1993
1994 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001995 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 break;
1997
1998 default:
1999 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
2000 ret = -EINVAL;
2001 }
2002
2003 return ret;
2004}
2005
2006static int
2007do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2008{
2009 int ret;
2010
Eric W. Biederman52e804c2012-11-16 03:03:05 +00002011 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return -EPERM;
2013
2014 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002015 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002016 ret = get_info(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002018
2019 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002020 ret = get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08002021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 case IPT_SO_GET_REVISION_MATCH:
2024 case IPT_SO_GET_REVISION_TARGET: {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02002025 struct xt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002026 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 if (*len != sizeof(rev)) {
2029 ret = -EINVAL;
2030 break;
2031 }
2032 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2033 ret = -EFAULT;
2034 break;
2035 }
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01002036 rev.name[sizeof(rev.name)-1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002039 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002041 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Harald Welte2e4e6a12006-01-12 13:30:04 -08002043 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2044 rev.revision,
2045 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 "ipt_%s", rev.name);
2047 break;
2048 }
2049
2050 default:
2051 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2052 ret = -EINVAL;
2053 }
2054
2055 return ret;
2056}
2057
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02002058struct xt_table *ipt_register_table(struct net *net,
2059 const struct xt_table *table,
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002060 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002063 struct xt_table_info *newinfo;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02002064 struct xt_table_info bootstrap = {0};
Eric Dumazet31836062005-12-13 23:13:48 -08002065 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08002066 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Harald Welte2e4e6a12006-01-12 13:30:04 -08002068 newinfo = xt_alloc_table_info(repl->size);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002069 if (!newinfo) {
2070 ret = -ENOMEM;
2071 goto out;
2072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Patrick McHardy9c547952007-12-17 21:52:00 -08002074 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002075 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2076 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Jan Engelhardt0f234212010-02-24 18:36:04 +01002078 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002079 if (ret != 0)
2080 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002082 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08002083 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002084 ret = PTR_ERR(new_table);
2085 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002088 return new_table;
2089
2090out_free:
2091 xt_free_table_info(newinfo);
2092out:
2093 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Alexey Dobriyanf54e9362010-01-18 08:25:47 +01002096void ipt_unregister_table(struct net *net, struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002098 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002099 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08002100 struct module *table_owner = table->me;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01002101 struct ipt_entry *iter;
Eric Dumazet31836062005-12-13 23:13:48 -08002102
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002103 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002106 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01002107 xt_entry_foreach(iter, loc_cpu_entry, private->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01002108 cleanup_entry(iter, net);
Alexey Dobriyandf200962008-01-31 04:05:34 -08002109 if (private->number > private->initial_entries)
2110 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002111 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
2114/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002115static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2117 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002118 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Patrick McHardy9c547952007-12-17 21:52:00 -08002120 return ((test_type == 0xFF) ||
2121 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 ^ invert;
2123}
2124
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002125static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +02002126icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Jan Engelhardt5452e422008-04-14 11:15:35 +02002128 const struct icmphdr *ic;
2129 struct icmphdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002130 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002133 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002134 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002136 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (ic == NULL) {
2138 /* We've been asked to examine this packet, and we
2139 * can't. Hence, no choice but to drop.
2140 */
2141 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtb4ba2612009-07-07 20:54:30 +02002142 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002143 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
2146 return icmp_type_code_match(icmpinfo->type,
2147 icmpinfo->code[0],
2148 icmpinfo->code[1],
2149 ic->type, ic->code,
2150 !!(icmpinfo->invflags&IPT_ICMP_INV));
2151}
2152
Jan Engelhardtb0f38452010-03-19 17:16:42 +01002153static int icmp_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002155 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002157 /* Must specify no unknown invflags */
Jan Engelhardtbd414ee2010-03-23 16:35:56 +01002158 return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Jan Engelhardt45385062009-07-04 12:50:00 +02002161static struct xt_target ipt_builtin_tg[] __read_mostly = {
2162 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02002163 .name = XT_STANDARD_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02002164 .targetsize = sizeof(int),
2165 .family = NFPROTO_IPV4,
Dmitry Mishin27229712006-04-01 02:25:19 -08002166#ifdef CONFIG_COMPAT
Jan Engelhardt45385062009-07-04 12:50:00 +02002167 .compatsize = sizeof(compat_int_t),
2168 .compat_from_user = compat_standard_from_user,
2169 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002170#endif
Jan Engelhardt45385062009-07-04 12:50:00 +02002171 },
2172 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02002173 .name = XT_ERROR_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02002174 .target = ipt_error,
Jan Engelhardt12b00c22010-10-13 15:56:56 +02002175 .targetsize = XT_FUNCTION_MAXNAMELEN,
Jan Engelhardt45385062009-07-04 12:50:00 +02002176 .family = NFPROTO_IPV4,
2177 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178};
2179
2180static struct nf_sockopt_ops ipt_sockopts = {
2181 .pf = PF_INET,
2182 .set_optmin = IPT_BASE_CTL,
2183 .set_optmax = IPT_SO_SET_MAX+1,
2184 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002185#ifdef CONFIG_COMPAT
2186 .compat_set = compat_do_ipt_set_ctl,
2187#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 .get_optmin = IPT_BASE_CTL,
2189 .get_optmax = IPT_SO_GET_MAX+1,
2190 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002191#ifdef CONFIG_COMPAT
2192 .compat_get = compat_do_ipt_get_ctl,
2193#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002194 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195};
2196
Jan Engelhardt45385062009-07-04 12:50:00 +02002197static struct xt_match ipt_builtin_mt[] __read_mostly = {
2198 {
2199 .name = "icmp",
2200 .match = icmp_match,
2201 .matchsize = sizeof(struct ipt_icmp),
2202 .checkentry = icmp_checkentry,
2203 .proto = IPPROTO_ICMP,
2204 .family = NFPROTO_IPV4,
2205 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206};
2207
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002208static int __net_init ip_tables_net_init(struct net *net)
2209{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02002210 return xt_proto_init(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002211}
2212
2213static void __net_exit ip_tables_net_exit(struct net *net)
2214{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02002215 xt_proto_fini(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002216}
2217
2218static struct pernet_operations ip_tables_net_ops = {
2219 .init = ip_tables_net_init,
2220 .exit = ip_tables_net_exit,
2221};
2222
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002223static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
2225 int ret;
2226
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002227 ret = register_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002228 if (ret < 0)
2229 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002230
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002231 /* No one else will be downing sem now, so we won't sleep */
Jan Engelhardt45385062009-07-04 12:50:00 +02002232 ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002233 if (ret < 0)
2234 goto err2;
Jan Engelhardt45385062009-07-04 12:50:00 +02002235 ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002236 if (ret < 0)
2237 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 /* Register setsockopt */
2240 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002241 if (ret < 0)
2242 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01002244 pr_info("(C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002246
2247err5:
Jan Engelhardt45385062009-07-04 12:50:00 +02002248 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002249err4:
Jan Engelhardt45385062009-07-04 12:50:00 +02002250 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002251err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002252 unregister_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002253err1:
2254 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002257static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
2259 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002260
Jan Engelhardt45385062009-07-04 12:50:00 +02002261 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
2262 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002263 unregister_pernet_subsys(&ip_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265
2266EXPORT_SYMBOL(ipt_register_table);
2267EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002269module_init(ip_tables_init);
2270module_exit(ip_tables_fini);