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 static struct nf_hook_ops *rawtable_ops __read_mostly;
37 static int iptable_raw_table_init(struct net *net)
39 struct ipt_replace *repl;
40 const struct xt_table *table = &packet_raw;
43 if (raw_before_defrag)
44 table = &packet_raw_before_defrag;
46 repl = ipt_alloc_initial_table(table);
49 ret = ipt_register_table(net, table, repl, rawtable_ops);
54 static void __net_exit iptable_raw_net_pre_exit(struct net *net)
56 ipt_unregister_table_pre_exit(net, "raw");
59 static void __net_exit iptable_raw_net_exit(struct net *net)
61 ipt_unregister_table_exit(net, "raw");
64 static struct pernet_operations iptable_raw_net_ops = {
65 .pre_exit = iptable_raw_net_pre_exit,
66 .exit = iptable_raw_net_exit,
69 static int __init iptable_raw_init(void)
72 const struct xt_table *table = &packet_raw;
74 if (raw_before_defrag) {
75 table = &packet_raw_before_defrag;
77 pr_info("Enabling raw table before defrag\n");
80 ret = xt_register_template(table,
81 iptable_raw_table_init);
85 rawtable_ops = xt_hook_ops_alloc(table, ipt_do_table);
86 if (IS_ERR(rawtable_ops)) {
87 xt_unregister_template(table);
88 return PTR_ERR(rawtable_ops);
91 ret = register_pernet_subsys(&iptable_raw_net_ops);
93 xt_unregister_template(table);
101 static void __exit iptable_raw_fini(void)
103 unregister_pernet_subsys(&iptable_raw_net_ops);
105 xt_unregister_template(&packet_raw);
108 module_init(iptable_raw_init);
109 module_exit(iptable_raw_fini);
110 MODULE_LICENSE("GPL");