blob: 41e79613eb0a211fa370d2650fac5e691d95ea37 [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>
Patrick McHardydd13b012008-04-14 11:15:52 +020026#include <net/netfilter/nf_nat_helper.h>
27
28int (*nf_nat_seq_adjust_hook)(struct sk_buff *skb,
29 struct nf_conn *ct,
30 enum ip_conntrack_info ctinfo);
31EXPORT_SYMBOL_GPL(nf_nat_seq_adjust_hook);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080032
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080033static int ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
34 struct nf_conntrack_tuple *tuple)
35{
Jan Engelhardt32948582008-01-31 04:53:24 -080036 const __be32 *ap;
37 __be32 _addrs[2];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080038 ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
39 sizeof(u_int32_t) * 2, _addrs);
40 if (ap == NULL)
41 return 0;
42
43 tuple->src.u3.ip = ap[0];
44 tuple->dst.u3.ip = ap[1];
45
46 return 1;
47}
48
49static int ipv4_invert_tuple(struct nf_conntrack_tuple *tuple,
50 const struct nf_conntrack_tuple *orig)
51{
52 tuple->src.u3.ip = orig->dst.u3.ip;
53 tuple->dst.u3.ip = orig->src.u3.ip;
54
55 return 1;
56}
57
58static int ipv4_print_tuple(struct seq_file *s,
59 const struct nf_conntrack_tuple *tuple)
60{
61 return seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090062 NIPQUAD(tuple->src.u3.ip),
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080063 NIPQUAD(tuple->dst.u3.ip));
64}
65
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080066/* Returns new sk_buff, or NULL */
Herbert Xu776c7292007-10-14 00:38:32 -070067static int nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068{
Herbert Xu776c7292007-10-14 00:38:32 -070069 int err;
70
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080071 skb_orphan(skb);
72
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090073 local_bh_disable();
Herbert Xu776c7292007-10-14 00:38:32 -070074 err = ip_defrag(skb, user);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090075 local_bh_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080076
Herbert Xu776c7292007-10-14 00:38:32 -070077 if (!err)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070078 ip_send_check(ip_hdr(skb));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080079
Herbert Xu776c7292007-10-14 00:38:32 -070080 return err;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080081}
82
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070083static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
84 unsigned int *dataoff, u_int8_t *protonum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080085{
Jan Engelhardt32948582008-01-31 04:53:24 -080086 const struct iphdr *iph;
87 struct iphdr _iph;
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070088
89 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
90 if (iph == NULL)
91 return -NF_DROP;
92
Patrick McHardy0fb96702007-09-11 11:27:01 +020093 /* Conntrack defragments packets, we might still see fragments
94 * inside ICMP packets though. */
95 if (iph->frag_off & htons(IP_OFFSET))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080096 return -NF_DROP;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080097
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070098 *dataoff = nhoff + (iph->ihl << 2);
99 *protonum = iph->protocol;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800100
101 return NF_ACCEPT;
102}
103
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800104static unsigned int ipv4_confirm(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700105 struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800106 const struct net_device *in,
107 const struct net_device *out,
108 int (*okfn)(struct sk_buff *))
109{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800110 struct nf_conn *ct;
111 enum ip_conntrack_info ctinfo;
Jan Engelhardt32948582008-01-31 04:53:24 -0800112 const struct nf_conn_help *help;
113 const struct nf_conntrack_helper *helper;
Patrick McHardydd13b012008-04-14 11:15:52 +0200114 unsigned int ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800115
116 /* This is where we call the helper: as the packet goes out. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700117 ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy6442f1c2006-05-29 18:21:53 -0700118 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
Patrick McHardydd13b012008-04-14 11:15:52 +0200119 goto out;
Harald Weltedc808fe2006-03-20 17:56:32 -0800120
121 help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700122 if (!help)
Patrick McHardydd13b012008-04-14 11:15:52 +0200123 goto out;
124
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700125 /* rcu_read_lock()ed by nf_hook_slow */
126 helper = rcu_dereference(help->helper);
127 if (!helper)
Patrick McHardydd13b012008-04-14 11:15:52 +0200128 goto out;
129
130 ret = helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
131 ct, ctinfo);
132 if (ret != NF_ACCEPT)
133 return ret;
134
135 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
136 typeof(nf_nat_seq_adjust_hook) seq_adjust;
137
138 seq_adjust = rcu_dereference(nf_nat_seq_adjust_hook);
139 if (!seq_adjust || !seq_adjust(skb, ct, ctinfo))
140 return NF_DROP;
141 }
142out:
143 /* We've seen it coming out the other side: confirm it */
144 return nf_conntrack_confirm(skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800145}
146
147static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700148 struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800149 const struct net_device *in,
150 const struct net_device *out,
151 int (*okfn)(struct sk_buff *))
152{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800153 /* Previously seen (loopback)? Ignore. Do this before
154 fragment check. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700155 if (skb->nfct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800156 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800157
158 /* Gather fragments. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700159 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
160 if (nf_ct_ipv4_gather_frags(skb,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800161 hooknum == NF_INET_PRE_ROUTING ?
Herbert Xu776c7292007-10-14 00:38:32 -0700162 IP_DEFRAG_CONNTRACK_IN :
163 IP_DEFRAG_CONNTRACK_OUT))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800164 return NF_STOLEN;
165 }
166 return NF_ACCEPT;
167}
168
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169static unsigned int ipv4_conntrack_in(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700170 struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800171 const struct net_device *in,
172 const struct net_device *out,
173 int (*okfn)(struct sk_buff *))
174{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700175 return nf_conntrack_in(PF_INET, hooknum, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800176}
177
178static unsigned int ipv4_conntrack_local(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700179 struct sk_buff *skb,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900180 const struct net_device *in,
181 const struct net_device *out,
182 int (*okfn)(struct sk_buff *))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800183{
184 /* root is playing with raw sockets. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700185 if (skb->len < sizeof(struct iphdr) ||
186 ip_hdrlen(skb) < sizeof(struct iphdr)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800187 if (net_ratelimit())
188 printk("ipt_hook: happy cracking.\n");
189 return NF_ACCEPT;
190 }
Herbert Xu3db05fe2007-10-15 00:53:15 -0700191 return nf_conntrack_in(PF_INET, hooknum, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800192}
193
194/* Connection tracking may drop packets, but never alters them, so
195 make it the first hook. */
Patrick McHardy19994142007-12-05 01:23:00 -0800196static struct nf_hook_ops ipv4_conntrack_ops[] __read_mostly = {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700197 {
198 .hook = ipv4_conntrack_defrag,
199 .owner = THIS_MODULE,
200 .pf = PF_INET,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800201 .hooknum = NF_INET_PRE_ROUTING,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700202 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
203 },
204 {
205 .hook = ipv4_conntrack_in,
206 .owner = THIS_MODULE,
207 .pf = PF_INET,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800208 .hooknum = NF_INET_PRE_ROUTING,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700209 .priority = NF_IP_PRI_CONNTRACK,
210 },
211 {
212 .hook = ipv4_conntrack_defrag,
213 .owner = THIS_MODULE,
214 .pf = PF_INET,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800215 .hooknum = NF_INET_LOCAL_OUT,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700216 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
217 },
218 {
219 .hook = ipv4_conntrack_local,
220 .owner = THIS_MODULE,
221 .pf = PF_INET,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800222 .hooknum = NF_INET_LOCAL_OUT,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700223 .priority = NF_IP_PRI_CONNTRACK,
224 },
225 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700226 .hook = ipv4_confirm,
227 .owner = THIS_MODULE,
228 .pf = PF_INET,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800229 .hooknum = NF_INET_POST_ROUTING,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700230 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
231 },
232 {
233 .hook = ipv4_confirm,
234 .owner = THIS_MODULE,
235 .pf = PF_INET,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800236 .hooknum = NF_INET_LOCAL_IN,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700237 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
238 },
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800239};
240
Patrick McHardya999e682006-11-29 02:35:20 +0100241#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
242static int log_invalid_proto_min = 0;
243static int log_invalid_proto_max = 255;
244
245static ctl_table ip_ct_sysctl_table[] = {
246 {
247 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
248 .procname = "ip_conntrack_max",
249 .data = &nf_conntrack_max,
250 .maxlen = sizeof(int),
251 .mode = 0644,
252 .proc_handler = &proc_dointvec,
253 },
254 {
255 .ctl_name = NET_IPV4_NF_CONNTRACK_COUNT,
256 .procname = "ip_conntrack_count",
257 .data = &nf_conntrack_count,
258 .maxlen = sizeof(int),
259 .mode = 0444,
260 .proc_handler = &proc_dointvec,
261 },
262 {
263 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
264 .procname = "ip_conntrack_buckets",
265 .data = &nf_conntrack_htable_size,
266 .maxlen = sizeof(unsigned int),
267 .mode = 0444,
268 .proc_handler = &proc_dointvec,
269 },
270 {
271 .ctl_name = NET_IPV4_NF_CONNTRACK_CHECKSUM,
272 .procname = "ip_conntrack_checksum",
273 .data = &nf_conntrack_checksum,
274 .maxlen = sizeof(int),
275 .mode = 0644,
276 .proc_handler = &proc_dointvec,
277 },
278 {
279 .ctl_name = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
280 .procname = "ip_conntrack_log_invalid",
281 .data = &nf_ct_log_invalid,
282 .maxlen = sizeof(unsigned int),
283 .mode = 0644,
284 .proc_handler = &proc_dointvec_minmax,
285 .strategy = &sysctl_intvec,
286 .extra1 = &log_invalid_proto_min,
287 .extra2 = &log_invalid_proto_max,
288 },
289 {
290 .ctl_name = 0
291 }
292};
293#endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
294
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800295/* Fast function for those who don't want to parse /proc (and I don't
296 blame them). */
297/* Reversing the socket's dst/src point of view gives us the reply
298 mapping. */
299static int
300getorigdst(struct sock *sk, int optval, void __user *user, int *len)
301{
Jan Engelhardt32948582008-01-31 04:53:24 -0800302 const struct inet_sock *inet = inet_sk(sk);
303 const struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800304 struct nf_conntrack_tuple tuple;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900305
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800306 NF_CT_TUPLE_U_BLANK(&tuple);
307 tuple.src.u3.ip = inet->rcv_saddr;
308 tuple.src.u.tcp.port = inet->sport;
309 tuple.dst.u3.ip = inet->daddr;
310 tuple.dst.u.tcp.port = inet->dport;
311 tuple.src.l3num = PF_INET;
312 tuple.dst.protonum = IPPROTO_TCP;
313
314 /* We only do TCP at the moment: is there a better way? */
315 if (strcmp(sk->sk_prot->name, "TCP")) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700316 pr_debug("SO_ORIGINAL_DST: Not a TCP socket\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800317 return -ENOPROTOOPT;
318 }
319
320 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700321 pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n",
322 *len, sizeof(struct sockaddr_in));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800323 return -EINVAL;
324 }
325
Patrick McHardy330f7db2007-07-07 22:28:42 -0700326 h = nf_conntrack_find_get(&tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800327 if (h) {
328 struct sockaddr_in sin;
329 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
330
331 sin.sin_family = AF_INET;
332 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
333 .tuple.dst.u.tcp.port;
334 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
335 .tuple.dst.u3.ip;
Marcel Holtmann6c813c32006-05-28 22:50:18 -0700336 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800337
Patrick McHardy0d537782007-07-07 22:39:38 -0700338 pr_debug("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
339 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800340 nf_ct_put(ct);
341 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
342 return -EFAULT;
343 else
344 return 0;
345 }
Patrick McHardy0d537782007-07-07 22:39:38 -0700346 pr_debug("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
347 NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
348 NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800349 return -ENOENT;
350}
351
Patrick McHardye281db5c2007-03-04 15:57:25 -0800352#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800353
354#include <linux/netfilter/nfnetlink.h>
355#include <linux/netfilter/nfnetlink_conntrack.h>
356
Patrick McHardyfdf70832007-09-28 14:37:41 -0700357static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800358 const struct nf_conntrack_tuple *tuple)
359{
Patrick McHardy77236b62007-12-17 22:29:45 -0800360 NLA_PUT_BE32(skb, CTA_IP_V4_SRC, tuple->src.u3.ip);
361 NLA_PUT_BE32(skb, CTA_IP_V4_DST, tuple->dst.u3.ip);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800362 return 0;
363
Patrick McHardydf6fb862007-09-28 14:37:03 -0700364nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800365 return -1;
366}
367
Patrick McHardyf73e9242007-09-28 14:39:55 -0700368static const struct nla_policy ipv4_nla_policy[CTA_IP_MAX+1] = {
369 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
370 [CTA_IP_V4_DST] = { .type = NLA_U32 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800371};
372
Patrick McHardyfdf70832007-09-28 14:37:41 -0700373static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800374 struct nf_conntrack_tuple *t)
375{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700376 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800377 return -EINVAL;
378
Patrick McHardy77236b62007-12-17 22:29:45 -0800379 t->src.u3.ip = nla_get_be32(tb[CTA_IP_V4_SRC]);
380 t->dst.u3.ip = nla_get_be32(tb[CTA_IP_V4_DST]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800381
382 return 0;
383}
384#endif
385
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800386static struct nf_sockopt_ops so_getorigdst = {
387 .pf = PF_INET,
388 .get_optmin = SO_ORIGINAL_DST,
389 .get_optmax = SO_ORIGINAL_DST+1,
390 .get = &getorigdst,
Neil Horman16fcec32007-09-11 11:28:26 +0200391 .owner = THIS_MODULE,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800392};
393
Patrick McHardy61075af2007-07-14 20:48:19 -0700394struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 __read_mostly = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800395 .l3proto = PF_INET,
396 .name = "ipv4",
397 .pkt_to_tuple = ipv4_pkt_to_tuple,
398 .invert_tuple = ipv4_invert_tuple,
399 .print_tuple = ipv4_print_tuple,
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -0700400 .get_l4proto = ipv4_get_l4proto,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800401#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700402 .tuple_to_nlattr = ipv4_tuple_to_nlattr,
403 .nlattr_to_tuple = ipv4_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700404 .nla_policy = ipv4_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800405#endif
Patrick McHardya999e682006-11-29 02:35:20 +0100406#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
407 .ctl_table_path = nf_net_ipv4_netfilter_sysctl_path,
408 .ctl_table = ip_ct_sysctl_table,
409#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800410 .me = THIS_MODULE,
411};
412
Patrick McHardyfae718d2007-12-24 21:09:10 -0800413module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
414 &nf_conntrack_htable_size, 0600);
415
Patrick McHardy32292a72006-04-06 14:11:30 -0700416MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
Patrick McHardyd2483dd2006-12-02 22:06:05 -0800417MODULE_ALIAS("ip_conntrack");
Patrick McHardy32292a72006-04-06 14:11:30 -0700418MODULE_LICENSE("GPL");
419
420static int __init nf_conntrack_l3proto_ipv4_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800421{
422 int ret = 0;
423
Patrick McHardy32292a72006-04-06 14:11:30 -0700424 need_conntrack();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800425
426 ret = nf_register_sockopt(&so_getorigdst);
427 if (ret < 0) {
428 printk(KERN_ERR "Unable to register netfilter socket option\n");
Patrick McHardy32292a72006-04-06 14:11:30 -0700429 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800430 }
431
Martin Josefsson605dcad2006-11-29 02:35:06 +0100432 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800433 if (ret < 0) {
434 printk("nf_conntrack_ipv4: can't register tcp.\n");
435 goto cleanup_sockopt;
436 }
437
Martin Josefsson605dcad2006-11-29 02:35:06 +0100438 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800439 if (ret < 0) {
440 printk("nf_conntrack_ipv4: can't register udp.\n");
441 goto cleanup_tcp;
442 }
443
Martin Josefsson605dcad2006-11-29 02:35:06 +0100444 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800445 if (ret < 0) {
446 printk("nf_conntrack_ipv4: can't register icmp.\n");
447 goto cleanup_udp;
448 }
449
450 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4);
451 if (ret < 0) {
452 printk("nf_conntrack_ipv4: can't register ipv4\n");
453 goto cleanup_icmp;
454 }
455
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700456 ret = nf_register_hooks(ipv4_conntrack_ops,
457 ARRAY_SIZE(ipv4_conntrack_ops));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800458 if (ret < 0) {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700459 printk("nf_conntrack_ipv4: can't register hooks.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800460 goto cleanup_ipv4;
461 }
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100462#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
463 ret = nf_conntrack_ipv4_compat_init();
464 if (ret < 0)
465 goto cleanup_hooks;
466#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800467 return ret;
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100468#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
469 cleanup_hooks:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900470 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100471#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800472 cleanup_ipv4:
473 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
474 cleanup_icmp:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100475 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800476 cleanup_udp:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100477 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800478 cleanup_tcp:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100479 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800480 cleanup_sockopt:
481 nf_unregister_sockopt(&so_getorigdst);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800482 return ret;
483}
484
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800485static void __exit nf_conntrack_l3proto_ipv4_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800486{
Patrick McHardy32292a72006-04-06 14:11:30 -0700487 synchronize_net();
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100488#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
489 nf_conntrack_ipv4_compat_fini();
490#endif
Patrick McHardy32292a72006-04-06 14:11:30 -0700491 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
492 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
Martin Josefsson605dcad2006-11-29 02:35:06 +0100493 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
494 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
495 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
Patrick McHardy32292a72006-04-06 14:11:30 -0700496 nf_unregister_sockopt(&so_getorigdst);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800497}
498
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800499module_init(nf_conntrack_l3proto_ipv4_init);
500module_exit(nf_conntrack_l3proto_ipv4_fini);
Patrick McHardy591e6202007-08-07 18:12:01 -0700501
502void need_ipv4_conntrack(void)
503{
504 return;
505}
506EXPORT_SYMBOL_GPL(need_ipv4_conntrack);