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, "\n");
119 /* Returns true when finished. */
121 conntrack_iterate(const struct ip_conntrack_tuple_hash *hash,
122 char *buffer, off_t offset, off_t *upto,
123 unsigned int *len, unsigned int maxlen)
126 IP_NF_ASSERT(hash->ctrack);
128 MUST_BE_READ_LOCKED(&ip_conntrack_lock);
130 /* Only count originals */
134 if ((*upto)++ < offset)
137 newlen = print_conntrack(buffer + *len, hash->ctrack);
138 if (*len + newlen > maxlen)
146 list_conntracks(char *buffer, char **start, off_t offset, int length)
149 unsigned int len = 0;
153 READ_LOCK(&ip_conntrack_lock);
154 /* Traverse hash; print originals then reply. */
155 for (i = 0; i < ip_conntrack_htable_size; i++) {
156 if (LIST_FIND(&ip_conntrack_hash[i], conntrack_iterate,
157 struct ip_conntrack_tuple_hash *,
158 buffer, offset, &upto, &len, length))
162 /* Now iterate through expecteds. */
163 READ_LOCK(&ip_conntrack_expect_tuple_lock);
164 list_for_each(e, &ip_conntrack_expect_list) {
165 unsigned int last_len;
166 struct ip_conntrack_expect *expect
167 = (struct ip_conntrack_expect *)e;
168 if (upto++ < offset) continue;
171 len += print_expect(buffer + len, expect);
174 goto finished_expects;
179 READ_UNLOCK(&ip_conntrack_expect_tuple_lock);
181 READ_UNLOCK(&ip_conntrack_lock);
183 /* `start' hack - see fs/proc/generic.c line ~165 */
184 *start = (char *)((unsigned int)upto - offset);
188 static unsigned int ip_confirm(unsigned int hooknum,
189 struct sk_buff **pskb,
190 const struct net_device *in,
191 const struct net_device *out,
192 int (*okfn)(struct sk_buff *))
194 /* We've seen it coming out the other side: confirm it */
195 return ip_conntrack_confirm(*pskb);
198 static unsigned int ip_conntrack_defrag(unsigned int hooknum,
199 struct sk_buff **pskb,
200 const struct net_device *in,
201 const struct net_device *out,
202 int (*okfn)(struct sk_buff *))
204 /* Previously seen (loopback)? Ignore. Do this before
209 /* Gather fragments. */
210 if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
211 *pskb = ip_ct_gather_frags(*pskb);
218 static unsigned int ip_refrag(unsigned int hooknum,
219 struct sk_buff **pskb,
220 const struct net_device *in,
221 const struct net_device *out,
222 int (*okfn)(struct sk_buff *))
224 struct rtable *rt = (struct rtable *)(*pskb)->dst;
226 /* We've seen it coming out the other side: confirm */
227 if (ip_confirm(hooknum, pskb, in, out, okfn) != NF_ACCEPT)
230 /* Local packets are never produced too large for their
231 interface. We degfragment them at LOCAL_OUT, however,
232 so we have to refragment them here. */
233 if ((*pskb)->len > dst_pmtu(&rt->u.dst) &&
234 !skb_shinfo(*pskb)->tso_size) {
235 /* No hook can be after us, so this should be OK. */
236 ip_fragment(*pskb, okfn);
242 static unsigned int ip_conntrack_local(unsigned int hooknum,
243 struct sk_buff **pskb,
244 const struct net_device *in,
245 const struct net_device *out,
246 int (*okfn)(struct sk_buff *))
248 /* root is playing with raw sockets. */
249 if ((*pskb)->len < sizeof(struct iphdr)
250 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
252 printk("ipt_hook: happy cracking.\n");
255 return ip_conntrack_in(hooknum, pskb, in, out, okfn);
258 /* Connection tracking may drop packets, but never alters them, so
259 make it the first hook. */
260 static struct nf_hook_ops ip_conntrack_defrag_ops = {
261 .hook = ip_conntrack_defrag,
262 .owner = THIS_MODULE,
264 .hooknum = NF_IP_PRE_ROUTING,
265 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
268 static struct nf_hook_ops ip_conntrack_in_ops = {
269 .hook = ip_conntrack_in,
270 .owner = THIS_MODULE,
272 .hooknum = NF_IP_PRE_ROUTING,
273 .priority = NF_IP_PRI_CONNTRACK,
276 static struct nf_hook_ops ip_conntrack_defrag_local_out_ops = {
277 .hook = ip_conntrack_defrag,
278 .owner = THIS_MODULE,
280 .hooknum = NF_IP_LOCAL_OUT,
281 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
284 static struct nf_hook_ops ip_conntrack_local_out_ops = {
285 .hook = ip_conntrack_local,
286 .owner = THIS_MODULE,
288 .hooknum = NF_IP_LOCAL_OUT,
289 .priority = NF_IP_PRI_CONNTRACK,
292 /* Refragmenter; last chance. */
293 static struct nf_hook_ops ip_conntrack_out_ops = {
295 .owner = THIS_MODULE,
297 .hooknum = NF_IP_POST_ROUTING,
298 .priority = NF_IP_PRI_LAST,
301 static struct nf_hook_ops ip_conntrack_local_in_ops = {
303 .owner = THIS_MODULE,
305 .hooknum = NF_IP_LOCAL_IN,
306 .priority = NF_IP_PRI_LAST-1,
313 /* From ip_conntrack_core.c */
314 extern int ip_conntrack_max;
315 extern unsigned int ip_conntrack_htable_size;
317 /* From ip_conntrack_proto_tcp.c */
318 extern unsigned long ip_ct_tcp_timeout_syn_sent;
319 extern unsigned long ip_ct_tcp_timeout_syn_recv;
320 extern unsigned long ip_ct_tcp_timeout_established;
321 extern unsigned long ip_ct_tcp_timeout_fin_wait;
322 extern unsigned long ip_ct_tcp_timeout_close_wait;
323 extern unsigned long ip_ct_tcp_timeout_last_ack;
324 extern unsigned long ip_ct_tcp_timeout_time_wait;
325 extern unsigned long ip_ct_tcp_timeout_close;
327 /* From ip_conntrack_proto_udp.c */
328 extern unsigned long ip_ct_udp_timeout;
329 extern unsigned long ip_ct_udp_timeout_stream;
331 /* From ip_conntrack_proto_icmp.c */
332 extern unsigned long ip_ct_icmp_timeout;
334 /* From ip_conntrack_proto_icmp.c */
335 extern unsigned long ip_ct_generic_timeout;
337 static struct ctl_table_header *ip_ct_sysctl_header;
339 static ctl_table ip_ct_sysctl_table[] = {
341 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
342 .procname = "ip_conntrack_max",
343 .data = &ip_conntrack_max,
344 .maxlen = sizeof(int),
346 .proc_handler = &proc_dointvec,
349 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
350 .procname = "ip_conntrack_buckets",
351 .data = &ip_conntrack_htable_size,
352 .maxlen = sizeof(unsigned int),
354 .proc_handler = &proc_dointvec,
357 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
358 .procname = "ip_conntrack_tcp_timeout_syn_sent",
359 .data = &ip_ct_tcp_timeout_syn_sent,
360 .maxlen = sizeof(unsigned int),
362 .proc_handler = &proc_dointvec_jiffies,
365 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
366 .procname = "ip_conntrack_tcp_timeout_syn_recv",
367 .data = &ip_ct_tcp_timeout_syn_recv,
368 .maxlen = sizeof(unsigned int),
370 .proc_handler = &proc_dointvec_jiffies,
373 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
374 .procname = "ip_conntrack_tcp_timeout_established",
375 .data = &ip_ct_tcp_timeout_established,
376 .maxlen = sizeof(unsigned int),
378 .proc_handler = &proc_dointvec_jiffies,
381 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
382 .procname = "ip_conntrack_tcp_timeout_fin_wait",
383 .data = &ip_ct_tcp_timeout_fin_wait,
384 .maxlen = sizeof(unsigned int),
386 .proc_handler = &proc_dointvec_jiffies,
389 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
390 .procname = "ip_conntrack_tcp_timeout_close_wait",
391 .data = &ip_ct_tcp_timeout_close_wait,
392 .maxlen = sizeof(unsigned int),
394 .proc_handler = &proc_dointvec_jiffies,
397 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
398 .procname = "ip_conntrack_tcp_timeout_last_ack",
399 .data = &ip_ct_tcp_timeout_last_ack,
400 .maxlen = sizeof(unsigned int),
402 .proc_handler = &proc_dointvec_jiffies,
405 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
406 .procname = "ip_conntrack_tcp_timeout_time_wait",
407 .data = &ip_ct_tcp_timeout_time_wait,
408 .maxlen = sizeof(unsigned int),
410 .proc_handler = &proc_dointvec_jiffies,
413 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
414 .procname = "ip_conntrack_tcp_timeout_close",
415 .data = &ip_ct_tcp_timeout_close,
416 .maxlen = sizeof(unsigned int),
418 .proc_handler = &proc_dointvec_jiffies,
421 .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT,
422 .procname = "ip_conntrack_udp_timeout",
423 .data = &ip_ct_udp_timeout,
424 .maxlen = sizeof(unsigned int),
426 .proc_handler = &proc_dointvec_jiffies,
429 .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
430 .procname = "ip_conntrack_udp_timeout_stream",
431 .data = &ip_ct_udp_timeout_stream,
432 .maxlen = sizeof(unsigned int),
434 .proc_handler = &proc_dointvec_jiffies,
437 .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
438 .procname = "ip_conntrack_icmp_timeout",
439 .data = &ip_ct_icmp_timeout,
440 .maxlen = sizeof(unsigned int),
442 .proc_handler = &proc_dointvec_jiffies,
445 .ctl_name = NET_IPV4_NF_CONNTRACK_GENERIC_TIMEOUT,
446 .procname = "ip_conntrack_generic_timeout",
447 .data = &ip_ct_generic_timeout,
448 .maxlen = sizeof(unsigned int),
450 .proc_handler = &proc_dointvec_jiffies,
455 #define NET_IP_CONNTRACK_MAX 2089
457 static ctl_table ip_ct_netfilter_table[] = {
459 .ctl_name = NET_IPV4_NETFILTER,
460 .procname = "netfilter",
462 .child = ip_ct_sysctl_table,
465 .ctl_name = NET_IP_CONNTRACK_MAX,
466 .procname = "ip_conntrack_max",
467 .data = &ip_conntrack_max,
468 .maxlen = sizeof(int),
470 .proc_handler = &proc_dointvec
475 static ctl_table ip_ct_ipv4_table[] = {
477 .ctl_name = NET_IPV4,
480 .child = ip_ct_netfilter_table,
485 static ctl_table ip_ct_net_table[] = {
490 .child = ip_ct_ipv4_table,
495 static int init_or_cleanup(int init)
497 struct proc_dir_entry *proc;
500 if (!init) goto cleanup;
502 ret = ip_conntrack_init();
504 goto cleanup_nothing;
506 proc = proc_net_create("ip_conntrack", 0440, list_conntracks);
507 if (!proc) goto cleanup_init;
508 proc->owner = THIS_MODULE;
510 ret = nf_register_hook(&ip_conntrack_defrag_ops);
512 printk("ip_conntrack: can't register pre-routing defrag hook.\n");
515 ret = nf_register_hook(&ip_conntrack_defrag_local_out_ops);
517 printk("ip_conntrack: can't register local_out defrag hook.\n");
518 goto cleanup_defragops;
520 ret = nf_register_hook(&ip_conntrack_in_ops);
522 printk("ip_conntrack: can't register pre-routing hook.\n");
523 goto cleanup_defraglocalops;
525 ret = nf_register_hook(&ip_conntrack_local_out_ops);
527 printk("ip_conntrack: can't register local out hook.\n");
530 ret = nf_register_hook(&ip_conntrack_out_ops);
532 printk("ip_conntrack: can't register post-routing hook.\n");
533 goto cleanup_inandlocalops;
535 ret = nf_register_hook(&ip_conntrack_local_in_ops);
537 printk("ip_conntrack: can't register local in hook.\n");
538 goto cleanup_inoutandlocalops;
541 ip_ct_sysctl_header = register_sysctl_table(ip_ct_net_table, 0);
542 if (ip_ct_sysctl_header == NULL) {
543 printk("ip_conntrack: can't register to sysctl.\n");
552 unregister_sysctl_table(ip_ct_sysctl_header);
554 nf_unregister_hook(&ip_conntrack_local_in_ops);
555 cleanup_inoutandlocalops:
556 nf_unregister_hook(&ip_conntrack_out_ops);
557 cleanup_inandlocalops:
558 nf_unregister_hook(&ip_conntrack_local_out_ops);
560 nf_unregister_hook(&ip_conntrack_in_ops);
561 cleanup_defraglocalops:
562 nf_unregister_hook(&ip_conntrack_defrag_local_out_ops);
564 nf_unregister_hook(&ip_conntrack_defrag_ops);
566 proc_net_remove("ip_conntrack");
568 ip_conntrack_cleanup();
573 /* FIXME: Allow NULL functions and sub in pointers to generic for
575 int ip_conntrack_protocol_register(struct ip_conntrack_protocol *proto)
580 WRITE_LOCK(&ip_conntrack_lock);
581 list_for_each(i, &protocol_list) {
582 if (((struct ip_conntrack_protocol *)i)->proto
589 list_prepend(&protocol_list, proto);
592 WRITE_UNLOCK(&ip_conntrack_lock);
596 void ip_conntrack_protocol_unregister(struct ip_conntrack_protocol *proto)
598 WRITE_LOCK(&ip_conntrack_lock);
600 /* ip_ct_find_proto() returns proto_generic in case there is no protocol
601 * helper. So this should be enough - HW */
602 LIST_DELETE(&protocol_list, proto);
603 WRITE_UNLOCK(&ip_conntrack_lock);
605 /* Somebody could be still looking at the proto in bh. */
608 /* Remove all contrack entries for this protocol */
609 ip_ct_selective_cleanup(kill_proto, &proto->proto);
612 static int __init init(void)
614 return init_or_cleanup(1);
617 static void __exit fini(void)
625 /* Some modules need us, but don't depend directly on any symbol.
626 They should call this. */
627 void need_ip_conntrack(void)
631 EXPORT_SYMBOL(ip_conntrack_protocol_register);
632 EXPORT_SYMBOL(ip_conntrack_protocol_unregister);
633 EXPORT_SYMBOL(invert_tuplepr);
634 EXPORT_SYMBOL(ip_conntrack_alter_reply);
635 EXPORT_SYMBOL(ip_conntrack_destroyed);
636 EXPORT_SYMBOL(ip_conntrack_get);
637 EXPORT_SYMBOL(need_ip_conntrack);
638 EXPORT_SYMBOL(ip_conntrack_helper_register);
639 EXPORT_SYMBOL(ip_conntrack_helper_unregister);
640 EXPORT_SYMBOL(ip_ct_selective_cleanup);
641 EXPORT_SYMBOL(ip_ct_refresh);
642 EXPORT_SYMBOL(ip_ct_find_proto);
643 EXPORT_SYMBOL(__ip_ct_find_proto);
644 EXPORT_SYMBOL(ip_ct_find_helper);
645 EXPORT_SYMBOL(ip_conntrack_expect_alloc);
646 EXPORT_SYMBOL(ip_conntrack_expect_related);
647 EXPORT_SYMBOL(ip_conntrack_change_expect);
648 EXPORT_SYMBOL(ip_conntrack_unexpect_related);
649 EXPORT_SYMBOL_GPL(ip_conntrack_expect_find_get);
650 EXPORT_SYMBOL_GPL(ip_conntrack_expect_put);
651 EXPORT_SYMBOL(ip_conntrack_tuple_taken);
652 EXPORT_SYMBOL(ip_ct_gather_frags);
653 EXPORT_SYMBOL(ip_conntrack_htable_size);
654 EXPORT_SYMBOL(ip_conntrack_expect_list);
655 EXPORT_SYMBOL(ip_conntrack_lock);
656 EXPORT_SYMBOL(ip_conntrack_hash);
657 EXPORT_SYMBOL(ip_conntrack_untracked);
658 EXPORT_SYMBOL_GPL(ip_conntrack_find_get);
659 EXPORT_SYMBOL_GPL(ip_conntrack_put);