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