netfilter: ip6tables: unregister the tables by name
[linux-block.git] / net / ipv6 / netfilter / ip6table_mangle.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * IPv6 packet mangling table, a port of the IPv4 mangle table to IPv6
4  *
5  * Copyright (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
6  * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
7  */
8 #include <linux/module.h>
9 #include <linux/netfilter_ipv6/ip6_tables.h>
10 #include <linux/slab.h>
11 #include <net/ipv6.h>
12
13 MODULE_LICENSE("GPL");
14 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
15 MODULE_DESCRIPTION("ip6tables mangle table");
16
17 #define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
18                             (1 << NF_INET_LOCAL_IN) | \
19                             (1 << NF_INET_FORWARD) | \
20                             (1 << NF_INET_LOCAL_OUT) | \
21                             (1 << NF_INET_POST_ROUTING))
22
23 static int __net_init ip6table_mangle_table_init(struct net *net);
24
25 static const struct xt_table packet_mangler = {
26         .name           = "mangle",
27         .valid_hooks    = MANGLE_VALID_HOOKS,
28         .me             = THIS_MODULE,
29         .af             = NFPROTO_IPV6,
30         .priority       = NF_IP6_PRI_MANGLE,
31         .table_init     = ip6table_mangle_table_init,
32 };
33
34 static unsigned int
35 ip6t_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
36 {
37         unsigned int ret;
38         struct in6_addr saddr, daddr;
39         u_int8_t hop_limit;
40         u_int32_t flowlabel, mark;
41         int err;
42
43         /* save source/dest address, mark, hoplimit, flowlabel, priority,  */
44         memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
45         memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
46         mark = skb->mark;
47         hop_limit = ipv6_hdr(skb)->hop_limit;
48
49         /* flowlabel and prio (includes version, which shouldn't change either */
50         flowlabel = *((u_int32_t *)ipv6_hdr(skb));
51
52         ret = ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle);
53
54         if (ret != NF_DROP && ret != NF_STOLEN &&
55             (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) ||
56              !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) ||
57              skb->mark != mark ||
58              ipv6_hdr(skb)->hop_limit != hop_limit ||
59              flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
60                 err = ip6_route_me_harder(state->net, state->sk, skb);
61                 if (err < 0)
62                         ret = NF_DROP_ERR(err);
63         }
64
65         return ret;
66 }
67
68 /* The work comes in here from netfilter.c. */
69 static unsigned int
70 ip6table_mangle_hook(void *priv, struct sk_buff *skb,
71                      const struct nf_hook_state *state)
72 {
73         if (state->hook == NF_INET_LOCAL_OUT)
74                 return ip6t_mangle_out(skb, state);
75         return ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle);
76 }
77
78 static struct nf_hook_ops *mangle_ops __read_mostly;
79 static int __net_init ip6table_mangle_table_init(struct net *net)
80 {
81         struct ip6t_replace *repl;
82         int ret;
83
84         if (net->ipv6.ip6table_mangle)
85                 return 0;
86
87         repl = ip6t_alloc_initial_table(&packet_mangler);
88         if (repl == NULL)
89                 return -ENOMEM;
90         ret = ip6t_register_table(net, &packet_mangler, repl, mangle_ops,
91                                   &net->ipv6.ip6table_mangle);
92         kfree(repl);
93         return ret;
94 }
95
96 static void __net_exit ip6table_mangle_net_pre_exit(struct net *net)
97 {
98         ip6t_unregister_table_pre_exit(net, "mangle", mangle_ops);
99 }
100
101 static void __net_exit ip6table_mangle_net_exit(struct net *net)
102 {
103         ip6t_unregister_table_exit(net, "mangle");
104         net->ipv6.ip6table_mangle = NULL;
105 }
106
107 static struct pernet_operations ip6table_mangle_net_ops = {
108         .pre_exit = ip6table_mangle_net_pre_exit,
109         .exit = ip6table_mangle_net_exit,
110 };
111
112 static int __init ip6table_mangle_init(void)
113 {
114         int ret;
115
116         mangle_ops = xt_hook_ops_alloc(&packet_mangler, ip6table_mangle_hook);
117         if (IS_ERR(mangle_ops))
118                 return PTR_ERR(mangle_ops);
119
120         ret = register_pernet_subsys(&ip6table_mangle_net_ops);
121         if (ret < 0) {
122                 kfree(mangle_ops);
123                 return ret;
124         }
125
126         ret = ip6table_mangle_table_init(&init_net);
127         if (ret) {
128                 unregister_pernet_subsys(&ip6table_mangle_net_ops);
129                 kfree(mangle_ops);
130         }
131         return ret;
132 }
133
134 static void __exit ip6table_mangle_fini(void)
135 {
136         unregister_pernet_subsys(&ip6table_mangle_net_ops);
137         kfree(mangle_ops);
138 }
139
140 module_init(ip6table_mangle_init);
141 module_exit(ip6table_mangle_fini);