blob: 4822459e8f425b4139cb09dcbbcb2b8248602d21 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#ifdef CONFIG_NETFILTER_DEBUG
Stephen Hemmingeraf567602010-05-13 15:00:20 +020039#define IP_NF_ASSERT(x) WARN_ON(!(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#else
41#define IP_NF_ASSERT(x)
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jan Engelhardte3eaa992009-06-17 22:14:54 +020044void *ipt_alloc_initial_table(const struct xt_table *info)
45{
46 return xt_alloc_initial_table(ipt, IPT);
47}
48EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* Returns whether matches rule or not. */
Denys Vlasenko022748a2008-01-14 23:44:05 -080051/* Performance critical - called for every packet */
Patrick McHardy9c547952007-12-17 21:52:00 -080052static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070053ip_packet_match(const struct iphdr *ip,
54 const char *indev,
55 const char *outdev,
56 const struct ipt_ip *ipinfo,
57 int isfrag)
58{
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned long ret;
60
Joe Perchesc37a2df2016-06-24 13:25:22 -070061 if (NF_INVF(ipinfo, IPT_INV_SRCIP,
62 (ip->saddr & ipinfo->smsk.s_addr) != ipinfo->src.s_addr) ||
63 NF_INVF(ipinfo, IPT_INV_DSTIP,
64 (ip->daddr & ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr))
Patrick McHardy9c547952007-12-17 21:52:00 -080065 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Eric Dumazetb8dfe492009-03-25 17:31:52 +010067 ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Joe Perchesc37a2df2016-06-24 13:25:22 -070069 if (NF_INVF(ipinfo, IPT_INV_VIA_IN, ret != 0))
Patrick McHardy9c547952007-12-17 21:52:00 -080070 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Eric Dumazetb8dfe492009-03-25 17:31:52 +010072 ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Joe Perchesc37a2df2016-06-24 13:25:22 -070074 if (NF_INVF(ipinfo, IPT_INV_VIA_OUT, ret != 0))
Patrick McHardy9c547952007-12-17 21:52:00 -080075 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 /* Check specific protocol */
Joe Perches3666ed12009-11-23 23:17:06 +010078 if (ipinfo->proto &&
Joe Perchesc37a2df2016-06-24 13:25:22 -070079 NF_INVF(ipinfo, IPT_INV_PROTO, ip->protocol != ipinfo->proto))
Patrick McHardy9c547952007-12-17 21:52:00 -080080 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 /* If we have a fragment rule but the packet is not a fragment
83 * then we return zero */
Joe Perchesc37a2df2016-06-24 13:25:22 -070084 if (NF_INVF(ipinfo, IPT_INV_FRAG,
85 (ipinfo->flags & IPT_F_FRAG) && !isfrag))
Patrick McHardy9c547952007-12-17 21:52:00 -080086 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Patrick McHardy9c547952007-12-17 21:52:00 -080088 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Denys Vlasenko022748a2008-01-14 23:44:05 -080091static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070092ip_checkentry(const struct ipt_ip *ip)
93{
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +020094 if (ip->flags & ~IPT_F_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070095 return false;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +020096 if (ip->invflags & ~IPT_INV_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070097 return false;
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070098 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200102ipt_error(struct sk_buff *skb, const struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Joe Perchese87cc472012-05-13 21:56:26 +0000104 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 return NF_DROP;
107}
108
Denys Vlasenko022748a2008-01-14 23:44:05 -0800109/* Performance critical */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static inline struct ipt_entry *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200111get_entry(const void *base, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 return (struct ipt_entry *)(base + offset);
114}
115
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700116/* All zeroes == unconditional rule. */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800117/* Mildly perf critical (only if packet tracing is on) */
Florian Westphal54d83fc2016-03-22 18:02:52 +0100118static inline bool unconditional(const struct ipt_entry *e)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700119{
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200120 static const struct ipt_ip uncond;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700121
Florian Westphal54d83fc2016-03-22 18:02:52 +0100122 return e->target_offset == sizeof(struct ipt_entry) &&
123 memcmp(&e->ip, &uncond, sizeof(uncond)) == 0;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700124}
125
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200126/* for const-correctness */
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200127static inline const struct xt_entry_target *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200128ipt_get_target_c(const struct ipt_entry *e)
129{
130 return ipt_get_target((struct ipt_entry *)e);
131}
132
Gao fengf0165882013-03-21 19:48:42 +0000133#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Denys Vlasenko022748a2008-01-14 23:44:05 -0800134static const char *const hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800135 [NF_INET_PRE_ROUTING] = "PREROUTING",
136 [NF_INET_LOCAL_IN] = "INPUT",
Patrick McHardy9c547952007-12-17 21:52:00 -0800137 [NF_INET_FORWARD] = "FORWARD",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800138 [NF_INET_LOCAL_OUT] = "OUTPUT",
139 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700140};
141
142enum nf_ip_trace_comments {
143 NF_IP_TRACE_COMMENT_RULE,
144 NF_IP_TRACE_COMMENT_RETURN,
145 NF_IP_TRACE_COMMENT_POLICY,
146};
147
Denys Vlasenko022748a2008-01-14 23:44:05 -0800148static const char *const comments[] = {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700149 [NF_IP_TRACE_COMMENT_RULE] = "rule",
150 [NF_IP_TRACE_COMMENT_RETURN] = "return",
151 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
152};
153
154static struct nf_loginfo trace_loginfo = {
155 .type = NF_LOG_TYPE_LOG,
156 .u = {
157 .log = {
158 .level = 4,
Liping Zhangff107d22016-09-25 16:35:56 +0800159 .logflags = NF_LOG_DEFAULT_MASK,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700160 },
161 },
162};
163
Denys Vlasenko022748a2008-01-14 23:44:05 -0800164/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700165static inline int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200166get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e,
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200167 const char *hookname, const char **chainname,
168 const char **comment, unsigned int *rulenum)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700169{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200170 const struct xt_standard_target *t = (void *)ipt_get_target_c(s);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700171
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200172 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700173 /* Head of user chain: ERROR target with chainname */
174 *chainname = t->target.data;
175 (*rulenum) = 0;
176 } else if (s == e) {
177 (*rulenum)++;
178
Florian Westphal54d83fc2016-03-22 18:02:52 +0100179 if (unconditional(s) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100180 strcmp(t->target.u.kernel.target->name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200181 XT_STANDARD_TARGET) == 0 &&
Florian Westphal54d83fc2016-03-22 18:02:52 +0100182 t->verdict < 0) {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700183 /* Tail of chains: STANDARD target (return/policy) */
184 *comment = *chainname == hookname
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200185 ? comments[NF_IP_TRACE_COMMENT_POLICY]
186 : comments[NF_IP_TRACE_COMMENT_RETURN];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700187 }
188 return 1;
189 } else
190 (*rulenum)++;
191
192 return 0;
193}
194
Eric W. Biederman9dff2c92015-09-15 20:04:17 -0500195static void trace_packet(struct net *net,
196 const struct sk_buff *skb,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700197 unsigned int hook,
198 const struct net_device *in,
199 const struct net_device *out,
Jan Engelhardtecb6f852008-01-31 03:54:47 -0800200 const char *tablename,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200201 const struct xt_table_info *private,
202 const struct ipt_entry *e)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700203{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200204 const struct ipt_entry *root;
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200205 const char *hookname, *chainname, *comment;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100206 const struct ipt_entry *iter;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700207 unsigned int rulenum = 0;
208
Florian Westphal482cfc32015-06-11 01:34:55 +0200209 root = get_entry(private->entries, private->hook_entry[hook]);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700210
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200211 hookname = chainname = hooknames[hook];
212 comment = comments[NF_IP_TRACE_COMMENT_RULE];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700213
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100214 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
215 if (get_chainname_rulenum(iter, e, hookname,
216 &chainname, &comment, &rulenum) != 0)
217 break;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700218
Pablo Neira Ayuso4017a7e2015-03-02 01:10:28 +0100219 nf_log_trace(net, AF_INET, hook, skb, in, out, &trace_loginfo,
220 "TRACE: %s:%s:%s:%u ",
221 tablename, chainname, comment, rulenum);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700222}
223#endif
224
Florian Westphal6c7941d2015-07-14 17:51:10 +0200225static inline
Jan Engelhardt98e86402009-04-15 21:06:05 +0200226struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
227{
228 return (void *)entry + entry->next_offset;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/* Returns one of the generic firewall policies, like NF_ACCEPT. */
232unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700233ipt_do_table(struct sk_buff *skb,
David S. Miller1c491ba2015-04-03 20:56:08 -0400234 const struct nf_hook_state *state,
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800235 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Eric W. Biederman6cb8ff3f12015-09-18 14:32:55 -0500237 unsigned int hook = state->hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Jan Engelhardt5452e422008-04-14 11:15:35 +0200239 const struct iphdr *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* Initializing verdict to NF_DROP keeps gcc happy. */
241 unsigned int verdict = NF_DROP;
242 const char *indev, *outdev;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200243 const void *table_base;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200244 struct ipt_entry *e, **jumpstack;
Florian Westphal7814b6e2015-07-14 17:51:08 +0200245 unsigned int stackidx, cpu;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200246 const struct xt_table_info *private;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200247 struct xt_action_param acpar;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200248 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* Initialization */
Florian Westphal7814b6e2015-07-14 17:51:08 +0200251 stackidx = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700252 ip = ip_hdr(skb);
David S. Miller1c491ba2015-04-03 20:56:08 -0400253 indev = state->in ? state->in->name : nulldevname;
254 outdev = state->out ? state->out->name : nulldevname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* We handle fragments by dealing with the first fragment as
256 * if it was a normal packet. All other fragments are treated
257 * normally, except that they will NEVER match rules that ask
258 * things we don't know, ie. tcp syn flag or ports). If the
259 * rule is also a fragment-specific rule, non-fragments won't
260 * match it. */
Jan Engelhardtde74c162009-07-05 18:26:37 +0200261 acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
262 acpar.thoff = ip_hdrlen(skb);
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200263 acpar.hotdrop = false;
Eric W. Biederman156c1962015-09-18 14:32:58 -0500264 acpar.net = state->net;
David S. Miller1c491ba2015-04-03 20:56:08 -0400265 acpar.in = state->in;
266 acpar.out = state->out;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200267 acpar.family = NFPROTO_IPV4;
268 acpar.hooknum = hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200271 local_bh_disable();
272 addend = xt_write_recseq_begin();
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700273 private = table->private;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200274 cpu = smp_processor_id();
Will Deaconb416c142013-10-21 13:14:53 +0100275 /*
276 * Ensure we load private-> members after we've fetched the base
277 * pointer.
278 */
279 smp_read_barrier_depends();
Florian Westphal482cfc32015-06-11 01:34:55 +0200280 table_base = private->entries;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200281 jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
Florian Westphal7814b6e2015-07-14 17:51:08 +0200282
283 /* Switch to alternate jumpstack if we're being invoked via TEE.
284 * TEE issues XT_CONTINUE verdict on original skb so we must not
285 * clobber the jumpstack.
286 *
287 * For recursion via REJECT or SYNPROXY the stack will be clobbered
288 * but it is no problem since absolute verdict is issued by these.
289 */
Florian Westphaldcebd312015-07-14 17:51:09 +0200290 if (static_key_false(&xt_tee_enabled))
291 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
Stephen Hemminger78454472009-02-20 10:35:32 +0100292
Harald Welte2e4e6a12006-01-12 13:30:04 -0800293 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 do {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200296 const struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100297 const struct xt_entry_match *ematch;
Florian Westphal71ae0df2015-06-11 01:34:54 +0200298 struct xt_counters *counter;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 IP_NF_ASSERT(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200301 if (!ip_packet_match(ip, indev, outdev,
Jan Engelhardtde74c162009-07-05 18:26:37 +0200302 &e->ip, acpar.fragoff)) {
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100303 no_match:
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200304 e = ipt_next_entry(e);
305 continue;
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Jan Engelhardtef53d702009-07-09 19:14:18 +0200308 xt_ematch_foreach(ematch, e) {
Jan Engelhardtde74c162009-07-05 18:26:37 +0200309 acpar.match = ematch->u.kernel.match;
310 acpar.matchinfo = ematch->data;
311 if (!acpar.match->match(skb, &acpar))
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100312 goto no_match;
Jan Engelhardtef53d702009-07-09 19:14:18 +0200313 }
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100314
Florian Westphal71ae0df2015-06-11 01:34:54 +0200315 counter = xt_get_this_cpu_counter(&e->counters);
316 ADD_COUNTER(*counter, skb->len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200318 t = ipt_get_target(e);
319 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700320
Gao fengf0165882013-03-21 19:48:42 +0000321#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200322 /* The packet is traced: log it */
323 if (unlikely(skb->nf_trace))
Eric W. Biederman9dff2c92015-09-15 20:04:17 -0500324 trace_packet(state->net, skb, hook, state->in,
325 state->out, table->name, private, e);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700326#endif
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200327 /* Standard target? */
328 if (!t->u.kernel.target->target) {
329 int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200331 v = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200332 if (v < 0) {
333 /* Pop from stack? */
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200334 if (v != XT_RETURN) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000335 verdict = (unsigned int)(-v) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 break;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200337 }
Florian Westphal7814b6e2015-07-14 17:51:08 +0200338 if (stackidx == 0) {
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200339 e = get_entry(table_base,
340 private->underflow[hook]);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200341 } else {
Florian Westphal7814b6e2015-07-14 17:51:08 +0200342 e = jumpstack[--stackidx];
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200343 e = ipt_next_entry(e);
344 }
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200345 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Joe Perches3666ed12009-11-23 23:17:06 +0100347 if (table_base + v != ipt_next_entry(e) &&
Florian Westphalf506da52018-02-07 13:46:25 +0100348 !(e->ip.flags & IPT_F_GOTO)) {
349 if (unlikely(stackidx >= private->stacksize)) {
350 verdict = NF_DROP;
351 break;
352 }
Florian Westphal7814b6e2015-07-14 17:51:08 +0200353 jumpstack[stackidx++] = e;
Florian Westphalf506da52018-02-07 13:46:25 +0100354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200356 e = get_entry(table_base, v);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200357 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200359
Jan Engelhardtde74c162009-07-05 18:26:37 +0200360 acpar.target = t->u.kernel.target;
361 acpar.targinfo = t->data;
Jan Engelhardtbb70dfa2009-04-15 21:40:13 +0200362
Jan Engelhardtde74c162009-07-05 18:26:37 +0200363 verdict = t->u.kernel.target->target(skb, &acpar);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200364 /* Target might have changed stuff. */
365 ip = ip_hdr(skb);
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200366 if (verdict == XT_CONTINUE)
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200367 e = ipt_next_entry(e);
368 else
369 /* Verdict */
370 break;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200371 } while (!acpar.hotdrop);
Florian Westphal7814b6e2015-07-14 17:51:08 +0200372
Ian Morris24cebe32015-10-14 23:17:07 +0100373 xt_write_recseq_end(addend);
374 local_bh_enable();
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200375
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200376 if (acpar.hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return NF_DROP;
378 else return verdict;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/* Figures out from what hook each rule can be called: returns 0 if
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200382 there are loops. Puts hook bitmask in comefrom. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static int
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200384mark_source_chains(const struct xt_table_info *newinfo,
Florian Westphalf4dc7772016-07-14 17:51:26 +0200385 unsigned int valid_hooks, void *entry0,
386 unsigned int *offsets)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 unsigned int hook;
389
390 /* No recursion; use packet counter to save back ptrs (reset
391 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800392 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800394 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (!(valid_hooks & (1 << hook)))
397 continue;
398
399 /* Set initial back pointer. */
400 e->counters.pcnt = pos;
401
402 for (;;) {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200403 const struct xt_standard_target *t
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200404 = (void *)ipt_get_target_c(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800405 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200407 if (e->comefrom & (1 << NF_INET_NUMHOOKS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200409
Patrick McHardy9c547952007-12-17 21:52:00 -0800410 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* Unconditional return/END. */
Florian Westphal54d83fc2016-03-22 18:02:52 +0100413 if ((unconditional(e) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100414 (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200415 XT_STANDARD_TARGET) == 0) &&
Florian Westphal54d83fc2016-03-22 18:02:52 +0100416 t->verdict < 0) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 unsigned int oldpos, size;
418
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100419 if ((strcmp(t->target.u.user.name,
Ian Morris24cebe32015-10-14 23:17:07 +0100420 XT_STANDARD_TARGET) == 0) &&
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200421 t->verdict < -NF_MAX_VERDICT - 1)
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800422 return 0;
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* Return: backtrack through the last
425 big jump. */
426 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800427 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 oldpos = pos;
429 pos = e->counters.pcnt;
430 e->counters.pcnt = 0;
431
432 /* We're at the start. */
433 if (pos == oldpos)
434 goto next;
435
436 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800437 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 } while (oldpos == pos + e->next_offset);
439
440 /* Move along one */
441 size = e->next_offset;
442 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800443 (entry0 + pos + size);
Florian Westphalf24e2302016-04-01 14:17:21 +0200444 if (pos + size >= newinfo->size)
445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 e->counters.pcnt = pos;
447 pos += size;
448 } else {
449 int newpos = t->verdict;
450
451 if (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200452 XT_STANDARD_TARGET) == 0 &&
Joe Perches3666ed12009-11-23 23:17:06 +0100453 newpos >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* This a jump; chase it. */
Florian Westphalf4dc7772016-07-14 17:51:26 +0200455 if (!xt_find_jump_offset(offsets, newpos,
456 newinfo->number))
457 return 0;
Florian Westphal36472342016-04-01 14:17:22 +0200458 e = (struct ipt_entry *)
459 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 } else {
461 /* ... this is a fallthru */
462 newpos = pos + e->next_offset;
Florian Westphalf24e2302016-04-01 14:17:21 +0200463 if (newpos >= newinfo->size)
464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800467 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 e->counters.pcnt = pos;
469 pos = newpos;
470 }
471 }
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200472next: ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 return 1;
475}
476
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200477static void cleanup_match(struct xt_entry_match *m, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200479 struct xt_mtdtor_param par;
480
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100481 par.net = net;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200482 par.match = m->u.kernel.match;
483 par.matchinfo = m->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200484 par.family = NFPROTO_IPV4;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200485 if (par.match->destroy != NULL)
486 par.match->destroy(&par);
487 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Denys Vlasenko022748a2008-01-14 23:44:05 -0800490static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200491check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Dmitry Mishina96be242006-12-12 00:29:26 -0800492{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200493 const struct ipt_ip *ip = par->entryinfo;
Dmitry Mishina96be242006-12-12 00:29:26 -0800494
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200495 par->match = m->u.kernel.match;
496 par->matchinfo = m->data;
497
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200498 return xt_check_match(par, m->u.match_size - sizeof(*m),
499 ip->proto, ip->invflags & IPT_INV_PROTO);
Dmitry Mishina96be242006-12-12 00:29:26 -0800500}
501
Denys Vlasenko022748a2008-01-14 23:44:05 -0800502static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200503find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800505 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800506 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200508 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
509 m->u.user.revision);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200510 if (IS_ERR(match))
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200511 return PTR_ERR(match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 m->u.kernel.match = match;
513
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100514 ret = check_match(m, par);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800515 if (ret)
516 goto err;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800519err:
520 module_put(m->u.kernel.match->me);
521 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Patrick McHardyadd67462010-02-03 13:45:12 +0100524static int check_target(struct ipt_entry *e, struct net *net, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800525{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200526 struct xt_entry_target *t = ipt_get_target(e);
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200527 struct xt_tgchk_param par = {
Patrick McHardyadd67462010-02-03 13:45:12 +0100528 .net = net,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200529 .table = name,
530 .entryinfo = e,
531 .target = t->u.kernel.target,
532 .targinfo = t->data,
533 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200534 .family = NFPROTO_IPV4,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200535 };
Dmitry Mishina96be242006-12-12 00:29:26 -0800536
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200537 return xt_check_target(&par, t->u.target_size - sizeof(*t),
538 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
Dmitry Mishina96be242006-12-12 00:29:26 -0800539}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Denys Vlasenko022748a2008-01-14 23:44:05 -0800541static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100542find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100543 unsigned int size,
544 struct xt_percpu_counter_alloc_state *alloc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200546 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800547 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int ret;
549 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200550 struct xt_mtchk_param mtpar;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100551 struct xt_entry_match *ematch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100553 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
Florian Westphal71ae0df2015-06-11 01:34:54 +0200554 return -ENOMEM;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 j = 0;
Florian Westphal40352e72018-06-07 21:34:43 +0200557 memset(&mtpar, 0, sizeof(mtpar));
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100558 mtpar.net = net;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200559 mtpar.table = name;
560 mtpar.entryinfo = &e->ip;
561 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200562 mtpar.family = NFPROTO_IPV4;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100563 xt_ematch_foreach(ematch, e) {
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100564 ret = find_check_match(ematch, &mtpar);
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100565 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100566 goto cleanup_matches;
567 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 t = ipt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200571 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
572 t->u.user.revision);
573 if (IS_ERR(target)) {
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200574 ret = PTR_ERR(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 goto cleanup_matches;
576 }
577 t->u.kernel.target = target;
578
Patrick McHardyadd67462010-02-03 13:45:12 +0100579 ret = check_target(e, net, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800580 if (ret)
581 goto err;
Florian Westphal71ae0df2015-06-11 01:34:54 +0200582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800584 err:
585 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 cleanup_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100587 xt_ematch_foreach(ematch, e) {
588 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100589 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100590 cleanup_match(ematch, net);
591 }
Florian Westphal71ae0df2015-06-11 01:34:54 +0200592
Florian Westphal61346e22016-11-22 14:44:17 +0100593 xt_percpu_counter_free(&e->counters);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return ret;
596}
597
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200598static bool check_underflow(const struct ipt_entry *e)
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200599{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200600 const struct xt_entry_target *t;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200601 unsigned int verdict;
602
Florian Westphal54d83fc2016-03-22 18:02:52 +0100603 if (!unconditional(e))
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200604 return false;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200605 t = ipt_get_target_c(e);
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200606 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
607 return false;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200608 verdict = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200609 verdict = -verdict - 1;
610 return verdict == NF_DROP || verdict == NF_ACCEPT;
611}
612
Denys Vlasenko022748a2008-01-14 23:44:05 -0800613static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800615 struct xt_table_info *newinfo,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200616 const unsigned char *base,
617 const unsigned char *limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 const unsigned int *hook_entries,
619 const unsigned int *underflows,
Jan Engelhardt05595182010-02-24 18:33:43 +0100620 unsigned int valid_hooks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 unsigned int h;
Florian Westphalbdf533d2016-03-22 18:02:49 +0100623 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Joe Perches3666ed12009-11-23 23:17:06 +0100625 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
Florian Westphal6e94e0c2016-03-22 18:02:50 +0100626 (unsigned char *)e + sizeof(struct ipt_entry) >= limit ||
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200627 (unsigned char *)e + e->next_offset > limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 if (e->next_offset
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200631 < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Florian Westphalaa412ba2016-04-01 14:17:24 +0200634 if (!ip_checkentry(&e->ip))
635 return -EINVAL;
636
Florian Westphalce683e52016-04-01 14:17:28 +0200637 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
638 e->next_offset);
Florian Westphalbdf533d2016-03-22 18:02:49 +0100639 if (err)
640 return err;
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800643 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Jan Engelhardta7d51732009-07-18 14:52:58 +0200644 if (!(valid_hooks & (1 << h)))
645 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if ((unsigned char *)e - base == hook_entries[h])
647 newinfo->hook_entry[h] = hook_entries[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200648 if ((unsigned char *)e - base == underflows[h]) {
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200649 if (!check_underflow(e))
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200650 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 newinfo->underflow[h] = underflows[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800657 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 e->comefrom = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return 0;
660}
661
Jan Engelhardt05595182010-02-24 18:33:43 +0100662static void
663cleanup_entry(struct ipt_entry *e, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200665 struct xt_tgdtor_param par;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200666 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100667 struct xt_entry_match *ematch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100670 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100671 cleanup_match(ematch, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 t = ipt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200673
Patrick McHardyadd67462010-02-03 13:45:12 +0100674 par.net = net;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200675 par.target = t->u.kernel.target;
676 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200677 par.family = NFPROTO_IPV4;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200678 if (par.target->destroy != NULL)
679 par.target->destroy(&par);
680 module_put(par.target->me);
Florian Westphal61346e22016-11-22 14:44:17 +0100681 xt_percpu_counter_free(&e->counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684/* Checks and translates the user-supplied table segment (held in
685 newinfo) */
686static int
Jan Engelhardt0f234212010-02-24 18:36:04 +0100687translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
Ian Morris6c282552015-10-14 23:17:06 +0100688 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100690 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100691 struct ipt_entry *iter;
Florian Westphalf4dc7772016-07-14 17:51:26 +0200692 unsigned int *offsets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 unsigned int i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100694 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Jan Engelhardt0f234212010-02-24 18:36:04 +0100696 newinfo->size = repl->size;
697 newinfo->number = repl->num_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800700 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 newinfo->hook_entry[i] = 0xFFFFFFFF;
702 newinfo->underflow[i] = 0xFFFFFFFF;
703 }
704
Florian Westphalf4dc7772016-07-14 17:51:26 +0200705 offsets = xt_alloc_entry_offsets(newinfo->number);
706 if (!offsets)
707 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 i = 0;
709 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100710 xt_entry_foreach(iter, entry0, newinfo->size) {
711 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +0100712 entry0 + repl->size,
713 repl->hook_entry,
714 repl->underflow,
715 repl->valid_hooks);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100716 if (ret != 0)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200717 goto out_free;
718 if (i < repl->num_entries)
719 offsets[i] = (void *)iter - entry0;
Jan Engelhardt05595182010-02-24 18:33:43 +0100720 ++i;
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200721 if (strcmp(ipt_get_target(iter)->u.user.name,
722 XT_ERROR_TARGET) == 0)
723 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Florian Westphalf4dc7772016-07-14 17:51:26 +0200726 ret = -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200727 if (i != repl->num_entries)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200728 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800731 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* Only hooks which are valid */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100733 if (!(repl->valid_hooks & (1 << i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 continue;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200735 if (newinfo->hook_entry[i] == 0xFFFFFFFF)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200736 goto out_free;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200737 if (newinfo->underflow[i] == 0xFFFFFFFF)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200738 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
Florian Westphalf4dc7772016-07-14 17:51:26 +0200741 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
742 ret = -ELOOP;
743 goto out_free;
744 }
745 kvfree(offsets);
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* Finally, each sanity check must pass */
748 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100749 xt_entry_foreach(iter, entry0, newinfo->size) {
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100750 ret = find_check_entry(iter, net, repl->name, repl->size,
751 &alloc_state);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100752 if (ret != 0)
753 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100754 ++i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800757 if (ret != 0) {
Jan Engelhardt05595182010-02-24 18:33:43 +0100758 xt_entry_foreach(iter, entry0, newinfo->size) {
759 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100760 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100761 cleanup_entry(iter, net);
762 }
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800763 return ret;
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return ret;
Florian Westphalf4dc7772016-07-14 17:51:26 +0200767 out_free:
768 kvfree(offsets);
769 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800773get_counters(const struct xt_table_info *t,
774 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100776 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 unsigned int cpu;
778 unsigned int i;
779
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700780 for_each_possible_cpu(cpu) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200781 seqcount_t *s = &per_cpu(xt_recseq, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 i = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200784 xt_entry_foreach(iter, t->entries, t->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +0200785 struct xt_counters *tmp;
Eric Dumazet83723d62011-01-10 20:11:38 +0100786 u64 bcnt, pcnt;
787 unsigned int start;
788
Florian Westphal71ae0df2015-06-11 01:34:54 +0200789 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100790 do {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200791 start = read_seqcount_begin(s);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200792 bcnt = tmp->bcnt;
793 pcnt = tmp->pcnt;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200794 } while (read_seqcount_retry(s, start));
Eric Dumazet83723d62011-01-10 20:11:38 +0100795
796 ADD_COUNTER(counters[i], bcnt, pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +0100797 ++i; /* macro does multi eval of i */
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100800}
801
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200802static struct xt_counters *alloc_counters(const struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Dmitry Mishin27229712006-04-01 02:25:19 -0800804 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800805 struct xt_counters *counters;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200806 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /* We need atomic snapshot of counters: rest doesn't change
809 (other than comefrom, which userspace doesn't care
810 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800811 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet83723d62011-01-10 20:11:38 +0100812 counters = vzalloc(countersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700815 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700817 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Dmitry Mishin27229712006-04-01 02:25:19 -0800819 return counters;
820}
821
822static int
823copy_entries_to_user(unsigned int total_size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200824 const struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800825 void __user *userptr)
826{
827 unsigned int off, num;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200828 const struct ipt_entry *e;
Dmitry Mishin27229712006-04-01 02:25:19 -0800829 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200830 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800831 int ret = 0;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700832 const void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -0800833
834 counters = alloc_counters(table);
835 if (IS_ERR(counters))
836 return PTR_ERR(counters);
837
Florian Westphal482cfc32015-06-11 01:34:55 +0200838 loc_cpu_entry = private->entries;
Eric Dumazet31836062005-12-13 23:13:48 -0800839 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ret = -EFAULT;
841 goto free_counters;
842 }
843
844 /* FIXME: use iterator macros --RR */
845 /* ... then go back and fix counters and names */
846 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
847 unsigned int i;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200848 const struct xt_entry_match *m;
849 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Eric Dumazet31836062005-12-13 23:13:48 -0800851 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (copy_to_user(userptr + off
853 + offsetof(struct ipt_entry, counters),
854 &counters[num],
855 sizeof(counters[num])) != 0) {
856 ret = -EFAULT;
857 goto free_counters;
858 }
859
860 for (i = sizeof(struct ipt_entry);
861 i < e->target_offset;
862 i += m->u.match_size) {
863 m = (void *)e + i;
864
865 if (copy_to_user(userptr + off + i
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200866 + offsetof(struct xt_entry_match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 u.user.name),
868 m->u.kernel.match->name,
869 strlen(m->u.kernel.match->name)+1)
870 != 0) {
871 ret = -EFAULT;
872 goto free_counters;
873 }
874 }
875
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200876 t = ipt_get_target_c(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (copy_to_user(userptr + off + e->target_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200878 + offsetof(struct xt_entry_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 u.user.name),
880 t->u.kernel.target->name,
881 strlen(t->u.kernel.target->name)+1) != 0) {
882 ret = -EFAULT;
883 goto free_counters;
884 }
885 }
886
887 free_counters:
888 vfree(counters);
889 return ret;
890}
891
Dmitry Mishin27229712006-04-01 02:25:19 -0800892#ifdef CONFIG_COMPAT
Jan Engelhardt739674f2009-06-26 08:23:19 +0200893static void compat_standard_from_user(void *dst, const void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -0800894{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700895 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -0800896
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700897 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800898 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700899 memcpy(dst, &v, sizeof(v));
900}
901
Jan Engelhardt739674f2009-06-26 08:23:19 +0200902static int compat_standard_to_user(void __user *dst, const void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -0800903{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700904 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -0800905
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700906 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800907 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700908 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -0800909}
910
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200911static int compat_calc_entry(const struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -0800912 const struct xt_table_info *info,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200913 const void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -0800914{
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100915 const struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200916 const struct xt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -0800917 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -0800918 int off, i, ret;
919
Patrick McHardy30c08c42007-12-17 21:47:14 -0800920 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -0800921 entry_offset = (void *)e - base;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100922 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100923 off += xt_compat_match_offset(ematch->u.kernel.match);
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200924 t = ipt_get_target_c(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700925 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -0800926 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800927 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -0800928 if (ret)
929 return ret;
930
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800931 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -0800932 if (info->hook_entry[i] &&
933 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -0800934 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -0800935 if (info->underflow[i] &&
936 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -0800937 newinfo->underflow[i] -= off;
938 }
939 return 0;
940}
941
Eric Dumazet259d4e42007-12-04 23:24:56 -0800942static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -0800943 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -0800944{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100945 struct ipt_entry *iter;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700946 const void *loc_cpu_entry;
Jan Engelhardt05595182010-02-24 18:33:43 +0100947 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -0800948
949 if (!newinfo || !info)
950 return -EINVAL;
951
Florian Westphal482cfc32015-06-11 01:34:55 +0200952 /* we dont care about newinfo->entries */
Eric Dumazet259d4e42007-12-04 23:24:56 -0800953 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
954 newinfo->initial_entries = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200955 loc_cpu_entry = info->entries;
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100956 xt_compat_init_offsets(AF_INET, info->number);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100957 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
958 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
959 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +0100960 return ret;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100961 }
Jan Engelhardt05595182010-02-24 18:33:43 +0100962 return 0;
Dmitry Mishin27229712006-04-01 02:25:19 -0800963}
964#endif
965
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200966static int get_info(struct net *net, void __user *user,
Ian Morris6c282552015-10-14 23:17:06 +0100967 const int *len, int compat)
Dmitry Mishin27229712006-04-01 02:25:19 -0800968{
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200969 char name[XT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800970 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -0800971 int ret;
972
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200973 if (*len != sizeof(struct ipt_getinfo))
Dmitry Mishin27229712006-04-01 02:25:19 -0800974 return -EINVAL;
Dmitry Mishin27229712006-04-01 02:25:19 -0800975
976 if (copy_from_user(name, user, sizeof(name)) != 0)
977 return -EFAULT;
978
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200979 name[XT_TABLE_MAXNAMELEN-1] = '\0';
Dmitry Mishin27229712006-04-01 02:25:19 -0800980#ifdef CONFIG_COMPAT
981 if (compat)
982 xt_compat_lock(AF_INET);
983#endif
Alexey Dobriyan34bd1372008-01-31 04:03:03 -0800984 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -0800985 "iptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +0000986 if (!IS_ERR_OR_NULL(t)) {
Dmitry Mishin27229712006-04-01 02:25:19 -0800987 struct ipt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200988 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800989#ifdef CONFIG_COMPAT
Alexey Dobriyan14c7dbe2010-02-08 11:17:43 -0800990 struct xt_table_info tmp;
991
Dmitry Mishin27229712006-04-01 02:25:19 -0800992 if (compat) {
Dmitry Mishin27229712006-04-01 02:25:19 -0800993 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800994 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -0800995 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -0800996 }
997#endif
Vasiliy Kulikovb5f15ac2010-11-03 08:45:06 +0100998 memset(&info, 0, sizeof(info));
Dmitry Mishin27229712006-04-01 02:25:19 -0800999 info.valid_hooks = t->valid_hooks;
1000 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -08001001 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -08001002 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -08001003 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -08001004 info.num_entries = private->number;
1005 info.size = private->size;
1006 strcpy(info.name, name);
1007
1008 if (copy_to_user(user, &info, *len) != 0)
1009 ret = -EFAULT;
1010 else
1011 ret = 0;
1012
1013 xt_table_unlock(t);
1014 module_put(t->me);
1015 } else
1016 ret = t ? PTR_ERR(t) : -ENOENT;
1017#ifdef CONFIG_COMPAT
1018 if (compat)
1019 xt_compat_unlock(AF_INET);
1020#endif
1021 return ret;
1022}
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001025get_entries(struct net *net, struct ipt_get_entries __user *uptr,
1026 const int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001029 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001030 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001032 if (*len < sizeof(get))
Dmitry Mishin27229712006-04-01 02:25:19 -08001033 return -EINVAL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001034 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1035 return -EFAULT;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001036 if (*len != sizeof(struct ipt_get_entries) + get.size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001037 return -EINVAL;
Pablo Neira Ayusob301f252016-03-24 21:29:53 +01001038 get.name[sizeof(get.name) - 1] = '\0';
Dmitry Mishin27229712006-04-01 02:25:19 -08001039
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001040 t = xt_find_table_lock(net, AF_INET, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001041 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001042 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001043 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001044 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 t, uptr->entrytable);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001046 else
Patrick McHardy544473c2008-04-14 11:15:45 +02001047 ret = -EAGAIN;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001050 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 } else
1052 ret = t ? PTR_ERR(t) : -ENOENT;
1053
1054 return ret;
1055}
1056
1057static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001058__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001059 struct xt_table_info *newinfo, unsigned int num_counters,
1060 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001061{
1062 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001063 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001064 struct xt_table_info *oldinfo;
1065 struct xt_counters *counters;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001066 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001067
1068 ret = 0;
Eric Dumazet83723d62011-01-10 20:11:38 +01001069 counters = vzalloc(num_counters * sizeof(struct xt_counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001070 if (!counters) {
1071 ret = -ENOMEM;
1072 goto out;
1073 }
1074
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001075 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Dmitry Mishin27229712006-04-01 02:25:19 -08001076 "iptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001077 if (IS_ERR_OR_NULL(t)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001078 ret = t ? PTR_ERR(t) : -ENOENT;
1079 goto free_newinfo_counters_untrans;
1080 }
1081
1082 /* You lied! */
1083 if (valid_hooks != t->valid_hooks) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001084 ret = -EINVAL;
1085 goto put_module;
1086 }
1087
1088 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1089 if (!oldinfo)
1090 goto put_module;
1091
1092 /* Update module usage count based on number of rules */
Dmitry Mishin27229712006-04-01 02:25:19 -08001093 if ((oldinfo->number > oldinfo->initial_entries) ||
1094 (newinfo->number <= oldinfo->initial_entries))
1095 module_put(t->me);
1096 if ((oldinfo->number > oldinfo->initial_entries) &&
1097 (newinfo->number <= oldinfo->initial_entries))
1098 module_put(t->me);
1099
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001100 /* Get the old counters, and synchronize with replace */
Dmitry Mishin27229712006-04-01 02:25:19 -08001101 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001102
Dmitry Mishin27229712006-04-01 02:25:19 -08001103 /* Decrease module usage counts and free resource */
Florian Westphal482cfc32015-06-11 01:34:55 +02001104 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001105 cleanup_entry(iter, net);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001106
Dmitry Mishin27229712006-04-01 02:25:19 -08001107 xt_free_table_info(oldinfo);
1108 if (copy_to_user(counters_ptr, counters,
Thomas Grafc58dd2d2014-04-04 17:57:45 +02001109 sizeof(struct xt_counters) * num_counters) != 0) {
1110 /* Silent error, can't fail, new table is already in place */
1111 net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
1112 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001113 vfree(counters);
1114 xt_table_unlock(t);
1115 return ret;
1116
1117 put_module:
1118 module_put(t->me);
1119 xt_table_unlock(t);
1120 free_newinfo_counters_untrans:
1121 vfree(counters);
1122 out:
1123 return ret;
1124}
1125
1126static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001127do_replace(struct net *net, const void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 int ret;
1130 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001131 struct xt_table_info *newinfo;
1132 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001133 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1136 return -EFAULT;
1137
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001138 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001139 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1140 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001141 if (tmp.num_counters == 0)
1142 return -EINVAL;
1143
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01001144 tmp.name[sizeof(tmp.name)-1] = 0;
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001145
Harald Welte2e4e6a12006-01-12 13:30:04 -08001146 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!newinfo)
1148 return -ENOMEM;
1149
Florian Westphal482cfc32015-06-11 01:34:55 +02001150 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -08001151 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 tmp.size) != 0) {
1153 ret = -EFAULT;
1154 goto free_newinfo;
1155 }
1156
Jan Engelhardt0f234212010-02-24 18:36:04 +01001157 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001159 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001161 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001162 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001163 if (ret)
1164 goto free_newinfo_untrans;
1165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Dmitry Mishin27229712006-04-01 02:25:19 -08001167 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001168 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001169 cleanup_entry(iter, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001171 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return ret;
1173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001176do_add_counters(struct net *net, const void __user *user,
Ian Morris6c282552015-10-14 23:17:06 +01001177 unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Florian Westphal482cfc32015-06-11 01:34:55 +02001179 unsigned int i;
Dmitry Mishin27229712006-04-01 02:25:19 -08001180 struct xt_counters_info tmp;
1181 struct xt_counters *paddc;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001182 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001183 const struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int ret = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001185 struct ipt_entry *iter;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001186 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Florian Westphald7591f02016-04-01 15:37:59 +02001188 paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1189 if (IS_ERR(paddc))
1190 return PTR_ERR(paddc);
Dmitry Mishin27229712006-04-01 02:25:19 -08001191
Florian Westphald7591f02016-04-01 15:37:59 +02001192 t = xt_find_table_lock(net, AF_INET, tmp.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001193 if (IS_ERR_OR_NULL(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ret = t ? PTR_ERR(t) : -ENOENT;
1195 goto free;
1196 }
1197
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001198 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001199 private = t->private;
Florian Westphald7591f02016-04-01 15:37:59 +02001200 if (private->number != tmp.num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 ret = -EINVAL;
1202 goto unlock_up_free;
1203 }
1204
1205 i = 0;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001206 addend = xt_write_recseq_begin();
Florian Westphal482cfc32015-06-11 01:34:55 +02001207 xt_entry_foreach(iter, private->entries, private->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +02001208 struct xt_counters *tmp;
1209
1210 tmp = xt_get_this_cpu_counter(&iter->counters);
1211 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +01001212 ++i;
1213 }
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001214 xt_write_recseq_end(addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001216 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001217 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 module_put(t->me);
1219 free:
1220 vfree(paddc);
1221
1222 return ret;
1223}
1224
Dmitry Mishin27229712006-04-01 02:25:19 -08001225#ifdef CONFIG_COMPAT
1226struct compat_ipt_replace {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001227 char name[XT_TABLE_MAXNAMELEN];
Dmitry Mishin27229712006-04-01 02:25:19 -08001228 u32 valid_hooks;
1229 u32 num_entries;
1230 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001231 u32 hook_entry[NF_INET_NUMHOOKS];
1232 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001233 u32 num_counters;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001234 compat_uptr_t counters; /* struct xt_counters * */
Dmitry Mishin27229712006-04-01 02:25:19 -08001235 struct compat_ipt_entry entries[0];
1236};
1237
Patrick McHardya18aa312007-12-12 10:35:16 -08001238static int
1239compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001240 unsigned int *size, struct xt_counters *counters,
Jan Engelhardt05595182010-02-24 18:33:43 +01001241 unsigned int i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001242{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001243 struct xt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001244 struct compat_ipt_entry __user *ce;
1245 u_int16_t target_offset, next_offset;
1246 compat_uint_t origsize;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001247 const struct xt_entry_match *ematch;
1248 int ret = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001249
Dmitry Mishin27229712006-04-01 02:25:19 -08001250 origsize = *size;
1251 ce = (struct compat_ipt_entry __user *)*dstptr;
Jan Engelhardt05595182010-02-24 18:33:43 +01001252 if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
1253 copy_to_user(&ce->counters, &counters[i],
1254 sizeof(counters[i])) != 0)
1255 return -EFAULT;
Patrick McHardya18aa312007-12-12 10:35:16 -08001256
Dmitry Mishin27229712006-04-01 02:25:19 -08001257 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001258 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1259
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001260 xt_ematch_foreach(ematch, e) {
1261 ret = xt_compat_match_to_user(ematch, dstptr, size);
1262 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001263 return ret;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001264 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001265 target_offset = e->target_offset - (origsize - *size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001266 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001267 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001268 if (ret)
Jan Engelhardt05595182010-02-24 18:33:43 +01001269 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001270 next_offset = e->next_offset - (origsize - *size);
Jan Engelhardt05595182010-02-24 18:33:43 +01001271 if (put_user(target_offset, &ce->target_offset) != 0 ||
1272 put_user(next_offset, &ce->next_offset) != 0)
1273 return -EFAULT;
Dmitry Mishin27229712006-04-01 02:25:19 -08001274 return 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001275}
1276
Denys Vlasenko022748a2008-01-14 23:44:05 -08001277static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001278compat_find_calc_match(struct xt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001279 const struct ipt_ip *ip,
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001280 int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001281{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001282 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001283
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001284 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
1285 m->u.user.revision);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001286 if (IS_ERR(match))
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001287 return PTR_ERR(match);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001288
Dmitry Mishin27229712006-04-01 02:25:19 -08001289 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001290 *size += xt_compat_match_offset(match);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001291 return 0;
1292}
1293
Jan Engelhardt05595182010-02-24 18:33:43 +01001294static void compat_release_entry(struct compat_ipt_entry *e)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001295{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001296 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001297 struct xt_entry_match *ematch;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001298
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001299 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001300 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001301 module_put(ematch->u.kernel.match->me);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001302 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001303 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001304}
1305
Denys Vlasenko022748a2008-01-14 23:44:05 -08001306static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001307check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001308 struct xt_table_info *newinfo,
1309 unsigned int *size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001310 const unsigned char *base,
Florian Westphal09d96862016-04-01 14:17:34 +02001311 const unsigned char *limit)
Dmitry Mishin27229712006-04-01 02:25:19 -08001312{
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001313 struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001314 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001315 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001316 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001317 unsigned int j;
Florian Westphal09d96862016-04-01 14:17:34 +02001318 int ret, off;
Dmitry Mishin27229712006-04-01 02:25:19 -08001319
Joe Perches3666ed12009-11-23 23:17:06 +01001320 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
Florian Westphal6e94e0c2016-03-22 18:02:50 +01001321 (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit ||
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001322 (unsigned char *)e + e->next_offset > limit)
Dmitry Mishin27229712006-04-01 02:25:19 -08001323 return -EINVAL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001324
1325 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001326 sizeof(struct compat_xt_entry_target))
Dmitry Mishin27229712006-04-01 02:25:19 -08001327 return -EINVAL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001328
Florian Westphalaa412ba2016-04-01 14:17:24 +02001329 if (!ip_checkentry(&e->ip))
1330 return -EINVAL;
1331
Florian Westphalce683e52016-04-01 14:17:28 +02001332 ret = xt_compat_check_entry_offsets(e, e->elems,
Florian Westphalfc1221b2016-04-01 14:17:26 +02001333 e->target_offset, e->next_offset);
Dmitry Mishina96be242006-12-12 00:29:26 -08001334 if (ret)
1335 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001336
Patrick McHardy30c08c42007-12-17 21:47:14 -08001337 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001338 entry_offset = (void *)e - (void *)base;
1339 j = 0;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001340 xt_ematch_foreach(ematch, e) {
Florian Westphal7d3f8432016-04-01 14:17:30 +02001341 ret = compat_find_calc_match(ematch, &e->ip, &off);
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001342 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001343 goto release_matches;
1344 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001345 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001346
Patrick McHardy73cd5982007-12-17 21:47:32 -08001347 t = compat_ipt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001348 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
1349 t->u.user.revision);
1350 if (IS_ERR(target)) {
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001351 ret = PTR_ERR(target);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001352 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001353 }
1354 t->u.kernel.target = target;
1355
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001356 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001357 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001358 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001359 if (ret)
1360 goto out;
1361
Dmitry Mishin27229712006-04-01 02:25:19 -08001362 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001363
Dmitry Mishin27229712006-04-01 02:25:19 -08001364out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001365 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001366release_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001367 xt_ematch_foreach(ematch, e) {
1368 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001369 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001370 module_put(ematch->u.kernel.match->me);
1371 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001372 return ret;
1373}
1374
Florian Westphal01883462016-04-01 14:17:33 +02001375static void
Patrick McHardy73cd5982007-12-17 21:47:32 -08001376compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Florian Westphal7d3f8432016-04-01 14:17:30 +02001377 unsigned int *size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001378 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001379{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001380 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001381 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001382 struct ipt_entry *de;
1383 unsigned int origsize;
Florian Westphal01883462016-04-01 14:17:33 +02001384 int h;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001385 struct xt_entry_match *ematch;
Dmitry Mishin27229712006-04-01 02:25:19 -08001386
Dmitry Mishin27229712006-04-01 02:25:19 -08001387 origsize = *size;
1388 de = (struct ipt_entry *)*dstptr;
1389 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001390 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001391
Patrick McHardy73cd5982007-12-17 21:47:32 -08001392 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001393 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1394
Florian Westphal01883462016-04-01 14:17:33 +02001395 xt_ematch_foreach(ematch, e)
1396 xt_compat_match_from_user(ematch, dstptr, size);
1397
Dmitry Mishin27229712006-04-01 02:25:19 -08001398 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001399 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001400 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001401 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001402
1403 de->next_offset = e->next_offset - (origsize - *size);
Florian Westphal09d96862016-04-01 14:17:34 +02001404
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001405 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001406 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1407 newinfo->hook_entry[h] -= origsize - *size;
1408 if ((unsigned char *)de - base < newinfo->underflow[h])
1409 newinfo->underflow[h] -= origsize - *size;
1410 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001411}
Dmitry Mishin27229712006-04-01 02:25:19 -08001412
Denys Vlasenko022748a2008-01-14 23:44:05 -08001413static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001414translate_compat_table(struct net *net,
Patrick McHardy4b478242007-12-17 21:46:15 -08001415 struct xt_table_info **pinfo,
1416 void **pentry0,
Florian Westphal7d3f8432016-04-01 14:17:30 +02001417 const struct compat_ipt_replace *compatr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001418{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001419 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001420 struct xt_table_info *newinfo, *info;
1421 void *pos, *entry0, *entry1;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001422 struct compat_ipt_entry *iter0;
Florian Westphal09d96862016-04-01 14:17:34 +02001423 struct ipt_replace repl;
Dmitry Mishin27229712006-04-01 02:25:19 -08001424 unsigned int size;
Jan Engelhardt05595182010-02-24 18:33:43 +01001425 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001426
1427 info = *pinfo;
1428 entry0 = *pentry0;
Florian Westphal7d3f8432016-04-01 14:17:30 +02001429 size = compatr->size;
1430 info->number = compatr->num_entries;
Dmitry Mishin27229712006-04-01 02:25:19 -08001431
Dmitry Mishin920b8682006-10-30 15:14:27 -08001432 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001433 xt_compat_lock(AF_INET);
Florian Westphal7d3f8432016-04-01 14:17:30 +02001434 xt_compat_init_offsets(AF_INET, compatr->num_entries);
Dmitry Mishin27229712006-04-01 02:25:19 -08001435 /* Walk through entries, checking offsets. */
Florian Westphal7d3f8432016-04-01 14:17:30 +02001436 xt_entry_foreach(iter0, entry0, compatr->size) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001437 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001438 entry0,
Florian Westphal09d96862016-04-01 14:17:34 +02001439 entry0 + compatr->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001440 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001441 goto out_unlock;
1442 ++j;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001443 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001444
1445 ret = -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001446 if (j != compatr->num_entries)
Dmitry Mishin27229712006-04-01 02:25:19 -08001447 goto out_unlock;
Dmitry Mishin27229712006-04-01 02:25:19 -08001448
Dmitry Mishin27229712006-04-01 02:25:19 -08001449 ret = -ENOMEM;
1450 newinfo = xt_alloc_table_info(size);
1451 if (!newinfo)
1452 goto out_unlock;
1453
Florian Westphal7d3f8432016-04-01 14:17:30 +02001454 newinfo->number = compatr->num_entries;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001455 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Florian Westphal09d96862016-04-01 14:17:34 +02001456 newinfo->hook_entry[i] = compatr->hook_entry[i];
1457 newinfo->underflow[i] = compatr->underflow[i];
Dmitry Mishin27229712006-04-01 02:25:19 -08001458 }
Florian Westphal482cfc32015-06-11 01:34:55 +02001459 entry1 = newinfo->entries;
Dmitry Mishin27229712006-04-01 02:25:19 -08001460 pos = entry1;
Florian Westphal7d3f8432016-04-01 14:17:30 +02001461 size = compatr->size;
Florian Westphal01883462016-04-01 14:17:33 +02001462 xt_entry_foreach(iter0, entry0, compatr->size)
1463 compat_copy_entry_from_user(iter0, &pos, &size,
1464 newinfo, entry1);
1465
Florian Westphal09d96862016-04-01 14:17:34 +02001466 /* all module references in entry0 are now gone.
1467 * entry1/newinfo contains a 64bit ruleset that looks exactly as
1468 * generated by 64bit userspace.
1469 *
1470 * Call standard translate_table() to validate all hook_entrys,
1471 * underflows, check for loops, etc.
1472 */
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001473 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001474 xt_compat_unlock(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001475
Florian Westphal09d96862016-04-01 14:17:34 +02001476 memcpy(&repl, compatr, sizeof(*compatr));
Dmitry Mishin27229712006-04-01 02:25:19 -08001477
Florian Westphal09d96862016-04-01 14:17:34 +02001478 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1479 repl.hook_entry[i] = newinfo->hook_entry[i];
1480 repl.underflow[i] = newinfo->underflow[i];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001481 }
Florian Westphal09d96862016-04-01 14:17:34 +02001482
1483 repl.num_counters = 0;
1484 repl.counters = NULL;
1485 repl.size = newinfo->size;
1486 ret = translate_table(net, newinfo, entry1, &repl);
1487 if (ret)
1488 goto free_newinfo;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001489
Dmitry Mishin27229712006-04-01 02:25:19 -08001490 *pinfo = newinfo;
1491 *pentry0 = entry1;
1492 xt_free_table_info(info);
1493 return 0;
1494
1495free_newinfo:
1496 xt_free_table_info(newinfo);
Florian Westphal09d96862016-04-01 14:17:34 +02001497 return ret;
1498out_unlock:
1499 xt_compat_flush_offsets(AF_INET);
1500 xt_compat_unlock(AF_INET);
Florian Westphal7d3f8432016-04-01 14:17:30 +02001501 xt_entry_foreach(iter0, entry0, compatr->size) {
Jan Engelhardt05595182010-02-24 18:33:43 +01001502 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001503 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001504 compat_release_entry(iter0);
1505 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001506 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001507}
1508
1509static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001510compat_do_replace(struct net *net, void __user *user, unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001511{
1512 int ret;
1513 struct compat_ipt_replace tmp;
1514 struct xt_table_info *newinfo;
1515 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001516 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001517
1518 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1519 return -EFAULT;
1520
Dmitry Mishin27229712006-04-01 02:25:19 -08001521 /* overflow check */
Dmitry Mishin27229712006-04-01 02:25:19 -08001522 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1523 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001524 if (tmp.num_counters == 0)
1525 return -EINVAL;
1526
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01001527 tmp.name[sizeof(tmp.name)-1] = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001528
1529 newinfo = xt_alloc_table_info(tmp.size);
1530 if (!newinfo)
1531 return -ENOMEM;
1532
Florian Westphal482cfc32015-06-11 01:34:55 +02001533 loc_cpu_entry = newinfo->entries;
Dmitry Mishin27229712006-04-01 02:25:19 -08001534 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1535 tmp.size) != 0) {
1536 ret = -EFAULT;
1537 goto free_newinfo;
1538 }
1539
Florian Westphal7d3f8432016-04-01 14:17:30 +02001540 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
Dmitry Mishin27229712006-04-01 02:25:19 -08001541 if (ret != 0)
1542 goto free_newinfo;
1543
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001544 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001545 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001546 if (ret)
1547 goto free_newinfo_untrans;
1548 return 0;
1549
1550 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001551 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001552 cleanup_entry(iter, net);
Dmitry Mishin27229712006-04-01 02:25:19 -08001553 free_newinfo:
1554 xt_free_table_info(newinfo);
1555 return ret;
1556}
1557
1558static int
1559compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001560 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001561{
1562 int ret;
1563
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001564 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Dmitry Mishin27229712006-04-01 02:25:19 -08001565 return -EPERM;
1566
1567 switch (cmd) {
1568 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001569 ret = compat_do_replace(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001570 break;
1571
1572 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001573 ret = do_add_counters(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001574 break;
1575
1576 default:
Dmitry Mishin27229712006-04-01 02:25:19 -08001577 ret = -EINVAL;
1578 }
1579
1580 return ret;
1581}
1582
Patrick McHardy4b478242007-12-17 21:46:15 -08001583struct compat_ipt_get_entries {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001584 char name[XT_TABLE_MAXNAMELEN];
Dmitry Mishin27229712006-04-01 02:25:19 -08001585 compat_uint_t size;
1586 struct compat_ipt_entry entrytable[0];
1587};
1588
Patrick McHardy4b478242007-12-17 21:46:15 -08001589static int
1590compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1591 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001592{
Dmitry Mishin27229712006-04-01 02:25:19 -08001593 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001594 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001595 void __user *pos;
1596 unsigned int size;
1597 int ret = 0;
Patrick McHardya18aa312007-12-12 10:35:16 -08001598 unsigned int i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001599 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001600
1601 counters = alloc_counters(table);
1602 if (IS_ERR(counters))
1603 return PTR_ERR(counters);
1604
Dmitry Mishin27229712006-04-01 02:25:19 -08001605 pos = userptr;
1606 size = total_size;
Florian Westphal482cfc32015-06-11 01:34:55 +02001607 xt_entry_foreach(iter, private->entries, total_size) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001608 ret = compat_copy_entry_to_user(iter, &pos,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001609 &size, counters, i++);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001610 if (ret != 0)
1611 break;
1612 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001613
Dmitry Mishin27229712006-04-01 02:25:19 -08001614 vfree(counters);
1615 return ret;
1616}
1617
1618static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001619compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1620 int *len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001621{
1622 int ret;
1623 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001624 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001625
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001626 if (*len < sizeof(get))
Dmitry Mishin27229712006-04-01 02:25:19 -08001627 return -EINVAL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001628
1629 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1630 return -EFAULT;
1631
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001632 if (*len != sizeof(struct compat_ipt_get_entries) + get.size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001633 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001634
Pablo Neira Ayusob301f252016-03-24 21:29:53 +01001635 get.name[sizeof(get.name) - 1] = '\0';
Dmitry Mishin27229712006-04-01 02:25:19 -08001636
1637 xt_compat_lock(AF_INET);
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001638 t = xt_find_table_lock(net, AF_INET, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001639 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001640 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001641 struct xt_table_info info;
Dmitry Mishin27229712006-04-01 02:25:19 -08001642 ret = compat_table_info(private, &info);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001643 if (!ret && get.size == info.size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001644 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001645 t, uptr->entrytable);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001646 else if (!ret)
Patrick McHardy544473c2008-04-14 11:15:45 +02001647 ret = -EAGAIN;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001648
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001649 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001650 module_put(t->me);
1651 xt_table_unlock(t);
1652 } else
1653 ret = t ? PTR_ERR(t) : -ENOENT;
1654
1655 xt_compat_unlock(AF_INET);
1656 return ret;
1657}
1658
Patrick McHardy79030ed2006-09-20 12:05:08 -07001659static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1660
Dmitry Mishin27229712006-04-01 02:25:19 -08001661static int
1662compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1663{
1664 int ret;
1665
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001666 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Björn Steinbrink82fac052006-10-20 00:21:10 -07001667 return -EPERM;
1668
Dmitry Mishin27229712006-04-01 02:25:19 -08001669 switch (cmd) {
1670 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001671 ret = get_info(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001672 break;
1673 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001674 ret = compat_get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001675 break;
1676 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001677 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001678 }
1679 return ret;
1680}
1681#endif
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001684do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 int ret;
1687
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001688 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return -EPERM;
1690
1691 switch (cmd) {
1692 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001693 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
1695
1696 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001697 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 break;
1699
1700 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 ret = -EINVAL;
1702 }
1703
1704 return ret;
1705}
1706
1707static int
1708do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1709{
1710 int ret;
1711
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001712 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return -EPERM;
1714
1715 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001716 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001717 ret = get_info(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08001719
1720 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001721 ret = get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001722 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 case IPT_SO_GET_REVISION_MATCH:
1725 case IPT_SO_GET_REVISION_TARGET: {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001726 struct xt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001727 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 if (*len != sizeof(rev)) {
1730 ret = -EINVAL;
1731 break;
1732 }
1733 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1734 ret = -EFAULT;
1735 break;
1736 }
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01001737 rev.name[sizeof(rev.name)-1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001740 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08001742 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Harald Welte2e4e6a12006-01-12 13:30:04 -08001744 try_then_request_module(xt_find_revision(AF_INET, rev.name,
1745 rev.revision,
1746 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 "ipt_%s", rev.name);
1748 break;
1749 }
1750
1751 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 ret = -EINVAL;
1753 }
1754
1755 return ret;
1756}
1757
Florian Westphalb9e69e12016-02-25 10:08:36 +01001758static void __ipt_unregister_table(struct net *net, struct xt_table *table)
1759{
1760 struct xt_table_info *private;
1761 void *loc_cpu_entry;
1762 struct module *table_owner = table->me;
1763 struct ipt_entry *iter;
1764
1765 private = xt_unregister_table(table);
1766
1767 /* Decrease module usage counts and free resources */
1768 loc_cpu_entry = private->entries;
1769 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1770 cleanup_entry(iter, net);
1771 if (private->number > private->initial_entries)
1772 module_put(table_owner);
1773 xt_free_table_info(private);
1774}
1775
Florian Westphala67dd262016-02-25 10:08:35 +01001776int ipt_register_table(struct net *net, const struct xt_table *table,
1777 const struct ipt_replace *repl,
1778 const struct nf_hook_ops *ops, struct xt_table **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001781 struct xt_table_info *newinfo;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001782 struct xt_table_info bootstrap = {0};
Eric Dumazet31836062005-12-13 23:13:48 -08001783 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08001784 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Harald Welte2e4e6a12006-01-12 13:30:04 -08001786 newinfo = xt_alloc_table_info(repl->size);
Florian Westphala67dd262016-02-25 10:08:35 +01001787 if (!newinfo)
1788 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Florian Westphal482cfc32015-06-11 01:34:55 +02001790 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -08001791 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Jan Engelhardt0f234212010-02-24 18:36:04 +01001793 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001794 if (ret != 0)
1795 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001797 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001798 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001799 ret = PTR_ERR(new_table);
1800 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802
Florian Westphalb9e69e12016-02-25 10:08:36 +01001803 /* set res now, will see skbs right after nf_register_net_hooks */
Florian Westphala67dd262016-02-25 10:08:35 +01001804 WRITE_ONCE(*res, new_table);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001805
1806 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1807 if (ret != 0) {
1808 __ipt_unregister_table(net, new_table);
1809 *res = NULL;
1810 }
1811
Florian Westphala67dd262016-02-25 10:08:35 +01001812 return ret;
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001813
1814out_free:
1815 xt_free_table_info(newinfo);
Florian Westphala67dd262016-02-25 10:08:35 +01001816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Florian Westphala67dd262016-02-25 10:08:35 +01001819void ipt_unregister_table(struct net *net, struct xt_table *table,
1820 const struct nf_hook_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Florian Westphalb9e69e12016-02-25 10:08:36 +01001822 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1823 __ipt_unregister_table(net, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
1826/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001827static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1829 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001830 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Patrick McHardy9c547952007-12-17 21:52:00 -08001832 return ((test_type == 0xFF) ||
1833 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 ^ invert;
1835}
1836
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001837static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +02001838icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Jan Engelhardt5452e422008-04-14 11:15:35 +02001840 const struct icmphdr *ic;
1841 struct icmphdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001842 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001845 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001846 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001848 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (ic == NULL) {
1850 /* We've been asked to examine this packet, and we
1851 * can't. Hence, no choice but to drop.
1852 */
Jan Engelhardtb4ba2612009-07-07 20:54:30 +02001853 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001854 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856
1857 return icmp_type_code_match(icmpinfo->type,
1858 icmpinfo->code[0],
1859 icmpinfo->code[1],
1860 ic->type, ic->code,
1861 !!(icmpinfo->invflags&IPT_ICMP_INV));
1862}
1863
Jan Engelhardtb0f38452010-03-19 17:16:42 +01001864static int icmp_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001866 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Patrick McHardy1d5cd902006-03-20 18:01:14 -08001868 /* Must specify no unknown invflags */
Jan Engelhardtbd414ee2010-03-23 16:35:56 +01001869 return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Jan Engelhardt45385062009-07-04 12:50:00 +02001872static struct xt_target ipt_builtin_tg[] __read_mostly = {
1873 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001874 .name = XT_STANDARD_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001875 .targetsize = sizeof(int),
1876 .family = NFPROTO_IPV4,
Dmitry Mishin27229712006-04-01 02:25:19 -08001877#ifdef CONFIG_COMPAT
Jan Engelhardt45385062009-07-04 12:50:00 +02001878 .compatsize = sizeof(compat_int_t),
1879 .compat_from_user = compat_standard_from_user,
1880 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08001881#endif
Jan Engelhardt45385062009-07-04 12:50:00 +02001882 },
1883 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001884 .name = XT_ERROR_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001885 .target = ipt_error,
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001886 .targetsize = XT_FUNCTION_MAXNAMELEN,
Jan Engelhardt45385062009-07-04 12:50:00 +02001887 .family = NFPROTO_IPV4,
1888 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889};
1890
1891static struct nf_sockopt_ops ipt_sockopts = {
1892 .pf = PF_INET,
1893 .set_optmin = IPT_BASE_CTL,
1894 .set_optmax = IPT_SO_SET_MAX+1,
1895 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08001896#ifdef CONFIG_COMPAT
1897 .compat_set = compat_do_ipt_set_ctl,
1898#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 .get_optmin = IPT_BASE_CTL,
1900 .get_optmax = IPT_SO_GET_MAX+1,
1901 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08001902#ifdef CONFIG_COMPAT
1903 .compat_get = compat_do_ipt_get_ctl,
1904#endif
Neil Horman16fcec32007-09-11 11:28:26 +02001905 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906};
1907
Jan Engelhardt45385062009-07-04 12:50:00 +02001908static struct xt_match ipt_builtin_mt[] __read_mostly = {
1909 {
1910 .name = "icmp",
1911 .match = icmp_match,
1912 .matchsize = sizeof(struct ipt_icmp),
1913 .checkentry = icmp_checkentry,
1914 .proto = IPPROTO_ICMP,
1915 .family = NFPROTO_IPV4,
Florian Westphal894b7532018-07-04 20:25:32 +02001916 .me = THIS_MODULE,
Jan Engelhardt45385062009-07-04 12:50:00 +02001917 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918};
1919
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001920static int __net_init ip_tables_net_init(struct net *net)
1921{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02001922 return xt_proto_init(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001923}
1924
1925static void __net_exit ip_tables_net_exit(struct net *net)
1926{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02001927 xt_proto_fini(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001928}
1929
1930static struct pernet_operations ip_tables_net_ops = {
1931 .init = ip_tables_net_init,
1932 .exit = ip_tables_net_exit,
1933};
1934
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001935static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 int ret;
1938
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001939 ret = register_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001940 if (ret < 0)
1941 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001942
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001943 /* No one else will be downing sem now, so we won't sleep */
Jan Engelhardt45385062009-07-04 12:50:00 +02001944 ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001945 if (ret < 0)
1946 goto err2;
Jan Engelhardt45385062009-07-04 12:50:00 +02001947 ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001948 if (ret < 0)
1949 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 /* Register setsockopt */
1952 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001953 if (ret < 0)
1954 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01001956 pr_info("(C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001958
1959err5:
Jan Engelhardt45385062009-07-04 12:50:00 +02001960 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001961err4:
Jan Engelhardt45385062009-07-04 12:50:00 +02001962 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001963err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001964 unregister_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001965err1:
1966 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001969static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001972
Jan Engelhardt45385062009-07-04 12:50:00 +02001973 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
1974 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001975 unregister_pernet_subsys(&ip_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
1978EXPORT_SYMBOL(ipt_register_table);
1979EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001981module_init(ip_tables_init);
1982module_exit(ip_tables_fini);