1 /* This file contains all the functions required for the standalone
4 These are not required by the compatibility layer.
7 /* (C) 1999-2001 Paul `Rusty' Russell
8 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/config.h>
16 #include <linux/types.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <linux/module.h>
21 #include <linux/skbuff.h>
22 #include <linux/proc_fs.h>
24 #include <linux/sysctl.h>
26 #include <net/checksum.h>
29 #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_conntrack_lock)
30 #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_conntrack_lock)
32 #include <linux/netfilter_ipv4/ip_conntrack.h>
33 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
34 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
35 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
36 #include <linux/netfilter_ipv4/listhelp.h>
41 #define DEBUGP(format, args...)
44 MODULE_LICENSE("GPL");
46 static int kill_proto(const struct ip_conntrack *i, void *data)
48 return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum ==
49 *((u_int8_t *) data));
53 print_tuple(char *buffer, const struct ip_conntrack_tuple *tuple,
54 struct ip_conntrack_protocol *proto)
58 len = sprintf(buffer, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
59 NIPQUAD(tuple->src.ip), NIPQUAD(tuple->dst.ip));
61 len += proto->print_tuple(buffer + len, tuple);
66 /* FIXME: Don't print source proto part. --RR */
68 print_expect(char *buffer, const struct ip_conntrack_expect *expect)
72 if (expect->expectant->helper->timeout)
73 len = sprintf(buffer, "EXPECTING: %lu ",
74 timer_pending(&expect->timeout)
75 ? (expect->timeout.expires - jiffies)/HZ : 0);
77 len = sprintf(buffer, "EXPECTING: - ");
78 len += sprintf(buffer + len, "use=%u proto=%u ",
79 atomic_read(&expect->use), expect->tuple.dst.protonum);
80 len += print_tuple(buffer + len, &expect->tuple,
81 __ip_ct_find_proto(expect->tuple.dst.protonum));
82 len += sprintf(buffer + len, "\n");
87 print_conntrack(char *buffer, struct ip_conntrack *conntrack)
90 struct ip_conntrack_protocol *proto
91 = __ip_ct_find_proto(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
94 len = sprintf(buffer, "%-8s %u %lu ",
96 conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
98 timer_pending(&conntrack->timeout)
99 ? (conntrack->timeout.expires - jiffies)/HZ : 0);
101 len += proto->print_conntrack(buffer + len, conntrack);
102 len += print_tuple(buffer + len,
103 &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
105 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
106 len += sprintf(buffer + len, "[UNREPLIED] ");
107 len += print_tuple(buffer + len,
108 &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
110 if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
111 len += sprintf(buffer + len, "[ASSURED] ");
112 len += sprintf(buffer + len, "use=%u ",
113 atomic_read(&conntrack->ct_general.use));
114 len += sprintf(buffer + len, "xid=%d ",
116 len += sprintf(buffer + len, "\n");
121 /* Returns true when finished. */
123 conntrack_iterate(const struct ip_conntrack_tuple_hash *hash,
124 char *buffer, off_t offset, off_t *upto,
125 unsigned int *len, unsigned int maxlen)
128 IP_NF_ASSERT(hash->ctrack);
130 MUST_BE_READ_LOCKED(&ip_conntrack_lock);
132 /* Only count originals */
136 if ((*upto)++ < offset)
139 newlen = print_conntrack(buffer + *len, hash->ctrack);
140 if (*len + newlen > maxlen)
148 list_conntracks(char *buffer, char **start, off_t offset, int length)
151 unsigned int len = 0;
155 READ_LOCK(&ip_conntrack_lock);
156 /* Traverse hash; print originals then reply. */
157 for (i = 0; i < ip_conntrack_htable_size; i++) {
158 if (LIST_FIND(&ip_conntrack_hash[i], conntrack_iterate,
159 struct ip_conntrack_tuple_hash *,
160 buffer, offset, &upto, &len, length))
164 /* Now iterate through expecteds. */
165 READ_LOCK(&ip_conntrack_expect_tuple_lock);
166 list_for_each(e, &ip_conntrack_expect_list) {
167 unsigned int last_len;
168 struct ip_conntrack_expect *expect
169 = (struct ip_conntrack_expect *)e;
170 if (upto++ < offset) continue;
173 len += print_expect(buffer + len, expect);
176 goto finished_expects;
181 READ_UNLOCK(&ip_conntrack_expect_tuple_lock);
183 READ_UNLOCK(&ip_conntrack_lock);
185 /* `start' hack - see fs/proc/generic.c line ~165 */
186 *start = (char *)((unsigned int)upto - offset);
190 static unsigned int ip_confirm(unsigned int hooknum,
191 struct sk_buff **pskb,
192 const struct net_device *in,
193 const struct net_device *out,
194 int (*okfn)(struct sk_buff *))
196 /* We've seen it coming out the other side: confirm it */
197 return ip_conntrack_confirm(*pskb);
200 static unsigned int ip_conntrack_defrag(unsigned int hooknum,
201 struct sk_buff **pskb,
202 const struct net_device *in,
203 const struct net_device *out,
204 int (*okfn)(struct sk_buff *))
206 /* Previously seen (loopback)? Ignore. Do this before
211 /* Gather fragments. */
212 if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
213 *pskb = ip_ct_gather_frags(*pskb);
220 static unsigned int ip_refrag(unsigned int hooknum,
221 struct sk_buff **pskb,
222 const struct net_device *in,
223 const struct net_device *out,
224 int (*okfn)(struct sk_buff *))
226 struct rtable *rt = (struct rtable *)(*pskb)->dst;
228 /* We've seen it coming out the other side: confirm */
229 if (ip_confirm(hooknum, pskb, in, out, okfn) != NF_ACCEPT)
232 /* Local packets are never produced too large for their
233 interface. We degfragment them at LOCAL_OUT, however,
234 so we have to refragment them here. */
235 if ((*pskb)->len > dst_pmtu(&rt->u.dst) &&
236 !skb_shinfo(*pskb)->tso_size) {
237 /* No hook can be after us, so this should be OK. */
238 ip_fragment(*pskb, okfn);
244 static unsigned int ip_conntrack_local(unsigned int hooknum,
245 struct sk_buff **pskb,
246 const struct net_device *in,
247 const struct net_device *out,
248 int (*okfn)(struct sk_buff *))
250 /* root is playing with raw sockets. */
251 if ((*pskb)->len < sizeof(struct iphdr)
252 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
254 printk("ipt_hook: happy cracking.\n");
257 return ip_conntrack_in(hooknum, pskb, in, out, okfn);
260 /* Connection tracking may drop packets, but never alters them, so
261 make it the first hook. */
262 static struct nf_hook_ops ip_conntrack_defrag_ops = {
263 .hook = ip_conntrack_defrag,
264 .owner = THIS_MODULE,
266 .hooknum = NF_IP_PRE_ROUTING,
267 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
270 static struct nf_hook_ops ip_conntrack_in_ops = {
271 .hook = ip_conntrack_in,
272 .owner = THIS_MODULE,
274 .hooknum = NF_IP_PRE_ROUTING,
275 .priority = NF_IP_PRI_CONNTRACK,
278 static struct nf_hook_ops ip_conntrack_defrag_local_out_ops = {
279 .hook = ip_conntrack_defrag,
280 .owner = THIS_MODULE,
282 .hooknum = NF_IP_LOCAL_OUT,
283 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
286 static struct nf_hook_ops ip_conntrack_local_out_ops = {
287 .hook = ip_conntrack_local,
288 .owner = THIS_MODULE,
290 .hooknum = NF_IP_LOCAL_OUT,
291 .priority = NF_IP_PRI_CONNTRACK,
294 /* Refragmenter; last chance. */
295 static struct nf_hook_ops ip_conntrack_out_ops = {
297 .owner = THIS_MODULE,
299 .hooknum = NF_IP_POST_ROUTING,
300 .priority = NF_IP_PRI_LAST,
303 static struct nf_hook_ops ip_conntrack_local_in_ops = {
305 .owner = THIS_MODULE,
307 .hooknum = NF_IP_LOCAL_IN,
308 .priority = NF_IP_PRI_LAST-1,
315 /* From ip_conntrack_core.c */
316 extern int ip_conntrack_max;
317 extern unsigned int ip_conntrack_htable_size;
319 /* From ip_conntrack_proto_tcp.c */
320 extern unsigned long ip_ct_tcp_timeout_syn_sent;
321 extern unsigned long ip_ct_tcp_timeout_syn_recv;
322 extern unsigned long ip_ct_tcp_timeout_established;
323 extern unsigned long ip_ct_tcp_timeout_fin_wait;
324 extern unsigned long ip_ct_tcp_timeout_close_wait;
325 extern unsigned long ip_ct_tcp_timeout_last_ack;
326 extern unsigned long ip_ct_tcp_timeout_time_wait;
327 extern unsigned long ip_ct_tcp_timeout_close;
329 /* From ip_conntrack_proto_udp.c */
330 extern unsigned long ip_ct_udp_timeout;
331 extern unsigned long ip_ct_udp_timeout_stream;
333 /* From ip_conntrack_proto_icmp.c */
334 extern unsigned long ip_ct_icmp_timeout;
336 /* From ip_conntrack_proto_icmp.c */
337 extern unsigned long ip_ct_generic_timeout;
339 static struct ctl_table_header *ip_ct_sysctl_header;
341 static ctl_table ip_ct_sysctl_table[] = {
343 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
344 .procname = "ip_conntrack_max",
345 .data = &ip_conntrack_max,
346 .maxlen = sizeof(int),
348 .proc_handler = &proc_dointvec,
351 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
352 .procname = "ip_conntrack_buckets",
353 .data = &ip_conntrack_htable_size,
354 .maxlen = sizeof(unsigned int),
356 .proc_handler = &proc_dointvec,
359 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
360 .procname = "ip_conntrack_tcp_timeout_syn_sent",
361 .data = &ip_ct_tcp_timeout_syn_sent,
362 .maxlen = sizeof(unsigned int),
364 .proc_handler = &proc_dointvec_jiffies,
367 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
368 .procname = "ip_conntrack_tcp_timeout_syn_recv",
369 .data = &ip_ct_tcp_timeout_syn_recv,
370 .maxlen = sizeof(unsigned int),
372 .proc_handler = &proc_dointvec_jiffies,
375 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
376 .procname = "ip_conntrack_tcp_timeout_established",
377 .data = &ip_ct_tcp_timeout_established,
378 .maxlen = sizeof(unsigned int),
380 .proc_handler = &proc_dointvec_jiffies,
383 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
384 .procname = "ip_conntrack_tcp_timeout_fin_wait",
385 .data = &ip_ct_tcp_timeout_fin_wait,
386 .maxlen = sizeof(unsigned int),
388 .proc_handler = &proc_dointvec_jiffies,
391 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
392 .procname = "ip_conntrack_tcp_timeout_close_wait",
393 .data = &ip_ct_tcp_timeout_close_wait,
394 .maxlen = sizeof(unsigned int),
396 .proc_handler = &proc_dointvec_jiffies,
399 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
400 .procname = "ip_conntrack_tcp_timeout_last_ack",
401 .data = &ip_ct_tcp_timeout_last_ack,
402 .maxlen = sizeof(unsigned int),
404 .proc_handler = &proc_dointvec_jiffies,
407 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
408 .procname = "ip_conntrack_tcp_timeout_time_wait",
409 .data = &ip_ct_tcp_timeout_time_wait,
410 .maxlen = sizeof(unsigned int),
412 .proc_handler = &proc_dointvec_jiffies,
415 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
416 .procname = "ip_conntrack_tcp_timeout_close",
417 .data = &ip_ct_tcp_timeout_close,
418 .maxlen = sizeof(unsigned int),
420 .proc_handler = &proc_dointvec_jiffies,
423 .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT,
424 .procname = "ip_conntrack_udp_timeout",
425 .data = &ip_ct_udp_timeout,
426 .maxlen = sizeof(unsigned int),
428 .proc_handler = &proc_dointvec_jiffies,
431 .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
432 .procname = "ip_conntrack_udp_timeout_stream",
433 .data = &ip_ct_udp_timeout_stream,
434 .maxlen = sizeof(unsigned int),
436 .proc_handler = &proc_dointvec_jiffies,
439 .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
440 .procname = "ip_conntrack_icmp_timeout",
441 .data = &ip_ct_icmp_timeout,
442 .maxlen = sizeof(unsigned int),
444 .proc_handler = &proc_dointvec_jiffies,
447 .ctl_name = NET_IPV4_NF_CONNTRACK_GENERIC_TIMEOUT,
448 .procname = "ip_conntrack_generic_timeout",
449 .data = &ip_ct_generic_timeout,
450 .maxlen = sizeof(unsigned int),
452 .proc_handler = &proc_dointvec_jiffies,
457 #define NET_IP_CONNTRACK_MAX 2089
459 static ctl_table ip_ct_netfilter_table[] = {
461 .ctl_name = NET_IPV4_NETFILTER,
462 .procname = "netfilter",
464 .child = ip_ct_sysctl_table,
467 .ctl_name = NET_IP_CONNTRACK_MAX,
468 .procname = "ip_conntrack_max",
469 .data = &ip_conntrack_max,
470 .maxlen = sizeof(int),
472 .proc_handler = &proc_dointvec
477 static ctl_table ip_ct_ipv4_table[] = {
479 .ctl_name = NET_IPV4,
482 .child = ip_ct_netfilter_table,
487 static ctl_table ip_ct_net_table[] = {
492 .child = ip_ct_ipv4_table,
497 static int init_or_cleanup(int init)
499 struct proc_dir_entry *proc;
502 if (!init) goto cleanup;
504 ret = ip_conntrack_init();
506 goto cleanup_nothing;
508 proc = proc_net_create("ip_conntrack", 0440, list_conntracks);
509 if (!proc) goto cleanup_init;
510 proc->owner = THIS_MODULE;
512 ret = nf_register_hook(&ip_conntrack_defrag_ops);
514 printk("ip_conntrack: can't register pre-routing defrag hook.\n");
517 ret = nf_register_hook(&ip_conntrack_defrag_local_out_ops);
519 printk("ip_conntrack: can't register local_out defrag hook.\n");
520 goto cleanup_defragops;
522 ret = nf_register_hook(&ip_conntrack_in_ops);
524 printk("ip_conntrack: can't register pre-routing hook.\n");
525 goto cleanup_defraglocalops;
527 ret = nf_register_hook(&ip_conntrack_local_out_ops);
529 printk("ip_conntrack: can't register local out hook.\n");
532 ret = nf_register_hook(&ip_conntrack_out_ops);
534 printk("ip_conntrack: can't register post-routing hook.\n");
535 goto cleanup_inandlocalops;
537 ret = nf_register_hook(&ip_conntrack_local_in_ops);
539 printk("ip_conntrack: can't register local in hook.\n");
540 goto cleanup_inoutandlocalops;
543 ip_ct_sysctl_header = register_sysctl_table(ip_ct_net_table, 0);
544 if (ip_ct_sysctl_header == NULL) {
545 printk("ip_conntrack: can't register to sysctl.\n");
554 unregister_sysctl_table(ip_ct_sysctl_header);
556 nf_unregister_hook(&ip_conntrack_local_in_ops);
557 cleanup_inoutandlocalops:
558 nf_unregister_hook(&ip_conntrack_out_ops);
559 cleanup_inandlocalops:
560 nf_unregister_hook(&ip_conntrack_local_out_ops);
562 nf_unregister_hook(&ip_conntrack_in_ops);
563 cleanup_defraglocalops:
564 nf_unregister_hook(&ip_conntrack_defrag_local_out_ops);
566 nf_unregister_hook(&ip_conntrack_defrag_ops);
568 proc_net_remove("ip_conntrack");
570 ip_conntrack_cleanup();
575 /* FIXME: Allow NULL functions and sub in pointers to generic for
577 int ip_conntrack_protocol_register(struct ip_conntrack_protocol *proto)
582 WRITE_LOCK(&ip_conntrack_lock);
583 list_for_each(i, &protocol_list) {
584 if (((struct ip_conntrack_protocol *)i)->proto
591 list_prepend(&protocol_list, proto);
594 WRITE_UNLOCK(&ip_conntrack_lock);
598 void ip_conntrack_protocol_unregister(struct ip_conntrack_protocol *proto)
600 WRITE_LOCK(&ip_conntrack_lock);
602 /* ip_ct_find_proto() returns proto_generic in case there is no protocol
603 * helper. So this should be enough - HW */
604 LIST_DELETE(&protocol_list, proto);
605 WRITE_UNLOCK(&ip_conntrack_lock);
607 /* Somebody could be still looking at the proto in bh. */
610 /* Remove all contrack entries for this protocol */
611 ip_ct_selective_cleanup(kill_proto, &proto->proto);
614 static int __init init(void)
616 return init_or_cleanup(1);
619 static void __exit fini(void)
627 /* Some modules need us, but don't depend directly on any symbol.
628 They should call this. */
629 void need_ip_conntrack(void)
633 EXPORT_SYMBOL(ip_conntrack_protocol_register);
634 EXPORT_SYMBOL(ip_conntrack_protocol_unregister);
635 EXPORT_SYMBOL(invert_tuplepr);
636 EXPORT_SYMBOL(ip_conntrack_alter_reply);
637 EXPORT_SYMBOL(ip_conntrack_destroyed);
638 EXPORT_SYMBOL(ip_conntrack_get);
639 EXPORT_SYMBOL(need_ip_conntrack);
640 EXPORT_SYMBOL(ip_conntrack_helper_register);
641 EXPORT_SYMBOL(ip_conntrack_helper_unregister);
642 EXPORT_SYMBOL(ip_ct_selective_cleanup);
643 EXPORT_SYMBOL(ip_ct_refresh);
644 EXPORT_SYMBOL(ip_ct_find_proto);
645 EXPORT_SYMBOL(__ip_ct_find_proto);
646 EXPORT_SYMBOL(ip_ct_find_helper);
647 EXPORT_SYMBOL(ip_conntrack_expect_alloc);
648 EXPORT_SYMBOL(ip_conntrack_expect_related);
649 EXPORT_SYMBOL(ip_conntrack_change_expect);
650 EXPORT_SYMBOL(ip_conntrack_unexpect_related);
651 EXPORT_SYMBOL_GPL(ip_conntrack_expect_find_get);
652 EXPORT_SYMBOL_GPL(ip_conntrack_expect_put);
653 EXPORT_SYMBOL(ip_conntrack_tuple_taken);
654 EXPORT_SYMBOL(ip_ct_gather_frags);
655 EXPORT_SYMBOL(ip_conntrack_htable_size);
656 EXPORT_SYMBOL(ip_conntrack_expect_list);
657 EXPORT_SYMBOL(ip_conntrack_lock);
658 EXPORT_SYMBOL(ip_conntrack_hash);
659 EXPORT_SYMBOL(ip_conntrack_untracked);
660 EXPORT_SYMBOL_GPL(ip_conntrack_find_get);
661 EXPORT_SYMBOL_GPL(ip_conntrack_put);