blob: 77ca556aad91efb9949417e50ed9af4b06d01a0b [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 */
8
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08009#include <linux/types.h>
10#include <linux/ip.h>
11#include <linux/netfilter.h>
12#include <linux/module.h>
13#include <linux/skbuff.h>
14#include <linux/icmp.h>
15#include <linux/sysctl.h>
Patrick McHardy0ae2cfe2006-01-05 12:21:52 -080016#include <net/route.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080017#include <net/ip.h>
18
19#include <linux/netfilter_ipv4.h>
20#include <net/netfilter/nf_conntrack.h>
21#include <net/netfilter/nf_conntrack_helper.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010022#include <net/netfilter/nf_conntrack_l4proto.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080023#include <net/netfilter/nf_conntrack_l3proto.h>
24#include <net/netfilter/nf_conntrack_core.h>
25#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
26
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080027static int ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
28 struct nf_conntrack_tuple *tuple)
29{
Patrick McHardybff9a892006-12-02 22:05:08 -080030 __be32 _addrs[2], *ap;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080031 ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
32 sizeof(u_int32_t) * 2, _addrs);
33 if (ap == NULL)
34 return 0;
35
36 tuple->src.u3.ip = ap[0];
37 tuple->dst.u3.ip = ap[1];
38
39 return 1;
40}
41
42static int ipv4_invert_tuple(struct nf_conntrack_tuple *tuple,
43 const struct nf_conntrack_tuple *orig)
44{
45 tuple->src.u3.ip = orig->dst.u3.ip;
46 tuple->dst.u3.ip = orig->src.u3.ip;
47
48 return 1;
49}
50
51static int ipv4_print_tuple(struct seq_file *s,
52 const struct nf_conntrack_tuple *tuple)
53{
54 return seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090055 NIPQUAD(tuple->src.u3.ip),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080056 NIPQUAD(tuple->dst.u3.ip));
57}
58
59static int ipv4_print_conntrack(struct seq_file *s,
60 const struct nf_conn *conntrack)
61{
62 return 0;
63}
64
65/* Returns new sk_buff, or NULL */
66static struct sk_buff *
67nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)
68{
69 skb_orphan(skb);
70
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090071 local_bh_disable();
72 skb = ip_defrag(skb, user);
73 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080074
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090075 if (skb)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070076 ip_send_check(ip_hdr(skb));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080077
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090078 return skb;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080079}
80
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070081static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
82 unsigned int *dataoff, u_int8_t *protonum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080083{
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070084 struct iphdr _iph, *iph;
85
86 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
87 if (iph == NULL)
88 return -NF_DROP;
89
Patrick McHardy0fb96702007-09-11 11:27:01 +020090 /* Conntrack defragments packets, we might still see fragments
91 * inside ICMP packets though. */
92 if (iph->frag_off & htons(IP_OFFSET))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080093 return -NF_DROP;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080094
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070095 *dataoff = nhoff + (iph->ihl << 2);
96 *protonum = iph->protocol;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080097
98 return NF_ACCEPT;
99}
100
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800101static unsigned int ipv4_confirm(unsigned int hooknum,
102 struct sk_buff **pskb,
103 const struct net_device *in,
104 const struct net_device *out,
105 int (*okfn)(struct sk_buff *))
106{
107 /* We've seen it coming out the other side: confirm it */
108 return nf_conntrack_confirm(pskb);
109}
110
111static unsigned int ipv4_conntrack_help(unsigned int hooknum,
112 struct sk_buff **pskb,
113 const struct net_device *in,
114 const struct net_device *out,
115 int (*okfn)(struct sk_buff *))
116{
117 struct nf_conn *ct;
118 enum ip_conntrack_info ctinfo;
Harald Weltedc808fe2006-03-20 17:56:32 -0800119 struct nf_conn_help *help;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700120 struct nf_conntrack_helper *helper;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800121
122 /* This is where we call the helper: as the packet goes out. */
123 ct = nf_ct_get(*pskb, &ctinfo);
Patrick McHardy6442f1c2006-05-29 18:21:53 -0700124 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
Harald Weltedc808fe2006-03-20 17:56:32 -0800125 return NF_ACCEPT;
126
127 help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700128 if (!help)
Harald Weltedc808fe2006-03-20 17:56:32 -0800129 return NF_ACCEPT;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700130 /* rcu_read_lock()ed by nf_hook_slow */
131 helper = rcu_dereference(help->helper);
132 if (!helper)
133 return NF_ACCEPT;
134 return helper->help(pskb, skb_network_offset(*pskb) + ip_hdrlen(*pskb),
135 ct, ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800136}
137
138static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
139 struct sk_buff **pskb,
140 const struct net_device *in,
141 const struct net_device *out,
142 int (*okfn)(struct sk_buff *))
143{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800144 /* Previously seen (loopback)? Ignore. Do this before
145 fragment check. */
146 if ((*pskb)->nfct)
147 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800148
149 /* Gather fragments. */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700150 if (ip_hdr(*pskb)->frag_off & htons(IP_MF | IP_OFFSET)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800151 *pskb = nf_ct_ipv4_gather_frags(*pskb,
152 hooknum == NF_IP_PRE_ROUTING ?
153 IP_DEFRAG_CONNTRACK_IN :
154 IP_DEFRAG_CONNTRACK_OUT);
155 if (!*pskb)
156 return NF_STOLEN;
157 }
158 return NF_ACCEPT;
159}
160
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800161static unsigned int ipv4_conntrack_in(unsigned int hooknum,
162 struct sk_buff **pskb,
163 const struct net_device *in,
164 const struct net_device *out,
165 int (*okfn)(struct sk_buff *))
166{
167 return nf_conntrack_in(PF_INET, hooknum, pskb);
168}
169
170static unsigned int ipv4_conntrack_local(unsigned int hooknum,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900171 struct sk_buff **pskb,
172 const struct net_device *in,
173 const struct net_device *out,
174 int (*okfn)(struct sk_buff *))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800175{
176 /* root is playing with raw sockets. */
177 if ((*pskb)->len < sizeof(struct iphdr)
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300178 || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800179 if (net_ratelimit())
180 printk("ipt_hook: happy cracking.\n");
181 return NF_ACCEPT;
182 }
183 return nf_conntrack_in(PF_INET, hooknum, pskb);
184}
185
186/* Connection tracking may drop packets, but never alters them, so
187 make it the first hook. */
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700188static struct nf_hook_ops ipv4_conntrack_ops[] = {
189 {
190 .hook = ipv4_conntrack_defrag,
191 .owner = THIS_MODULE,
192 .pf = PF_INET,
193 .hooknum = NF_IP_PRE_ROUTING,
194 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
195 },
196 {
197 .hook = ipv4_conntrack_in,
198 .owner = THIS_MODULE,
199 .pf = PF_INET,
200 .hooknum = NF_IP_PRE_ROUTING,
201 .priority = NF_IP_PRI_CONNTRACK,
202 },
203 {
204 .hook = ipv4_conntrack_defrag,
205 .owner = THIS_MODULE,
206 .pf = PF_INET,
207 .hooknum = NF_IP_LOCAL_OUT,
208 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
209 },
210 {
211 .hook = ipv4_conntrack_local,
212 .owner = THIS_MODULE,
213 .pf = PF_INET,
214 .hooknum = NF_IP_LOCAL_OUT,
215 .priority = NF_IP_PRI_CONNTRACK,
216 },
217 {
218 .hook = ipv4_conntrack_help,
219 .owner = THIS_MODULE,
220 .pf = PF_INET,
221 .hooknum = NF_IP_POST_ROUTING,
222 .priority = NF_IP_PRI_CONNTRACK_HELPER,
223 },
224 {
225 .hook = ipv4_conntrack_help,
226 .owner = THIS_MODULE,
227 .pf = PF_INET,
228 .hooknum = NF_IP_LOCAL_IN,
229 .priority = NF_IP_PRI_CONNTRACK_HELPER,
230 },
231 {
232 .hook = ipv4_confirm,
233 .owner = THIS_MODULE,
234 .pf = PF_INET,
235 .hooknum = NF_IP_POST_ROUTING,
236 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
237 },
238 {
239 .hook = ipv4_confirm,
240 .owner = THIS_MODULE,
241 .pf = PF_INET,
242 .hooknum = NF_IP_LOCAL_IN,
243 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
244 },
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800245};
246
Patrick McHardya999e682006-11-29 02:35:20 +0100247#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
248static int log_invalid_proto_min = 0;
249static int log_invalid_proto_max = 255;
250
251static ctl_table ip_ct_sysctl_table[] = {
252 {
253 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
254 .procname = "ip_conntrack_max",
255 .data = &nf_conntrack_max,
256 .maxlen = sizeof(int),
257 .mode = 0644,
258 .proc_handler = &proc_dointvec,
259 },
260 {
261 .ctl_name = NET_IPV4_NF_CONNTRACK_COUNT,
262 .procname = "ip_conntrack_count",
263 .data = &nf_conntrack_count,
264 .maxlen = sizeof(int),
265 .mode = 0444,
266 .proc_handler = &proc_dointvec,
267 },
268 {
269 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
270 .procname = "ip_conntrack_buckets",
271 .data = &nf_conntrack_htable_size,
272 .maxlen = sizeof(unsigned int),
273 .mode = 0444,
274 .proc_handler = &proc_dointvec,
275 },
276 {
277 .ctl_name = NET_IPV4_NF_CONNTRACK_CHECKSUM,
278 .procname = "ip_conntrack_checksum",
279 .data = &nf_conntrack_checksum,
280 .maxlen = sizeof(int),
281 .mode = 0644,
282 .proc_handler = &proc_dointvec,
283 },
284 {
285 .ctl_name = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
286 .procname = "ip_conntrack_log_invalid",
287 .data = &nf_ct_log_invalid,
288 .maxlen = sizeof(unsigned int),
289 .mode = 0644,
290 .proc_handler = &proc_dointvec_minmax,
291 .strategy = &sysctl_intvec,
292 .extra1 = &log_invalid_proto_min,
293 .extra2 = &log_invalid_proto_max,
294 },
295 {
296 .ctl_name = 0
297 }
298};
299#endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
300
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800301/* Fast function for those who don't want to parse /proc (and I don't
302 blame them). */
303/* Reversing the socket's dst/src point of view gives us the reply
304 mapping. */
305static int
306getorigdst(struct sock *sk, int optval, void __user *user, int *len)
307{
308 struct inet_sock *inet = inet_sk(sk);
309 struct nf_conntrack_tuple_hash *h;
310 struct nf_conntrack_tuple tuple;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900311
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800312 NF_CT_TUPLE_U_BLANK(&tuple);
313 tuple.src.u3.ip = inet->rcv_saddr;
314 tuple.src.u.tcp.port = inet->sport;
315 tuple.dst.u3.ip = inet->daddr;
316 tuple.dst.u.tcp.port = inet->dport;
317 tuple.src.l3num = PF_INET;
318 tuple.dst.protonum = IPPROTO_TCP;
319
320 /* We only do TCP at the moment: is there a better way? */
321 if (strcmp(sk->sk_prot->name, "TCP")) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700322 pr_debug("SO_ORIGINAL_DST: Not a TCP socket\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800323 return -ENOPROTOOPT;
324 }
325
326 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700327 pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n",
328 *len, sizeof(struct sockaddr_in));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800329 return -EINVAL;
330 }
331
Patrick McHardy330f7db2007-07-07 22:28:42 -0700332 h = nf_conntrack_find_get(&tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800333 if (h) {
334 struct sockaddr_in sin;
335 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
336
337 sin.sin_family = AF_INET;
338 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
339 .tuple.dst.u.tcp.port;
340 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
341 .tuple.dst.u3.ip;
Marcel Holtmann6c813c32006-05-28 22:50:18 -0700342 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800343
Patrick McHardy0d537782007-07-07 22:39:38 -0700344 pr_debug("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
345 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800346 nf_ct_put(ct);
347 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
348 return -EFAULT;
349 else
350 return 0;
351 }
Patrick McHardy0d537782007-07-07 22:39:38 -0700352 pr_debug("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
353 NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
354 NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800355 return -ENOENT;
356}
357
Patrick McHardye281db5c2007-03-04 15:57:25 -0800358#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800359
360#include <linux/netfilter/nfnetlink.h>
361#include <linux/netfilter/nfnetlink_conntrack.h>
362
Patrick McHardyfdf70832007-09-28 14:37:41 -0700363static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800364 const struct nf_conntrack_tuple *tuple)
365{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700366 NLA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t),
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800367 &tuple->src.u3.ip);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700368 NLA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t),
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800369 &tuple->dst.u3.ip);
370 return 0;
371
Patrick McHardydf6fb862007-09-28 14:37:03 -0700372nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800373 return -1;
374}
375
Patrick McHardydf6fb862007-09-28 14:37:03 -0700376static const size_t cta_min_ip[CTA_IP_MAX+1] = {
377 [CTA_IP_V4_SRC] = sizeof(u_int32_t),
378 [CTA_IP_V4_DST] = sizeof(u_int32_t),
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800379};
380
Patrick McHardyfdf70832007-09-28 14:37:41 -0700381static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800382 struct nf_conntrack_tuple *t)
383{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700384 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800385 return -EINVAL;
386
Patrick McHardyfdf70832007-09-28 14:37:41 -0700387 if (nlattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800388 return -EINVAL;
389
Patrick McHardydf6fb862007-09-28 14:37:03 -0700390 t->src.u3.ip = *(__be32 *)nla_data(tb[CTA_IP_V4_SRC]);
391 t->dst.u3.ip = *(__be32 *)nla_data(tb[CTA_IP_V4_DST]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800392
393 return 0;
394}
395#endif
396
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800397static struct nf_sockopt_ops so_getorigdst = {
398 .pf = PF_INET,
399 .get_optmin = SO_ORIGINAL_DST,
400 .get_optmax = SO_ORIGINAL_DST+1,
401 .get = &getorigdst,
Neil Horman16fcec32007-09-11 11:28:26 +0200402 .owner = THIS_MODULE,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800403};
404
Patrick McHardy61075af2007-07-14 20:48:19 -0700405struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 __read_mostly = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800406 .l3proto = PF_INET,
407 .name = "ipv4",
408 .pkt_to_tuple = ipv4_pkt_to_tuple,
409 .invert_tuple = ipv4_invert_tuple,
410 .print_tuple = ipv4_print_tuple,
411 .print_conntrack = ipv4_print_conntrack,
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -0700412 .get_l4proto = ipv4_get_l4proto,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800413#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700414 .tuple_to_nlattr = ipv4_tuple_to_nlattr,
415 .nlattr_to_tuple = ipv4_nlattr_to_tuple,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800416#endif
Patrick McHardya999e682006-11-29 02:35:20 +0100417#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
418 .ctl_table_path = nf_net_ipv4_netfilter_sysctl_path,
419 .ctl_table = ip_ct_sysctl_table,
420#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800421 .me = THIS_MODULE,
422};
423
Patrick McHardy32292a72006-04-06 14:11:30 -0700424MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
Patrick McHardyd2483dd2006-12-02 22:06:05 -0800425MODULE_ALIAS("ip_conntrack");
Patrick McHardy32292a72006-04-06 14:11:30 -0700426MODULE_LICENSE("GPL");
427
428static int __init nf_conntrack_l3proto_ipv4_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800429{
430 int ret = 0;
431
Patrick McHardy32292a72006-04-06 14:11:30 -0700432 need_conntrack();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800433
434 ret = nf_register_sockopt(&so_getorigdst);
435 if (ret < 0) {
436 printk(KERN_ERR "Unable to register netfilter socket option\n");
Patrick McHardy32292a72006-04-06 14:11:30 -0700437 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800438 }
439
Martin Josefsson605dcad2006-11-29 02:35:06 +0100440 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800441 if (ret < 0) {
442 printk("nf_conntrack_ipv4: can't register tcp.\n");
443 goto cleanup_sockopt;
444 }
445
Martin Josefsson605dcad2006-11-29 02:35:06 +0100446 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800447 if (ret < 0) {
448 printk("nf_conntrack_ipv4: can't register udp.\n");
449 goto cleanup_tcp;
450 }
451
Martin Josefsson605dcad2006-11-29 02:35:06 +0100452 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800453 if (ret < 0) {
454 printk("nf_conntrack_ipv4: can't register icmp.\n");
455 goto cleanup_udp;
456 }
457
458 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4);
459 if (ret < 0) {
460 printk("nf_conntrack_ipv4: can't register ipv4\n");
461 goto cleanup_icmp;
462 }
463
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700464 ret = nf_register_hooks(ipv4_conntrack_ops,
465 ARRAY_SIZE(ipv4_conntrack_ops));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800466 if (ret < 0) {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700467 printk("nf_conntrack_ipv4: can't register hooks.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800468 goto cleanup_ipv4;
469 }
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100470#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
471 ret = nf_conntrack_ipv4_compat_init();
472 if (ret < 0)
473 goto cleanup_hooks;
474#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800475 return ret;
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100476#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
477 cleanup_hooks:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900478 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100479#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800480 cleanup_ipv4:
481 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
482 cleanup_icmp:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100483 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800484 cleanup_udp:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100485 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800486 cleanup_tcp:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100487 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800488 cleanup_sockopt:
489 nf_unregister_sockopt(&so_getorigdst);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800490 return ret;
491}
492
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800493static void __exit nf_conntrack_l3proto_ipv4_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800494{
Patrick McHardy32292a72006-04-06 14:11:30 -0700495 synchronize_net();
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100496#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
497 nf_conntrack_ipv4_compat_fini();
498#endif
Patrick McHardy32292a72006-04-06 14:11:30 -0700499 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
500 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
Martin Josefsson605dcad2006-11-29 02:35:06 +0100501 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
502 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
503 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
Patrick McHardy32292a72006-04-06 14:11:30 -0700504 nf_unregister_sockopt(&so_getorigdst);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800505}
506
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800507module_init(nf_conntrack_l3proto_ipv4_init);
508module_exit(nf_conntrack_l3proto_ipv4_fini);
Patrick McHardy591e6202007-08-07 18:12:01 -0700509
510void need_ipv4_conntrack(void)
511{
512 return;
513}
514EXPORT_SYMBOL_GPL(need_ipv4_conntrack);