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