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