]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv6/netfilter/ip6_tables.c
net: ethernet: arc: fix error handling in emac_rockchip_probe
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / netfilter / ip6_tables.c
CommitLineData
1da177e4
LT
1/*
2 * Packet matching code.
3 *
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
6b7d31fc 5 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
f229f6ce 6 * Copyright (c) 2006-2010 Patrick McHardy <kaber@trash.net>
1da177e4
LT
7 *
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.
1da177e4 11 */
a81b2ce8 12
90e7d4ab 13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
a81b2ce8
JP
14
15#include <linux/kernel.h>
4fc268d2 16#include <linux/capability.h>
14c85021 17#include <linux/in.h>
1da177e4
LT
18#include <linux/skbuff.h>
19#include <linux/kmod.h>
20#include <linux/vmalloc.h>
21#include <linux/netdevice.h>
22#include <linux/module.h>
4bdbf6c0 23#include <linux/poison.h>
1da177e4 24#include <linux/icmpv6.h>
1da177e4 25#include <net/ipv6.h>
3bc3fe5e 26#include <net/compat.h>
7c0f6ba6 27#include <linux/uaccess.h>
57b47a53 28#include <linux/mutex.h>
1da177e4 29#include <linux/proc_fs.h>
3bc3fe5e 30#include <linux/err.h>
c8923c6b 31#include <linux/cpumask.h>
1da177e4
LT
32
33#include <linux/netfilter_ipv6/ip6_tables.h>
2e4e6a17 34#include <linux/netfilter/x_tables.h>
f01ffbd6 35#include <net/netfilter/nf_log.h>
e3eaa991 36#include "../../netfilter/xt_repldata.h"
1da177e4
LT
37
38MODULE_LICENSE("GPL");
39MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
40MODULE_DESCRIPTION("IPv6 packet filter");
41
e3eaa991
JE
42void *ip6t_alloc_initial_table(const struct xt_table *info)
43{
44 return xt_alloc_initial_table(ip6t, IP6T);
45}
46EXPORT_SYMBOL_GPL(ip6t_alloc_initial_table);
47
1da177e4 48/* Returns whether matches rule or not. */
022748a9 49/* Performance critical - called for every packet */
1d93a9cb 50static inline bool
1da177e4
LT
51ip6_packet_match(const struct sk_buff *skb,
52 const char *indev,
53 const char *outdev,
54 const struct ip6t_ip6 *ip6info,
55 unsigned int *protoff,
cff533ac 56 int *fragoff, bool *hotdrop)
1da177e4 57{
1da177e4 58 unsigned long ret;
0660e03f 59 const struct ipv6hdr *ipv6 = ipv6_hdr(skb);
1da177e4 60
c37a2dfa
JP
61 if (NF_INVF(ip6info, IP6T_INV_SRCIP,
62 ipv6_masked_addr_cmp(&ipv6->saddr, &ip6info->smsk,
63 &ip6info->src)) ||
64 NF_INVF(ip6info, IP6T_INV_DSTIP,
65 ipv6_masked_addr_cmp(&ipv6->daddr, &ip6info->dmsk,
66 &ip6info->dst)))
1d93a9cb 67 return false;
1da177e4 68
b8dfe498 69 ret = ifname_compare_aligned(indev, ip6info->iniface, ip6info->iniface_mask);
1da177e4 70
c37a2dfa 71 if (NF_INVF(ip6info, IP6T_INV_VIA_IN, ret != 0))
1d93a9cb 72 return false;
1da177e4 73
b8dfe498 74 ret = ifname_compare_aligned(outdev, ip6info->outiface, ip6info->outiface_mask);
1da177e4 75
c37a2dfa 76 if (NF_INVF(ip6info, IP6T_INV_VIA_OUT, ret != 0))
1d93a9cb 77 return false;
1da177e4
LT
78
79/* ... might want to do something with class and flowlabel here ... */
80
81 /* look for the desired protocol header */
4305ae44 82 if (ip6info->flags & IP6T_F_PROTO) {
b777e0ce
PM
83 int protohdr;
84 unsigned short _frag_off;
1da177e4 85
84018f55 86 protohdr = ipv6_find_hdr(skb, protoff, -1, &_frag_off, NULL);
51d8b1a6
PM
87 if (protohdr < 0) {
88 if (_frag_off == 0)
cff533ac 89 *hotdrop = true;
1d93a9cb 90 return false;
51d8b1a6 91 }
b777e0ce 92 *fragoff = _frag_off;
1da177e4 93
b777e0ce 94 if (ip6info->proto == protohdr) {
4305ae44 95 if (ip6info->invflags & IP6T_INV_PROTO)
1d93a9cb 96 return false;
4305ae44 97
1d93a9cb 98 return true;
1da177e4
LT
99 }
100
101 /* We need match for the '-p all', too! */
102 if ((ip6info->proto != 0) &&
103 !(ip6info->invflags & IP6T_INV_PROTO))
1d93a9cb 104 return false;
1da177e4 105 }
1d93a9cb 106 return true;
1da177e4
LT
107}
108
109/* should be ip6 safe */
022748a9 110static bool
1da177e4
LT
111ip6_checkentry(const struct ip6t_ip6 *ipv6)
112{
d7cdf816 113 if (ipv6->flags & ~IP6T_F_MASK)
ccb79bdc 114 return false;
d7cdf816 115 if (ipv6->invflags & ~IP6T_INV_MASK)
ccb79bdc 116 return false;
d7cdf816 117
ccb79bdc 118 return true;
1da177e4
LT
119}
120
121static unsigned int
4b560b44 122ip6t_error(struct sk_buff *skb, const struct xt_action_param *par)
1da177e4 123{
e87cc472 124 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
1da177e4
LT
125
126 return NF_DROP;
127}
128
1da177e4 129static inline struct ip6t_entry *
d5d1baa1 130get_entry(const void *base, unsigned int offset)
1da177e4
LT
131{
132 return (struct ip6t_entry *)(base + offset);
133}
134
ba9dda3a 135/* All zeroes == unconditional rule. */
022748a9 136/* Mildly perf critical (only if packet tracing is on) */
54d83fc7 137static inline bool unconditional(const struct ip6t_entry *e)
ba9dda3a 138{
47901dc2 139 static const struct ip6t_ip6 uncond;
ba9dda3a 140
54d83fc7
FW
141 return e->target_offset == sizeof(struct ip6t_entry) &&
142 memcmp(&e->ipv6, &uncond, sizeof(uncond)) == 0;
ba9dda3a
JK
143}
144
87a2e70d 145static inline const struct xt_entry_target *
d5d1baa1
JE
146ip6t_get_target_c(const struct ip6t_entry *e)
147{
148 return ip6t_get_target((struct ip6t_entry *)e);
149}
150
07a93626 151#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
ba9dda3a 152/* This cries for unification! */
022748a9 153static const char *const hooknames[] = {
6e23ae2a
PM
154 [NF_INET_PRE_ROUTING] = "PREROUTING",
155 [NF_INET_LOCAL_IN] = "INPUT",
156 [NF_INET_FORWARD] = "FORWARD",
157 [NF_INET_LOCAL_OUT] = "OUTPUT",
158 [NF_INET_POST_ROUTING] = "POSTROUTING",
ba9dda3a
JK
159};
160
161enum nf_ip_trace_comments {
162 NF_IP6_TRACE_COMMENT_RULE,
163 NF_IP6_TRACE_COMMENT_RETURN,
164 NF_IP6_TRACE_COMMENT_POLICY,
165};
166
022748a9 167static const char *const comments[] = {
ba9dda3a
JK
168 [NF_IP6_TRACE_COMMENT_RULE] = "rule",
169 [NF_IP6_TRACE_COMMENT_RETURN] = "return",
170 [NF_IP6_TRACE_COMMENT_POLICY] = "policy",
171};
172
549d2d41 173static const struct nf_loginfo trace_loginfo = {
ba9dda3a
JK
174 .type = NF_LOG_TYPE_LOG,
175 .u = {
176 .log = {
a81b2ce8 177 .level = LOGLEVEL_WARNING,
ff107d27 178 .logflags = NF_LOG_DEFAULT_MASK,
ba9dda3a
JK
179 },
180 },
181};
182
022748a9 183/* Mildly perf critical (only if packet tracing is on) */
ba9dda3a 184static inline int
d5d1baa1 185get_chainname_rulenum(const struct ip6t_entry *s, const struct ip6t_entry *e,
4f2f6f23
JE
186 const char *hookname, const char **chainname,
187 const char **comment, unsigned int *rulenum)
ba9dda3a 188{
87a2e70d 189 const struct xt_standard_target *t = (void *)ip6t_get_target_c(s);
ba9dda3a 190
243bf6e2 191 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
ba9dda3a
JK
192 /* Head of user chain: ERROR target with chainname */
193 *chainname = t->target.data;
194 (*rulenum) = 0;
195 } else if (s == e) {
196 (*rulenum)++;
197
54d83fc7 198 if (unconditional(s) &&
3666ed1c 199 strcmp(t->target.u.kernel.target->name,
243bf6e2 200 XT_STANDARD_TARGET) == 0 &&
54d83fc7 201 t->verdict < 0) {
ba9dda3a
JK
202 /* Tail of chains: STANDARD target (return/policy) */
203 *comment = *chainname == hookname
4f2f6f23
JE
204 ? comments[NF_IP6_TRACE_COMMENT_POLICY]
205 : comments[NF_IP6_TRACE_COMMENT_RETURN];
ba9dda3a
JK
206 }
207 return 1;
208 } else
209 (*rulenum)++;
210
211 return 0;
212}
213
9dff2c96
EB
214static void trace_packet(struct net *net,
215 const struct sk_buff *skb,
ba9dda3a
JK
216 unsigned int hook,
217 const struct net_device *in,
218 const struct net_device *out,
ecb6f85e 219 const char *tablename,
d5d1baa1
JE
220 const struct xt_table_info *private,
221 const struct ip6t_entry *e)
ba9dda3a 222{
5452e425 223 const struct ip6t_entry *root;
4f2f6f23 224 const char *hookname, *chainname, *comment;
72b2b1dd 225 const struct ip6t_entry *iter;
ba9dda3a
JK
226 unsigned int rulenum = 0;
227
482cfc31 228 root = get_entry(private->entries, private->hook_entry[hook]);
ba9dda3a 229
4f2f6f23
JE
230 hookname = chainname = hooknames[hook];
231 comment = comments[NF_IP6_TRACE_COMMENT_RULE];
ba9dda3a 232
72b2b1dd
JE
233 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
234 if (get_chainname_rulenum(iter, e, hookname,
235 &chainname, &comment, &rulenum) != 0)
236 break;
ba9dda3a 237
4017a7ee
PNA
238 nf_log_trace(net, AF_INET6, hook, skb, in, out, &trace_loginfo,
239 "TRACE: %s:%s:%s:%u ",
240 tablename, chainname, comment, rulenum);
ba9dda3a
JK
241}
242#endif
243
6c7941de 244static inline struct ip6t_entry *
98e86403
JE
245ip6t_next_entry(const struct ip6t_entry *entry)
246{
247 return (void *)entry + entry->next_offset;
248}
249
1da177e4
LT
250/* Returns one of the generic firewall policies, like NF_ACCEPT. */
251unsigned int
3db05fea 252ip6t_do_table(struct sk_buff *skb,
8f8a3715 253 const struct nf_hook_state *state,
fe1cb108 254 struct xt_table *table)
1da177e4 255{
6cb8ff3f 256 unsigned int hook = state->hook;
6b7d31fc 257 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
1da177e4
LT
258 /* Initializing verdict to NF_DROP keeps gcc happy. */
259 unsigned int verdict = NF_DROP;
260 const char *indev, *outdev;
d5d1baa1 261 const void *table_base;
f3c5c1bf 262 struct ip6t_entry *e, **jumpstack;
7814b6ec 263 unsigned int stackidx, cpu;
d5d1baa1 264 const struct xt_table_info *private;
de74c169 265 struct xt_action_param acpar;
7f5c6d4f 266 unsigned int addend;
1da177e4
LT
267
268 /* Initialization */
7814b6ec 269 stackidx = 0;
8f8a3715
DM
270 indev = state->in ? state->in->name : nulldevname;
271 outdev = state->out ? state->out->name : nulldevname;
1da177e4
LT
272 /* We handle fragments by dealing with the first fragment as
273 * if it was a normal packet. All other fragments are treated
274 * normally, except that they will NEVER match rules that ask
275 * things we don't know, ie. tcp syn flag or ports). If the
276 * rule is also a fragment-specific rule, non-fragments won't
277 * match it. */
b4ba2611 278 acpar.hotdrop = false;
613dbd95 279 acpar.state = state;
1da177e4 280
9efdb14f 281 WARN_ON(!(table->valid_hooks & (1 << hook)));
78454473 282
7f5c6d4f
ED
283 local_bh_disable();
284 addend = xt_write_recseq_begin();
942e4a2b 285 private = table->private;
b416c144
WD
286 /*
287 * Ensure we load private-> members after we've fetched the base
288 * pointer.
289 */
290 smp_read_barrier_depends();
f3c5c1bf 291 cpu = smp_processor_id();
482cfc31 292 table_base = private->entries;
f3c5c1bf 293 jumpstack = (struct ip6t_entry **)private->jumpstack[cpu];
7814b6ec
FW
294
295 /* Switch to alternate jumpstack if we're being invoked via TEE.
296 * TEE issues XT_CONTINUE verdict on original skb so we must not
297 * clobber the jumpstack.
298 *
299 * For recursion via REJECT or SYNPROXY the stack will be clobbered
300 * but it is no problem since absolute verdict is issued by these.
301 */
dcebd315
FW
302 if (static_key_false(&xt_tee_enabled))
303 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
78454473 304
2e4e6a17 305 e = get_entry(table_base, private->hook_entry[hook]);
1da177e4 306
1da177e4 307 do {
87a2e70d 308 const struct xt_entry_target *t;
dcea992a 309 const struct xt_entry_match *ematch;
71ae0dff 310 struct xt_counters *counter;
a1ff4ac8 311
9efdb14f 312 WARN_ON(!e);
84018f55 313 acpar.thoff = 0;
a1ff4ac8 314 if (!ip6_packet_match(skb, indev, outdev, &e->ipv6,
b4ba2611 315 &acpar.thoff, &acpar.fragoff, &acpar.hotdrop)) {
dcea992a 316 no_match:
a1ff4ac8
JE
317 e = ip6t_next_entry(e);
318 continue;
319 }
1da177e4 320
ef53d702 321 xt_ematch_foreach(ematch, e) {
de74c169
JE
322 acpar.match = ematch->u.kernel.match;
323 acpar.matchinfo = ematch->data;
324 if (!acpar.match->match(skb, &acpar))
dcea992a 325 goto no_match;
ef53d702 326 }
dcea992a 327
71ae0dff
FW
328 counter = xt_get_this_cpu_counter(&e->counters);
329 ADD_COUNTER(*counter, skb->len, 1);
1da177e4 330
d5d1baa1 331 t = ip6t_get_target_c(e);
9efdb14f 332 WARN_ON(!t->u.kernel.target);
ba9dda3a 333
07a93626 334#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
a1ff4ac8
JE
335 /* The packet is traced: log it */
336 if (unlikely(skb->nf_trace))
9dff2c96
EB
337 trace_packet(state->net, skb, hook, state->in,
338 state->out, table->name, private, e);
ba9dda3a 339#endif
a1ff4ac8
JE
340 /* Standard target? */
341 if (!t->u.kernel.target->target) {
342 int v;
343
87a2e70d 344 v = ((struct xt_standard_target *)t)->verdict;
a1ff4ac8
JE
345 if (v < 0) {
346 /* Pop from stack? */
243bf6e2 347 if (v != XT_RETURN) {
95c96174 348 verdict = (unsigned int)(-v) - 1;
a1ff4ac8 349 break;
1da177e4 350 }
7814b6ec 351 if (stackidx == 0)
f3c5c1bf
JE
352 e = get_entry(table_base,
353 private->underflow[hook]);
354 else
7814b6ec 355 e = ip6t_next_entry(jumpstack[--stackidx]);
a1ff4ac8
JE
356 continue;
357 }
3666ed1c
JP
358 if (table_base + v != ip6t_next_entry(e) &&
359 !(e->ipv6.flags & IP6T_F_GOTO)) {
7814b6ec 360 jumpstack[stackidx++] = e;
a1ff4ac8 361 }
1da177e4 362
a1ff4ac8 363 e = get_entry(table_base, v);
7a6b1c46
JE
364 continue;
365 }
366
de74c169
JE
367 acpar.target = t->u.kernel.target;
368 acpar.targinfo = t->data;
7eb35586 369
de74c169 370 verdict = t->u.kernel.target->target(skb, &acpar);
243bf6e2 371 if (verdict == XT_CONTINUE)
7a6b1c46
JE
372 e = ip6t_next_entry(e);
373 else
374 /* Verdict */
375 break;
b4ba2611 376 } while (!acpar.hotdrop);
1da177e4 377
7695495d
IM
378 xt_write_recseq_end(addend);
379 local_bh_enable();
1da177e4 380
b4ba2611 381 if (acpar.hotdrop)
1da177e4
LT
382 return NF_DROP;
383 else return verdict;
1da177e4
LT
384}
385
1da177e4 386/* Figures out from what hook each rule can be called: returns 0 if
98dbbfc3 387 there are loops. Puts hook bitmask in comefrom. */
1da177e4 388static int
98dbbfc3 389mark_source_chains(const struct xt_table_info *newinfo,
f4dc7771
FW
390 unsigned int valid_hooks, void *entry0,
391 unsigned int *offsets)
1da177e4
LT
392{
393 unsigned int hook;
394
395 /* No recursion; use packet counter to save back ptrs (reset
396 to 0 as we leave), and comefrom to save source hook bitmask */
6e23ae2a 397 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
1da177e4 398 unsigned int pos = newinfo->hook_entry[hook];
68ad546a 399 struct ip6t_entry *e = entry0 + pos;
1da177e4
LT
400
401 if (!(valid_hooks & (1 << hook)))
402 continue;
403
404 /* Set initial back pointer. */
405 e->counters.pcnt = pos;
406
407 for (;;) {
87a2e70d 408 const struct xt_standard_target *t
d5d1baa1 409 = (void *)ip6t_get_target_c(e);
9c547959 410 int visited = e->comefrom & (1 << hook);
1da177e4 411
d7cdf816 412 if (e->comefrom & (1 << NF_INET_NUMHOOKS))
1da177e4 413 return 0;
d7cdf816 414
9c547959 415 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
1da177e4
LT
416
417 /* Unconditional return/END. */
54d83fc7 418 if ((unconditional(e) &&
3666ed1c 419 (strcmp(t->target.u.user.name,
243bf6e2 420 XT_STANDARD_TARGET) == 0) &&
54d83fc7 421 t->verdict < 0) || visited) {
1da177e4
LT
422 unsigned int oldpos, size;
423
1f9352ae 424 if ((strcmp(t->target.u.user.name,
243bf6e2 425 XT_STANDARD_TARGET) == 0) &&
d7cdf816 426 t->verdict < -NF_MAX_VERDICT - 1)
74c9c0c1 427 return 0;
74c9c0c1 428
1da177e4
LT
429 /* Return: backtrack through the last
430 big jump. */
431 do {
6e23ae2a 432 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
1da177e4
LT
433 oldpos = pos;
434 pos = e->counters.pcnt;
435 e->counters.pcnt = 0;
436
437 /* We're at the start. */
438 if (pos == oldpos)
439 goto next;
440
68ad546a 441 e = entry0 + pos;
1da177e4
LT
442 } while (oldpos == pos + e->next_offset);
443
444 /* Move along one */
445 size = e->next_offset;
68ad546a 446 e = entry0 + pos + size;
f24e230d
FW
447 if (pos + size >= newinfo->size)
448 return 0;
1da177e4
LT
449 e->counters.pcnt = pos;
450 pos += size;
451 } else {
452 int newpos = t->verdict;
453
454 if (strcmp(t->target.u.user.name,
243bf6e2 455 XT_STANDARD_TARGET) == 0 &&
3666ed1c 456 newpos >= 0) {
1da177e4 457 /* This a jump; chase it. */
f4dc7771
FW
458 if (!xt_find_jump_offset(offsets, newpos,
459 newinfo->number))
460 return 0;
68ad546a 461 e = entry0 + newpos;
1da177e4
LT
462 } else {
463 /* ... this is a fallthru */
464 newpos = pos + e->next_offset;
f24e230d
FW
465 if (newpos >= newinfo->size)
466 return 0;
1da177e4 467 }
68ad546a 468 e = entry0 + newpos;
1da177e4
LT
469 e->counters.pcnt = pos;
470 pos = newpos;
471 }
472 }
d7cdf816 473next: ;
1da177e4
LT
474 }
475 return 1;
476}
477
87a2e70d 478static void cleanup_match(struct xt_entry_match *m, struct net *net)
1da177e4 479{
6be3d859
JE
480 struct xt_mtdtor_param par;
481
f54e9367 482 par.net = net;
6be3d859
JE
483 par.match = m->u.kernel.match;
484 par.matchinfo = m->data;
916a917d 485 par.family = NFPROTO_IPV6;
6be3d859
JE
486 if (par.match->destroy != NULL)
487 par.match->destroy(&par);
488 module_put(par.match->me);
1da177e4
LT
489}
490
87a2e70d 491static int check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
f173c8a1 492{
9b4fce7a 493 const struct ip6t_ip6 *ipv6 = par->entryinfo;
f173c8a1 494
9b4fce7a
JE
495 par->match = m->u.kernel.match;
496 par->matchinfo = m->data;
497
d7cdf816
PNA
498 return xt_check_match(par, m->u.match_size - sizeof(*m),
499 ipv6->proto, ipv6->invflags & IP6T_INV_PROTO);
f173c8a1
PM
500}
501
022748a9 502static int
87a2e70d 503find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
1da177e4 504{
6709dbbb 505 struct xt_match *match;
3cdc7c95 506 int ret;
1da177e4 507
fd0ec0e6
JE
508 match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
509 m->u.user.revision);
d7cdf816 510 if (IS_ERR(match))
fd0ec0e6 511 return PTR_ERR(match);
d7cdf816 512
1da177e4 513 m->u.kernel.match = match;
1da177e4 514
6bdb331b 515 ret = check_match(m, par);
3cdc7c95
PM
516 if (ret)
517 goto err;
518
1da177e4 519 return 0;
3cdc7c95
PM
520err:
521 module_put(m->u.kernel.match->me);
522 return ret;
1da177e4
LT
523}
524
add67461 525static int check_target(struct ip6t_entry *e, struct net *net, const char *name)
1da177e4 526{
87a2e70d 527 struct xt_entry_target *t = ip6t_get_target(e);
af5d6dc2 528 struct xt_tgchk_param par = {
add67461 529 .net = net,
af5d6dc2
JE
530 .table = name,
531 .entryinfo = e,
532 .target = t->u.kernel.target,
533 .targinfo = t->data,
534 .hook_mask = e->comefrom,
916a917d 535 .family = NFPROTO_IPV6,
af5d6dc2 536 };
1da177e4 537
f173c8a1 538 t = ip6t_get_target(e);
d7cdf816
PNA
539 return xt_check_target(&par, t->u.target_size - sizeof(*t),
540 e->ipv6.proto,
541 e->ipv6.invflags & IP6T_INV_PROTO);
f173c8a1 542}
1da177e4 543
022748a9 544static int
a83d8e8d 545find_check_entry(struct ip6t_entry *e, struct net *net, const char *name,
ae0ac0ed
FW
546 unsigned int size,
547 struct xt_percpu_counter_alloc_state *alloc_state)
f173c8a1 548{
87a2e70d 549 struct xt_entry_target *t;
f173c8a1
PM
550 struct xt_target *target;
551 int ret;
552 unsigned int j;
9b4fce7a 553 struct xt_mtchk_param mtpar;
dcea992a 554 struct xt_entry_match *ematch;
f173c8a1 555
ae0ac0ed 556 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
71ae0dff
FW
557 return -ENOMEM;
558
1da177e4 559 j = 0;
a83d8e8d 560 mtpar.net = net;
9b4fce7a
JE
561 mtpar.table = name;
562 mtpar.entryinfo = &e->ipv6;
563 mtpar.hook_mask = e->comefrom;
916a917d 564 mtpar.family = NFPROTO_IPV6;
dcea992a 565 xt_ematch_foreach(ematch, e) {
6bdb331b 566 ret = find_check_match(ematch, &mtpar);
dcea992a 567 if (ret != 0)
6bdb331b
JE
568 goto cleanup_matches;
569 ++j;
dcea992a 570 }
1da177e4
LT
571
572 t = ip6t_get_target(e);
d2a7b6ba
JE
573 target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name,
574 t->u.user.revision);
575 if (IS_ERR(target)) {
d2a7b6ba 576 ret = PTR_ERR(target);
1da177e4
LT
577 goto cleanup_matches;
578 }
579 t->u.kernel.target = target;
6b7d31fc 580
add67461 581 ret = check_target(e, net, name);
3cdc7c95
PM
582 if (ret)
583 goto err;
1da177e4 584 return 0;
3cdc7c95
PM
585 err:
586 module_put(t->u.kernel.target->me);
1da177e4 587 cleanup_matches:
6bdb331b
JE
588 xt_ematch_foreach(ematch, e) {
589 if (j-- == 0)
dcea992a 590 break;
6bdb331b
JE
591 cleanup_match(ematch, net);
592 }
71ae0dff 593
4d31eef5 594 xt_percpu_counter_free(&e->counters);
71ae0dff 595
1da177e4
LT
596 return ret;
597}
598
d5d1baa1 599static bool check_underflow(const struct ip6t_entry *e)
e2fe35c1 600{
87a2e70d 601 const struct xt_entry_target *t;
e2fe35c1
JE
602 unsigned int verdict;
603
54d83fc7 604 if (!unconditional(e))
e2fe35c1 605 return false;
d5d1baa1 606 t = ip6t_get_target_c(e);
e2fe35c1
JE
607 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
608 return false;
87a2e70d 609 verdict = ((struct xt_standard_target *)t)->verdict;
e2fe35c1
JE
610 verdict = -verdict - 1;
611 return verdict == NF_DROP || verdict == NF_ACCEPT;
612}
613
022748a9 614static int
1da177e4 615check_entry_size_and_hooks(struct ip6t_entry *e,
2e4e6a17 616 struct xt_table_info *newinfo,
d5d1baa1
JE
617 const unsigned char *base,
618 const unsigned char *limit,
1da177e4
LT
619 const unsigned int *hook_entries,
620 const unsigned int *underflows,
0559518b 621 unsigned int valid_hooks)
1da177e4
LT
622{
623 unsigned int h;
bdf533de 624 int err;
1da177e4 625
3666ed1c 626 if ((unsigned long)e % __alignof__(struct ip6t_entry) != 0 ||
6e94e0cf 627 (unsigned char *)e + sizeof(struct ip6t_entry) >= limit ||
d7cdf816 628 (unsigned char *)e + e->next_offset > limit)
1da177e4 629 return -EINVAL;
1da177e4
LT
630
631 if (e->next_offset
d7cdf816 632 < sizeof(struct ip6t_entry) + sizeof(struct xt_entry_target))
1da177e4 633 return -EINVAL;
1da177e4 634
aa412ba2
FW
635 if (!ip6_checkentry(&e->ipv6))
636 return -EINVAL;
637
ce683e5f
FW
638 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
639 e->next_offset);
bdf533de
FW
640 if (err)
641 return err;
642
1da177e4 643 /* Check hooks & underflows */
6e23ae2a 644 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
a7d51738
JE
645 if (!(valid_hooks & (1 << h)))
646 continue;
1da177e4
LT
647 if ((unsigned char *)e - base == hook_entries[h])
648 newinfo->hook_entry[h] = hook_entries[h];
90e7d4ab 649 if ((unsigned char *)e - base == underflows[h]) {
d7cdf816 650 if (!check_underflow(e))
90e7d4ab 651 return -EINVAL;
d7cdf816 652
1da177e4 653 newinfo->underflow[h] = underflows[h];
90e7d4ab 654 }
1da177e4
LT
655 }
656
1da177e4 657 /* Clear counters and comefrom */
2e4e6a17 658 e->counters = ((struct xt_counters) { 0, 0 });
1da177e4 659 e->comefrom = 0;
1da177e4
LT
660 return 0;
661}
662
0559518b 663static void cleanup_entry(struct ip6t_entry *e, struct net *net)
1da177e4 664{
a2df1648 665 struct xt_tgdtor_param par;
87a2e70d 666 struct xt_entry_target *t;
dcea992a 667 struct xt_entry_match *ematch;
1da177e4 668
1da177e4 669 /* Cleanup all matches */
dcea992a 670 xt_ematch_foreach(ematch, e)
6bdb331b 671 cleanup_match(ematch, net);
1da177e4 672 t = ip6t_get_target(e);
a2df1648 673
add67461 674 par.net = net;
a2df1648
JE
675 par.target = t->u.kernel.target;
676 par.targinfo = t->data;
916a917d 677 par.family = NFPROTO_IPV6;
a2df1648
JE
678 if (par.target->destroy != NULL)
679 par.target->destroy(&par);
680 module_put(par.target->me);
4d31eef5 681 xt_percpu_counter_free(&e->counters);
1da177e4
LT
682}
683
684/* Checks and translates the user-supplied table segment (held in
685 newinfo) */
686static int
0f234214 687translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
cda219c6 688 const struct ip6t_replace *repl)
1da177e4 689{
ae0ac0ed 690 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
72b2b1dd 691 struct ip6t_entry *iter;
f4dc7771 692 unsigned int *offsets;
1da177e4 693 unsigned int i;
72b2b1dd 694 int ret = 0;
1da177e4 695
0f234214
JE
696 newinfo->size = repl->size;
697 newinfo->number = repl->num_entries;
1da177e4
LT
698
699 /* Init all hooks to impossible value. */
6e23ae2a 700 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1da177e4
LT
701 newinfo->hook_entry[i] = 0xFFFFFFFF;
702 newinfo->underflow[i] = 0xFFFFFFFF;
703 }
704
f4dc7771
FW
705 offsets = xt_alloc_entry_offsets(newinfo->number);
706 if (!offsets)
707 return -ENOMEM;
1da177e4
LT
708 i = 0;
709 /* Walk through entries, checking offsets. */
72b2b1dd
JE
710 xt_entry_foreach(iter, entry0, newinfo->size) {
711 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
6b4ff2d7
JE
712 entry0 + repl->size,
713 repl->hook_entry,
714 repl->underflow,
715 repl->valid_hooks);
72b2b1dd 716 if (ret != 0)
f4dc7771
FW
717 goto out_free;
718 if (i < repl->num_entries)
719 offsets[i] = (void *)iter - entry0;
0559518b 720 ++i;
98dbbfc3
FW
721 if (strcmp(ip6t_get_target(iter)->u.user.name,
722 XT_ERROR_TARGET) == 0)
723 ++newinfo->stacksize;
72b2b1dd 724 }
1da177e4 725
f4dc7771 726 ret = -EINVAL;
d7cdf816 727 if (i != repl->num_entries)
f4dc7771 728 goto out_free;
1da177e4
LT
729
730 /* Check hooks all assigned */
6e23ae2a 731 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1da177e4 732 /* Only hooks which are valid */
0f234214 733 if (!(repl->valid_hooks & (1 << i)))
1da177e4 734 continue;
d7cdf816 735 if (newinfo->hook_entry[i] == 0xFFFFFFFF)
f4dc7771 736 goto out_free;
d7cdf816 737 if (newinfo->underflow[i] == 0xFFFFFFFF)
f4dc7771 738 goto out_free;
1da177e4
LT
739 }
740
f4dc7771
FW
741 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
742 ret = -ELOOP;
743 goto out_free;
744 }
745 kvfree(offsets);
74c9c0c1 746
1da177e4
LT
747 /* Finally, each sanity check must pass */
748 i = 0;
72b2b1dd 749 xt_entry_foreach(iter, entry0, newinfo->size) {
ae0ac0ed
FW
750 ret = find_check_entry(iter, net, repl->name, repl->size,
751 &alloc_state);
72b2b1dd
JE
752 if (ret != 0)
753 break;
0559518b 754 ++i;
72b2b1dd 755 }
1da177e4 756
74c9c0c1 757 if (ret != 0) {
0559518b
JE
758 xt_entry_foreach(iter, entry0, newinfo->size) {
759 if (i-- == 0)
72b2b1dd 760 break;
0559518b
JE
761 cleanup_entry(iter, net);
762 }
74c9c0c1
DM
763 return ret;
764 }
1da177e4 765
f4dc7771
FW
766 return ret;
767 out_free:
768 kvfree(offsets);
9c547959 769 return ret;
1da177e4
LT
770}
771
1da177e4 772static void
2e4e6a17
HW
773get_counters(const struct xt_table_info *t,
774 struct xt_counters counters[])
1da177e4 775{
72b2b1dd 776 struct ip6t_entry *iter;
1da177e4
LT
777 unsigned int cpu;
778 unsigned int i;
779
6f912042 780 for_each_possible_cpu(cpu) {
7f5c6d4f 781 seqcount_t *s = &per_cpu(xt_recseq, cpu);
83723d60 782
1da177e4 783 i = 0;
482cfc31 784 xt_entry_foreach(iter, t->entries, t->size) {
71ae0dff 785 struct xt_counters *tmp;
83723d60
ED
786 u64 bcnt, pcnt;
787 unsigned int start;
788
71ae0dff 789 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
83723d60 790 do {
7f5c6d4f 791 start = read_seqcount_begin(s);
71ae0dff
FW
792 bcnt = tmp->bcnt;
793 pcnt = tmp->pcnt;
7f5c6d4f 794 } while (read_seqcount_retry(s, start));
83723d60
ED
795
796 ADD_COUNTER(counters[i], bcnt, pcnt);
0559518b 797 ++i;
a5d7a714 798 cond_resched();
0559518b 799 }
1da177e4 800 }
78454473
SH
801}
802
d13e7b2e
FW
803static void get_old_counters(const struct xt_table_info *t,
804 struct xt_counters counters[])
805{
806 struct ip6t_entry *iter;
807 unsigned int cpu, i;
808
809 for_each_possible_cpu(cpu) {
810 i = 0;
811 xt_entry_foreach(iter, t->entries, t->size) {
812 const struct xt_counters *tmp;
813
814 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
815 ADD_COUNTER(counters[i], tmp->bcnt, tmp->pcnt);
816 ++i;
817 }
818 cond_resched();
819 }
820}
821
d5d1baa1 822static struct xt_counters *alloc_counters(const struct xt_table *table)
1da177e4 823{
ed1a6f5e 824 unsigned int countersize;
2e4e6a17 825 struct xt_counters *counters;
d5d1baa1 826 const struct xt_table_info *private = table->private;
1da177e4
LT
827
828 /* We need atomic snapshot of counters: rest doesn't change
829 (other than comefrom, which userspace doesn't care
830 about). */
2e4e6a17 831 countersize = sizeof(struct xt_counters) * private->number;
83723d60 832 counters = vzalloc(countersize);
1da177e4
LT
833
834 if (counters == NULL)
942e4a2b 835 return ERR_PTR(-ENOMEM);
78454473 836
942e4a2b 837 get_counters(private, counters);
78454473 838
49a88d18 839 return counters;
ed1a6f5e
PM
840}
841
842static int
843copy_entries_to_user(unsigned int total_size,
d5d1baa1 844 const struct xt_table *table,
ed1a6f5e
PM
845 void __user *userptr)
846{
847 unsigned int off, num;
d5d1baa1 848 const struct ip6t_entry *e;
ed1a6f5e 849 struct xt_counters *counters;
5452e425 850 const struct xt_table_info *private = table->private;
ed1a6f5e 851 int ret = 0;
711bdde6 852 const void *loc_cpu_entry;
ed1a6f5e
PM
853
854 counters = alloc_counters(table);
855 if (IS_ERR(counters))
856 return PTR_ERR(counters);
857
482cfc31 858 loc_cpu_entry = private->entries;
1da177e4
LT
859
860 /* FIXME: use iterator macros --RR */
861 /* ... then go back and fix counters and names */
862 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
863 unsigned int i;
87a2e70d
JE
864 const struct xt_entry_match *m;
865 const struct xt_entry_target *t;
1da177e4 866
68ad546a 867 e = loc_cpu_entry + off;
e47ddb2c
WB
868 if (copy_to_user(userptr + off, e, sizeof(*e))) {
869 ret = -EFAULT;
870 goto free_counters;
871 }
1da177e4
LT
872 if (copy_to_user(userptr + off
873 + offsetof(struct ip6t_entry, counters),
874 &counters[num],
875 sizeof(counters[num])) != 0) {
876 ret = -EFAULT;
877 goto free_counters;
878 }
879
880 for (i = sizeof(struct ip6t_entry);
881 i < e->target_offset;
882 i += m->u.match_size) {
883 m = (void *)e + i;
884
e47ddb2c 885 if (xt_match_to_user(m, userptr + off + i)) {
1da177e4
LT
886 ret = -EFAULT;
887 goto free_counters;
888 }
889 }
890
d5d1baa1 891 t = ip6t_get_target_c(e);
e47ddb2c 892 if (xt_target_to_user(t, userptr + off + e->target_offset)) {
1da177e4
LT
893 ret = -EFAULT;
894 goto free_counters;
895 }
896 }
897
898 free_counters:
899 vfree(counters);
900 return ret;
901}
902
3bc3fe5e 903#ifdef CONFIG_COMPAT
739674fb 904static void compat_standard_from_user(void *dst, const void *src)
3bc3fe5e
PM
905{
906 int v = *(compat_int_t *)src;
907
908 if (v > 0)
909 v += xt_compat_calc_jump(AF_INET6, v);
910 memcpy(dst, &v, sizeof(v));
911}
912
739674fb 913static int compat_standard_to_user(void __user *dst, const void *src)
3bc3fe5e
PM
914{
915 compat_int_t cv = *(int *)src;
916
917 if (cv > 0)
918 cv -= xt_compat_calc_jump(AF_INET6, cv);
919 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
920}
921
d5d1baa1 922static int compat_calc_entry(const struct ip6t_entry *e,
3bc3fe5e 923 const struct xt_table_info *info,
d5d1baa1 924 const void *base, struct xt_table_info *newinfo)
3bc3fe5e 925{
dcea992a 926 const struct xt_entry_match *ematch;
87a2e70d 927 const struct xt_entry_target *t;
3bc3fe5e
PM
928 unsigned int entry_offset;
929 int off, i, ret;
930
931 off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
932 entry_offset = (void *)e - base;
dcea992a 933 xt_ematch_foreach(ematch, e)
6bdb331b 934 off += xt_compat_match_offset(ematch->u.kernel.match);
d5d1baa1 935 t = ip6t_get_target_c(e);
3bc3fe5e
PM
936 off += xt_compat_target_offset(t->u.kernel.target);
937 newinfo->size -= off;
938 ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
939 if (ret)
940 return ret;
941
942 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
943 if (info->hook_entry[i] &&
944 (e < (struct ip6t_entry *)(base + info->hook_entry[i])))
945 newinfo->hook_entry[i] -= off;
946 if (info->underflow[i] &&
947 (e < (struct ip6t_entry *)(base + info->underflow[i])))
948 newinfo->underflow[i] -= off;
949 }
950 return 0;
951}
952
953static int compat_table_info(const struct xt_table_info *info,
954 struct xt_table_info *newinfo)
955{
72b2b1dd 956 struct ip6t_entry *iter;
711bdde6 957 const void *loc_cpu_entry;
0559518b 958 int ret;
3bc3fe5e
PM
959
960 if (!newinfo || !info)
961 return -EINVAL;
962
482cfc31 963 /* we dont care about newinfo->entries */
3bc3fe5e
PM
964 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
965 newinfo->initial_entries = 0;
482cfc31 966 loc_cpu_entry = info->entries;
255d0dc3 967 xt_compat_init_offsets(AF_INET6, info->number);
72b2b1dd
JE
968 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
969 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
970 if (ret != 0)
0559518b 971 return ret;
72b2b1dd 972 }
0559518b 973 return 0;
3bc3fe5e
PM
974}
975#endif
976
d5d1baa1 977static int get_info(struct net *net, void __user *user,
cda219c6 978 const int *len, int compat)
433665c9 979{
12b00c2c 980 char name[XT_TABLE_MAXNAMELEN];
433665c9
PM
981 struct xt_table *t;
982 int ret;
983
d7cdf816 984 if (*len != sizeof(struct ip6t_getinfo))
433665c9 985 return -EINVAL;
433665c9
PM
986
987 if (copy_from_user(name, user, sizeof(name)) != 0)
988 return -EFAULT;
989
12b00c2c 990 name[XT_TABLE_MAXNAMELEN-1] = '\0';
3bc3fe5e
PM
991#ifdef CONFIG_COMPAT
992 if (compat)
993 xt_compat_lock(AF_INET6);
994#endif
336b517f 995 t = try_then_request_module(xt_find_table_lock(net, AF_INET6, name),
433665c9 996 "ip6table_%s", name);
eb1a6bdc 997 if (t) {
433665c9 998 struct ip6t_getinfo info;
5452e425 999 const struct xt_table_info *private = t->private;
3bc3fe5e 1000#ifdef CONFIG_COMPAT
14c7dbe0
AD
1001 struct xt_table_info tmp;
1002
3bc3fe5e 1003 if (compat) {
3bc3fe5e
PM
1004 ret = compat_table_info(private, &tmp);
1005 xt_compat_flush_offsets(AF_INET6);
1006 private = &tmp;
1007 }
1008#endif
cccbe5ef 1009 memset(&info, 0, sizeof(info));
433665c9
PM
1010 info.valid_hooks = t->valid_hooks;
1011 memcpy(info.hook_entry, private->hook_entry,
1012 sizeof(info.hook_entry));
1013 memcpy(info.underflow, private->underflow,
1014 sizeof(info.underflow));
1015 info.num_entries = private->number;
1016 info.size = private->size;
b5dd674b 1017 strcpy(info.name, name);
433665c9
PM
1018
1019 if (copy_to_user(user, &info, *len) != 0)
1020 ret = -EFAULT;
1021 else
1022 ret = 0;
1023
1024 xt_table_unlock(t);
1025 module_put(t->me);
1026 } else
eb1a6bdc 1027 ret = -ENOENT;
3bc3fe5e
PM
1028#ifdef CONFIG_COMPAT
1029 if (compat)
1030 xt_compat_unlock(AF_INET6);
1031#endif
433665c9
PM
1032 return ret;
1033}
1034
1da177e4 1035static int
d5d1baa1 1036get_entries(struct net *net, struct ip6t_get_entries __user *uptr,
cda219c6 1037 const int *len)
1da177e4
LT
1038{
1039 int ret;
d924357c 1040 struct ip6t_get_entries get;
2e4e6a17 1041 struct xt_table *t;
1da177e4 1042
d7cdf816 1043 if (*len < sizeof(get))
d924357c 1044 return -EINVAL;
d924357c
PM
1045 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1046 return -EFAULT;
d7cdf816 1047 if (*len != sizeof(struct ip6t_get_entries) + get.size)
d924357c 1048 return -EINVAL;
d7cdf816 1049
b301f253 1050 get.name[sizeof(get.name) - 1] = '\0';
d924357c 1051
336b517f 1052 t = xt_find_table_lock(net, AF_INET6, get.name);
eb1a6bdc 1053 if (t) {
2e4e6a17 1054 struct xt_table_info *private = t->private;
d924357c 1055 if (get.size == private->size)
2e4e6a17 1056 ret = copy_entries_to_user(private->size,
1da177e4 1057 t, uptr->entrytable);
d7cdf816 1058 else
544473c1 1059 ret = -EAGAIN;
d7cdf816 1060
6b7d31fc 1061 module_put(t->me);
2e4e6a17 1062 xt_table_unlock(t);
1da177e4 1063 } else
eb1a6bdc 1064 ret = -ENOENT;
1da177e4
LT
1065
1066 return ret;
1067}
1068
1069static int
336b517f 1070__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
3bc3fe5e
PM
1071 struct xt_table_info *newinfo, unsigned int num_counters,
1072 void __user *counters_ptr)
1da177e4
LT
1073{
1074 int ret;
2e4e6a17 1075 struct xt_table *t;
3bc3fe5e 1076 struct xt_table_info *oldinfo;
2e4e6a17 1077 struct xt_counters *counters;
72b2b1dd 1078 struct ip6t_entry *iter;
1da177e4 1079
3bc3fe5e 1080 ret = 0;
83723d60 1081 counters = vzalloc(num_counters * sizeof(struct xt_counters));
1da177e4
LT
1082 if (!counters) {
1083 ret = -ENOMEM;
3bc3fe5e 1084 goto out;
1da177e4 1085 }
1da177e4 1086
336b517f 1087 t = try_then_request_module(xt_find_table_lock(net, AF_INET6, name),
3bc3fe5e 1088 "ip6table_%s", name);
eb1a6bdc
JL
1089 if (!t) {
1090 ret = -ENOENT;
1da177e4 1091 goto free_newinfo_counters_untrans;
6b7d31fc 1092 }
1da177e4
LT
1093
1094 /* You lied! */
3bc3fe5e 1095 if (valid_hooks != t->valid_hooks) {
1da177e4 1096 ret = -EINVAL;
6b7d31fc 1097 goto put_module;
1da177e4
LT
1098 }
1099
3bc3fe5e 1100 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1da177e4
LT
1101 if (!oldinfo)
1102 goto put_module;
1103
1104 /* Update module usage count based on number of rules */
1ab1457c
YH
1105 if ((oldinfo->number > oldinfo->initial_entries) ||
1106 (newinfo->number <= oldinfo->initial_entries))
1da177e4
LT
1107 module_put(t->me);
1108 if ((oldinfo->number > oldinfo->initial_entries) &&
1109 (newinfo->number <= oldinfo->initial_entries))
1110 module_put(t->me);
1111
d13e7b2e 1112 get_old_counters(oldinfo, counters);
942e4a2b 1113
1da177e4 1114 /* Decrease module usage counts and free resource */
482cfc31 1115 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
0559518b 1116 cleanup_entry(iter, net);
72b2b1dd 1117
2e4e6a17 1118 xt_free_table_info(oldinfo);
3bc3fe5e 1119 if (copy_to_user(counters_ptr, counters,
c58dd2dd
TG
1120 sizeof(struct xt_counters) * num_counters) != 0) {
1121 /* Silent error, can't fail, new table is already in place */
1122 net_warn_ratelimited("ip6tables: counters copy to user failed while replacing table\n");
1123 }
1da177e4 1124 vfree(counters);
2e4e6a17 1125 xt_table_unlock(t);
1da177e4
LT
1126 return ret;
1127
1128 put_module:
1129 module_put(t->me);
2e4e6a17 1130 xt_table_unlock(t);
1da177e4 1131 free_newinfo_counters_untrans:
1da177e4 1132 vfree(counters);
3bc3fe5e
PM
1133 out:
1134 return ret;
1135}
1136
1137static int
d5d1baa1 1138do_replace(struct net *net, const void __user *user, unsigned int len)
3bc3fe5e
PM
1139{
1140 int ret;
1141 struct ip6t_replace tmp;
1142 struct xt_table_info *newinfo;
1143 void *loc_cpu_entry;
72b2b1dd 1144 struct ip6t_entry *iter;
3bc3fe5e
PM
1145
1146 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1147 return -EFAULT;
1148
1149 /* overflow check */
1150 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1151 return -ENOMEM;
1086bbe9
DJ
1152 if (tmp.num_counters == 0)
1153 return -EINVAL;
1154
6a8ab060 1155 tmp.name[sizeof(tmp.name)-1] = 0;
3bc3fe5e
PM
1156
1157 newinfo = xt_alloc_table_info(tmp.size);
1158 if (!newinfo)
1159 return -ENOMEM;
1160
482cfc31 1161 loc_cpu_entry = newinfo->entries;
3bc3fe5e
PM
1162 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1163 tmp.size) != 0) {
1164 ret = -EFAULT;
1165 goto free_newinfo;
1166 }
1167
0f234214 1168 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
3bc3fe5e
PM
1169 if (ret != 0)
1170 goto free_newinfo;
1171
336b517f 1172 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
3bc3fe5e
PM
1173 tmp.num_counters, tmp.counters);
1174 if (ret)
1175 goto free_newinfo_untrans;
1176 return 0;
1177
1178 free_newinfo_untrans:
72b2b1dd 1179 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
0559518b 1180 cleanup_entry(iter, net);
1da177e4 1181 free_newinfo:
2e4e6a17 1182 xt_free_table_info(newinfo);
1da177e4
LT
1183 return ret;
1184}
1185
1da177e4 1186static int
d5d1baa1 1187do_add_counters(struct net *net, const void __user *user, unsigned int len,
336b517f 1188 int compat)
1da177e4 1189{
482cfc31 1190 unsigned int i;
3bc3fe5e
PM
1191 struct xt_counters_info tmp;
1192 struct xt_counters *paddc;
2e4e6a17 1193 struct xt_table *t;
5452e425 1194 const struct xt_table_info *private;
6b7d31fc 1195 int ret = 0;
72b2b1dd 1196 struct ip6t_entry *iter;
7f5c6d4f 1197 unsigned int addend;
1da177e4 1198
d7591f0c
FW
1199 paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1200 if (IS_ERR(paddc))
1201 return PTR_ERR(paddc);
1202 t = xt_find_table_lock(net, AF_INET6, tmp.name);
eb1a6bdc
JL
1203 if (!t) {
1204 ret = -ENOENT;
1da177e4 1205 goto free;
6b7d31fc 1206 }
1da177e4 1207
942e4a2b 1208 local_bh_disable();
2e4e6a17 1209 private = t->private;
d7591f0c 1210 if (private->number != tmp.num_counters) {
1da177e4
LT
1211 ret = -EINVAL;
1212 goto unlock_up_free;
1213 }
1214
1215 i = 0;
7f5c6d4f 1216 addend = xt_write_recseq_begin();
482cfc31 1217 xt_entry_foreach(iter, private->entries, private->size) {
71ae0dff
FW
1218 struct xt_counters *tmp;
1219
1220 tmp = xt_get_this_cpu_counter(&iter->counters);
1221 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
0559518b
JE
1222 ++i;
1223 }
7f5c6d4f 1224 xt_write_recseq_end(addend);
1da177e4 1225 unlock_up_free:
942e4a2b 1226 local_bh_enable();
2e4e6a17 1227 xt_table_unlock(t);
6b7d31fc 1228 module_put(t->me);
1da177e4
LT
1229 free:
1230 vfree(paddc);
1231
1232 return ret;
1233}
1234
3bc3fe5e
PM
1235#ifdef CONFIG_COMPAT
1236struct compat_ip6t_replace {
12b00c2c 1237 char name[XT_TABLE_MAXNAMELEN];
3bc3fe5e
PM
1238 u32 valid_hooks;
1239 u32 num_entries;
1240 u32 size;
1241 u32 hook_entry[NF_INET_NUMHOOKS];
1242 u32 underflow[NF_INET_NUMHOOKS];
1243 u32 num_counters;
87a2e70d 1244 compat_uptr_t counters; /* struct xt_counters * */
3bc3fe5e
PM
1245 struct compat_ip6t_entry entries[0];
1246};
1247
1248static int
1249compat_copy_entry_to_user(struct ip6t_entry *e, void __user **dstptr,
b0a6363c 1250 unsigned int *size, struct xt_counters *counters,
0559518b 1251 unsigned int i)
3bc3fe5e 1252{
87a2e70d 1253 struct xt_entry_target *t;
3bc3fe5e
PM
1254 struct compat_ip6t_entry __user *ce;
1255 u_int16_t target_offset, next_offset;
1256 compat_uint_t origsize;
dcea992a
JE
1257 const struct xt_entry_match *ematch;
1258 int ret = 0;
3bc3fe5e 1259
3bc3fe5e 1260 origsize = *size;
68ad546a 1261 ce = *dstptr;
0559518b
JE
1262 if (copy_to_user(ce, e, sizeof(struct ip6t_entry)) != 0 ||
1263 copy_to_user(&ce->counters, &counters[i],
1264 sizeof(counters[i])) != 0)
1265 return -EFAULT;
3bc3fe5e
PM
1266
1267 *dstptr += sizeof(struct compat_ip6t_entry);
1268 *size -= sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1269
dcea992a
JE
1270 xt_ematch_foreach(ematch, e) {
1271 ret = xt_compat_match_to_user(ematch, dstptr, size);
1272 if (ret != 0)
6bdb331b 1273 return ret;
dcea992a 1274 }
3bc3fe5e 1275 target_offset = e->target_offset - (origsize - *size);
3bc3fe5e
PM
1276 t = ip6t_get_target(e);
1277 ret = xt_compat_target_to_user(t, dstptr, size);
1278 if (ret)
0559518b 1279 return ret;
3bc3fe5e 1280 next_offset = e->next_offset - (origsize - *size);
0559518b
JE
1281 if (put_user(target_offset, &ce->target_offset) != 0 ||
1282 put_user(next_offset, &ce->next_offset) != 0)
1283 return -EFAULT;
3bc3fe5e 1284 return 0;
3bc3fe5e
PM
1285}
1286
022748a9 1287static int
87a2e70d 1288compat_find_calc_match(struct xt_entry_match *m,
3bc3fe5e 1289 const struct ip6t_ip6 *ipv6,
6bdb331b 1290 int *size)
3bc3fe5e
PM
1291{
1292 struct xt_match *match;
1293
fd0ec0e6
JE
1294 match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
1295 m->u.user.revision);
d7cdf816 1296 if (IS_ERR(match))
fd0ec0e6 1297 return PTR_ERR(match);
d7cdf816 1298
3bc3fe5e
PM
1299 m->u.kernel.match = match;
1300 *size += xt_compat_match_offset(match);
3bc3fe5e
PM
1301 return 0;
1302}
1303
0559518b 1304static void compat_release_entry(struct compat_ip6t_entry *e)
3bc3fe5e 1305{
87a2e70d 1306 struct xt_entry_target *t;
dcea992a 1307 struct xt_entry_match *ematch;
3bc3fe5e 1308
3bc3fe5e 1309 /* Cleanup all matches */
dcea992a 1310 xt_ematch_foreach(ematch, e)
6bdb331b 1311 module_put(ematch->u.kernel.match->me);
3bc3fe5e
PM
1312 t = compat_ip6t_get_target(e);
1313 module_put(t->u.kernel.target->me);
3bc3fe5e
PM
1314}
1315
022748a9 1316static int
3bc3fe5e
PM
1317check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
1318 struct xt_table_info *newinfo,
1319 unsigned int *size,
d5d1baa1 1320 const unsigned char *base,
09d96860 1321 const unsigned char *limit)
3bc3fe5e 1322{
dcea992a 1323 struct xt_entry_match *ematch;
87a2e70d 1324 struct xt_entry_target *t;
3bc3fe5e
PM
1325 struct xt_target *target;
1326 unsigned int entry_offset;
b0a6363c 1327 unsigned int j;
09d96860 1328 int ret, off;
3bc3fe5e 1329
3666ed1c 1330 if ((unsigned long)e % __alignof__(struct compat_ip6t_entry) != 0 ||
6e94e0cf 1331 (unsigned char *)e + sizeof(struct compat_ip6t_entry) >= limit ||
d7cdf816 1332 (unsigned char *)e + e->next_offset > limit)
3bc3fe5e 1333 return -EINVAL;
3bc3fe5e
PM
1334
1335 if (e->next_offset < sizeof(struct compat_ip6t_entry) +
d7cdf816 1336 sizeof(struct compat_xt_entry_target))
3bc3fe5e 1337 return -EINVAL;
3bc3fe5e 1338
aa412ba2
FW
1339 if (!ip6_checkentry(&e->ipv6))
1340 return -EINVAL;
1341
ce683e5f 1342 ret = xt_compat_check_entry_offsets(e, e->elems,
fc1221b3 1343 e->target_offset, e->next_offset);
3bc3fe5e
PM
1344 if (ret)
1345 return ret;
1346
1347 off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1348 entry_offset = (void *)e - (void *)base;
1349 j = 0;
dcea992a 1350 xt_ematch_foreach(ematch, e) {
329a0807 1351 ret = compat_find_calc_match(ematch, &e->ipv6, &off);
dcea992a 1352 if (ret != 0)
6bdb331b
JE
1353 goto release_matches;
1354 ++j;
dcea992a 1355 }
3bc3fe5e
PM
1356
1357 t = compat_ip6t_get_target(e);
d2a7b6ba
JE
1358 target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name,
1359 t->u.user.revision);
1360 if (IS_ERR(target)) {
d2a7b6ba 1361 ret = PTR_ERR(target);
3bc3fe5e
PM
1362 goto release_matches;
1363 }
1364 t->u.kernel.target = target;
1365
1366 off += xt_compat_target_offset(target);
1367 *size += off;
1368 ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
1369 if (ret)
1370 goto out;
1371
3bc3fe5e
PM
1372 return 0;
1373
1374out:
1375 module_put(t->u.kernel.target->me);
1376release_matches:
6bdb331b
JE
1377 xt_ematch_foreach(ematch, e) {
1378 if (j-- == 0)
dcea992a 1379 break;
6bdb331b
JE
1380 module_put(ematch->u.kernel.match->me);
1381 }
3bc3fe5e
PM
1382 return ret;
1383}
1384
0188346f 1385static void
3bc3fe5e 1386compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr,
329a0807 1387 unsigned int *size,
3bc3fe5e
PM
1388 struct xt_table_info *newinfo, unsigned char *base)
1389{
87a2e70d 1390 struct xt_entry_target *t;
3bc3fe5e
PM
1391 struct ip6t_entry *de;
1392 unsigned int origsize;
0188346f 1393 int h;
dcea992a 1394 struct xt_entry_match *ematch;
3bc3fe5e 1395
3bc3fe5e 1396 origsize = *size;
68ad546a 1397 de = *dstptr;
3bc3fe5e
PM
1398 memcpy(de, e, sizeof(struct ip6t_entry));
1399 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1400
1401 *dstptr += sizeof(struct ip6t_entry);
1402 *size += sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1403
0188346f
FW
1404 xt_ematch_foreach(ematch, e)
1405 xt_compat_match_from_user(ematch, dstptr, size);
1406
3bc3fe5e
PM
1407 de->target_offset = e->target_offset - (origsize - *size);
1408 t = compat_ip6t_get_target(e);
3bc3fe5e
PM
1409 xt_compat_target_from_user(t, dstptr, size);
1410
1411 de->next_offset = e->next_offset - (origsize - *size);
1412 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1413 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1414 newinfo->hook_entry[h] -= origsize - *size;
1415 if ((unsigned char *)de - base < newinfo->underflow[h])
1416 newinfo->underflow[h] -= origsize - *size;
1417 }
3bc3fe5e
PM
1418}
1419
3bc3fe5e 1420static int
f54e9367 1421translate_compat_table(struct net *net,
3bc3fe5e
PM
1422 struct xt_table_info **pinfo,
1423 void **pentry0,
329a0807 1424 const struct compat_ip6t_replace *compatr)
3bc3fe5e
PM
1425{
1426 unsigned int i, j;
1427 struct xt_table_info *newinfo, *info;
1428 void *pos, *entry0, *entry1;
72b2b1dd 1429 struct compat_ip6t_entry *iter0;
09d96860 1430 struct ip6t_replace repl;
3bc3fe5e 1431 unsigned int size;
72b2b1dd 1432 int ret = 0;
3bc3fe5e
PM
1433
1434 info = *pinfo;
1435 entry0 = *pentry0;
329a0807
FW
1436 size = compatr->size;
1437 info->number = compatr->num_entries;
3bc3fe5e 1438
3bc3fe5e
PM
1439 j = 0;
1440 xt_compat_lock(AF_INET6);
329a0807 1441 xt_compat_init_offsets(AF_INET6, compatr->num_entries);
3bc3fe5e 1442 /* Walk through entries, checking offsets. */
329a0807 1443 xt_entry_foreach(iter0, entry0, compatr->size) {
72b2b1dd 1444 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
6b4ff2d7 1445 entry0,
09d96860 1446 entry0 + compatr->size);
72b2b1dd 1447 if (ret != 0)
0559518b
JE
1448 goto out_unlock;
1449 ++j;
72b2b1dd 1450 }
3bc3fe5e
PM
1451
1452 ret = -EINVAL;
d7cdf816 1453 if (j != compatr->num_entries)
3bc3fe5e 1454 goto out_unlock;
3bc3fe5e 1455
3bc3fe5e
PM
1456 ret = -ENOMEM;
1457 newinfo = xt_alloc_table_info(size);
1458 if (!newinfo)
1459 goto out_unlock;
1460
329a0807 1461 newinfo->number = compatr->num_entries;
3bc3fe5e 1462 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
09d96860
FW
1463 newinfo->hook_entry[i] = compatr->hook_entry[i];
1464 newinfo->underflow[i] = compatr->underflow[i];
3bc3fe5e 1465 }
482cfc31 1466 entry1 = newinfo->entries;
3bc3fe5e 1467 pos = entry1;
09d96860 1468 size = compatr->size;
0188346f
FW
1469 xt_entry_foreach(iter0, entry0, compatr->size)
1470 compat_copy_entry_from_user(iter0, &pos, &size,
1471 newinfo, entry1);
1472
09d96860 1473 /* all module references in entry0 are now gone. */
3bc3fe5e
PM
1474 xt_compat_flush_offsets(AF_INET6);
1475 xt_compat_unlock(AF_INET6);
3bc3fe5e 1476
09d96860 1477 memcpy(&repl, compatr, sizeof(*compatr));
3bc3fe5e 1478
09d96860
FW
1479 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1480 repl.hook_entry[i] = newinfo->hook_entry[i];
1481 repl.underflow[i] = newinfo->underflow[i];
3bc3fe5e
PM
1482 }
1483
09d96860
FW
1484 repl.num_counters = 0;
1485 repl.counters = NULL;
1486 repl.size = newinfo->size;
1487 ret = translate_table(net, newinfo, entry1, &repl);
1488 if (ret)
1489 goto free_newinfo;
1490
3bc3fe5e
PM
1491 *pinfo = newinfo;
1492 *pentry0 = entry1;
1493 xt_free_table_info(info);
1494 return 0;
1495
1496free_newinfo:
1497 xt_free_table_info(newinfo);
09d96860
FW
1498 return ret;
1499out_unlock:
1500 xt_compat_flush_offsets(AF_INET6);
1501 xt_compat_unlock(AF_INET6);
329a0807 1502 xt_entry_foreach(iter0, entry0, compatr->size) {
0559518b 1503 if (j-- == 0)
72b2b1dd 1504 break;
0559518b
JE
1505 compat_release_entry(iter0);
1506 }
3bc3fe5e 1507 return ret;
3bc3fe5e
PM
1508}
1509
1510static int
336b517f 1511compat_do_replace(struct net *net, void __user *user, unsigned int len)
3bc3fe5e
PM
1512{
1513 int ret;
1514 struct compat_ip6t_replace tmp;
1515 struct xt_table_info *newinfo;
1516 void *loc_cpu_entry;
72b2b1dd 1517 struct ip6t_entry *iter;
3bc3fe5e
PM
1518
1519 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1520 return -EFAULT;
1521
1522 /* overflow check */
3bc3fe5e
PM
1523 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1524 return -ENOMEM;
1086bbe9
DJ
1525 if (tmp.num_counters == 0)
1526 return -EINVAL;
1527
6a8ab060 1528 tmp.name[sizeof(tmp.name)-1] = 0;
3bc3fe5e
PM
1529
1530 newinfo = xt_alloc_table_info(tmp.size);
1531 if (!newinfo)
1532 return -ENOMEM;
1533
482cfc31 1534 loc_cpu_entry = newinfo->entries;
3bc3fe5e
PM
1535 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1536 tmp.size) != 0) {
1537 ret = -EFAULT;
1538 goto free_newinfo;
1539 }
1540
329a0807 1541 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
3bc3fe5e
PM
1542 if (ret != 0)
1543 goto free_newinfo;
1544
336b517f 1545 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
3bc3fe5e
PM
1546 tmp.num_counters, compat_ptr(tmp.counters));
1547 if (ret)
1548 goto free_newinfo_untrans;
1549 return 0;
1550
1551 free_newinfo_untrans:
72b2b1dd 1552 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
0559518b 1553 cleanup_entry(iter, net);
3bc3fe5e
PM
1554 free_newinfo:
1555 xt_free_table_info(newinfo);
1556 return ret;
1557}
1558
1559static int
1560compat_do_ip6t_set_ctl(struct sock *sk, int cmd, void __user *user,
1561 unsigned int len)
1562{
1563 int ret;
1564
af31f412 1565 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
3bc3fe5e
PM
1566 return -EPERM;
1567
1568 switch (cmd) {
1569 case IP6T_SO_SET_REPLACE:
3b1e0a65 1570 ret = compat_do_replace(sock_net(sk), user, len);
3bc3fe5e
PM
1571 break;
1572
1573 case IP6T_SO_SET_ADD_COUNTERS:
3b1e0a65 1574 ret = do_add_counters(sock_net(sk), user, len, 1);
3bc3fe5e
PM
1575 break;
1576
1577 default:
3bc3fe5e
PM
1578 ret = -EINVAL;
1579 }
1580
1581 return ret;
1582}
1583
1584struct compat_ip6t_get_entries {
12b00c2c 1585 char name[XT_TABLE_MAXNAMELEN];
3bc3fe5e
PM
1586 compat_uint_t size;
1587 struct compat_ip6t_entry entrytable[0];
1588};
1589
1590static int
1591compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1592 void __user *userptr)
1593{
1594 struct xt_counters *counters;
5452e425 1595 const struct xt_table_info *private = table->private;
3bc3fe5e
PM
1596 void __user *pos;
1597 unsigned int size;
1598 int ret = 0;
3bc3fe5e 1599 unsigned int i = 0;
72b2b1dd 1600 struct ip6t_entry *iter;
3bc3fe5e
PM
1601
1602 counters = alloc_counters(table);
1603 if (IS_ERR(counters))
1604 return PTR_ERR(counters);
1605
3bc3fe5e
PM
1606 pos = userptr;
1607 size = total_size;
482cfc31 1608 xt_entry_foreach(iter, private->entries, total_size) {
72b2b1dd 1609 ret = compat_copy_entry_to_user(iter, &pos,
6b4ff2d7 1610 &size, counters, i++);
72b2b1dd
JE
1611 if (ret != 0)
1612 break;
1613 }
3bc3fe5e
PM
1614
1615 vfree(counters);
1616 return ret;
1617}
1618
1619static int
336b517f
AD
1620compat_get_entries(struct net *net, struct compat_ip6t_get_entries __user *uptr,
1621 int *len)
3bc3fe5e
PM
1622{
1623 int ret;
1624 struct compat_ip6t_get_entries get;
1625 struct xt_table *t;
1626
d7cdf816 1627 if (*len < sizeof(get))
3bc3fe5e 1628 return -EINVAL;
3bc3fe5e
PM
1629
1630 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1631 return -EFAULT;
1632
d7cdf816 1633 if (*len != sizeof(struct compat_ip6t_get_entries) + get.size)
3bc3fe5e 1634 return -EINVAL;
d7cdf816 1635
b301f253 1636 get.name[sizeof(get.name) - 1] = '\0';
3bc3fe5e
PM
1637
1638 xt_compat_lock(AF_INET6);
336b517f 1639 t = xt_find_table_lock(net, AF_INET6, get.name);
eb1a6bdc 1640 if (t) {
5452e425 1641 const struct xt_table_info *private = t->private;
3bc3fe5e 1642 struct xt_table_info info;
3bc3fe5e 1643 ret = compat_table_info(private, &info);
d7cdf816 1644 if (!ret && get.size == info.size)
3bc3fe5e
PM
1645 ret = compat_copy_entries_to_user(private->size,
1646 t, uptr->entrytable);
d7cdf816 1647 else if (!ret)
544473c1 1648 ret = -EAGAIN;
d7cdf816 1649
3bc3fe5e
PM
1650 xt_compat_flush_offsets(AF_INET6);
1651 module_put(t->me);
1652 xt_table_unlock(t);
1653 } else
eb1a6bdc 1654 ret = -ENOENT;
3bc3fe5e
PM
1655
1656 xt_compat_unlock(AF_INET6);
1657 return ret;
1658}
1659
1660static int do_ip6t_get_ctl(struct sock *, int, void __user *, int *);
1661
1662static int
1663compat_do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1664{
1665 int ret;
1666
af31f412 1667 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
3bc3fe5e
PM
1668 return -EPERM;
1669
1670 switch (cmd) {
1671 case IP6T_SO_GET_INFO:
3b1e0a65 1672 ret = get_info(sock_net(sk), user, len, 1);
3bc3fe5e
PM
1673 break;
1674 case IP6T_SO_GET_ENTRIES:
3b1e0a65 1675 ret = compat_get_entries(sock_net(sk), user, len);
3bc3fe5e
PM
1676 break;
1677 default:
1678 ret = do_ip6t_get_ctl(sk, cmd, user, len);
1679 }
1680 return ret;
1681}
1682#endif
1683
1da177e4
LT
1684static int
1685do_ip6t_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1686{
1687 int ret;
1688
af31f412 1689 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1da177e4
LT
1690 return -EPERM;
1691
1692 switch (cmd) {
1693 case IP6T_SO_SET_REPLACE:
3b1e0a65 1694 ret = do_replace(sock_net(sk), user, len);
1da177e4
LT
1695 break;
1696
1697 case IP6T_SO_SET_ADD_COUNTERS:
3b1e0a65 1698 ret = do_add_counters(sock_net(sk), user, len, 0);
1da177e4
LT
1699 break;
1700
1701 default:
1da177e4
LT
1702 ret = -EINVAL;
1703 }
1704
1705 return ret;
1706}
1707
1708static int
1709do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1710{
1711 int ret;
1712
af31f412 1713 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1da177e4
LT
1714 return -EPERM;
1715
1716 switch (cmd) {
433665c9 1717 case IP6T_SO_GET_INFO:
3b1e0a65 1718 ret = get_info(sock_net(sk), user, len, 0);
433665c9 1719 break;
1da177e4 1720
d924357c 1721 case IP6T_SO_GET_ENTRIES:
3b1e0a65 1722 ret = get_entries(sock_net(sk), user, len);
1da177e4 1723 break;
1da177e4 1724
6b7d31fc
HW
1725 case IP6T_SO_GET_REVISION_MATCH:
1726 case IP6T_SO_GET_REVISION_TARGET: {
12b00c2c 1727 struct xt_get_revision rev;
2e4e6a17 1728 int target;
6b7d31fc
HW
1729
1730 if (*len != sizeof(rev)) {
1731 ret = -EINVAL;
1732 break;
1733 }
1734 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1735 ret = -EFAULT;
1736 break;
1737 }
6a8ab060 1738 rev.name[sizeof(rev.name)-1] = 0;
6b7d31fc
HW
1739
1740 if (cmd == IP6T_SO_GET_REVISION_TARGET)
2e4e6a17 1741 target = 1;
6b7d31fc 1742 else
2e4e6a17 1743 target = 0;
6b7d31fc 1744
2e4e6a17
HW
1745 try_then_request_module(xt_find_revision(AF_INET6, rev.name,
1746 rev.revision,
1747 target, &ret),
6b7d31fc
HW
1748 "ip6t_%s", rev.name);
1749 break;
1750 }
1751
1da177e4 1752 default:
1da177e4
LT
1753 ret = -EINVAL;
1754 }
1755
1756 return ret;
1757}
1758
b9e69e12
FW
1759static void __ip6t_unregister_table(struct net *net, struct xt_table *table)
1760{
1761 struct xt_table_info *private;
1762 void *loc_cpu_entry;
1763 struct module *table_owner = table->me;
1764 struct ip6t_entry *iter;
1765
1766 private = xt_unregister_table(table);
1767
1768 /* Decrease module usage counts and free resources */
1769 loc_cpu_entry = private->entries;
1770 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1771 cleanup_entry(iter, net);
1772 if (private->number > private->initial_entries)
1773 module_put(table_owner);
1774 xt_free_table_info(private);
1775}
1776
a67dd266
FW
1777int ip6t_register_table(struct net *net, const struct xt_table *table,
1778 const struct ip6t_replace *repl,
1779 const struct nf_hook_ops *ops,
1780 struct xt_table **res)
1da177e4
LT
1781{
1782 int ret;
2e4e6a17 1783 struct xt_table_info *newinfo;
f3c5c1bf 1784 struct xt_table_info bootstrap = {0};
31836064 1785 void *loc_cpu_entry;
a98da11d 1786 struct xt_table *new_table;
1da177e4 1787
2e4e6a17 1788 newinfo = xt_alloc_table_info(repl->size);
a67dd266
FW
1789 if (!newinfo)
1790 return -ENOMEM;
1da177e4 1791
482cfc31 1792 loc_cpu_entry = newinfo->entries;
31836064 1793 memcpy(loc_cpu_entry, repl->entries, repl->size);
1da177e4 1794
0f234214 1795 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
44d34e72
AD
1796 if (ret != 0)
1797 goto out_free;
1da177e4 1798
336b517f 1799 new_table = xt_register_table(net, table, &bootstrap, newinfo);
a98da11d 1800 if (IS_ERR(new_table)) {
44d34e72
AD
1801 ret = PTR_ERR(new_table);
1802 goto out_free;
1da177e4 1803 }
a67dd266 1804
b9e69e12 1805 /* set res now, will see skbs right after nf_register_net_hooks */
a67dd266 1806 WRITE_ONCE(*res, new_table);
b9e69e12
FW
1807
1808 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1809 if (ret != 0) {
1810 __ip6t_unregister_table(net, new_table);
1811 *res = NULL;
1812 }
1813
a67dd266 1814 return ret;
1da177e4 1815
44d34e72
AD
1816out_free:
1817 xt_free_table_info(newinfo);
a67dd266 1818 return ret;
1da177e4
LT
1819}
1820
a67dd266
FW
1821void ip6t_unregister_table(struct net *net, struct xt_table *table,
1822 const struct nf_hook_ops *ops)
1da177e4 1823{
b9e69e12
FW
1824 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1825 __ip6t_unregister_table(net, table);
1da177e4
LT
1826}
1827
1828/* Returns 1 if the type and code is matched by the range, 0 otherwise */
ccb79bdc 1829static inline bool
1da177e4
LT
1830icmp6_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1831 u_int8_t type, u_int8_t code,
ccb79bdc 1832 bool invert)
1da177e4
LT
1833{
1834 return (type == test_type && code >= min_code && code <= max_code)
1835 ^ invert;
1836}
1837
1d93a9cb 1838static bool
62fc8051 1839icmp6_match(const struct sk_buff *skb, struct xt_action_param *par)
1da177e4 1840{
5452e425
JE
1841 const struct icmp6hdr *ic;
1842 struct icmp6hdr _icmph;
f7108a20 1843 const struct ip6t_icmp *icmpinfo = par->matchinfo;
1da177e4
LT
1844
1845 /* Must not be a fragment. */
f7108a20 1846 if (par->fragoff != 0)
1d93a9cb 1847 return false;
1da177e4 1848
f7108a20 1849 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
1da177e4
LT
1850 if (ic == NULL) {
1851 /* We've been asked to examine this packet, and we
9c547959
PM
1852 * can't. Hence, no choice but to drop.
1853 */
b4ba2611 1854 par->hotdrop = true;
1d93a9cb 1855 return false;
1da177e4
LT
1856 }
1857
1858 return icmp6_type_code_match(icmpinfo->type,
1859 icmpinfo->code[0],
1860 icmpinfo->code[1],
1861 ic->icmp6_type, ic->icmp6_code,
1862 !!(icmpinfo->invflags&IP6T_ICMP_INV));
1863}
1864
1865/* Called when user tries to insert an entry of this type. */
b0f38452 1866static int icmp6_checkentry(const struct xt_mtchk_param *par)
1da177e4 1867{
9b4fce7a 1868 const struct ip6t_icmp *icmpinfo = par->matchinfo;
1da177e4 1869
7f939713 1870 /* Must specify no unknown invflags */
bd414ee6 1871 return (icmpinfo->invflags & ~IP6T_ICMP_INV) ? -EINVAL : 0;
1da177e4
LT
1872}
1873
1874/* The built-in targets: standard (NULL) and error. */
4538506b
JE
1875static struct xt_target ip6t_builtin_tg[] __read_mostly = {
1876 {
243bf6e2 1877 .name = XT_STANDARD_TARGET,
4538506b
JE
1878 .targetsize = sizeof(int),
1879 .family = NFPROTO_IPV6,
3bc3fe5e 1880#ifdef CONFIG_COMPAT
4538506b
JE
1881 .compatsize = sizeof(compat_int_t),
1882 .compat_from_user = compat_standard_from_user,
1883 .compat_to_user = compat_standard_to_user,
3bc3fe5e 1884#endif
4538506b
JE
1885 },
1886 {
243bf6e2 1887 .name = XT_ERROR_TARGET,
4538506b 1888 .target = ip6t_error,
12b00c2c 1889 .targetsize = XT_FUNCTION_MAXNAMELEN,
4538506b
JE
1890 .family = NFPROTO_IPV6,
1891 },
1da177e4
LT
1892};
1893
1894static struct nf_sockopt_ops ip6t_sockopts = {
1895 .pf = PF_INET6,
1896 .set_optmin = IP6T_BASE_CTL,
1897 .set_optmax = IP6T_SO_SET_MAX+1,
1898 .set = do_ip6t_set_ctl,
3bc3fe5e
PM
1899#ifdef CONFIG_COMPAT
1900 .compat_set = compat_do_ip6t_set_ctl,
1901#endif
1da177e4
LT
1902 .get_optmin = IP6T_BASE_CTL,
1903 .get_optmax = IP6T_SO_GET_MAX+1,
1904 .get = do_ip6t_get_ctl,
3bc3fe5e
PM
1905#ifdef CONFIG_COMPAT
1906 .compat_get = compat_do_ip6t_get_ctl,
1907#endif
16fcec35 1908 .owner = THIS_MODULE,
1da177e4
LT
1909};
1910
4538506b
JE
1911static struct xt_match ip6t_builtin_mt[] __read_mostly = {
1912 {
1913 .name = "icmp6",
1914 .match = icmp6_match,
1915 .matchsize = sizeof(struct ip6t_icmp),
1916 .checkentry = icmp6_checkentry,
1917 .proto = IPPROTO_ICMPV6,
1918 .family = NFPROTO_IPV6,
1919 },
1da177e4
LT
1920};
1921
3cb609d5
AD
1922static int __net_init ip6_tables_net_init(struct net *net)
1923{
383ca5b8 1924 return xt_proto_init(net, NFPROTO_IPV6);
3cb609d5
AD
1925}
1926
1927static void __net_exit ip6_tables_net_exit(struct net *net)
1928{
383ca5b8 1929 xt_proto_fini(net, NFPROTO_IPV6);
3cb609d5
AD
1930}
1931
1932static struct pernet_operations ip6_tables_net_ops = {
1933 .init = ip6_tables_net_init,
1934 .exit = ip6_tables_net_exit,
1935};
1936
65b4b4e8 1937static int __init ip6_tables_init(void)
1da177e4
LT
1938{
1939 int ret;
1940
3cb609d5 1941 ret = register_pernet_subsys(&ip6_tables_net_ops);
0eff66e6
PM
1942 if (ret < 0)
1943 goto err1;
2e4e6a17 1944
25985edc 1945 /* No one else will be downing sem now, so we won't sleep */
4538506b 1946 ret = xt_register_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
0eff66e6
PM
1947 if (ret < 0)
1948 goto err2;
4538506b 1949 ret = xt_register_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
0eff66e6
PM
1950 if (ret < 0)
1951 goto err4;
1da177e4
LT
1952
1953 /* Register setsockopt */
1954 ret = nf_register_sockopt(&ip6t_sockopts);
0eff66e6
PM
1955 if (ret < 0)
1956 goto err5;
1da177e4 1957
ff67e4e4 1958 pr_info("(C) 2000-2006 Netfilter Core Team\n");
1da177e4 1959 return 0;
0eff66e6
PM
1960
1961err5:
4538506b 1962 xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
0eff66e6 1963err4:
4538506b 1964 xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
0eff66e6 1965err2:
3cb609d5 1966 unregister_pernet_subsys(&ip6_tables_net_ops);
0eff66e6
PM
1967err1:
1968 return ret;
1da177e4
LT
1969}
1970
65b4b4e8 1971static void __exit ip6_tables_fini(void)
1da177e4
LT
1972{
1973 nf_unregister_sockopt(&ip6t_sockopts);
9c547959 1974
4538506b
JE
1975 xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
1976 xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
3cb609d5 1977 unregister_pernet_subsys(&ip6_tables_net_ops);
1da177e4
LT
1978}
1979
1980EXPORT_SYMBOL(ip6t_register_table);
1981EXPORT_SYMBOL(ip6t_unregister_table);
1982EXPORT_SYMBOL(ip6t_do_table);
1da177e4 1983
65b4b4e8
AM
1984module_init(ip6_tables_init);
1985module_exit(ip6_tables_fini);