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