1 // SPDX-License-Identifier: GPL-2.0-only
3 * 'raw' table, which is the very first hooked in at PRE_ROUTING and LOCAL_OUT .
5 * Copyright (C) 2003 Jozsef Kadlecsik <kadlec@netfilter.org>
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/module.h>
9 #include <linux/netfilter_ipv4/ip_tables.h>
10 #include <linux/slab.h>
13 #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
15 static bool raw_before_defrag __read_mostly;
16 MODULE_PARM_DESC(raw_before_defrag, "Enable raw table before defrag");
17 module_param(raw_before_defrag, bool, 0000);
19 static const struct xt_table packet_raw = {
21 .valid_hooks = RAW_VALID_HOOKS,
24 .priority = NF_IP_PRI_RAW,
27 static const struct xt_table packet_raw_before_defrag = {
29 .valid_hooks = RAW_VALID_HOOKS,
32 .priority = NF_IP_PRI_RAW_BEFORE_DEFRAG,
35 /* The work comes in here from netfilter.c. */
37 iptable_raw_hook(void *priv, struct sk_buff *skb,
38 const struct nf_hook_state *state)
40 return ipt_do_table(skb, state, priv);
43 static struct nf_hook_ops *rawtable_ops __read_mostly;
45 static int __net_init iptable_raw_table_init(struct net *net)
47 struct ipt_replace *repl;
48 const struct xt_table *table = &packet_raw;
51 if (raw_before_defrag)
52 table = &packet_raw_before_defrag;
54 repl = ipt_alloc_initial_table(table);
57 ret = ipt_register_table(net, table, repl, rawtable_ops);
62 static void __net_exit iptable_raw_net_pre_exit(struct net *net)
64 ipt_unregister_table_pre_exit(net, "raw");
67 static void __net_exit iptable_raw_net_exit(struct net *net)
69 ipt_unregister_table_exit(net, "raw");
72 static struct pernet_operations iptable_raw_net_ops = {
73 .pre_exit = iptable_raw_net_pre_exit,
74 .exit = iptable_raw_net_exit,
77 static int __init iptable_raw_init(void)
80 const struct xt_table *table = &packet_raw;
82 if (raw_before_defrag) {
83 table = &packet_raw_before_defrag;
85 pr_info("Enabling raw table before defrag\n");
88 ret = xt_register_template(table,
89 iptable_raw_table_init);
93 rawtable_ops = xt_hook_ops_alloc(table, iptable_raw_hook);
94 if (IS_ERR(rawtable_ops)) {
95 xt_unregister_template(table);
96 return PTR_ERR(rawtable_ops);
99 ret = register_pernet_subsys(&iptable_raw_net_ops);
101 xt_unregister_template(table);
109 static void __exit iptable_raw_fini(void)
111 unregister_pernet_subsys(&iptable_raw_net_ops);
113 xt_unregister_template(&packet_raw);
116 module_init(iptable_raw_init);
117 module_exit(iptable_raw_fini);
118 MODULE_LICENSE("GPL");