f2fs: Provide a splice-read wrapper
[linux-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
c2f12630 1128 if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
3bc3fe5e
PM
1129 return -EFAULT;
1130
1131 /* overflow check */
1132 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1133 return -ENOMEM;
1086bbe9
DJ
1134 if (tmp.num_counters == 0)
1135 return -EINVAL;
1136
6a8ab060 1137 tmp.name[sizeof(tmp.name)-1] = 0;
3bc3fe5e
PM
1138
1139 newinfo = xt_alloc_table_info(tmp.size);
1140 if (!newinfo)
1141 return -ENOMEM;
1142
482cfc31 1143 loc_cpu_entry = newinfo->entries;
d3c48151
CH
1144 if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
1145 tmp.size) != 0) {
3bc3fe5e
PM
1146 ret = -EFAULT;
1147 goto free_newinfo;
1148 }
1149
0f234214 1150 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
3bc3fe5e
PM
1151 if (ret != 0)
1152 goto free_newinfo;
1153
336b517f 1154 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
3bc3fe5e
PM
1155 tmp.num_counters, tmp.counters);
1156 if (ret)
1157 goto free_newinfo_untrans;
1158 return 0;
1159
1160 free_newinfo_untrans:
72b2b1dd 1161 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
0559518b 1162 cleanup_entry(iter, net);
1da177e4 1163 free_newinfo:
2e4e6a17 1164 xt_free_table_info(newinfo);
1da177e4
LT
1165 return ret;
1166}
1167
1da177e4 1168static int
ab214d1b 1169do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
1da177e4 1170{
482cfc31 1171 unsigned int i;
3bc3fe5e
PM
1172 struct xt_counters_info tmp;
1173 struct xt_counters *paddc;
2e4e6a17 1174 struct xt_table *t;
5452e425 1175 const struct xt_table_info *private;
6b7d31fc 1176 int ret = 0;
72b2b1dd 1177 struct ip6t_entry *iter;
7f5c6d4f 1178 unsigned int addend;
1da177e4 1179
ab214d1b 1180 paddc = xt_copy_counters(arg, len, &tmp);
d7591f0c
FW
1181 if (IS_ERR(paddc))
1182 return PTR_ERR(paddc);
1183 t = xt_find_table_lock(net, AF_INET6, tmp.name);
03d13b68
FW
1184 if (IS_ERR(t)) {
1185 ret = PTR_ERR(t);
1da177e4 1186 goto free;
6b7d31fc 1187 }
1da177e4 1188
942e4a2b 1189 local_bh_disable();
d3d40f23 1190 private = t->private;
d7591f0c 1191 if (private->number != tmp.num_counters) {
1da177e4
LT
1192 ret = -EINVAL;
1193 goto unlock_up_free;
1194 }
1195
1196 i = 0;
7f5c6d4f 1197 addend = xt_write_recseq_begin();
482cfc31 1198 xt_entry_foreach(iter, private->entries, private->size) {
71ae0dff
FW
1199 struct xt_counters *tmp;
1200
1201 tmp = xt_get_this_cpu_counter(&iter->counters);
1202 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
0559518b
JE
1203 ++i;
1204 }
7f5c6d4f 1205 xt_write_recseq_end(addend);
1da177e4 1206 unlock_up_free:
942e4a2b 1207 local_bh_enable();
2e4e6a17 1208 xt_table_unlock(t);
6b7d31fc 1209 module_put(t->me);
1da177e4
LT
1210 free:
1211 vfree(paddc);
1212
1213 return ret;
1214}
1215
47a6959f 1216#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
3bc3fe5e 1217struct compat_ip6t_replace {
12b00c2c 1218 char name[XT_TABLE_MAXNAMELEN];
3bc3fe5e
PM
1219 u32 valid_hooks;
1220 u32 num_entries;
1221 u32 size;
1222 u32 hook_entry[NF_INET_NUMHOOKS];
1223 u32 underflow[NF_INET_NUMHOOKS];
1224 u32 num_counters;
87a2e70d 1225 compat_uptr_t counters; /* struct xt_counters * */
6daf1414 1226 struct compat_ip6t_entry entries[];
3bc3fe5e
PM
1227};
1228
1229static int
1230compat_copy_entry_to_user(struct ip6t_entry *e, void __user **dstptr,
b0a6363c 1231 unsigned int *size, struct xt_counters *counters,
0559518b 1232 unsigned int i)
3bc3fe5e 1233{
87a2e70d 1234 struct xt_entry_target *t;
3bc3fe5e
PM
1235 struct compat_ip6t_entry __user *ce;
1236 u_int16_t target_offset, next_offset;
1237 compat_uint_t origsize;
dcea992a
JE
1238 const struct xt_entry_match *ematch;
1239 int ret = 0;
3bc3fe5e 1240
3bc3fe5e 1241 origsize = *size;
68ad546a 1242 ce = *dstptr;
0559518b
JE
1243 if (copy_to_user(ce, e, sizeof(struct ip6t_entry)) != 0 ||
1244 copy_to_user(&ce->counters, &counters[i],
1245 sizeof(counters[i])) != 0)
1246 return -EFAULT;
3bc3fe5e
PM
1247
1248 *dstptr += sizeof(struct compat_ip6t_entry);
1249 *size -= sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1250
dcea992a
JE
1251 xt_ematch_foreach(ematch, e) {
1252 ret = xt_compat_match_to_user(ematch, dstptr, size);
1253 if (ret != 0)
6bdb331b 1254 return ret;
dcea992a 1255 }
3bc3fe5e 1256 target_offset = e->target_offset - (origsize - *size);
3bc3fe5e
PM
1257 t = ip6t_get_target(e);
1258 ret = xt_compat_target_to_user(t, dstptr, size);
1259 if (ret)
0559518b 1260 return ret;
3bc3fe5e 1261 next_offset = e->next_offset - (origsize - *size);
0559518b
JE
1262 if (put_user(target_offset, &ce->target_offset) != 0 ||
1263 put_user(next_offset, &ce->next_offset) != 0)
1264 return -EFAULT;
3bc3fe5e 1265 return 0;
3bc3fe5e
PM
1266}
1267
022748a9 1268static int
87a2e70d 1269compat_find_calc_match(struct xt_entry_match *m,
3bc3fe5e 1270 const struct ip6t_ip6 *ipv6,
6bdb331b 1271 int *size)
3bc3fe5e
PM
1272{
1273 struct xt_match *match;
1274
fd0ec0e6
JE
1275 match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
1276 m->u.user.revision);
d7cdf816 1277 if (IS_ERR(match))
fd0ec0e6 1278 return PTR_ERR(match);
d7cdf816 1279
3bc3fe5e
PM
1280 m->u.kernel.match = match;
1281 *size += xt_compat_match_offset(match);
3bc3fe5e
PM
1282 return 0;
1283}
1284
0559518b 1285static void compat_release_entry(struct compat_ip6t_entry *e)
3bc3fe5e 1286{
87a2e70d 1287 struct xt_entry_target *t;
dcea992a 1288 struct xt_entry_match *ematch;
3bc3fe5e 1289
3bc3fe5e 1290 /* Cleanup all matches */
dcea992a 1291 xt_ematch_foreach(ematch, e)
6bdb331b 1292 module_put(ematch->u.kernel.match->me);
3bc3fe5e
PM
1293 t = compat_ip6t_get_target(e);
1294 module_put(t->u.kernel.target->me);
3bc3fe5e
PM
1295}
1296
022748a9 1297static int
3bc3fe5e
PM
1298check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
1299 struct xt_table_info *newinfo,
1300 unsigned int *size,
d5d1baa1 1301 const unsigned char *base,
09d96860 1302 const unsigned char *limit)
3bc3fe5e 1303{
dcea992a 1304 struct xt_entry_match *ematch;
87a2e70d 1305 struct xt_entry_target *t;
3bc3fe5e
PM
1306 struct xt_target *target;
1307 unsigned int entry_offset;
b0a6363c 1308 unsigned int j;
09d96860 1309 int ret, off;
3bc3fe5e 1310
3666ed1c 1311 if ((unsigned long)e % __alignof__(struct compat_ip6t_entry) != 0 ||
6e94e0cf 1312 (unsigned char *)e + sizeof(struct compat_ip6t_entry) >= limit ||
d7cdf816 1313 (unsigned char *)e + e->next_offset > limit)
3bc3fe5e 1314 return -EINVAL;
3bc3fe5e
PM
1315
1316 if (e->next_offset < sizeof(struct compat_ip6t_entry) +
d7cdf816 1317 sizeof(struct compat_xt_entry_target))
3bc3fe5e 1318 return -EINVAL;
3bc3fe5e 1319
aa412ba2
FW
1320 if (!ip6_checkentry(&e->ipv6))
1321 return -EINVAL;
1322
ce683e5f 1323 ret = xt_compat_check_entry_offsets(e, e->elems,
fc1221b3 1324 e->target_offset, e->next_offset);
3bc3fe5e
PM
1325 if (ret)
1326 return ret;
1327
1328 off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1329 entry_offset = (void *)e - (void *)base;
1330 j = 0;
dcea992a 1331 xt_ematch_foreach(ematch, e) {
329a0807 1332 ret = compat_find_calc_match(ematch, &e->ipv6, &off);
dcea992a 1333 if (ret != 0)
6bdb331b
JE
1334 goto release_matches;
1335 ++j;
dcea992a 1336 }
3bc3fe5e
PM
1337
1338 t = compat_ip6t_get_target(e);
d2a7b6ba
JE
1339 target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name,
1340 t->u.user.revision);
1341 if (IS_ERR(target)) {
d2a7b6ba 1342 ret = PTR_ERR(target);
3bc3fe5e
PM
1343 goto release_matches;
1344 }
1345 t->u.kernel.target = target;
1346
1347 off += xt_compat_target_offset(target);
1348 *size += off;
1349 ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
1350 if (ret)
1351 goto out;
1352
3bc3fe5e
PM
1353 return 0;
1354
1355out:
1356 module_put(t->u.kernel.target->me);
1357release_matches:
6bdb331b
JE
1358 xt_ematch_foreach(ematch, e) {
1359 if (j-- == 0)
dcea992a 1360 break;
6bdb331b
JE
1361 module_put(ematch->u.kernel.match->me);
1362 }
3bc3fe5e
PM
1363 return ret;
1364}
1365
0188346f 1366static void
3bc3fe5e 1367compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr,
329a0807 1368 unsigned int *size,
3bc3fe5e
PM
1369 struct xt_table_info *newinfo, unsigned char *base)
1370{
87a2e70d 1371 struct xt_entry_target *t;
3bc3fe5e
PM
1372 struct ip6t_entry *de;
1373 unsigned int origsize;
0188346f 1374 int h;
dcea992a 1375 struct xt_entry_match *ematch;
3bc3fe5e 1376
3bc3fe5e 1377 origsize = *size;
68ad546a 1378 de = *dstptr;
3bc3fe5e
PM
1379 memcpy(de, e, sizeof(struct ip6t_entry));
1380 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1381
1382 *dstptr += sizeof(struct ip6t_entry);
1383 *size += sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1384
0188346f
FW
1385 xt_ematch_foreach(ematch, e)
1386 xt_compat_match_from_user(ematch, dstptr, size);
1387
3bc3fe5e
PM
1388 de->target_offset = e->target_offset - (origsize - *size);
1389 t = compat_ip6t_get_target(e);
3bc3fe5e
PM
1390 xt_compat_target_from_user(t, dstptr, size);
1391
1392 de->next_offset = e->next_offset - (origsize - *size);
1393 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1394 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1395 newinfo->hook_entry[h] -= origsize - *size;
1396 if ((unsigned char *)de - base < newinfo->underflow[h])
1397 newinfo->underflow[h] -= origsize - *size;
1398 }
3bc3fe5e
PM
1399}
1400
3bc3fe5e 1401static int
f54e9367 1402translate_compat_table(struct net *net,
3bc3fe5e
PM
1403 struct xt_table_info **pinfo,
1404 void **pentry0,
329a0807 1405 const struct compat_ip6t_replace *compatr)
3bc3fe5e
PM
1406{
1407 unsigned int i, j;
1408 struct xt_table_info *newinfo, *info;
1409 void *pos, *entry0, *entry1;
72b2b1dd 1410 struct compat_ip6t_entry *iter0;
09d96860 1411 struct ip6t_replace repl;
3bc3fe5e 1412 unsigned int size;
9782a11e 1413 int ret;
3bc3fe5e
PM
1414
1415 info = *pinfo;
1416 entry0 = *pentry0;
329a0807
FW
1417 size = compatr->size;
1418 info->number = compatr->num_entries;
3bc3fe5e 1419
3bc3fe5e
PM
1420 j = 0;
1421 xt_compat_lock(AF_INET6);
9782a11e
FW
1422 ret = xt_compat_init_offsets(AF_INET6, compatr->num_entries);
1423 if (ret)
1424 goto out_unlock;
3bc3fe5e 1425 /* Walk through entries, checking offsets. */
329a0807 1426 xt_entry_foreach(iter0, entry0, compatr->size) {
72b2b1dd 1427 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
6b4ff2d7 1428 entry0,
09d96860 1429 entry0 + compatr->size);
72b2b1dd 1430 if (ret != 0)
0559518b
JE
1431 goto out_unlock;
1432 ++j;
72b2b1dd 1433 }
3bc3fe5e
PM
1434
1435 ret = -EINVAL;
d7cdf816 1436 if (j != compatr->num_entries)
3bc3fe5e 1437 goto out_unlock;
3bc3fe5e 1438
3bc3fe5e
PM
1439 ret = -ENOMEM;
1440 newinfo = xt_alloc_table_info(size);
1441 if (!newinfo)
1442 goto out_unlock;
1443
b29c457a
FW
1444 memset(newinfo->entries, 0, size);
1445
329a0807 1446 newinfo->number = compatr->num_entries;
3bc3fe5e 1447 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
09d96860
FW
1448 newinfo->hook_entry[i] = compatr->hook_entry[i];
1449 newinfo->underflow[i] = compatr->underflow[i];
3bc3fe5e 1450 }
482cfc31 1451 entry1 = newinfo->entries;
3bc3fe5e 1452 pos = entry1;
09d96860 1453 size = compatr->size;
0188346f
FW
1454 xt_entry_foreach(iter0, entry0, compatr->size)
1455 compat_copy_entry_from_user(iter0, &pos, &size,
1456 newinfo, entry1);
1457
09d96860 1458 /* all module references in entry0 are now gone. */
3bc3fe5e
PM
1459 xt_compat_flush_offsets(AF_INET6);
1460 xt_compat_unlock(AF_INET6);
3bc3fe5e 1461
09d96860 1462 memcpy(&repl, compatr, sizeof(*compatr));
3bc3fe5e 1463
09d96860
FW
1464 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1465 repl.hook_entry[i] = newinfo->hook_entry[i];
1466 repl.underflow[i] = newinfo->underflow[i];
3bc3fe5e
PM
1467 }
1468
09d96860
FW
1469 repl.num_counters = 0;
1470 repl.counters = NULL;
1471 repl.size = newinfo->size;
1472 ret = translate_table(net, newinfo, entry1, &repl);
1473 if (ret)
1474 goto free_newinfo;
1475
3bc3fe5e
PM
1476 *pinfo = newinfo;
1477 *pentry0 = entry1;
1478 xt_free_table_info(info);
1479 return 0;
1480
1481free_newinfo:
1482 xt_free_table_info(newinfo);
09d96860
FW
1483 return ret;
1484out_unlock:
1485 xt_compat_flush_offsets(AF_INET6);
1486 xt_compat_unlock(AF_INET6);
329a0807 1487 xt_entry_foreach(iter0, entry0, compatr->size) {
0559518b 1488 if (j-- == 0)
72b2b1dd 1489 break;
0559518b
JE
1490 compat_release_entry(iter0);
1491 }
3bc3fe5e 1492 return ret;
3bc3fe5e
PM
1493}
1494
1495static int
c2f12630 1496compat_do_replace(struct net *net, sockptr_t arg, unsigned int len)
3bc3fe5e
PM
1497{
1498 int ret;
1499 struct compat_ip6t_replace tmp;
1500 struct xt_table_info *newinfo;
1501 void *loc_cpu_entry;
72b2b1dd 1502 struct ip6t_entry *iter;
3bc3fe5e 1503
c2f12630 1504 if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
3bc3fe5e
PM
1505 return -EFAULT;
1506
1507 /* overflow check */
3bc3fe5e
PM
1508 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1509 return -ENOMEM;
1086bbe9
DJ
1510 if (tmp.num_counters == 0)
1511 return -EINVAL;
1512
6a8ab060 1513 tmp.name[sizeof(tmp.name)-1] = 0;
3bc3fe5e
PM
1514
1515 newinfo = xt_alloc_table_info(tmp.size);
1516 if (!newinfo)
1517 return -ENOMEM;
1518
482cfc31 1519 loc_cpu_entry = newinfo->entries;
d3c48151
CH
1520 if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
1521 tmp.size) != 0) {
3bc3fe5e
PM
1522 ret = -EFAULT;
1523 goto free_newinfo;
1524 }
1525
329a0807 1526 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
3bc3fe5e
PM
1527 if (ret != 0)
1528 goto free_newinfo;
1529
336b517f 1530 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
3bc3fe5e
PM
1531 tmp.num_counters, compat_ptr(tmp.counters));
1532 if (ret)
1533 goto free_newinfo_untrans;
1534 return 0;
1535
1536 free_newinfo_untrans:
72b2b1dd 1537 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
0559518b 1538 cleanup_entry(iter, net);
3bc3fe5e
PM
1539 free_newinfo:
1540 xt_free_table_info(newinfo);
1541 return ret;
1542}
1543
3bc3fe5e 1544struct compat_ip6t_get_entries {
12b00c2c 1545 char name[XT_TABLE_MAXNAMELEN];
3bc3fe5e 1546 compat_uint_t size;
6daf1414 1547 struct compat_ip6t_entry entrytable[];
3bc3fe5e
PM
1548};
1549
1550static int
1551compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1552 void __user *userptr)
1553{
1554 struct xt_counters *counters;
d3d40f23 1555 const struct xt_table_info *private = table->private;
3bc3fe5e
PM
1556 void __user *pos;
1557 unsigned int size;
1558 int ret = 0;
3bc3fe5e 1559 unsigned int i = 0;
72b2b1dd 1560 struct ip6t_entry *iter;
3bc3fe5e
PM
1561
1562 counters = alloc_counters(table);
1563 if (IS_ERR(counters))
1564 return PTR_ERR(counters);
1565
3bc3fe5e
PM
1566 pos = userptr;
1567 size = total_size;
482cfc31 1568 xt_entry_foreach(iter, private->entries, total_size) {
72b2b1dd 1569 ret = compat_copy_entry_to_user(iter, &pos,
6b4ff2d7 1570 &size, counters, i++);
72b2b1dd
JE
1571 if (ret != 0)
1572 break;
1573 }
3bc3fe5e
PM
1574
1575 vfree(counters);
1576 return ret;
1577}
1578
1579static int
336b517f
AD
1580compat_get_entries(struct net *net, struct compat_ip6t_get_entries __user *uptr,
1581 int *len)
3bc3fe5e
PM
1582{
1583 int ret;
1584 struct compat_ip6t_get_entries get;
1585 struct xt_table *t;
1586
d7cdf816 1587 if (*len < sizeof(get))
3bc3fe5e 1588 return -EINVAL;
3bc3fe5e
PM
1589
1590 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1591 return -EFAULT;
1592
d7cdf816 1593 if (*len != sizeof(struct compat_ip6t_get_entries) + get.size)
3bc3fe5e 1594 return -EINVAL;
d7cdf816 1595
b301f253 1596 get.name[sizeof(get.name) - 1] = '\0';
3bc3fe5e
PM
1597
1598 xt_compat_lock(AF_INET6);
336b517f 1599 t = xt_find_table_lock(net, AF_INET6, get.name);
03d13b68 1600 if (!IS_ERR(t)) {
abe7034b 1601 const struct xt_table_info *private = t->private;
3bc3fe5e 1602 struct xt_table_info info;
3bc3fe5e 1603 ret = compat_table_info(private, &info);
d7cdf816 1604 if (!ret && get.size == info.size)
3bc3fe5e
PM
1605 ret = compat_copy_entries_to_user(private->size,
1606 t, uptr->entrytable);
d7cdf816 1607 else if (!ret)
544473c1 1608 ret = -EAGAIN;
d7cdf816 1609
3bc3fe5e
PM
1610 xt_compat_flush_offsets(AF_INET6);
1611 module_put(t->me);
1612 xt_table_unlock(t);
1613 } else
03d13b68 1614 ret = PTR_ERR(t);
3bc3fe5e
PM
1615
1616 xt_compat_unlock(AF_INET6);
1617 return ret;
1618}
3bc3fe5e
PM
1619#endif
1620
1da177e4 1621static int
c2f12630 1622do_ip6t_set_ctl(struct sock *sk, int cmd, sockptr_t arg, unsigned int len)
1da177e4
LT
1623{
1624 int ret;
1625
af31f412 1626 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1da177e4
LT
1627 return -EPERM;
1628
1629 switch (cmd) {
1630 case IP6T_SO_SET_REPLACE:
47a6959f 1631#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
f415e76f 1632 if (in_compat_syscall())
c2f12630 1633 ret = compat_do_replace(sock_net(sk), arg, len);
f415e76f
CH
1634 else
1635#endif
c2f12630 1636 ret = do_replace(sock_net(sk), arg, len);
1da177e4
LT
1637 break;
1638
1639 case IP6T_SO_SET_ADD_COUNTERS:
c2f12630 1640 ret = do_add_counters(sock_net(sk), arg, len);
1da177e4
LT
1641 break;
1642
1643 default:
1da177e4
LT
1644 ret = -EINVAL;
1645 }
1646
1647 return ret;
1648}
1649
1650static int
1651do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1652{
1653 int ret;
1654
af31f412 1655 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1da177e4
LT
1656 return -EPERM;
1657
1658 switch (cmd) {
433665c9 1659 case IP6T_SO_GET_INFO:
f415e76f 1660 ret = get_info(sock_net(sk), user, len);
433665c9 1661 break;
1da177e4 1662
d924357c 1663 case IP6T_SO_GET_ENTRIES:
47a6959f 1664#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
f415e76f
CH
1665 if (in_compat_syscall())
1666 ret = compat_get_entries(sock_net(sk), user, len);
1667 else
1668#endif
1669 ret = get_entries(sock_net(sk), user, len);
1da177e4 1670 break;
1da177e4 1671
6b7d31fc
HW
1672 case IP6T_SO_GET_REVISION_MATCH:
1673 case IP6T_SO_GET_REVISION_TARGET: {
12b00c2c 1674 struct xt_get_revision rev;
2e4e6a17 1675 int target;
6b7d31fc
HW
1676
1677 if (*len != sizeof(rev)) {
1678 ret = -EINVAL;
1679 break;
1680 }
1681 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1682 ret = -EFAULT;
1683 break;
1684 }
6a8ab060 1685 rev.name[sizeof(rev.name)-1] = 0;
6b7d31fc
HW
1686
1687 if (cmd == IP6T_SO_GET_REVISION_TARGET)
2e4e6a17 1688 target = 1;
6b7d31fc 1689 else
2e4e6a17 1690 target = 0;
6b7d31fc 1691
2e4e6a17
HW
1692 try_then_request_module(xt_find_revision(AF_INET6, rev.name,
1693 rev.revision,
1694 target, &ret),
6b7d31fc
HW
1695 "ip6t_%s", rev.name);
1696 break;
1697 }
1698
1da177e4 1699 default:
1da177e4
LT
1700 ret = -EINVAL;
1701 }
1702
1703 return ret;
1704}
1705
b9e69e12
FW
1706static void __ip6t_unregister_table(struct net *net, struct xt_table *table)
1707{
1708 struct xt_table_info *private;
1709 void *loc_cpu_entry;
1710 struct module *table_owner = table->me;
1711 struct ip6t_entry *iter;
1712
1713 private = xt_unregister_table(table);
1714
1715 /* Decrease module usage counts and free resources */
1716 loc_cpu_entry = private->entries;
1717 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1718 cleanup_entry(iter, net);
1719 if (private->number > private->initial_entries)
1720 module_put(table_owner);
1721 xt_free_table_info(private);
1722}
1723
a67dd266
FW
1724int ip6t_register_table(struct net *net, const struct xt_table *table,
1725 const struct ip6t_replace *repl,
ee177a54 1726 const struct nf_hook_ops *template_ops)
1da177e4 1727{
ee177a54
FW
1728 struct nf_hook_ops *ops;
1729 unsigned int num_ops;
1730 int ret, i;
2e4e6a17 1731 struct xt_table_info *newinfo;
f3c5c1bf 1732 struct xt_table_info bootstrap = {0};
31836064 1733 void *loc_cpu_entry;
a98da11d 1734 struct xt_table *new_table;
1da177e4 1735
2e4e6a17 1736 newinfo = xt_alloc_table_info(repl->size);
a67dd266
FW
1737 if (!newinfo)
1738 return -ENOMEM;
1da177e4 1739
482cfc31 1740 loc_cpu_entry = newinfo->entries;
31836064 1741 memcpy(loc_cpu_entry, repl->entries, repl->size);
1da177e4 1742
0f234214 1743 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
ee177a54
FW
1744 if (ret != 0) {
1745 xt_free_table_info(newinfo);
1746 return ret;
1747 }
1da177e4 1748
336b517f 1749 new_table = xt_register_table(net, table, &bootstrap, newinfo);
a98da11d 1750 if (IS_ERR(new_table)) {
0af8c09c
PT
1751 struct ip6t_entry *iter;
1752
1753 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1754 cleanup_entry(iter, net);
ee177a54
FW
1755 xt_free_table_info(newinfo);
1756 return PTR_ERR(new_table);
1da177e4 1757 }
a67dd266 1758
ee177a54 1759 if (!template_ops)
ba7d284a 1760 return 0;
b9e69e12 1761
ee177a54
FW
1762 num_ops = hweight32(table->valid_hooks);
1763 if (num_ops == 0) {
1764 ret = -EINVAL;
1765 goto out_free;
b9e69e12
FW
1766 }
1767
ee177a54
FW
1768 ops = kmemdup(template_ops, sizeof(*ops) * num_ops, GFP_KERNEL);
1769 if (!ops) {
1770 ret = -ENOMEM;
1771 goto out_free;
1772 }
1773
1774 for (i = 0; i < num_ops; i++)
1775 ops[i].priv = new_table;
1776
1777 new_table->ops = ops;
1778
1779 ret = nf_register_net_hooks(net, ops, num_ops);
1780 if (ret != 0)
1781 goto out_free;
1782
a67dd266 1783 return ret;
1da177e4 1784
44d34e72 1785out_free:
ee177a54 1786 __ip6t_unregister_table(net, new_table);
a67dd266 1787 return ret;
1da177e4
LT
1788}
1789
ee177a54 1790void ip6t_unregister_table_pre_exit(struct net *net, const char *name)
57ea5f18 1791{
6c071754
FW
1792 struct xt_table *table = xt_find_table(net, NFPROTO_IPV6, name);
1793
1794 if (table)
ee177a54 1795 nf_unregister_net_hooks(net, table->ops, hweight32(table->valid_hooks));
57ea5f18
DW
1796}
1797
6c071754 1798void ip6t_unregister_table_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)
1803 __ip6t_unregister_table(net, table);
57ea5f18
DW
1804}
1805
1da177e4 1806/* The built-in targets: standard (NULL) and error. */
4538506b
JE
1807static struct xt_target ip6t_builtin_tg[] __read_mostly = {
1808 {
243bf6e2 1809 .name = XT_STANDARD_TARGET,
4538506b
JE
1810 .targetsize = sizeof(int),
1811 .family = NFPROTO_IPV6,
47a6959f 1812#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
4538506b
JE
1813 .compatsize = sizeof(compat_int_t),
1814 .compat_from_user = compat_standard_from_user,
1815 .compat_to_user = compat_standard_to_user,
3bc3fe5e 1816#endif
4538506b
JE
1817 },
1818 {
243bf6e2 1819 .name = XT_ERROR_TARGET,
4538506b 1820 .target = ip6t_error,
12b00c2c 1821 .targetsize = XT_FUNCTION_MAXNAMELEN,
4538506b
JE
1822 .family = NFPROTO_IPV6,
1823 },
1da177e4
LT
1824};
1825
1826static struct nf_sockopt_ops ip6t_sockopts = {
1827 .pf = PF_INET6,
1828 .set_optmin = IP6T_BASE_CTL,
1829 .set_optmax = IP6T_SO_SET_MAX+1,
1830 .set = do_ip6t_set_ctl,
1831 .get_optmin = IP6T_BASE_CTL,
1832 .get_optmax = IP6T_SO_GET_MAX+1,
1833 .get = do_ip6t_get_ctl,
16fcec35 1834 .owner = THIS_MODULE,
1da177e4
LT
1835};
1836
3cb609d5
AD
1837static int __net_init ip6_tables_net_init(struct net *net)
1838{
383ca5b8 1839 return xt_proto_init(net, NFPROTO_IPV6);
3cb609d5
AD
1840}
1841
1842static void __net_exit ip6_tables_net_exit(struct net *net)
1843{
383ca5b8 1844 xt_proto_fini(net, NFPROTO_IPV6);
3cb609d5
AD
1845}
1846
1847static struct pernet_operations ip6_tables_net_ops = {
1848 .init = ip6_tables_net_init,
1849 .exit = ip6_tables_net_exit,
1850};
1851
65b4b4e8 1852static int __init ip6_tables_init(void)
1da177e4
LT
1853{
1854 int ret;
1855
3cb609d5 1856 ret = register_pernet_subsys(&ip6_tables_net_ops);
0eff66e6
PM
1857 if (ret < 0)
1858 goto err1;
2e4e6a17 1859
25985edc 1860 /* No one else will be downing sem now, so we won't sleep */
4538506b 1861 ret = xt_register_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
0eff66e6
PM
1862 if (ret < 0)
1863 goto err2;
1da177e4
LT
1864
1865 /* Register setsockopt */
1866 ret = nf_register_sockopt(&ip6t_sockopts);
0eff66e6 1867 if (ret < 0)
36ce9982 1868 goto err4;
1da177e4 1869
1da177e4 1870 return 0;
0eff66e6 1871
0eff66e6 1872err4:
4538506b 1873 xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
0eff66e6 1874err2:
3cb609d5 1875 unregister_pernet_subsys(&ip6_tables_net_ops);
0eff66e6
PM
1876err1:
1877 return ret;
1da177e4
LT
1878}
1879
65b4b4e8 1880static void __exit ip6_tables_fini(void)
1da177e4
LT
1881{
1882 nf_unregister_sockopt(&ip6t_sockopts);
9c547959 1883
4538506b 1884 xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
3cb609d5 1885 unregister_pernet_subsys(&ip6_tables_net_ops);
1da177e4
LT
1886}
1887
1888EXPORT_SYMBOL(ip6t_register_table);
57ea5f18
DW
1889EXPORT_SYMBOL(ip6t_unregister_table_pre_exit);
1890EXPORT_SYMBOL(ip6t_unregister_table_exit);
1da177e4 1891EXPORT_SYMBOL(ip6t_do_table);
1da177e4 1892
65b4b4e8
AM
1893module_init(ip6_tables_init);
1894module_exit(ip6_tables_fini);