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 int nf_nat_module_is_loaded = 0;
107 EXPORT_SYMBOL_GPL(nf_nat_module_is_loaded);
109 static u_int32_t ipv4_get_features(const struct nf_conntrack_tuple *tuple)
111 if (nf_nat_module_is_loaded)
114 return NF_CT_F_BASIC;
117 static unsigned int ipv4_confirm(unsigned int hooknum,
118 struct sk_buff **pskb,
119 const struct net_device *in,
120 const struct net_device *out,
121 int (*okfn)(struct sk_buff *))
123 /* We've seen it coming out the other side: confirm it */
124 return nf_conntrack_confirm(pskb);
127 static unsigned int ipv4_conntrack_help(unsigned int hooknum,
128 struct sk_buff **pskb,
129 const struct net_device *in,
130 const struct net_device *out,
131 int (*okfn)(struct sk_buff *))
134 enum ip_conntrack_info ctinfo;
135 struct nf_conn_help *help;
137 /* This is where we call the helper: as the packet goes out. */
138 ct = nf_ct_get(*pskb, &ctinfo);
139 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
142 help = nfct_help(ct);
143 if (!help || !help->helper)
146 return help->helper->help(pskb,
147 skb_network_offset(*pskb) + ip_hdrlen(*pskb),
151 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
152 struct sk_buff **pskb,
153 const struct net_device *in,
154 const struct net_device *out,
155 int (*okfn)(struct sk_buff *))
157 /* Previously seen (loopback)? Ignore. Do this before
162 /* Gather fragments. */
163 if (ip_hdr(*pskb)->frag_off & htons(IP_MF | IP_OFFSET)) {
164 *pskb = nf_ct_ipv4_gather_frags(*pskb,
165 hooknum == NF_IP_PRE_ROUTING ?
166 IP_DEFRAG_CONNTRACK_IN :
167 IP_DEFRAG_CONNTRACK_OUT);
174 static unsigned int ipv4_conntrack_in(unsigned int hooknum,
175 struct sk_buff **pskb,
176 const struct net_device *in,
177 const struct net_device *out,
178 int (*okfn)(struct sk_buff *))
180 return nf_conntrack_in(PF_INET, hooknum, pskb);
183 static unsigned int ipv4_conntrack_local(unsigned int hooknum,
184 struct sk_buff **pskb,
185 const struct net_device *in,
186 const struct net_device *out,
187 int (*okfn)(struct sk_buff *))
189 /* root is playing with raw sockets. */
190 if ((*pskb)->len < sizeof(struct iphdr)
191 || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
193 printk("ipt_hook: happy cracking.\n");
196 return nf_conntrack_in(PF_INET, hooknum, pskb);
199 /* Connection tracking may drop packets, but never alters them, so
200 make it the first hook. */
201 static struct nf_hook_ops ipv4_conntrack_ops[] = {
203 .hook = ipv4_conntrack_defrag,
204 .owner = THIS_MODULE,
206 .hooknum = NF_IP_PRE_ROUTING,
207 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
210 .hook = ipv4_conntrack_in,
211 .owner = THIS_MODULE,
213 .hooknum = NF_IP_PRE_ROUTING,
214 .priority = NF_IP_PRI_CONNTRACK,
217 .hook = ipv4_conntrack_defrag,
218 .owner = THIS_MODULE,
220 .hooknum = NF_IP_LOCAL_OUT,
221 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
224 .hook = ipv4_conntrack_local,
225 .owner = THIS_MODULE,
227 .hooknum = NF_IP_LOCAL_OUT,
228 .priority = NF_IP_PRI_CONNTRACK,
231 .hook = ipv4_conntrack_help,
232 .owner = THIS_MODULE,
234 .hooknum = NF_IP_POST_ROUTING,
235 .priority = NF_IP_PRI_CONNTRACK_HELPER,
238 .hook = ipv4_conntrack_help,
239 .owner = THIS_MODULE,
241 .hooknum = NF_IP_LOCAL_IN,
242 .priority = NF_IP_PRI_CONNTRACK_HELPER,
245 .hook = ipv4_confirm,
246 .owner = THIS_MODULE,
248 .hooknum = NF_IP_POST_ROUTING,
249 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
252 .hook = ipv4_confirm,
253 .owner = THIS_MODULE,
255 .hooknum = NF_IP_LOCAL_IN,
256 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
260 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
261 static int log_invalid_proto_min = 0;
262 static int log_invalid_proto_max = 255;
264 static ctl_table ip_ct_sysctl_table[] = {
266 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
267 .procname = "ip_conntrack_max",
268 .data = &nf_conntrack_max,
269 .maxlen = sizeof(int),
271 .proc_handler = &proc_dointvec,
274 .ctl_name = NET_IPV4_NF_CONNTRACK_COUNT,
275 .procname = "ip_conntrack_count",
276 .data = &nf_conntrack_count,
277 .maxlen = sizeof(int),
279 .proc_handler = &proc_dointvec,
282 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
283 .procname = "ip_conntrack_buckets",
284 .data = &nf_conntrack_htable_size,
285 .maxlen = sizeof(unsigned int),
287 .proc_handler = &proc_dointvec,
290 .ctl_name = NET_IPV4_NF_CONNTRACK_CHECKSUM,
291 .procname = "ip_conntrack_checksum",
292 .data = &nf_conntrack_checksum,
293 .maxlen = sizeof(int),
295 .proc_handler = &proc_dointvec,
298 .ctl_name = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
299 .procname = "ip_conntrack_log_invalid",
300 .data = &nf_ct_log_invalid,
301 .maxlen = sizeof(unsigned int),
303 .proc_handler = &proc_dointvec_minmax,
304 .strategy = &sysctl_intvec,
305 .extra1 = &log_invalid_proto_min,
306 .extra2 = &log_invalid_proto_max,
312 #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
314 /* Fast function for those who don't want to parse /proc (and I don't
316 /* Reversing the socket's dst/src point of view gives us the reply
319 getorigdst(struct sock *sk, int optval, void __user *user, int *len)
321 struct inet_sock *inet = inet_sk(sk);
322 struct nf_conntrack_tuple_hash *h;
323 struct nf_conntrack_tuple tuple;
325 NF_CT_TUPLE_U_BLANK(&tuple);
326 tuple.src.u3.ip = inet->rcv_saddr;
327 tuple.src.u.tcp.port = inet->sport;
328 tuple.dst.u3.ip = inet->daddr;
329 tuple.dst.u.tcp.port = inet->dport;
330 tuple.src.l3num = PF_INET;
331 tuple.dst.protonum = IPPROTO_TCP;
333 /* We only do TCP at the moment: is there a better way? */
334 if (strcmp(sk->sk_prot->name, "TCP")) {
335 DEBUGP("SO_ORIGINAL_DST: Not a TCP socket\n");
339 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
340 DEBUGP("SO_ORIGINAL_DST: len %u not %u\n",
341 *len, sizeof(struct sockaddr_in));
345 h = nf_conntrack_find_get(&tuple, NULL);
347 struct sockaddr_in sin;
348 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
350 sin.sin_family = AF_INET;
351 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
352 .tuple.dst.u.tcp.port;
353 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
355 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
357 DEBUGP("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
358 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
360 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
365 DEBUGP("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
366 NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
367 NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
371 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
373 #include <linux/netfilter/nfnetlink.h>
374 #include <linux/netfilter/nfnetlink_conntrack.h>
376 static int ipv4_tuple_to_nfattr(struct sk_buff *skb,
377 const struct nf_conntrack_tuple *tuple)
379 NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t),
381 NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t),
389 static const size_t cta_min_ip[CTA_IP_MAX] = {
390 [CTA_IP_V4_SRC-1] = sizeof(u_int32_t),
391 [CTA_IP_V4_DST-1] = sizeof(u_int32_t),
394 static int ipv4_nfattr_to_tuple(struct nfattr *tb[],
395 struct nf_conntrack_tuple *t)
397 if (!tb[CTA_IP_V4_SRC-1] || !tb[CTA_IP_V4_DST-1])
400 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
403 t->src.u3.ip = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
404 t->dst.u3.ip = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
410 static struct nf_sockopt_ops so_getorigdst = {
412 .get_optmin = SO_ORIGINAL_DST,
413 .get_optmax = SO_ORIGINAL_DST+1,
417 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 = {
420 .pkt_to_tuple = ipv4_pkt_to_tuple,
421 .invert_tuple = ipv4_invert_tuple,
422 .print_tuple = ipv4_print_tuple,
423 .print_conntrack = ipv4_print_conntrack,
424 .prepare = ipv4_prepare,
425 .get_features = ipv4_get_features,
426 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
427 .tuple_to_nfattr = ipv4_tuple_to_nfattr,
428 .nfattr_to_tuple = ipv4_nfattr_to_tuple,
430 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
431 .ctl_table_path = nf_net_ipv4_netfilter_sysctl_path,
432 .ctl_table = ip_ct_sysctl_table,
437 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
438 MODULE_ALIAS("ip_conntrack");
439 MODULE_LICENSE("GPL");
441 static int __init nf_conntrack_l3proto_ipv4_init(void)
447 ret = nf_register_sockopt(&so_getorigdst);
449 printk(KERN_ERR "Unable to register netfilter socket option\n");
453 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4);
455 printk("nf_conntrack_ipv4: can't register tcp.\n");
456 goto cleanup_sockopt;
459 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4);
461 printk("nf_conntrack_ipv4: can't register udp.\n");
465 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp);
467 printk("nf_conntrack_ipv4: can't register icmp.\n");
471 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4);
473 printk("nf_conntrack_ipv4: can't register ipv4\n");
477 ret = nf_register_hooks(ipv4_conntrack_ops,
478 ARRAY_SIZE(ipv4_conntrack_ops));
480 printk("nf_conntrack_ipv4: can't register hooks.\n");
483 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
484 ret = nf_conntrack_ipv4_compat_init();
489 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
491 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
494 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
496 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
498 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
500 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
502 nf_unregister_sockopt(&so_getorigdst);
506 static void __exit nf_conntrack_l3proto_ipv4_fini(void)
509 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
510 nf_conntrack_ipv4_compat_fini();
512 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
513 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
514 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
515 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
516 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
517 nf_unregister_sockopt(&so_getorigdst);
520 module_init(nf_conntrack_l3proto_ipv4_init);
521 module_exit(nf_conntrack_l3proto_ipv4_fini);