blob: 671eb3222f697fcf0d10bb17e2c2f3f6dcc358c1 [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 Welte6b7d31f2005-10-26 09:34:24 +02005 * 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 */
Joe Perchesa81b2ce2015-03-23 11:50:10 -070012
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +020013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Joe Perchesa81b2ce2015-03-23 11:50:10 -070014
15#include <linux/kernel.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020017#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/skbuff.h>
19#include <linux/kmod.h>
20#include <linux/vmalloc.h>
21#include <linux/netdevice.h>
22#include <linux/module.h>
Randy Dunlap4bdbf6c2006-07-03 19:47:27 -070023#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/icmpv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <net/ipv6.h>
Patrick McHardy3bc3fe52007-12-17 21:50:37 -080026#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/uaccess.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -080028#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/proc_fs.h>
Patrick McHardy3bc3fe52007-12-17 21:50:37 -080030#include <linux/err.h>
David S. Millerc8923c62005-10-13 14:41:23 -070031#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/netfilter_ipv6/ip6_tables.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080034#include <linux/netfilter/x_tables.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080035#include <net/netfilter/nf_log.h>
Jan Engelhardte3eaa992009-06-17 22:14:54 +020036#include "../../netfilter/xt_repldata.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38MODULE_LICENSE("GPL");
39MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
40MODULE_DESCRIPTION("IPv6 packet filter");
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#ifdef CONFIG_NETFILTER_DEBUG
Stephen Hemmingeraf567602010-05-13 15:00:20 +020043#define IP_NF_ASSERT(x) WARN_ON(!(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#else
45#define IP_NF_ASSERT(x)
46#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jan Engelhardte3eaa992009-06-17 22:14:54 +020048void *ip6t_alloc_initial_table(const struct xt_table *info)
49{
50 return xt_alloc_initial_table(ip6t, IP6T);
51}
52EXPORT_SYMBOL_GPL(ip6t_alloc_initial_table);
53
Harald Welte6b7d31f2005-10-26 09:34:24 +020054/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 We keep a set of rules for each CPU, so we can avoid write-locking
Harald Welte6b7d31f2005-10-26 09:34:24 +020056 them in the softirq when updating the counters and therefore
57 only need to read-lock in the softirq; doing a write_lock_bh() in user
58 context stops packets coming through and allows user context to read
59 the counters or update the rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 Hence the start of any table is given by get_table() below. */
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* Returns whether matches rule or not. */
Denys Vlasenko022748a2008-01-14 23:44:05 -080064/* Performance critical - called for every packet */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070065static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070066ip6_packet_match(const struct sk_buff *skb,
67 const char *indev,
68 const char *outdev,
69 const struct ip6t_ip6 *ip6info,
70 unsigned int *protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070071 int *fragoff, bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 unsigned long ret;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070074 const struct ipv6hdr *ipv6 = ipv6_hdr(skb);
Subash Abhinov Kasiviswanathan5769be32017-04-18 15:22:01 -060075#if IS_ENABLED(IP6_NF_IPTABLES_128)
76 const __uint128_t *ulm1 = (const __uint128_t *)&ip6info->smsk;
77 const __uint128_t *ulm2 = (const __uint128_t *)&ip6info->dmsk;
78#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Subash Abhinov Kasiviswanathan5769be32017-04-18 15:22:01 -060080#if IS_ENABLED(IP6_NF_IPTABLES_128)
81 if (*ulm1 || *ulm2)
82#endif
83 {
84 if (NF_INVF(ip6info, IP6T_INV_SRCIP,
85 ipv6_masked_addr_cmp(&ipv6->saddr, &ip6info->smsk,
86 &ip6info->src)) ||
87 NF_INVF(ip6info, IP6T_INV_DSTIP,
88 ipv6_masked_addr_cmp(&ipv6->daddr, &ip6info->dmsk,
89 &ip6info->dst)))
90 return false;
91 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Eric Dumazetb8dfe492009-03-25 17:31:52 +010093 ret = ifname_compare_aligned(indev, ip6info->iniface, ip6info->iniface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Joe Perchesc37a2df2016-06-24 13:25:22 -070095 if (NF_INVF(ip6info, IP6T_INV_VIA_IN, ret != 0))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070096 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Eric Dumazetb8dfe492009-03-25 17:31:52 +010098 ret = ifname_compare_aligned(outdev, ip6info->outiface, ip6info->outiface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Joe Perchesc37a2df2016-06-24 13:25:22 -0700100 if (NF_INVF(ip6info, IP6T_INV_VIA_OUT, ret != 0))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700101 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* ... might want to do something with class and flowlabel here ... */
104
105 /* look for the desired protocol header */
Ian Morris4305ae42015-10-11 17:32:19 +0100106 if (ip6info->flags & IP6T_F_PROTO) {
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800107 int protohdr;
108 unsigned short _frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Hans Schillstrom84018f52012-04-23 03:35:26 +0000110 protohdr = ipv6_find_hdr(skb, protoff, -1, &_frag_off, NULL);
Patrick McHardy51d8b1a2006-10-24 16:14:04 -0700111 if (protohdr < 0) {
112 if (_frag_off == 0)
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700113 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700114 return false;
Patrick McHardy51d8b1a2006-10-24 16:14:04 -0700115 }
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800116 *fragoff = _frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800118 if (ip6info->proto == protohdr) {
Ian Morris4305ae42015-10-11 17:32:19 +0100119 if (ip6info->invflags & IP6T_INV_PROTO)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700120 return false;
Ian Morris4305ae42015-10-11 17:32:19 +0100121
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700122 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
125 /* We need match for the '-p all', too! */
126 if ((ip6info->proto != 0) &&
127 !(ip6info->invflags & IP6T_INV_PROTO))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700128 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700130 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133/* should be ip6 safe */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800134static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135ip6_checkentry(const struct ip6t_ip6 *ipv6)
136{
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200137 if (ipv6->flags & ~IP6T_F_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700138 return false;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200139 if (ipv6->invflags & ~IP6T_INV_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700140 return false;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200141
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700142 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200146ip6t_error(struct sk_buff *skb, const struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Joe Perchese87cc472012-05-13 21:56:26 +0000148 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return NF_DROP;
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static inline struct ip6t_entry *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200154get_entry(const void *base, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 return (struct ip6t_entry *)(base + offset);
157}
158
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700159/* All zeroes == unconditional rule. */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800160/* Mildly perf critical (only if packet tracing is on) */
Florian Westphal54d83fc2016-03-22 18:02:52 +0100161static inline bool unconditional(const struct ip6t_entry *e)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700162{
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200163 static const struct ip6t_ip6 uncond;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700164
Florian Westphal54d83fc2016-03-22 18:02:52 +0100165 return e->target_offset == sizeof(struct ip6t_entry) &&
166 memcmp(&e->ipv6, &uncond, sizeof(uncond)) == 0;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700167}
168
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200169static inline const struct xt_entry_target *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200170ip6t_get_target_c(const struct ip6t_entry *e)
171{
172 return ip6t_get_target((struct ip6t_entry *)e);
173}
174
Amerigo Wang07a93622012-10-29 16:23:10 +0000175#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700176/* This cries for unification! */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800177static const char *const hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800178 [NF_INET_PRE_ROUTING] = "PREROUTING",
179 [NF_INET_LOCAL_IN] = "INPUT",
180 [NF_INET_FORWARD] = "FORWARD",
181 [NF_INET_LOCAL_OUT] = "OUTPUT",
182 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700183};
184
185enum nf_ip_trace_comments {
186 NF_IP6_TRACE_COMMENT_RULE,
187 NF_IP6_TRACE_COMMENT_RETURN,
188 NF_IP6_TRACE_COMMENT_POLICY,
189};
190
Denys Vlasenko022748a2008-01-14 23:44:05 -0800191static const char *const comments[] = {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700192 [NF_IP6_TRACE_COMMENT_RULE] = "rule",
193 [NF_IP6_TRACE_COMMENT_RETURN] = "return",
194 [NF_IP6_TRACE_COMMENT_POLICY] = "policy",
195};
196
197static struct nf_loginfo trace_loginfo = {
198 .type = NF_LOG_TYPE_LOG,
199 .u = {
200 .log = {
Joe Perchesa81b2ce2015-03-23 11:50:10 -0700201 .level = LOGLEVEL_WARNING,
Liping Zhangff107d22016-09-25 16:35:56 +0800202 .logflags = NF_LOG_DEFAULT_MASK,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700203 },
204 },
205};
206
Denys Vlasenko022748a2008-01-14 23:44:05 -0800207/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700208static inline int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200209get_chainname_rulenum(const struct ip6t_entry *s, const struct ip6t_entry *e,
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200210 const char *hookname, const char **chainname,
211 const char **comment, unsigned int *rulenum)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700212{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200213 const struct xt_standard_target *t = (void *)ip6t_get_target_c(s);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700214
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200215 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700216 /* Head of user chain: ERROR target with chainname */
217 *chainname = t->target.data;
218 (*rulenum) = 0;
219 } else if (s == e) {
220 (*rulenum)++;
221
Florian Westphal54d83fc2016-03-22 18:02:52 +0100222 if (unconditional(s) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100223 strcmp(t->target.u.kernel.target->name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200224 XT_STANDARD_TARGET) == 0 &&
Florian Westphal54d83fc2016-03-22 18:02:52 +0100225 t->verdict < 0) {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700226 /* Tail of chains: STANDARD target (return/policy) */
227 *comment = *chainname == hookname
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200228 ? comments[NF_IP6_TRACE_COMMENT_POLICY]
229 : comments[NF_IP6_TRACE_COMMENT_RETURN];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700230 }
231 return 1;
232 } else
233 (*rulenum)++;
234
235 return 0;
236}
237
Eric W. Biederman9dff2c92015-09-15 20:04:17 -0500238static void trace_packet(struct net *net,
239 const struct sk_buff *skb,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700240 unsigned int hook,
241 const struct net_device *in,
242 const struct net_device *out,
Jan Engelhardtecb6f852008-01-31 03:54:47 -0800243 const char *tablename,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200244 const struct xt_table_info *private,
245 const struct ip6t_entry *e)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700246{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200247 const struct ip6t_entry *root;
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200248 const char *hookname, *chainname, *comment;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100249 const struct ip6t_entry *iter;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700250 unsigned int rulenum = 0;
251
Florian Westphal482cfc32015-06-11 01:34:55 +0200252 root = get_entry(private->entries, private->hook_entry[hook]);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700253
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200254 hookname = chainname = hooknames[hook];
255 comment = comments[NF_IP6_TRACE_COMMENT_RULE];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700256
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100257 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
258 if (get_chainname_rulenum(iter, e, hookname,
259 &chainname, &comment, &rulenum) != 0)
260 break;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700261
Pablo Neira Ayuso4017a7e2015-03-02 01:10:28 +0100262 nf_log_trace(net, AF_INET6, hook, skb, in, out, &trace_loginfo,
263 "TRACE: %s:%s:%s:%u ",
264 tablename, chainname, comment, rulenum);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700265}
266#endif
267
Florian Westphal6c7941d2015-07-14 17:51:10 +0200268static inline struct ip6t_entry *
Jan Engelhardt98e86402009-04-15 21:06:05 +0200269ip6t_next_entry(const struct ip6t_entry *entry)
270{
271 return (void *)entry + entry->next_offset;
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/* Returns one of the generic firewall policies, like NF_ACCEPT. */
275unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700276ip6t_do_table(struct sk_buff *skb,
David S. Miller8f8a3712015-04-03 21:09:51 -0400277 const struct nf_hook_state *state,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700278 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Eric W. Biederman6cb8ff3f12015-09-18 14:32:55 -0500280 unsigned int hook = state->hook;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200281 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* Initializing verdict to NF_DROP keeps gcc happy. */
283 unsigned int verdict = NF_DROP;
284 const char *indev, *outdev;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200285 const void *table_base;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200286 struct ip6t_entry *e, **jumpstack;
Florian Westphal7814b6e2015-07-14 17:51:08 +0200287 unsigned int stackidx, cpu;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200288 const struct xt_table_info *private;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200289 struct xt_action_param acpar;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200290 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* Initialization */
Florian Westphal7814b6e2015-07-14 17:51:08 +0200293 stackidx = 0;
David S. Miller8f8a3712015-04-03 21:09:51 -0400294 indev = state->in ? state->in->name : nulldevname;
295 outdev = state->out ? state->out->name : nulldevname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* We handle fragments by dealing with the first fragment as
297 * if it was a normal packet. All other fragments are treated
298 * normally, except that they will NEVER match rules that ask
299 * things we don't know, ie. tcp syn flag or ports). If the
300 * rule is also a fragment-specific rule, non-fragments won't
301 * match it. */
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200302 acpar.hotdrop = false;
Eric W. Biederman156c1962015-09-18 14:32:58 -0500303 acpar.net = state->net;
David S. Miller8f8a3712015-04-03 21:09:51 -0400304 acpar.in = state->in;
305 acpar.out = state->out;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200306 acpar.family = NFPROTO_IPV6;
307 acpar.hooknum = hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Stephen Hemminger78454472009-02-20 10:35:32 +0100310
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200311 local_bh_disable();
312 addend = xt_write_recseq_begin();
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700313 private = table->private;
Will Deaconb416c142013-10-21 13:14:53 +0100314 /*
315 * Ensure we load private-> members after we've fetched the base
316 * pointer.
317 */
318 smp_read_barrier_depends();
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200319 cpu = smp_processor_id();
Florian Westphal482cfc32015-06-11 01:34:55 +0200320 table_base = private->entries;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200321 jumpstack = (struct ip6t_entry **)private->jumpstack[cpu];
Florian Westphal7814b6e2015-07-14 17:51:08 +0200322
323 /* Switch to alternate jumpstack if we're being invoked via TEE.
324 * TEE issues XT_CONTINUE verdict on original skb so we must not
325 * clobber the jumpstack.
326 *
327 * For recursion via REJECT or SYNPROXY the stack will be clobbered
328 * but it is no problem since absolute verdict is issued by these.
329 */
Florian Westphaldcebd312015-07-14 17:51:09 +0200330 if (static_key_false(&xt_tee_enabled))
331 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
Stephen Hemminger78454472009-02-20 10:35:32 +0100332
Harald Welte2e4e6a12006-01-12 13:30:04 -0800333 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 do {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200336 const struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100337 const struct xt_entry_match *ematch;
Florian Westphal71ae0df2015-06-11 01:34:54 +0200338 struct xt_counters *counter;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 IP_NF_ASSERT(e);
Hans Schillstrom84018f52012-04-23 03:35:26 +0000341 acpar.thoff = 0;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200342 if (!ip6_packet_match(skb, indev, outdev, &e->ipv6,
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200343 &acpar.thoff, &acpar.fragoff, &acpar.hotdrop)) {
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100344 no_match:
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200345 e = ip6t_next_entry(e);
346 continue;
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Jan Engelhardtef53d702009-07-09 19:14:18 +0200349 xt_ematch_foreach(ematch, e) {
Jan Engelhardtde74c162009-07-05 18:26:37 +0200350 acpar.match = ematch->u.kernel.match;
351 acpar.matchinfo = ematch->data;
352 if (!acpar.match->match(skb, &acpar))
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100353 goto no_match;
Jan Engelhardtef53d702009-07-09 19:14:18 +0200354 }
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100355
Florian Westphal71ae0df2015-06-11 01:34:54 +0200356 counter = xt_get_this_cpu_counter(&e->counters);
357 ADD_COUNTER(*counter, skb->len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200359 t = ip6t_get_target_c(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200360 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700361
Amerigo Wang07a93622012-10-29 16:23:10 +0000362#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200363 /* The packet is traced: log it */
364 if (unlikely(skb->nf_trace))
Eric W. Biederman9dff2c92015-09-15 20:04:17 -0500365 trace_packet(state->net, skb, hook, state->in,
366 state->out, table->name, private, e);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700367#endif
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200368 /* Standard target? */
369 if (!t->u.kernel.target->target) {
370 int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200372 v = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200373 if (v < 0) {
374 /* Pop from stack? */
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200375 if (v != XT_RETURN) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000376 verdict = (unsigned int)(-v) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200378 }
Florian Westphal7814b6e2015-07-14 17:51:08 +0200379 if (stackidx == 0)
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200380 e = get_entry(table_base,
381 private->underflow[hook]);
382 else
Florian Westphal7814b6e2015-07-14 17:51:08 +0200383 e = ip6t_next_entry(jumpstack[--stackidx]);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200384 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Joe Perches3666ed12009-11-23 23:17:06 +0100386 if (table_base + v != ip6t_next_entry(e) &&
387 !(e->ipv6.flags & IP6T_F_GOTO)) {
Florian Westphalf506da52018-02-07 13:46:25 +0100388 if (unlikely(stackidx >= private->stacksize)) {
389 verdict = NF_DROP;
390 break;
391 }
Florian Westphal7814b6e2015-07-14 17:51:08 +0200392 jumpstack[stackidx++] = e;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200395 e = get_entry(table_base, v);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200396 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200398
Jan Engelhardtde74c162009-07-05 18:26:37 +0200399 acpar.target = t->u.kernel.target;
400 acpar.targinfo = t->data;
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200401
Jan Engelhardtde74c162009-07-05 18:26:37 +0200402 verdict = t->u.kernel.target->target(skb, &acpar);
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200403 if (verdict == XT_CONTINUE)
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200404 e = ip6t_next_entry(e);
405 else
406 /* Verdict */
407 break;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200408 } while (!acpar.hotdrop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Ian Morris76954952015-10-11 17:32:16 +0100410 xt_write_recseq_end(addend);
411 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200413 if (acpar.hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return NF_DROP;
415 else return verdict;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* Figures out from what hook each rule can be called: returns 0 if
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200419 there are loops. Puts hook bitmask in comefrom. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static int
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200421mark_source_chains(const struct xt_table_info *newinfo,
Florian Westphalf4dc7772016-07-14 17:51:26 +0200422 unsigned int valid_hooks, void *entry0,
423 unsigned int *offsets)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 unsigned int hook;
426
427 /* No recursion; use packet counter to save back ptrs (reset
428 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800429 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800431 struct ip6t_entry *e = (struct ip6t_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (!(valid_hooks & (1 << hook)))
434 continue;
435
436 /* Set initial back pointer. */
437 e->counters.pcnt = pos;
438
439 for (;;) {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200440 const struct xt_standard_target *t
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200441 = (void *)ip6t_get_target_c(e);
Patrick McHardy9c547952007-12-17 21:52:00 -0800442 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200444 if (e->comefrom & (1 << NF_INET_NUMHOOKS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200446
Patrick McHardy9c547952007-12-17 21:52:00 -0800447 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* Unconditional return/END. */
Florian Westphal54d83fc2016-03-22 18:02:52 +0100450 if ((unconditional(e) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100451 (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200452 XT_STANDARD_TARGET) == 0) &&
Florian Westphal54d83fc2016-03-22 18:02:52 +0100453 t->verdict < 0) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 unsigned int oldpos, size;
455
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100456 if ((strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200457 XT_STANDARD_TARGET) == 0) &&
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200458 t->verdict < -NF_MAX_VERDICT - 1)
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800459 return 0;
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* Return: backtrack through the last
462 big jump. */
463 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800464 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 oldpos = pos;
466 pos = e->counters.pcnt;
467 e->counters.pcnt = 0;
468
469 /* We're at the start. */
470 if (pos == oldpos)
471 goto next;
472
473 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800474 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } while (oldpos == pos + e->next_offset);
476
477 /* Move along one */
478 size = e->next_offset;
479 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800480 (entry0 + pos + size);
Florian Westphalf24e2302016-04-01 14:17:21 +0200481 if (pos + size >= newinfo->size)
482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 e->counters.pcnt = pos;
484 pos += size;
485 } else {
486 int newpos = t->verdict;
487
488 if (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200489 XT_STANDARD_TARGET) == 0 &&
Joe Perches3666ed12009-11-23 23:17:06 +0100490 newpos >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* This a jump; chase it. */
Florian Westphalf4dc7772016-07-14 17:51:26 +0200492 if (!xt_find_jump_offset(offsets, newpos,
493 newinfo->number))
494 return 0;
Florian Westphal36472342016-04-01 14:17:22 +0200495 e = (struct ip6t_entry *)
496 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 } else {
498 /* ... this is a fallthru */
499 newpos = pos + e->next_offset;
Florian Westphalf24e2302016-04-01 14:17:21 +0200500 if (newpos >= newinfo->size)
501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800504 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 e->counters.pcnt = pos;
506 pos = newpos;
507 }
508 }
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200509next: ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511 return 1;
512}
513
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200514static void cleanup_match(struct xt_entry_match *m, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200516 struct xt_mtdtor_param par;
517
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100518 par.net = net;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200519 par.match = m->u.kernel.match;
520 par.matchinfo = m->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200521 par.family = NFPROTO_IPV6;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200522 if (par.match->destroy != NULL)
523 par.match->destroy(&par);
524 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200527static int check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800528{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200529 const struct ip6t_ip6 *ipv6 = par->entryinfo;
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800530
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200531 par->match = m->u.kernel.match;
532 par->matchinfo = m->data;
533
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200534 return xt_check_match(par, m->u.match_size - sizeof(*m),
535 ipv6->proto, ipv6->invflags & IP6T_INV_PROTO);
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800536}
537
Denys Vlasenko022748a2008-01-14 23:44:05 -0800538static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200539find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800541 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800542 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200544 match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
545 m->u.user.revision);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200546 if (IS_ERR(match))
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200547 return PTR_ERR(match);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 m->u.kernel.match = match;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100551 ret = check_match(m, par);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800552 if (ret)
553 goto err;
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800556err:
557 module_put(m->u.kernel.match->me);
558 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Patrick McHardyadd67462010-02-03 13:45:12 +0100561static int check_target(struct ip6t_entry *e, struct net *net, const char *name)
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800562{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200563 struct xt_entry_target *t = ip6t_get_target(e);
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200564 struct xt_tgchk_param par = {
Patrick McHardyadd67462010-02-03 13:45:12 +0100565 .net = net,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200566 .table = name,
567 .entryinfo = e,
568 .target = t->u.kernel.target,
569 .targinfo = t->data,
570 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200571 .family = NFPROTO_IPV6,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200572 };
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800573
574 t = ip6t_get_target(e);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200575 return xt_check_target(&par, t->u.target_size - sizeof(*t),
576 e->ipv6.proto,
577 e->ipv6.invflags & IP6T_INV_PROTO);
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800578}
579
Denys Vlasenko022748a2008-01-14 23:44:05 -0800580static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100581find_check_entry(struct ip6t_entry *e, struct net *net, const char *name,
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100582 unsigned int size,
583 struct xt_percpu_counter_alloc_state *alloc_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200585 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800586 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int ret;
588 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200589 struct xt_mtchk_param mtpar;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100590 struct xt_entry_match *ematch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100592 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
Florian Westphal71ae0df2015-06-11 01:34:54 +0200593 return -ENOMEM;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 j = 0;
Florian Westphal40352e72018-06-07 21:34:43 +0200596 memset(&mtpar, 0, sizeof(mtpar));
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100597 mtpar.net = net;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200598 mtpar.table = name;
599 mtpar.entryinfo = &e->ipv6;
600 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200601 mtpar.family = NFPROTO_IPV6;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100602 xt_ematch_foreach(ematch, e) {
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100603 ret = find_check_match(ematch, &mtpar);
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100604 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100605 goto cleanup_matches;
606 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 t = ip6t_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200610 target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name,
611 t->u.user.revision);
612 if (IS_ERR(target)) {
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200613 ret = PTR_ERR(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto cleanup_matches;
615 }
616 t->u.kernel.target = target;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200617
Patrick McHardyadd67462010-02-03 13:45:12 +0100618 ret = check_target(e, net, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800619 if (ret)
620 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800622 err:
623 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 cleanup_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100625 xt_ematch_foreach(ematch, e) {
626 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100627 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100628 cleanup_match(ematch, net);
629 }
Florian Westphal71ae0df2015-06-11 01:34:54 +0200630
Florian Westphal61346e22016-11-22 14:44:17 +0100631 xt_percpu_counter_free(&e->counters);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return ret;
634}
635
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200636static bool check_underflow(const struct ip6t_entry *e)
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200637{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200638 const struct xt_entry_target *t;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200639 unsigned int verdict;
640
Florian Westphal54d83fc2016-03-22 18:02:52 +0100641 if (!unconditional(e))
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200642 return false;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200643 t = ip6t_get_target_c(e);
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200644 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
645 return false;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200646 verdict = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200647 verdict = -verdict - 1;
648 return verdict == NF_DROP || verdict == NF_ACCEPT;
649}
650
Denys Vlasenko022748a2008-01-14 23:44:05 -0800651static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652check_entry_size_and_hooks(struct ip6t_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800653 struct xt_table_info *newinfo,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200654 const unsigned char *base,
655 const unsigned char *limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 const unsigned int *hook_entries,
657 const unsigned int *underflows,
Jan Engelhardt05595182010-02-24 18:33:43 +0100658 unsigned int valid_hooks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 unsigned int h;
Florian Westphalbdf533d2016-03-22 18:02:49 +0100661 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Joe Perches3666ed12009-11-23 23:17:06 +0100663 if ((unsigned long)e % __alignof__(struct ip6t_entry) != 0 ||
Florian Westphal6e94e0c2016-03-22 18:02:50 +0100664 (unsigned char *)e + sizeof(struct ip6t_entry) >= limit ||
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200665 (unsigned char *)e + e->next_offset > limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 if (e->next_offset
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200669 < sizeof(struct ip6t_entry) + sizeof(struct xt_entry_target))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Florian Westphalaa412ba2016-04-01 14:17:24 +0200672 if (!ip6_checkentry(&e->ipv6))
673 return -EINVAL;
674
Florian Westphalce683e52016-04-01 14:17:28 +0200675 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
676 e->next_offset);
Florian Westphalbdf533d2016-03-22 18:02:49 +0100677 if (err)
678 return err;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800681 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Jan Engelhardta7d51732009-07-18 14:52:58 +0200682 if (!(valid_hooks & (1 << h)))
683 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if ((unsigned char *)e - base == hook_entries[h])
685 newinfo->hook_entry[h] = hook_entries[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200686 if ((unsigned char *)e - base == underflows[h]) {
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200687 if (!check_underflow(e))
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200688 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 newinfo->underflow[h] = underflows[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800695 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 e->comefrom = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698}
699
Jan Engelhardt05595182010-02-24 18:33:43 +0100700static void cleanup_entry(struct ip6t_entry *e, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200702 struct xt_tgdtor_param par;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200703 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100704 struct xt_entry_match *ematch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100707 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100708 cleanup_match(ematch, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 t = ip6t_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200710
Patrick McHardyadd67462010-02-03 13:45:12 +0100711 par.net = net;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200712 par.target = t->u.kernel.target;
713 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200714 par.family = NFPROTO_IPV6;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200715 if (par.target->destroy != NULL)
716 par.target->destroy(&par);
717 module_put(par.target->me);
Florian Westphal61346e22016-11-22 14:44:17 +0100718 xt_percpu_counter_free(&e->counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721/* Checks and translates the user-supplied table segment (held in
722 newinfo) */
723static int
Jan Engelhardt0f234212010-02-24 18:36:04 +0100724translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
Ian Morriscda219c2015-10-11 17:32:15 +0100725 const struct ip6t_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100727 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100728 struct ip6t_entry *iter;
Florian Westphalf4dc7772016-07-14 17:51:26 +0200729 unsigned int *offsets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 unsigned int i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100731 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Jan Engelhardt0f234212010-02-24 18:36:04 +0100733 newinfo->size = repl->size;
734 newinfo->number = repl->num_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800737 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 newinfo->hook_entry[i] = 0xFFFFFFFF;
739 newinfo->underflow[i] = 0xFFFFFFFF;
740 }
741
Florian Westphalf4dc7772016-07-14 17:51:26 +0200742 offsets = xt_alloc_entry_offsets(newinfo->number);
743 if (!offsets)
744 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 i = 0;
746 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100747 xt_entry_foreach(iter, entry0, newinfo->size) {
748 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +0100749 entry0 + repl->size,
750 repl->hook_entry,
751 repl->underflow,
752 repl->valid_hooks);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100753 if (ret != 0)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200754 goto out_free;
755 if (i < repl->num_entries)
756 offsets[i] = (void *)iter - entry0;
Jan Engelhardt05595182010-02-24 18:33:43 +0100757 ++i;
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200758 if (strcmp(ip6t_get_target(iter)->u.user.name,
759 XT_ERROR_TARGET) == 0)
760 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Florian Westphalf4dc7772016-07-14 17:51:26 +0200763 ret = -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200764 if (i != repl->num_entries)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200765 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800768 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /* Only hooks which are valid */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100770 if (!(repl->valid_hooks & (1 << i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 continue;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200772 if (newinfo->hook_entry[i] == 0xFFFFFFFF)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200773 goto out_free;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200774 if (newinfo->underflow[i] == 0xFFFFFFFF)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200775 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
Florian Westphalf4dc7772016-07-14 17:51:26 +0200778 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
779 ret = -ELOOP;
780 goto out_free;
781 }
782 kvfree(offsets);
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 /* Finally, each sanity check must pass */
785 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100786 xt_entry_foreach(iter, entry0, newinfo->size) {
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100787 ret = find_check_entry(iter, net, repl->name, repl->size,
788 &alloc_state);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100789 if (ret != 0)
790 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100791 ++i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800794 if (ret != 0) {
Jan Engelhardt05595182010-02-24 18:33:43 +0100795 xt_entry_foreach(iter, entry0, newinfo->size) {
796 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100797 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100798 cleanup_entry(iter, net);
799 }
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800800 return ret;
801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Patrick McHardy9c547952007-12-17 21:52:00 -0800803 return ret;
Florian Westphalf4dc7772016-07-14 17:51:26 +0200804 out_free:
805 kvfree(offsets);
806 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800810get_counters(const struct xt_table_info *t,
811 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100813 struct ip6t_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 unsigned int cpu;
815 unsigned int i;
816
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700817 for_each_possible_cpu(cpu) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200818 seqcount_t *s = &per_cpu(xt_recseq, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 i = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200821 xt_entry_foreach(iter, t->entries, t->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +0200822 struct xt_counters *tmp;
Eric Dumazet83723d62011-01-10 20:11:38 +0100823 u64 bcnt, pcnt;
824 unsigned int start;
825
Florian Westphal71ae0df2015-06-11 01:34:54 +0200826 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100827 do {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200828 start = read_seqcount_begin(s);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200829 bcnt = tmp->bcnt;
830 pcnt = tmp->pcnt;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200831 } while (read_seqcount_retry(s, start));
Eric Dumazet83723d62011-01-10 20:11:38 +0100832
833 ADD_COUNTER(counters[i], bcnt, pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +0100834 ++i;
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100837}
838
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200839static struct xt_counters *alloc_counters(const struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800841 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800842 struct xt_counters *counters;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200843 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /* We need atomic snapshot of counters: rest doesn't change
846 (other than comefrom, which userspace doesn't care
847 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800848 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet83723d62011-01-10 20:11:38 +0100849 counters = vzalloc(countersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700852 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700854 get_counters(private, counters);
Stephen Hemminger78454472009-02-20 10:35:32 +0100855
Eric Dumazet49a88d12009-04-06 17:06:55 +0200856 return counters;
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800857}
858
859static int
860copy_entries_to_user(unsigned int total_size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200861 const struct xt_table *table,
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800862 void __user *userptr)
863{
864 unsigned int off, num;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200865 const struct ip6t_entry *e;
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800866 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200867 const struct xt_table_info *private = table->private;
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800868 int ret = 0;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700869 const void *loc_cpu_entry;
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800870
871 counters = alloc_counters(table);
872 if (IS_ERR(counters))
873 return PTR_ERR(counters);
874
Florian Westphal482cfc32015-06-11 01:34:55 +0200875 loc_cpu_entry = private->entries;
Eric Dumazet31836062005-12-13 23:13:48 -0800876 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 ret = -EFAULT;
878 goto free_counters;
879 }
880
881 /* FIXME: use iterator macros --RR */
882 /* ... then go back and fix counters and names */
883 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
884 unsigned int i;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200885 const struct xt_entry_match *m;
886 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Eric Dumazet31836062005-12-13 23:13:48 -0800888 e = (struct ip6t_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (copy_to_user(userptr + off
890 + offsetof(struct ip6t_entry, counters),
891 &counters[num],
892 sizeof(counters[num])) != 0) {
893 ret = -EFAULT;
894 goto free_counters;
895 }
896
897 for (i = sizeof(struct ip6t_entry);
898 i < e->target_offset;
899 i += m->u.match_size) {
900 m = (void *)e + i;
901
902 if (copy_to_user(userptr + off + i
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200903 + offsetof(struct xt_entry_match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 u.user.name),
905 m->u.kernel.match->name,
906 strlen(m->u.kernel.match->name)+1)
907 != 0) {
908 ret = -EFAULT;
909 goto free_counters;
910 }
911 }
912
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200913 t = ip6t_get_target_c(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (copy_to_user(userptr + off + e->target_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200915 + offsetof(struct xt_entry_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 u.user.name),
917 t->u.kernel.target->name,
918 strlen(t->u.kernel.target->name)+1) != 0) {
919 ret = -EFAULT;
920 goto free_counters;
921 }
922 }
923
924 free_counters:
925 vfree(counters);
926 return ret;
927}
928
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800929#ifdef CONFIG_COMPAT
Jan Engelhardt739674f2009-06-26 08:23:19 +0200930static void compat_standard_from_user(void *dst, const void *src)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800931{
932 int v = *(compat_int_t *)src;
933
934 if (v > 0)
935 v += xt_compat_calc_jump(AF_INET6, v);
936 memcpy(dst, &v, sizeof(v));
937}
938
Jan Engelhardt739674f2009-06-26 08:23:19 +0200939static int compat_standard_to_user(void __user *dst, const void *src)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800940{
941 compat_int_t cv = *(int *)src;
942
943 if (cv > 0)
944 cv -= xt_compat_calc_jump(AF_INET6, cv);
945 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
946}
947
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200948static int compat_calc_entry(const struct ip6t_entry *e,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800949 const struct xt_table_info *info,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200950 const void *base, struct xt_table_info *newinfo)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800951{
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100952 const struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200953 const struct xt_entry_target *t;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800954 unsigned int entry_offset;
955 int off, i, ret;
956
957 off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
958 entry_offset = (void *)e - base;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100959 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100960 off += xt_compat_match_offset(ematch->u.kernel.match);
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200961 t = ip6t_get_target_c(e);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800962 off += xt_compat_target_offset(t->u.kernel.target);
963 newinfo->size -= off;
964 ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
965 if (ret)
966 return ret;
967
968 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
969 if (info->hook_entry[i] &&
970 (e < (struct ip6t_entry *)(base + info->hook_entry[i])))
971 newinfo->hook_entry[i] -= off;
972 if (info->underflow[i] &&
973 (e < (struct ip6t_entry *)(base + info->underflow[i])))
974 newinfo->underflow[i] -= off;
975 }
976 return 0;
977}
978
979static int compat_table_info(const struct xt_table_info *info,
980 struct xt_table_info *newinfo)
981{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100982 struct ip6t_entry *iter;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700983 const void *loc_cpu_entry;
Jan Engelhardt05595182010-02-24 18:33:43 +0100984 int ret;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800985
986 if (!newinfo || !info)
987 return -EINVAL;
988
Florian Westphal482cfc32015-06-11 01:34:55 +0200989 /* we dont care about newinfo->entries */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800990 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
991 newinfo->initial_entries = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200992 loc_cpu_entry = info->entries;
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100993 xt_compat_init_offsets(AF_INET6, info->number);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100994 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
995 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
996 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +0100997 return ret;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100998 }
Jan Engelhardt05595182010-02-24 18:33:43 +0100999 return 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001000}
1001#endif
1002
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001003static int get_info(struct net *net, void __user *user,
Ian Morriscda219c2015-10-11 17:32:15 +01001004 const int *len, int compat)
Patrick McHardy433665c2007-12-17 21:50:05 -08001005{
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001006 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardy433665c2007-12-17 21:50:05 -08001007 struct xt_table *t;
1008 int ret;
1009
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001010 if (*len != sizeof(struct ip6t_getinfo))
Patrick McHardy433665c2007-12-17 21:50:05 -08001011 return -EINVAL;
Patrick McHardy433665c2007-12-17 21:50:05 -08001012
1013 if (copy_from_user(name, user, sizeof(name)) != 0)
1014 return -EFAULT;
1015
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001016 name[XT_TABLE_MAXNAMELEN-1] = '\0';
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001017#ifdef CONFIG_COMPAT
1018 if (compat)
1019 xt_compat_lock(AF_INET6);
1020#endif
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001021 t = try_then_request_module(xt_find_table_lock(net, AF_INET6, name),
Patrick McHardy433665c2007-12-17 21:50:05 -08001022 "ip6table_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001023 if (!IS_ERR_OR_NULL(t)) {
Patrick McHardy433665c2007-12-17 21:50:05 -08001024 struct ip6t_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001025 const struct xt_table_info *private = t->private;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001026#ifdef CONFIG_COMPAT
Alexey Dobriyan14c7dbe2010-02-08 11:17:43 -08001027 struct xt_table_info tmp;
1028
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001029 if (compat) {
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001030 ret = compat_table_info(private, &tmp);
1031 xt_compat_flush_offsets(AF_INET6);
1032 private = &tmp;
1033 }
1034#endif
Jan Engelhardtcccbe5e2010-11-03 18:55:39 -07001035 memset(&info, 0, sizeof(info));
Patrick McHardy433665c2007-12-17 21:50:05 -08001036 info.valid_hooks = t->valid_hooks;
1037 memcpy(info.hook_entry, private->hook_entry,
1038 sizeof(info.hook_entry));
1039 memcpy(info.underflow, private->underflow,
1040 sizeof(info.underflow));
1041 info.num_entries = private->number;
1042 info.size = private->size;
Patrick McHardyb5dd6742007-12-17 21:52:35 -08001043 strcpy(info.name, name);
Patrick McHardy433665c2007-12-17 21:50:05 -08001044
1045 if (copy_to_user(user, &info, *len) != 0)
1046 ret = -EFAULT;
1047 else
1048 ret = 0;
1049
1050 xt_table_unlock(t);
1051 module_put(t->me);
1052 } else
1053 ret = t ? PTR_ERR(t) : -ENOENT;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001054#ifdef CONFIG_COMPAT
1055 if (compat)
1056 xt_compat_unlock(AF_INET6);
1057#endif
Patrick McHardy433665c2007-12-17 21:50:05 -08001058 return ret;
1059}
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001062get_entries(struct net *net, struct ip6t_get_entries __user *uptr,
Ian Morriscda219c2015-10-11 17:32:15 +01001063 const int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 int ret;
Patrick McHardyd9243572007-12-17 21:50:22 -08001066 struct ip6t_get_entries get;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001067 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001069 if (*len < sizeof(get))
Patrick McHardyd9243572007-12-17 21:50:22 -08001070 return -EINVAL;
Patrick McHardyd9243572007-12-17 21:50:22 -08001071 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1072 return -EFAULT;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001073 if (*len != sizeof(struct ip6t_get_entries) + get.size)
Patrick McHardyd9243572007-12-17 21:50:22 -08001074 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001075
Pablo Neira Ayusob301f252016-03-24 21:29:53 +01001076 get.name[sizeof(get.name) - 1] = '\0';
Patrick McHardyd9243572007-12-17 21:50:22 -08001077
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001078 t = xt_find_table_lock(net, AF_INET6, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001079 if (!IS_ERR_OR_NULL(t)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001080 struct xt_table_info *private = t->private;
Patrick McHardyd9243572007-12-17 21:50:22 -08001081 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001082 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 t, uptr->entrytable);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001084 else
Patrick McHardy544473c2008-04-14 11:15:45 +02001085 ret = -EAGAIN;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001086
Harald Welte6b7d31f2005-10-26 09:34:24 +02001087 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001088 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 } else
Harald Welte6b7d31f2005-10-26 09:34:24 +02001090 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 return ret;
1093}
1094
1095static int
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001096__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001097 struct xt_table_info *newinfo, unsigned int num_counters,
1098 void __user *counters_ptr)
1099{
1100 int ret;
1101 struct xt_table *t;
1102 struct xt_table_info *oldinfo;
1103 struct xt_counters *counters;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001104 struct ip6t_entry *iter;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001105
1106 ret = 0;
Eric Dumazet83723d62011-01-10 20:11:38 +01001107 counters = vzalloc(num_counters * sizeof(struct xt_counters));
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001108 if (!counters) {
1109 ret = -ENOMEM;
1110 goto out;
1111 }
1112
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001113 t = try_then_request_module(xt_find_table_lock(net, AF_INET6, name),
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001114 "ip6table_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001115 if (IS_ERR_OR_NULL(t)) {
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001116 ret = t ? PTR_ERR(t) : -ENOENT;
1117 goto free_newinfo_counters_untrans;
1118 }
1119
1120 /* You lied! */
1121 if (valid_hooks != t->valid_hooks) {
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001122 ret = -EINVAL;
1123 goto put_module;
1124 }
1125
1126 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1127 if (!oldinfo)
1128 goto put_module;
1129
1130 /* Update module usage count based on number of rules */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001131 if ((oldinfo->number > oldinfo->initial_entries) ||
1132 (newinfo->number <= oldinfo->initial_entries))
1133 module_put(t->me);
1134 if ((oldinfo->number > oldinfo->initial_entries) &&
1135 (newinfo->number <= oldinfo->initial_entries))
1136 module_put(t->me);
1137
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001138 /* Get the old counters, and synchronize with replace */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001139 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001140
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001141 /* Decrease module usage counts and free resource */
Florian Westphal482cfc32015-06-11 01:34:55 +02001142 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001143 cleanup_entry(iter, net);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001144
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001145 xt_free_table_info(oldinfo);
1146 if (copy_to_user(counters_ptr, counters,
Thomas Grafc58dd2d2014-04-04 17:57:45 +02001147 sizeof(struct xt_counters) * num_counters) != 0) {
1148 /* Silent error, can't fail, new table is already in place */
1149 net_warn_ratelimited("ip6tables: counters copy to user failed while replacing table\n");
1150 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001151 vfree(counters);
1152 xt_table_unlock(t);
1153 return ret;
1154
1155 put_module:
1156 module_put(t->me);
1157 xt_table_unlock(t);
1158 free_newinfo_counters_untrans:
1159 vfree(counters);
1160 out:
1161 return ret;
1162}
1163
1164static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001165do_replace(struct net *net, const void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 int ret;
1168 struct ip6t_replace tmp;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001169 struct xt_table_info *newinfo;
1170 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001171 struct ip6t_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1174 return -EFAULT;
1175
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001176 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001177 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1178 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001179 if (tmp.num_counters == 0)
1180 return -EINVAL;
1181
Vasiliy Kulikov6a8ab0602011-03-15 13:37:13 +01001182 tmp.name[sizeof(tmp.name)-1] = 0;
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001183
Harald Welte2e4e6a12006-01-12 13:30:04 -08001184 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (!newinfo)
1186 return -ENOMEM;
1187
Florian Westphal482cfc32015-06-11 01:34:55 +02001188 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -08001189 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 tmp.size) != 0) {
1191 ret = -EFAULT;
1192 goto free_newinfo;
1193 }
1194
Jan Engelhardt0f234212010-02-24 18:36:04 +01001195 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (ret != 0)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001197 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001199 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001200 tmp.num_counters, tmp.counters);
1201 if (ret)
1202 goto free_newinfo_untrans;
1203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001205 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001206 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001207 cleanup_entry(iter, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001209 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return ret;
1211}
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001214do_add_counters(struct net *net, const void __user *user, unsigned int len,
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001215 int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Florian Westphal482cfc32015-06-11 01:34:55 +02001217 unsigned int i;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001218 struct xt_counters_info tmp;
1219 struct xt_counters *paddc;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001220 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001221 const struct xt_table_info *private;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001222 int ret = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001223 struct ip6t_entry *iter;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001224 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Florian Westphald7591f02016-04-01 15:37:59 +02001226 paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1227 if (IS_ERR(paddc))
1228 return PTR_ERR(paddc);
1229 t = xt_find_table_lock(net, AF_INET6, tmp.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001230 if (IS_ERR_OR_NULL(t)) {
Harald Welte6b7d31f2005-10-26 09:34:24 +02001231 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 goto free;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001235 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001236 private = t->private;
Florian Westphald7591f02016-04-01 15:37:59 +02001237 if (private->number != tmp.num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 ret = -EINVAL;
1239 goto unlock_up_free;
1240 }
1241
1242 i = 0;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001243 addend = xt_write_recseq_begin();
Florian Westphal482cfc32015-06-11 01:34:55 +02001244 xt_entry_foreach(iter, private->entries, private->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +02001245 struct xt_counters *tmp;
1246
1247 tmp = xt_get_this_cpu_counter(&iter->counters);
1248 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +01001249 ++i;
1250 }
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001251 xt_write_recseq_end(addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001253 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001254 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001255 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 free:
1257 vfree(paddc);
1258
1259 return ret;
1260}
1261
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001262#ifdef CONFIG_COMPAT
1263struct compat_ip6t_replace {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001264 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001265 u32 valid_hooks;
1266 u32 num_entries;
1267 u32 size;
1268 u32 hook_entry[NF_INET_NUMHOOKS];
1269 u32 underflow[NF_INET_NUMHOOKS];
1270 u32 num_counters;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001271 compat_uptr_t counters; /* struct xt_counters * */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001272 struct compat_ip6t_entry entries[0];
1273};
1274
1275static int
1276compat_copy_entry_to_user(struct ip6t_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001277 unsigned int *size, struct xt_counters *counters,
Jan Engelhardt05595182010-02-24 18:33:43 +01001278 unsigned int i)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001279{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001280 struct xt_entry_target *t;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001281 struct compat_ip6t_entry __user *ce;
1282 u_int16_t target_offset, next_offset;
1283 compat_uint_t origsize;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001284 const struct xt_entry_match *ematch;
1285 int ret = 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001286
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001287 origsize = *size;
1288 ce = (struct compat_ip6t_entry __user *)*dstptr;
Jan Engelhardt05595182010-02-24 18:33:43 +01001289 if (copy_to_user(ce, e, sizeof(struct ip6t_entry)) != 0 ||
1290 copy_to_user(&ce->counters, &counters[i],
1291 sizeof(counters[i])) != 0)
1292 return -EFAULT;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001293
1294 *dstptr += sizeof(struct compat_ip6t_entry);
1295 *size -= sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1296
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001297 xt_ematch_foreach(ematch, e) {
1298 ret = xt_compat_match_to_user(ematch, dstptr, size);
1299 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001300 return ret;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001301 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001302 target_offset = e->target_offset - (origsize - *size);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001303 t = ip6t_get_target(e);
1304 ret = xt_compat_target_to_user(t, dstptr, size);
1305 if (ret)
Jan Engelhardt05595182010-02-24 18:33:43 +01001306 return ret;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001307 next_offset = e->next_offset - (origsize - *size);
Jan Engelhardt05595182010-02-24 18:33:43 +01001308 if (put_user(target_offset, &ce->target_offset) != 0 ||
1309 put_user(next_offset, &ce->next_offset) != 0)
1310 return -EFAULT;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001311 return 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001312}
1313
Denys Vlasenko022748a2008-01-14 23:44:05 -08001314static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001315compat_find_calc_match(struct xt_entry_match *m,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001316 const struct ip6t_ip6 *ipv6,
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001317 int *size)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001318{
1319 struct xt_match *match;
1320
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001321 match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
1322 m->u.user.revision);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001323 if (IS_ERR(match))
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001324 return PTR_ERR(match);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001325
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001326 m->u.kernel.match = match;
1327 *size += xt_compat_match_offset(match);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001328 return 0;
1329}
1330
Jan Engelhardt05595182010-02-24 18:33:43 +01001331static void compat_release_entry(struct compat_ip6t_entry *e)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001332{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001333 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001334 struct xt_entry_match *ematch;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001335
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001336 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001337 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001338 module_put(ematch->u.kernel.match->me);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001339 t = compat_ip6t_get_target(e);
1340 module_put(t->u.kernel.target->me);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001341}
1342
Denys Vlasenko022748a2008-01-14 23:44:05 -08001343static int
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001344check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
1345 struct xt_table_info *newinfo,
1346 unsigned int *size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001347 const unsigned char *base,
Florian Westphal09d96862016-04-01 14:17:34 +02001348 const unsigned char *limit)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001349{
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001350 struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001351 struct xt_entry_target *t;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001352 struct xt_target *target;
1353 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001354 unsigned int j;
Florian Westphal09d96862016-04-01 14:17:34 +02001355 int ret, off;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001356
Joe Perches3666ed12009-11-23 23:17:06 +01001357 if ((unsigned long)e % __alignof__(struct compat_ip6t_entry) != 0 ||
Florian Westphal6e94e0c2016-03-22 18:02:50 +01001358 (unsigned char *)e + sizeof(struct compat_ip6t_entry) >= limit ||
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001359 (unsigned char *)e + e->next_offset > limit)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001360 return -EINVAL;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001361
1362 if (e->next_offset < sizeof(struct compat_ip6t_entry) +
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001363 sizeof(struct compat_xt_entry_target))
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001364 return -EINVAL;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001365
Florian Westphalaa412ba2016-04-01 14:17:24 +02001366 if (!ip6_checkentry(&e->ipv6))
1367 return -EINVAL;
1368
Florian Westphalce683e52016-04-01 14:17:28 +02001369 ret = xt_compat_check_entry_offsets(e, e->elems,
Florian Westphalfc1221b2016-04-01 14:17:26 +02001370 e->target_offset, e->next_offset);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001371 if (ret)
1372 return ret;
1373
1374 off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1375 entry_offset = (void *)e - (void *)base;
1376 j = 0;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001377 xt_ematch_foreach(ematch, e) {
Florian Westphal329a0802016-04-01 14:17:31 +02001378 ret = compat_find_calc_match(ematch, &e->ipv6, &off);
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001379 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001380 goto release_matches;
1381 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001382 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001383
1384 t = compat_ip6t_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001385 target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name,
1386 t->u.user.revision);
1387 if (IS_ERR(target)) {
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001388 ret = PTR_ERR(target);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001389 goto release_matches;
1390 }
1391 t->u.kernel.target = target;
1392
1393 off += xt_compat_target_offset(target);
1394 *size += off;
1395 ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
1396 if (ret)
1397 goto out;
1398
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001399 return 0;
1400
1401out:
1402 module_put(t->u.kernel.target->me);
1403release_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001404 xt_ematch_foreach(ematch, e) {
1405 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001406 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001407 module_put(ematch->u.kernel.match->me);
1408 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001409 return ret;
1410}
1411
Florian Westphal01883462016-04-01 14:17:33 +02001412static void
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001413compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr,
Florian Westphal329a0802016-04-01 14:17:31 +02001414 unsigned int *size,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001415 struct xt_table_info *newinfo, unsigned char *base)
1416{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001417 struct xt_entry_target *t;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001418 struct ip6t_entry *de;
1419 unsigned int origsize;
Florian Westphal01883462016-04-01 14:17:33 +02001420 int h;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001421 struct xt_entry_match *ematch;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001422
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001423 origsize = *size;
1424 de = (struct ip6t_entry *)*dstptr;
1425 memcpy(de, e, sizeof(struct ip6t_entry));
1426 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1427
1428 *dstptr += sizeof(struct ip6t_entry);
1429 *size += sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1430
Florian Westphal01883462016-04-01 14:17:33 +02001431 xt_ematch_foreach(ematch, e)
1432 xt_compat_match_from_user(ematch, dstptr, size);
1433
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001434 de->target_offset = e->target_offset - (origsize - *size);
1435 t = compat_ip6t_get_target(e);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001436 xt_compat_target_from_user(t, dstptr, size);
1437
1438 de->next_offset = e->next_offset - (origsize - *size);
1439 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1440 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1441 newinfo->hook_entry[h] -= origsize - *size;
1442 if ((unsigned char *)de - base < newinfo->underflow[h])
1443 newinfo->underflow[h] -= origsize - *size;
1444 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001445}
1446
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001447static int
Alexey Dobriyanf54e9362010-01-18 08:25:47 +01001448translate_compat_table(struct net *net,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001449 struct xt_table_info **pinfo,
1450 void **pentry0,
Florian Westphal329a0802016-04-01 14:17:31 +02001451 const struct compat_ip6t_replace *compatr)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001452{
1453 unsigned int i, j;
1454 struct xt_table_info *newinfo, *info;
1455 void *pos, *entry0, *entry1;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001456 struct compat_ip6t_entry *iter0;
Florian Westphal09d96862016-04-01 14:17:34 +02001457 struct ip6t_replace repl;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001458 unsigned int size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001459 int ret = 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001460
1461 info = *pinfo;
1462 entry0 = *pentry0;
Florian Westphal329a0802016-04-01 14:17:31 +02001463 size = compatr->size;
1464 info->number = compatr->num_entries;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001465
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001466 j = 0;
1467 xt_compat_lock(AF_INET6);
Florian Westphal329a0802016-04-01 14:17:31 +02001468 xt_compat_init_offsets(AF_INET6, compatr->num_entries);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001469 /* Walk through entries, checking offsets. */
Florian Westphal329a0802016-04-01 14:17:31 +02001470 xt_entry_foreach(iter0, entry0, compatr->size) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001471 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001472 entry0,
Florian Westphal09d96862016-04-01 14:17:34 +02001473 entry0 + compatr->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001474 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001475 goto out_unlock;
1476 ++j;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001477 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001478
1479 ret = -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001480 if (j != compatr->num_entries)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001481 goto out_unlock;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001482
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001483 ret = -ENOMEM;
1484 newinfo = xt_alloc_table_info(size);
1485 if (!newinfo)
1486 goto out_unlock;
1487
Florian Westphal329a0802016-04-01 14:17:31 +02001488 newinfo->number = compatr->num_entries;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001489 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Florian Westphal09d96862016-04-01 14:17:34 +02001490 newinfo->hook_entry[i] = compatr->hook_entry[i];
1491 newinfo->underflow[i] = compatr->underflow[i];
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001492 }
Florian Westphal482cfc32015-06-11 01:34:55 +02001493 entry1 = newinfo->entries;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001494 pos = entry1;
Florian Westphal09d96862016-04-01 14:17:34 +02001495 size = compatr->size;
Florian Westphal01883462016-04-01 14:17:33 +02001496 xt_entry_foreach(iter0, entry0, compatr->size)
1497 compat_copy_entry_from_user(iter0, &pos, &size,
1498 newinfo, entry1);
1499
Florian Westphal09d96862016-04-01 14:17:34 +02001500 /* all module references in entry0 are now gone. */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001501 xt_compat_flush_offsets(AF_INET6);
1502 xt_compat_unlock(AF_INET6);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001503
Florian Westphal09d96862016-04-01 14:17:34 +02001504 memcpy(&repl, compatr, sizeof(*compatr));
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001505
Florian Westphal09d96862016-04-01 14:17:34 +02001506 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1507 repl.hook_entry[i] = newinfo->hook_entry[i];
1508 repl.underflow[i] = newinfo->underflow[i];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001509 }
Florian Westphal09d96862016-04-01 14:17:34 +02001510
1511 repl.num_counters = 0;
1512 repl.counters = NULL;
1513 repl.size = newinfo->size;
1514 ret = translate_table(net, newinfo, entry1, &repl);
1515 if (ret)
1516 goto free_newinfo;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001517
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001518 *pinfo = newinfo;
1519 *pentry0 = entry1;
1520 xt_free_table_info(info);
1521 return 0;
1522
1523free_newinfo:
1524 xt_free_table_info(newinfo);
Florian Westphal09d96862016-04-01 14:17:34 +02001525 return ret;
1526out_unlock:
1527 xt_compat_flush_offsets(AF_INET6);
1528 xt_compat_unlock(AF_INET6);
Florian Westphal329a0802016-04-01 14:17:31 +02001529 xt_entry_foreach(iter0, entry0, compatr->size) {
Jan Engelhardt05595182010-02-24 18:33:43 +01001530 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001531 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001532 compat_release_entry(iter0);
1533 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001534 return ret;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001535}
1536
1537static int
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001538compat_do_replace(struct net *net, void __user *user, unsigned int len)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001539{
1540 int ret;
1541 struct compat_ip6t_replace tmp;
1542 struct xt_table_info *newinfo;
1543 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001544 struct ip6t_entry *iter;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001545
1546 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1547 return -EFAULT;
1548
1549 /* overflow check */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001550 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1551 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001552 if (tmp.num_counters == 0)
1553 return -EINVAL;
1554
Vasiliy Kulikov6a8ab0602011-03-15 13:37:13 +01001555 tmp.name[sizeof(tmp.name)-1] = 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001556
1557 newinfo = xt_alloc_table_info(tmp.size);
1558 if (!newinfo)
1559 return -ENOMEM;
1560
Florian Westphal482cfc32015-06-11 01:34:55 +02001561 loc_cpu_entry = newinfo->entries;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001562 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1563 tmp.size) != 0) {
1564 ret = -EFAULT;
1565 goto free_newinfo;
1566 }
1567
Florian Westphal329a0802016-04-01 14:17:31 +02001568 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001569 if (ret != 0)
1570 goto free_newinfo;
1571
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001572 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001573 tmp.num_counters, compat_ptr(tmp.counters));
1574 if (ret)
1575 goto free_newinfo_untrans;
1576 return 0;
1577
1578 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001579 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001580 cleanup_entry(iter, net);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001581 free_newinfo:
1582 xt_free_table_info(newinfo);
1583 return ret;
1584}
1585
1586static int
1587compat_do_ip6t_set_ctl(struct sock *sk, int cmd, void __user *user,
1588 unsigned int len)
1589{
1590 int ret;
1591
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001592 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001593 return -EPERM;
1594
1595 switch (cmd) {
1596 case IP6T_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001597 ret = compat_do_replace(sock_net(sk), user, len);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001598 break;
1599
1600 case IP6T_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001601 ret = do_add_counters(sock_net(sk), user, len, 1);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001602 break;
1603
1604 default:
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001605 ret = -EINVAL;
1606 }
1607
1608 return ret;
1609}
1610
1611struct compat_ip6t_get_entries {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001612 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001613 compat_uint_t size;
1614 struct compat_ip6t_entry entrytable[0];
1615};
1616
1617static int
1618compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1619 void __user *userptr)
1620{
1621 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001622 const struct xt_table_info *private = table->private;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001623 void __user *pos;
1624 unsigned int size;
1625 int ret = 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001626 unsigned int i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001627 struct ip6t_entry *iter;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001628
1629 counters = alloc_counters(table);
1630 if (IS_ERR(counters))
1631 return PTR_ERR(counters);
1632
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001633 pos = userptr;
1634 size = total_size;
Florian Westphal482cfc32015-06-11 01:34:55 +02001635 xt_entry_foreach(iter, private->entries, total_size) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001636 ret = compat_copy_entry_to_user(iter, &pos,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001637 &size, counters, i++);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001638 if (ret != 0)
1639 break;
1640 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001641
1642 vfree(counters);
1643 return ret;
1644}
1645
1646static int
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001647compat_get_entries(struct net *net, struct compat_ip6t_get_entries __user *uptr,
1648 int *len)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001649{
1650 int ret;
1651 struct compat_ip6t_get_entries get;
1652 struct xt_table *t;
1653
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001654 if (*len < sizeof(get))
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001655 return -EINVAL;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001656
1657 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1658 return -EFAULT;
1659
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001660 if (*len != sizeof(struct compat_ip6t_get_entries) + get.size)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001661 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001662
Pablo Neira Ayusob301f252016-03-24 21:29:53 +01001663 get.name[sizeof(get.name) - 1] = '\0';
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001664
1665 xt_compat_lock(AF_INET6);
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001666 t = xt_find_table_lock(net, AF_INET6, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001667 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001668 const struct xt_table_info *private = t->private;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001669 struct xt_table_info info;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001670 ret = compat_table_info(private, &info);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001671 if (!ret && get.size == info.size)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001672 ret = compat_copy_entries_to_user(private->size,
1673 t, uptr->entrytable);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001674 else if (!ret)
Patrick McHardy544473c2008-04-14 11:15:45 +02001675 ret = -EAGAIN;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001676
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001677 xt_compat_flush_offsets(AF_INET6);
1678 module_put(t->me);
1679 xt_table_unlock(t);
1680 } else
1681 ret = t ? PTR_ERR(t) : -ENOENT;
1682
1683 xt_compat_unlock(AF_INET6);
1684 return ret;
1685}
1686
1687static int do_ip6t_get_ctl(struct sock *, int, void __user *, int *);
1688
1689static int
1690compat_do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1691{
1692 int ret;
1693
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001694 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001695 return -EPERM;
1696
1697 switch (cmd) {
1698 case IP6T_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001699 ret = get_info(sock_net(sk), user, len, 1);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001700 break;
1701 case IP6T_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001702 ret = compat_get_entries(sock_net(sk), user, len);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001703 break;
1704 default:
1705 ret = do_ip6t_get_ctl(sk, cmd, user, len);
1706 }
1707 return ret;
1708}
1709#endif
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711static int
1712do_ip6t_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1713{
1714 int ret;
1715
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001716 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return -EPERM;
1718
1719 switch (cmd) {
1720 case IP6T_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001721 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 break;
1723
1724 case IP6T_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001725 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 break;
1727
1728 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 ret = -EINVAL;
1730 }
1731
1732 return ret;
1733}
1734
1735static int
1736do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1737{
1738 int ret;
1739
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001740 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return -EPERM;
1742
1743 switch (cmd) {
Patrick McHardy433665c2007-12-17 21:50:05 -08001744 case IP6T_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001745 ret = get_info(sock_net(sk), user, len, 0);
Patrick McHardy433665c2007-12-17 21:50:05 -08001746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Patrick McHardyd9243572007-12-17 21:50:22 -08001748 case IP6T_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001749 ret = get_entries(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Harald Welte6b7d31f2005-10-26 09:34:24 +02001752 case IP6T_SO_GET_REVISION_MATCH:
1753 case IP6T_SO_GET_REVISION_TARGET: {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001754 struct xt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001755 int target;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001756
1757 if (*len != sizeof(rev)) {
1758 ret = -EINVAL;
1759 break;
1760 }
1761 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1762 ret = -EFAULT;
1763 break;
1764 }
Vasiliy Kulikov6a8ab0602011-03-15 13:37:13 +01001765 rev.name[sizeof(rev.name)-1] = 0;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001766
1767 if (cmd == IP6T_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001768 target = 1;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001769 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08001770 target = 0;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001771
Harald Welte2e4e6a12006-01-12 13:30:04 -08001772 try_then_request_module(xt_find_revision(AF_INET6, rev.name,
1773 rev.revision,
1774 target, &ret),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001775 "ip6t_%s", rev.name);
1776 break;
1777 }
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 ret = -EINVAL;
1781 }
1782
1783 return ret;
1784}
1785
Florian Westphalb9e69e12016-02-25 10:08:36 +01001786static void __ip6t_unregister_table(struct net *net, struct xt_table *table)
1787{
1788 struct xt_table_info *private;
1789 void *loc_cpu_entry;
1790 struct module *table_owner = table->me;
1791 struct ip6t_entry *iter;
1792
1793 private = xt_unregister_table(table);
1794
1795 /* Decrease module usage counts and free resources */
1796 loc_cpu_entry = private->entries;
1797 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1798 cleanup_entry(iter, net);
1799 if (private->number > private->initial_entries)
1800 module_put(table_owner);
1801 xt_free_table_info(private);
1802}
1803
Florian Westphala67dd262016-02-25 10:08:35 +01001804int ip6t_register_table(struct net *net, const struct xt_table *table,
1805 const struct ip6t_replace *repl,
1806 const struct nf_hook_ops *ops,
1807 struct xt_table **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001810 struct xt_table_info *newinfo;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001811 struct xt_table_info bootstrap = {0};
Eric Dumazet31836062005-12-13 23:13:48 -08001812 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08001813 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Harald Welte2e4e6a12006-01-12 13:30:04 -08001815 newinfo = xt_alloc_table_info(repl->size);
Florian Westphala67dd262016-02-25 10:08:35 +01001816 if (!newinfo)
1817 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Florian Westphal482cfc32015-06-11 01:34:55 +02001819 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -08001820 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Jan Engelhardt0f234212010-02-24 18:36:04 +01001822 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001823 if (ret != 0)
1824 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001826 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001827 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001828 ret = PTR_ERR(new_table);
1829 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
Florian Westphala67dd262016-02-25 10:08:35 +01001831
Florian Westphalb9e69e12016-02-25 10:08:36 +01001832 /* set res now, will see skbs right after nf_register_net_hooks */
Florian Westphala67dd262016-02-25 10:08:35 +01001833 WRITE_ONCE(*res, new_table);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001834
1835 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1836 if (ret != 0) {
1837 __ip6t_unregister_table(net, new_table);
1838 *res = NULL;
1839 }
1840
Florian Westphala67dd262016-02-25 10:08:35 +01001841 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001843out_free:
1844 xt_free_table_info(newinfo);
Florian Westphala67dd262016-02-25 10:08:35 +01001845 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Florian Westphala67dd262016-02-25 10:08:35 +01001848void ip6t_unregister_table(struct net *net, struct xt_table *table,
1849 const struct nf_hook_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Florian Westphalb9e69e12016-02-25 10:08:36 +01001851 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1852 __ip6t_unregister_table(net, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
1855/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07001856static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857icmp6_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1858 u_int8_t type, u_int8_t code,
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07001859 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 return (type == test_type && code >= min_code && code <= max_code)
1862 ^ invert;
1863}
1864
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001865static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +02001866icmp6_match(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Jan Engelhardt5452e422008-04-14 11:15:35 +02001868 const struct icmp6hdr *ic;
1869 struct icmp6hdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001870 const struct ip6t_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001873 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001874 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001876 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (ic == NULL) {
1878 /* We've been asked to examine this packet, and we
Patrick McHardy9c547952007-12-17 21:52:00 -08001879 * can't. Hence, no choice but to drop.
1880 */
Jan Engelhardtb4ba2612009-07-07 20:54:30 +02001881 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001882 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884
1885 return icmp6_type_code_match(icmpinfo->type,
1886 icmpinfo->code[0],
1887 icmpinfo->code[1],
1888 ic->icmp6_type, ic->icmp6_code,
1889 !!(icmpinfo->invflags&IP6T_ICMP_INV));
1890}
1891
1892/* Called when user tries to insert an entry of this type. */
Jan Engelhardtb0f38452010-03-19 17:16:42 +01001893static int icmp6_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001895 const struct ip6t_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Patrick McHardy7f939712006-03-20 18:01:43 -08001897 /* Must specify no unknown invflags */
Jan Engelhardtbd414ee2010-03-23 16:35:56 +01001898 return (icmpinfo->invflags & ~IP6T_ICMP_INV) ? -EINVAL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
1901/* The built-in targets: standard (NULL) and error. */
Jan Engelhardt45385062009-07-04 12:50:00 +02001902static struct xt_target ip6t_builtin_tg[] __read_mostly = {
1903 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001904 .name = XT_STANDARD_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001905 .targetsize = sizeof(int),
1906 .family = NFPROTO_IPV6,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001907#ifdef CONFIG_COMPAT
Jan Engelhardt45385062009-07-04 12:50:00 +02001908 .compatsize = sizeof(compat_int_t),
1909 .compat_from_user = compat_standard_from_user,
1910 .compat_to_user = compat_standard_to_user,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001911#endif
Jan Engelhardt45385062009-07-04 12:50:00 +02001912 },
1913 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001914 .name = XT_ERROR_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001915 .target = ip6t_error,
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001916 .targetsize = XT_FUNCTION_MAXNAMELEN,
Jan Engelhardt45385062009-07-04 12:50:00 +02001917 .family = NFPROTO_IPV6,
1918 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919};
1920
1921static struct nf_sockopt_ops ip6t_sockopts = {
1922 .pf = PF_INET6,
1923 .set_optmin = IP6T_BASE_CTL,
1924 .set_optmax = IP6T_SO_SET_MAX+1,
1925 .set = do_ip6t_set_ctl,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001926#ifdef CONFIG_COMPAT
1927 .compat_set = compat_do_ip6t_set_ctl,
1928#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 .get_optmin = IP6T_BASE_CTL,
1930 .get_optmax = IP6T_SO_GET_MAX+1,
1931 .get = do_ip6t_get_ctl,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001932#ifdef CONFIG_COMPAT
1933 .compat_get = compat_do_ip6t_get_ctl,
1934#endif
Neil Horman16fcec32007-09-11 11:28:26 +02001935 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936};
1937
Jan Engelhardt45385062009-07-04 12:50:00 +02001938static struct xt_match ip6t_builtin_mt[] __read_mostly = {
1939 {
1940 .name = "icmp6",
1941 .match = icmp6_match,
1942 .matchsize = sizeof(struct ip6t_icmp),
1943 .checkentry = icmp6_checkentry,
1944 .proto = IPPROTO_ICMPV6,
1945 .family = NFPROTO_IPV6,
Florian Westphal894b7532018-07-04 20:25:32 +02001946 .me = THIS_MODULE,
Jan Engelhardt45385062009-07-04 12:50:00 +02001947 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948};
1949
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001950static int __net_init ip6_tables_net_init(struct net *net)
1951{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02001952 return xt_proto_init(net, NFPROTO_IPV6);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001953}
1954
1955static void __net_exit ip6_tables_net_exit(struct net *net)
1956{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02001957 xt_proto_fini(net, NFPROTO_IPV6);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001958}
1959
1960static struct pernet_operations ip6_tables_net_ops = {
1961 .init = ip6_tables_net_init,
1962 .exit = ip6_tables_net_exit,
1963};
1964
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001965static int __init ip6_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 int ret;
1968
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001969 ret = register_pernet_subsys(&ip6_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001970 if (ret < 0)
1971 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001972
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001973 /* No one else will be downing sem now, so we won't sleep */
Jan Engelhardt45385062009-07-04 12:50:00 +02001974 ret = xt_register_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001975 if (ret < 0)
1976 goto err2;
Jan Engelhardt45385062009-07-04 12:50:00 +02001977 ret = xt_register_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001978 if (ret < 0)
1979 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 /* Register setsockopt */
1982 ret = nf_register_sockopt(&ip6t_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001983 if (ret < 0)
1984 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01001986 pr_info("(C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001988
1989err5:
Jan Engelhardt45385062009-07-04 12:50:00 +02001990 xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001991err4:
Jan Engelhardt45385062009-07-04 12:50:00 +02001992 xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001993err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001994 unregister_pernet_subsys(&ip6_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001995err1:
1996 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001999static void __exit ip6_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 nf_unregister_sockopt(&ip6t_sockopts);
Patrick McHardy9c547952007-12-17 21:52:00 -08002002
Jan Engelhardt45385062009-07-04 12:50:00 +02002003 xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
2004 xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08002005 unregister_pernet_subsys(&ip6_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
2008EXPORT_SYMBOL(ip6t_register_table);
2009EXPORT_SYMBOL(ip6t_unregister_table);
2010EXPORT_SYMBOL(ip6t_do_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Andrew Morton65b4b4e2006-03-28 16:37:06 -08002012module_init(ip6_tables_init);
2013module_exit(ip6_tables_fini);