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>
30 #define DEBUGP(format, args...)
33 static int ipv4_pkt_to_tuple(const struct sk_buff
*skb
, unsigned int nhoff
,
34 struct nf_conntrack_tuple
*tuple
)
36 __be32 _addrs
[2], *ap
;
37 ap
= skb_header_pointer(skb
, nhoff
+ offsetof(struct iphdr
, saddr
),
38 sizeof(u_int32_t
) * 2, _addrs
);
42 tuple
->src
.u3
.ip
= ap
[0];
43 tuple
->dst
.u3
.ip
= ap
[1];
48 static int ipv4_invert_tuple(struct nf_conntrack_tuple
*tuple
,
49 const struct nf_conntrack_tuple
*orig
)
51 tuple
->src
.u3
.ip
= orig
->dst
.u3
.ip
;
52 tuple
->dst
.u3
.ip
= orig
->src
.u3
.ip
;
57 static int ipv4_print_tuple(struct seq_file
*s
,
58 const struct nf_conntrack_tuple
*tuple
)
60 return seq_printf(s
, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
61 NIPQUAD(tuple
->src
.u3
.ip
),
62 NIPQUAD(tuple
->dst
.u3
.ip
));
65 static int ipv4_print_conntrack(struct seq_file
*s
,
66 const struct nf_conn
*conntrack
)
71 /* Returns new sk_buff, or NULL */
72 static struct sk_buff
*
73 nf_ct_ipv4_gather_frags(struct sk_buff
*skb
, u_int32_t user
)
78 skb
= ip_defrag(skb
, user
);
82 ip_send_check(ip_hdr(skb
));
88 ipv4_prepare(struct sk_buff
**pskb
, unsigned int hooknum
, unsigned int *dataoff
,
92 if (ip_hdr(*pskb
)->frag_off
& htons(IP_OFFSET
)) {
93 if (net_ratelimit()) {
94 printk(KERN_ERR
"ipv4_prepare: Frag of proto %u (hook=%u)\n",
95 ip_hdr(*pskb
)->protocol
, hooknum
);
100 *dataoff
= skb_network_offset(*pskb
) + ip_hdrlen(*pskb
);
101 *protonum
= ip_hdr(*pskb
)->protocol
;
106 static unsigned int ipv4_confirm(unsigned int hooknum
,
107 struct sk_buff
**pskb
,
108 const struct net_device
*in
,
109 const struct net_device
*out
,
110 int (*okfn
)(struct sk_buff
*))
112 /* We've seen it coming out the other side: confirm it */
113 return nf_conntrack_confirm(pskb
);
116 static unsigned int ipv4_conntrack_help(unsigned int hooknum
,
117 struct sk_buff
**pskb
,
118 const struct net_device
*in
,
119 const struct net_device
*out
,
120 int (*okfn
)(struct sk_buff
*))
123 enum ip_conntrack_info ctinfo
;
124 struct nf_conn_help
*help
;
125 struct nf_conntrack_helper
*helper
;
127 /* This is where we call the helper: as the packet goes out. */
128 ct
= nf_ct_get(*pskb
, &ctinfo
);
129 if (!ct
|| ctinfo
== IP_CT_RELATED
+ IP_CT_IS_REPLY
)
132 help
= nfct_help(ct
);
135 /* rcu_read_lock()ed by nf_hook_slow */
136 helper
= rcu_dereference(help
->helper
);
139 return helper
->help(pskb
, skb_network_offset(*pskb
) + ip_hdrlen(*pskb
),
143 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum
,
144 struct sk_buff
**pskb
,
145 const struct net_device
*in
,
146 const struct net_device
*out
,
147 int (*okfn
)(struct sk_buff
*))
149 /* Previously seen (loopback)? Ignore. Do this before
154 /* Gather fragments. */
155 if (ip_hdr(*pskb
)->frag_off
& htons(IP_MF
| IP_OFFSET
)) {
156 *pskb
= nf_ct_ipv4_gather_frags(*pskb
,
157 hooknum
== NF_IP_PRE_ROUTING
?
158 IP_DEFRAG_CONNTRACK_IN
:
159 IP_DEFRAG_CONNTRACK_OUT
);
166 static unsigned int ipv4_conntrack_in(unsigned int hooknum
,
167 struct sk_buff
**pskb
,
168 const struct net_device
*in
,
169 const struct net_device
*out
,
170 int (*okfn
)(struct sk_buff
*))
172 return nf_conntrack_in(PF_INET
, hooknum
, pskb
);
175 static unsigned int ipv4_conntrack_local(unsigned int hooknum
,
176 struct sk_buff
**pskb
,
177 const struct net_device
*in
,
178 const struct net_device
*out
,
179 int (*okfn
)(struct sk_buff
*))
181 /* root is playing with raw sockets. */
182 if ((*pskb
)->len
< sizeof(struct iphdr
)
183 || ip_hdrlen(*pskb
) < sizeof(struct iphdr
)) {
185 printk("ipt_hook: happy cracking.\n");
188 return nf_conntrack_in(PF_INET
, hooknum
, pskb
);
191 /* Connection tracking may drop packets, but never alters them, so
192 make it the first hook. */
193 static struct nf_hook_ops ipv4_conntrack_ops
[] = {
195 .hook
= ipv4_conntrack_defrag
,
196 .owner
= THIS_MODULE
,
198 .hooknum
= NF_IP_PRE_ROUTING
,
199 .priority
= NF_IP_PRI_CONNTRACK_DEFRAG
,
202 .hook
= ipv4_conntrack_in
,
203 .owner
= THIS_MODULE
,
205 .hooknum
= NF_IP_PRE_ROUTING
,
206 .priority
= NF_IP_PRI_CONNTRACK
,
209 .hook
= ipv4_conntrack_defrag
,
210 .owner
= THIS_MODULE
,
212 .hooknum
= NF_IP_LOCAL_OUT
,
213 .priority
= NF_IP_PRI_CONNTRACK_DEFRAG
,
216 .hook
= ipv4_conntrack_local
,
217 .owner
= THIS_MODULE
,
219 .hooknum
= NF_IP_LOCAL_OUT
,
220 .priority
= NF_IP_PRI_CONNTRACK
,
223 .hook
= ipv4_conntrack_help
,
224 .owner
= THIS_MODULE
,
226 .hooknum
= NF_IP_POST_ROUTING
,
227 .priority
= NF_IP_PRI_CONNTRACK_HELPER
,
230 .hook
= ipv4_conntrack_help
,
231 .owner
= THIS_MODULE
,
233 .hooknum
= NF_IP_LOCAL_IN
,
234 .priority
= NF_IP_PRI_CONNTRACK_HELPER
,
237 .hook
= ipv4_confirm
,
238 .owner
= THIS_MODULE
,
240 .hooknum
= NF_IP_POST_ROUTING
,
241 .priority
= NF_IP_PRI_CONNTRACK_CONFIRM
,
244 .hook
= ipv4_confirm
,
245 .owner
= THIS_MODULE
,
247 .hooknum
= NF_IP_LOCAL_IN
,
248 .priority
= NF_IP_PRI_CONNTRACK_CONFIRM
,
252 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
253 static int log_invalid_proto_min
= 0;
254 static int log_invalid_proto_max
= 255;
256 static ctl_table ip_ct_sysctl_table
[] = {
258 .ctl_name
= NET_IPV4_NF_CONNTRACK_MAX
,
259 .procname
= "ip_conntrack_max",
260 .data
= &nf_conntrack_max
,
261 .maxlen
= sizeof(int),
263 .proc_handler
= &proc_dointvec
,
266 .ctl_name
= NET_IPV4_NF_CONNTRACK_COUNT
,
267 .procname
= "ip_conntrack_count",
268 .data
= &nf_conntrack_count
,
269 .maxlen
= sizeof(int),
271 .proc_handler
= &proc_dointvec
,
274 .ctl_name
= NET_IPV4_NF_CONNTRACK_BUCKETS
,
275 .procname
= "ip_conntrack_buckets",
276 .data
= &nf_conntrack_htable_size
,
277 .maxlen
= sizeof(unsigned int),
279 .proc_handler
= &proc_dointvec
,
282 .ctl_name
= NET_IPV4_NF_CONNTRACK_CHECKSUM
,
283 .procname
= "ip_conntrack_checksum",
284 .data
= &nf_conntrack_checksum
,
285 .maxlen
= sizeof(int),
287 .proc_handler
= &proc_dointvec
,
290 .ctl_name
= NET_IPV4_NF_CONNTRACK_LOG_INVALID
,
291 .procname
= "ip_conntrack_log_invalid",
292 .data
= &nf_ct_log_invalid
,
293 .maxlen
= sizeof(unsigned int),
295 .proc_handler
= &proc_dointvec_minmax
,
296 .strategy
= &sysctl_intvec
,
297 .extra1
= &log_invalid_proto_min
,
298 .extra2
= &log_invalid_proto_max
,
304 #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
306 /* Fast function for those who don't want to parse /proc (and I don't
308 /* Reversing the socket's dst/src point of view gives us the reply
311 getorigdst(struct sock
*sk
, int optval
, void __user
*user
, int *len
)
313 struct inet_sock
*inet
= inet_sk(sk
);
314 struct nf_conntrack_tuple_hash
*h
;
315 struct nf_conntrack_tuple tuple
;
317 NF_CT_TUPLE_U_BLANK(&tuple
);
318 tuple
.src
.u3
.ip
= inet
->rcv_saddr
;
319 tuple
.src
.u
.tcp
.port
= inet
->sport
;
320 tuple
.dst
.u3
.ip
= inet
->daddr
;
321 tuple
.dst
.u
.tcp
.port
= inet
->dport
;
322 tuple
.src
.l3num
= PF_INET
;
323 tuple
.dst
.protonum
= IPPROTO_TCP
;
325 /* We only do TCP at the moment: is there a better way? */
326 if (strcmp(sk
->sk_prot
->name
, "TCP")) {
327 DEBUGP("SO_ORIGINAL_DST: Not a TCP socket\n");
331 if ((unsigned int) *len
< sizeof(struct sockaddr_in
)) {
332 DEBUGP("SO_ORIGINAL_DST: len %u not %u\n",
333 *len
, sizeof(struct sockaddr_in
));
337 h
= nf_conntrack_find_get(&tuple
, NULL
);
339 struct sockaddr_in sin
;
340 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
342 sin
.sin_family
= AF_INET
;
343 sin
.sin_port
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
]
344 .tuple
.dst
.u
.tcp
.port
;
345 sin
.sin_addr
.s_addr
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
]
347 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
349 DEBUGP("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
350 NIPQUAD(sin
.sin_addr
.s_addr
), ntohs(sin
.sin_port
));
352 if (copy_to_user(user
, &sin
, sizeof(sin
)) != 0)
357 DEBUGP("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
358 NIPQUAD(tuple
.src
.u3
.ip
), ntohs(tuple
.src
.u
.tcp
.port
),
359 NIPQUAD(tuple
.dst
.u3
.ip
), ntohs(tuple
.dst
.u
.tcp
.port
));
363 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
365 #include <linux/netfilter/nfnetlink.h>
366 #include <linux/netfilter/nfnetlink_conntrack.h>
368 static int ipv4_tuple_to_nfattr(struct sk_buff
*skb
,
369 const struct nf_conntrack_tuple
*tuple
)
371 NFA_PUT(skb
, CTA_IP_V4_SRC
, sizeof(u_int32_t
),
373 NFA_PUT(skb
, CTA_IP_V4_DST
, sizeof(u_int32_t
),
381 static const size_t cta_min_ip
[CTA_IP_MAX
] = {
382 [CTA_IP_V4_SRC
-1] = sizeof(u_int32_t
),
383 [CTA_IP_V4_DST
-1] = sizeof(u_int32_t
),
386 static int ipv4_nfattr_to_tuple(struct nfattr
*tb
[],
387 struct nf_conntrack_tuple
*t
)
389 if (!tb
[CTA_IP_V4_SRC
-1] || !tb
[CTA_IP_V4_DST
-1])
392 if (nfattr_bad_size(tb
, CTA_IP_MAX
, cta_min_ip
))
395 t
->src
.u3
.ip
= *(__be32
*)NFA_DATA(tb
[CTA_IP_V4_SRC
-1]);
396 t
->dst
.u3
.ip
= *(__be32
*)NFA_DATA(tb
[CTA_IP_V4_DST
-1]);
402 static struct nf_sockopt_ops so_getorigdst
= {
404 .get_optmin
= SO_ORIGINAL_DST
,
405 .get_optmax
= SO_ORIGINAL_DST
+1,
409 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4
= {
412 .pkt_to_tuple
= ipv4_pkt_to_tuple
,
413 .invert_tuple
= ipv4_invert_tuple
,
414 .print_tuple
= ipv4_print_tuple
,
415 .print_conntrack
= ipv4_print_conntrack
,
416 .prepare
= ipv4_prepare
,
417 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
418 .tuple_to_nfattr
= ipv4_tuple_to_nfattr
,
419 .nfattr_to_tuple
= ipv4_nfattr_to_tuple
,
421 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
422 .ctl_table_path
= nf_net_ipv4_netfilter_sysctl_path
,
423 .ctl_table
= ip_ct_sysctl_table
,
428 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET
));
429 MODULE_ALIAS("ip_conntrack");
430 MODULE_LICENSE("GPL");
432 static int __init
nf_conntrack_l3proto_ipv4_init(void)
438 ret
= nf_register_sockopt(&so_getorigdst
);
440 printk(KERN_ERR
"Unable to register netfilter socket option\n");
444 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4
);
446 printk("nf_conntrack_ipv4: can't register tcp.\n");
447 goto cleanup_sockopt
;
450 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4
);
452 printk("nf_conntrack_ipv4: can't register udp.\n");
456 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp
);
458 printk("nf_conntrack_ipv4: can't register icmp.\n");
462 ret
= nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4
);
464 printk("nf_conntrack_ipv4: can't register ipv4\n");
468 ret
= nf_register_hooks(ipv4_conntrack_ops
,
469 ARRAY_SIZE(ipv4_conntrack_ops
));
471 printk("nf_conntrack_ipv4: can't register hooks.\n");
474 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
475 ret
= nf_conntrack_ipv4_compat_init();
480 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
482 nf_unregister_hooks(ipv4_conntrack_ops
, ARRAY_SIZE(ipv4_conntrack_ops
));
485 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4
);
487 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp
);
489 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4
);
491 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4
);
493 nf_unregister_sockopt(&so_getorigdst
);
497 static void __exit
nf_conntrack_l3proto_ipv4_fini(void)
500 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
501 nf_conntrack_ipv4_compat_fini();
503 nf_unregister_hooks(ipv4_conntrack_ops
, ARRAY_SIZE(ipv4_conntrack_ops
));
504 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4
);
505 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp
);
506 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4
);
507 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4
);
508 nf_unregister_sockopt(&so_getorigdst
);
511 module_init(nf_conntrack_l3proto_ipv4_init
);
512 module_exit(nf_conntrack_l3proto_ipv4_fini
);