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