blob: 55aacea24396dc17d429896c5ff77e5e8162a52e [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Joe Perchesc37a2df2016-06-24 13:25:22 -070076 if (NF_INVF(ip6info, IP6T_INV_SRCIP,
77 ipv6_masked_addr_cmp(&ipv6->saddr, &ip6info->smsk,
78 &ip6info->src)) ||
79 NF_INVF(ip6info, IP6T_INV_DSTIP,
80 ipv6_masked_addr_cmp(&ipv6->daddr, &ip6info->dmsk,
81 &ip6info->dst)))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070082 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Eric Dumazetb8dfe492009-03-25 17:31:52 +010084 ret = ifname_compare_aligned(indev, ip6info->iniface, ip6info->iniface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Joe Perchesc37a2df2016-06-24 13:25:22 -070086 if (NF_INVF(ip6info, IP6T_INV_VIA_IN, ret != 0))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070087 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Eric Dumazetb8dfe492009-03-25 17:31:52 +010089 ret = ifname_compare_aligned(outdev, ip6info->outiface, ip6info->outiface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Joe Perchesc37a2df2016-06-24 13:25:22 -070091 if (NF_INVF(ip6info, IP6T_INV_VIA_OUT, ret != 0))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070092 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/* ... might want to do something with class and flowlabel here ... */
95
96 /* look for the desired protocol header */
Ian Morris4305ae42015-10-11 17:32:19 +010097 if (ip6info->flags & IP6T_F_PROTO) {
Patrick McHardyb777e0c2006-01-05 12:21:16 -080098 int protohdr;
99 unsigned short _frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Hans Schillstrom84018f52012-04-23 03:35:26 +0000101 protohdr = ipv6_find_hdr(skb, protoff, -1, &_frag_off, NULL);
Patrick McHardy51d8b1a2006-10-24 16:14:04 -0700102 if (protohdr < 0) {
103 if (_frag_off == 0)
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700104 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700105 return false;
Patrick McHardy51d8b1a2006-10-24 16:14:04 -0700106 }
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800107 *fragoff = _frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Patrick McHardyb777e0c2006-01-05 12:21:16 -0800109 if (ip6info->proto == protohdr) {
Ian Morris4305ae42015-10-11 17:32:19 +0100110 if (ip6info->invflags & IP6T_INV_PROTO)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700111 return false;
Ian Morris4305ae42015-10-11 17:32:19 +0100112
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700113 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115
116 /* We need match for the '-p all', too! */
117 if ((ip6info->proto != 0) &&
118 !(ip6info->invflags & IP6T_INV_PROTO))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700119 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700121 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124/* should be ip6 safe */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800125static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126ip6_checkentry(const struct ip6t_ip6 *ipv6)
127{
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200128 if (ipv6->flags & ~IP6T_F_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700129 return false;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200130 if (ipv6->invflags & ~IP6T_INV_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700131 return false;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200132
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700133 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200137ip6t_error(struct sk_buff *skb, const struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Joe Perchese87cc472012-05-13 21:56:26 +0000139 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 return NF_DROP;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static inline struct ip6t_entry *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200145get_entry(const void *base, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 return (struct ip6t_entry *)(base + offset);
148}
149
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700150/* All zeroes == unconditional rule. */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800151/* Mildly perf critical (only if packet tracing is on) */
Florian Westphal54d83fc2016-03-22 18:02:52 +0100152static inline bool unconditional(const struct ip6t_entry *e)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700153{
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200154 static const struct ip6t_ip6 uncond;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700155
Florian Westphal54d83fc2016-03-22 18:02:52 +0100156 return e->target_offset == sizeof(struct ip6t_entry) &&
157 memcmp(&e->ipv6, &uncond, sizeof(uncond)) == 0;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700158}
159
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200160static inline const struct xt_entry_target *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200161ip6t_get_target_c(const struct ip6t_entry *e)
162{
163 return ip6t_get_target((struct ip6t_entry *)e);
164}
165
Amerigo Wang07a93622012-10-29 16:23:10 +0000166#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700167/* This cries for unification! */
Denys Vlasenko022748a2008-01-14 23:44:05 -0800168static const char *const hooknames[] = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800169 [NF_INET_PRE_ROUTING] = "PREROUTING",
170 [NF_INET_LOCAL_IN] = "INPUT",
171 [NF_INET_FORWARD] = "FORWARD",
172 [NF_INET_LOCAL_OUT] = "OUTPUT",
173 [NF_INET_POST_ROUTING] = "POSTROUTING",
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700174};
175
176enum nf_ip_trace_comments {
177 NF_IP6_TRACE_COMMENT_RULE,
178 NF_IP6_TRACE_COMMENT_RETURN,
179 NF_IP6_TRACE_COMMENT_POLICY,
180};
181
Denys Vlasenko022748a2008-01-14 23:44:05 -0800182static const char *const comments[] = {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700183 [NF_IP6_TRACE_COMMENT_RULE] = "rule",
184 [NF_IP6_TRACE_COMMENT_RETURN] = "return",
185 [NF_IP6_TRACE_COMMENT_POLICY] = "policy",
186};
187
188static struct nf_loginfo trace_loginfo = {
189 .type = NF_LOG_TYPE_LOG,
190 .u = {
191 .log = {
Joe Perchesa81b2ce2015-03-23 11:50:10 -0700192 .level = LOGLEVEL_WARNING,
Liping Zhangff107d22016-09-25 16:35:56 +0800193 .logflags = NF_LOG_DEFAULT_MASK,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700194 },
195 },
196};
197
Denys Vlasenko022748a2008-01-14 23:44:05 -0800198/* Mildly perf critical (only if packet tracing is on) */
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700199static inline int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200200get_chainname_rulenum(const struct ip6t_entry *s, const struct ip6t_entry *e,
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200201 const char *hookname, const char **chainname,
202 const char **comment, unsigned int *rulenum)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700203{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200204 const struct xt_standard_target *t = (void *)ip6t_get_target_c(s);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700205
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200206 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700207 /* Head of user chain: ERROR target with chainname */
208 *chainname = t->target.data;
209 (*rulenum) = 0;
210 } else if (s == e) {
211 (*rulenum)++;
212
Florian Westphal54d83fc2016-03-22 18:02:52 +0100213 if (unconditional(s) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100214 strcmp(t->target.u.kernel.target->name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200215 XT_STANDARD_TARGET) == 0 &&
Florian Westphal54d83fc2016-03-22 18:02:52 +0100216 t->verdict < 0) {
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700217 /* Tail of chains: STANDARD target (return/policy) */
218 *comment = *chainname == hookname
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200219 ? comments[NF_IP6_TRACE_COMMENT_POLICY]
220 : comments[NF_IP6_TRACE_COMMENT_RETURN];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700221 }
222 return 1;
223 } else
224 (*rulenum)++;
225
226 return 0;
227}
228
Eric W. Biederman9dff2c92015-09-15 20:04:17 -0500229static void trace_packet(struct net *net,
230 const struct sk_buff *skb,
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700231 unsigned int hook,
232 const struct net_device *in,
233 const struct net_device *out,
Jan Engelhardtecb6f852008-01-31 03:54:47 -0800234 const char *tablename,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200235 const struct xt_table_info *private,
236 const struct ip6t_entry *e)
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700237{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200238 const struct ip6t_entry *root;
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200239 const char *hookname, *chainname, *comment;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100240 const struct ip6t_entry *iter;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700241 unsigned int rulenum = 0;
242
Florian Westphal482cfc32015-06-11 01:34:55 +0200243 root = get_entry(private->entries, private->hook_entry[hook]);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700244
Jan Engelhardt4f2f6f22009-04-15 20:31:13 +0200245 hookname = chainname = hooknames[hook];
246 comment = comments[NF_IP6_TRACE_COMMENT_RULE];
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700247
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100248 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
249 if (get_chainname_rulenum(iter, e, hookname,
250 &chainname, &comment, &rulenum) != 0)
251 break;
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700252
Pablo Neira Ayuso4017a7e2015-03-02 01:10:28 +0100253 nf_log_trace(net, AF_INET6, hook, skb, in, out, &trace_loginfo,
254 "TRACE: %s:%s:%s:%u ",
255 tablename, chainname, comment, rulenum);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700256}
257#endif
258
Florian Westphal6c7941d2015-07-14 17:51:10 +0200259static inline struct ip6t_entry *
Jan Engelhardt98e86402009-04-15 21:06:05 +0200260ip6t_next_entry(const struct ip6t_entry *entry)
261{
262 return (void *)entry + entry->next_offset;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/* Returns one of the generic firewall policies, like NF_ACCEPT. */
266unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700267ip6t_do_table(struct sk_buff *skb,
David S. Miller8f8a3712015-04-03 21:09:51 -0400268 const struct nf_hook_state *state,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700269 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Eric W. Biederman6cb8ff3f12015-09-18 14:32:55 -0500271 unsigned int hook = state->hook;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200272 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Initializing verdict to NF_DROP keeps gcc happy. */
274 unsigned int verdict = NF_DROP;
275 const char *indev, *outdev;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200276 const void *table_base;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200277 struct ip6t_entry *e, **jumpstack;
Florian Westphal7814b6e2015-07-14 17:51:08 +0200278 unsigned int stackidx, cpu;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200279 const struct xt_table_info *private;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200280 struct xt_action_param acpar;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200281 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* Initialization */
Florian Westphal7814b6e2015-07-14 17:51:08 +0200284 stackidx = 0;
David S. Miller8f8a3712015-04-03 21:09:51 -0400285 indev = state->in ? state->in->name : nulldevname;
286 outdev = state->out ? state->out->name : nulldevname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* We handle fragments by dealing with the first fragment as
288 * if it was a normal packet. All other fragments are treated
289 * normally, except that they will NEVER match rules that ask
290 * things we don't know, ie. tcp syn flag or ports). If the
291 * rule is also a fragment-specific rule, non-fragments won't
292 * match it. */
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200293 acpar.hotdrop = false;
Eric W. Biederman156c1962015-09-18 14:32:58 -0500294 acpar.net = state->net;
David S. Miller8f8a3712015-04-03 21:09:51 -0400295 acpar.in = state->in;
296 acpar.out = state->out;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200297 acpar.family = NFPROTO_IPV6;
298 acpar.hooknum = hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
Stephen Hemminger78454472009-02-20 10:35:32 +0100301
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200302 local_bh_disable();
303 addend = xt_write_recseq_begin();
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700304 private = table->private;
Will Deaconb416c142013-10-21 13:14:53 +0100305 /*
306 * Ensure we load private-> members after we've fetched the base
307 * pointer.
308 */
309 smp_read_barrier_depends();
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200310 cpu = smp_processor_id();
Florian Westphal482cfc32015-06-11 01:34:55 +0200311 table_base = private->entries;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200312 jumpstack = (struct ip6t_entry **)private->jumpstack[cpu];
Florian Westphal7814b6e2015-07-14 17:51:08 +0200313
314 /* Switch to alternate jumpstack if we're being invoked via TEE.
315 * TEE issues XT_CONTINUE verdict on original skb so we must not
316 * clobber the jumpstack.
317 *
318 * For recursion via REJECT or SYNPROXY the stack will be clobbered
319 * but it is no problem since absolute verdict is issued by these.
320 */
Florian Westphaldcebd312015-07-14 17:51:09 +0200321 if (static_key_false(&xt_tee_enabled))
322 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
Stephen Hemminger78454472009-02-20 10:35:32 +0100323
Harald Welte2e4e6a12006-01-12 13:30:04 -0800324 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 do {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200327 const struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100328 const struct xt_entry_match *ematch;
Florian Westphal71ae0df2015-06-11 01:34:54 +0200329 struct xt_counters *counter;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 IP_NF_ASSERT(e);
Hans Schillstrom84018f52012-04-23 03:35:26 +0000332 acpar.thoff = 0;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200333 if (!ip6_packet_match(skb, indev, outdev, &e->ipv6,
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200334 &acpar.thoff, &acpar.fragoff, &acpar.hotdrop)) {
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100335 no_match:
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200336 e = ip6t_next_entry(e);
337 continue;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jan Engelhardtef53d702009-07-09 19:14:18 +0200340 xt_ematch_foreach(ematch, e) {
Jan Engelhardtde74c162009-07-05 18:26:37 +0200341 acpar.match = ematch->u.kernel.match;
342 acpar.matchinfo = ematch->data;
343 if (!acpar.match->match(skb, &acpar))
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100344 goto no_match;
Jan Engelhardtef53d702009-07-09 19:14:18 +0200345 }
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100346
Florian Westphal71ae0df2015-06-11 01:34:54 +0200347 counter = xt_get_this_cpu_counter(&e->counters);
348 ADD_COUNTER(*counter, skb->len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200350 t = ip6t_get_target_c(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200351 IP_NF_ASSERT(t->u.kernel.target);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700352
Amerigo Wang07a93622012-10-29 16:23:10 +0000353#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200354 /* The packet is traced: log it */
355 if (unlikely(skb->nf_trace))
Eric W. Biederman9dff2c92015-09-15 20:04:17 -0500356 trace_packet(state->net, skb, hook, state->in,
357 state->out, table->name, private, e);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700358#endif
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200359 /* Standard target? */
360 if (!t->u.kernel.target->target) {
361 int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200363 v = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200364 if (v < 0) {
365 /* Pop from stack? */
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200366 if (v != XT_RETURN) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000367 verdict = (unsigned int)(-v) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 break;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200369 }
Florian Westphal7814b6e2015-07-14 17:51:08 +0200370 if (stackidx == 0)
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200371 e = get_entry(table_base,
372 private->underflow[hook]);
373 else
Florian Westphal7814b6e2015-07-14 17:51:08 +0200374 e = ip6t_next_entry(jumpstack[--stackidx]);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200375 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Joe Perches3666ed12009-11-23 23:17:06 +0100377 if (table_base + v != ip6t_next_entry(e) &&
378 !(e->ipv6.flags & IP6T_F_GOTO)) {
Florian Westphal7814b6e2015-07-14 17:51:08 +0200379 jumpstack[stackidx++] = e;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200382 e = get_entry(table_base, v);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200383 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200385
Jan Engelhardtde74c162009-07-05 18:26:37 +0200386 acpar.target = t->u.kernel.target;
387 acpar.targinfo = t->data;
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200388
Jan Engelhardtde74c162009-07-05 18:26:37 +0200389 verdict = t->u.kernel.target->target(skb, &acpar);
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200390 if (verdict == XT_CONTINUE)
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200391 e = ip6t_next_entry(e);
392 else
393 /* Verdict */
394 break;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200395 } while (!acpar.hotdrop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Ian Morris76954952015-10-11 17:32:16 +0100397 xt_write_recseq_end(addend);
398 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200400 if (acpar.hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return NF_DROP;
402 else return verdict;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/* Figures out from what hook each rule can be called: returns 0 if
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200406 there are loops. Puts hook bitmask in comefrom. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static int
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200408mark_source_chains(const struct xt_table_info *newinfo,
Florian Westphalf4dc7772016-07-14 17:51:26 +0200409 unsigned int valid_hooks, void *entry0,
410 unsigned int *offsets)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 unsigned int hook;
413
414 /* No recursion; use packet counter to save back ptrs (reset
415 to 0 as we leave), and comefrom to save source hook bitmask */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800416 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 unsigned int pos = newinfo->hook_entry[hook];
Patrick McHardy9c547952007-12-17 21:52:00 -0800418 struct ip6t_entry *e = (struct ip6t_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (!(valid_hooks & (1 << hook)))
421 continue;
422
423 /* Set initial back pointer. */
424 e->counters.pcnt = pos;
425
426 for (;;) {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200427 const struct xt_standard_target *t
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200428 = (void *)ip6t_get_target_c(e);
Patrick McHardy9c547952007-12-17 21:52:00 -0800429 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200431 if (e->comefrom & (1 << NF_INET_NUMHOOKS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return 0;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200433
Patrick McHardy9c547952007-12-17 21:52:00 -0800434 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* Unconditional return/END. */
Florian Westphal54d83fc2016-03-22 18:02:52 +0100437 if ((unconditional(e) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100438 (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200439 XT_STANDARD_TARGET) == 0) &&
Florian Westphal54d83fc2016-03-22 18:02:52 +0100440 t->verdict < 0) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned int oldpos, size;
442
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100443 if ((strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200444 XT_STANDARD_TARGET) == 0) &&
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200445 t->verdict < -NF_MAX_VERDICT - 1)
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800446 return 0;
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* Return: backtrack through the last
449 big jump. */
450 do {
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800451 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 oldpos = pos;
453 pos = e->counters.pcnt;
454 e->counters.pcnt = 0;
455
456 /* We're at the start. */
457 if (pos == oldpos)
458 goto next;
459
460 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800461 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 } while (oldpos == pos + e->next_offset);
463
464 /* Move along one */
465 size = e->next_offset;
466 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800467 (entry0 + pos + size);
Florian Westphalf24e2302016-04-01 14:17:21 +0200468 if (pos + size >= newinfo->size)
469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 e->counters.pcnt = pos;
471 pos += size;
472 } else {
473 int newpos = t->verdict;
474
475 if (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200476 XT_STANDARD_TARGET) == 0 &&
Joe Perches3666ed12009-11-23 23:17:06 +0100477 newpos >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* This a jump; chase it. */
Florian Westphalf4dc7772016-07-14 17:51:26 +0200479 if (!xt_find_jump_offset(offsets, newpos,
480 newinfo->number))
481 return 0;
Florian Westphal36472342016-04-01 14:17:22 +0200482 e = (struct ip6t_entry *)
483 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 } else {
485 /* ... this is a fallthru */
486 newpos = pos + e->next_offset;
Florian Westphalf24e2302016-04-01 14:17:21 +0200487 if (newpos >= newinfo->size)
488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 e = (struct ip6t_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800491 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 e->counters.pcnt = pos;
493 pos = newpos;
494 }
495 }
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200496next: ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 return 1;
499}
500
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200501static void cleanup_match(struct xt_entry_match *m, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200503 struct xt_mtdtor_param par;
504
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100505 par.net = net;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200506 par.match = m->u.kernel.match;
507 par.matchinfo = m->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200508 par.family = NFPROTO_IPV6;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200509 if (par.match->destroy != NULL)
510 par.match->destroy(&par);
511 module_put(par.match->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200514static int check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800515{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200516 const struct ip6t_ip6 *ipv6 = par->entryinfo;
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800517
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200518 par->match = m->u.kernel.match;
519 par->matchinfo = m->data;
520
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200521 return xt_check_match(par, m->u.match_size - sizeof(*m),
522 ipv6->proto, ipv6->invflags & IP6T_INV_PROTO);
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800523}
524
Denys Vlasenko022748a2008-01-14 23:44:05 -0800525static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200526find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800528 struct xt_match *match;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800529 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200531 match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
532 m->u.user.revision);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200533 if (IS_ERR(match))
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200534 return PTR_ERR(match);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 m->u.kernel.match = match;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100538 ret = check_match(m, par);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800539 if (ret)
540 goto err;
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800543err:
544 module_put(m->u.kernel.match->me);
545 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Patrick McHardyadd67462010-02-03 13:45:12 +0100548static int check_target(struct ip6t_entry *e, struct net *net, const char *name)
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800549{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200550 struct xt_entry_target *t = ip6t_get_target(e);
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200551 struct xt_tgchk_param par = {
Patrick McHardyadd67462010-02-03 13:45:12 +0100552 .net = net,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200553 .table = name,
554 .entryinfo = e,
555 .target = t->u.kernel.target,
556 .targinfo = t->data,
557 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200558 .family = NFPROTO_IPV6,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200559 };
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800560
561 t = ip6t_get_target(e);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200562 return xt_check_target(&par, t->u.target_size - sizeof(*t),
563 e->ipv6.proto,
564 e->ipv6.invflags & IP6T_INV_PROTO);
Patrick McHardyf173c8a2007-12-17 21:48:17 -0800565}
566
Denys Vlasenko022748a2008-01-14 23:44:05 -0800567static int
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100568find_check_entry(struct ip6t_entry *e, struct net *net, const char *name,
Jan Engelhardt05595182010-02-24 18:33:43 +0100569 unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200571 struct xt_entry_target *t;
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800572 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int ret;
574 unsigned int j;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200575 struct xt_mtchk_param mtpar;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100576 struct xt_entry_match *ematch;
Pablo Neira Ayuso92b44232016-04-29 10:39:34 +0200577 unsigned long pcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Pablo Neira Ayuso92b44232016-04-29 10:39:34 +0200579 pcnt = xt_percpu_counter_alloc();
580 if (IS_ERR_VALUE(pcnt))
Florian Westphal71ae0df2015-06-11 01:34:54 +0200581 return -ENOMEM;
Pablo Neira Ayuso92b44232016-04-29 10:39:34 +0200582 e->counters.pcnt = pcnt;
Florian Westphal71ae0df2015-06-11 01:34:54 +0200583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 j = 0;
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100585 mtpar.net = net;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200586 mtpar.table = name;
587 mtpar.entryinfo = &e->ipv6;
588 mtpar.hook_mask = e->comefrom;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200589 mtpar.family = NFPROTO_IPV6;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100590 xt_ematch_foreach(ematch, e) {
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100591 ret = find_check_match(ematch, &mtpar);
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100592 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100593 goto cleanup_matches;
594 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 t = ip6t_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200598 target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name,
599 t->u.user.revision);
600 if (IS_ERR(target)) {
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200601 ret = PTR_ERR(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 goto cleanup_matches;
603 }
604 t->u.kernel.target = target;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200605
Patrick McHardyadd67462010-02-03 13:45:12 +0100606 ret = check_target(e, net, name);
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800607 if (ret)
608 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800610 err:
611 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 cleanup_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100613 xt_ematch_foreach(ematch, e) {
614 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100615 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100616 cleanup_match(ematch, net);
617 }
Florian Westphal71ae0df2015-06-11 01:34:54 +0200618
619 xt_percpu_counter_free(e->counters.pcnt);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return ret;
622}
623
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200624static bool check_underflow(const struct ip6t_entry *e)
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200625{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200626 const struct xt_entry_target *t;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200627 unsigned int verdict;
628
Florian Westphal54d83fc2016-03-22 18:02:52 +0100629 if (!unconditional(e))
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200630 return false;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200631 t = ip6t_get_target_c(e);
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200632 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
633 return false;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200634 verdict = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200635 verdict = -verdict - 1;
636 return verdict == NF_DROP || verdict == NF_ACCEPT;
637}
638
Denys Vlasenko022748a2008-01-14 23:44:05 -0800639static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640check_entry_size_and_hooks(struct ip6t_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800641 struct xt_table_info *newinfo,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200642 const unsigned char *base,
643 const unsigned char *limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 const unsigned int *hook_entries,
645 const unsigned int *underflows,
Jan Engelhardt05595182010-02-24 18:33:43 +0100646 unsigned int valid_hooks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 unsigned int h;
Florian Westphalbdf533d2016-03-22 18:02:49 +0100649 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Joe Perches3666ed12009-11-23 23:17:06 +0100651 if ((unsigned long)e % __alignof__(struct ip6t_entry) != 0 ||
Florian Westphal6e94e0c2016-03-22 18:02:50 +0100652 (unsigned char *)e + sizeof(struct ip6t_entry) >= limit ||
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200653 (unsigned char *)e + e->next_offset > limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (e->next_offset
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200657 < sizeof(struct ip6t_entry) + sizeof(struct xt_entry_target))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Florian Westphalaa412ba2016-04-01 14:17:24 +0200660 if (!ip6_checkentry(&e->ipv6))
661 return -EINVAL;
662
Florian Westphalce683e52016-04-01 14:17:28 +0200663 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
664 e->next_offset);
Florian Westphalbdf533d2016-03-22 18:02:49 +0100665 if (err)
666 return err;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /* Check hooks & underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800669 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
Jan Engelhardta7d51732009-07-18 14:52:58 +0200670 if (!(valid_hooks & (1 << h)))
671 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if ((unsigned char *)e - base == hook_entries[h])
673 newinfo->hook_entry[h] = hook_entries[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200674 if ((unsigned char *)e - base == underflows[h]) {
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200675 if (!check_underflow(e))
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200676 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 newinfo->underflow[h] = underflows[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800683 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 e->comefrom = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return 0;
686}
687
Jan Engelhardt05595182010-02-24 18:33:43 +0100688static void cleanup_entry(struct ip6t_entry *e, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200690 struct xt_tgdtor_param par;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200691 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100692 struct xt_entry_match *ematch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100695 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100696 cleanup_match(ematch, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 t = ip6t_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200698
Patrick McHardyadd67462010-02-03 13:45:12 +0100699 par.net = net;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200700 par.target = t->u.kernel.target;
701 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200702 par.family = NFPROTO_IPV6;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200703 if (par.target->destroy != NULL)
704 par.target->destroy(&par);
705 module_put(par.target->me);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200706
707 xt_percpu_counter_free(e->counters.pcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/* Checks and translates the user-supplied table segment (held in
711 newinfo) */
712static int
Jan Engelhardt0f234212010-02-24 18:36:04 +0100713translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
Ian Morriscda219c2015-10-11 17:32:15 +0100714 const struct ip6t_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100716 struct ip6t_entry *iter;
Florian Westphalf4dc7772016-07-14 17:51:26 +0200717 unsigned int *offsets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 unsigned int i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100719 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Jan Engelhardt0f234212010-02-24 18:36:04 +0100721 newinfo->size = repl->size;
722 newinfo->number = repl->num_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /* Init all hooks to impossible value. */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800725 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 newinfo->hook_entry[i] = 0xFFFFFFFF;
727 newinfo->underflow[i] = 0xFFFFFFFF;
728 }
729
Florian Westphalf4dc7772016-07-14 17:51:26 +0200730 offsets = xt_alloc_entry_offsets(newinfo->number);
731 if (!offsets)
732 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 i = 0;
734 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100735 xt_entry_foreach(iter, entry0, newinfo->size) {
736 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +0100737 entry0 + repl->size,
738 repl->hook_entry,
739 repl->underflow,
740 repl->valid_hooks);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100741 if (ret != 0)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200742 goto out_free;
743 if (i < repl->num_entries)
744 offsets[i] = (void *)iter - entry0;
Jan Engelhardt05595182010-02-24 18:33:43 +0100745 ++i;
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200746 if (strcmp(ip6t_get_target(iter)->u.user.name,
747 XT_ERROR_TARGET) == 0)
748 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Florian Westphalf4dc7772016-07-14 17:51:26 +0200751 ret = -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200752 if (i != repl->num_entries)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200753 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /* Check hooks all assigned */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800756 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* Only hooks which are valid */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100758 if (!(repl->valid_hooks & (1 << i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 continue;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200760 if (newinfo->hook_entry[i] == 0xFFFFFFFF)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200761 goto out_free;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200762 if (newinfo->underflow[i] == 0xFFFFFFFF)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200763 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
Florian Westphalf4dc7772016-07-14 17:51:26 +0200766 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
767 ret = -ELOOP;
768 goto out_free;
769 }
770 kvfree(offsets);
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* Finally, each sanity check must pass */
773 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100774 xt_entry_foreach(iter, entry0, newinfo->size) {
Jan Engelhardt0f234212010-02-24 18:36:04 +0100775 ret = find_check_entry(iter, net, repl->name, repl->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100776 if (ret != 0)
777 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100778 ++i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800781 if (ret != 0) {
Jan Engelhardt05595182010-02-24 18:33:43 +0100782 xt_entry_foreach(iter, entry0, newinfo->size) {
783 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100784 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100785 cleanup_entry(iter, net);
786 }
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800787 return ret;
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Patrick McHardy9c547952007-12-17 21:52:00 -0800790 return ret;
Florian Westphalf4dc7772016-07-14 17:51:26 +0200791 out_free:
792 kvfree(offsets);
793 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796static void
Harald Welte2e4e6a12006-01-12 13:30:04 -0800797get_counters(const struct xt_table_info *t,
798 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100800 struct ip6t_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 unsigned int cpu;
802 unsigned int i;
803
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700804 for_each_possible_cpu(cpu) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200805 seqcount_t *s = &per_cpu(xt_recseq, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 i = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200808 xt_entry_foreach(iter, t->entries, t->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +0200809 struct xt_counters *tmp;
Eric Dumazet83723d62011-01-10 20:11:38 +0100810 u64 bcnt, pcnt;
811 unsigned int start;
812
Florian Westphal71ae0df2015-06-11 01:34:54 +0200813 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100814 do {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200815 start = read_seqcount_begin(s);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200816 bcnt = tmp->bcnt;
817 pcnt = tmp->pcnt;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200818 } while (read_seqcount_retry(s, start));
Eric Dumazet83723d62011-01-10 20:11:38 +0100819
820 ADD_COUNTER(counters[i], bcnt, pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +0100821 ++i;
822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100824}
825
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200826static struct xt_counters *alloc_counters(const struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800828 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800829 struct xt_counters *counters;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200830 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /* We need atomic snapshot of counters: rest doesn't change
833 (other than comefrom, which userspace doesn't care
834 about). */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800835 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet83723d62011-01-10 20:11:38 +0100836 counters = vzalloc(countersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700839 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700841 get_counters(private, counters);
Stephen Hemminger78454472009-02-20 10:35:32 +0100842
Eric Dumazet49a88d12009-04-06 17:06:55 +0200843 return counters;
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800844}
845
846static int
847copy_entries_to_user(unsigned int total_size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200848 const struct xt_table *table,
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800849 void __user *userptr)
850{
851 unsigned int off, num;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200852 const struct ip6t_entry *e;
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800853 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200854 const struct xt_table_info *private = table->private;
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800855 int ret = 0;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700856 const void *loc_cpu_entry;
Patrick McHardyed1a6f52007-12-17 21:49:51 -0800857
858 counters = alloc_counters(table);
859 if (IS_ERR(counters))
860 return PTR_ERR(counters);
861
Florian Westphal482cfc32015-06-11 01:34:55 +0200862 loc_cpu_entry = private->entries;
Eric Dumazet31836062005-12-13 23:13:48 -0800863 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 ret = -EFAULT;
865 goto free_counters;
866 }
867
868 /* FIXME: use iterator macros --RR */
869 /* ... then go back and fix counters and names */
870 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
871 unsigned int i;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200872 const struct xt_entry_match *m;
873 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Eric Dumazet31836062005-12-13 23:13:48 -0800875 e = (struct ip6t_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (copy_to_user(userptr + off
877 + offsetof(struct ip6t_entry, counters),
878 &counters[num],
879 sizeof(counters[num])) != 0) {
880 ret = -EFAULT;
881 goto free_counters;
882 }
883
884 for (i = sizeof(struct ip6t_entry);
885 i < e->target_offset;
886 i += m->u.match_size) {
887 m = (void *)e + i;
888
889 if (copy_to_user(userptr + off + i
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200890 + offsetof(struct xt_entry_match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 u.user.name),
892 m->u.kernel.match->name,
893 strlen(m->u.kernel.match->name)+1)
894 != 0) {
895 ret = -EFAULT;
896 goto free_counters;
897 }
898 }
899
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200900 t = ip6t_get_target_c(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (copy_to_user(userptr + off + e->target_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200902 + offsetof(struct xt_entry_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 u.user.name),
904 t->u.kernel.target->name,
905 strlen(t->u.kernel.target->name)+1) != 0) {
906 ret = -EFAULT;
907 goto free_counters;
908 }
909 }
910
911 free_counters:
912 vfree(counters);
913 return ret;
914}
915
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800916#ifdef CONFIG_COMPAT
Jan Engelhardt739674f2009-06-26 08:23:19 +0200917static void compat_standard_from_user(void *dst, const void *src)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800918{
919 int v = *(compat_int_t *)src;
920
921 if (v > 0)
922 v += xt_compat_calc_jump(AF_INET6, v);
923 memcpy(dst, &v, sizeof(v));
924}
925
Jan Engelhardt739674f2009-06-26 08:23:19 +0200926static int compat_standard_to_user(void __user *dst, const void *src)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800927{
928 compat_int_t cv = *(int *)src;
929
930 if (cv > 0)
931 cv -= xt_compat_calc_jump(AF_INET6, cv);
932 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
933}
934
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200935static int compat_calc_entry(const struct ip6t_entry *e,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800936 const struct xt_table_info *info,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200937 const void *base, struct xt_table_info *newinfo)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800938{
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100939 const struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200940 const struct xt_entry_target *t;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800941 unsigned int entry_offset;
942 int off, i, ret;
943
944 off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
945 entry_offset = (void *)e - base;
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100946 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +0100947 off += xt_compat_match_offset(ematch->u.kernel.match);
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200948 t = ip6t_get_target_c(e);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800949 off += xt_compat_target_offset(t->u.kernel.target);
950 newinfo->size -= off;
951 ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
952 if (ret)
953 return ret;
954
955 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
956 if (info->hook_entry[i] &&
957 (e < (struct ip6t_entry *)(base + info->hook_entry[i])))
958 newinfo->hook_entry[i] -= off;
959 if (info->underflow[i] &&
960 (e < (struct ip6t_entry *)(base + info->underflow[i])))
961 newinfo->underflow[i] -= off;
962 }
963 return 0;
964}
965
966static int compat_table_info(const struct xt_table_info *info,
967 struct xt_table_info *newinfo)
968{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100969 struct ip6t_entry *iter;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700970 const void *loc_cpu_entry;
Jan Engelhardt05595182010-02-24 18:33:43 +0100971 int ret;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800972
973 if (!newinfo || !info)
974 return -EINVAL;
975
Florian Westphal482cfc32015-06-11 01:34:55 +0200976 /* we dont care about newinfo->entries */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800977 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
978 newinfo->initial_entries = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200979 loc_cpu_entry = info->entries;
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100980 xt_compat_init_offsets(AF_INET6, info->number);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100981 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
982 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
983 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +0100984 return ret;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100985 }
Jan Engelhardt05595182010-02-24 18:33:43 +0100986 return 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -0800987}
988#endif
989
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200990static int get_info(struct net *net, void __user *user,
Ian Morriscda219c2015-10-11 17:32:15 +0100991 const int *len, int compat)
Patrick McHardy433665c2007-12-17 21:50:05 -0800992{
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200993 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardy433665c2007-12-17 21:50:05 -0800994 struct xt_table *t;
995 int ret;
996
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200997 if (*len != sizeof(struct ip6t_getinfo))
Patrick McHardy433665c2007-12-17 21:50:05 -0800998 return -EINVAL;
Patrick McHardy433665c2007-12-17 21:50:05 -0800999
1000 if (copy_from_user(name, user, sizeof(name)) != 0)
1001 return -EFAULT;
1002
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001003 name[XT_TABLE_MAXNAMELEN-1] = '\0';
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001004#ifdef CONFIG_COMPAT
1005 if (compat)
1006 xt_compat_lock(AF_INET6);
1007#endif
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001008 t = try_then_request_module(xt_find_table_lock(net, AF_INET6, name),
Patrick McHardy433665c2007-12-17 21:50:05 -08001009 "ip6table_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001010 if (!IS_ERR_OR_NULL(t)) {
Patrick McHardy433665c2007-12-17 21:50:05 -08001011 struct ip6t_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001012 const struct xt_table_info *private = t->private;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001013#ifdef CONFIG_COMPAT
Alexey Dobriyan14c7dbe2010-02-08 11:17:43 -08001014 struct xt_table_info tmp;
1015
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001016 if (compat) {
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001017 ret = compat_table_info(private, &tmp);
1018 xt_compat_flush_offsets(AF_INET6);
1019 private = &tmp;
1020 }
1021#endif
Jan Engelhardtcccbe5e2010-11-03 18:55:39 -07001022 memset(&info, 0, sizeof(info));
Patrick McHardy433665c2007-12-17 21:50:05 -08001023 info.valid_hooks = t->valid_hooks;
1024 memcpy(info.hook_entry, private->hook_entry,
1025 sizeof(info.hook_entry));
1026 memcpy(info.underflow, private->underflow,
1027 sizeof(info.underflow));
1028 info.num_entries = private->number;
1029 info.size = private->size;
Patrick McHardyb5dd6742007-12-17 21:52:35 -08001030 strcpy(info.name, name);
Patrick McHardy433665c2007-12-17 21:50:05 -08001031
1032 if (copy_to_user(user, &info, *len) != 0)
1033 ret = -EFAULT;
1034 else
1035 ret = 0;
1036
1037 xt_table_unlock(t);
1038 module_put(t->me);
1039 } else
1040 ret = t ? PTR_ERR(t) : -ENOENT;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001041#ifdef CONFIG_COMPAT
1042 if (compat)
1043 xt_compat_unlock(AF_INET6);
1044#endif
Patrick McHardy433665c2007-12-17 21:50:05 -08001045 return ret;
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001049get_entries(struct net *net, struct ip6t_get_entries __user *uptr,
Ian Morriscda219c2015-10-11 17:32:15 +01001050 const int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 int ret;
Patrick McHardyd9243572007-12-17 21:50:22 -08001053 struct ip6t_get_entries get;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001054 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001056 if (*len < sizeof(get))
Patrick McHardyd9243572007-12-17 21:50:22 -08001057 return -EINVAL;
Patrick McHardyd9243572007-12-17 21:50:22 -08001058 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1059 return -EFAULT;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001060 if (*len != sizeof(struct ip6t_get_entries) + get.size)
Patrick McHardyd9243572007-12-17 21:50:22 -08001061 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001062
Pablo Neira Ayusob301f252016-03-24 21:29:53 +01001063 get.name[sizeof(get.name) - 1] = '\0';
Patrick McHardyd9243572007-12-17 21:50:22 -08001064
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001065 t = xt_find_table_lock(net, AF_INET6, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001066 if (!IS_ERR_OR_NULL(t)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001067 struct xt_table_info *private = t->private;
Patrick McHardyd9243572007-12-17 21:50:22 -08001068 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001069 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 t, uptr->entrytable);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001071 else
Patrick McHardy544473c2008-04-14 11:15:45 +02001072 ret = -EAGAIN;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001073
Harald Welte6b7d31f2005-10-26 09:34:24 +02001074 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001075 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 } else
Harald Welte6b7d31f2005-10-26 09:34:24 +02001077 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 return ret;
1080}
1081
1082static int
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001083__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001084 struct xt_table_info *newinfo, unsigned int num_counters,
1085 void __user *counters_ptr)
1086{
1087 int ret;
1088 struct xt_table *t;
1089 struct xt_table_info *oldinfo;
1090 struct xt_counters *counters;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001091 struct ip6t_entry *iter;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001092
1093 ret = 0;
Eric Dumazet83723d62011-01-10 20:11:38 +01001094 counters = vzalloc(num_counters * sizeof(struct xt_counters));
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001095 if (!counters) {
1096 ret = -ENOMEM;
1097 goto out;
1098 }
1099
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001100 t = try_then_request_module(xt_find_table_lock(net, AF_INET6, name),
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001101 "ip6table_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001102 if (IS_ERR_OR_NULL(t)) {
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001103 ret = t ? PTR_ERR(t) : -ENOENT;
1104 goto free_newinfo_counters_untrans;
1105 }
1106
1107 /* You lied! */
1108 if (valid_hooks != t->valid_hooks) {
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001109 ret = -EINVAL;
1110 goto put_module;
1111 }
1112
1113 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1114 if (!oldinfo)
1115 goto put_module;
1116
1117 /* Update module usage count based on number of rules */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001118 if ((oldinfo->number > oldinfo->initial_entries) ||
1119 (newinfo->number <= oldinfo->initial_entries))
1120 module_put(t->me);
1121 if ((oldinfo->number > oldinfo->initial_entries) &&
1122 (newinfo->number <= oldinfo->initial_entries))
1123 module_put(t->me);
1124
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001125 /* Get the old counters, and synchronize with replace */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001126 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001127
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001128 /* Decrease module usage counts and free resource */
Florian Westphal482cfc32015-06-11 01:34:55 +02001129 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001130 cleanup_entry(iter, net);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001131
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001132 xt_free_table_info(oldinfo);
1133 if (copy_to_user(counters_ptr, counters,
Thomas Grafc58dd2d2014-04-04 17:57:45 +02001134 sizeof(struct xt_counters) * num_counters) != 0) {
1135 /* Silent error, can't fail, new table is already in place */
1136 net_warn_ratelimited("ip6tables: counters copy to user failed while replacing table\n");
1137 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001138 vfree(counters);
1139 xt_table_unlock(t);
1140 return ret;
1141
1142 put_module:
1143 module_put(t->me);
1144 xt_table_unlock(t);
1145 free_newinfo_counters_untrans:
1146 vfree(counters);
1147 out:
1148 return ret;
1149}
1150
1151static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001152do_replace(struct net *net, const void __user *user, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 int ret;
1155 struct ip6t_replace tmp;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001156 struct xt_table_info *newinfo;
1157 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001158 struct ip6t_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1161 return -EFAULT;
1162
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001163 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001164 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1165 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001166 if (tmp.num_counters == 0)
1167 return -EINVAL;
1168
Vasiliy Kulikov6a8ab0602011-03-15 13:37:13 +01001169 tmp.name[sizeof(tmp.name)-1] = 0;
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001170
Harald Welte2e4e6a12006-01-12 13:30:04 -08001171 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (!newinfo)
1173 return -ENOMEM;
1174
Florian Westphal482cfc32015-06-11 01:34:55 +02001175 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -08001176 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 tmp.size) != 0) {
1178 ret = -EFAULT;
1179 goto free_newinfo;
1180 }
1181
Jan Engelhardt0f234212010-02-24 18:36:04 +01001182 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (ret != 0)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001184 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001186 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001187 tmp.num_counters, tmp.counters);
1188 if (ret)
1189 goto free_newinfo_untrans;
1190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001192 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001193 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001194 cleanup_entry(iter, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001196 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return ret;
1198}
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200static int
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001201do_add_counters(struct net *net, const void __user *user, unsigned int len,
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001202 int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Florian Westphal482cfc32015-06-11 01:34:55 +02001204 unsigned int i;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001205 struct xt_counters_info tmp;
1206 struct xt_counters *paddc;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001207 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001208 const struct xt_table_info *private;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001209 int ret = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001210 struct ip6t_entry *iter;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001211 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Florian Westphald7591f02016-04-01 15:37:59 +02001213 paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1214 if (IS_ERR(paddc))
1215 return PTR_ERR(paddc);
1216 t = xt_find_table_lock(net, AF_INET6, tmp.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001217 if (IS_ERR_OR_NULL(t)) {
Harald Welte6b7d31f2005-10-26 09:34:24 +02001218 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 goto free;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001222 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001223 private = t->private;
Florian Westphald7591f02016-04-01 15:37:59 +02001224 if (private->number != tmp.num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 ret = -EINVAL;
1226 goto unlock_up_free;
1227 }
1228
1229 i = 0;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001230 addend = xt_write_recseq_begin();
Florian Westphal482cfc32015-06-11 01:34:55 +02001231 xt_entry_foreach(iter, private->entries, private->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +02001232 struct xt_counters *tmp;
1233
1234 tmp = xt_get_this_cpu_counter(&iter->counters);
1235 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +01001236 ++i;
1237 }
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001238 xt_write_recseq_end(addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001240 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001241 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001242 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 free:
1244 vfree(paddc);
1245
1246 return ret;
1247}
1248
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001249#ifdef CONFIG_COMPAT
1250struct compat_ip6t_replace {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001251 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001252 u32 valid_hooks;
1253 u32 num_entries;
1254 u32 size;
1255 u32 hook_entry[NF_INET_NUMHOOKS];
1256 u32 underflow[NF_INET_NUMHOOKS];
1257 u32 num_counters;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001258 compat_uptr_t counters; /* struct xt_counters * */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001259 struct compat_ip6t_entry entries[0];
1260};
1261
1262static int
1263compat_copy_entry_to_user(struct ip6t_entry *e, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001264 unsigned int *size, struct xt_counters *counters,
Jan Engelhardt05595182010-02-24 18:33:43 +01001265 unsigned int i)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001266{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001267 struct xt_entry_target *t;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001268 struct compat_ip6t_entry __user *ce;
1269 u_int16_t target_offset, next_offset;
1270 compat_uint_t origsize;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001271 const struct xt_entry_match *ematch;
1272 int ret = 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001273
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001274 origsize = *size;
1275 ce = (struct compat_ip6t_entry __user *)*dstptr;
Jan Engelhardt05595182010-02-24 18:33:43 +01001276 if (copy_to_user(ce, e, sizeof(struct ip6t_entry)) != 0 ||
1277 copy_to_user(&ce->counters, &counters[i],
1278 sizeof(counters[i])) != 0)
1279 return -EFAULT;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001280
1281 *dstptr += sizeof(struct compat_ip6t_entry);
1282 *size -= sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1283
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001284 xt_ematch_foreach(ematch, e) {
1285 ret = xt_compat_match_to_user(ematch, dstptr, size);
1286 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001287 return ret;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001288 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001289 target_offset = e->target_offset - (origsize - *size);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001290 t = ip6t_get_target(e);
1291 ret = xt_compat_target_to_user(t, dstptr, size);
1292 if (ret)
Jan Engelhardt05595182010-02-24 18:33:43 +01001293 return ret;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001294 next_offset = e->next_offset - (origsize - *size);
Jan Engelhardt05595182010-02-24 18:33:43 +01001295 if (put_user(target_offset, &ce->target_offset) != 0 ||
1296 put_user(next_offset, &ce->next_offset) != 0)
1297 return -EFAULT;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001298 return 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001299}
1300
Denys Vlasenko022748a2008-01-14 23:44:05 -08001301static int
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001302compat_find_calc_match(struct xt_entry_match *m,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001303 const struct ip6t_ip6 *ipv6,
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001304 int *size)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001305{
1306 struct xt_match *match;
1307
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001308 match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
1309 m->u.user.revision);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001310 if (IS_ERR(match))
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +02001311 return PTR_ERR(match);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001312
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001313 m->u.kernel.match = match;
1314 *size += xt_compat_match_offset(match);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001315 return 0;
1316}
1317
Jan Engelhardt05595182010-02-24 18:33:43 +01001318static void compat_release_entry(struct compat_ip6t_entry *e)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001319{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001320 struct xt_entry_target *t;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001321 struct xt_entry_match *ematch;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001322
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001323 /* Cleanup all matches */
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001324 xt_ematch_foreach(ematch, e)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001325 module_put(ematch->u.kernel.match->me);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001326 t = compat_ip6t_get_target(e);
1327 module_put(t->u.kernel.target->me);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001328}
1329
Denys Vlasenko022748a2008-01-14 23:44:05 -08001330static int
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001331check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
1332 struct xt_table_info *newinfo,
1333 unsigned int *size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001334 const unsigned char *base,
Florian Westphal09d96862016-04-01 14:17:34 +02001335 const unsigned char *limit)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001336{
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001337 struct xt_entry_match *ematch;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001338 struct xt_entry_target *t;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001339 struct xt_target *target;
1340 unsigned int entry_offset;
Patrick McHardyb0a63632008-01-31 04:10:18 -08001341 unsigned int j;
Florian Westphal09d96862016-04-01 14:17:34 +02001342 int ret, off;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001343
Joe Perches3666ed12009-11-23 23:17:06 +01001344 if ((unsigned long)e % __alignof__(struct compat_ip6t_entry) != 0 ||
Florian Westphal6e94e0c2016-03-22 18:02:50 +01001345 (unsigned char *)e + sizeof(struct compat_ip6t_entry) >= limit ||
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001346 (unsigned char *)e + e->next_offset > limit)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001347 return -EINVAL;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001348
1349 if (e->next_offset < sizeof(struct compat_ip6t_entry) +
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001350 sizeof(struct compat_xt_entry_target))
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001351 return -EINVAL;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001352
Florian Westphalaa412ba2016-04-01 14:17:24 +02001353 if (!ip6_checkentry(&e->ipv6))
1354 return -EINVAL;
1355
Florian Westphalce683e52016-04-01 14:17:28 +02001356 ret = xt_compat_check_entry_offsets(e, e->elems,
Florian Westphalfc1221b2016-04-01 14:17:26 +02001357 e->target_offset, e->next_offset);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001358 if (ret)
1359 return ret;
1360
1361 off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1362 entry_offset = (void *)e - (void *)base;
1363 j = 0;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001364 xt_ematch_foreach(ematch, e) {
Florian Westphal329a0802016-04-01 14:17:31 +02001365 ret = compat_find_calc_match(ematch, &e->ipv6, &off);
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001366 if (ret != 0)
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001367 goto release_matches;
1368 ++j;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001369 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001370
1371 t = compat_ip6t_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001372 target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name,
1373 t->u.user.revision);
1374 if (IS_ERR(target)) {
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001375 ret = PTR_ERR(target);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001376 goto release_matches;
1377 }
1378 t->u.kernel.target = target;
1379
1380 off += xt_compat_target_offset(target);
1381 *size += off;
1382 ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
1383 if (ret)
1384 goto out;
1385
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001386 return 0;
1387
1388out:
1389 module_put(t->u.kernel.target->me);
1390release_matches:
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001391 xt_ematch_foreach(ematch, e) {
1392 if (j-- == 0)
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001393 break;
Jan Engelhardt6bdb3312010-02-24 18:35:37 +01001394 module_put(ematch->u.kernel.match->me);
1395 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001396 return ret;
1397}
1398
Florian Westphal01883462016-04-01 14:17:33 +02001399static void
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001400compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr,
Florian Westphal329a0802016-04-01 14:17:31 +02001401 unsigned int *size,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001402 struct xt_table_info *newinfo, unsigned char *base)
1403{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001404 struct xt_entry_target *t;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001405 struct ip6t_entry *de;
1406 unsigned int origsize;
Florian Westphal01883462016-04-01 14:17:33 +02001407 int h;
Jan Engelhardtdcea9922010-02-24 18:34:48 +01001408 struct xt_entry_match *ematch;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001409
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001410 origsize = *size;
1411 de = (struct ip6t_entry *)*dstptr;
1412 memcpy(de, e, sizeof(struct ip6t_entry));
1413 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1414
1415 *dstptr += sizeof(struct ip6t_entry);
1416 *size += sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1417
Florian Westphal01883462016-04-01 14:17:33 +02001418 xt_ematch_foreach(ematch, e)
1419 xt_compat_match_from_user(ematch, dstptr, size);
1420
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001421 de->target_offset = e->target_offset - (origsize - *size);
1422 t = compat_ip6t_get_target(e);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001423 xt_compat_target_from_user(t, dstptr, size);
1424
1425 de->next_offset = e->next_offset - (origsize - *size);
1426 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1427 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1428 newinfo->hook_entry[h] -= origsize - *size;
1429 if ((unsigned char *)de - base < newinfo->underflow[h])
1430 newinfo->underflow[h] -= origsize - *size;
1431 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001432}
1433
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001434static int
Alexey Dobriyanf54e9362010-01-18 08:25:47 +01001435translate_compat_table(struct net *net,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001436 struct xt_table_info **pinfo,
1437 void **pentry0,
Florian Westphal329a0802016-04-01 14:17:31 +02001438 const struct compat_ip6t_replace *compatr)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001439{
1440 unsigned int i, j;
1441 struct xt_table_info *newinfo, *info;
1442 void *pos, *entry0, *entry1;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001443 struct compat_ip6t_entry *iter0;
Florian Westphal09d96862016-04-01 14:17:34 +02001444 struct ip6t_replace repl;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001445 unsigned int size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001446 int ret = 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001447
1448 info = *pinfo;
1449 entry0 = *pentry0;
Florian Westphal329a0802016-04-01 14:17:31 +02001450 size = compatr->size;
1451 info->number = compatr->num_entries;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001452
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001453 j = 0;
1454 xt_compat_lock(AF_INET6);
Florian Westphal329a0802016-04-01 14:17:31 +02001455 xt_compat_init_offsets(AF_INET6, compatr->num_entries);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001456 /* Walk through entries, checking offsets. */
Florian Westphal329a0802016-04-01 14:17:31 +02001457 xt_entry_foreach(iter0, entry0, compatr->size) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001458 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001459 entry0,
Florian Westphal09d96862016-04-01 14:17:34 +02001460 entry0 + compatr->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001461 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001462 goto out_unlock;
1463 ++j;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001464 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001465
1466 ret = -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001467 if (j != compatr->num_entries)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001468 goto out_unlock;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001469
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001470 ret = -ENOMEM;
1471 newinfo = xt_alloc_table_info(size);
1472 if (!newinfo)
1473 goto out_unlock;
1474
Florian Westphal329a0802016-04-01 14:17:31 +02001475 newinfo->number = compatr->num_entries;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001476 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
Florian Westphal09d96862016-04-01 14:17:34 +02001477 newinfo->hook_entry[i] = compatr->hook_entry[i];
1478 newinfo->underflow[i] = compatr->underflow[i];
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001479 }
Florian Westphal482cfc32015-06-11 01:34:55 +02001480 entry1 = newinfo->entries;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001481 pos = entry1;
Florian Westphal09d96862016-04-01 14:17:34 +02001482 size = compatr->size;
Florian Westphal01883462016-04-01 14:17:33 +02001483 xt_entry_foreach(iter0, entry0, compatr->size)
1484 compat_copy_entry_from_user(iter0, &pos, &size,
1485 newinfo, entry1);
1486
Florian Westphal09d96862016-04-01 14:17:34 +02001487 /* all module references in entry0 are now gone. */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001488 xt_compat_flush_offsets(AF_INET6);
1489 xt_compat_unlock(AF_INET6);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001490
Florian Westphal09d96862016-04-01 14:17:34 +02001491 memcpy(&repl, compatr, sizeof(*compatr));
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001492
Florian Westphal09d96862016-04-01 14:17:34 +02001493 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1494 repl.hook_entry[i] = newinfo->hook_entry[i];
1495 repl.underflow[i] = newinfo->underflow[i];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001496 }
Florian Westphal09d96862016-04-01 14:17:34 +02001497
1498 repl.num_counters = 0;
1499 repl.counters = NULL;
1500 repl.size = newinfo->size;
1501 ret = translate_table(net, newinfo, entry1, &repl);
1502 if (ret)
1503 goto free_newinfo;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001504
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001505 *pinfo = newinfo;
1506 *pentry0 = entry1;
1507 xt_free_table_info(info);
1508 return 0;
1509
1510free_newinfo:
1511 xt_free_table_info(newinfo);
Florian Westphal09d96862016-04-01 14:17:34 +02001512 return ret;
1513out_unlock:
1514 xt_compat_flush_offsets(AF_INET6);
1515 xt_compat_unlock(AF_INET6);
Florian Westphal329a0802016-04-01 14:17:31 +02001516 xt_entry_foreach(iter0, entry0, compatr->size) {
Jan Engelhardt05595182010-02-24 18:33:43 +01001517 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001518 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001519 compat_release_entry(iter0);
1520 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001521 return ret;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001522}
1523
1524static int
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001525compat_do_replace(struct net *net, void __user *user, unsigned int len)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001526{
1527 int ret;
1528 struct compat_ip6t_replace tmp;
1529 struct xt_table_info *newinfo;
1530 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001531 struct ip6t_entry *iter;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001532
1533 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1534 return -EFAULT;
1535
1536 /* overflow check */
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001537 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1538 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001539 if (tmp.num_counters == 0)
1540 return -EINVAL;
1541
Vasiliy Kulikov6a8ab0602011-03-15 13:37:13 +01001542 tmp.name[sizeof(tmp.name)-1] = 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001543
1544 newinfo = xt_alloc_table_info(tmp.size);
1545 if (!newinfo)
1546 return -ENOMEM;
1547
Florian Westphal482cfc32015-06-11 01:34:55 +02001548 loc_cpu_entry = newinfo->entries;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001549 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1550 tmp.size) != 0) {
1551 ret = -EFAULT;
1552 goto free_newinfo;
1553 }
1554
Florian Westphal329a0802016-04-01 14:17:31 +02001555 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001556 if (ret != 0)
1557 goto free_newinfo;
1558
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001559 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001560 tmp.num_counters, compat_ptr(tmp.counters));
1561 if (ret)
1562 goto free_newinfo_untrans;
1563 return 0;
1564
1565 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001566 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001567 cleanup_entry(iter, net);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001568 free_newinfo:
1569 xt_free_table_info(newinfo);
1570 return ret;
1571}
1572
1573static int
1574compat_do_ip6t_set_ctl(struct sock *sk, int cmd, void __user *user,
1575 unsigned int len)
1576{
1577 int ret;
1578
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001579 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001580 return -EPERM;
1581
1582 switch (cmd) {
1583 case IP6T_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001584 ret = compat_do_replace(sock_net(sk), user, len);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001585 break;
1586
1587 case IP6T_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001588 ret = do_add_counters(sock_net(sk), user, len, 1);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001589 break;
1590
1591 default:
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001592 ret = -EINVAL;
1593 }
1594
1595 return ret;
1596}
1597
1598struct compat_ip6t_get_entries {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001599 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001600 compat_uint_t size;
1601 struct compat_ip6t_entry entrytable[0];
1602};
1603
1604static int
1605compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1606 void __user *userptr)
1607{
1608 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001609 const struct xt_table_info *private = table->private;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001610 void __user *pos;
1611 unsigned int size;
1612 int ret = 0;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001613 unsigned int i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001614 struct ip6t_entry *iter;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001615
1616 counters = alloc_counters(table);
1617 if (IS_ERR(counters))
1618 return PTR_ERR(counters);
1619
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001620 pos = userptr;
1621 size = total_size;
Florian Westphal482cfc32015-06-11 01:34:55 +02001622 xt_entry_foreach(iter, private->entries, total_size) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001623 ret = compat_copy_entry_to_user(iter, &pos,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001624 &size, counters, i++);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001625 if (ret != 0)
1626 break;
1627 }
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001628
1629 vfree(counters);
1630 return ret;
1631}
1632
1633static int
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001634compat_get_entries(struct net *net, struct compat_ip6t_get_entries __user *uptr,
1635 int *len)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001636{
1637 int ret;
1638 struct compat_ip6t_get_entries get;
1639 struct xt_table *t;
1640
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001641 if (*len < sizeof(get))
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001642 return -EINVAL;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001643
1644 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1645 return -EFAULT;
1646
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001647 if (*len != sizeof(struct compat_ip6t_get_entries) + get.size)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001648 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001649
Pablo Neira Ayusob301f252016-03-24 21:29:53 +01001650 get.name[sizeof(get.name) - 1] = '\0';
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001651
1652 xt_compat_lock(AF_INET6);
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001653 t = xt_find_table_lock(net, AF_INET6, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001654 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001655 const struct xt_table_info *private = t->private;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001656 struct xt_table_info info;
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001657 ret = compat_table_info(private, &info);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001658 if (!ret && get.size == info.size)
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001659 ret = compat_copy_entries_to_user(private->size,
1660 t, uptr->entrytable);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001661 else if (!ret)
Patrick McHardy544473c2008-04-14 11:15:45 +02001662 ret = -EAGAIN;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001663
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001664 xt_compat_flush_offsets(AF_INET6);
1665 module_put(t->me);
1666 xt_table_unlock(t);
1667 } else
1668 ret = t ? PTR_ERR(t) : -ENOENT;
1669
1670 xt_compat_unlock(AF_INET6);
1671 return ret;
1672}
1673
1674static int do_ip6t_get_ctl(struct sock *, int, void __user *, int *);
1675
1676static int
1677compat_do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1678{
1679 int ret;
1680
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001681 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001682 return -EPERM;
1683
1684 switch (cmd) {
1685 case IP6T_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001686 ret = get_info(sock_net(sk), user, len, 1);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001687 break;
1688 case IP6T_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001689 ret = compat_get_entries(sock_net(sk), user, len);
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001690 break;
1691 default:
1692 ret = do_ip6t_get_ctl(sk, cmd, user, len);
1693 }
1694 return ret;
1695}
1696#endif
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698static int
1699do_ip6t_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1700{
1701 int ret;
1702
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001703 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return -EPERM;
1705
1706 switch (cmd) {
1707 case IP6T_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001708 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 break;
1710
1711 case IP6T_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001712 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 break;
1714
1715 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 ret = -EINVAL;
1717 }
1718
1719 return ret;
1720}
1721
1722static int
1723do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1724{
1725 int ret;
1726
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001727 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return -EPERM;
1729
1730 switch (cmd) {
Patrick McHardy433665c2007-12-17 21:50:05 -08001731 case IP6T_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001732 ret = get_info(sock_net(sk), user, len, 0);
Patrick McHardy433665c2007-12-17 21:50:05 -08001733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Patrick McHardyd9243572007-12-17 21:50:22 -08001735 case IP6T_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001736 ret = get_entries(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Harald Welte6b7d31f2005-10-26 09:34:24 +02001739 case IP6T_SO_GET_REVISION_MATCH:
1740 case IP6T_SO_GET_REVISION_TARGET: {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001741 struct xt_get_revision rev;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001742 int target;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001743
1744 if (*len != sizeof(rev)) {
1745 ret = -EINVAL;
1746 break;
1747 }
1748 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1749 ret = -EFAULT;
1750 break;
1751 }
Vasiliy Kulikov6a8ab0602011-03-15 13:37:13 +01001752 rev.name[sizeof(rev.name)-1] = 0;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001753
1754 if (cmd == IP6T_SO_GET_REVISION_TARGET)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001755 target = 1;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001756 else
Harald Welte2e4e6a12006-01-12 13:30:04 -08001757 target = 0;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001758
Harald Welte2e4e6a12006-01-12 13:30:04 -08001759 try_then_request_module(xt_find_revision(AF_INET6, rev.name,
1760 rev.revision,
1761 target, &ret),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001762 "ip6t_%s", rev.name);
1763 break;
1764 }
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 ret = -EINVAL;
1768 }
1769
1770 return ret;
1771}
1772
Florian Westphalb9e69e12016-02-25 10:08:36 +01001773static void __ip6t_unregister_table(struct net *net, struct xt_table *table)
1774{
1775 struct xt_table_info *private;
1776 void *loc_cpu_entry;
1777 struct module *table_owner = table->me;
1778 struct ip6t_entry *iter;
1779
1780 private = xt_unregister_table(table);
1781
1782 /* Decrease module usage counts and free resources */
1783 loc_cpu_entry = private->entries;
1784 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1785 cleanup_entry(iter, net);
1786 if (private->number > private->initial_entries)
1787 module_put(table_owner);
1788 xt_free_table_info(private);
1789}
1790
Florian Westphala67dd262016-02-25 10:08:35 +01001791int ip6t_register_table(struct net *net, const struct xt_table *table,
1792 const struct ip6t_replace *repl,
1793 const struct nf_hook_ops *ops,
1794 struct xt_table **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001797 struct xt_table_info *newinfo;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001798 struct xt_table_info bootstrap = {0};
Eric Dumazet31836062005-12-13 23:13:48 -08001799 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08001800 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Harald Welte2e4e6a12006-01-12 13:30:04 -08001802 newinfo = xt_alloc_table_info(repl->size);
Florian Westphala67dd262016-02-25 10:08:35 +01001803 if (!newinfo)
1804 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Florian Westphal482cfc32015-06-11 01:34:55 +02001806 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -08001807 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Jan Engelhardt0f234212010-02-24 18:36:04 +01001809 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001810 if (ret != 0)
1811 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Alexey Dobriyan336b5172008-01-31 04:03:45 -08001813 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001814 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001815 ret = PTR_ERR(new_table);
1816 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
Florian Westphala67dd262016-02-25 10:08:35 +01001818
Florian Westphalb9e69e12016-02-25 10:08:36 +01001819 /* set res now, will see skbs right after nf_register_net_hooks */
Florian Westphala67dd262016-02-25 10:08:35 +01001820 WRITE_ONCE(*res, new_table);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001821
1822 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1823 if (ret != 0) {
1824 __ip6t_unregister_table(net, new_table);
1825 *res = NULL;
1826 }
1827
Florian Westphala67dd262016-02-25 10:08:35 +01001828 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001830out_free:
1831 xt_free_table_info(newinfo);
Florian Westphala67dd262016-02-25 10:08:35 +01001832 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Florian Westphala67dd262016-02-25 10:08:35 +01001835void ip6t_unregister_table(struct net *net, struct xt_table *table,
1836 const struct nf_hook_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Florian Westphalb9e69e12016-02-25 10:08:36 +01001838 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1839 __ip6t_unregister_table(net, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
1842/* Returns 1 if the type and code is matched by the range, 0 otherwise */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07001843static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844icmp6_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1845 u_int8_t type, u_int8_t code,
Jan Engelhardtccb79bd2007-07-07 22:16:00 -07001846 bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 return (type == test_type && code >= min_code && code <= max_code)
1849 ^ invert;
1850}
1851
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001852static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +02001853icmp6_match(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Jan Engelhardt5452e422008-04-14 11:15:35 +02001855 const struct icmp6hdr *ic;
1856 struct icmp6hdr _icmph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001857 const struct ip6t_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001860 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001861 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Jan Engelhardtf7108a22008-10-08 11:35:18 +02001863 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (ic == NULL) {
1865 /* We've been asked to examine this packet, and we
Patrick McHardy9c547952007-12-17 21:52:00 -08001866 * can't. Hence, no choice but to drop.
1867 */
Jan Engelhardtb4ba2612009-07-07 20:54:30 +02001868 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -07001869 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871
1872 return icmp6_type_code_match(icmpinfo->type,
1873 icmpinfo->code[0],
1874 icmpinfo->code[1],
1875 ic->icmp6_type, ic->icmp6_code,
1876 !!(icmpinfo->invflags&IP6T_ICMP_INV));
1877}
1878
1879/* Called when user tries to insert an entry of this type. */
Jan Engelhardtb0f38452010-03-19 17:16:42 +01001880static int icmp6_checkentry(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +02001882 const struct ip6t_icmp *icmpinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Patrick McHardy7f939712006-03-20 18:01:43 -08001884 /* Must specify no unknown invflags */
Jan Engelhardtbd414ee2010-03-23 16:35:56 +01001885 return (icmpinfo->invflags & ~IP6T_ICMP_INV) ? -EINVAL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
1888/* The built-in targets: standard (NULL) and error. */
Jan Engelhardt45385062009-07-04 12:50:00 +02001889static struct xt_target ip6t_builtin_tg[] __read_mostly = {
1890 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001891 .name = XT_STANDARD_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001892 .targetsize = sizeof(int),
1893 .family = NFPROTO_IPV6,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001894#ifdef CONFIG_COMPAT
Jan Engelhardt45385062009-07-04 12:50:00 +02001895 .compatsize = sizeof(compat_int_t),
1896 .compat_from_user = compat_standard_from_user,
1897 .compat_to_user = compat_standard_to_user,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001898#endif
Jan Engelhardt45385062009-07-04 12:50:00 +02001899 },
1900 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001901 .name = XT_ERROR_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001902 .target = ip6t_error,
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001903 .targetsize = XT_FUNCTION_MAXNAMELEN,
Jan Engelhardt45385062009-07-04 12:50:00 +02001904 .family = NFPROTO_IPV6,
1905 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906};
1907
1908static struct nf_sockopt_ops ip6t_sockopts = {
1909 .pf = PF_INET6,
1910 .set_optmin = IP6T_BASE_CTL,
1911 .set_optmax = IP6T_SO_SET_MAX+1,
1912 .set = do_ip6t_set_ctl,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001913#ifdef CONFIG_COMPAT
1914 .compat_set = compat_do_ip6t_set_ctl,
1915#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 .get_optmin = IP6T_BASE_CTL,
1917 .get_optmax = IP6T_SO_GET_MAX+1,
1918 .get = do_ip6t_get_ctl,
Patrick McHardy3bc3fe52007-12-17 21:50:37 -08001919#ifdef CONFIG_COMPAT
1920 .compat_get = compat_do_ip6t_get_ctl,
1921#endif
Neil Horman16fcec32007-09-11 11:28:26 +02001922 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923};
1924
Jan Engelhardt45385062009-07-04 12:50:00 +02001925static struct xt_match ip6t_builtin_mt[] __read_mostly = {
1926 {
1927 .name = "icmp6",
1928 .match = icmp6_match,
1929 .matchsize = sizeof(struct ip6t_icmp),
1930 .checkentry = icmp6_checkentry,
1931 .proto = IPPROTO_ICMPV6,
1932 .family = NFPROTO_IPV6,
1933 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934};
1935
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001936static int __net_init ip6_tables_net_init(struct net *net)
1937{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02001938 return xt_proto_init(net, NFPROTO_IPV6);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001939}
1940
1941static void __net_exit ip6_tables_net_exit(struct net *net)
1942{
Jan Engelhardt383ca5b2009-04-14 14:24:21 +02001943 xt_proto_fini(net, NFPROTO_IPV6);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001944}
1945
1946static struct pernet_operations ip6_tables_net_ops = {
1947 .init = ip6_tables_net_init,
1948 .exit = ip6_tables_net_exit,
1949};
1950
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001951static int __init ip6_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 int ret;
1954
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001955 ret = register_pernet_subsys(&ip6_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001956 if (ret < 0)
1957 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001958
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001959 /* No one else will be downing sem now, so we won't sleep */
Jan Engelhardt45385062009-07-04 12:50:00 +02001960 ret = xt_register_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001961 if (ret < 0)
1962 goto err2;
Jan Engelhardt45385062009-07-04 12:50:00 +02001963 ret = xt_register_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001964 if (ret < 0)
1965 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* Register setsockopt */
1968 ret = nf_register_sockopt(&ip6t_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001969 if (ret < 0)
1970 goto err5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01001972 pr_info("(C) 2000-2006 Netfilter Core Team\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001974
1975err5:
Jan Engelhardt45385062009-07-04 12:50:00 +02001976 xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001977err4:
Jan Engelhardt45385062009-07-04 12:50:00 +02001978 xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001979err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001980 unregister_pernet_subsys(&ip6_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001981err1:
1982 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001985static void __exit ip6_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 nf_unregister_sockopt(&ip6t_sockopts);
Patrick McHardy9c547952007-12-17 21:52:00 -08001988
Jan Engelhardt45385062009-07-04 12:50:00 +02001989 xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
1990 xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001991 unregister_pernet_subsys(&ip6_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
1994EXPORT_SYMBOL(ip6t_register_table);
1995EXPORT_SYMBOL(ip6t_unregister_table);
1996EXPORT_SYMBOL(ip6t_do_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001998module_init(ip6_tables_init);
1999module_exit(ip6_tables_fini);