1 // SPDX-License-Identifier: GPL-2.0-only
3 * Packet matching code.
5 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
6 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
7 * Copyright (c) 2006-2010 Patrick McHardy <kaber@trash.net>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/kernel.h>
13 #include <linux/capability.h>
15 #include <linux/skbuff.h>
16 #include <linux/kmod.h>
17 #include <linux/vmalloc.h>
18 #include <linux/netdevice.h>
19 #include <linux/module.h>
20 #include <linux/poison.h>
21 #include <linux/icmpv6.h>
23 #include <net/compat.h>
24 #include <linux/uaccess.h>
25 #include <linux/mutex.h>
26 #include <linux/proc_fs.h>
27 #include <linux/err.h>
28 #include <linux/cpumask.h>
30 #include <linux/netfilter_ipv6/ip6_tables.h>
31 #include <linux/netfilter/x_tables.h>
32 #include <net/netfilter/nf_log.h>
33 #include "../../netfilter/xt_repldata.h"
35 MODULE_LICENSE("GPL");
36 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
37 MODULE_DESCRIPTION("IPv6 packet filter");
38 MODULE_ALIAS("ip6t_icmp6");
40 void *ip6t_alloc_initial_table(const struct xt_table *info)
42 return xt_alloc_initial_table(ip6t, IP6T);
44 EXPORT_SYMBOL_GPL(ip6t_alloc_initial_table);
46 /* Returns whether matches rule or not. */
47 /* Performance critical - called for every packet */
49 ip6_packet_match(const struct sk_buff *skb,
52 const struct ip6t_ip6 *ip6info,
53 unsigned int *protoff,
54 u16 *fragoff, bool *hotdrop)
57 const struct ipv6hdr *ipv6 = ipv6_hdr(skb);
59 if (NF_INVF(ip6info, IP6T_INV_SRCIP,
60 ipv6_masked_addr_cmp(&ipv6->saddr, &ip6info->smsk,
62 NF_INVF(ip6info, IP6T_INV_DSTIP,
63 ipv6_masked_addr_cmp(&ipv6->daddr, &ip6info->dmsk,
67 ret = ifname_compare_aligned(indev, ip6info->iniface, ip6info->iniface_mask);
69 if (NF_INVF(ip6info, IP6T_INV_VIA_IN, ret != 0))
72 ret = ifname_compare_aligned(outdev, ip6info->outiface, ip6info->outiface_mask);
74 if (NF_INVF(ip6info, IP6T_INV_VIA_OUT, ret != 0))
77 /* ... might want to do something with class and flowlabel here ... */
79 /* look for the desired protocol header */
80 if (ip6info->flags & IP6T_F_PROTO) {
82 unsigned short _frag_off;
84 protohdr = ipv6_find_hdr(skb, protoff, -1, &_frag_off, NULL);
92 if (ip6info->proto == protohdr) {
93 if (ip6info->invflags & IP6T_INV_PROTO)
99 /* We need match for the '-p all', too! */
100 if ((ip6info->proto != 0) &&
101 !(ip6info->invflags & IP6T_INV_PROTO))
107 /* should be ip6 safe */
109 ip6_checkentry(const struct ip6t_ip6 *ipv6)
111 if (ipv6->flags & ~IP6T_F_MASK)
113 if (ipv6->invflags & ~IP6T_INV_MASK)
120 ip6t_error(struct sk_buff *skb, const struct xt_action_param *par)
122 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
127 static inline struct ip6t_entry *
128 get_entry(const void *base, unsigned int offset)
130 return (struct ip6t_entry *)(base + offset);
133 /* All zeroes == unconditional rule. */
134 /* Mildly perf critical (only if packet tracing is on) */
135 static inline bool unconditional(const struct ip6t_entry *e)
137 static const struct ip6t_ip6 uncond;
139 return e->target_offset == sizeof(struct ip6t_entry) &&
140 memcmp(&e->ipv6, &uncond, sizeof(uncond)) == 0;
143 static inline const struct xt_entry_target *
144 ip6t_get_target_c(const struct ip6t_entry *e)
146 return ip6t_get_target((struct ip6t_entry *)e);
149 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
150 /* This cries for unification! */
151 static const char *const hooknames[] = {
152 [NF_INET_PRE_ROUTING] = "PREROUTING",
153 [NF_INET_LOCAL_IN] = "INPUT",
154 [NF_INET_FORWARD] = "FORWARD",
155 [NF_INET_LOCAL_OUT] = "OUTPUT",
156 [NF_INET_POST_ROUTING] = "POSTROUTING",
159 enum nf_ip_trace_comments {
160 NF_IP6_TRACE_COMMENT_RULE,
161 NF_IP6_TRACE_COMMENT_RETURN,
162 NF_IP6_TRACE_COMMENT_POLICY,
165 static const char *const comments[] = {
166 [NF_IP6_TRACE_COMMENT_RULE] = "rule",
167 [NF_IP6_TRACE_COMMENT_RETURN] = "return",
168 [NF_IP6_TRACE_COMMENT_POLICY] = "policy",
171 static const struct nf_loginfo trace_loginfo = {
172 .type = NF_LOG_TYPE_LOG,
175 .level = LOGLEVEL_WARNING,
176 .logflags = NF_LOG_DEFAULT_MASK,
181 /* Mildly perf critical (only if packet tracing is on) */
183 get_chainname_rulenum(const struct ip6t_entry *s, const struct ip6t_entry *e,
184 const char *hookname, const char **chainname,
185 const char **comment, unsigned int *rulenum)
187 const struct xt_standard_target *t = (void *)ip6t_get_target_c(s);
189 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
190 /* Head of user chain: ERROR target with chainname */
191 *chainname = t->target.data;
196 if (unconditional(s) &&
197 strcmp(t->target.u.kernel.target->name,
198 XT_STANDARD_TARGET) == 0 &&
200 /* Tail of chains: STANDARD target (return/policy) */
201 *comment = *chainname == hookname
202 ? comments[NF_IP6_TRACE_COMMENT_POLICY]
203 : comments[NF_IP6_TRACE_COMMENT_RETURN];
212 static void trace_packet(struct net *net,
213 const struct sk_buff *skb,
215 const struct net_device *in,
216 const struct net_device *out,
217 const char *tablename,
218 const struct xt_table_info *private,
219 const struct ip6t_entry *e)
221 const struct ip6t_entry *root;
222 const char *hookname, *chainname, *comment;
223 const struct ip6t_entry *iter;
224 unsigned int rulenum = 0;
226 root = get_entry(private->entries, private->hook_entry[hook]);
228 hookname = chainname = hooknames[hook];
229 comment = comments[NF_IP6_TRACE_COMMENT_RULE];
231 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
232 if (get_chainname_rulenum(iter, e, hookname,
233 &chainname, &comment, &rulenum) != 0)
236 nf_log_trace(net, AF_INET6, hook, skb, in, out, &trace_loginfo,
237 "TRACE: %s:%s:%s:%u ",
238 tablename, chainname, comment, rulenum);
242 static inline struct ip6t_entry *
243 ip6t_next_entry(const struct ip6t_entry *entry)
245 return (void *)entry + entry->next_offset;
248 /* Returns one of the generic firewall policies, like NF_ACCEPT. */
250 ip6t_do_table(void *priv, struct sk_buff *skb,
251 const struct nf_hook_state *state)
253 const struct xt_table *table = priv;
254 unsigned int hook = state->hook;
255 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
256 /* Initializing verdict to NF_DROP keeps gcc happy. */
257 unsigned int verdict = NF_DROP;
258 const char *indev, *outdev;
259 const void *table_base;
260 struct ip6t_entry *e, **jumpstack;
261 unsigned int stackidx, cpu;
262 const struct xt_table_info *private;
263 struct xt_action_param acpar;
268 indev = state->in ? state->in->name : nulldevname;
269 outdev = state->out ? state->out->name : nulldevname;
270 /* We handle fragments by dealing with the first fragment as
271 * if it was a normal packet. All other fragments are treated
272 * normally, except that they will NEVER match rules that ask
273 * things we don't know, ie. tcp syn flag or ports). If the
274 * rule is also a fragment-specific rule, non-fragments won't
277 acpar.hotdrop = false;
280 WARN_ON(!(table->valid_hooks & (1 << hook)));
283 addend = xt_write_recseq_begin();
284 private = READ_ONCE(table->private); /* Address dependency. */
285 cpu = smp_processor_id();
286 table_base = private->entries;
287 jumpstack = (struct ip6t_entry **)private->jumpstack[cpu];
289 /* Switch to alternate jumpstack if we're being invoked via TEE.
290 * TEE issues XT_CONTINUE verdict on original skb so we must not
291 * clobber the jumpstack.
293 * For recursion via REJECT or SYNPROXY the stack will be clobbered
294 * but it is no problem since absolute verdict is issued by these.
296 if (static_key_false(&xt_tee_enabled))
297 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
299 e = get_entry(table_base, private->hook_entry[hook]);
302 const struct xt_entry_target *t;
303 const struct xt_entry_match *ematch;
304 struct xt_counters *counter;
308 if (!ip6_packet_match(skb, indev, outdev, &e->ipv6,
309 &acpar.thoff, &acpar.fragoff, &acpar.hotdrop)) {
311 e = ip6t_next_entry(e);
315 xt_ematch_foreach(ematch, e) {
316 acpar.match = ematch->u.kernel.match;
317 acpar.matchinfo = ematch->data;
318 if (!acpar.match->match(skb, &acpar))
322 counter = xt_get_this_cpu_counter(&e->counters);
323 ADD_COUNTER(*counter, skb->len, 1);
325 t = ip6t_get_target_c(e);
326 WARN_ON(!t->u.kernel.target);
328 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
329 /* The packet is traced: log it */
330 if (unlikely(skb->nf_trace))
331 trace_packet(state->net, skb, hook, state->in,
332 state->out, table->name, private, e);
334 /* Standard target? */
335 if (!t->u.kernel.target->target) {
338 v = ((struct xt_standard_target *)t)->verdict;
340 /* Pop from stack? */
341 if (v != XT_RETURN) {
342 verdict = (unsigned int)(-v) - 1;
346 e = get_entry(table_base,
347 private->underflow[hook]);
349 e = ip6t_next_entry(jumpstack[--stackidx]);
352 if (table_base + v != ip6t_next_entry(e) &&
353 !(e->ipv6.flags & IP6T_F_GOTO)) {
354 if (unlikely(stackidx >= private->stacksize)) {
358 jumpstack[stackidx++] = e;
361 e = get_entry(table_base, v);
365 acpar.target = t->u.kernel.target;
366 acpar.targinfo = t->data;
368 verdict = t->u.kernel.target->target(skb, &acpar);
369 if (verdict == XT_CONTINUE)
370 e = ip6t_next_entry(e);
374 } while (!acpar.hotdrop);
376 xt_write_recseq_end(addend);
384 /* Figures out from what hook each rule can be called: returns 0 if
385 there are loops. Puts hook bitmask in comefrom. */
387 mark_source_chains(const struct xt_table_info *newinfo,
388 unsigned int valid_hooks, void *entry0,
389 unsigned int *offsets)
393 /* No recursion; use packet counter to save back ptrs (reset
394 to 0 as we leave), and comefrom to save source hook bitmask */
395 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
396 unsigned int pos = newinfo->hook_entry[hook];
397 struct ip6t_entry *e = entry0 + pos;
399 if (!(valid_hooks & (1 << hook)))
402 /* Set initial back pointer. */
403 e->counters.pcnt = pos;
406 const struct xt_standard_target *t
407 = (void *)ip6t_get_target_c(e);
408 int visited = e->comefrom & (1 << hook);
410 if (e->comefrom & (1 << NF_INET_NUMHOOKS))
413 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
415 /* Unconditional return/END. */
416 if ((unconditional(e) &&
417 (strcmp(t->target.u.user.name,
418 XT_STANDARD_TARGET) == 0) &&
419 t->verdict < 0) || visited) {
420 unsigned int oldpos, size;
422 /* Return: backtrack through the last
425 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
427 pos = e->counters.pcnt;
428 e->counters.pcnt = 0;
430 /* We're at the start. */
435 } while (oldpos == pos + e->next_offset);
438 size = e->next_offset;
439 e = entry0 + pos + size;
440 if (pos + size >= newinfo->size)
442 e->counters.pcnt = pos;
445 int newpos = t->verdict;
447 if (strcmp(t->target.u.user.name,
448 XT_STANDARD_TARGET) == 0 &&
450 /* This a jump; chase it. */
451 if (!xt_find_jump_offset(offsets, newpos,
455 /* ... this is a fallthru */
456 newpos = pos + e->next_offset;
457 if (newpos >= newinfo->size)
461 e->counters.pcnt = pos;
470 static void cleanup_match(struct xt_entry_match *m, struct net *net)
472 struct xt_mtdtor_param par;
475 par.match = m->u.kernel.match;
476 par.matchinfo = m->data;
477 par.family = NFPROTO_IPV6;
478 if (par.match->destroy != NULL)
479 par.match->destroy(&par);
480 module_put(par.match->me);
483 static int check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
485 const struct ip6t_ip6 *ipv6 = par->entryinfo;
487 par->match = m->u.kernel.match;
488 par->matchinfo = m->data;
490 return xt_check_match(par, m->u.match_size - sizeof(*m),
491 ipv6->proto, ipv6->invflags & IP6T_INV_PROTO);
495 find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
497 struct xt_match *match;
500 match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
503 return PTR_ERR(match);
505 m->u.kernel.match = match;
507 ret = check_match(m, par);
513 module_put(m->u.kernel.match->me);
517 static int check_target(struct ip6t_entry *e, struct net *net, const char *name)
519 struct xt_entry_target *t = ip6t_get_target(e);
520 struct xt_tgchk_param par = {
524 .target = t->u.kernel.target,
526 .hook_mask = e->comefrom,
527 .family = NFPROTO_IPV6,
530 return xt_check_target(&par, t->u.target_size - sizeof(*t),
532 e->ipv6.invflags & IP6T_INV_PROTO);
536 find_check_entry(struct ip6t_entry *e, struct net *net, const char *name,
538 struct xt_percpu_counter_alloc_state *alloc_state)
540 struct xt_entry_target *t;
541 struct xt_target *target;
544 struct xt_mtchk_param mtpar;
545 struct xt_entry_match *ematch;
547 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
551 memset(&mtpar, 0, sizeof(mtpar));
554 mtpar.entryinfo = &e->ipv6;
555 mtpar.hook_mask = e->comefrom;
556 mtpar.family = NFPROTO_IPV6;
557 xt_ematch_foreach(ematch, e) {
558 ret = find_check_match(ematch, &mtpar);
560 goto cleanup_matches;
564 t = ip6t_get_target(e);
565 target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name,
567 if (IS_ERR(target)) {
568 ret = PTR_ERR(target);
569 goto cleanup_matches;
571 t->u.kernel.target = target;
573 ret = check_target(e, net, name);
578 module_put(t->u.kernel.target->me);
580 xt_ematch_foreach(ematch, e) {
583 cleanup_match(ematch, net);
586 xt_percpu_counter_free(&e->counters);
591 static bool check_underflow(const struct ip6t_entry *e)
593 const struct xt_entry_target *t;
594 unsigned int verdict;
596 if (!unconditional(e))
598 t = ip6t_get_target_c(e);
599 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
601 verdict = ((struct xt_standard_target *)t)->verdict;
602 verdict = -verdict - 1;
603 return verdict == NF_DROP || verdict == NF_ACCEPT;
607 check_entry_size_and_hooks(struct ip6t_entry *e,
608 struct xt_table_info *newinfo,
609 const unsigned char *base,
610 const unsigned char *limit,
611 const unsigned int *hook_entries,
612 const unsigned int *underflows,
613 unsigned int valid_hooks)
618 if ((unsigned long)e % __alignof__(struct ip6t_entry) != 0 ||
619 (unsigned char *)e + sizeof(struct ip6t_entry) >= limit ||
620 (unsigned char *)e + e->next_offset > limit)
624 < sizeof(struct ip6t_entry) + sizeof(struct xt_entry_target))
627 if (!ip6_checkentry(&e->ipv6))
630 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
635 /* Check hooks & underflows */
636 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
637 if (!(valid_hooks & (1 << h)))
639 if ((unsigned char *)e - base == hook_entries[h])
640 newinfo->hook_entry[h] = hook_entries[h];
641 if ((unsigned char *)e - base == underflows[h]) {
642 if (!check_underflow(e))
645 newinfo->underflow[h] = underflows[h];
649 /* Clear counters and comefrom */
650 e->counters = ((struct xt_counters) { 0, 0 });
655 static void cleanup_entry(struct ip6t_entry *e, struct net *net)
657 struct xt_tgdtor_param par;
658 struct xt_entry_target *t;
659 struct xt_entry_match *ematch;
661 /* Cleanup all matches */
662 xt_ematch_foreach(ematch, e)
663 cleanup_match(ematch, net);
664 t = ip6t_get_target(e);
667 par.target = t->u.kernel.target;
668 par.targinfo = t->data;
669 par.family = NFPROTO_IPV6;
670 if (par.target->destroy != NULL)
671 par.target->destroy(&par);
672 module_put(par.target->me);
673 xt_percpu_counter_free(&e->counters);
676 /* Checks and translates the user-supplied table segment (held in
679 translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
680 const struct ip6t_replace *repl)
682 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
683 struct ip6t_entry *iter;
684 unsigned int *offsets;
688 newinfo->size = repl->size;
689 newinfo->number = repl->num_entries;
691 /* Init all hooks to impossible value. */
692 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
693 newinfo->hook_entry[i] = 0xFFFFFFFF;
694 newinfo->underflow[i] = 0xFFFFFFFF;
697 offsets = xt_alloc_entry_offsets(newinfo->number);
701 /* Walk through entries, checking offsets. */
702 xt_entry_foreach(iter, entry0, newinfo->size) {
703 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
710 if (i < repl->num_entries)
711 offsets[i] = (void *)iter - entry0;
713 if (strcmp(ip6t_get_target(iter)->u.user.name,
714 XT_ERROR_TARGET) == 0)
715 ++newinfo->stacksize;
719 if (i != repl->num_entries)
722 ret = xt_check_table_hooks(newinfo, repl->valid_hooks);
726 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
732 /* Finally, each sanity check must pass */
734 xt_entry_foreach(iter, entry0, newinfo->size) {
735 ret = find_check_entry(iter, net, repl->name, repl->size,
743 xt_entry_foreach(iter, entry0, newinfo->size) {
746 cleanup_entry(iter, net);
758 get_counters(const struct xt_table_info *t,
759 struct xt_counters counters[])
761 struct ip6t_entry *iter;
765 for_each_possible_cpu(cpu) {
766 seqcount_t *s = &per_cpu(xt_recseq, cpu);
769 xt_entry_foreach(iter, t->entries, t->size) {
770 struct xt_counters *tmp;
774 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
776 start = read_seqcount_begin(s);
779 } while (read_seqcount_retry(s, start));
781 ADD_COUNTER(counters[i], bcnt, pcnt);
788 static void get_old_counters(const struct xt_table_info *t,
789 struct xt_counters counters[])
791 struct ip6t_entry *iter;
794 for_each_possible_cpu(cpu) {
796 xt_entry_foreach(iter, t->entries, t->size) {
797 const struct xt_counters *tmp;
799 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
800 ADD_COUNTER(counters[i], tmp->bcnt, tmp->pcnt);
807 static struct xt_counters *alloc_counters(const struct xt_table *table)
809 unsigned int countersize;
810 struct xt_counters *counters;
811 const struct xt_table_info *private = table->private;
813 /* We need atomic snapshot of counters: rest doesn't change
814 (other than comefrom, which userspace doesn't care
816 countersize = sizeof(struct xt_counters) * private->number;
817 counters = vzalloc(countersize);
819 if (counters == NULL)
820 return ERR_PTR(-ENOMEM);
822 get_counters(private, counters);
828 copy_entries_to_user(unsigned int total_size,
829 const struct xt_table *table,
830 void __user *userptr)
832 unsigned int off, num;
833 const struct ip6t_entry *e;
834 struct xt_counters *counters;
835 const struct xt_table_info *private = table->private;
837 const void *loc_cpu_entry;
839 counters = alloc_counters(table);
840 if (IS_ERR(counters))
841 return PTR_ERR(counters);
843 loc_cpu_entry = private->entries;
845 /* FIXME: use iterator macros --RR */
846 /* ... then go back and fix counters and names */
847 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
849 const struct xt_entry_match *m;
850 const struct xt_entry_target *t;
852 e = loc_cpu_entry + off;
853 if (copy_to_user(userptr + off, e, sizeof(*e))) {
857 if (copy_to_user(userptr + off
858 + offsetof(struct ip6t_entry, counters),
860 sizeof(counters[num])) != 0) {
865 for (i = sizeof(struct ip6t_entry);
866 i < e->target_offset;
867 i += m->u.match_size) {
870 if (xt_match_to_user(m, userptr + off + i)) {
876 t = ip6t_get_target_c(e);
877 if (xt_target_to_user(t, userptr + off + e->target_offset)) {
888 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
889 static void compat_standard_from_user(void *dst, const void *src)
891 int v = *(compat_int_t *)src;
894 v += xt_compat_calc_jump(AF_INET6, v);
895 memcpy(dst, &v, sizeof(v));
898 static int compat_standard_to_user(void __user *dst, const void *src)
900 compat_int_t cv = *(int *)src;
903 cv -= xt_compat_calc_jump(AF_INET6, cv);
904 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
907 static int compat_calc_entry(const struct ip6t_entry *e,
908 const struct xt_table_info *info,
909 const void *base, struct xt_table_info *newinfo)
911 const struct xt_entry_match *ematch;
912 const struct xt_entry_target *t;
913 unsigned int entry_offset;
916 off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
917 entry_offset = (void *)e - base;
918 xt_ematch_foreach(ematch, e)
919 off += xt_compat_match_offset(ematch->u.kernel.match);
920 t = ip6t_get_target_c(e);
921 off += xt_compat_target_offset(t->u.kernel.target);
922 newinfo->size -= off;
923 ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
927 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
928 if (info->hook_entry[i] &&
929 (e < (struct ip6t_entry *)(base + info->hook_entry[i])))
930 newinfo->hook_entry[i] -= off;
931 if (info->underflow[i] &&
932 (e < (struct ip6t_entry *)(base + info->underflow[i])))
933 newinfo->underflow[i] -= off;
938 static int compat_table_info(const struct xt_table_info *info,
939 struct xt_table_info *newinfo)
941 struct ip6t_entry *iter;
942 const void *loc_cpu_entry;
945 if (!newinfo || !info)
948 /* we dont care about newinfo->entries */
949 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
950 newinfo->initial_entries = 0;
951 loc_cpu_entry = info->entries;
952 ret = xt_compat_init_offsets(AF_INET6, info->number);
955 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
956 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
964 static int get_info(struct net *net, void __user *user, const int *len)
966 char name[XT_TABLE_MAXNAMELEN];
970 if (*len != sizeof(struct ip6t_getinfo))
973 if (copy_from_user(name, user, sizeof(name)) != 0)
976 name[XT_TABLE_MAXNAMELEN-1] = '\0';
977 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
978 if (in_compat_syscall())
979 xt_compat_lock(AF_INET6);
981 t = xt_request_find_table_lock(net, AF_INET6, name);
983 struct ip6t_getinfo info;
984 const struct xt_table_info *private = t->private;
985 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
986 struct xt_table_info tmp;
988 if (in_compat_syscall()) {
989 ret = compat_table_info(private, &tmp);
990 xt_compat_flush_offsets(AF_INET6);
994 memset(&info, 0, sizeof(info));
995 info.valid_hooks = t->valid_hooks;
996 memcpy(info.hook_entry, private->hook_entry,
997 sizeof(info.hook_entry));
998 memcpy(info.underflow, private->underflow,
999 sizeof(info.underflow));
1000 info.num_entries = private->number;
1001 info.size = private->size;
1002 strcpy(info.name, name);
1004 if (copy_to_user(user, &info, *len) != 0)
1013 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1014 if (in_compat_syscall())
1015 xt_compat_unlock(AF_INET6);
1021 get_entries(struct net *net, struct ip6t_get_entries __user *uptr,
1025 struct ip6t_get_entries get;
1028 if (*len < sizeof(get))
1030 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1032 if (*len != sizeof(struct ip6t_get_entries) + get.size)
1035 get.name[sizeof(get.name) - 1] = '\0';
1037 t = xt_find_table_lock(net, AF_INET6, get.name);
1039 struct xt_table_info *private = t->private;
1040 if (get.size == private->size)
1041 ret = copy_entries_to_user(private->size,
1042 t, uptr->entrytable);
1055 __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
1056 struct xt_table_info *newinfo, unsigned int num_counters,
1057 void __user *counters_ptr)
1061 struct xt_table_info *oldinfo;
1062 struct xt_counters *counters;
1063 struct ip6t_entry *iter;
1065 counters = xt_counters_alloc(num_counters);
1071 t = xt_request_find_table_lock(net, AF_INET6, name);
1074 goto free_newinfo_counters_untrans;
1078 if (valid_hooks != t->valid_hooks) {
1083 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1087 /* Update module usage count based on number of rules */
1088 if ((oldinfo->number > oldinfo->initial_entries) ||
1089 (newinfo->number <= oldinfo->initial_entries))
1091 if ((oldinfo->number > oldinfo->initial_entries) &&
1092 (newinfo->number <= oldinfo->initial_entries))
1097 get_old_counters(oldinfo, counters);
1099 /* Decrease module usage counts and free resource */
1100 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
1101 cleanup_entry(iter, net);
1103 xt_free_table_info(oldinfo);
1104 if (copy_to_user(counters_ptr, counters,
1105 sizeof(struct xt_counters) * num_counters) != 0) {
1106 /* Silent error, can't fail, new table is already in place */
1107 net_warn_ratelimited("ip6tables: counters copy to user failed while replacing table\n");
1115 free_newinfo_counters_untrans:
1122 do_replace(struct net *net, sockptr_t arg, unsigned int len)
1125 struct ip6t_replace tmp;
1126 struct xt_table_info *newinfo;
1127 void *loc_cpu_entry;
1128 struct ip6t_entry *iter;
1130 if (len < sizeof(tmp))
1132 if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
1135 /* overflow check */
1136 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1138 if (tmp.num_counters == 0)
1140 if ((u64)len < (u64)tmp.size + sizeof(tmp))
1143 tmp.name[sizeof(tmp.name)-1] = 0;
1145 newinfo = xt_alloc_table_info(tmp.size);
1149 loc_cpu_entry = newinfo->entries;
1150 if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
1156 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
1160 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1161 tmp.num_counters, tmp.counters);
1163 goto free_newinfo_untrans;
1166 free_newinfo_untrans:
1167 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1168 cleanup_entry(iter, net);
1170 xt_free_table_info(newinfo);
1175 do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
1178 struct xt_counters_info tmp;
1179 struct xt_counters *paddc;
1181 const struct xt_table_info *private;
1183 struct ip6t_entry *iter;
1184 unsigned int addend;
1186 paddc = xt_copy_counters(arg, len, &tmp);
1188 return PTR_ERR(paddc);
1189 t = xt_find_table_lock(net, AF_INET6, tmp.name);
1196 private = t->private;
1197 if (private->number != tmp.num_counters) {
1199 goto unlock_up_free;
1203 addend = xt_write_recseq_begin();
1204 xt_entry_foreach(iter, private->entries, private->size) {
1205 struct xt_counters *tmp;
1207 tmp = xt_get_this_cpu_counter(&iter->counters);
1208 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1211 xt_write_recseq_end(addend);
1222 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1223 struct compat_ip6t_replace {
1224 char name[XT_TABLE_MAXNAMELEN];
1228 u32 hook_entry[NF_INET_NUMHOOKS];
1229 u32 underflow[NF_INET_NUMHOOKS];
1231 compat_uptr_t counters; /* struct xt_counters * */
1232 struct compat_ip6t_entry entries[];
1236 compat_copy_entry_to_user(struct ip6t_entry *e, void __user **dstptr,
1237 unsigned int *size, struct xt_counters *counters,
1240 struct xt_entry_target *t;
1241 struct compat_ip6t_entry __user *ce;
1242 u_int16_t target_offset, next_offset;
1243 compat_uint_t origsize;
1244 const struct xt_entry_match *ematch;
1249 if (copy_to_user(ce, e, sizeof(struct ip6t_entry)) != 0 ||
1250 copy_to_user(&ce->counters, &counters[i],
1251 sizeof(counters[i])) != 0)
1254 *dstptr += sizeof(struct compat_ip6t_entry);
1255 *size -= sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1257 xt_ematch_foreach(ematch, e) {
1258 ret = xt_compat_match_to_user(ematch, dstptr, size);
1262 target_offset = e->target_offset - (origsize - *size);
1263 t = ip6t_get_target(e);
1264 ret = xt_compat_target_to_user(t, dstptr, size);
1267 next_offset = e->next_offset - (origsize - *size);
1268 if (put_user(target_offset, &ce->target_offset) != 0 ||
1269 put_user(next_offset, &ce->next_offset) != 0)
1275 compat_find_calc_match(struct xt_entry_match *m,
1276 const struct ip6t_ip6 *ipv6,
1279 struct xt_match *match;
1281 match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
1282 m->u.user.revision);
1284 return PTR_ERR(match);
1286 m->u.kernel.match = match;
1287 *size += xt_compat_match_offset(match);
1291 static void compat_release_entry(struct compat_ip6t_entry *e)
1293 struct xt_entry_target *t;
1294 struct xt_entry_match *ematch;
1296 /* Cleanup all matches */
1297 xt_ematch_foreach(ematch, e)
1298 module_put(ematch->u.kernel.match->me);
1299 t = compat_ip6t_get_target(e);
1300 module_put(t->u.kernel.target->me);
1304 check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
1305 struct xt_table_info *newinfo,
1307 const unsigned char *base,
1308 const unsigned char *limit)
1310 struct xt_entry_match *ematch;
1311 struct xt_entry_target *t;
1312 struct xt_target *target;
1313 unsigned int entry_offset;
1317 if ((unsigned long)e % __alignof__(struct compat_ip6t_entry) != 0 ||
1318 (unsigned char *)e + sizeof(struct compat_ip6t_entry) >= limit ||
1319 (unsigned char *)e + e->next_offset > limit)
1322 if (e->next_offset < sizeof(struct compat_ip6t_entry) +
1323 sizeof(struct compat_xt_entry_target))
1326 if (!ip6_checkentry(&e->ipv6))
1329 ret = xt_compat_check_entry_offsets(e, e->elems,
1330 e->target_offset, e->next_offset);
1334 off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1335 entry_offset = (void *)e - (void *)base;
1337 xt_ematch_foreach(ematch, e) {
1338 ret = compat_find_calc_match(ematch, &e->ipv6, &off);
1340 goto release_matches;
1344 t = compat_ip6t_get_target(e);
1345 target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name,
1346 t->u.user.revision);
1347 if (IS_ERR(target)) {
1348 ret = PTR_ERR(target);
1349 goto release_matches;
1351 t->u.kernel.target = target;
1353 off += xt_compat_target_offset(target);
1355 ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
1362 module_put(t->u.kernel.target->me);
1364 xt_ematch_foreach(ematch, e) {
1367 module_put(ematch->u.kernel.match->me);
1373 compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr,
1375 struct xt_table_info *newinfo, unsigned char *base)
1377 struct xt_entry_target *t;
1378 struct ip6t_entry *de;
1379 unsigned int origsize;
1381 struct xt_entry_match *ematch;
1385 memcpy(de, e, sizeof(struct ip6t_entry));
1386 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1388 *dstptr += sizeof(struct ip6t_entry);
1389 *size += sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1391 xt_ematch_foreach(ematch, e)
1392 xt_compat_match_from_user(ematch, dstptr, size);
1394 de->target_offset = e->target_offset - (origsize - *size);
1395 t = compat_ip6t_get_target(e);
1396 xt_compat_target_from_user(t, dstptr, size);
1398 de->next_offset = e->next_offset - (origsize - *size);
1399 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1400 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1401 newinfo->hook_entry[h] -= origsize - *size;
1402 if ((unsigned char *)de - base < newinfo->underflow[h])
1403 newinfo->underflow[h] -= origsize - *size;
1408 translate_compat_table(struct net *net,
1409 struct xt_table_info **pinfo,
1411 const struct compat_ip6t_replace *compatr)
1414 struct xt_table_info *newinfo, *info;
1415 void *pos, *entry0, *entry1;
1416 struct compat_ip6t_entry *iter0;
1417 struct ip6t_replace repl;
1423 size = compatr->size;
1424 info->number = compatr->num_entries;
1427 xt_compat_lock(AF_INET6);
1428 ret = xt_compat_init_offsets(AF_INET6, compatr->num_entries);
1431 /* Walk through entries, checking offsets. */
1432 xt_entry_foreach(iter0, entry0, compatr->size) {
1433 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1435 entry0 + compatr->size);
1442 if (j != compatr->num_entries)
1446 newinfo = xt_alloc_table_info(size);
1450 memset(newinfo->entries, 0, size);
1452 newinfo->number = compatr->num_entries;
1453 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1454 newinfo->hook_entry[i] = compatr->hook_entry[i];
1455 newinfo->underflow[i] = compatr->underflow[i];
1457 entry1 = newinfo->entries;
1459 size = compatr->size;
1460 xt_entry_foreach(iter0, entry0, compatr->size)
1461 compat_copy_entry_from_user(iter0, &pos, &size,
1464 /* all module references in entry0 are now gone. */
1465 xt_compat_flush_offsets(AF_INET6);
1466 xt_compat_unlock(AF_INET6);
1468 memcpy(&repl, compatr, sizeof(*compatr));
1470 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1471 repl.hook_entry[i] = newinfo->hook_entry[i];
1472 repl.underflow[i] = newinfo->underflow[i];
1475 repl.num_counters = 0;
1476 repl.counters = NULL;
1477 repl.size = newinfo->size;
1478 ret = translate_table(net, newinfo, entry1, &repl);
1484 xt_free_table_info(info);
1488 xt_free_table_info(newinfo);
1491 xt_compat_flush_offsets(AF_INET6);
1492 xt_compat_unlock(AF_INET6);
1493 xt_entry_foreach(iter0, entry0, compatr->size) {
1496 compat_release_entry(iter0);
1502 compat_do_replace(struct net *net, sockptr_t arg, unsigned int len)
1505 struct compat_ip6t_replace tmp;
1506 struct xt_table_info *newinfo;
1507 void *loc_cpu_entry;
1508 struct ip6t_entry *iter;
1510 if (len < sizeof(tmp))
1512 if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
1515 /* overflow check */
1516 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1518 if (tmp.num_counters == 0)
1520 if ((u64)len < (u64)tmp.size + sizeof(tmp))
1523 tmp.name[sizeof(tmp.name)-1] = 0;
1525 newinfo = xt_alloc_table_info(tmp.size);
1529 loc_cpu_entry = newinfo->entries;
1530 if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
1536 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
1540 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1541 tmp.num_counters, compat_ptr(tmp.counters));
1543 goto free_newinfo_untrans;
1546 free_newinfo_untrans:
1547 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1548 cleanup_entry(iter, net);
1550 xt_free_table_info(newinfo);
1554 struct compat_ip6t_get_entries {
1555 char name[XT_TABLE_MAXNAMELEN];
1557 struct compat_ip6t_entry entrytable[];
1561 compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1562 void __user *userptr)
1564 struct xt_counters *counters;
1565 const struct xt_table_info *private = table->private;
1570 struct ip6t_entry *iter;
1572 counters = alloc_counters(table);
1573 if (IS_ERR(counters))
1574 return PTR_ERR(counters);
1578 xt_entry_foreach(iter, private->entries, total_size) {
1579 ret = compat_copy_entry_to_user(iter, &pos,
1580 &size, counters, i++);
1590 compat_get_entries(struct net *net, struct compat_ip6t_get_entries __user *uptr,
1594 struct compat_ip6t_get_entries get;
1597 if (*len < sizeof(get))
1600 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1603 if (*len != sizeof(struct compat_ip6t_get_entries) + get.size)
1606 get.name[sizeof(get.name) - 1] = '\0';
1608 xt_compat_lock(AF_INET6);
1609 t = xt_find_table_lock(net, AF_INET6, get.name);
1611 const struct xt_table_info *private = t->private;
1612 struct xt_table_info info;
1613 ret = compat_table_info(private, &info);
1614 if (!ret && get.size == info.size)
1615 ret = compat_copy_entries_to_user(private->size,
1616 t, uptr->entrytable);
1620 xt_compat_flush_offsets(AF_INET6);
1626 xt_compat_unlock(AF_INET6);
1632 do_ip6t_set_ctl(struct sock *sk, int cmd, sockptr_t arg, unsigned int len)
1636 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1640 case IP6T_SO_SET_REPLACE:
1641 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1642 if (in_compat_syscall())
1643 ret = compat_do_replace(sock_net(sk), arg, len);
1646 ret = do_replace(sock_net(sk), arg, len);
1649 case IP6T_SO_SET_ADD_COUNTERS:
1650 ret = do_add_counters(sock_net(sk), arg, len);
1661 do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1665 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1669 case IP6T_SO_GET_INFO:
1670 ret = get_info(sock_net(sk), user, len);
1673 case IP6T_SO_GET_ENTRIES:
1674 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1675 if (in_compat_syscall())
1676 ret = compat_get_entries(sock_net(sk), user, len);
1679 ret = get_entries(sock_net(sk), user, len);
1682 case IP6T_SO_GET_REVISION_MATCH:
1683 case IP6T_SO_GET_REVISION_TARGET: {
1684 struct xt_get_revision rev;
1687 if (*len != sizeof(rev)) {
1691 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1695 rev.name[sizeof(rev.name)-1] = 0;
1697 if (cmd == IP6T_SO_GET_REVISION_TARGET)
1702 try_then_request_module(xt_find_revision(AF_INET6, rev.name,
1705 "ip6t_%s", rev.name);
1716 static void __ip6t_unregister_table(struct net *net, struct xt_table *table)
1718 struct xt_table_info *private;
1719 void *loc_cpu_entry;
1720 struct module *table_owner = table->me;
1721 struct ip6t_entry *iter;
1723 private = xt_unregister_table(table);
1725 /* Decrease module usage counts and free resources */
1726 loc_cpu_entry = private->entries;
1727 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1728 cleanup_entry(iter, net);
1729 if (private->number > private->initial_entries)
1730 module_put(table_owner);
1731 xt_free_table_info(private);
1734 int ip6t_register_table(struct net *net, const struct xt_table *table,
1735 const struct ip6t_replace *repl,
1736 const struct nf_hook_ops *template_ops)
1738 struct nf_hook_ops *ops;
1739 unsigned int num_ops;
1741 struct xt_table_info *newinfo;
1742 struct xt_table_info bootstrap = {0};
1743 void *loc_cpu_entry;
1744 struct xt_table *new_table;
1746 newinfo = xt_alloc_table_info(repl->size);
1750 loc_cpu_entry = newinfo->entries;
1751 memcpy(loc_cpu_entry, repl->entries, repl->size);
1753 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
1755 xt_free_table_info(newinfo);
1759 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1760 if (IS_ERR(new_table)) {
1761 struct ip6t_entry *iter;
1763 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1764 cleanup_entry(iter, net);
1765 xt_free_table_info(newinfo);
1766 return PTR_ERR(new_table);
1772 num_ops = hweight32(table->valid_hooks);
1778 ops = kmemdup(template_ops, sizeof(*ops) * num_ops, GFP_KERNEL);
1784 for (i = 0; i < num_ops; i++)
1785 ops[i].priv = new_table;
1787 new_table->ops = ops;
1789 ret = nf_register_net_hooks(net, ops, num_ops);
1796 __ip6t_unregister_table(net, new_table);
1800 void ip6t_unregister_table_pre_exit(struct net *net, const char *name)
1802 struct xt_table *table = xt_find_table(net, NFPROTO_IPV6, name);
1805 nf_unregister_net_hooks(net, table->ops, hweight32(table->valid_hooks));
1808 void ip6t_unregister_table_exit(struct net *net, const char *name)
1810 struct xt_table *table = xt_find_table(net, NFPROTO_IPV6, name);
1813 __ip6t_unregister_table(net, table);
1816 /* Returns 1 if the type and code is matched by the range, 0 otherwise */
1818 icmp6_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1819 u_int8_t type, u_int8_t code,
1822 return (type == test_type && code >= min_code && code <= max_code)
1827 icmp6_match(const struct sk_buff *skb, struct xt_action_param *par)
1829 const struct icmp6hdr *ic;
1830 struct icmp6hdr _icmph;
1831 const struct ip6t_icmp *icmpinfo = par->matchinfo;
1833 /* Must not be a fragment. */
1834 if (par->fragoff != 0)
1837 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
1839 /* We've been asked to examine this packet, and we
1840 * can't. Hence, no choice but to drop.
1842 par->hotdrop = true;
1846 return icmp6_type_code_match(icmpinfo->type,
1849 ic->icmp6_type, ic->icmp6_code,
1850 !!(icmpinfo->invflags&IP6T_ICMP_INV));
1853 /* Called when user tries to insert an entry of this type. */
1854 static int icmp6_checkentry(const struct xt_mtchk_param *par)
1856 const struct ip6t_icmp *icmpinfo = par->matchinfo;
1858 /* Must specify no unknown invflags */
1859 return (icmpinfo->invflags & ~IP6T_ICMP_INV) ? -EINVAL : 0;
1862 /* The built-in targets: standard (NULL) and error. */
1863 static struct xt_target ip6t_builtin_tg[] __read_mostly = {
1865 .name = XT_STANDARD_TARGET,
1866 .targetsize = sizeof(int),
1867 .family = NFPROTO_IPV6,
1868 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1869 .compatsize = sizeof(compat_int_t),
1870 .compat_from_user = compat_standard_from_user,
1871 .compat_to_user = compat_standard_to_user,
1875 .name = XT_ERROR_TARGET,
1876 .target = ip6t_error,
1877 .targetsize = XT_FUNCTION_MAXNAMELEN,
1878 .family = NFPROTO_IPV6,
1882 static struct nf_sockopt_ops ip6t_sockopts = {
1884 .set_optmin = IP6T_BASE_CTL,
1885 .set_optmax = IP6T_SO_SET_MAX+1,
1886 .set = do_ip6t_set_ctl,
1887 .get_optmin = IP6T_BASE_CTL,
1888 .get_optmax = IP6T_SO_GET_MAX+1,
1889 .get = do_ip6t_get_ctl,
1890 .owner = THIS_MODULE,
1893 static struct xt_match ip6t_builtin_mt[] __read_mostly = {
1896 .match = icmp6_match,
1897 .matchsize = sizeof(struct ip6t_icmp),
1898 .checkentry = icmp6_checkentry,
1899 .proto = IPPROTO_ICMPV6,
1900 .family = NFPROTO_IPV6,
1905 static int __net_init ip6_tables_net_init(struct net *net)
1907 return xt_proto_init(net, NFPROTO_IPV6);
1910 static void __net_exit ip6_tables_net_exit(struct net *net)
1912 xt_proto_fini(net, NFPROTO_IPV6);
1915 static struct pernet_operations ip6_tables_net_ops = {
1916 .init = ip6_tables_net_init,
1917 .exit = ip6_tables_net_exit,
1920 static int __init ip6_tables_init(void)
1924 ret = register_pernet_subsys(&ip6_tables_net_ops);
1928 /* No one else will be downing sem now, so we won't sleep */
1929 ret = xt_register_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
1932 ret = xt_register_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
1936 /* Register setsockopt */
1937 ret = nf_register_sockopt(&ip6t_sockopts);
1944 xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
1946 xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
1948 unregister_pernet_subsys(&ip6_tables_net_ops);
1953 static void __exit ip6_tables_fini(void)
1955 nf_unregister_sockopt(&ip6t_sockopts);
1957 xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
1958 xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
1959 unregister_pernet_subsys(&ip6_tables_net_ops);
1962 EXPORT_SYMBOL(ip6t_register_table);
1963 EXPORT_SYMBOL(ip6t_unregister_table_pre_exit);
1964 EXPORT_SYMBOL(ip6t_unregister_table_exit);
1965 EXPORT_SYMBOL(ip6t_do_table);
1967 module_init(ip6_tables_init);
1968 module_exit(ip6_tables_fini);