1
2
3
4
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7#include <linux/module.h>
8#include <linux/netfilter_ipv4/ip_tables.h>
9#include <linux/slab.h>
10#include <net/ip.h>
11
12#define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
13
14static int __net_init iptable_raw_table_init(struct net *net);
15
16static bool raw_before_defrag __read_mostly;
17MODULE_PARM_DESC(raw_before_defrag, "Enable raw table before defrag");
18module_param(raw_before_defrag, bool, 0000);
19
20static const struct xt_table packet_raw = {
21 .name = "raw",
22 .valid_hooks = RAW_VALID_HOOKS,
23 .me = THIS_MODULE,
24 .af = NFPROTO_IPV4,
25 .priority = NF_IP_PRI_RAW,
26 .table_init = iptable_raw_table_init,
27};
28
29static const struct xt_table packet_raw_before_defrag = {
30 .name = "raw",
31 .valid_hooks = RAW_VALID_HOOKS,
32 .me = THIS_MODULE,
33 .af = NFPROTO_IPV4,
34 .priority = NF_IP_PRI_RAW_BEFORE_DEFRAG,
35 .table_init = iptable_raw_table_init,
36};
37
38
39static unsigned int
40iptable_raw_hook(void *priv, struct sk_buff *skb,
41 const struct nf_hook_state *state)
42{
43 return ipt_do_table(skb, state, state->net->ipv4.iptable_raw);
44}
45
46static struct nf_hook_ops *rawtable_ops __read_mostly;
47
48static int __net_init iptable_raw_table_init(struct net *net)
49{
50 struct ipt_replace *repl;
51 const struct xt_table *table = &packet_raw;
52 int ret;
53
54 if (raw_before_defrag)
55 table = &packet_raw_before_defrag;
56
57 if (net->ipv4.iptable_raw)
58 return 0;
59
60 repl = ipt_alloc_initial_table(table);
61 if (repl == NULL)
62 return -ENOMEM;
63 ret = ipt_register_table(net, table, repl, rawtable_ops,
64 &net->ipv4.iptable_raw);
65 kfree(repl);
66 return ret;
67}
68
69static void __net_exit iptable_raw_net_pre_exit(struct net *net)
70{
71 if (net->ipv4.iptable_raw)
72 ipt_unregister_table_pre_exit(net, net->ipv4.iptable_raw,
73 rawtable_ops);
74}
75
76static void __net_exit iptable_raw_net_exit(struct net *net)
77{
78 if (!net->ipv4.iptable_raw)
79 return;
80 ipt_unregister_table_exit(net, net->ipv4.iptable_raw);
81 net->ipv4.iptable_raw = NULL;
82}
83
84static struct pernet_operations iptable_raw_net_ops = {
85 .pre_exit = iptable_raw_net_pre_exit,
86 .exit = iptable_raw_net_exit,
87};
88
89static int __init iptable_raw_init(void)
90{
91 int ret;
92 const struct xt_table *table = &packet_raw;
93
94 if (raw_before_defrag) {
95 table = &packet_raw_before_defrag;
96
97 pr_info("Enabling raw table before defrag\n");
98 }
99
100 rawtable_ops = xt_hook_ops_alloc(table, iptable_raw_hook);
101 if (IS_ERR(rawtable_ops))
102 return PTR_ERR(rawtable_ops);
103
104 ret = register_pernet_subsys(&iptable_raw_net_ops);
105 if (ret < 0) {
106 kfree(rawtable_ops);
107 return ret;
108 }
109
110 ret = iptable_raw_table_init(&init_net);
111 if (ret) {
112 unregister_pernet_subsys(&iptable_raw_net_ops);
113 kfree(rawtable_ops);
114 }
115
116 return ret;
117}
118
119static void __exit iptable_raw_fini(void)
120{
121 unregister_pernet_subsys(&iptable_raw_net_ops);
122 kfree(rawtable_ops);
123}
124
125module_init(iptable_raw_init);
126module_exit(iptable_raw_fini);
127MODULE_LICENSE("GPL");
128