1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * IPv6 raw table, a port of the IPv4 raw table to IPv6
4 *
5 * Copyright (C) 2003 Jozsef Kadlecsik <kadlec@netfilter.org>
6 */
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/module.h>
9 #include <linux/netfilter_ipv6/ip6_tables.h>
10 #include <linux/slab.h>
11
12 #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
13
14 static bool raw_before_defrag __read_mostly;
15 MODULE_PARM_DESC(raw_before_defrag, "Enable raw table before defrag");
16 module_param(raw_before_defrag, bool, 0000);
17
18 static const struct xt_table packet_raw = {
19 .name = "raw",
20 .valid_hooks = RAW_VALID_HOOKS,
21 .me = THIS_MODULE,
22 .af = NFPROTO_IPV6,
23 .priority = NF_IP6_PRI_RAW,
24 };
25
26 static const struct xt_table packet_raw_before_defrag = {
27 .name = "raw",
28 .valid_hooks = RAW_VALID_HOOKS,
29 .me = THIS_MODULE,
30 .af = NFPROTO_IPV6,
31 .priority = NF_IP6_PRI_RAW_BEFORE_DEFRAG,
32 };
33
34 /* The work comes in here from netfilter.c. */
35 static unsigned int
ip6table_raw_hook(void * priv,struct sk_buff * skb,const struct nf_hook_state * state)36 ip6table_raw_hook(void *priv, struct sk_buff *skb,
37 const struct nf_hook_state *state)
38 {
39 return ip6t_do_table(skb, state, priv);
40 }
41
42 static struct nf_hook_ops *rawtable_ops __read_mostly;
43
ip6table_raw_table_init(struct net * net)44 static int ip6table_raw_table_init(struct net *net)
45 {
46 struct ip6t_replace *repl;
47 const struct xt_table *table = &packet_raw;
48 int ret;
49
50 if (raw_before_defrag)
51 table = &packet_raw_before_defrag;
52
53 repl = ip6t_alloc_initial_table(table);
54 if (repl == NULL)
55 return -ENOMEM;
56 ret = ip6t_register_table(net, table, repl, rawtable_ops);
57 kfree(repl);
58 return ret;
59 }
60
ip6table_raw_net_pre_exit(struct net * net)61 static void __net_exit ip6table_raw_net_pre_exit(struct net *net)
62 {
63 ip6t_unregister_table_pre_exit(net, "raw");
64 }
65
ip6table_raw_net_exit(struct net * net)66 static void __net_exit ip6table_raw_net_exit(struct net *net)
67 {
68 ip6t_unregister_table_exit(net, "raw");
69 }
70
71 static struct pernet_operations ip6table_raw_net_ops = {
72 .pre_exit = ip6table_raw_net_pre_exit,
73 .exit = ip6table_raw_net_exit,
74 };
75
ip6table_raw_init(void)76 static int __init ip6table_raw_init(void)
77 {
78 const struct xt_table *table = &packet_raw;
79 int ret;
80
81 if (raw_before_defrag) {
82 table = &packet_raw_before_defrag;
83 pr_info("Enabling raw table before defrag\n");
84 }
85
86 ret = xt_register_template(table, ip6table_raw_table_init);
87 if (ret < 0)
88 return ret;
89
90 /* Register hooks */
91 rawtable_ops = xt_hook_ops_alloc(table, ip6table_raw_hook);
92 if (IS_ERR(rawtable_ops)) {
93 xt_unregister_template(table);
94 return PTR_ERR(rawtable_ops);
95 }
96
97 ret = register_pernet_subsys(&ip6table_raw_net_ops);
98 if (ret < 0) {
99 kfree(rawtable_ops);
100 xt_unregister_template(table);
101 return ret;
102 }
103
104 return ret;
105 }
106
ip6table_raw_fini(void)107 static void __exit ip6table_raw_fini(void)
108 {
109 unregister_pernet_subsys(&ip6table_raw_net_ops);
110 xt_unregister_template(&packet_raw);
111 kfree(rawtable_ops);
112 }
113
114 module_init(ip6table_raw_init);
115 module_exit(ip6table_raw_fini);
116 MODULE_LICENSE("GPL");
117