blob: e7abf5145edc126b60524f0cecf179f7eef75d7c [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>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02006 * Copyright (C) 2006-2010 Patrick McHardy <kaber@trash.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +020012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/cache.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080014#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/skbuff.h>
16#include <linux/kmod.h>
17#include <linux/vmalloc.h>
18#include <linux/netdevice.h>
19#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/icmp.h>
21#include <net/ip.h>
Dmitry Mishin27229712006-04-01 02:25:19 -080022#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -080024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/proc_fs.h>
26#include <linux/err.h>
David S. Millerc8923c62005-10-13 14:41:23 -070027#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Harald Welte2e4e6a12006-01-12 13:30:04 -080029#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/netfilter_ipv4/ip_tables.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080031#include <net/netfilter/nf_log.h>
Jan Engelhardte3eaa992009-06-17 22:14:54 +020032#include "../../netfilter/xt_repldata.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34MODULE_LICENSE("GPL");
35MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
36MODULE_DESCRIPTION("IPv4 packet filter");
37
38/*#define DEBUG_IP_FIREWALL*/
39/*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging */
40/*#define DEBUG_IP_FIREWALL_USER*/
41
42#ifdef DEBUG_IP_FIREWALL
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010043#define dprintf(format, args...) pr_info(format , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#else
45#define dprintf(format, args...)
46#endif
47
48#ifdef DEBUG_IP_FIREWALL_USER
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010049#define duprintf(format, args...) pr_info(format , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#else
51#define duprintf(format, args...)
52#endif
53
54#ifdef CONFIG_NETFILTER_DEBUG
Stephen Hemmingeraf567602010-05-13 15:00:20 +020055#define IP_NF_ASSERT(x) WARN_ON(!(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#else
57#define IP_NF_ASSERT(x)
58#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#if 0
61/* All the better to debug you with... */
62#define static
63#define inline
64#endif
65
Jan Engelhardte3eaa992009-06-17 22:14:54 +020066void *ipt_alloc_initial_table(const struct xt_table *info)
67{
68 return xt_alloc_initial_table(ipt, IPT);
69}
70EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Returns whether matches rule or not. */
Denys Vlasenko022748a2008-01-14 23:44:05 -080073/* Performance critical - called for every packet */
Patrick McHardy9c547952007-12-17 21:52:00 -080074static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070075ip_packet_match(const struct iphdr *ip,
76 const char *indev,
77 const char *outdev,
78 const struct ipt_ip *ipinfo,
79 int isfrag)
80{
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 unsigned long ret;
82
Jan Engelhardte79ec502007-12-17 22:44:06 -080083#define FWINV(bool, invflg) ((bool) ^ !!(ipinfo->invflags & (invflg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
Joe Perches3666ed12009-11-23 23:17:06 +010086 IPT_INV_SRCIP) ||
87 FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
88 IPT_INV_DSTIP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 dprintf("Source or dest mismatch.\n");
90
Harvey Harrisoncffee382008-10-31 00:53:08 -070091 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
92 &ip->saddr, &ipinfo->smsk.s_addr, &ipinfo->src.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
Harvey Harrisoncffee382008-10-31 00:53:08 -070094 dprintf("DST: %pI4 Mask: %pI4 Target: %pI4.%s\n",
95 &ip->daddr, &ipinfo->dmsk.s_addr, &ipinfo->dst.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -080097 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
Eric Dumazetb8dfe492009-03-25 17:31:52 +0100100 ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
103 dprintf("VIA in mismatch (%s vs %s).%s\n",
104 indev, ipinfo->iniface,
105 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800106 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Eric Dumazetb8dfe492009-03-25 17:31:52 +0100109 ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
112 dprintf("VIA out mismatch (%s vs %s).%s\n",
113 outdev, ipinfo->outiface,
114 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800115 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
118 /* Check specific protocol */
Joe Perches3666ed12009-11-23 23:17:06 +0100119 if (ipinfo->proto &&
120 FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 dprintf("Packet protocol %hi does not match %hi.%s\n",
122 ip->protocol, ipinfo->proto,
123 ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
Patrick McHardy9c547952007-12-17 21:52:00 -0800124 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126
127 /* If we have a fragment rule but the packet is not a fragment
128 * then we return zero */
129 if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
130 dprintf("Fragment rule but not fragment.%s\n",
131 ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
Patrick McHardy9c547952007-12-17 21:52:00 -0800132 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134
Patrick McHardy9c547952007-12-17 21:52:00 -0800135 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Denys Vlasenko022748a2008-01-14 23:44:05 -0800138static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139ip_checkentry(const struct ipt_ip *ip)
140{
141 if (ip->flags & ~IPT_F_MASK) {
142 duprintf("Unknown flag bits set: %08X\n",
143 ip->flags & ~IPT_F_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700144 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 if (ip->invflags & ~IPT_INV_MASK) {
147 duprintf("Unknown invflag bits set: %08X\n",
148 ip->invflags & ~IPT_INV_MASK);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700149 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700151 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200155ipt_error(struct sk_buff *skb, const struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Joe Perchese87cc472012-05-13 21:56:26 +0000157 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 return NF_DROP;
160}
161
Denys Vlasenko022748a2008-01-14 23:44:05 -0800162/* Performance critical */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static inline struct ipt_entry *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200164get_entry(const void *base, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 return (struct ipt_entry *)(base + offset);
167}
168
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700169/* All zeroes == unconditional rule. */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800170/* Mildly perf critical (only if packet tracing is on) */
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200171static inline bool unconditional(const struct ipt_ip *ip)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700172{
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200173 static const struct ipt_ip uncond;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700174
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200175 return memcmp(ip, &uncond, sizeof(uncond)) == 0;
Jan Engelhardte79ec502007-12-17 22:44:06 -0800176#undef FWINV
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700177}
178
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200179/* for const-correctness */
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200180static inline const struct xt_entry_target *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200181ipt_get_target_c(const struct ipt_entry *e)
182{
183 return ipt_get_target((struct ipt_entry *)e);
184}
185
Gao fengf0165882013-03-21 19:48:42 +0000186#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
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;
Gao feng30e0c6a2013-03-24 23:50:40 +0000262 struct net *net = dev_net(in ? in : out);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700263
Jan Engelhardtccf5bd82009-04-15 20:27:03 +0200264 table_base = private->entries[smp_processor_id()];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700265 root = get_entry(table_base, private->hook_entry[hook]);
266
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200267 hookname = chainname = hooknames[hook];
268 comment = comments[NF_IP_TRACE_COMMENT_RULE];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700269
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100270 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
271 if (get_chainname_rulenum(iter, e, hookname,
272 &chainname, &comment, &rulenum) != 0)
273 break;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700274
Pablo Neira Ayuso4017a7e2015-03-02 01:10:28 +0100275 nf_log_trace(net, AF_INET, hook, skb, in, out, &trace_loginfo,
276 "TRACE: %s:%s:%s:%u ",
277 tablename, chainname, comment, rulenum);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700278}
279#endif
280
Jan Engelhardt98e86402009-04-15 21:06:05 +0200281static inline __pure
282struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
283{
284 return (void *)entry + entry->next_offset;
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/* Returns one of the generic firewall policies, like NF_ACCEPT. */
288unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700289ipt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 unsigned int hook,
David S. Miller1c491ba2015-04-03 20:56:08 -0400291 const struct nf_hook_state *state,
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);
David S. Miller1c491ba2015-04-03 20:56:08 -0400308 indev = state->in ? state->in->name : nulldevname;
309 outdev = state->out ? state->out->name : nulldevname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /* 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;
David S. Miller1c491ba2015-04-03 20:56:08 -0400319 acpar.in = state->in;
320 acpar.out = state->out;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200321 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();
Will Deaconb416c142013-10-21 13:14:53 +0100329 /*
330 * Ensure we load private-> members after we've fetched the base
331 * pointer.
332 */
333 smp_read_barrier_depends();
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200334 table_base = private->entries[cpu];
335 jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
Eric Dumazet7489aec2010-05-31 16:41:35 +0200336 stackptr = per_cpu_ptr(private->stackptr, cpu);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200337 origptr = *stackptr;
Stephen Hemminger78454472009-02-20 10:35:32 +0100338
Harald Welte2e4e6a12006-01-12 13:30:04 -0800339 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Patrick McHardycecc74d2010-04-22 13:03:24 +0200341 pr_debug("Entering %s(hook %u); sp at %u (UF %p)\n",
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200342 table->name, hook, origptr,
343 get_entry(table_base, private->underflow[hook]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 do {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200346 const struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100347 const struct xt_entry_match *ematch;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 IP_NF_ASSERT(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200350 if (!ip_packet_match(ip, indev, outdev,
Jan Engelhardtde74c162009-07-05 18:26:37 +0200351 &e->ip, acpar.fragoff)) {
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100352 no_match:
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200353 e = ipt_next_entry(e);
354 continue;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jan Engelhardtef53d702009-07-09 19:14:18 +0200357 xt_ematch_foreach(ematch, e) {
Jan Engelhardtde74c162009-07-05 18:26:37 +0200358 acpar.match = ematch->u.kernel.match;
359 acpar.matchinfo = ematch->data;
360 if (!acpar.match->match(skb, &acpar))
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100361 goto no_match;
Jan Engelhardtef53d702009-07-09 19:14:18 +0200362 }
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100363
Changli Gao7df08842010-07-23 16:25:11 +0200364 ADD_COUNTER(e->counters, skb->len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200366 t = ipt_get_target(e);
367 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700368
Gao fengf0165882013-03-21 19:48:42 +0000369#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200370 /* The packet is traced: log it */
371 if (unlikely(skb->nf_trace))
David S. Miller1c491ba2015-04-03 20:56:08 -0400372 trace_packet(skb, hook, state->in, state->out,
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200373 table->name, private, e);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700374#endif
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200375 /* Standard target? */
376 if (!t->u.kernel.target->target) {
377 int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200379 v = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200380 if (v < 0) {
381 /* Pop from stack? */
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200382 if (v != XT_RETURN) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000383 verdict = (unsigned int)(-v) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 break;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200385 }
Eric Dumazetdb856672011-03-20 15:40:06 +0100386 if (*stackptr <= origptr) {
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200387 e = get_entry(table_base,
388 private->underflow[hook]);
Patrick McHardycecc74d2010-04-22 13:03:24 +0200389 pr_debug("Underflow (this is normal) "
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200390 "to %p\n", e);
391 } else {
392 e = jumpstack[--*stackptr];
Patrick McHardycecc74d2010-04-22 13:03:24 +0200393 pr_debug("Pulled %p out from pos %u\n",
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200394 e, *stackptr);
395 e = ipt_next_entry(e);
396 }
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200397 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Joe Perches3666ed12009-11-23 23:17:06 +0100399 if (table_base + v != ipt_next_entry(e) &&
400 !(e->ip.flags & IPT_F_GOTO)) {
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200401 if (*stackptr >= private->stacksize) {
402 verdict = NF_DROP;
403 break;
404 }
405 jumpstack[(*stackptr)++] = e;
Patrick McHardycecc74d2010-04-22 13:03:24 +0200406 pr_debug("Pushed %p into pos %u\n",
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200407 e, *stackptr - 1);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200410 e = get_entry(table_base, v);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200411 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200413
Jan Engelhardtde74c162009-07-05 18:26:37 +0200414 acpar.target = t->u.kernel.target;
415 acpar.targinfo = t->data;
Jan Engelhardtbb70dfa2009-04-15 21:40:13 +0200416
Jan Engelhardtde74c162009-07-05 18:26:37 +0200417 verdict = t->u.kernel.target->target(skb, &acpar);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200418 /* Target might have changed stuff. */
419 ip = ip_hdr(skb);
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200420 if (verdict == XT_CONTINUE)
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200421 e = ipt_next_entry(e);
422 else
423 /* Verdict */
424 break;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200425 } while (!acpar.hotdrop);
Patrick McHardycecc74d2010-04-22 13:03:24 +0200426 pr_debug("Exiting %s; resetting sp from %u to %u\n",
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200427 __func__, *stackptr, origptr);
428 *stackptr = origptr;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200429 xt_write_recseq_end(addend);
430 local_bh_enable();
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432#ifdef DEBUG_ALLOW_ALL
433 return NF_ACCEPT;
434#else
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200435 if (acpar.hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return NF_DROP;
437 else return verdict;
438#endif
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/* Figures out from what hook each rule can be called: returns 0 if
442 there are loops. Puts hook bitmask in comefrom. */
443static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200444mark_source_chains(const struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800445 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 unsigned int hook;
448
449 /* No recursion; use packet counter to save back ptrs (reset
450 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800451 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800453 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 if (!(valid_hooks & (1 << hook)))
456 continue;
457
458 /* Set initial back pointer. */
459 e->counters.pcnt = pos;
460
461 for (;;) {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200462 const struct xt_standard_target *t
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200463 = (void *)ipt_get_target_c(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800464 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800466 if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200467 pr_err("iptables: loop hook %u pos %u %08X.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 hook, pos, e->comefrom);
469 return 0;
470 }
Patrick McHardy9c547952007-12-17 21:52:00 -0800471 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* Unconditional return/END. */
Joe Perches3666ed12009-11-23 23:17:06 +0100474 if ((e->target_offset == sizeof(struct ipt_entry) &&
475 (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200476 XT_STANDARD_TARGET) == 0) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100477 t->verdict < 0 && unconditional(&e->ip)) ||
478 visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 unsigned int oldpos, size;
480
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100481 if ((strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200482 XT_STANDARD_TARGET) == 0) &&
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100483 t->verdict < -NF_MAX_VERDICT - 1) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800484 duprintf("mark_source_chains: bad "
485 "negative verdict (%i)\n",
486 t->verdict);
487 return 0;
488 }
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* Return: backtrack through the last
491 big jump. */
492 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800493 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494#ifdef DEBUG_IP_FIREWALL_USER
495 if (e->comefrom
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800496 & (1 << NF_INET_NUMHOOKS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 duprintf("Back unset "
498 "on hook %u "
499 "rule %u\n",
500 hook, pos);
501 }
502#endif
503 oldpos = pos;
504 pos = e->counters.pcnt;
505 e->counters.pcnt = 0;
506
507 /* We're at the start. */
508 if (pos == oldpos)
509 goto next;
510
511 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800512 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 } while (oldpos == pos + e->next_offset);
514
515 /* Move along one */
516 size = e->next_offset;
517 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800518 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 e->counters.pcnt = pos;
520 pos += size;
521 } else {
522 int newpos = t->verdict;
523
524 if (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200525 XT_STANDARD_TARGET) == 0 &&
Joe Perches3666ed12009-11-23 23:17:06 +0100526 newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800527 if (newpos > newinfo->size -
528 sizeof(struct ipt_entry)) {
529 duprintf("mark_source_chains: "
530 "bad verdict (%i)\n",
531 newpos);
532 return 0;
533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* This a jump; chase it. */
535 duprintf("Jump rule %u -> %u\n",
536 pos, newpos);
537 } else {
538 /* ... this is a fallthru */
539 newpos = pos + e->next_offset;
540 }
541 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800542 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 e->counters.pcnt = pos;
544 pos = newpos;
545 }
546 }
547 next:
548 duprintf("Finished chain %u\n", hook);
549 }
550 return 1;
551}
552
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200553static void cleanup_match(struct xt_entry_match *m, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200555 struct xt_mtdtor_param par;
556
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100557 par.net = net;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200558 par.match = m->u.kernel.match;
559 par.matchinfo = m->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200560 par.family = NFPROTO_IPV4;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200561 if (par.match->destroy != NULL)
562 par.match->destroy(&par);
563 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Denys Vlasenko022748a2008-01-14 23:44:05 -0800566static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200567check_entry(const struct ipt_entry *e, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800568{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200569 const struct xt_entry_target *t;
Dmitry Mishina96be242006-12-12 00:29:26 -0800570
571 if (!ip_checkentry(&e->ip)) {
Sebastian Andrzej Siewior63f6fe92011-06-16 17:16:37 +0200572 duprintf("ip check failed %p %s.\n", e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -0800573 return -EINVAL;
574 }
575
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200576 if (e->target_offset + sizeof(struct xt_entry_target) >
Patrick McHardy9c547952007-12-17 21:52:00 -0800577 e->next_offset)
Dmitry Mishina96be242006-12-12 00:29:26 -0800578 return -EINVAL;
579
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200580 t = ipt_get_target_c(e);
Dmitry Mishina96be242006-12-12 00:29:26 -0800581 if (e->target_offset + t->u.target_size > e->next_offset)
582 return -EINVAL;
583
584 return 0;
585}
586
Denys Vlasenko022748a2008-01-14 23:44:05 -0800587static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200588check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Dmitry Mishina96be242006-12-12 00:29:26 -0800589{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200590 const struct ipt_ip *ip = par->entryinfo;
Dmitry Mishina96be242006-12-12 00:29:26 -0800591 int ret;
592
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200593 par->match = m->u.kernel.match;
594 par->matchinfo = m->data;
595
Jan Engelhardt916a9172008-10-08 11:35:20 +0200596 ret = xt_check_match(par, m->u.match_size - sizeof(*m),
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200597 ip->proto, ip->invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200598 if (ret < 0) {
Jan Engelhardtb5cad0d2010-05-02 13:55:21 +0200599 duprintf("check failed for `%s'.\n", par->match->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200600 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800601 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200602 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800603}
604
Denys Vlasenko022748a2008-01-14 23:44:05 -0800605static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200606find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800608 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800609 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200611 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
612 m->u.user.revision);
613 if (IS_ERR(match)) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800614 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200615 return PTR_ERR(match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 m->u.kernel.match = match;
618
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100619 ret = check_match(m, par);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800620 if (ret)
621 goto err;
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800624err:
625 module_put(m->u.kernel.match->me);
626 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Patrick McHardyadd67462010-02-03 13:45:12 +0100629static int check_target(struct ipt_entry *e, struct net *net, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800630{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200631 struct xt_entry_target *t = ipt_get_target(e);
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200632 struct xt_tgchk_param par = {
Patrick McHardyadd67462010-02-03 13:45:12 +0100633 .net = net,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200634 .table = name,
635 .entryinfo = e,
636 .target = t->u.kernel.target,
637 .targinfo = t->data,
638 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200639 .family = NFPROTO_IPV4,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200640 };
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900641 int ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800642
Jan Engelhardt916a9172008-10-08 11:35:20 +0200643 ret = xt_check_target(&par, t->u.target_size - sizeof(*t),
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200644 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200645 if (ret < 0) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100646 duprintf("check failed for `%s'.\n",
Dmitry Mishina96be242006-12-12 00:29:26 -0800647 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200648 return ret;
Dmitry Mishina96be242006-12-12 00:29:26 -0800649 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200650 return 0;
Dmitry Mishina96be242006-12-12 00:29:26 -0800651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Denys Vlasenko022748a2008-01-14 23:44:05 -0800653static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100654find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
Jan Engelhardt05595182010-02-24 18:33:43 +0100655 unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200657 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800658 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int ret;
660 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200661 struct xt_mtchk_param mtpar;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100662 struct xt_entry_match *ematch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Dmitry Mishina96be242006-12-12 00:29:26 -0800664 ret = check_entry(e, name);
665 if (ret)
666 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 j = 0;
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100669 mtpar.net = net;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200670 mtpar.table = name;
671 mtpar.entryinfo = &e->ip;
672 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200673 mtpar.family = NFPROTO_IPV4;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100674 xt_ematch_foreach(ematch, e) {
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100675 ret = find_check_match(ematch, &mtpar);
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100676 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100677 goto cleanup_matches;
678 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 t = ipt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200682 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
683 t->u.user.revision);
684 if (IS_ERR(target)) {
Dmitry Mishina96be242006-12-12 00:29:26 -0800685 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200686 ret = PTR_ERR(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto cleanup_matches;
688 }
689 t->u.kernel.target = target;
690
Patrick McHardyadd67462010-02-03 13:45:12 +0100691 ret = check_target(e, net, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800692 if (ret)
693 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800695 err:
696 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 cleanup_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100698 xt_ematch_foreach(ematch, e) {
699 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100700 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100701 cleanup_match(ematch, net);
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return ret;
704}
705
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200706static bool check_underflow(const struct ipt_entry *e)
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200707{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200708 const struct xt_entry_target *t;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200709 unsigned int verdict;
710
711 if (!unconditional(&e->ip))
712 return false;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200713 t = ipt_get_target_c(e);
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200714 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
715 return false;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200716 verdict = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200717 verdict = -verdict - 1;
718 return verdict == NF_DROP || verdict == NF_ACCEPT;
719}
720
Denys Vlasenko022748a2008-01-14 23:44:05 -0800721static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800723 struct xt_table_info *newinfo,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200724 const unsigned char *base,
725 const unsigned char *limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 const unsigned int *hook_entries,
727 const unsigned int *underflows,
Jan Engelhardt05595182010-02-24 18:33:43 +0100728 unsigned int valid_hooks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 unsigned int h;
731
Joe Perches3666ed12009-11-23 23:17:06 +0100732 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
733 (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 duprintf("Bad offset %p\n", e);
735 return -EINVAL;
736 }
737
738 if (e->next_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200739 < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 duprintf("checking: element %p size %u\n",
741 e, e->next_offset);
742 return -EINVAL;
743 }
744
745 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800746 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Jan Engelhardta7d51732009-07-18 14:52:58 +0200747 if (!(valid_hooks & (1 << h)))
748 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if ((unsigned char *)e - base == hook_entries[h])
750 newinfo->hook_entry[h] = hook_entries[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200751 if ((unsigned char *)e - base == underflows[h]) {
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200752 if (!check_underflow(e)) {
753 pr_err("Underflows must be unconditional and "
754 "use the STANDARD target with "
755 "ACCEPT/DROP\n");
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200756 return -EINVAL;
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 newinfo->underflow[h] = underflows[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800763 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 e->comefrom = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return 0;
766}
767
Jan Engelhardt05595182010-02-24 18:33:43 +0100768static void
769cleanup_entry(struct ipt_entry *e, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200771 struct xt_tgdtor_param par;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200772 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100773 struct xt_entry_match *ematch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100776 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100777 cleanup_match(ematch, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 t = ipt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200779
Patrick McHardyadd67462010-02-03 13:45:12 +0100780 par.net = net;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200781 par.target = t->u.kernel.target;
782 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200783 par.family = NFPROTO_IPV4;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200784 if (par.target->destroy != NULL)
785 par.target->destroy(&par);
786 module_put(par.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789/* Checks and translates the user-supplied table segment (held in
790 newinfo) */
791static int
Jan Engelhardt0f234212010-02-24 18:36:04 +0100792translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
793 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100795 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 unsigned int i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100797 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Jan Engelhardt0f234212010-02-24 18:36:04 +0100799 newinfo->size = repl->size;
800 newinfo->number = repl->num_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800803 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 newinfo->hook_entry[i] = 0xFFFFFFFF;
805 newinfo->underflow[i] = 0xFFFFFFFF;
806 }
807
808 duprintf("translate_table: size %u\n", newinfo->size);
809 i = 0;
810 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100811 xt_entry_foreach(iter, entry0, newinfo->size) {
812 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +0100813 entry0 + repl->size,
814 repl->hook_entry,
815 repl->underflow,
816 repl->valid_hooks);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100817 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +0100818 return ret;
819 ++i;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200820 if (strcmp(ipt_get_target(iter)->u.user.name,
821 XT_ERROR_TARGET) == 0)
822 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Jan Engelhardt0f234212010-02-24 18:36:04 +0100825 if (i != repl->num_entries) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 duprintf("translate_table: %u not %u entries\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100827 i, repl->num_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -EINVAL;
829 }
830
831 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800832 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* Only hooks which are valid */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100834 if (!(repl->valid_hooks & (1 << i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 continue;
836 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
837 duprintf("Invalid hook entry %u %u\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100838 i, repl->hook_entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -EINVAL;
840 }
841 if (newinfo->underflow[i] == 0xFFFFFFFF) {
842 duprintf("Invalid underflow %u %u\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100843 i, repl->underflow[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EINVAL;
845 }
846 }
847
Jan Engelhardt0f234212010-02-24 18:36:04 +0100848 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0))
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800849 return -ELOOP;
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* Finally, each sanity check must pass */
852 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100853 xt_entry_foreach(iter, entry0, newinfo->size) {
Jan Engelhardt0f234212010-02-24 18:36:04 +0100854 ret = find_check_entry(iter, net, repl->name, repl->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100855 if (ret != 0)
856 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100857 ++i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800860 if (ret != 0) {
Jan Engelhardt05595182010-02-24 18:33:43 +0100861 xt_entry_foreach(iter, entry0, newinfo->size) {
862 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100863 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100864 cleanup_entry(iter, net);
865 }
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800866 return ret;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700870 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800871 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
872 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
875 return ret;
876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800879get_counters(const struct xt_table_info *t,
880 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100882 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 unsigned int cpu;
884 unsigned int i;
885
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700886 for_each_possible_cpu(cpu) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200887 seqcount_t *s = &per_cpu(xt_recseq, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 i = 0;
Jan Engelhardt05595182010-02-24 18:33:43 +0100890 xt_entry_foreach(iter, t->entries[cpu], t->size) {
Eric Dumazet83723d62011-01-10 20:11:38 +0100891 u64 bcnt, pcnt;
892 unsigned int start;
893
894 do {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200895 start = read_seqcount_begin(s);
Eric Dumazet83723d62011-01-10 20:11:38 +0100896 bcnt = iter->counters.bcnt;
897 pcnt = iter->counters.pcnt;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200898 } while (read_seqcount_retry(s, start));
Eric Dumazet83723d62011-01-10 20:11:38 +0100899
900 ADD_COUNTER(counters[i], bcnt, pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +0100901 ++i; /* macro does multi eval of i */
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100904}
905
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200906static struct xt_counters *alloc_counters(const struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Dmitry Mishin27229712006-04-01 02:25:19 -0800908 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800909 struct xt_counters *counters;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200910 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /* We need atomic snapshot of counters: rest doesn't change
913 (other than comefrom, which userspace doesn't care
914 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800915 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet83723d62011-01-10 20:11:38 +0100916 counters = vzalloc(countersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700919 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700921 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Dmitry Mishin27229712006-04-01 02:25:19 -0800923 return counters;
924}
925
926static int
927copy_entries_to_user(unsigned int total_size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200928 const struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800929 void __user *userptr)
930{
931 unsigned int off, num;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200932 const struct ipt_entry *e;
Dmitry Mishin27229712006-04-01 02:25:19 -0800933 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200934 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800935 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200936 const void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -0800937
938 counters = alloc_counters(table);
939 if (IS_ERR(counters))
940 return PTR_ERR(counters);
941
Eric Dumazet31836062005-12-13 23:13:48 -0800942 /* choose the copy that is on our node/cpu, ...
943 * This choice is lazy (because current thread is
944 * allowed to migrate to another cpu)
945 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800946 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800947 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 ret = -EFAULT;
949 goto free_counters;
950 }
951
952 /* FIXME: use iterator macros --RR */
953 /* ... then go back and fix counters and names */
954 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
955 unsigned int i;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200956 const struct xt_entry_match *m;
957 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Eric Dumazet31836062005-12-13 23:13:48 -0800959 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (copy_to_user(userptr + off
961 + offsetof(struct ipt_entry, counters),
962 &counters[num],
963 sizeof(counters[num])) != 0) {
964 ret = -EFAULT;
965 goto free_counters;
966 }
967
968 for (i = sizeof(struct ipt_entry);
969 i < e->target_offset;
970 i += m->u.match_size) {
971 m = (void *)e + i;
972
973 if (copy_to_user(userptr + off + i
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200974 + offsetof(struct xt_entry_match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 u.user.name),
976 m->u.kernel.match->name,
977 strlen(m->u.kernel.match->name)+1)
978 != 0) {
979 ret = -EFAULT;
980 goto free_counters;
981 }
982 }
983
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200984 t = ipt_get_target_c(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (copy_to_user(userptr + off + e->target_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200986 + offsetof(struct xt_entry_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 u.user.name),
988 t->u.kernel.target->name,
989 strlen(t->u.kernel.target->name)+1) != 0) {
990 ret = -EFAULT;
991 goto free_counters;
992 }
993 }
994
995 free_counters:
996 vfree(counters);
997 return ret;
998}
999
Dmitry Mishin27229712006-04-01 02:25:19 -08001000#ifdef CONFIG_COMPAT
Jan Engelhardt739674f2009-06-26 08:23:19 +02001001static void compat_standard_from_user(void *dst, const void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001002{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001003 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001004
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001005 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001006 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001007 memcpy(dst, &v, sizeof(v));
1008}
1009
Jan Engelhardt739674f2009-06-26 08:23:19 +02001010static int compat_standard_to_user(void __user *dst, const void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -08001011{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001012 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -08001013
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001014 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001015 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001016 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001017}
1018
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001019static int compat_calc_entry(const struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001020 const struct xt_table_info *info,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001021 const void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001022{
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001023 const struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001024 const struct xt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001025 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -08001026 int off, i, ret;
1027
Patrick McHardy30c08c42007-12-17 21:47:14 -08001028 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001029 entry_offset = (void *)e - base;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001030 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001031 off += xt_compat_match_offset(ematch->u.kernel.match);
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001032 t = ipt_get_target_c(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001033 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001034 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001035 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001036 if (ret)
1037 return ret;
1038
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001039 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -08001040 if (info->hook_entry[i] &&
1041 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001042 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -08001043 if (info->underflow[i] &&
1044 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -08001045 newinfo->underflow[i] -= off;
1046 }
1047 return 0;
1048}
1049
Eric Dumazet259d4e42007-12-04 23:24:56 -08001050static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -08001051 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -08001052{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001053 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001054 void *loc_cpu_entry;
Jan Engelhardt05595182010-02-24 18:33:43 +01001055 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001056
1057 if (!newinfo || !info)
1058 return -EINVAL;
1059
Eric Dumazet259d4e42007-12-04 23:24:56 -08001060 /* we dont care about newinfo->entries[] */
1061 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1062 newinfo->initial_entries = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001063 loc_cpu_entry = info->entries[raw_smp_processor_id()];
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001064 xt_compat_init_offsets(AF_INET, info->number);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001065 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
1066 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
1067 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001068 return ret;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001069 }
Jan Engelhardt05595182010-02-24 18:33:43 +01001070 return 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001071}
1072#endif
1073
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001074static int get_info(struct net *net, void __user *user,
1075 const int *len, int compat)
Dmitry Mishin27229712006-04-01 02:25:19 -08001076{
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001077 char name[XT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001078 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001079 int ret;
1080
1081 if (*len != sizeof(struct ipt_getinfo)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001082 duprintf("length %u != %zu\n", *len,
1083 sizeof(struct ipt_getinfo));
Dmitry Mishin27229712006-04-01 02:25:19 -08001084 return -EINVAL;
1085 }
1086
1087 if (copy_from_user(name, user, sizeof(name)) != 0)
1088 return -EFAULT;
1089
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001090 name[XT_TABLE_MAXNAMELEN-1] = '\0';
Dmitry Mishin27229712006-04-01 02:25:19 -08001091#ifdef CONFIG_COMPAT
1092 if (compat)
1093 xt_compat_lock(AF_INET);
1094#endif
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001095 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -08001096 "iptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001097 if (!IS_ERR_OR_NULL(t)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001098 struct ipt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001099 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001100#ifdef CONFIG_COMPAT
Alexey Dobriyan14c7dbe2010-02-08 11:17:43 -08001101 struct xt_table_info tmp;
1102
Dmitry Mishin27229712006-04-01 02:25:19 -08001103 if (compat) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001104 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001105 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -08001106 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001107 }
1108#endif
Vasiliy Kulikovb5f15ac2010-11-03 08:45:06 +01001109 memset(&info, 0, sizeof(info));
Dmitry Mishin27229712006-04-01 02:25:19 -08001110 info.valid_hooks = t->valid_hooks;
1111 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001112 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001113 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001114 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001115 info.num_entries = private->number;
1116 info.size = private->size;
1117 strcpy(info.name, name);
1118
1119 if (copy_to_user(user, &info, *len) != 0)
1120 ret = -EFAULT;
1121 else
1122 ret = 0;
1123
1124 xt_table_unlock(t);
1125 module_put(t->me);
1126 } else
1127 ret = t ? PTR_ERR(t) : -ENOENT;
1128#ifdef CONFIG_COMPAT
1129 if (compat)
1130 xt_compat_unlock(AF_INET);
1131#endif
1132 return ret;
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001136get_entries(struct net *net, struct ipt_get_entries __user *uptr,
1137 const int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001140 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001141 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Dmitry Mishin27229712006-04-01 02:25:19 -08001143 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001144 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001145 return -EINVAL;
1146 }
1147 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1148 return -EFAULT;
1149 if (*len != sizeof(struct ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001150 duprintf("get_entries: %u != %zu\n",
1151 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001152 return -EINVAL;
1153 }
1154
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001155 t = xt_find_table_lock(net, AF_INET, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001156 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001157 const struct xt_table_info *private = t->private;
Patrick McHardy9c547952007-12-17 21:52:00 -08001158 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001159 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001160 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 t, uptr->entrytable);
1162 else {
1163 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001164 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001165 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001168 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 } else
1170 ret = t ? PTR_ERR(t) : -ENOENT;
1171
1172 return ret;
1173}
1174
1175static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001176__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001177 struct xt_table_info *newinfo, unsigned int num_counters,
1178 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001179{
1180 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001181 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001182 struct xt_table_info *oldinfo;
1183 struct xt_counters *counters;
1184 void *loc_cpu_old_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001185 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001186
1187 ret = 0;
Eric Dumazet83723d62011-01-10 20:11:38 +01001188 counters = vzalloc(num_counters * sizeof(struct xt_counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001189 if (!counters) {
1190 ret = -ENOMEM;
1191 goto out;
1192 }
1193
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001194 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Dmitry Mishin27229712006-04-01 02:25:19 -08001195 "iptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001196 if (IS_ERR_OR_NULL(t)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001197 ret = t ? PTR_ERR(t) : -ENOENT;
1198 goto free_newinfo_counters_untrans;
1199 }
1200
1201 /* You lied! */
1202 if (valid_hooks != t->valid_hooks) {
1203 duprintf("Valid hook crap: %08X vs %08X\n",
1204 valid_hooks, t->valid_hooks);
1205 ret = -EINVAL;
1206 goto put_module;
1207 }
1208
1209 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1210 if (!oldinfo)
1211 goto put_module;
1212
1213 /* Update module usage count based on number of rules */
1214 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1215 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1216 if ((oldinfo->number > oldinfo->initial_entries) ||
1217 (newinfo->number <= oldinfo->initial_entries))
1218 module_put(t->me);
1219 if ((oldinfo->number > oldinfo->initial_entries) &&
1220 (newinfo->number <= oldinfo->initial_entries))
1221 module_put(t->me);
1222
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001223 /* Get the old counters, and synchronize with replace */
Dmitry Mishin27229712006-04-01 02:25:19 -08001224 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001225
Dmitry Mishin27229712006-04-01 02:25:19 -08001226 /* Decrease module usage counts and free resource */
1227 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001228 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001229 cleanup_entry(iter, net);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001230
Dmitry Mishin27229712006-04-01 02:25:19 -08001231 xt_free_table_info(oldinfo);
1232 if (copy_to_user(counters_ptr, counters,
Thomas Grafc58dd2d2014-04-04 17:57:45 +02001233 sizeof(struct xt_counters) * num_counters) != 0) {
1234 /* Silent error, can't fail, new table is already in place */
1235 net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
1236 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001237 vfree(counters);
1238 xt_table_unlock(t);
1239 return ret;
1240
1241 put_module:
1242 module_put(t->me);
1243 xt_table_unlock(t);
1244 free_newinfo_counters_untrans:
1245 vfree(counters);
1246 out:
1247 return ret;
1248}
1249
1250static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001251do_replace(struct net *net, const void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 int ret;
1254 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001255 struct xt_table_info *newinfo;
1256 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001257 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1260 return -EFAULT;
1261
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001262 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001263 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1264 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001265 if (tmp.num_counters == 0)
1266 return -EINVAL;
1267
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01001268 tmp.name[sizeof(tmp.name)-1] = 0;
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001269
Harald Welte2e4e6a12006-01-12 13:30:04 -08001270 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (!newinfo)
1272 return -ENOMEM;
1273
Patrick McHardy9c547952007-12-17 21:52:00 -08001274 /* choose the copy that is on our node/cpu */
Eric Dumazet31836062005-12-13 23:13:48 -08001275 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1276 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 tmp.size) != 0) {
1278 ret = -EFAULT;
1279 goto free_newinfo;
1280 }
1281
Jan Engelhardt0f234212010-02-24 18:36:04 +01001282 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001284 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01001286 duprintf("Translated table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001288 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001289 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001290 if (ret)
1291 goto free_newinfo_untrans;
1292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Dmitry Mishin27229712006-04-01 02:25:19 -08001294 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001295 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001296 cleanup_entry(iter, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001298 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return ret;
1300}
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001303do_add_counters(struct net *net, const void __user *user,
1304 unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001306 unsigned int i, curcpu;
Dmitry Mishin27229712006-04-01 02:25:19 -08001307 struct xt_counters_info tmp;
1308 struct xt_counters *paddc;
1309 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001310 const char *name;
Dmitry Mishin27229712006-04-01 02:25:19 -08001311 int size;
1312 void *ptmp;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001313 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001314 const struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001316 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001317 struct ipt_entry *iter;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001318 unsigned int addend;
Dmitry Mishin27229712006-04-01 02:25:19 -08001319#ifdef CONFIG_COMPAT
1320 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Dmitry Mishin27229712006-04-01 02:25:19 -08001322 if (compat) {
1323 ptmp = &compat_tmp;
1324 size = sizeof(struct compat_xt_counters_info);
1325 } else
1326#endif
1327 {
1328 ptmp = &tmp;
1329 size = sizeof(struct xt_counters_info);
1330 }
1331
1332 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return -EFAULT;
1334
Dmitry Mishin27229712006-04-01 02:25:19 -08001335#ifdef CONFIG_COMPAT
1336 if (compat) {
1337 num_counters = compat_tmp.num_counters;
1338 name = compat_tmp.name;
1339 } else
1340#endif
1341 {
1342 num_counters = tmp.num_counters;
1343 name = tmp.name;
1344 }
1345
1346 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -EINVAL;
1348
Eric Dumazete12f8e22010-06-04 13:31:29 +02001349 paddc = vmalloc(len - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (!paddc)
1351 return -ENOMEM;
1352
Dmitry Mishin27229712006-04-01 02:25:19 -08001353 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 ret = -EFAULT;
1355 goto free;
1356 }
1357
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001358 t = xt_find_table_lock(net, AF_INET, name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001359 if (IS_ERR_OR_NULL(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 ret = t ? PTR_ERR(t) : -ENOENT;
1361 goto free;
1362 }
1363
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001364 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001365 private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001366 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 ret = -EINVAL;
1368 goto unlock_up_free;
1369 }
1370
1371 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001372 /* Choose the copy that is on our node */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001373 curcpu = smp_processor_id();
1374 loc_cpu_entry = private->entries[curcpu];
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001375 addend = xt_write_recseq_begin();
Jan Engelhardt05595182010-02-24 18:33:43 +01001376 xt_entry_foreach(iter, loc_cpu_entry, private->size) {
1377 ADD_COUNTER(iter->counters, paddc[i].bcnt, paddc[i].pcnt);
1378 ++i;
1379 }
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001380 xt_write_recseq_end(addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001382 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001383 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 module_put(t->me);
1385 free:
1386 vfree(paddc);
1387
1388 return ret;
1389}
1390
Dmitry Mishin27229712006-04-01 02:25:19 -08001391#ifdef CONFIG_COMPAT
1392struct compat_ipt_replace {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001393 char name[XT_TABLE_MAXNAMELEN];
Dmitry Mishin27229712006-04-01 02:25:19 -08001394 u32 valid_hooks;
1395 u32 num_entries;
1396 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001397 u32 hook_entry[NF_INET_NUMHOOKS];
1398 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001399 u32 num_counters;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001400 compat_uptr_t counters; /* struct xt_counters * */
Dmitry Mishin27229712006-04-01 02:25:19 -08001401 struct compat_ipt_entry entries[0];
1402};
1403
Patrick McHardya18aa312007-12-12 10:35:16 -08001404static int
1405compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001406 unsigned int *size, struct xt_counters *counters,
Jan Engelhardt05595182010-02-24 18:33:43 +01001407 unsigned int i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001408{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001409 struct xt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001410 struct compat_ipt_entry __user *ce;
1411 u_int16_t target_offset, next_offset;
1412 compat_uint_t origsize;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001413 const struct xt_entry_match *ematch;
1414 int ret = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001415
Dmitry Mishin27229712006-04-01 02:25:19 -08001416 origsize = *size;
1417 ce = (struct compat_ipt_entry __user *)*dstptr;
Jan Engelhardt05595182010-02-24 18:33:43 +01001418 if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
1419 copy_to_user(&ce->counters, &counters[i],
1420 sizeof(counters[i])) != 0)
1421 return -EFAULT;
Patrick McHardya18aa312007-12-12 10:35:16 -08001422
Dmitry Mishin27229712006-04-01 02:25:19 -08001423 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001424 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1425
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001426 xt_ematch_foreach(ematch, e) {
1427 ret = xt_compat_match_to_user(ematch, dstptr, size);
1428 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001429 return ret;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001430 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001431 target_offset = e->target_offset - (origsize - *size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001432 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001433 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001434 if (ret)
Jan Engelhardt05595182010-02-24 18:33:43 +01001435 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001436 next_offset = e->next_offset - (origsize - *size);
Jan Engelhardt05595182010-02-24 18:33:43 +01001437 if (put_user(target_offset, &ce->target_offset) != 0 ||
1438 put_user(next_offset, &ce->next_offset) != 0)
1439 return -EFAULT;
Dmitry Mishin27229712006-04-01 02:25:19 -08001440 return 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001441}
1442
Denys Vlasenko022748a2008-01-14 23:44:05 -08001443static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001444compat_find_calc_match(struct xt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001445 const char *name,
1446 const struct ipt_ip *ip,
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001447 int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001448{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001449 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001450
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001451 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
1452 m->u.user.revision);
1453 if (IS_ERR(match)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001454 duprintf("compat_check_calc_match: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001455 m->u.user.name);
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001456 return PTR_ERR(match);
Dmitry Mishin27229712006-04-01 02:25:19 -08001457 }
1458 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001459 *size += xt_compat_match_offset(match);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001460 return 0;
1461}
1462
Jan Engelhardt05595182010-02-24 18:33:43 +01001463static void compat_release_entry(struct compat_ipt_entry *e)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001464{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001465 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001466 struct xt_entry_match *ematch;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001467
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001468 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001469 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001470 module_put(ematch->u.kernel.match->me);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001471 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001472 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001473}
1474
Denys Vlasenko022748a2008-01-14 23:44:05 -08001475static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001476check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001477 struct xt_table_info *newinfo,
1478 unsigned int *size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001479 const unsigned char *base,
1480 const unsigned char *limit,
1481 const unsigned int *hook_entries,
1482 const unsigned int *underflows,
Patrick McHardy4b478242007-12-17 21:46:15 -08001483 const char *name)
Dmitry Mishin27229712006-04-01 02:25:19 -08001484{
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001485 struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001486 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001487 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001488 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001489 unsigned int j;
1490 int ret, off, h;
Dmitry Mishin27229712006-04-01 02:25:19 -08001491
1492 duprintf("check_compat_entry_size_and_hooks %p\n", e);
Joe Perches3666ed12009-11-23 23:17:06 +01001493 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
1494 (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001495 duprintf("Bad offset %p, limit = %p\n", e, limit);
1496 return -EINVAL;
1497 }
1498
1499 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Patrick McHardy4b478242007-12-17 21:46:15 -08001500 sizeof(struct compat_xt_entry_target)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001501 duprintf("checking: element %p size %u\n",
1502 e, e->next_offset);
1503 return -EINVAL;
1504 }
1505
Patrick McHardy73cd5982007-12-17 21:47:32 -08001506 /* For purposes of check_entry casting the compat entry is fine */
1507 ret = check_entry((struct ipt_entry *)e, name);
Dmitry Mishina96be242006-12-12 00:29:26 -08001508 if (ret)
1509 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001510
Patrick McHardy30c08c42007-12-17 21:47:14 -08001511 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001512 entry_offset = (void *)e - (void *)base;
1513 j = 0;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001514 xt_ematch_foreach(ematch, e) {
Florian Westphal2f065502015-05-24 01:00:41 +02001515 ret = compat_find_calc_match(ematch, name, &e->ip, &off);
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001516 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001517 goto release_matches;
1518 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001519 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001520
Patrick McHardy73cd5982007-12-17 21:47:32 -08001521 t = compat_ipt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001522 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
1523 t->u.user.revision);
1524 if (IS_ERR(target)) {
Dmitry Mishina96be242006-12-12 00:29:26 -08001525 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
Patrick McHardy4b478242007-12-17 21:46:15 -08001526 t->u.user.name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001527 ret = PTR_ERR(target);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001528 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001529 }
1530 t->u.kernel.target = target;
1531
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001532 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001533 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001534 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001535 if (ret)
1536 goto out;
1537
1538 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001539 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001540 if ((unsigned char *)e - base == hook_entries[h])
1541 newinfo->hook_entry[h] = hook_entries[h];
1542 if ((unsigned char *)e - base == underflows[h])
1543 newinfo->underflow[h] = underflows[h];
1544 }
1545
1546 /* Clear counters and comefrom */
Patrick McHardy73cd5982007-12-17 21:47:32 -08001547 memset(&e->counters, 0, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001548 e->comefrom = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001549 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001550
Dmitry Mishin27229712006-04-01 02:25:19 -08001551out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001552 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001553release_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001554 xt_ematch_foreach(ematch, e) {
1555 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001556 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001557 module_put(ematch->u.kernel.match->me);
1558 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001559 return ret;
1560}
1561
Patrick McHardy4b478242007-12-17 21:46:15 -08001562static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001563compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Patrick McHardy4b478242007-12-17 21:46:15 -08001564 unsigned int *size, const char *name,
1565 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001566{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001567 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001568 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001569 struct ipt_entry *de;
1570 unsigned int origsize;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001571 int ret, h;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001572 struct xt_entry_match *ematch;
Dmitry Mishin27229712006-04-01 02:25:19 -08001573
1574 ret = 0;
1575 origsize = *size;
1576 de = (struct ipt_entry *)*dstptr;
1577 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001578 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001579
Patrick McHardy73cd5982007-12-17 21:47:32 -08001580 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001581 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1582
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001583 xt_ematch_foreach(ematch, e) {
1584 ret = xt_compat_match_from_user(ematch, dstptr, size);
1585 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001586 return ret;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001587 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001588 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001589 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001590 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001591 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001592
1593 de->next_offset = e->next_offset - (origsize - *size);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001594 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001595 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1596 newinfo->hook_entry[h] -= origsize - *size;
1597 if ((unsigned char *)de - base < newinfo->underflow[h])
1598 newinfo->underflow[h] -= origsize - *size;
1599 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001600 return ret;
1601}
Dmitry Mishin27229712006-04-01 02:25:19 -08001602
Denys Vlasenko022748a2008-01-14 23:44:05 -08001603static int
Jan Engelhardt05595182010-02-24 18:33:43 +01001604compat_check_entry(struct ipt_entry *e, struct net *net, const char *name)
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001605{
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001606 struct xt_entry_match *ematch;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001607 struct xt_mtchk_param mtpar;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001608 unsigned int j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001609 int ret = 0;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001610
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001611 j = 0;
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001612 mtpar.net = net;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001613 mtpar.table = name;
1614 mtpar.entryinfo = &e->ip;
1615 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +02001616 mtpar.family = NFPROTO_IPV4;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001617 xt_ematch_foreach(ematch, e) {
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001618 ret = check_match(ematch, &mtpar);
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001619 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001620 goto cleanup_matches;
1621 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001622 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001623
Patrick McHardyadd67462010-02-03 13:45:12 +01001624 ret = check_target(e, net, name);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001625 if (ret)
1626 goto cleanup_matches;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001627 return 0;
1628
1629 cleanup_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001630 xt_ematch_foreach(ematch, e) {
1631 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001632 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001633 cleanup_match(ematch, net);
1634 }
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001635 return ret;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001636}
1637
Dmitry Mishin27229712006-04-01 02:25:19 -08001638static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001639translate_compat_table(struct net *net,
1640 const char *name,
Patrick McHardy4b478242007-12-17 21:46:15 -08001641 unsigned int valid_hooks,
1642 struct xt_table_info **pinfo,
1643 void **pentry0,
1644 unsigned int total_size,
1645 unsigned int number,
1646 unsigned int *hook_entries,
1647 unsigned int *underflows)
Dmitry Mishin27229712006-04-01 02:25:19 -08001648{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001649 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001650 struct xt_table_info *newinfo, *info;
1651 void *pos, *entry0, *entry1;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001652 struct compat_ipt_entry *iter0;
1653 struct ipt_entry *iter1;
Dmitry Mishin27229712006-04-01 02:25:19 -08001654 unsigned int size;
Jan Engelhardt05595182010-02-24 18:33:43 +01001655 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001656
1657 info = *pinfo;
1658 entry0 = *pentry0;
1659 size = total_size;
1660 info->number = number;
1661
1662 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001663 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001664 info->hook_entry[i] = 0xFFFFFFFF;
1665 info->underflow[i] = 0xFFFFFFFF;
1666 }
1667
1668 duprintf("translate_compat_table: size %u\n", info->size);
Dmitry Mishin920b8682006-10-30 15:14:27 -08001669 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001670 xt_compat_lock(AF_INET);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001671 xt_compat_init_offsets(AF_INET, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001672 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001673 xt_entry_foreach(iter0, entry0, total_size) {
1674 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001675 entry0,
1676 entry0 + total_size,
1677 hook_entries,
1678 underflows,
1679 name);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001680 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001681 goto out_unlock;
1682 ++j;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001683 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001684
1685 ret = -EINVAL;
Dmitry Mishin920b8682006-10-30 15:14:27 -08001686 if (j != number) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001687 duprintf("translate_compat_table: %u not %u entries\n",
Dmitry Mishin920b8682006-10-30 15:14:27 -08001688 j, number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001689 goto out_unlock;
1690 }
1691
1692 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001693 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001694 /* Only hooks which are valid */
1695 if (!(valid_hooks & (1 << i)))
1696 continue;
1697 if (info->hook_entry[i] == 0xFFFFFFFF) {
1698 duprintf("Invalid hook entry %u %u\n",
1699 i, hook_entries[i]);
1700 goto out_unlock;
1701 }
1702 if (info->underflow[i] == 0xFFFFFFFF) {
1703 duprintf("Invalid underflow %u %u\n",
1704 i, underflows[i]);
1705 goto out_unlock;
1706 }
1707 }
1708
1709 ret = -ENOMEM;
1710 newinfo = xt_alloc_table_info(size);
1711 if (!newinfo)
1712 goto out_unlock;
1713
1714 newinfo->number = number;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001715 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001716 newinfo->hook_entry[i] = info->hook_entry[i];
1717 newinfo->underflow[i] = info->underflow[i];
1718 }
1719 entry1 = newinfo->entries[raw_smp_processor_id()];
1720 pos = entry1;
Patrick McHardy4b478242007-12-17 21:46:15 -08001721 size = total_size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001722 xt_entry_foreach(iter0, entry0, total_size) {
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001723 ret = compat_copy_entry_from_user(iter0, &pos, &size,
1724 name, newinfo, entry1);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001725 if (ret != 0)
1726 break;
1727 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001728 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001729 xt_compat_unlock(AF_INET);
1730 if (ret)
1731 goto free_newinfo;
1732
1733 ret = -ELOOP;
1734 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1735 goto free_newinfo;
1736
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001737 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001738 xt_entry_foreach(iter1, entry1, newinfo->size) {
Jan Engelhardt05595182010-02-24 18:33:43 +01001739 ret = compat_check_entry(iter1, net, name);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001740 if (ret != 0)
1741 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001742 ++i;
Florian Westphalcca77b72010-08-23 14:41:22 -07001743 if (strcmp(ipt_get_target(iter1)->u.user.name,
1744 XT_ERROR_TARGET) == 0)
1745 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001746 }
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001747 if (ret) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001748 /*
1749 * The first i matches need cleanup_entry (calls ->destroy)
1750 * because they had called ->check already. The other j-i
1751 * entries need only release.
1752 */
1753 int skip = i;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001754 j -= i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001755 xt_entry_foreach(iter0, entry0, newinfo->size) {
1756 if (skip-- > 0)
1757 continue;
Jan Engelhardt05595182010-02-24 18:33:43 +01001758 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001759 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001760 compat_release_entry(iter0);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001761 }
Jan Engelhardt05595182010-02-24 18:33:43 +01001762 xt_entry_foreach(iter1, entry1, newinfo->size) {
1763 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001764 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001765 cleanup_entry(iter1, net);
1766 }
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001767 xt_free_table_info(newinfo);
1768 return ret;
1769 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001770
Dmitry Mishin27229712006-04-01 02:25:19 -08001771 /* And one copy for every other CPU */
Andrew Mortonfb1bb342006-06-25 05:46:43 -07001772 for_each_possible_cpu(i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001773 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1774 memcpy(newinfo->entries[i], entry1, newinfo->size);
1775
1776 *pinfo = newinfo;
1777 *pentry0 = entry1;
1778 xt_free_table_info(info);
1779 return 0;
1780
1781free_newinfo:
1782 xt_free_table_info(newinfo);
1783out:
Jan Engelhardt05595182010-02-24 18:33:43 +01001784 xt_entry_foreach(iter0, entry0, total_size) {
1785 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001786 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001787 compat_release_entry(iter0);
1788 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001789 return ret;
1790out_unlock:
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001791 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001792 xt_compat_unlock(AF_INET);
1793 goto out;
1794}
1795
1796static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001797compat_do_replace(struct net *net, void __user *user, unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001798{
1799 int ret;
1800 struct compat_ipt_replace tmp;
1801 struct xt_table_info *newinfo;
1802 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001803 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001804
1805 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1806 return -EFAULT;
1807
Dmitry Mishin27229712006-04-01 02:25:19 -08001808 /* overflow check */
Eric Dumazet259d4e42007-12-04 23:24:56 -08001809 if (tmp.size >= INT_MAX / num_possible_cpus())
Dmitry Mishin27229712006-04-01 02:25:19 -08001810 return -ENOMEM;
1811 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1812 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001813 if (tmp.num_counters == 0)
1814 return -EINVAL;
1815
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01001816 tmp.name[sizeof(tmp.name)-1] = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001817
1818 newinfo = xt_alloc_table_info(tmp.size);
1819 if (!newinfo)
1820 return -ENOMEM;
1821
Patrick McHardy9c547952007-12-17 21:52:00 -08001822 /* choose the copy that is on our node/cpu */
Dmitry Mishin27229712006-04-01 02:25:19 -08001823 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1824 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1825 tmp.size) != 0) {
1826 ret = -EFAULT;
1827 goto free_newinfo;
1828 }
1829
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001830 ret = translate_compat_table(net, tmp.name, tmp.valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001831 &newinfo, &loc_cpu_entry, tmp.size,
1832 tmp.num_entries, tmp.hook_entry,
1833 tmp.underflow);
Dmitry Mishin27229712006-04-01 02:25:19 -08001834 if (ret != 0)
1835 goto free_newinfo;
1836
1837 duprintf("compat_do_replace: Translated table\n");
1838
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001839 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001840 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001841 if (ret)
1842 goto free_newinfo_untrans;
1843 return 0;
1844
1845 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001846 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001847 cleanup_entry(iter, net);
Dmitry Mishin27229712006-04-01 02:25:19 -08001848 free_newinfo:
1849 xt_free_table_info(newinfo);
1850 return ret;
1851}
1852
1853static int
1854compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001855 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001856{
1857 int ret;
1858
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001859 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Dmitry Mishin27229712006-04-01 02:25:19 -08001860 return -EPERM;
1861
1862 switch (cmd) {
1863 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001864 ret = compat_do_replace(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001865 break;
1866
1867 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001868 ret = do_add_counters(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001869 break;
1870
1871 default:
1872 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
1873 ret = -EINVAL;
1874 }
1875
1876 return ret;
1877}
1878
Patrick McHardy4b478242007-12-17 21:46:15 -08001879struct compat_ipt_get_entries {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001880 char name[XT_TABLE_MAXNAMELEN];
Dmitry Mishin27229712006-04-01 02:25:19 -08001881 compat_uint_t size;
1882 struct compat_ipt_entry entrytable[0];
1883};
1884
Patrick McHardy4b478242007-12-17 21:46:15 -08001885static int
1886compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1887 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001888{
Dmitry Mishin27229712006-04-01 02:25:19 -08001889 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001890 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001891 void __user *pos;
1892 unsigned int size;
1893 int ret = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001894 const void *loc_cpu_entry;
Patrick McHardya18aa312007-12-12 10:35:16 -08001895 unsigned int i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001896 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001897
1898 counters = alloc_counters(table);
1899 if (IS_ERR(counters))
1900 return PTR_ERR(counters);
1901
1902 /* choose the copy that is on our node/cpu, ...
1903 * This choice is lazy (because current thread is
1904 * allowed to migrate to another cpu)
1905 */
1906 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1907 pos = userptr;
1908 size = total_size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001909 xt_entry_foreach(iter, loc_cpu_entry, total_size) {
1910 ret = compat_copy_entry_to_user(iter, &pos,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001911 &size, counters, i++);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001912 if (ret != 0)
1913 break;
1914 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001915
Dmitry Mishin27229712006-04-01 02:25:19 -08001916 vfree(counters);
1917 return ret;
1918}
1919
1920static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001921compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1922 int *len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001923{
1924 int ret;
1925 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001926 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001927
Dmitry Mishin27229712006-04-01 02:25:19 -08001928 if (*len < sizeof(get)) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001929 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
Dmitry Mishin27229712006-04-01 02:25:19 -08001930 return -EINVAL;
1931 }
1932
1933 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1934 return -EFAULT;
1935
1936 if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
Patrick McHardyc9d8fe12007-12-17 21:52:15 -08001937 duprintf("compat_get_entries: %u != %zu\n",
1938 *len, sizeof(get) + get.size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001939 return -EINVAL;
1940 }
1941
1942 xt_compat_lock(AF_INET);
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001943 t = xt_find_table_lock(net, AF_INET, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001944 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001945 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001946 struct xt_table_info info;
Patrick McHardy9c547952007-12-17 21:52:00 -08001947 duprintf("t->private->number = %u\n", private->number);
Dmitry Mishin27229712006-04-01 02:25:19 -08001948 ret = compat_table_info(private, &info);
1949 if (!ret && get.size == info.size) {
1950 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001951 t, uptr->entrytable);
Dmitry Mishin27229712006-04-01 02:25:19 -08001952 } else if (!ret) {
1953 duprintf("compat_get_entries: I've got %u not %u!\n",
Patrick McHardy9c547952007-12-17 21:52:00 -08001954 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001955 ret = -EAGAIN;
Dmitry Mishin27229712006-04-01 02:25:19 -08001956 }
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001957 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001958 module_put(t->me);
1959 xt_table_unlock(t);
1960 } else
1961 ret = t ? PTR_ERR(t) : -ENOENT;
1962
1963 xt_compat_unlock(AF_INET);
1964 return ret;
1965}
1966
Patrick McHardy79030ed2006-09-20 12:05:08 -07001967static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1968
Dmitry Mishin27229712006-04-01 02:25:19 -08001969static int
1970compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1971{
1972 int ret;
1973
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001974 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Björn Steinbrink82fac052006-10-20 00:21:10 -07001975 return -EPERM;
1976
Dmitry Mishin27229712006-04-01 02:25:19 -08001977 switch (cmd) {
1978 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001979 ret = get_info(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001980 break;
1981 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001982 ret = compat_get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001983 break;
1984 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001985 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001986 }
1987 return ret;
1988}
1989#endif
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001992do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 int ret;
1995
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001996 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return -EPERM;
1998
1999 switch (cmd) {
2000 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002001 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 break;
2003
2004 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002005 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 break;
2007
2008 default:
2009 duprintf("do_ipt_set_ctl: unknown request %i\n", cmd);
2010 ret = -EINVAL;
2011 }
2012
2013 return ret;
2014}
2015
2016static int
2017do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2018{
2019 int ret;
2020
Eric W. Biederman52e804c2012-11-16 03:03:05 +00002021 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return -EPERM;
2023
2024 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08002025 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002026 ret = get_info(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08002028
2029 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002030 ret = get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08002031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 case IPT_SO_GET_REVISION_MATCH:
2034 case IPT_SO_GET_REVISION_TARGET: {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02002035 struct xt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002036 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 if (*len != sizeof(rev)) {
2039 ret = -EINVAL;
2040 break;
2041 }
2042 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
2043 ret = -EFAULT;
2044 break;
2045 }
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01002046 rev.name[sizeof(rev.name)-1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08002049 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08002051 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Harald Welte2e4e6a12006-01-12 13:30:04 -08002053 try_then_request_module(xt_find_revision(AF_INET, rev.name,
2054 rev.revision,
2055 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 "ipt_%s", rev.name);
2057 break;
2058 }
2059
2060 default:
2061 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
2062 ret = -EINVAL;
2063 }
2064
2065 return ret;
2066}
2067
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02002068struct xt_table *ipt_register_table(struct net *net,
2069 const struct xt_table *table,
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002070 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
2072 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002073 struct xt_table_info *newinfo;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02002074 struct xt_table_info bootstrap = {0};
Eric Dumazet31836062005-12-13 23:13:48 -08002075 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08002076 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Harald Welte2e4e6a12006-01-12 13:30:04 -08002078 newinfo = xt_alloc_table_info(repl->size);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002079 if (!newinfo) {
2080 ret = -ENOMEM;
2081 goto out;
2082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Patrick McHardy9c547952007-12-17 21:52:00 -08002084 /* choose the copy on our node/cpu, but dont care about preemption */
Eric Dumazet31836062005-12-13 23:13:48 -08002085 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
2086 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Jan Engelhardt0f234212010-02-24 18:36:04 +01002088 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002089 if (ret != 0)
2090 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002092 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08002093 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002094 ret = PTR_ERR(new_table);
2095 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08002098 return new_table;
2099
2100out_free:
2101 xt_free_table_info(newinfo);
2102out:
2103 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
Alexey Dobriyanf54e9362010-01-18 08:25:47 +01002106void ipt_unregister_table(struct net *net, struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Harald Welte2e4e6a12006-01-12 13:30:04 -08002108 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08002109 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08002110 struct module *table_owner = table->me;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01002111 struct ipt_entry *iter;
Eric Dumazet31836062005-12-13 23:13:48 -08002112
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002113 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08002116 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01002117 xt_entry_foreach(iter, loc_cpu_entry, private->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01002118 cleanup_entry(iter, net);
Alexey Dobriyandf200962008-01-31 04:05:34 -08002119 if (private->number > private->initial_entries)
2120 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002121 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
2124/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002125static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
2127 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002128 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Patrick McHardy9c547952007-12-17 21:52:00 -08002130 return ((test_type == 0xFF) ||
2131 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 ^ invert;
2133}
2134
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002135static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +02002136icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Jan Engelhardt5452e422008-04-14 11:15:35 +02002138 const struct icmphdr *ic;
2139 struct icmphdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002140 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002143 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002144 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Jan Engelhardtf7108a22008-10-08 11:35:18 +02002146 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (ic == NULL) {
2148 /* We've been asked to examine this packet, and we
2149 * can't. Hence, no choice but to drop.
2150 */
2151 duprintf("Dropping evil ICMP tinygram.\n");
Jan Engelhardtb4ba2612009-07-07 20:54:30 +02002152 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07002153 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155
2156 return icmp_type_code_match(icmpinfo->type,
2157 icmpinfo->code[0],
2158 icmpinfo->code[1],
2159 ic->type, ic->code,
2160 !!(icmpinfo->invflags&IPT_ICMP_INV));
2161}
2162
Jan Engelhardtb0f38452010-03-19 17:16:42 +01002163static int icmp_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02002165 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Patrick McHardy1d5cd902006-03-20 18:01:14 -08002167 /* Must specify no unknown invflags */
Jan Engelhardtbd414ee2010-03-23 16:35:56 +01002168 return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
Jan Engelhardt45385062009-07-04 12:50:00 +02002171static struct xt_target ipt_builtin_tg[] __read_mostly = {
2172 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02002173 .name = XT_STANDARD_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02002174 .targetsize = sizeof(int),
2175 .family = NFPROTO_IPV4,
Dmitry Mishin27229712006-04-01 02:25:19 -08002176#ifdef CONFIG_COMPAT
Jan Engelhardt45385062009-07-04 12:50:00 +02002177 .compatsize = sizeof(compat_int_t),
2178 .compat_from_user = compat_standard_from_user,
2179 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08002180#endif
Jan Engelhardt45385062009-07-04 12:50:00 +02002181 },
2182 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02002183 .name = XT_ERROR_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02002184 .target = ipt_error,
Jan Engelhardt12b00c22010-10-13 15:56:56 +02002185 .targetsize = XT_FUNCTION_MAXNAMELEN,
Jan Engelhardt45385062009-07-04 12:50:00 +02002186 .family = NFPROTO_IPV4,
2187 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188};
2189
2190static struct nf_sockopt_ops ipt_sockopts = {
2191 .pf = PF_INET,
2192 .set_optmin = IPT_BASE_CTL,
2193 .set_optmax = IPT_SO_SET_MAX+1,
2194 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002195#ifdef CONFIG_COMPAT
2196 .compat_set = compat_do_ipt_set_ctl,
2197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 .get_optmin = IPT_BASE_CTL,
2199 .get_optmax = IPT_SO_GET_MAX+1,
2200 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08002201#ifdef CONFIG_COMPAT
2202 .compat_get = compat_do_ipt_get_ctl,
2203#endif
Neil Horman16fcec32007-09-11 11:28:26 +02002204 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205};
2206
Jan Engelhardt45385062009-07-04 12:50:00 +02002207static struct xt_match ipt_builtin_mt[] __read_mostly = {
2208 {
2209 .name = "icmp",
2210 .match = icmp_match,
2211 .matchsize = sizeof(struct ipt_icmp),
2212 .checkentry = icmp_checkentry,
2213 .proto = IPPROTO_ICMP,
2214 .family = NFPROTO_IPV4,
2215 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216};
2217
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002218static int __net_init ip_tables_net_init(struct net *net)
2219{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02002220 return xt_proto_init(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002221}
2222
2223static void __net_exit ip_tables_net_exit(struct net *net)
2224{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02002225 xt_proto_fini(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002226}
2227
2228static struct pernet_operations ip_tables_net_ops = {
2229 .init = ip_tables_net_init,
2230 .exit = ip_tables_net_exit,
2231};
2232
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002233static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 int ret;
2236
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002237 ret = register_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002238 if (ret < 0)
2239 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002240
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002241 /* No one else will be downing sem now, so we won't sleep */
Jan Engelhardt45385062009-07-04 12:50:00 +02002242 ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002243 if (ret < 0)
2244 goto err2;
Jan Engelhardt45385062009-07-04 12:50:00 +02002245 ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002246 if (ret < 0)
2247 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 /* Register setsockopt */
2250 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002251 if (ret < 0)
2252 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01002254 pr_info("(C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002256
2257err5:
Jan Engelhardt45385062009-07-04 12:50:00 +02002258 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002259err4:
Jan Engelhardt45385062009-07-04 12:50:00 +02002260 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002261err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002262 unregister_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07002263err1:
2264 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002267static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002270
Jan Engelhardt45385062009-07-04 12:50:00 +02002271 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
2272 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002273 unregister_pernet_subsys(&ip_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
2276EXPORT_SYMBOL(ipt_register_table);
2277EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002279module_init(ip_tables_init);
2280module_exit(ip_tables_fini);