blob: 7c00ce90adb8496ed9fb97894163f783b158e7cf [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) &&
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200348 !(e->ip.flags & IPT_F_GOTO))
Florian Westphal7814b6e2015-07-14 17:51:08 +0200349 jumpstack[stackidx++] = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200351 e = get_entry(table_base, v);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200352 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200354
Jan Engelhardtde74c162009-07-05 18:26:37 +0200355 acpar.target = t->u.kernel.target;
356 acpar.targinfo = t->data;
Jan Engelhardtbb70dfa2009-04-15 21:40:13 +0200357
Jan Engelhardtde74c162009-07-05 18:26:37 +0200358 verdict = t->u.kernel.target->target(skb, &acpar);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200359 /* Target might have changed stuff. */
360 ip = ip_hdr(skb);
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200361 if (verdict == XT_CONTINUE)
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200362 e = ipt_next_entry(e);
363 else
364 /* Verdict */
365 break;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200366 } while (!acpar.hotdrop);
Florian Westphal7814b6e2015-07-14 17:51:08 +0200367
Ian Morris24cebe32015-10-14 23:17:07 +0100368 xt_write_recseq_end(addend);
369 local_bh_enable();
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200370
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200371 if (acpar.hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return NF_DROP;
373 else return verdict;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/* Figures out from what hook each rule can be called: returns 0 if
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200377 there are loops. Puts hook bitmask in comefrom. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static int
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200379mark_source_chains(const struct xt_table_info *newinfo,
Florian Westphalf4dc7772016-07-14 17:51:26 +0200380 unsigned int valid_hooks, void *entry0,
381 unsigned int *offsets)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 unsigned int hook;
384
385 /* No recursion; use packet counter to save back ptrs (reset
386 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800387 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800389 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (!(valid_hooks & (1 << hook)))
392 continue;
393
394 /* Set initial back pointer. */
395 e->counters.pcnt = pos;
396
397 for (;;) {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200398 const struct xt_standard_target *t
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200399 = (void *)ipt_get_target_c(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800400 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200402 if (e->comefrom & (1 << NF_INET_NUMHOOKS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return 0;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200404
Patrick McHardy9c547952007-12-17 21:52:00 -0800405 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* Unconditional return/END. */
Florian Westphal54d83fc2016-03-22 18:02:52 +0100408 if ((unconditional(e) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100409 (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200410 XT_STANDARD_TARGET) == 0) &&
Florian Westphal54d83fc2016-03-22 18:02:52 +0100411 t->verdict < 0) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 unsigned int oldpos, size;
413
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100414 if ((strcmp(t->target.u.user.name,
Ian Morris24cebe32015-10-14 23:17:07 +0100415 XT_STANDARD_TARGET) == 0) &&
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200416 t->verdict < -NF_MAX_VERDICT - 1)
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800417 return 0;
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* Return: backtrack through the last
420 big jump. */
421 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800422 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 oldpos = pos;
424 pos = e->counters.pcnt;
425 e->counters.pcnt = 0;
426
427 /* We're at the start. */
428 if (pos == oldpos)
429 goto next;
430
431 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800432 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 } while (oldpos == pos + e->next_offset);
434
435 /* Move along one */
436 size = e->next_offset;
437 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800438 (entry0 + pos + size);
Florian Westphalf24e2302016-04-01 14:17:21 +0200439 if (pos + size >= newinfo->size)
440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 e->counters.pcnt = pos;
442 pos += size;
443 } else {
444 int newpos = t->verdict;
445
446 if (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200447 XT_STANDARD_TARGET) == 0 &&
Joe Perches3666ed12009-11-23 23:17:06 +0100448 newpos >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* This a jump; chase it. */
Florian Westphalf4dc7772016-07-14 17:51:26 +0200450 if (!xt_find_jump_offset(offsets, newpos,
451 newinfo->number))
452 return 0;
Florian Westphal36472342016-04-01 14:17:22 +0200453 e = (struct ipt_entry *)
454 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 } else {
456 /* ... this is a fallthru */
457 newpos = pos + e->next_offset;
Florian Westphalf24e2302016-04-01 14:17:21 +0200458 if (newpos >= newinfo->size)
459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 e = (struct ipt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800462 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 e->counters.pcnt = pos;
464 pos = newpos;
465 }
466 }
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200467next: ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 return 1;
470}
471
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200472static void cleanup_match(struct xt_entry_match *m, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200474 struct xt_mtdtor_param par;
475
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100476 par.net = net;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200477 par.match = m->u.kernel.match;
478 par.matchinfo = m->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200479 par.family = NFPROTO_IPV4;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200480 if (par.match->destroy != NULL)
481 par.match->destroy(&par);
482 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Denys Vlasenko022748a2008-01-14 23:44:05 -0800485static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200486check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Dmitry Mishina96be242006-12-12 00:29:26 -0800487{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200488 const struct ipt_ip *ip = par->entryinfo;
Dmitry Mishina96be242006-12-12 00:29:26 -0800489
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200490 par->match = m->u.kernel.match;
491 par->matchinfo = m->data;
492
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200493 return xt_check_match(par, m->u.match_size - sizeof(*m),
494 ip->proto, ip->invflags & IPT_INV_PROTO);
Dmitry Mishina96be242006-12-12 00:29:26 -0800495}
496
Denys Vlasenko022748a2008-01-14 23:44:05 -0800497static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200498find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800500 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800501 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200503 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
504 m->u.user.revision);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200505 if (IS_ERR(match))
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200506 return PTR_ERR(match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 m->u.kernel.match = match;
508
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100509 ret = check_match(m, par);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800510 if (ret)
511 goto err;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800514err:
515 module_put(m->u.kernel.match->me);
516 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Patrick McHardyadd67462010-02-03 13:45:12 +0100519static int check_target(struct ipt_entry *e, struct net *net, const char *name)
Dmitry Mishina96be242006-12-12 00:29:26 -0800520{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200521 struct xt_entry_target *t = ipt_get_target(e);
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200522 struct xt_tgchk_param par = {
Patrick McHardyadd67462010-02-03 13:45:12 +0100523 .net = net,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200524 .table = name,
525 .entryinfo = e,
526 .target = t->u.kernel.target,
527 .targinfo = t->data,
528 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200529 .family = NFPROTO_IPV4,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200530 };
Dmitry Mishina96be242006-12-12 00:29:26 -0800531
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200532 return xt_check_target(&par, t->u.target_size - sizeof(*t),
533 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
Dmitry Mishina96be242006-12-12 00:29:26 -0800534}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Denys Vlasenko022748a2008-01-14 23:44:05 -0800536static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100537find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
Jan Engelhardt05595182010-02-24 18:33:43 +0100538 unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200540 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800541 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 int ret;
543 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200544 struct xt_mtchk_param mtpar;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100545 struct xt_entry_match *ematch;
Pablo Neira Ayuso92b44232016-04-29 10:39:34 +0200546 unsigned long pcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Pablo Neira Ayuso92b44232016-04-29 10:39:34 +0200548 pcnt = xt_percpu_counter_alloc();
549 if (IS_ERR_VALUE(pcnt))
Florian Westphal71ae0df2015-06-11 01:34:54 +0200550 return -ENOMEM;
Pablo Neira Ayuso92b44232016-04-29 10:39:34 +0200551 e->counters.pcnt = pcnt;
Florian Westphal71ae0df2015-06-11 01:34:54 +0200552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 j = 0;
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100554 mtpar.net = net;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200555 mtpar.table = name;
556 mtpar.entryinfo = &e->ip;
557 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200558 mtpar.family = NFPROTO_IPV4;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100559 xt_ematch_foreach(ematch, e) {
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100560 ret = find_check_match(ematch, &mtpar);
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100561 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100562 goto cleanup_matches;
563 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 t = ipt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200567 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
568 t->u.user.revision);
569 if (IS_ERR(target)) {
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200570 ret = PTR_ERR(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 goto cleanup_matches;
572 }
573 t->u.kernel.target = target;
574
Patrick McHardyadd67462010-02-03 13:45:12 +0100575 ret = check_target(e, net, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800576 if (ret)
577 goto err;
Florian Westphal71ae0df2015-06-11 01:34:54 +0200578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800580 err:
581 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 cleanup_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100583 xt_ematch_foreach(ematch, e) {
584 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100585 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100586 cleanup_match(ematch, net);
587 }
Florian Westphal71ae0df2015-06-11 01:34:54 +0200588
589 xt_percpu_counter_free(e->counters.pcnt);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return ret;
592}
593
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200594static bool check_underflow(const struct ipt_entry *e)
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200595{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200596 const struct xt_entry_target *t;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200597 unsigned int verdict;
598
Florian Westphal54d83fc2016-03-22 18:02:52 +0100599 if (!unconditional(e))
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200600 return false;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200601 t = ipt_get_target_c(e);
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200602 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
603 return false;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200604 verdict = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200605 verdict = -verdict - 1;
606 return verdict == NF_DROP || verdict == NF_ACCEPT;
607}
608
Denys Vlasenko022748a2008-01-14 23:44:05 -0800609static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610check_entry_size_and_hooks(struct ipt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800611 struct xt_table_info *newinfo,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200612 const unsigned char *base,
613 const unsigned char *limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 const unsigned int *hook_entries,
615 const unsigned int *underflows,
Jan Engelhardt05595182010-02-24 18:33:43 +0100616 unsigned int valid_hooks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 unsigned int h;
Florian Westphalbdf533d2016-03-22 18:02:49 +0100619 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Joe Perches3666ed12009-11-23 23:17:06 +0100621 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
Florian Westphal6e94e0c2016-03-22 18:02:50 +0100622 (unsigned char *)e + sizeof(struct ipt_entry) >= limit ||
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200623 (unsigned char *)e + e->next_offset > limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 if (e->next_offset
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200627 < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Florian Westphalaa412ba2016-04-01 14:17:24 +0200630 if (!ip_checkentry(&e->ip))
631 return -EINVAL;
632
Florian Westphalce683e52016-04-01 14:17:28 +0200633 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
634 e->next_offset);
Florian Westphalbdf533d2016-03-22 18:02:49 +0100635 if (err)
636 return err;
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800639 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Jan Engelhardta7d51732009-07-18 14:52:58 +0200640 if (!(valid_hooks & (1 << h)))
641 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if ((unsigned char *)e - base == hook_entries[h])
643 newinfo->hook_entry[h] = hook_entries[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200644 if ((unsigned char *)e - base == underflows[h]) {
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200645 if (!check_underflow(e))
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200646 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 newinfo->underflow[h] = underflows[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800653 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 e->comefrom = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 0;
656}
657
Jan Engelhardt05595182010-02-24 18:33:43 +0100658static void
659cleanup_entry(struct ipt_entry *e, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200661 struct xt_tgdtor_param par;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200662 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100663 struct xt_entry_match *ematch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100666 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100667 cleanup_match(ematch, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 t = ipt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200669
Patrick McHardyadd67462010-02-03 13:45:12 +0100670 par.net = net;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200671 par.target = t->u.kernel.target;
672 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200673 par.family = NFPROTO_IPV4;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200674 if (par.target->destroy != NULL)
675 par.target->destroy(&par);
676 module_put(par.target->me);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200677 xt_percpu_counter_free(e->counters.pcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680/* Checks and translates the user-supplied table segment (held in
681 newinfo) */
682static int
Jan Engelhardt0f234212010-02-24 18:36:04 +0100683translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
Ian Morris6c282552015-10-14 23:17:06 +0100684 const struct ipt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100686 struct ipt_entry *iter;
Florian Westphalf4dc7772016-07-14 17:51:26 +0200687 unsigned int *offsets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 unsigned int i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100689 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jan Engelhardt0f234212010-02-24 18:36:04 +0100691 newinfo->size = repl->size;
692 newinfo->number = repl->num_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800695 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 newinfo->hook_entry[i] = 0xFFFFFFFF;
697 newinfo->underflow[i] = 0xFFFFFFFF;
698 }
699
Florian Westphalf4dc7772016-07-14 17:51:26 +0200700 offsets = xt_alloc_entry_offsets(newinfo->number);
701 if (!offsets)
702 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 i = 0;
704 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100705 xt_entry_foreach(iter, entry0, newinfo->size) {
706 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +0100707 entry0 + repl->size,
708 repl->hook_entry,
709 repl->underflow,
710 repl->valid_hooks);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100711 if (ret != 0)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200712 goto out_free;
713 if (i < repl->num_entries)
714 offsets[i] = (void *)iter - entry0;
Jan Engelhardt05595182010-02-24 18:33:43 +0100715 ++i;
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200716 if (strcmp(ipt_get_target(iter)->u.user.name,
717 XT_ERROR_TARGET) == 0)
718 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Florian Westphalf4dc7772016-07-14 17:51:26 +0200721 ret = -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200722 if (i != repl->num_entries)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200723 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800726 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* Only hooks which are valid */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100728 if (!(repl->valid_hooks & (1 << i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 continue;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200730 if (newinfo->hook_entry[i] == 0xFFFFFFFF)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200731 goto out_free;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200732 if (newinfo->underflow[i] == 0xFFFFFFFF)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200733 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
Florian Westphalf4dc7772016-07-14 17:51:26 +0200736 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
737 ret = -ELOOP;
738 goto out_free;
739 }
740 kvfree(offsets);
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* Finally, each sanity check must pass */
743 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100744 xt_entry_foreach(iter, entry0, newinfo->size) {
Jan Engelhardt0f234212010-02-24 18:36:04 +0100745 ret = find_check_entry(iter, net, repl->name, repl->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100746 if (ret != 0)
747 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100748 ++i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800751 if (ret != 0) {
Jan Engelhardt05595182010-02-24 18:33:43 +0100752 xt_entry_foreach(iter, entry0, newinfo->size) {
753 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100754 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100755 cleanup_entry(iter, net);
756 }
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800757 return ret;
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return ret;
Florian Westphalf4dc7772016-07-14 17:51:26 +0200761 out_free:
762 kvfree(offsets);
763 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800767get_counters(const struct xt_table_info *t,
768 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100770 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 unsigned int cpu;
772 unsigned int i;
773
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700774 for_each_possible_cpu(cpu) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200775 seqcount_t *s = &per_cpu(xt_recseq, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 i = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200778 xt_entry_foreach(iter, t->entries, t->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +0200779 struct xt_counters *tmp;
Eric Dumazet83723d62011-01-10 20:11:38 +0100780 u64 bcnt, pcnt;
781 unsigned int start;
782
Florian Westphal71ae0df2015-06-11 01:34:54 +0200783 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100784 do {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200785 start = read_seqcount_begin(s);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200786 bcnt = tmp->bcnt;
787 pcnt = tmp->pcnt;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200788 } while (read_seqcount_retry(s, start));
Eric Dumazet83723d62011-01-10 20:11:38 +0100789
790 ADD_COUNTER(counters[i], bcnt, pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +0100791 ++i; /* macro does multi eval of i */
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100794}
795
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200796static struct xt_counters *alloc_counters(const struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Dmitry Mishin27229712006-04-01 02:25:19 -0800798 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800799 struct xt_counters *counters;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200800 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /* We need atomic snapshot of counters: rest doesn't change
803 (other than comefrom, which userspace doesn't care
804 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800805 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet83723d62011-01-10 20:11:38 +0100806 counters = vzalloc(countersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700809 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700811 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Dmitry Mishin27229712006-04-01 02:25:19 -0800813 return counters;
814}
815
816static int
817copy_entries_to_user(unsigned int total_size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200818 const struct xt_table *table,
Dmitry Mishin27229712006-04-01 02:25:19 -0800819 void __user *userptr)
820{
821 unsigned int off, num;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200822 const struct ipt_entry *e;
Dmitry Mishin27229712006-04-01 02:25:19 -0800823 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200824 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800825 int ret = 0;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700826 const void *loc_cpu_entry;
Dmitry Mishin27229712006-04-01 02:25:19 -0800827
828 counters = alloc_counters(table);
829 if (IS_ERR(counters))
830 return PTR_ERR(counters);
831
Florian Westphal482cfc32015-06-11 01:34:55 +0200832 loc_cpu_entry = private->entries;
Eric Dumazet31836062005-12-13 23:13:48 -0800833 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 ret = -EFAULT;
835 goto free_counters;
836 }
837
838 /* FIXME: use iterator macros --RR */
839 /* ... then go back and fix counters and names */
840 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
841 unsigned int i;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200842 const struct xt_entry_match *m;
843 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Eric Dumazet31836062005-12-13 23:13:48 -0800845 e = (struct ipt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (copy_to_user(userptr + off
847 + offsetof(struct ipt_entry, counters),
848 &counters[num],
849 sizeof(counters[num])) != 0) {
850 ret = -EFAULT;
851 goto free_counters;
852 }
853
854 for (i = sizeof(struct ipt_entry);
855 i < e->target_offset;
856 i += m->u.match_size) {
857 m = (void *)e + i;
858
859 if (copy_to_user(userptr + off + i
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200860 + offsetof(struct xt_entry_match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 u.user.name),
862 m->u.kernel.match->name,
863 strlen(m->u.kernel.match->name)+1)
864 != 0) {
865 ret = -EFAULT;
866 goto free_counters;
867 }
868 }
869
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200870 t = ipt_get_target_c(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (copy_to_user(userptr + off + e->target_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200872 + offsetof(struct xt_entry_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 u.user.name),
874 t->u.kernel.target->name,
875 strlen(t->u.kernel.target->name)+1) != 0) {
876 ret = -EFAULT;
877 goto free_counters;
878 }
879 }
880
881 free_counters:
882 vfree(counters);
883 return ret;
884}
885
Dmitry Mishin27229712006-04-01 02:25:19 -0800886#ifdef CONFIG_COMPAT
Jan Engelhardt739674f2009-06-26 08:23:19 +0200887static void compat_standard_from_user(void *dst, const void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -0800888{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700889 int v = *(compat_int_t *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -0800890
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700891 if (v > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800892 v += xt_compat_calc_jump(AF_INET, v);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700893 memcpy(dst, &v, sizeof(v));
894}
895
Jan Engelhardt739674f2009-06-26 08:23:19 +0200896static int compat_standard_to_user(void __user *dst, const void *src)
Dmitry Mishin27229712006-04-01 02:25:19 -0800897{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700898 compat_int_t cv = *(int *)src;
Dmitry Mishin27229712006-04-01 02:25:19 -0800899
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700900 if (cv > 0)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800901 cv -= xt_compat_calc_jump(AF_INET, cv);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700902 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
Dmitry Mishin27229712006-04-01 02:25:19 -0800903}
904
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200905static int compat_calc_entry(const struct ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -0800906 const struct xt_table_info *info,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200907 const void *base, struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -0800908{
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100909 const struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200910 const struct xt_entry_target *t;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -0800911 unsigned int entry_offset;
Dmitry Mishin27229712006-04-01 02:25:19 -0800912 int off, i, ret;
913
Patrick McHardy30c08c42007-12-17 21:47:14 -0800914 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -0800915 entry_offset = (void *)e - base;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100916 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100917 off += xt_compat_match_offset(ematch->u.kernel.match);
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200918 t = ipt_get_target_c(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700919 off += xt_compat_target_offset(t->u.kernel.target);
Dmitry Mishin27229712006-04-01 02:25:19 -0800920 newinfo->size -= off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800921 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -0800922 if (ret)
923 return ret;
924
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800925 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Patrick McHardy4b478242007-12-17 21:46:15 -0800926 if (info->hook_entry[i] &&
927 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -0800928 newinfo->hook_entry[i] -= off;
Patrick McHardy4b478242007-12-17 21:46:15 -0800929 if (info->underflow[i] &&
930 (e < (struct ipt_entry *)(base + info->underflow[i])))
Dmitry Mishin27229712006-04-01 02:25:19 -0800931 newinfo->underflow[i] -= off;
932 }
933 return 0;
934}
935
Eric Dumazet259d4e42007-12-04 23:24:56 -0800936static int compat_table_info(const struct xt_table_info *info,
Patrick McHardy4b478242007-12-17 21:46:15 -0800937 struct xt_table_info *newinfo)
Dmitry Mishin27229712006-04-01 02:25:19 -0800938{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100939 struct ipt_entry *iter;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700940 const void *loc_cpu_entry;
Jan Engelhardt05595182010-02-24 18:33:43 +0100941 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -0800942
943 if (!newinfo || !info)
944 return -EINVAL;
945
Florian Westphal482cfc32015-06-11 01:34:55 +0200946 /* we dont care about newinfo->entries */
Eric Dumazet259d4e42007-12-04 23:24:56 -0800947 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
948 newinfo->initial_entries = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200949 loc_cpu_entry = info->entries;
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100950 xt_compat_init_offsets(AF_INET, info->number);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100951 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
952 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
953 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +0100954 return ret;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100955 }
Jan Engelhardt05595182010-02-24 18:33:43 +0100956 return 0;
Dmitry Mishin27229712006-04-01 02:25:19 -0800957}
958#endif
959
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200960static int get_info(struct net *net, void __user *user,
Ian Morris6c282552015-10-14 23:17:06 +0100961 const int *len, int compat)
Dmitry Mishin27229712006-04-01 02:25:19 -0800962{
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200963 char name[XT_TABLE_MAXNAMELEN];
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800964 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -0800965 int ret;
966
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200967 if (*len != sizeof(struct ipt_getinfo))
Dmitry Mishin27229712006-04-01 02:25:19 -0800968 return -EINVAL;
Dmitry Mishin27229712006-04-01 02:25:19 -0800969
970 if (copy_from_user(name, user, sizeof(name)) != 0)
971 return -EFAULT;
972
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200973 name[XT_TABLE_MAXNAMELEN-1] = '\0';
Dmitry Mishin27229712006-04-01 02:25:19 -0800974#ifdef CONFIG_COMPAT
975 if (compat)
976 xt_compat_lock(AF_INET);
977#endif
Alexey Dobriyan34bd1372008-01-31 04:03:03 -0800978 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Patrick McHardy4b478242007-12-17 21:46:15 -0800979 "iptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +0000980 if (!IS_ERR_OR_NULL(t)) {
Dmitry Mishin27229712006-04-01 02:25:19 -0800981 struct ipt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200982 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -0800983#ifdef CONFIG_COMPAT
Alexey Dobriyan14c7dbe2010-02-08 11:17:43 -0800984 struct xt_table_info tmp;
985
Dmitry Mishin27229712006-04-01 02:25:19 -0800986 if (compat) {
Dmitry Mishin27229712006-04-01 02:25:19 -0800987 ret = compat_table_info(private, &tmp);
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800988 xt_compat_flush_offsets(AF_INET);
Patrick McHardy4b478242007-12-17 21:46:15 -0800989 private = &tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -0800990 }
991#endif
Vasiliy Kulikovb5f15ac2010-11-03 08:45:06 +0100992 memset(&info, 0, sizeof(info));
Dmitry Mishin27229712006-04-01 02:25:19 -0800993 info.valid_hooks = t->valid_hooks;
994 memcpy(info.hook_entry, private->hook_entry,
Patrick McHardy4b478242007-12-17 21:46:15 -0800995 sizeof(info.hook_entry));
Dmitry Mishin27229712006-04-01 02:25:19 -0800996 memcpy(info.underflow, private->underflow,
Patrick McHardy4b478242007-12-17 21:46:15 -0800997 sizeof(info.underflow));
Dmitry Mishin27229712006-04-01 02:25:19 -0800998 info.num_entries = private->number;
999 info.size = private->size;
1000 strcpy(info.name, name);
1001
1002 if (copy_to_user(user, &info, *len) != 0)
1003 ret = -EFAULT;
1004 else
1005 ret = 0;
1006
1007 xt_table_unlock(t);
1008 module_put(t->me);
1009 } else
1010 ret = t ? PTR_ERR(t) : -ENOENT;
1011#ifdef CONFIG_COMPAT
1012 if (compat)
1013 xt_compat_unlock(AF_INET);
1014#endif
1015 return ret;
1016}
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001019get_entries(struct net *net, struct ipt_get_entries __user *uptr,
1020 const int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001023 struct ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001024 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001026 if (*len < sizeof(get))
Dmitry Mishin27229712006-04-01 02:25:19 -08001027 return -EINVAL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001028 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1029 return -EFAULT;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001030 if (*len != sizeof(struct ipt_get_entries) + get.size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001031 return -EINVAL;
Pablo Neira Ayusob301f252016-03-24 21:29:53 +01001032 get.name[sizeof(get.name) - 1] = '\0';
Dmitry Mishin27229712006-04-01 02:25:19 -08001033
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001034 t = xt_find_table_lock(net, AF_INET, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001035 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001036 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001037 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001038 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 t, uptr->entrytable);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001040 else
Patrick McHardy544473c2008-04-14 11:15:45 +02001041 ret = -EAGAIN;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001044 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 } else
1046 ret = t ? PTR_ERR(t) : -ENOENT;
1047
1048 return ret;
1049}
1050
1051static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001052__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy4b478242007-12-17 21:46:15 -08001053 struct xt_table_info *newinfo, unsigned int num_counters,
1054 void __user *counters_ptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001055{
1056 int ret;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001057 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001058 struct xt_table_info *oldinfo;
1059 struct xt_counters *counters;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001060 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001061
1062 ret = 0;
Eric Dumazet83723d62011-01-10 20:11:38 +01001063 counters = vzalloc(num_counters * sizeof(struct xt_counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001064 if (!counters) {
1065 ret = -ENOMEM;
1066 goto out;
1067 }
1068
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001069 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
Dmitry Mishin27229712006-04-01 02:25:19 -08001070 "iptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001071 if (IS_ERR_OR_NULL(t)) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001072 ret = t ? PTR_ERR(t) : -ENOENT;
1073 goto free_newinfo_counters_untrans;
1074 }
1075
1076 /* You lied! */
1077 if (valid_hooks != t->valid_hooks) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001078 ret = -EINVAL;
1079 goto put_module;
1080 }
1081
1082 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1083 if (!oldinfo)
1084 goto put_module;
1085
1086 /* Update module usage count based on number of rules */
Dmitry Mishin27229712006-04-01 02:25:19 -08001087 if ((oldinfo->number > oldinfo->initial_entries) ||
1088 (newinfo->number <= oldinfo->initial_entries))
1089 module_put(t->me);
1090 if ((oldinfo->number > oldinfo->initial_entries) &&
1091 (newinfo->number <= oldinfo->initial_entries))
1092 module_put(t->me);
1093
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001094 /* Get the old counters, and synchronize with replace */
Dmitry Mishin27229712006-04-01 02:25:19 -08001095 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001096
Dmitry Mishin27229712006-04-01 02:25:19 -08001097 /* Decrease module usage counts and free resource */
Florian Westphal482cfc32015-06-11 01:34:55 +02001098 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001099 cleanup_entry(iter, net);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001100
Dmitry Mishin27229712006-04-01 02:25:19 -08001101 xt_free_table_info(oldinfo);
1102 if (copy_to_user(counters_ptr, counters,
Thomas Grafc58dd2d2014-04-04 17:57:45 +02001103 sizeof(struct xt_counters) * num_counters) != 0) {
1104 /* Silent error, can't fail, new table is already in place */
1105 net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
1106 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001107 vfree(counters);
1108 xt_table_unlock(t);
1109 return ret;
1110
1111 put_module:
1112 module_put(t->me);
1113 xt_table_unlock(t);
1114 free_newinfo_counters_untrans:
1115 vfree(counters);
1116 out:
1117 return ret;
1118}
1119
1120static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001121do_replace(struct net *net, const void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 int ret;
1124 struct ipt_replace tmp;
Dmitry Mishin27229712006-04-01 02:25:19 -08001125 struct xt_table_info *newinfo;
1126 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001127 struct ipt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1130 return -EFAULT;
1131
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001132 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001133 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1134 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001135 if (tmp.num_counters == 0)
1136 return -EINVAL;
1137
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01001138 tmp.name[sizeof(tmp.name)-1] = 0;
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001139
Harald Welte2e4e6a12006-01-12 13:30:04 -08001140 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!newinfo)
1142 return -ENOMEM;
1143
Florian Westphal482cfc32015-06-11 01:34:55 +02001144 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -08001145 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 tmp.size) != 0) {
1147 ret = -EFAULT;
1148 goto free_newinfo;
1149 }
1150
Jan Engelhardt0f234212010-02-24 18:36:04 +01001151 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (ret != 0)
Dmitry Mishin27229712006-04-01 02:25:19 -08001153 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001155 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001156 tmp.num_counters, tmp.counters);
Dmitry Mishin27229712006-04-01 02:25:19 -08001157 if (ret)
1158 goto free_newinfo_untrans;
1159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Dmitry Mishin27229712006-04-01 02:25:19 -08001161 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001162 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001163 cleanup_entry(iter, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001165 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return ret;
1167}
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001170do_add_counters(struct net *net, const void __user *user,
Ian Morris6c282552015-10-14 23:17:06 +01001171 unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Florian Westphal482cfc32015-06-11 01:34:55 +02001173 unsigned int i;
Dmitry Mishin27229712006-04-01 02:25:19 -08001174 struct xt_counters_info tmp;
1175 struct xt_counters *paddc;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001176 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001177 const struct xt_table_info *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 int ret = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001179 struct ipt_entry *iter;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001180 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Florian Westphald7591f02016-04-01 15:37:59 +02001182 paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1183 if (IS_ERR(paddc))
1184 return PTR_ERR(paddc);
Dmitry Mishin27229712006-04-01 02:25:19 -08001185
Florian Westphald7591f02016-04-01 15:37:59 +02001186 t = xt_find_table_lock(net, AF_INET, tmp.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001187 if (IS_ERR_OR_NULL(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 ret = t ? PTR_ERR(t) : -ENOENT;
1189 goto free;
1190 }
1191
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001192 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001193 private = t->private;
Florian Westphald7591f02016-04-01 15:37:59 +02001194 if (private->number != tmp.num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 ret = -EINVAL;
1196 goto unlock_up_free;
1197 }
1198
1199 i = 0;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001200 addend = xt_write_recseq_begin();
Florian Westphal482cfc32015-06-11 01:34:55 +02001201 xt_entry_foreach(iter, private->entries, private->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +02001202 struct xt_counters *tmp;
1203
1204 tmp = xt_get_this_cpu_counter(&iter->counters);
1205 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +01001206 ++i;
1207 }
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001208 xt_write_recseq_end(addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001210 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001211 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 module_put(t->me);
1213 free:
1214 vfree(paddc);
1215
1216 return ret;
1217}
1218
Dmitry Mishin27229712006-04-01 02:25:19 -08001219#ifdef CONFIG_COMPAT
1220struct compat_ipt_replace {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001221 char name[XT_TABLE_MAXNAMELEN];
Dmitry Mishin27229712006-04-01 02:25:19 -08001222 u32 valid_hooks;
1223 u32 num_entries;
1224 u32 size;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001225 u32 hook_entry[NF_INET_NUMHOOKS];
1226 u32 underflow[NF_INET_NUMHOOKS];
Dmitry Mishin27229712006-04-01 02:25:19 -08001227 u32 num_counters;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001228 compat_uptr_t counters; /* struct xt_counters * */
Dmitry Mishin27229712006-04-01 02:25:19 -08001229 struct compat_ipt_entry entries[0];
1230};
1231
Patrick McHardya18aa312007-12-12 10:35:16 -08001232static int
1233compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001234 unsigned int *size, struct xt_counters *counters,
Jan Engelhardt05595182010-02-24 18:33:43 +01001235 unsigned int i)
Dmitry Mishin27229712006-04-01 02:25:19 -08001236{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001237 struct xt_entry_target *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001238 struct compat_ipt_entry __user *ce;
1239 u_int16_t target_offset, next_offset;
1240 compat_uint_t origsize;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001241 const struct xt_entry_match *ematch;
1242 int ret = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001243
Dmitry Mishin27229712006-04-01 02:25:19 -08001244 origsize = *size;
1245 ce = (struct compat_ipt_entry __user *)*dstptr;
Jan Engelhardt05595182010-02-24 18:33:43 +01001246 if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
1247 copy_to_user(&ce->counters, &counters[i],
1248 sizeof(counters[i])) != 0)
1249 return -EFAULT;
Patrick McHardya18aa312007-12-12 10:35:16 -08001250
Dmitry Mishin27229712006-04-01 02:25:19 -08001251 *dstptr += sizeof(struct compat_ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001252 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1253
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001254 xt_ematch_foreach(ematch, e) {
1255 ret = xt_compat_match_to_user(ematch, dstptr, size);
1256 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001257 return ret;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001258 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001259 target_offset = e->target_offset - (origsize - *size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001260 t = ipt_get_target(e);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001261 ret = xt_compat_target_to_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001262 if (ret)
Jan Engelhardt05595182010-02-24 18:33:43 +01001263 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001264 next_offset = e->next_offset - (origsize - *size);
Jan Engelhardt05595182010-02-24 18:33:43 +01001265 if (put_user(target_offset, &ce->target_offset) != 0 ||
1266 put_user(next_offset, &ce->next_offset) != 0)
1267 return -EFAULT;
Dmitry Mishin27229712006-04-01 02:25:19 -08001268 return 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001269}
1270
Denys Vlasenko022748a2008-01-14 23:44:05 -08001271static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001272compat_find_calc_match(struct xt_entry_match *m,
Patrick McHardy4b478242007-12-17 21:46:15 -08001273 const struct ipt_ip *ip,
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001274 int *size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001275{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001276 struct xt_match *match;
Dmitry Mishin27229712006-04-01 02:25:19 -08001277
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001278 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
1279 m->u.user.revision);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001280 if (IS_ERR(match))
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001281 return PTR_ERR(match);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001282
Dmitry Mishin27229712006-04-01 02:25:19 -08001283 m->u.kernel.match = match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001284 *size += xt_compat_match_offset(match);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001285 return 0;
1286}
1287
Jan Engelhardt05595182010-02-24 18:33:43 +01001288static void compat_release_entry(struct compat_ipt_entry *e)
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001289{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001290 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001291 struct xt_entry_match *ematch;
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001292
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001293 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001294 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001295 module_put(ematch->u.kernel.match->me);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001296 t = compat_ipt_get_target(e);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001297 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001298}
1299
Denys Vlasenko022748a2008-01-14 23:44:05 -08001300static int
Patrick McHardy73cd5982007-12-17 21:47:32 -08001301check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
Patrick McHardy4b478242007-12-17 21:46:15 -08001302 struct xt_table_info *newinfo,
1303 unsigned int *size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001304 const unsigned char *base,
Florian Westphal09d96862016-04-01 14:17:34 +02001305 const unsigned char *limit)
Dmitry Mishin27229712006-04-01 02:25:19 -08001306{
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001307 struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001308 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001309 struct xt_target *target;
Dmitry Mishine5b5ef72007-01-04 12:14:41 -08001310 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001311 unsigned int j;
Florian Westphal09d96862016-04-01 14:17:34 +02001312 int ret, off;
Dmitry Mishin27229712006-04-01 02:25:19 -08001313
Joe Perches3666ed12009-11-23 23:17:06 +01001314 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
Florian Westphal6e94e0c2016-03-22 18:02:50 +01001315 (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit ||
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001316 (unsigned char *)e + e->next_offset > limit)
Dmitry Mishin27229712006-04-01 02:25:19 -08001317 return -EINVAL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001318
1319 if (e->next_offset < sizeof(struct compat_ipt_entry) +
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001320 sizeof(struct compat_xt_entry_target))
Dmitry Mishin27229712006-04-01 02:25:19 -08001321 return -EINVAL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001322
Florian Westphalaa412ba2016-04-01 14:17:24 +02001323 if (!ip_checkentry(&e->ip))
1324 return -EINVAL;
1325
Florian Westphalce683e52016-04-01 14:17:28 +02001326 ret = xt_compat_check_entry_offsets(e, e->elems,
Florian Westphalfc1221b2016-04-01 14:17:26 +02001327 e->target_offset, e->next_offset);
Dmitry Mishina96be242006-12-12 00:29:26 -08001328 if (ret)
1329 return ret;
Dmitry Mishin590bdf72006-10-30 15:12:55 -08001330
Patrick McHardy30c08c42007-12-17 21:47:14 -08001331 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
Dmitry Mishin27229712006-04-01 02:25:19 -08001332 entry_offset = (void *)e - (void *)base;
1333 j = 0;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001334 xt_ematch_foreach(ematch, e) {
Florian Westphal7d3f8432016-04-01 14:17:30 +02001335 ret = compat_find_calc_match(ematch, &e->ip, &off);
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001336 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001337 goto release_matches;
1338 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001339 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001340
Patrick McHardy73cd5982007-12-17 21:47:32 -08001341 t = compat_ipt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001342 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
1343 t->u.user.revision);
1344 if (IS_ERR(target)) {
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001345 ret = PTR_ERR(target);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001346 goto release_matches;
Dmitry Mishin27229712006-04-01 02:25:19 -08001347 }
1348 t->u.kernel.target = target;
1349
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001350 off += xt_compat_target_offset(target);
Dmitry Mishin27229712006-04-01 02:25:19 -08001351 *size += off;
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001352 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
Dmitry Mishin27229712006-04-01 02:25:19 -08001353 if (ret)
1354 goto out;
1355
Dmitry Mishin27229712006-04-01 02:25:19 -08001356 return 0;
Patrick McHardybec71b12006-09-20 12:04:08 -07001357
Dmitry Mishin27229712006-04-01 02:25:19 -08001358out:
Patrick McHardybec71b12006-09-20 12:04:08 -07001359 module_put(t->u.kernel.target->me);
Dmitry Mishin4c1b52b2007-06-05 12:56:09 -07001360release_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001361 xt_ematch_foreach(ematch, e) {
1362 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001363 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001364 module_put(ematch->u.kernel.match->me);
1365 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001366 return ret;
1367}
1368
Florian Westphal01883462016-04-01 14:17:33 +02001369static void
Patrick McHardy73cd5982007-12-17 21:47:32 -08001370compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
Florian Westphal7d3f8432016-04-01 14:17:30 +02001371 unsigned int *size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001372 struct xt_table_info *newinfo, unsigned char *base)
Dmitry Mishin27229712006-04-01 02:25:19 -08001373{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001374 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08001375 struct xt_target *target;
Dmitry Mishin27229712006-04-01 02:25:19 -08001376 struct ipt_entry *de;
1377 unsigned int origsize;
Florian Westphal01883462016-04-01 14:17:33 +02001378 int h;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001379 struct xt_entry_match *ematch;
Dmitry Mishin27229712006-04-01 02:25:19 -08001380
Dmitry Mishin27229712006-04-01 02:25:19 -08001381 origsize = *size;
1382 de = (struct ipt_entry *)*dstptr;
1383 memcpy(de, e, sizeof(struct ipt_entry));
Patrick McHardy73cd5982007-12-17 21:47:32 -08001384 memcpy(&de->counters, &e->counters, sizeof(e->counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001385
Patrick McHardy73cd5982007-12-17 21:47:32 -08001386 *dstptr += sizeof(struct ipt_entry);
Patrick McHardy30c08c42007-12-17 21:47:14 -08001387 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1388
Florian Westphal01883462016-04-01 14:17:33 +02001389 xt_ematch_foreach(ematch, e)
1390 xt_compat_match_from_user(ematch, dstptr, size);
1391
Dmitry Mishin27229712006-04-01 02:25:19 -08001392 de->target_offset = e->target_offset - (origsize - *size);
Patrick McHardy73cd5982007-12-17 21:47:32 -08001393 t = compat_ipt_get_target(e);
Dmitry Mishin27229712006-04-01 02:25:19 -08001394 target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001395 xt_compat_target_from_user(t, dstptr, size);
Dmitry Mishin27229712006-04-01 02:25:19 -08001396
1397 de->next_offset = e->next_offset - (origsize - *size);
Florian Westphal09d96862016-04-01 14:17:34 +02001398
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001399 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001400 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1401 newinfo->hook_entry[h] -= origsize - *size;
1402 if ((unsigned char *)de - base < newinfo->underflow[h])
1403 newinfo->underflow[h] -= origsize - *size;
1404 }
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001405}
Dmitry Mishin27229712006-04-01 02:25:19 -08001406
Denys Vlasenko022748a2008-01-14 23:44:05 -08001407static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +01001408translate_compat_table(struct net *net,
Patrick McHardy4b478242007-12-17 21:46:15 -08001409 struct xt_table_info **pinfo,
1410 void **pentry0,
Florian Westphal7d3f8432016-04-01 14:17:30 +02001411 const struct compat_ipt_replace *compatr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001412{
Dmitry Mishin920b8682006-10-30 15:14:27 -08001413 unsigned int i, j;
Dmitry Mishin27229712006-04-01 02:25:19 -08001414 struct xt_table_info *newinfo, *info;
1415 void *pos, *entry0, *entry1;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001416 struct compat_ipt_entry *iter0;
Florian Westphal09d96862016-04-01 14:17:34 +02001417 struct ipt_replace repl;
Dmitry Mishin27229712006-04-01 02:25:19 -08001418 unsigned int size;
Jan Engelhardt05595182010-02-24 18:33:43 +01001419 int ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001420
1421 info = *pinfo;
1422 entry0 = *pentry0;
Florian Westphal7d3f8432016-04-01 14:17:30 +02001423 size = compatr->size;
1424 info->number = compatr->num_entries;
Dmitry Mishin27229712006-04-01 02:25:19 -08001425
Dmitry Mishin920b8682006-10-30 15:14:27 -08001426 j = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001427 xt_compat_lock(AF_INET);
Florian Westphal7d3f8432016-04-01 14:17:30 +02001428 xt_compat_init_offsets(AF_INET, compatr->num_entries);
Dmitry Mishin27229712006-04-01 02:25:19 -08001429 /* Walk through entries, checking offsets. */
Florian Westphal7d3f8432016-04-01 14:17:30 +02001430 xt_entry_foreach(iter0, entry0, compatr->size) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001431 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001432 entry0,
Florian Westphal09d96862016-04-01 14:17:34 +02001433 entry0 + compatr->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001434 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001435 goto out_unlock;
1436 ++j;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001437 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001438
1439 ret = -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001440 if (j != compatr->num_entries)
Dmitry Mishin27229712006-04-01 02:25:19 -08001441 goto out_unlock;
Dmitry Mishin27229712006-04-01 02:25:19 -08001442
Dmitry Mishin27229712006-04-01 02:25:19 -08001443 ret = -ENOMEM;
1444 newinfo = xt_alloc_table_info(size);
1445 if (!newinfo)
1446 goto out_unlock;
1447
Florian Westphal7d3f8432016-04-01 14:17:30 +02001448 newinfo->number = compatr->num_entries;
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001449 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Florian Westphal09d96862016-04-01 14:17:34 +02001450 newinfo->hook_entry[i] = compatr->hook_entry[i];
1451 newinfo->underflow[i] = compatr->underflow[i];
Dmitry Mishin27229712006-04-01 02:25:19 -08001452 }
Florian Westphal482cfc32015-06-11 01:34:55 +02001453 entry1 = newinfo->entries;
Dmitry Mishin27229712006-04-01 02:25:19 -08001454 pos = entry1;
Florian Westphal7d3f8432016-04-01 14:17:30 +02001455 size = compatr->size;
Florian Westphal01883462016-04-01 14:17:33 +02001456 xt_entry_foreach(iter0, entry0, compatr->size)
1457 compat_copy_entry_from_user(iter0, &pos, &size,
1458 newinfo, entry1);
1459
Florian Westphal09d96862016-04-01 14:17:34 +02001460 /* all module references in entry0 are now gone.
1461 * entry1/newinfo contains a 64bit ruleset that looks exactly as
1462 * generated by 64bit userspace.
1463 *
1464 * Call standard translate_table() to validate all hook_entrys,
1465 * underflows, check for loops, etc.
1466 */
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001467 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001468 xt_compat_unlock(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001469
Florian Westphal09d96862016-04-01 14:17:34 +02001470 memcpy(&repl, compatr, sizeof(*compatr));
Dmitry Mishin27229712006-04-01 02:25:19 -08001471
Florian Westphal09d96862016-04-01 14:17:34 +02001472 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1473 repl.hook_entry[i] = newinfo->hook_entry[i];
1474 repl.underflow[i] = newinfo->underflow[i];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001475 }
Florian Westphal09d96862016-04-01 14:17:34 +02001476
1477 repl.num_counters = 0;
1478 repl.counters = NULL;
1479 repl.size = newinfo->size;
1480 ret = translate_table(net, newinfo, entry1, &repl);
1481 if (ret)
1482 goto free_newinfo;
Dmitry Mishinf6677f42006-12-05 13:44:07 -08001483
Dmitry Mishin27229712006-04-01 02:25:19 -08001484 *pinfo = newinfo;
1485 *pentry0 = entry1;
1486 xt_free_table_info(info);
1487 return 0;
1488
1489free_newinfo:
1490 xt_free_table_info(newinfo);
Florian Westphal09d96862016-04-01 14:17:34 +02001491 return ret;
1492out_unlock:
1493 xt_compat_flush_offsets(AF_INET);
1494 xt_compat_unlock(AF_INET);
Florian Westphal7d3f8432016-04-01 14:17:30 +02001495 xt_entry_foreach(iter0, entry0, compatr->size) {
Jan Engelhardt05595182010-02-24 18:33:43 +01001496 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001497 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001498 compat_release_entry(iter0);
1499 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001500 return ret;
Dmitry Mishin27229712006-04-01 02:25:19 -08001501}
1502
1503static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001504compat_do_replace(struct net *net, void __user *user, unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001505{
1506 int ret;
1507 struct compat_ipt_replace tmp;
1508 struct xt_table_info *newinfo;
1509 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001510 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001511
1512 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1513 return -EFAULT;
1514
Dmitry Mishin27229712006-04-01 02:25:19 -08001515 /* overflow check */
Dmitry Mishin27229712006-04-01 02:25:19 -08001516 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1517 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001518 if (tmp.num_counters == 0)
1519 return -EINVAL;
1520
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01001521 tmp.name[sizeof(tmp.name)-1] = 0;
Dmitry Mishin27229712006-04-01 02:25:19 -08001522
1523 newinfo = xt_alloc_table_info(tmp.size);
1524 if (!newinfo)
1525 return -ENOMEM;
1526
Florian Westphal482cfc32015-06-11 01:34:55 +02001527 loc_cpu_entry = newinfo->entries;
Dmitry Mishin27229712006-04-01 02:25:19 -08001528 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1529 tmp.size) != 0) {
1530 ret = -EFAULT;
1531 goto free_newinfo;
1532 }
1533
Florian Westphal7d3f8432016-04-01 14:17:30 +02001534 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
Dmitry Mishin27229712006-04-01 02:25:19 -08001535 if (ret != 0)
1536 goto free_newinfo;
1537
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001538 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy4b478242007-12-17 21:46:15 -08001539 tmp.num_counters, compat_ptr(tmp.counters));
Dmitry Mishin27229712006-04-01 02:25:19 -08001540 if (ret)
1541 goto free_newinfo_untrans;
1542 return 0;
1543
1544 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001545 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001546 cleanup_entry(iter, net);
Dmitry Mishin27229712006-04-01 02:25:19 -08001547 free_newinfo:
1548 xt_free_table_info(newinfo);
1549 return ret;
1550}
1551
1552static int
1553compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
Patrick McHardy4b478242007-12-17 21:46:15 -08001554 unsigned int len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001555{
1556 int ret;
1557
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001558 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Dmitry Mishin27229712006-04-01 02:25:19 -08001559 return -EPERM;
1560
1561 switch (cmd) {
1562 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001563 ret = compat_do_replace(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001564 break;
1565
1566 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001567 ret = do_add_counters(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001568 break;
1569
1570 default:
Dmitry Mishin27229712006-04-01 02:25:19 -08001571 ret = -EINVAL;
1572 }
1573
1574 return ret;
1575}
1576
Patrick McHardy4b478242007-12-17 21:46:15 -08001577struct compat_ipt_get_entries {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001578 char name[XT_TABLE_MAXNAMELEN];
Dmitry Mishin27229712006-04-01 02:25:19 -08001579 compat_uint_t size;
1580 struct compat_ipt_entry entrytable[0];
1581};
1582
Patrick McHardy4b478242007-12-17 21:46:15 -08001583static int
1584compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1585 void __user *userptr)
Dmitry Mishin27229712006-04-01 02:25:19 -08001586{
Dmitry Mishin27229712006-04-01 02:25:19 -08001587 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001588 const struct xt_table_info *private = table->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001589 void __user *pos;
1590 unsigned int size;
1591 int ret = 0;
Patrick McHardya18aa312007-12-12 10:35:16 -08001592 unsigned int i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001593 struct ipt_entry *iter;
Dmitry Mishin27229712006-04-01 02:25:19 -08001594
1595 counters = alloc_counters(table);
1596 if (IS_ERR(counters))
1597 return PTR_ERR(counters);
1598
Dmitry Mishin27229712006-04-01 02:25:19 -08001599 pos = userptr;
1600 size = total_size;
Florian Westphal482cfc32015-06-11 01:34:55 +02001601 xt_entry_foreach(iter, private->entries, total_size) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001602 ret = compat_copy_entry_to_user(iter, &pos,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001603 &size, counters, i++);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001604 if (ret != 0)
1605 break;
1606 }
Dmitry Mishin27229712006-04-01 02:25:19 -08001607
Dmitry Mishin27229712006-04-01 02:25:19 -08001608 vfree(counters);
1609 return ret;
1610}
1611
1612static int
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001613compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1614 int *len)
Dmitry Mishin27229712006-04-01 02:25:19 -08001615{
1616 int ret;
1617 struct compat_ipt_get_entries get;
Jan Engelhardte60a13e2007-02-07 15:12:33 -08001618 struct xt_table *t;
Dmitry Mishin27229712006-04-01 02:25:19 -08001619
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001620 if (*len < sizeof(get))
Dmitry Mishin27229712006-04-01 02:25:19 -08001621 return -EINVAL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001622
1623 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1624 return -EFAULT;
1625
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001626 if (*len != sizeof(struct compat_ipt_get_entries) + get.size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001627 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001628
Pablo Neira Ayusob301f252016-03-24 21:29:53 +01001629 get.name[sizeof(get.name) - 1] = '\0';
Dmitry Mishin27229712006-04-01 02:25:19 -08001630
1631 xt_compat_lock(AF_INET);
Alexey Dobriyan34bd1372008-01-31 04:03:03 -08001632 t = xt_find_table_lock(net, AF_INET, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001633 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001634 const struct xt_table_info *private = t->private;
Dmitry Mishin27229712006-04-01 02:25:19 -08001635 struct xt_table_info info;
Dmitry Mishin27229712006-04-01 02:25:19 -08001636 ret = compat_table_info(private, &info);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001637 if (!ret && get.size == info.size)
Dmitry Mishin27229712006-04-01 02:25:19 -08001638 ret = compat_copy_entries_to_user(private->size,
Patrick McHardy4b478242007-12-17 21:46:15 -08001639 t, uptr->entrytable);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001640 else if (!ret)
Patrick McHardy544473c2008-04-14 11:15:45 +02001641 ret = -EAGAIN;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001642
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001643 xt_compat_flush_offsets(AF_INET);
Dmitry Mishin27229712006-04-01 02:25:19 -08001644 module_put(t->me);
1645 xt_table_unlock(t);
1646 } else
1647 ret = t ? PTR_ERR(t) : -ENOENT;
1648
1649 xt_compat_unlock(AF_INET);
1650 return ret;
1651}
1652
Patrick McHardy79030ed2006-09-20 12:05:08 -07001653static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1654
Dmitry Mishin27229712006-04-01 02:25:19 -08001655static int
1656compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1657{
1658 int ret;
1659
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001660 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Björn Steinbrink82fac052006-10-20 00:21:10 -07001661 return -EPERM;
1662
Dmitry Mishin27229712006-04-01 02:25:19 -08001663 switch (cmd) {
1664 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001665 ret = get_info(sock_net(sk), user, len, 1);
Dmitry Mishin27229712006-04-01 02:25:19 -08001666 break;
1667 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001668 ret = compat_get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001669 break;
1670 default:
Patrick McHardy79030ed2006-09-20 12:05:08 -07001671 ret = do_ipt_get_ctl(sk, cmd, user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001672 }
1673 return ret;
1674}
1675#endif
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677static int
Patrick McHardy9c547952007-12-17 21:52:00 -08001678do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 int ret;
1681
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001682 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return -EPERM;
1684
1685 switch (cmd) {
1686 case IPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001687 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 break;
1689
1690 case IPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001691 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 break;
1693
1694 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 ret = -EINVAL;
1696 }
1697
1698 return ret;
1699}
1700
1701static int
1702do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1703{
1704 int ret;
1705
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001706 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return -EPERM;
1708
1709 switch (cmd) {
Dmitry Mishin27229712006-04-01 02:25:19 -08001710 case IPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001711 ret = get_info(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 break;
Dmitry Mishin27229712006-04-01 02:25:19 -08001713
1714 case IPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001715 ret = get_entries(sock_net(sk), user, len);
Dmitry Mishin27229712006-04-01 02:25:19 -08001716 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 case IPT_SO_GET_REVISION_MATCH:
1719 case IPT_SO_GET_REVISION_TARGET: {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001720 struct xt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001721 int target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 if (*len != sizeof(rev)) {
1724 ret = -EINVAL;
1725 break;
1726 }
1727 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1728 ret = -EFAULT;
1729 break;
1730 }
Vasiliy Kulikov78b79872011-03-15 13:36:05 +01001731 rev.name[sizeof(rev.name)-1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (cmd == IPT_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001734 target = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08001736 target = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Harald Welte2e4e6a12006-01-12 13:30:04 -08001738 try_then_request_module(xt_find_revision(AF_INET, rev.name,
1739 rev.revision,
1740 target, &ret),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 "ipt_%s", rev.name);
1742 break;
1743 }
1744
1745 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 ret = -EINVAL;
1747 }
1748
1749 return ret;
1750}
1751
Florian Westphalb9e69e12016-02-25 10:08:36 +01001752static void __ipt_unregister_table(struct net *net, struct xt_table *table)
1753{
1754 struct xt_table_info *private;
1755 void *loc_cpu_entry;
1756 struct module *table_owner = table->me;
1757 struct ipt_entry *iter;
1758
1759 private = xt_unregister_table(table);
1760
1761 /* Decrease module usage counts and free resources */
1762 loc_cpu_entry = private->entries;
1763 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1764 cleanup_entry(iter, net);
1765 if (private->number > private->initial_entries)
1766 module_put(table_owner);
1767 xt_free_table_info(private);
1768}
1769
Florian Westphala67dd262016-02-25 10:08:35 +01001770int ipt_register_table(struct net *net, const struct xt_table *table,
1771 const struct ipt_replace *repl,
1772 const struct nf_hook_ops *ops, struct xt_table **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001775 struct xt_table_info *newinfo;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001776 struct xt_table_info bootstrap = {0};
Eric Dumazet31836062005-12-13 23:13:48 -08001777 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08001778 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Harald Welte2e4e6a12006-01-12 13:30:04 -08001780 newinfo = xt_alloc_table_info(repl->size);
Florian Westphala67dd262016-02-25 10:08:35 +01001781 if (!newinfo)
1782 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Florian Westphal482cfc32015-06-11 01:34:55 +02001784 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -08001785 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Jan Engelhardt0f234212010-02-24 18:36:04 +01001787 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001788 if (ret != 0)
1789 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001791 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001792 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001793 ret = PTR_ERR(new_table);
1794 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796
Florian Westphalb9e69e12016-02-25 10:08:36 +01001797 /* set res now, will see skbs right after nf_register_net_hooks */
Florian Westphala67dd262016-02-25 10:08:35 +01001798 WRITE_ONCE(*res, new_table);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001799
1800 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1801 if (ret != 0) {
1802 __ipt_unregister_table(net, new_table);
1803 *res = NULL;
1804 }
1805
Florian Westphala67dd262016-02-25 10:08:35 +01001806 return ret;
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001807
1808out_free:
1809 xt_free_table_info(newinfo);
Florian Westphala67dd262016-02-25 10:08:35 +01001810 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Florian Westphala67dd262016-02-25 10:08:35 +01001813void ipt_unregister_table(struct net *net, struct xt_table *table,
1814 const struct nf_hook_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Florian Westphalb9e69e12016-02-25 10:08:36 +01001816 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1817 __ipt_unregister_table(net, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
1820/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001821static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1823 u_int8_t type, u_int8_t code,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001824 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Patrick McHardy9c547952007-12-17 21:52:00 -08001826 return ((test_type == 0xFF) ||
1827 (type == test_type && code >= min_code && code <= max_code))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 ^ invert;
1829}
1830
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001831static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +02001832icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Jan Engelhardt5452e422008-04-14 11:15:35 +02001834 const struct icmphdr *ic;
1835 struct icmphdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001836 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001839 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001840 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001842 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (ic == NULL) {
1844 /* We've been asked to examine this packet, and we
1845 * can't. Hence, no choice but to drop.
1846 */
Jan Engelhardtb4ba2612009-07-07 20:54:30 +02001847 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001848 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
1850
1851 return icmp_type_code_match(icmpinfo->type,
1852 icmpinfo->code[0],
1853 icmpinfo->code[1],
1854 ic->type, ic->code,
1855 !!(icmpinfo->invflags&IPT_ICMP_INV));
1856}
1857
Jan Engelhardtb0f38452010-03-19 17:16:42 +01001858static int icmp_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001860 const struct ipt_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Patrick McHardy1d5cd902006-03-20 18:01:14 -08001862 /* Must specify no unknown invflags */
Jan Engelhardtbd414ee2010-03-23 16:35:56 +01001863 return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
Jan Engelhardt45385062009-07-04 12:50:00 +02001866static struct xt_target ipt_builtin_tg[] __read_mostly = {
1867 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001868 .name = XT_STANDARD_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001869 .targetsize = sizeof(int),
1870 .family = NFPROTO_IPV4,
Dmitry Mishin27229712006-04-01 02:25:19 -08001871#ifdef CONFIG_COMPAT
Jan Engelhardt45385062009-07-04 12:50:00 +02001872 .compatsize = sizeof(compat_int_t),
1873 .compat_from_user = compat_standard_from_user,
1874 .compat_to_user = compat_standard_to_user,
Dmitry Mishin27229712006-04-01 02:25:19 -08001875#endif
Jan Engelhardt45385062009-07-04 12:50:00 +02001876 },
1877 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001878 .name = XT_ERROR_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001879 .target = ipt_error,
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001880 .targetsize = XT_FUNCTION_MAXNAMELEN,
Jan Engelhardt45385062009-07-04 12:50:00 +02001881 .family = NFPROTO_IPV4,
1882 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883};
1884
1885static struct nf_sockopt_ops ipt_sockopts = {
1886 .pf = PF_INET,
1887 .set_optmin = IPT_BASE_CTL,
1888 .set_optmax = IPT_SO_SET_MAX+1,
1889 .set = do_ipt_set_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08001890#ifdef CONFIG_COMPAT
1891 .compat_set = compat_do_ipt_set_ctl,
1892#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 .get_optmin = IPT_BASE_CTL,
1894 .get_optmax = IPT_SO_GET_MAX+1,
1895 .get = do_ipt_get_ctl,
Dmitry Mishin27229712006-04-01 02:25:19 -08001896#ifdef CONFIG_COMPAT
1897 .compat_get = compat_do_ipt_get_ctl,
1898#endif
Neil Horman16fcec32007-09-11 11:28:26 +02001899 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900};
1901
Jan Engelhardt45385062009-07-04 12:50:00 +02001902static struct xt_match ipt_builtin_mt[] __read_mostly = {
1903 {
1904 .name = "icmp",
1905 .match = icmp_match,
1906 .matchsize = sizeof(struct ipt_icmp),
1907 .checkentry = icmp_checkentry,
1908 .proto = IPPROTO_ICMP,
1909 .family = NFPROTO_IPV4,
1910 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911};
1912
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001913static int __net_init ip_tables_net_init(struct net *net)
1914{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02001915 return xt_proto_init(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001916}
1917
1918static void __net_exit ip_tables_net_exit(struct net *net)
1919{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02001920 xt_proto_fini(net, NFPROTO_IPV4);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001921}
1922
1923static struct pernet_operations ip_tables_net_ops = {
1924 .init = ip_tables_net_init,
1925 .exit = ip_tables_net_exit,
1926};
1927
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001928static int __init ip_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 int ret;
1931
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001932 ret = register_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001933 if (ret < 0)
1934 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001935
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001936 /* No one else will be downing sem now, so we won't sleep */
Jan Engelhardt45385062009-07-04 12:50:00 +02001937 ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001938 if (ret < 0)
1939 goto err2;
Jan Engelhardt45385062009-07-04 12:50:00 +02001940 ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001941 if (ret < 0)
1942 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 /* Register setsockopt */
1945 ret = nf_register_sockopt(&ipt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001946 if (ret < 0)
1947 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01001949 pr_info("(C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001951
1952err5:
Jan Engelhardt45385062009-07-04 12:50:00 +02001953 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001954err4:
Jan Engelhardt45385062009-07-04 12:50:00 +02001955 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001956err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001957 unregister_pernet_subsys(&ip_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001958err1:
1959 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001962static void __exit ip_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 nf_unregister_sockopt(&ipt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001965
Jan Engelhardt45385062009-07-04 12:50:00 +02001966 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
1967 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001968 unregister_pernet_subsys(&ip_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
1971EXPORT_SYMBOL(ipt_register_table);
1972EXPORT_SYMBOL(ipt_unregister_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973EXPORT_SYMBOL(ipt_do_table);
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001974module_init(ip_tables_init);
1975module_exit(ip_tables_fini);