1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
9 #include <linux/types.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>
16 #include <net/route.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <net/netfilter/nf_conntrack.h>
21 #include <net/netfilter/nf_conntrack_helper.h>
22 #include <net/netfilter/nf_conntrack_l4proto.h>
23 #include <net/netfilter/nf_conntrack_l3proto.h>
24 #include <net/netfilter/nf_conntrack_core.h>
25 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
27 static int ipv4_pkt_to_tuple(const struct sk_buff
*skb
, unsigned int nhoff
,
28 struct nf_conntrack_tuple
*tuple
)
30 __be32 _addrs
[2], *ap
;
31 ap
= skb_header_pointer(skb
, nhoff
+ offsetof(struct iphdr
, saddr
),
32 sizeof(u_int32_t
) * 2, _addrs
);
36 tuple
->src
.u3
.ip
= ap
[0];
37 tuple
->dst
.u3
.ip
= ap
[1];
42 static int ipv4_invert_tuple(struct nf_conntrack_tuple
*tuple
,
43 const struct nf_conntrack_tuple
*orig
)
45 tuple
->src
.u3
.ip
= orig
->dst
.u3
.ip
;
46 tuple
->dst
.u3
.ip
= orig
->src
.u3
.ip
;
51 static int ipv4_print_tuple(struct seq_file
*s
,
52 const struct nf_conntrack_tuple
*tuple
)
54 return seq_printf(s
, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
55 NIPQUAD(tuple
->src
.u3
.ip
),
56 NIPQUAD(tuple
->dst
.u3
.ip
));
59 static int ipv4_print_conntrack(struct seq_file
*s
,
60 const struct nf_conn
*conntrack
)
65 /* Returns new sk_buff, or NULL */
66 static struct sk_buff
*
67 nf_ct_ipv4_gather_frags(struct sk_buff
*skb
, u_int32_t user
)
72 skb
= ip_defrag(skb
, user
);
76 ip_send_check(ip_hdr(skb
));
82 ipv4_prepare(struct sk_buff
**pskb
, unsigned int hooknum
, unsigned int *dataoff
,
86 if (ip_hdr(*pskb
)->frag_off
& htons(IP_OFFSET
)) {
87 if (net_ratelimit()) {
88 printk(KERN_ERR
"ipv4_prepare: Frag of proto %u (hook=%u)\n",
89 ip_hdr(*pskb
)->protocol
, hooknum
);
94 *dataoff
= skb_network_offset(*pskb
) + ip_hdrlen(*pskb
);
95 *protonum
= ip_hdr(*pskb
)->protocol
;
100 static unsigned int ipv4_confirm(unsigned int hooknum
,
101 struct sk_buff
**pskb
,
102 const struct net_device
*in
,
103 const struct net_device
*out
,
104 int (*okfn
)(struct sk_buff
*))
106 /* We've seen it coming out the other side: confirm it */
107 return nf_conntrack_confirm(pskb
);
110 static unsigned int ipv4_conntrack_help(unsigned int hooknum
,
111 struct sk_buff
**pskb
,
112 const struct net_device
*in
,
113 const struct net_device
*out
,
114 int (*okfn
)(struct sk_buff
*))
117 enum ip_conntrack_info ctinfo
;
118 struct nf_conn_help
*help
;
119 struct nf_conntrack_helper
*helper
;
121 /* This is where we call the helper: as the packet goes out. */
122 ct
= nf_ct_get(*pskb
, &ctinfo
);
123 if (!ct
|| ctinfo
== IP_CT_RELATED
+ IP_CT_IS_REPLY
)
126 help
= nfct_help(ct
);
129 /* rcu_read_lock()ed by nf_hook_slow */
130 helper
= rcu_dereference(help
->helper
);
133 return helper
->help(pskb
, skb_network_offset(*pskb
) + ip_hdrlen(*pskb
),
137 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum
,
138 struct sk_buff
**pskb
,
139 const struct net_device
*in
,
140 const struct net_device
*out
,
141 int (*okfn
)(struct sk_buff
*))
143 /* Previously seen (loopback)? Ignore. Do this before
148 /* Gather fragments. */
149 if (ip_hdr(*pskb
)->frag_off
& htons(IP_MF
| IP_OFFSET
)) {
150 *pskb
= nf_ct_ipv4_gather_frags(*pskb
,
151 hooknum
== NF_IP_PRE_ROUTING
?
152 IP_DEFRAG_CONNTRACK_IN
:
153 IP_DEFRAG_CONNTRACK_OUT
);
160 static unsigned int ipv4_conntrack_in(unsigned int hooknum
,
161 struct sk_buff
**pskb
,
162 const struct net_device
*in
,
163 const struct net_device
*out
,
164 int (*okfn
)(struct sk_buff
*))
166 return nf_conntrack_in(PF_INET
, hooknum
, pskb
);
169 static unsigned int ipv4_conntrack_local(unsigned int hooknum
,
170 struct sk_buff
**pskb
,
171 const struct net_device
*in
,
172 const struct net_device
*out
,
173 int (*okfn
)(struct sk_buff
*))
175 /* root is playing with raw sockets. */
176 if ((*pskb
)->len
< sizeof(struct iphdr
)
177 || ip_hdrlen(*pskb
) < sizeof(struct iphdr
)) {
179 printk("ipt_hook: happy cracking.\n");
182 return nf_conntrack_in(PF_INET
, hooknum
, pskb
);
185 /* Connection tracking may drop packets, but never alters them, so
186 make it the first hook. */
187 static struct nf_hook_ops ipv4_conntrack_ops
[] = {
189 .hook
= ipv4_conntrack_defrag
,
190 .owner
= THIS_MODULE
,
192 .hooknum
= NF_IP_PRE_ROUTING
,
193 .priority
= NF_IP_PRI_CONNTRACK_DEFRAG
,
196 .hook
= ipv4_conntrack_in
,
197 .owner
= THIS_MODULE
,
199 .hooknum
= NF_IP_PRE_ROUTING
,
200 .priority
= NF_IP_PRI_CONNTRACK
,
203 .hook
= ipv4_conntrack_defrag
,
204 .owner
= THIS_MODULE
,
206 .hooknum
= NF_IP_LOCAL_OUT
,
207 .priority
= NF_IP_PRI_CONNTRACK_DEFRAG
,
210 .hook
= ipv4_conntrack_local
,
211 .owner
= THIS_MODULE
,
213 .hooknum
= NF_IP_LOCAL_OUT
,
214 .priority
= NF_IP_PRI_CONNTRACK
,
217 .hook
= ipv4_conntrack_help
,
218 .owner
= THIS_MODULE
,
220 .hooknum
= NF_IP_POST_ROUTING
,
221 .priority
= NF_IP_PRI_CONNTRACK_HELPER
,
224 .hook
= ipv4_conntrack_help
,
225 .owner
= THIS_MODULE
,
227 .hooknum
= NF_IP_LOCAL_IN
,
228 .priority
= NF_IP_PRI_CONNTRACK_HELPER
,
231 .hook
= ipv4_confirm
,
232 .owner
= THIS_MODULE
,
234 .hooknum
= NF_IP_POST_ROUTING
,
235 .priority
= NF_IP_PRI_CONNTRACK_CONFIRM
,
238 .hook
= ipv4_confirm
,
239 .owner
= THIS_MODULE
,
241 .hooknum
= NF_IP_LOCAL_IN
,
242 .priority
= NF_IP_PRI_CONNTRACK_CONFIRM
,
246 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
247 static int log_invalid_proto_min
= 0;
248 static int log_invalid_proto_max
= 255;
250 static ctl_table ip_ct_sysctl_table
[] = {
252 .ctl_name
= NET_IPV4_NF_CONNTRACK_MAX
,
253 .procname
= "ip_conntrack_max",
254 .data
= &nf_conntrack_max
,
255 .maxlen
= sizeof(int),
257 .proc_handler
= &proc_dointvec
,
260 .ctl_name
= NET_IPV4_NF_CONNTRACK_COUNT
,
261 .procname
= "ip_conntrack_count",
262 .data
= &nf_conntrack_count
,
263 .maxlen
= sizeof(int),
265 .proc_handler
= &proc_dointvec
,
268 .ctl_name
= NET_IPV4_NF_CONNTRACK_BUCKETS
,
269 .procname
= "ip_conntrack_buckets",
270 .data
= &nf_conntrack_htable_size
,
271 .maxlen
= sizeof(unsigned int),
273 .proc_handler
= &proc_dointvec
,
276 .ctl_name
= NET_IPV4_NF_CONNTRACK_CHECKSUM
,
277 .procname
= "ip_conntrack_checksum",
278 .data
= &nf_conntrack_checksum
,
279 .maxlen
= sizeof(int),
281 .proc_handler
= &proc_dointvec
,
284 .ctl_name
= NET_IPV4_NF_CONNTRACK_LOG_INVALID
,
285 .procname
= "ip_conntrack_log_invalid",
286 .data
= &nf_ct_log_invalid
,
287 .maxlen
= sizeof(unsigned int),
289 .proc_handler
= &proc_dointvec_minmax
,
290 .strategy
= &sysctl_intvec
,
291 .extra1
= &log_invalid_proto_min
,
292 .extra2
= &log_invalid_proto_max
,
298 #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
300 /* Fast function for those who don't want to parse /proc (and I don't
302 /* Reversing the socket's dst/src point of view gives us the reply
305 getorigdst(struct sock
*sk
, int optval
, void __user
*user
, int *len
)
307 struct inet_sock
*inet
= inet_sk(sk
);
308 struct nf_conntrack_tuple_hash
*h
;
309 struct nf_conntrack_tuple tuple
;
311 NF_CT_TUPLE_U_BLANK(&tuple
);
312 tuple
.src
.u3
.ip
= inet
->rcv_saddr
;
313 tuple
.src
.u
.tcp
.port
= inet
->sport
;
314 tuple
.dst
.u3
.ip
= inet
->daddr
;
315 tuple
.dst
.u
.tcp
.port
= inet
->dport
;
316 tuple
.src
.l3num
= PF_INET
;
317 tuple
.dst
.protonum
= IPPROTO_TCP
;
319 /* We only do TCP at the moment: is there a better way? */
320 if (strcmp(sk
->sk_prot
->name
, "TCP")) {
321 pr_debug("SO_ORIGINAL_DST: Not a TCP socket\n");
325 if ((unsigned int) *len
< sizeof(struct sockaddr_in
)) {
326 pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n",
327 *len
, sizeof(struct sockaddr_in
));
331 h
= nf_conntrack_find_get(&tuple
);
333 struct sockaddr_in sin
;
334 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
336 sin
.sin_family
= AF_INET
;
337 sin
.sin_port
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
]
338 .tuple
.dst
.u
.tcp
.port
;
339 sin
.sin_addr
.s_addr
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
]
341 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
343 pr_debug("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
344 NIPQUAD(sin
.sin_addr
.s_addr
), ntohs(sin
.sin_port
));
346 if (copy_to_user(user
, &sin
, sizeof(sin
)) != 0)
351 pr_debug("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
352 NIPQUAD(tuple
.src
.u3
.ip
), ntohs(tuple
.src
.u
.tcp
.port
),
353 NIPQUAD(tuple
.dst
.u3
.ip
), ntohs(tuple
.dst
.u
.tcp
.port
));
357 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
359 #include <linux/netfilter/nfnetlink.h>
360 #include <linux/netfilter/nfnetlink_conntrack.h>
362 static int ipv4_tuple_to_nfattr(struct sk_buff
*skb
,
363 const struct nf_conntrack_tuple
*tuple
)
365 NFA_PUT(skb
, CTA_IP_V4_SRC
, sizeof(u_int32_t
),
367 NFA_PUT(skb
, CTA_IP_V4_DST
, sizeof(u_int32_t
),
375 static const size_t cta_min_ip
[CTA_IP_MAX
] = {
376 [CTA_IP_V4_SRC
-1] = sizeof(u_int32_t
),
377 [CTA_IP_V4_DST
-1] = sizeof(u_int32_t
),
380 static int ipv4_nfattr_to_tuple(struct nfattr
*tb
[],
381 struct nf_conntrack_tuple
*t
)
383 if (!tb
[CTA_IP_V4_SRC
-1] || !tb
[CTA_IP_V4_DST
-1])
386 if (nfattr_bad_size(tb
, CTA_IP_MAX
, cta_min_ip
))
389 t
->src
.u3
.ip
= *(__be32
*)NFA_DATA(tb
[CTA_IP_V4_SRC
-1]);
390 t
->dst
.u3
.ip
= *(__be32
*)NFA_DATA(tb
[CTA_IP_V4_DST
-1]);
396 static struct nf_sockopt_ops so_getorigdst
= {
398 .get_optmin
= SO_ORIGINAL_DST
,
399 .get_optmax
= SO_ORIGINAL_DST
+1,
403 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4
= {
406 .pkt_to_tuple
= ipv4_pkt_to_tuple
,
407 .invert_tuple
= ipv4_invert_tuple
,
408 .print_tuple
= ipv4_print_tuple
,
409 .print_conntrack
= ipv4_print_conntrack
,
410 .prepare
= ipv4_prepare
,
411 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
412 .tuple_to_nfattr
= ipv4_tuple_to_nfattr
,
413 .nfattr_to_tuple
= ipv4_nfattr_to_tuple
,
415 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
416 .ctl_table_path
= nf_net_ipv4_netfilter_sysctl_path
,
417 .ctl_table
= ip_ct_sysctl_table
,
422 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET
));
423 MODULE_ALIAS("ip_conntrack");
424 MODULE_LICENSE("GPL");
426 static int __init
nf_conntrack_l3proto_ipv4_init(void)
432 ret
= nf_register_sockopt(&so_getorigdst
);
434 printk(KERN_ERR
"Unable to register netfilter socket option\n");
438 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4
);
440 printk("nf_conntrack_ipv4: can't register tcp.\n");
441 goto cleanup_sockopt
;
444 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4
);
446 printk("nf_conntrack_ipv4: can't register udp.\n");
450 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp
);
452 printk("nf_conntrack_ipv4: can't register icmp.\n");
456 ret
= nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4
);
458 printk("nf_conntrack_ipv4: can't register ipv4\n");
462 ret
= nf_register_hooks(ipv4_conntrack_ops
,
463 ARRAY_SIZE(ipv4_conntrack_ops
));
465 printk("nf_conntrack_ipv4: can't register hooks.\n");
468 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
469 ret
= nf_conntrack_ipv4_compat_init();
474 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
476 nf_unregister_hooks(ipv4_conntrack_ops
, ARRAY_SIZE(ipv4_conntrack_ops
));
479 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4
);
481 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp
);
483 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4
);
485 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4
);
487 nf_unregister_sockopt(&so_getorigdst
);
491 static void __exit
nf_conntrack_l3proto_ipv4_fini(void)
494 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
495 nf_conntrack_ipv4_compat_fini();
497 nf_unregister_hooks(ipv4_conntrack_ops
, ARRAY_SIZE(ipv4_conntrack_ops
));
498 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4
);
499 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp
);
500 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4
);
501 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4
);
502 nf_unregister_sockopt(&so_getorigdst
);
505 module_init(nf_conntrack_l3proto_ipv4_init
);
506 module_exit(nf_conntrack_l3proto_ipv4_fini
);