[NETFILTER]: x_tables: return new table from {arp,ip,ip6}t_register_table()
[linux-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 struct
13 {
14         struct ipt_replace repl;
15         struct ipt_standard entries[2];
16         struct ipt_error term;
17 } initial_table __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 struct xt_table __packet_raw = {
40         .name = "raw",
41         .valid_hooks =  RAW_VALID_HOOKS,
42         .lock = RW_LOCK_UNLOCKED,
43         .me = THIS_MODULE,
44         .af = AF_INET,
45 };
46 static struct xt_table *packet_raw;
47
48 /* The work comes in here from netfilter.c. */
49 static unsigned int
50 ipt_hook(unsigned int hook,
51          struct sk_buff *skb,
52          const struct net_device *in,
53          const struct net_device *out,
54          int (*okfn)(struct sk_buff *))
55 {
56         return ipt_do_table(skb, hook, in, out, packet_raw);
57 }
58
59 static unsigned int
60 ipt_local_hook(unsigned int hook,
61                struct sk_buff *skb,
62                const struct net_device *in,
63                const struct net_device *out,
64                int (*okfn)(struct sk_buff *))
65 {
66         /* root is playing with raw sockets. */
67         if (skb->len < sizeof(struct iphdr) ||
68             ip_hdrlen(skb) < sizeof(struct iphdr)) {
69                 if (net_ratelimit())
70                         printk("iptable_raw: ignoring short SOCK_RAW "
71                                "packet.\n");
72                 return NF_ACCEPT;
73         }
74         return ipt_do_table(skb, hook, in, out, packet_raw);
75 }
76
77 /* 'raw' is the very first table. */
78 static struct nf_hook_ops ipt_ops[] __read_mostly = {
79         {
80                 .hook = ipt_hook,
81                 .pf = PF_INET,
82                 .hooknum = NF_INET_PRE_ROUTING,
83                 .priority = NF_IP_PRI_RAW,
84                 .owner = THIS_MODULE,
85         },
86         {
87                 .hook = ipt_local_hook,
88                 .pf = PF_INET,
89                 .hooknum = NF_INET_LOCAL_OUT,
90                 .priority = NF_IP_PRI_RAW,
91                 .owner = THIS_MODULE,
92         },
93 };
94
95 static int __init iptable_raw_init(void)
96 {
97         int ret;
98
99         /* Register table */
100         packet_raw = ipt_register_table(&init_net, &__packet_raw,
101                                         &initial_table.repl);
102         if (IS_ERR(packet_raw))
103                 return PTR_ERR(packet_raw);
104
105         /* Register hooks */
106         ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
107         if (ret < 0)
108                 goto cleanup_table;
109
110         return ret;
111
112  cleanup_table:
113         ipt_unregister_table(packet_raw);
114         return ret;
115 }
116
117 static void __exit iptable_raw_fini(void)
118 {
119         nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
120         ipt_unregister_table(packet_raw);
121 }
122
123 module_init(iptable_raw_init);
124 module_exit(iptable_raw_fini);
125 MODULE_LICENSE("GPL");