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>
28 #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_conntrack_lock)
29 #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_conntrack_lock)
31 #include <linux/netfilter_ipv4/ip_conntrack.h>
32 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
33 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
34 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
35 #include <linux/netfilter_ipv4/listhelp.h>
40 #define DEBUGP(format, args...)
43 MODULE_LICENSE("GPL");
45 static int kill_proto(const struct ip_conntrack *i, void *data)
47 return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum ==
48 *((u_int8_t *) data));
52 print_tuple(char *buffer, const struct ip_conntrack_tuple *tuple,
53 struct ip_conntrack_protocol *proto)
57 len = sprintf(buffer, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
58 NIPQUAD(tuple->src.ip), NIPQUAD(tuple->dst.ip));
60 len += proto->print_tuple(buffer + len, tuple);
65 /* FIXME: Don't print source proto part. --RR */
67 print_expect(char *buffer, const struct ip_conntrack_expect *expect)
71 if (expect->expectant->helper->timeout)
72 len = sprintf(buffer, "EXPECTING: %lu ",
73 timer_pending(&expect->timeout)
74 ? (expect->timeout.expires - jiffies)/HZ : 0);
76 len = sprintf(buffer, "EXPECTING: - ");
77 len += sprintf(buffer + len, "use=%u proto=%u ",
78 atomic_read(&expect->use), expect->tuple.dst.protonum);
79 len += print_tuple(buffer + len, &expect->tuple,
80 __ip_ct_find_proto(expect->tuple.dst.protonum));
81 len += sprintf(buffer + len, "\n");
86 print_conntrack(char *buffer, struct ip_conntrack *conntrack)
89 struct ip_conntrack_protocol *proto
90 = __ip_ct_find_proto(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
93 len = sprintf(buffer, "%-8s %u %lu ",
95 conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
97 timer_pending(&conntrack->timeout)
98 ? (conntrack->timeout.expires - jiffies)/HZ : 0);
100 len += proto->print_conntrack(buffer + len, conntrack);
101 len += print_tuple(buffer + len,
102 &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
104 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
105 len += sprintf(buffer + len, "[UNREPLIED] ");
106 len += print_tuple(buffer + len,
107 &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
109 if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
110 len += sprintf(buffer + len, "[ASSURED] ");
111 len += sprintf(buffer + len, "use=%u ",
112 atomic_read(&conntrack->ct_general.use));
113 len += sprintf(buffer + len, "\n");
118 /* Returns true when finished. */
120 conntrack_iterate(const struct ip_conntrack_tuple_hash *hash,
121 char *buffer, off_t offset, off_t *upto,
122 unsigned int *len, unsigned int maxlen)
125 IP_NF_ASSERT(hash->ctrack);
127 MUST_BE_READ_LOCKED(&ip_conntrack_lock);
129 /* Only count originals */
133 if ((*upto)++ < offset)
136 newlen = print_conntrack(buffer + *len, hash->ctrack);
137 if (*len + newlen > maxlen)
145 list_conntracks(char *buffer, char **start, off_t offset, int length)
148 unsigned int len = 0;
152 READ_LOCK(&ip_conntrack_lock);
153 /* Traverse hash; print originals then reply. */
154 for (i = 0; i < ip_conntrack_htable_size; i++) {
155 if (LIST_FIND(&ip_conntrack_hash[i], conntrack_iterate,
156 struct ip_conntrack_tuple_hash *,
157 buffer, offset, &upto, &len, length))
161 /* Now iterate through expecteds. */
162 READ_LOCK(&ip_conntrack_expect_tuple_lock);
163 list_for_each(e, &ip_conntrack_expect_list) {
164 unsigned int last_len;
165 struct ip_conntrack_expect *expect
166 = (struct ip_conntrack_expect *)e;
167 if (upto++ < offset) continue;
170 len += print_expect(buffer + len, expect);
173 goto finished_expects;
178 READ_UNLOCK(&ip_conntrack_expect_tuple_lock);
180 READ_UNLOCK(&ip_conntrack_lock);
182 /* `start' hack - see fs/proc/generic.c line ~165 */
183 *start = (char *)((unsigned int)upto - offset);
187 static unsigned int ip_confirm(unsigned int hooknum,
188 struct sk_buff **pskb,
189 const struct net_device *in,
190 const struct net_device *out,
191 int (*okfn)(struct sk_buff *))
193 /* We've seen it coming out the other side: confirm it */
194 return ip_conntrack_confirm(*pskb);
197 static unsigned int ip_conntrack_defrag(unsigned int hooknum,
198 struct sk_buff **pskb,
199 const struct net_device *in,
200 const struct net_device *out,
201 int (*okfn)(struct sk_buff *))
203 /* Previously seen (loopback)? Ignore. Do this before
208 /* Gather fragments. */
209 if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
210 *pskb = ip_ct_gather_frags(*pskb);
217 static unsigned int ip_refrag(unsigned int hooknum,
218 struct sk_buff **pskb,
219 const struct net_device *in,
220 const struct net_device *out,
221 int (*okfn)(struct sk_buff *))
223 struct rtable *rt = (struct rtable *)(*pskb)->dst;
225 /* We've seen it coming out the other side: confirm */
226 if (ip_confirm(hooknum, pskb, in, out, okfn) != NF_ACCEPT)
229 /* Local packets are never produced too large for their
230 interface. We degfragment them at LOCAL_OUT, however,
231 so we have to refragment them here. */
232 if ((*pskb)->len > dst_pmtu(&rt->u.dst) &&
233 !skb_shinfo(*pskb)->tso_size) {
234 /* No hook can be after us, so this should be OK. */
235 ip_fragment(*pskb, okfn);
241 static unsigned int ip_conntrack_local(unsigned int hooknum,
242 struct sk_buff **pskb,
243 const struct net_device *in,
244 const struct net_device *out,
245 int (*okfn)(struct sk_buff *))
247 /* root is playing with raw sockets. */
248 if ((*pskb)->len < sizeof(struct iphdr)
249 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
251 printk("ipt_hook: happy cracking.\n");
254 return ip_conntrack_in(hooknum, pskb, in, out, okfn);
257 /* Connection tracking may drop packets, but never alters them, so
258 make it the first hook. */
259 static struct nf_hook_ops ip_conntrack_defrag_ops = {
260 .hook = ip_conntrack_defrag,
261 .owner = THIS_MODULE,
263 .hooknum = NF_IP_PRE_ROUTING,
264 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
267 static struct nf_hook_ops ip_conntrack_in_ops = {
268 .hook = ip_conntrack_in,
269 .owner = THIS_MODULE,
271 .hooknum = NF_IP_PRE_ROUTING,
272 .priority = NF_IP_PRI_CONNTRACK,
275 static struct nf_hook_ops ip_conntrack_defrag_local_out_ops = {
276 .hook = ip_conntrack_defrag,
277 .owner = THIS_MODULE,
279 .hooknum = NF_IP_LOCAL_OUT,
280 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
283 static struct nf_hook_ops ip_conntrack_local_out_ops = {
284 .hook = ip_conntrack_local,
285 .owner = THIS_MODULE,
287 .hooknum = NF_IP_LOCAL_OUT,
288 .priority = NF_IP_PRI_CONNTRACK,
291 /* Refragmenter; last chance. */
292 static struct nf_hook_ops ip_conntrack_out_ops = {
294 .owner = THIS_MODULE,
296 .hooknum = NF_IP_POST_ROUTING,
297 .priority = NF_IP_PRI_LAST,
300 static struct nf_hook_ops ip_conntrack_local_in_ops = {
302 .owner = THIS_MODULE,
304 .hooknum = NF_IP_LOCAL_IN,
305 .priority = NF_IP_PRI_LAST-1,
312 /* From ip_conntrack_core.c */
313 extern int ip_conntrack_max;
314 extern unsigned int ip_conntrack_htable_size;
316 /* From ip_conntrack_proto_tcp.c */
317 extern unsigned long ip_ct_tcp_timeout_syn_sent;
318 extern unsigned long ip_ct_tcp_timeout_syn_recv;
319 extern unsigned long ip_ct_tcp_timeout_established;
320 extern unsigned long ip_ct_tcp_timeout_fin_wait;
321 extern unsigned long ip_ct_tcp_timeout_close_wait;
322 extern unsigned long ip_ct_tcp_timeout_last_ack;
323 extern unsigned long ip_ct_tcp_timeout_time_wait;
324 extern unsigned long ip_ct_tcp_timeout_close;
326 /* From ip_conntrack_proto_udp.c */
327 extern unsigned long ip_ct_udp_timeout;
328 extern unsigned long ip_ct_udp_timeout_stream;
330 /* From ip_conntrack_proto_icmp.c */
331 extern unsigned long ip_ct_icmp_timeout;
333 /* From ip_conntrack_proto_icmp.c */
334 extern unsigned long ip_ct_generic_timeout;
336 static struct ctl_table_header *ip_ct_sysctl_header;
338 static ctl_table ip_ct_sysctl_table[] = {
340 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
341 .procname = "ip_conntrack_max",
342 .data = &ip_conntrack_max,
343 .maxlen = sizeof(int),
345 .proc_handler = &proc_dointvec,
348 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
349 .procname = "ip_conntrack_buckets",
350 .data = &ip_conntrack_htable_size,
351 .maxlen = sizeof(unsigned int),
353 .proc_handler = &proc_dointvec,
356 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
357 .procname = "ip_conntrack_tcp_timeout_syn_sent",
358 .data = &ip_ct_tcp_timeout_syn_sent,
359 .maxlen = sizeof(unsigned int),
361 .proc_handler = &proc_dointvec_jiffies,
364 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
365 .procname = "ip_conntrack_tcp_timeout_syn_recv",
366 .data = &ip_ct_tcp_timeout_syn_recv,
367 .maxlen = sizeof(unsigned int),
369 .proc_handler = &proc_dointvec_jiffies,
372 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
373 .procname = "ip_conntrack_tcp_timeout_established",
374 .data = &ip_ct_tcp_timeout_established,
375 .maxlen = sizeof(unsigned int),
377 .proc_handler = &proc_dointvec_jiffies,
380 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
381 .procname = "ip_conntrack_tcp_timeout_fin_wait",
382 .data = &ip_ct_tcp_timeout_fin_wait,
383 .maxlen = sizeof(unsigned int),
385 .proc_handler = &proc_dointvec_jiffies,
388 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
389 .procname = "ip_conntrack_tcp_timeout_close_wait",
390 .data = &ip_ct_tcp_timeout_close_wait,
391 .maxlen = sizeof(unsigned int),
393 .proc_handler = &proc_dointvec_jiffies,
396 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
397 .procname = "ip_conntrack_tcp_timeout_last_ack",
398 .data = &ip_ct_tcp_timeout_last_ack,
399 .maxlen = sizeof(unsigned int),
401 .proc_handler = &proc_dointvec_jiffies,
404 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
405 .procname = "ip_conntrack_tcp_timeout_time_wait",
406 .data = &ip_ct_tcp_timeout_time_wait,
407 .maxlen = sizeof(unsigned int),
409 .proc_handler = &proc_dointvec_jiffies,
412 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
413 .procname = "ip_conntrack_tcp_timeout_close",
414 .data = &ip_ct_tcp_timeout_close,
415 .maxlen = sizeof(unsigned int),
417 .proc_handler = &proc_dointvec_jiffies,
420 .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT,
421 .procname = "ip_conntrack_udp_timeout",
422 .data = &ip_ct_udp_timeout,
423 .maxlen = sizeof(unsigned int),
425 .proc_handler = &proc_dointvec_jiffies,
428 .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
429 .procname = "ip_conntrack_udp_timeout_stream",
430 .data = &ip_ct_udp_timeout_stream,
431 .maxlen = sizeof(unsigned int),
433 .proc_handler = &proc_dointvec_jiffies,
436 .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
437 .procname = "ip_conntrack_icmp_timeout",
438 .data = &ip_ct_icmp_timeout,
439 .maxlen = sizeof(unsigned int),
441 .proc_handler = &proc_dointvec_jiffies,
444 .ctl_name = NET_IPV4_NF_CONNTRACK_GENERIC_TIMEOUT,
445 .procname = "ip_conntrack_generic_timeout",
446 .data = &ip_ct_generic_timeout,
447 .maxlen = sizeof(unsigned int),
449 .proc_handler = &proc_dointvec_jiffies,
454 #define NET_IP_CONNTRACK_MAX 2089
456 static ctl_table ip_ct_netfilter_table[] = {
458 .ctl_name = NET_IPV4_NETFILTER,
459 .procname = "netfilter",
461 .child = ip_ct_sysctl_table,
464 .ctl_name = NET_IP_CONNTRACK_MAX,
465 .procname = "ip_conntrack_max",
466 .data = &ip_conntrack_max,
467 .maxlen = sizeof(int),
469 .proc_handler = &proc_dointvec
474 static ctl_table ip_ct_ipv4_table[] = {
476 .ctl_name = NET_IPV4,
479 .child = ip_ct_netfilter_table,
484 static ctl_table ip_ct_net_table[] = {
489 .child = ip_ct_ipv4_table,
494 static int init_or_cleanup(int init)
496 struct proc_dir_entry *proc;
499 if (!init) goto cleanup;
501 ret = ip_conntrack_init();
503 goto cleanup_nothing;
505 proc = proc_net_create("ip_conntrack",0,list_conntracks);
506 if (!proc) goto cleanup_init;
507 proc->owner = THIS_MODULE;
509 ret = nf_register_hook(&ip_conntrack_defrag_ops);
511 printk("ip_conntrack: can't register pre-routing defrag hook.\n");
514 ret = nf_register_hook(&ip_conntrack_defrag_local_out_ops);
516 printk("ip_conntrack: can't register local_out defrag hook.\n");
517 goto cleanup_defragops;
519 ret = nf_register_hook(&ip_conntrack_in_ops);
521 printk("ip_conntrack: can't register pre-routing hook.\n");
522 goto cleanup_defraglocalops;
524 ret = nf_register_hook(&ip_conntrack_local_out_ops);
526 printk("ip_conntrack: can't register local out hook.\n");
529 ret = nf_register_hook(&ip_conntrack_out_ops);
531 printk("ip_conntrack: can't register post-routing hook.\n");
532 goto cleanup_inandlocalops;
534 ret = nf_register_hook(&ip_conntrack_local_in_ops);
536 printk("ip_conntrack: can't register local in hook.\n");
537 goto cleanup_inoutandlocalops;
540 ip_ct_sysctl_header = register_sysctl_table(ip_ct_net_table, 0);
541 if (ip_ct_sysctl_header == NULL) {
542 printk("ip_conntrack: can't register to sysctl.\n");
551 unregister_sysctl_table(ip_ct_sysctl_header);
553 nf_unregister_hook(&ip_conntrack_local_in_ops);
554 cleanup_inoutandlocalops:
555 nf_unregister_hook(&ip_conntrack_out_ops);
556 cleanup_inandlocalops:
557 nf_unregister_hook(&ip_conntrack_local_out_ops);
559 nf_unregister_hook(&ip_conntrack_in_ops);
560 cleanup_defraglocalops:
561 nf_unregister_hook(&ip_conntrack_defrag_local_out_ops);
563 nf_unregister_hook(&ip_conntrack_defrag_ops);
565 proc_net_remove("ip_conntrack");
567 ip_conntrack_cleanup();
572 /* FIXME: Allow NULL functions and sub in pointers to generic for
574 int ip_conntrack_protocol_register(struct ip_conntrack_protocol *proto)
579 WRITE_LOCK(&ip_conntrack_lock);
580 list_for_each(i, &protocol_list) {
581 if (((struct ip_conntrack_protocol *)i)->proto
588 list_prepend(&protocol_list, proto);
591 WRITE_UNLOCK(&ip_conntrack_lock);
595 void ip_conntrack_protocol_unregister(struct ip_conntrack_protocol *proto)
597 WRITE_LOCK(&ip_conntrack_lock);
599 /* ip_ct_find_proto() returns proto_generic in case there is no protocol
600 * helper. So this should be enough - HW */
601 LIST_DELETE(&protocol_list, proto);
602 WRITE_UNLOCK(&ip_conntrack_lock);
604 /* Somebody could be still looking at the proto in bh. */
607 /* Remove all contrack entries for this protocol */
608 ip_ct_selective_cleanup(kill_proto, &proto->proto);
611 static int __init init(void)
613 return init_or_cleanup(1);
616 static void __exit fini(void)
624 /* Some modules need us, but don't depend directly on any symbol.
625 They should call this. */
626 void need_ip_conntrack(void)
630 EXPORT_SYMBOL(ip_conntrack_protocol_register);
631 EXPORT_SYMBOL(ip_conntrack_protocol_unregister);
632 EXPORT_SYMBOL(invert_tuplepr);
633 EXPORT_SYMBOL(ip_conntrack_alter_reply);
634 EXPORT_SYMBOL(ip_conntrack_destroyed);
635 EXPORT_SYMBOL(ip_conntrack_get);
636 EXPORT_SYMBOL(need_ip_conntrack);
637 EXPORT_SYMBOL(ip_conntrack_helper_register);
638 EXPORT_SYMBOL(ip_conntrack_helper_unregister);
639 EXPORT_SYMBOL(ip_ct_selective_cleanup);
640 EXPORT_SYMBOL(ip_ct_refresh);
641 EXPORT_SYMBOL(ip_ct_find_proto);
642 EXPORT_SYMBOL(__ip_ct_find_proto);
643 EXPORT_SYMBOL(ip_ct_find_helper);
644 EXPORT_SYMBOL(ip_conntrack_expect_alloc);
645 EXPORT_SYMBOL(ip_conntrack_expect_related);
646 EXPORT_SYMBOL(ip_conntrack_change_expect);
647 EXPORT_SYMBOL(ip_conntrack_unexpect_related);
648 EXPORT_SYMBOL_GPL(ip_conntrack_expect_find_get);
649 EXPORT_SYMBOL_GPL(ip_conntrack_expect_put);
650 EXPORT_SYMBOL(ip_conntrack_tuple_taken);
651 EXPORT_SYMBOL(ip_ct_gather_frags);
652 EXPORT_SYMBOL(ip_conntrack_htable_size);
653 EXPORT_SYMBOL(ip_conntrack_expect_list);
654 EXPORT_SYMBOL(ip_conntrack_lock);
655 EXPORT_SYMBOL(ip_conntrack_hash);
656 EXPORT_SYMBOL(ip_conntrack_untracked);
657 EXPORT_SYMBOL_GPL(ip_conntrack_find_get);
658 EXPORT_SYMBOL_GPL(ip_conntrack_put);