1 // SPDX-License-Identifier: GPL-2.0-only
3 * IPv6 raw table, a port of the IPv4 raw table to IPv6
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>
12 #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
14 static int __net_init ip6table_raw_table_init(struct net *net);
16 static bool raw_before_defrag __read_mostly;
17 MODULE_PARM_DESC(raw_before_defrag, "Enable raw table before defrag");
18 module_param(raw_before_defrag, bool, 0000);
20 static const struct xt_table packet_raw = {
22 .valid_hooks = RAW_VALID_HOOKS,
25 .priority = NF_IP6_PRI_RAW,
26 .table_init = ip6table_raw_table_init,
29 static const struct xt_table packet_raw_before_defrag = {
31 .valid_hooks = RAW_VALID_HOOKS,
34 .priority = NF_IP6_PRI_RAW_BEFORE_DEFRAG,
35 .table_init = ip6table_raw_table_init,
38 /* The work comes in here from netfilter.c. */
40 ip6table_raw_hook(void *priv, struct sk_buff *skb,
41 const struct nf_hook_state *state)
43 return ip6t_do_table(skb, state, state->net->ipv6.ip6table_raw);
46 static struct nf_hook_ops *rawtable_ops __read_mostly;
48 static int __net_init ip6table_raw_table_init(struct net *net)
50 struct ip6t_replace *repl;
51 const struct xt_table *table = &packet_raw;
54 if (raw_before_defrag)
55 table = &packet_raw_before_defrag;
57 if (net->ipv6.ip6table_raw)
60 repl = ip6t_alloc_initial_table(table);
63 ret = ip6t_register_table(net, table, repl, rawtable_ops,
64 &net->ipv6.ip6table_raw);
69 static void __net_exit ip6table_raw_net_pre_exit(struct net *net)
71 if (net->ipv6.ip6table_raw)
72 ip6t_unregister_table_pre_exit(net, net->ipv6.ip6table_raw,
76 static void __net_exit ip6table_raw_net_exit(struct net *net)
78 if (!net->ipv6.ip6table_raw)
80 ip6t_unregister_table_exit(net, net->ipv6.ip6table_raw);
81 net->ipv6.ip6table_raw = NULL;
84 static struct pernet_operations ip6table_raw_net_ops = {
85 .pre_exit = ip6table_raw_net_pre_exit,
86 .exit = ip6table_raw_net_exit,
89 static int __init ip6table_raw_init(void)
92 const struct xt_table *table = &packet_raw;
94 if (raw_before_defrag) {
95 table = &packet_raw_before_defrag;
97 pr_info("Enabling raw table before defrag\n");
101 rawtable_ops = xt_hook_ops_alloc(table, ip6table_raw_hook);
102 if (IS_ERR(rawtable_ops))
103 return PTR_ERR(rawtable_ops);
105 ret = register_pernet_subsys(&ip6table_raw_net_ops);
111 ret = ip6table_raw_table_init(&init_net);
113 unregister_pernet_subsys(&ip6table_raw_net_ops);
119 static void __exit ip6table_raw_fini(void)
121 unregister_pernet_subsys(&ip6table_raw_net_ops);
125 module_init(ip6table_raw_init);
126 module_exit(ip6table_raw_fini);
127 MODULE_LICENSE("GPL");