]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv4/netfilter/iptable_raw.c
netfilter: xtables: compact table hook functions (1/2)
[net-next-2.6.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 <net/ip.h>
9
10 #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
11
12 static const struct
13 {
14         struct ipt_replace repl;
15         struct ipt_standard entries[2];
16         struct ipt_error term;
17 } initial_table __net_initdata = {
18         .repl = {
19                 .name = "raw",
20                 .valid_hooks = RAW_VALID_HOOKS,
21                 .num_entries = 3,
22                 .size = sizeof(struct ipt_standard) * 2 + sizeof(struct ipt_error),
23                 .hook_entry = {
24                         [NF_INET_PRE_ROUTING] = 0,
25                         [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard)
26                 },
27                 .underflow = {
28                         [NF_INET_PRE_ROUTING] = 0,
29                         [NF_INET_LOCAL_OUT]  = sizeof(struct ipt_standard)
30                 },
31         },
32         .entries = {
33                 IPT_STANDARD_INIT(NF_ACCEPT),   /* PRE_ROUTING */
34                 IPT_STANDARD_INIT(NF_ACCEPT),   /* LOCAL_OUT */
35         },
36         .term = IPT_ERROR_INIT,                 /* ERROR */
37 };
38
39 static const struct xt_table packet_raw = {
40         .name = "raw",
41         .valid_hooks =  RAW_VALID_HOOKS,
42         .me = THIS_MODULE,
43         .af = NFPROTO_IPV4,
44 };
45
46 /* The work comes in here from netfilter.c. */
47 static unsigned int
48 iptable_raw_hook(unsigned int hook, struct sk_buff *skb,
49                  const struct net_device *in, const struct net_device *out,
50                  int (*okfn)(struct sk_buff *))
51 {
52         if (hook == NF_INET_PRE_ROUTING)
53                 return ipt_do_table(skb, hook, in, out,
54                                     dev_net(in)->ipv4.iptable_raw);
55
56         /* OUTPUT: */
57         /* root is playing with raw sockets. */
58         if (skb->len < sizeof(struct iphdr) ||
59             ip_hdrlen(skb) < sizeof(struct iphdr))
60                 return NF_ACCEPT;
61         return ipt_do_table(skb, hook, in, out,
62                             dev_net(out)->ipv4.iptable_raw);
63 }
64
65 /* 'raw' is the very first table. */
66 static struct nf_hook_ops ipt_ops[] __read_mostly = {
67         {
68                 .hook = iptable_raw_hook,
69                 .pf = NFPROTO_IPV4,
70                 .hooknum = NF_INET_PRE_ROUTING,
71                 .priority = NF_IP_PRI_RAW,
72                 .owner = THIS_MODULE,
73         },
74         {
75                 .hook = iptable_raw_hook,
76                 .pf = NFPROTO_IPV4,
77                 .hooknum = NF_INET_LOCAL_OUT,
78                 .priority = NF_IP_PRI_RAW,
79                 .owner = THIS_MODULE,
80         },
81 };
82
83 static int __net_init iptable_raw_net_init(struct net *net)
84 {
85         /* Register table */
86         net->ipv4.iptable_raw =
87                 ipt_register_table(net, &packet_raw, &initial_table.repl);
88         if (IS_ERR(net->ipv4.iptable_raw))
89                 return PTR_ERR(net->ipv4.iptable_raw);
90         return 0;
91 }
92
93 static void __net_exit iptable_raw_net_exit(struct net *net)
94 {
95         ipt_unregister_table(net, net->ipv4.iptable_raw);
96 }
97
98 static struct pernet_operations iptable_raw_net_ops = {
99         .init = iptable_raw_net_init,
100         .exit = iptable_raw_net_exit,
101 };
102
103 static int __init iptable_raw_init(void)
104 {
105         int ret;
106
107         ret = register_pernet_subsys(&iptable_raw_net_ops);
108         if (ret < 0)
109                 return ret;
110
111         /* Register hooks */
112         ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
113         if (ret < 0)
114                 goto cleanup_table;
115
116         return ret;
117
118  cleanup_table:
119         unregister_pernet_subsys(&iptable_raw_net_ops);
120         return ret;
121 }
122
123 static void __exit iptable_raw_fini(void)
124 {
125         nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
126         unregister_pernet_subsys(&iptable_raw_net_ops);
127 }
128
129 module_init(iptable_raw_init);
130 module_exit(iptable_raw_fini);
131 MODULE_LICENSE("GPL");