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.
8 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9 * - move L3 protocol dependent part to this file.
10 * 23 Mar 2004: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
11 * - add get_features() to support various size of conntrack
14 * Derived from net/ipv4/netfilter/ip_conntrack_standalone.c
17 #include <linux/config.h>
18 #include <linux/types.h>
20 #include <linux/netfilter.h>
21 #include <linux/module.h>
22 #include <linux/skbuff.h>
23 #include <linux/icmp.h>
24 #include <linux/sysctl.h>
27 #include <linux/netfilter_ipv4.h>
28 #include <net/netfilter/nf_conntrack.h>
29 #include <net/netfilter/nf_conntrack_helper.h>
30 #include <net/netfilter/nf_conntrack_protocol.h>
31 #include <net/netfilter/nf_conntrack_l3proto.h>
32 #include <net/netfilter/nf_conntrack_core.h>
33 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
38 #define DEBUGP(format, args...)
41 DECLARE_PER_CPU(struct nf_conntrack_stat, nf_conntrack_stat);
43 static int ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
44 struct nf_conntrack_tuple *tuple)
46 u_int32_t _addrs[2], *ap;
47 ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
48 sizeof(u_int32_t) * 2, _addrs);
52 tuple->src.u3.ip = ap[0];
53 tuple->dst.u3.ip = ap[1];
58 static int ipv4_invert_tuple(struct nf_conntrack_tuple *tuple,
59 const struct nf_conntrack_tuple *orig)
61 tuple->src.u3.ip = orig->dst.u3.ip;
62 tuple->dst.u3.ip = orig->src.u3.ip;
67 static int ipv4_print_tuple(struct seq_file *s,
68 const struct nf_conntrack_tuple *tuple)
70 return seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
71 NIPQUAD(tuple->src.u3.ip),
72 NIPQUAD(tuple->dst.u3.ip));
75 static int ipv4_print_conntrack(struct seq_file *s,
76 const struct nf_conn *conntrack)
81 /* Returns new sk_buff, or NULL */
82 static struct sk_buff *
83 nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)
88 skb = ip_defrag(skb, user);
92 ip_send_check(skb->nh.iph);
98 ipv4_prepare(struct sk_buff **pskb, unsigned int hooknum, unsigned int *dataoff,
102 if ((*pskb)->nh.iph->frag_off & htons(IP_OFFSET)) {
103 if (net_ratelimit()) {
104 printk(KERN_ERR "ipv4_prepare: Frag of proto %u (hook=%u)\n",
105 (*pskb)->nh.iph->protocol, hooknum);
110 *dataoff = (*pskb)->nh.raw - (*pskb)->data + (*pskb)->nh.iph->ihl*4;
111 *protonum = (*pskb)->nh.iph->protocol;
116 int nat_module_is_loaded = 0;
117 static u_int32_t ipv4_get_features(const struct nf_conntrack_tuple *tuple)
119 if (nat_module_is_loaded)
122 return NF_CT_F_BASIC;
125 static unsigned int ipv4_confirm(unsigned int hooknum,
126 struct sk_buff **pskb,
127 const struct net_device *in,
128 const struct net_device *out,
129 int (*okfn)(struct sk_buff *))
131 /* We've seen it coming out the other side: confirm it */
132 return nf_conntrack_confirm(pskb);
135 static unsigned int ipv4_conntrack_help(unsigned int hooknum,
136 struct sk_buff **pskb,
137 const struct net_device *in,
138 const struct net_device *out,
139 int (*okfn)(struct sk_buff *))
142 enum ip_conntrack_info ctinfo;
144 /* This is where we call the helper: as the packet goes out. */
145 ct = nf_ct_get(*pskb, &ctinfo);
146 if (ct && ct->helper) {
148 ret = ct->helper->help(pskb,
149 (*pskb)->nh.raw - (*pskb)->data
150 + (*pskb)->nh.iph->ihl*4,
152 if (ret != NF_ACCEPT)
158 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
159 struct sk_buff **pskb,
160 const struct net_device *in,
161 const struct net_device *out,
162 int (*okfn)(struct sk_buff *))
164 #if !defined(CONFIG_IP_NF_NAT) && !defined(CONFIG_IP_NF_NAT_MODULE)
165 /* Previously seen (loopback)? Ignore. Do this before
171 /* Gather fragments. */
172 if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
173 *pskb = nf_ct_ipv4_gather_frags(*pskb,
174 hooknum == NF_IP_PRE_ROUTING ?
175 IP_DEFRAG_CONNTRACK_IN :
176 IP_DEFRAG_CONNTRACK_OUT);
183 static unsigned int ipv4_refrag(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 struct rtable *rt = (struct rtable *)(*pskb)->dst;
191 /* We've seen it coming out the other side: confirm */
192 if (ipv4_confirm(hooknum, pskb, in, out, okfn) != NF_ACCEPT)
195 /* Local packets are never produced too large for their
196 interface. We degfragment them at LOCAL_OUT, however,
197 so we have to refragment them here. */
198 if ((*pskb)->len > dst_mtu(&rt->u.dst) &&
199 !skb_shinfo(*pskb)->tso_size) {
200 /* No hook can be after us, so this should be OK. */
201 ip_fragment(*pskb, okfn);
207 static unsigned int ipv4_conntrack_in(unsigned int hooknum,
208 struct sk_buff **pskb,
209 const struct net_device *in,
210 const struct net_device *out,
211 int (*okfn)(struct sk_buff *))
213 return nf_conntrack_in(PF_INET, hooknum, pskb);
216 static unsigned int ipv4_conntrack_local(unsigned int hooknum,
217 struct sk_buff **pskb,
218 const struct net_device *in,
219 const struct net_device *out,
220 int (*okfn)(struct sk_buff *))
222 /* root is playing with raw sockets. */
223 if ((*pskb)->len < sizeof(struct iphdr)
224 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
226 printk("ipt_hook: happy cracking.\n");
229 return nf_conntrack_in(PF_INET, hooknum, pskb);
232 /* Connection tracking may drop packets, but never alters them, so
233 make it the first hook. */
234 static struct nf_hook_ops ipv4_conntrack_defrag_ops = {
235 .hook = ipv4_conntrack_defrag,
236 .owner = THIS_MODULE,
238 .hooknum = NF_IP_PRE_ROUTING,
239 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
242 static struct nf_hook_ops ipv4_conntrack_in_ops = {
243 .hook = ipv4_conntrack_in,
244 .owner = THIS_MODULE,
246 .hooknum = NF_IP_PRE_ROUTING,
247 .priority = NF_IP_PRI_CONNTRACK,
250 static struct nf_hook_ops ipv4_conntrack_defrag_local_out_ops = {
251 .hook = ipv4_conntrack_defrag,
252 .owner = THIS_MODULE,
254 .hooknum = NF_IP_LOCAL_OUT,
255 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
258 static struct nf_hook_ops ipv4_conntrack_local_out_ops = {
259 .hook = ipv4_conntrack_local,
260 .owner = THIS_MODULE,
262 .hooknum = NF_IP_LOCAL_OUT,
263 .priority = NF_IP_PRI_CONNTRACK,
267 static struct nf_hook_ops ipv4_conntrack_helper_out_ops = {
268 .hook = ipv4_conntrack_help,
269 .owner = THIS_MODULE,
271 .hooknum = NF_IP_POST_ROUTING,
272 .priority = NF_IP_PRI_CONNTRACK_HELPER,
275 static struct nf_hook_ops ipv4_conntrack_helper_in_ops = {
276 .hook = ipv4_conntrack_help,
277 .owner = THIS_MODULE,
279 .hooknum = NF_IP_LOCAL_IN,
280 .priority = NF_IP_PRI_CONNTRACK_HELPER,
284 /* Refragmenter; last chance. */
285 static struct nf_hook_ops ipv4_conntrack_out_ops = {
287 .owner = THIS_MODULE,
289 .hooknum = NF_IP_POST_ROUTING,
290 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
293 static struct nf_hook_ops ipv4_conntrack_local_in_ops = {
294 .hook = ipv4_confirm,
295 .owner = THIS_MODULE,
297 .hooknum = NF_IP_LOCAL_IN,
298 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
302 /* From nf_conntrack_proto_icmp.c */
303 extern unsigned long nf_ct_icmp_timeout;
304 static struct ctl_table_header *nf_ct_ipv4_sysctl_header;
306 static ctl_table nf_ct_sysctl_table[] = {
308 .ctl_name = NET_NF_CONNTRACK_ICMP_TIMEOUT,
309 .procname = "nf_conntrack_icmp_timeout",
310 .data = &nf_ct_icmp_timeout,
311 .maxlen = sizeof(unsigned int),
313 .proc_handler = &proc_dointvec_jiffies,
318 static ctl_table nf_ct_netfilter_table[] = {
320 .ctl_name = NET_NETFILTER,
321 .procname = "netfilter",
323 .child = nf_ct_sysctl_table,
328 static ctl_table nf_ct_net_table[] = {
333 .child = nf_ct_netfilter_table,
339 /* Fast function for those who don't want to parse /proc (and I don't
341 /* Reversing the socket's dst/src point of view gives us the reply
344 getorigdst(struct sock *sk, int optval, void __user *user, int *len)
346 struct inet_sock *inet = inet_sk(sk);
347 struct nf_conntrack_tuple_hash *h;
348 struct nf_conntrack_tuple tuple;
350 NF_CT_TUPLE_U_BLANK(&tuple);
351 tuple.src.u3.ip = inet->rcv_saddr;
352 tuple.src.u.tcp.port = inet->sport;
353 tuple.dst.u3.ip = inet->daddr;
354 tuple.dst.u.tcp.port = inet->dport;
355 tuple.src.l3num = PF_INET;
356 tuple.dst.protonum = IPPROTO_TCP;
358 /* We only do TCP at the moment: is there a better way? */
359 if (strcmp(sk->sk_prot->name, "TCP")) {
360 DEBUGP("SO_ORIGINAL_DST: Not a TCP socket\n");
364 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
365 DEBUGP("SO_ORIGINAL_DST: len %u not %u\n",
366 *len, sizeof(struct sockaddr_in));
370 h = nf_conntrack_find_get(&tuple, NULL);
372 struct sockaddr_in sin;
373 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
375 sin.sin_family = AF_INET;
376 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
377 .tuple.dst.u.tcp.port;
378 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
381 DEBUGP("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
382 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
384 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
389 DEBUGP("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
390 NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
391 NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
395 #if defined(CONFIG_NF_CT_NETLINK) || \
396 defined(CONFIG_NF_CT_NETLINK_MODULE)
398 #include <linux/netfilter/nfnetlink.h>
399 #include <linux/netfilter/nfnetlink_conntrack.h>
401 static int ipv4_tuple_to_nfattr(struct sk_buff *skb,
402 const struct nf_conntrack_tuple *tuple)
404 NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t),
406 NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t),
414 static const size_t cta_min_ip[CTA_IP_MAX] = {
415 [CTA_IP_V4_SRC-1] = sizeof(u_int32_t),
416 [CTA_IP_V4_DST-1] = sizeof(u_int32_t),
419 static int ipv4_nfattr_to_tuple(struct nfattr *tb[],
420 struct nf_conntrack_tuple *t)
422 if (!tb[CTA_IP_V4_SRC-1] || !tb[CTA_IP_V4_DST-1])
425 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
429 *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
431 *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
437 static struct nf_sockopt_ops so_getorigdst = {
439 .get_optmin = SO_ORIGINAL_DST,
440 .get_optmax = SO_ORIGINAL_DST+1,
444 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 = {
447 .pkt_to_tuple = ipv4_pkt_to_tuple,
448 .invert_tuple = ipv4_invert_tuple,
449 .print_tuple = ipv4_print_tuple,
450 .print_conntrack = ipv4_print_conntrack,
451 .prepare = ipv4_prepare,
452 .get_features = ipv4_get_features,
453 #if defined(CONFIG_NF_CT_NETLINK) || \
454 defined(CONFIG_NF_CT_NETLINK_MODULE)
455 .tuple_to_nfattr = ipv4_tuple_to_nfattr,
456 .nfattr_to_tuple = ipv4_nfattr_to_tuple,
461 extern struct nf_conntrack_protocol nf_conntrack_protocol_tcp4;
462 extern struct nf_conntrack_protocol nf_conntrack_protocol_udp4;
463 extern struct nf_conntrack_protocol nf_conntrack_protocol_icmp;
464 static int init_or_cleanup(int init)
468 if (!init) goto cleanup;
470 ret = nf_register_sockopt(&so_getorigdst);
472 printk(KERN_ERR "Unable to register netfilter socket option\n");
473 goto cleanup_nothing;
476 ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_tcp4);
478 printk("nf_conntrack_ipv4: can't register tcp.\n");
479 goto cleanup_sockopt;
482 ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_udp4);
484 printk("nf_conntrack_ipv4: can't register udp.\n");
488 ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_icmp);
490 printk("nf_conntrack_ipv4: can't register icmp.\n");
494 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4);
496 printk("nf_conntrack_ipv4: can't register ipv4\n");
500 ret = nf_register_hook(&ipv4_conntrack_defrag_ops);
502 printk("nf_conntrack_ipv4: can't register pre-routing defrag hook.\n");
505 ret = nf_register_hook(&ipv4_conntrack_defrag_local_out_ops);
507 printk("nf_conntrack_ipv4: can't register local_out defrag hook.\n");
508 goto cleanup_defragops;
511 ret = nf_register_hook(&ipv4_conntrack_in_ops);
513 printk("nf_conntrack_ipv4: can't register pre-routing hook.\n");
514 goto cleanup_defraglocalops;
517 ret = nf_register_hook(&ipv4_conntrack_local_out_ops);
519 printk("nf_conntrack_ipv4: can't register local out hook.\n");
523 ret = nf_register_hook(&ipv4_conntrack_helper_in_ops);
525 printk("nf_conntrack_ipv4: can't register local helper hook.\n");
526 goto cleanup_inandlocalops;
529 ret = nf_register_hook(&ipv4_conntrack_helper_out_ops);
531 printk("nf_conntrack_ipv4: can't register postrouting helper hook.\n");
532 goto cleanup_helperinops;
535 ret = nf_register_hook(&ipv4_conntrack_out_ops);
537 printk("nf_conntrack_ipv4: can't register post-routing hook.\n");
538 goto cleanup_helperoutops;
541 ret = nf_register_hook(&ipv4_conntrack_local_in_ops);
543 printk("nf_conntrack_ipv4: can't register local in hook.\n");
544 goto cleanup_inoutandlocalops;
548 nf_ct_ipv4_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
549 if (nf_ct_ipv4_sysctl_header == NULL) {
550 printk("nf_conntrack: can't register to sysctl.\n");
552 goto cleanup_localinops;
556 /* For use by REJECT target */
557 ip_ct_attach = __nf_conntrack_attach;
565 unregister_sysctl_table(nf_ct_ipv4_sysctl_header);
568 nf_unregister_hook(&ipv4_conntrack_local_in_ops);
569 cleanup_inoutandlocalops:
570 nf_unregister_hook(&ipv4_conntrack_out_ops);
571 cleanup_helperoutops:
572 nf_unregister_hook(&ipv4_conntrack_helper_out_ops);
574 nf_unregister_hook(&ipv4_conntrack_helper_in_ops);
575 cleanup_inandlocalops:
576 nf_unregister_hook(&ipv4_conntrack_local_out_ops);
578 nf_unregister_hook(&ipv4_conntrack_in_ops);
579 cleanup_defraglocalops:
580 nf_unregister_hook(&ipv4_conntrack_defrag_local_out_ops);
582 nf_unregister_hook(&ipv4_conntrack_defrag_ops);
584 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
586 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_icmp);
588 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_udp4);
590 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_tcp4);
592 nf_unregister_sockopt(&so_getorigdst);
597 MODULE_LICENSE("GPL");
599 static int __init init(void)
602 return init_or_cleanup(1);
605 static void __exit fini(void)
613 void need_ip_conntrack(void)
617 EXPORT_SYMBOL(need_ip_conntrack);
618 EXPORT_SYMBOL(nf_ct_ipv4_gather_frags);