more debug output
[linux-2.4.git] / net / ipv6 / netfilter / ip6table_filter.c
1 /*
2  * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
3  *
4  * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
5  */
6 #include <linux/module.h>
7 #include <linux/netfilter_ipv6/ip6_tables.h>
8
9 #define FILTER_VALID_HOOKS ((1 << NF_IP6_LOCAL_IN) | (1 << NF_IP6_FORWARD) | (1 << NF_IP6_LOCAL_OUT))
10
11 /* Standard entry. */
12 struct ip6t_standard
13 {
14         struct ip6t_entry entry;
15         struct ip6t_standard_target target;
16 };
17
18 struct ip6t_error_target
19 {
20         struct ip6t_entry_target target;
21         char errorname[IP6T_FUNCTION_MAXNAMELEN];
22 };
23
24 struct ip6t_error
25 {
26         struct ip6t_entry entry;
27         struct ip6t_error_target target;
28 };
29
30 static struct
31 {
32         struct ip6t_replace repl;
33         struct ip6t_standard entries[3];
34         struct ip6t_error term;
35 } initial_table __initdata
36 = { { "filter", FILTER_VALID_HOOKS, 4,
37       sizeof(struct ip6t_standard) * 3 + sizeof(struct ip6t_error),
38       { [NF_IP6_LOCAL_IN] 0,
39         [NF_IP6_FORWARD] sizeof(struct ip6t_standard),
40         [NF_IP6_LOCAL_OUT] sizeof(struct ip6t_standard) * 2 },
41       { [NF_IP6_LOCAL_IN] 0,
42         [NF_IP6_FORWARD] sizeof(struct ip6t_standard),
43         [NF_IP6_LOCAL_OUT] sizeof(struct ip6t_standard) * 2 },
44       0, NULL, { } },
45     {
46             /* LOCAL_IN */
47             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
48                 0,
49                 sizeof(struct ip6t_entry),
50                 sizeof(struct ip6t_standard),
51                 0, { 0, 0 }, { } },
52               { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
53                 -NF_ACCEPT - 1 } },
54             /* FORWARD */
55             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
56                 0,
57                 sizeof(struct ip6t_entry),
58                 sizeof(struct ip6t_standard),
59                 0, { 0, 0 }, { } },
60               { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
61                 -NF_ACCEPT - 1 } },
62             /* LOCAL_OUT */
63             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
64                 0,
65                 sizeof(struct ip6t_entry),
66                 sizeof(struct ip6t_standard),
67                 0, { 0, 0 }, { } },
68               { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
69                 -NF_ACCEPT - 1 } }
70     },
71     /* ERROR */
72     { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
73         0,
74         sizeof(struct ip6t_entry),
75         sizeof(struct ip6t_error),
76         0, { 0, 0 }, { } },
77       { { { { IP6T_ALIGN(sizeof(struct ip6t_error_target)), IP6T_ERROR_TARGET } },
78           { } },
79         "ERROR"
80       }
81     }
82 };
83
84 static struct ip6t_table packet_filter
85 = { { NULL, NULL }, "filter", &initial_table.repl,
86     FILTER_VALID_HOOKS, RW_LOCK_UNLOCKED, NULL, THIS_MODULE };
87
88 /* The work comes in here from netfilter.c. */
89 static unsigned int
90 ip6t_hook(unsigned int hook,
91          struct sk_buff **pskb,
92          const struct net_device *in,
93          const struct net_device *out,
94          int (*okfn)(struct sk_buff *))
95 {
96         return ip6t_do_table(pskb, hook, in, out, &packet_filter, NULL);
97 }
98
99 static unsigned int
100 ip6t_local_out_hook(unsigned int hook,
101                    struct sk_buff **pskb,
102                    const struct net_device *in,
103                    const struct net_device *out,
104                    int (*okfn)(struct sk_buff *))
105 {
106 #if 0
107         /* root is playing with raw sockets. */
108         if ((*pskb)->len < sizeof(struct iphdr)
109             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
110                 if (net_ratelimit())
111                         printk("ip6t_hook: happy cracking.\n");
112                 return NF_ACCEPT;
113         }
114 #endif
115
116         return ip6t_do_table(pskb, hook, in, out, &packet_filter, NULL);
117 }
118
119 static struct nf_hook_ops ip6t_ops[]
120 = { { { NULL, NULL }, ip6t_hook, PF_INET6, NF_IP6_LOCAL_IN, NF_IP6_PRI_FILTER },
121     { { NULL, NULL }, ip6t_hook, PF_INET6, NF_IP6_FORWARD, NF_IP6_PRI_FILTER },
122     { { NULL, NULL }, ip6t_local_out_hook, PF_INET6, NF_IP6_LOCAL_OUT,
123                 NF_IP6_PRI_FILTER }
124 };
125
126 /* Default to forward because I got too much mail already. */
127 static int forward = NF_ACCEPT;
128 MODULE_PARM(forward, "i");
129
130 static int __init init(void)
131 {
132         int ret;
133
134         if (forward < 0 || forward > NF_MAX_VERDICT) {
135                 printk("iptables forward must be 0 or 1\n");
136                 return -EINVAL;
137         }
138
139         /* Entry 1 is the FORWARD hook */
140         initial_table.entries[1].target.verdict = -forward - 1;
141
142         /* Register table */
143         ret = ip6t_register_table(&packet_filter);
144         if (ret < 0)
145                 return ret;
146
147         /* Register hooks */
148         ret = nf_register_hook(&ip6t_ops[0]);
149         if (ret < 0)
150                 goto cleanup_table;
151
152         ret = nf_register_hook(&ip6t_ops[1]);
153         if (ret < 0)
154                 goto cleanup_hook0;
155
156         ret = nf_register_hook(&ip6t_ops[2]);
157         if (ret < 0)
158                 goto cleanup_hook1;
159
160         return ret;
161
162  cleanup_hook1:
163         nf_unregister_hook(&ip6t_ops[1]);
164  cleanup_hook0:
165         nf_unregister_hook(&ip6t_ops[0]);
166  cleanup_table:
167         ip6t_unregister_table(&packet_filter);
168
169         return ret;
170 }
171
172 static void __exit fini(void)
173 {
174         unsigned int i;
175
176         for (i = 0; i < sizeof(ip6t_ops)/sizeof(struct nf_hook_ops); i++)
177                 nf_unregister_hook(&ip6t_ops[i]);
178
179         ip6t_unregister_table(&packet_filter);
180 }
181
182 module_init(init);
183 module_exit(fini);
184 MODULE_LICENSE("GPL");