Linux-libre 4.7-rc7-gnu
[librecmc/linux-libre.git] / net / ipv4 / netfilter / iptable_raw.c
1 /*
2  * 'raw' table, which is the very first hooked in at PRE_ROUTING and LOCAL_OUT .
3  *
4  * Copyright (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5  */
6 #include <linux/module.h>
7 #include <linux/netfilter_ipv4/ip_tables.h>
8 #include <linux/slab.h>
9 #include <net/ip.h>
10
11 #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
12
13 static int __net_init iptable_raw_table_init(struct net *net);
14
15 static const struct xt_table packet_raw = {
16         .name = "raw",
17         .valid_hooks =  RAW_VALID_HOOKS,
18         .me = THIS_MODULE,
19         .af = NFPROTO_IPV4,
20         .priority = NF_IP_PRI_RAW,
21         .table_init = iptable_raw_table_init,
22 };
23
24 /* The work comes in here from netfilter.c. */
25 static unsigned int
26 iptable_raw_hook(void *priv, struct sk_buff *skb,
27                  const struct nf_hook_state *state)
28 {
29         if (state->hook == NF_INET_LOCAL_OUT &&
30             (skb->len < sizeof(struct iphdr) ||
31              ip_hdrlen(skb) < sizeof(struct iphdr)))
32                 /* root is playing with raw sockets. */
33                 return NF_ACCEPT;
34
35         return ipt_do_table(skb, state, state->net->ipv4.iptable_raw);
36 }
37
38 static struct nf_hook_ops *rawtable_ops __read_mostly;
39
40 static int __net_init iptable_raw_table_init(struct net *net)
41 {
42         struct ipt_replace *repl;
43         int ret;
44
45         if (net->ipv4.iptable_raw)
46                 return 0;
47
48         repl = ipt_alloc_initial_table(&packet_raw);
49         if (repl == NULL)
50                 return -ENOMEM;
51         ret = ipt_register_table(net, &packet_raw, repl, rawtable_ops,
52                                  &net->ipv4.iptable_raw);
53         kfree(repl);
54         return ret;
55 }
56
57 static void __net_exit iptable_raw_net_exit(struct net *net)
58 {
59         if (!net->ipv4.iptable_raw)
60                 return;
61         ipt_unregister_table(net, net->ipv4.iptable_raw, rawtable_ops);
62         net->ipv4.iptable_raw = NULL;
63 }
64
65 static struct pernet_operations iptable_raw_net_ops = {
66         .exit = iptable_raw_net_exit,
67 };
68
69 static int __init iptable_raw_init(void)
70 {
71         int ret;
72
73         rawtable_ops = xt_hook_ops_alloc(&packet_raw, iptable_raw_hook);
74         if (IS_ERR(rawtable_ops))
75                 return PTR_ERR(rawtable_ops);
76
77         ret = register_pernet_subsys(&iptable_raw_net_ops);
78         if (ret < 0) {
79                 kfree(rawtable_ops);
80                 return ret;
81         }
82
83         ret = iptable_raw_table_init(&init_net);
84         if (ret) {
85                 unregister_pernet_subsys(&iptable_raw_net_ops);
86                 kfree(rawtable_ops);
87         }
88
89         return ret;
90 }
91
92 static void __exit iptable_raw_fini(void)
93 {
94         unregister_pernet_subsys(&iptable_raw_net_ops);
95         kfree(rawtable_ops);
96 }
97
98 module_init(iptable_raw_init);
99 module_exit(iptable_raw_fini);
100 MODULE_LICENSE("GPL");