]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - net/ipv4/netfilter/ip_conntrack_standalone.c
[INET_SOCK]: Move struct inet_sock & helper functions to net/inet_sock.h
[mirror_ubuntu-hirsute-kernel.git] / net / ipv4 / netfilter / ip_conntrack_standalone.c
1 /* This file contains all the functions required for the standalone
2 ip_conntrack module.
3
4 These are not required by the compatibility layer.
5 */
6
7 /* (C) 1999-2001 Paul `Rusty' Russell
8 * (C) 2002-2005 Netfilter Core Team <coreteam@netfilter.org>
9 *
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.
13 */
14
15 #include <linux/config.h>
16 #include <linux/types.h>
17 #include <linux/ip.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>
23 #include <linux/seq_file.h>
24 #include <linux/percpu.h>
25 #ifdef CONFIG_SYSCTL
26 #include <linux/sysctl.h>
27 #endif
28 #include <net/checksum.h>
29 #include <net/ip.h>
30 #include <net/route.h>
31
32 #define ASSERT_READ_LOCK(x)
33 #define ASSERT_WRITE_LOCK(x)
34
35 #include <linux/netfilter_ipv4/ip_conntrack.h>
36 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
37 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
38 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
39 #include <linux/netfilter_ipv4/listhelp.h>
40
41 #if 0
42 #define DEBUGP printk
43 #else
44 #define DEBUGP(format, args...)
45 #endif
46
47 MODULE_LICENSE("GPL");
48
49 extern atomic_t ip_conntrack_count;
50 DECLARE_PER_CPU(struct ip_conntrack_stat, ip_conntrack_stat);
51
52 static int kill_proto(struct ip_conntrack *i, void *data)
53 {
54 return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum ==
55 *((u_int8_t *) data));
56 }
57
58 #ifdef CONFIG_PROC_FS
59 static int
60 print_tuple(struct seq_file *s, const struct ip_conntrack_tuple *tuple,
61 struct ip_conntrack_protocol *proto)
62 {
63 seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
64 NIPQUAD(tuple->src.ip), NIPQUAD(tuple->dst.ip));
65 return proto->print_tuple(s, tuple);
66 }
67
68 #ifdef CONFIG_IP_NF_CT_ACCT
69 static unsigned int
70 seq_print_counters(struct seq_file *s,
71 const struct ip_conntrack_counter *counter)
72 {
73 return seq_printf(s, "packets=%llu bytes=%llu ",
74 (unsigned long long)counter->packets,
75 (unsigned long long)counter->bytes);
76 }
77 #else
78 #define seq_print_counters(x, y) 0
79 #endif
80
81 struct ct_iter_state {
82 unsigned int bucket;
83 };
84
85 static struct list_head *ct_get_first(struct seq_file *seq)
86 {
87 struct ct_iter_state *st = seq->private;
88
89 for (st->bucket = 0;
90 st->bucket < ip_conntrack_htable_size;
91 st->bucket++) {
92 if (!list_empty(&ip_conntrack_hash[st->bucket]))
93 return ip_conntrack_hash[st->bucket].next;
94 }
95 return NULL;
96 }
97
98 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
99 {
100 struct ct_iter_state *st = seq->private;
101
102 head = head->next;
103 while (head == &ip_conntrack_hash[st->bucket]) {
104 if (++st->bucket >= ip_conntrack_htable_size)
105 return NULL;
106 head = ip_conntrack_hash[st->bucket].next;
107 }
108 return head;
109 }
110
111 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
112 {
113 struct list_head *head = ct_get_first(seq);
114
115 if (head)
116 while (pos && (head = ct_get_next(seq, head)))
117 pos--;
118 return pos ? NULL : head;
119 }
120
121 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
122 {
123 read_lock_bh(&ip_conntrack_lock);
124 return ct_get_idx(seq, *pos);
125 }
126
127 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
128 {
129 (*pos)++;
130 return ct_get_next(s, v);
131 }
132
133 static void ct_seq_stop(struct seq_file *s, void *v)
134 {
135 read_unlock_bh(&ip_conntrack_lock);
136 }
137
138 static int ct_seq_show(struct seq_file *s, void *v)
139 {
140 const struct ip_conntrack_tuple_hash *hash = v;
141 const struct ip_conntrack *conntrack = tuplehash_to_ctrack(hash);
142 struct ip_conntrack_protocol *proto;
143
144 ASSERT_READ_LOCK(&ip_conntrack_lock);
145 IP_NF_ASSERT(conntrack);
146
147 /* we only want to print DIR_ORIGINAL */
148 if (DIRECTION(hash))
149 return 0;
150
151 proto = __ip_conntrack_proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
152 IP_NF_ASSERT(proto);
153
154 if (seq_printf(s, "%-8s %u %ld ",
155 proto->name,
156 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
157 timer_pending(&conntrack->timeout)
158 ? (long)(conntrack->timeout.expires - jiffies)/HZ
159 : 0) != 0)
160 return -ENOSPC;
161
162 if (proto->print_conntrack(s, conntrack))
163 return -ENOSPC;
164
165 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
166 proto))
167 return -ENOSPC;
168
169 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
170 return -ENOSPC;
171
172 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
173 if (seq_printf(s, "[UNREPLIED] "))
174 return -ENOSPC;
175
176 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
177 proto))
178 return -ENOSPC;
179
180 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
181 return -ENOSPC;
182
183 if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
184 if (seq_printf(s, "[ASSURED] "))
185 return -ENOSPC;
186
187 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
188 if (seq_printf(s, "mark=%u ", conntrack->mark))
189 return -ENOSPC;
190 #endif
191
192 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
193 return -ENOSPC;
194
195 return 0;
196 }
197
198 static struct seq_operations ct_seq_ops = {
199 .start = ct_seq_start,
200 .next = ct_seq_next,
201 .stop = ct_seq_stop,
202 .show = ct_seq_show
203 };
204
205 static int ct_open(struct inode *inode, struct file *file)
206 {
207 struct seq_file *seq;
208 struct ct_iter_state *st;
209 int ret;
210
211 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
212 if (st == NULL)
213 return -ENOMEM;
214 ret = seq_open(file, &ct_seq_ops);
215 if (ret)
216 goto out_free;
217 seq = file->private_data;
218 seq->private = st;
219 memset(st, 0, sizeof(struct ct_iter_state));
220 return ret;
221 out_free:
222 kfree(st);
223 return ret;
224 }
225
226 static struct file_operations ct_file_ops = {
227 .owner = THIS_MODULE,
228 .open = ct_open,
229 .read = seq_read,
230 .llseek = seq_lseek,
231 .release = seq_release_private,
232 };
233
234 /* expects */
235 static void *exp_seq_start(struct seq_file *s, loff_t *pos)
236 {
237 struct list_head *e = &ip_conntrack_expect_list;
238 loff_t i;
239
240 /* strange seq_file api calls stop even if we fail,
241 * thus we need to grab lock since stop unlocks */
242 read_lock_bh(&ip_conntrack_lock);
243
244 if (list_empty(e))
245 return NULL;
246
247 for (i = 0; i <= *pos; i++) {
248 e = e->next;
249 if (e == &ip_conntrack_expect_list)
250 return NULL;
251 }
252 return e;
253 }
254
255 static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
256 {
257 struct list_head *e = v;
258
259 ++*pos;
260 e = e->next;
261
262 if (e == &ip_conntrack_expect_list)
263 return NULL;
264
265 return e;
266 }
267
268 static void exp_seq_stop(struct seq_file *s, void *v)
269 {
270 read_unlock_bh(&ip_conntrack_lock);
271 }
272
273 static int exp_seq_show(struct seq_file *s, void *v)
274 {
275 struct ip_conntrack_expect *expect = v;
276
277 if (expect->timeout.function)
278 seq_printf(s, "%ld ", timer_pending(&expect->timeout)
279 ? (long)(expect->timeout.expires - jiffies)/HZ : 0);
280 else
281 seq_printf(s, "- ");
282
283 seq_printf(s, "proto=%u ", expect->tuple.dst.protonum);
284
285 print_tuple(s, &expect->tuple,
286 __ip_conntrack_proto_find(expect->tuple.dst.protonum));
287 return seq_putc(s, '\n');
288 }
289
290 static struct seq_operations exp_seq_ops = {
291 .start = exp_seq_start,
292 .next = exp_seq_next,
293 .stop = exp_seq_stop,
294 .show = exp_seq_show
295 };
296
297 static int exp_open(struct inode *inode, struct file *file)
298 {
299 return seq_open(file, &exp_seq_ops);
300 }
301
302 static struct file_operations exp_file_ops = {
303 .owner = THIS_MODULE,
304 .open = exp_open,
305 .read = seq_read,
306 .llseek = seq_lseek,
307 .release = seq_release
308 };
309
310 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
311 {
312 int cpu;
313
314 if (*pos == 0)
315 return SEQ_START_TOKEN;
316
317 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
318 if (!cpu_possible(cpu))
319 continue;
320 *pos = cpu+1;
321 return &per_cpu(ip_conntrack_stat, cpu);
322 }
323
324 return NULL;
325 }
326
327 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
328 {
329 int cpu;
330
331 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
332 if (!cpu_possible(cpu))
333 continue;
334 *pos = cpu+1;
335 return &per_cpu(ip_conntrack_stat, cpu);
336 }
337
338 return NULL;
339 }
340
341 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
342 {
343 }
344
345 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
346 {
347 unsigned int nr_conntracks = atomic_read(&ip_conntrack_count);
348 struct ip_conntrack_stat *st = v;
349
350 if (v == SEQ_START_TOKEN) {
351 seq_printf(seq, "entries searched found new invalid ignore delete delete_list insert insert_failed drop early_drop icmp_error expect_new expect_create expect_delete\n");
352 return 0;
353 }
354
355 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
356 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
357 nr_conntracks,
358 st->searched,
359 st->found,
360 st->new,
361 st->invalid,
362 st->ignore,
363 st->delete,
364 st->delete_list,
365 st->insert,
366 st->insert_failed,
367 st->drop,
368 st->early_drop,
369 st->error,
370
371 st->expect_new,
372 st->expect_create,
373 st->expect_delete
374 );
375 return 0;
376 }
377
378 static struct seq_operations ct_cpu_seq_ops = {
379 .start = ct_cpu_seq_start,
380 .next = ct_cpu_seq_next,
381 .stop = ct_cpu_seq_stop,
382 .show = ct_cpu_seq_show,
383 };
384
385 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
386 {
387 return seq_open(file, &ct_cpu_seq_ops);
388 }
389
390 static struct file_operations ct_cpu_seq_fops = {
391 .owner = THIS_MODULE,
392 .open = ct_cpu_seq_open,
393 .read = seq_read,
394 .llseek = seq_lseek,
395 .release = seq_release_private,
396 };
397 #endif
398
399 static unsigned int ip_confirm(unsigned int hooknum,
400 struct sk_buff **pskb,
401 const struct net_device *in,
402 const struct net_device *out,
403 int (*okfn)(struct sk_buff *))
404 {
405 /* We've seen it coming out the other side: confirm it */
406 return ip_conntrack_confirm(pskb);
407 }
408
409 static unsigned int ip_conntrack_help(unsigned int hooknum,
410 struct sk_buff **pskb,
411 const struct net_device *in,
412 const struct net_device *out,
413 int (*okfn)(struct sk_buff *))
414 {
415 struct ip_conntrack *ct;
416 enum ip_conntrack_info ctinfo;
417
418 /* This is where we call the helper: as the packet goes out. */
419 ct = ip_conntrack_get(*pskb, &ctinfo);
420 if (ct && ct->helper) {
421 unsigned int ret;
422 ret = ct->helper->help(pskb, ct, ctinfo);
423 if (ret != NF_ACCEPT)
424 return ret;
425 }
426 return NF_ACCEPT;
427 }
428
429 static unsigned int ip_conntrack_defrag(unsigned int hooknum,
430 struct sk_buff **pskb,
431 const struct net_device *in,
432 const struct net_device *out,
433 int (*okfn)(struct sk_buff *))
434 {
435 #if !defined(CONFIG_IP_NF_NAT) && !defined(CONFIG_IP_NF_NAT_MODULE)
436 /* Previously seen (loopback)? Ignore. Do this before
437 fragment check. */
438 if ((*pskb)->nfct)
439 return NF_ACCEPT;
440 #endif
441
442 /* Gather fragments. */
443 if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
444 *pskb = ip_ct_gather_frags(*pskb,
445 hooknum == NF_IP_PRE_ROUTING ?
446 IP_DEFRAG_CONNTRACK_IN :
447 IP_DEFRAG_CONNTRACK_OUT);
448 if (!*pskb)
449 return NF_STOLEN;
450 }
451 return NF_ACCEPT;
452 }
453
454 static unsigned int ip_refrag(unsigned int hooknum,
455 struct sk_buff **pskb,
456 const struct net_device *in,
457 const struct net_device *out,
458 int (*okfn)(struct sk_buff *))
459 {
460 struct rtable *rt = (struct rtable *)(*pskb)->dst;
461
462 /* We've seen it coming out the other side: confirm */
463 if (ip_confirm(hooknum, pskb, in, out, okfn) != NF_ACCEPT)
464 return NF_DROP;
465
466 /* Local packets are never produced too large for their
467 interface. We degfragment them at LOCAL_OUT, however,
468 so we have to refragment them here. */
469 if ((*pskb)->len > dst_mtu(&rt->u.dst) &&
470 !skb_shinfo(*pskb)->tso_size) {
471 /* No hook can be after us, so this should be OK. */
472 ip_fragment(*pskb, okfn);
473 return NF_STOLEN;
474 }
475 return NF_ACCEPT;
476 }
477
478 static unsigned int ip_conntrack_local(unsigned int hooknum,
479 struct sk_buff **pskb,
480 const struct net_device *in,
481 const struct net_device *out,
482 int (*okfn)(struct sk_buff *))
483 {
484 /* root is playing with raw sockets. */
485 if ((*pskb)->len < sizeof(struct iphdr)
486 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
487 if (net_ratelimit())
488 printk("ipt_hook: happy cracking.\n");
489 return NF_ACCEPT;
490 }
491 return ip_conntrack_in(hooknum, pskb, in, out, okfn);
492 }
493
494 /* Connection tracking may drop packets, but never alters them, so
495 make it the first hook. */
496 static struct nf_hook_ops ip_conntrack_defrag_ops = {
497 .hook = ip_conntrack_defrag,
498 .owner = THIS_MODULE,
499 .pf = PF_INET,
500 .hooknum = NF_IP_PRE_ROUTING,
501 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
502 };
503
504 static struct nf_hook_ops ip_conntrack_in_ops = {
505 .hook = ip_conntrack_in,
506 .owner = THIS_MODULE,
507 .pf = PF_INET,
508 .hooknum = NF_IP_PRE_ROUTING,
509 .priority = NF_IP_PRI_CONNTRACK,
510 };
511
512 static struct nf_hook_ops ip_conntrack_defrag_local_out_ops = {
513 .hook = ip_conntrack_defrag,
514 .owner = THIS_MODULE,
515 .pf = PF_INET,
516 .hooknum = NF_IP_LOCAL_OUT,
517 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
518 };
519
520 static struct nf_hook_ops ip_conntrack_local_out_ops = {
521 .hook = ip_conntrack_local,
522 .owner = THIS_MODULE,
523 .pf = PF_INET,
524 .hooknum = NF_IP_LOCAL_OUT,
525 .priority = NF_IP_PRI_CONNTRACK,
526 };
527
528 /* helpers */
529 static struct nf_hook_ops ip_conntrack_helper_out_ops = {
530 .hook = ip_conntrack_help,
531 .owner = THIS_MODULE,
532 .pf = PF_INET,
533 .hooknum = NF_IP_POST_ROUTING,
534 .priority = NF_IP_PRI_CONNTRACK_HELPER,
535 };
536
537 static struct nf_hook_ops ip_conntrack_helper_in_ops = {
538 .hook = ip_conntrack_help,
539 .owner = THIS_MODULE,
540 .pf = PF_INET,
541 .hooknum = NF_IP_LOCAL_IN,
542 .priority = NF_IP_PRI_CONNTRACK_HELPER,
543 };
544
545 /* Refragmenter; last chance. */
546 static struct nf_hook_ops ip_conntrack_out_ops = {
547 .hook = ip_refrag,
548 .owner = THIS_MODULE,
549 .pf = PF_INET,
550 .hooknum = NF_IP_POST_ROUTING,
551 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
552 };
553
554 static struct nf_hook_ops ip_conntrack_local_in_ops = {
555 .hook = ip_confirm,
556 .owner = THIS_MODULE,
557 .pf = PF_INET,
558 .hooknum = NF_IP_LOCAL_IN,
559 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
560 };
561
562 /* Sysctl support */
563
564 #ifdef CONFIG_SYSCTL
565
566 /* From ip_conntrack_core.c */
567 extern int ip_conntrack_max;
568 extern unsigned int ip_conntrack_htable_size;
569
570 /* From ip_conntrack_proto_tcp.c */
571 extern unsigned long ip_ct_tcp_timeout_syn_sent;
572 extern unsigned long ip_ct_tcp_timeout_syn_recv;
573 extern unsigned long ip_ct_tcp_timeout_established;
574 extern unsigned long ip_ct_tcp_timeout_fin_wait;
575 extern unsigned long ip_ct_tcp_timeout_close_wait;
576 extern unsigned long ip_ct_tcp_timeout_last_ack;
577 extern unsigned long ip_ct_tcp_timeout_time_wait;
578 extern unsigned long ip_ct_tcp_timeout_close;
579 extern unsigned long ip_ct_tcp_timeout_max_retrans;
580 extern int ip_ct_tcp_loose;
581 extern int ip_ct_tcp_be_liberal;
582 extern int ip_ct_tcp_max_retrans;
583
584 /* From ip_conntrack_proto_udp.c */
585 extern unsigned long ip_ct_udp_timeout;
586 extern unsigned long ip_ct_udp_timeout_stream;
587
588 /* From ip_conntrack_proto_icmp.c */
589 extern unsigned long ip_ct_icmp_timeout;
590
591 /* From ip_conntrack_proto_icmp.c */
592 extern unsigned long ip_ct_generic_timeout;
593
594 /* Log invalid packets of a given protocol */
595 static int log_invalid_proto_min = 0;
596 static int log_invalid_proto_max = 255;
597
598 static struct ctl_table_header *ip_ct_sysctl_header;
599
600 static ctl_table ip_ct_sysctl_table[] = {
601 {
602 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
603 .procname = "ip_conntrack_max",
604 .data = &ip_conntrack_max,
605 .maxlen = sizeof(int),
606 .mode = 0644,
607 .proc_handler = &proc_dointvec,
608 },
609 {
610 .ctl_name = NET_IPV4_NF_CONNTRACK_COUNT,
611 .procname = "ip_conntrack_count",
612 .data = &ip_conntrack_count,
613 .maxlen = sizeof(int),
614 .mode = 0444,
615 .proc_handler = &proc_dointvec,
616 },
617 {
618 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
619 .procname = "ip_conntrack_buckets",
620 .data = &ip_conntrack_htable_size,
621 .maxlen = sizeof(unsigned int),
622 .mode = 0444,
623 .proc_handler = &proc_dointvec,
624 },
625 {
626 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
627 .procname = "ip_conntrack_tcp_timeout_syn_sent",
628 .data = &ip_ct_tcp_timeout_syn_sent,
629 .maxlen = sizeof(unsigned int),
630 .mode = 0644,
631 .proc_handler = &proc_dointvec_jiffies,
632 },
633 {
634 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
635 .procname = "ip_conntrack_tcp_timeout_syn_recv",
636 .data = &ip_ct_tcp_timeout_syn_recv,
637 .maxlen = sizeof(unsigned int),
638 .mode = 0644,
639 .proc_handler = &proc_dointvec_jiffies,
640 },
641 {
642 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
643 .procname = "ip_conntrack_tcp_timeout_established",
644 .data = &ip_ct_tcp_timeout_established,
645 .maxlen = sizeof(unsigned int),
646 .mode = 0644,
647 .proc_handler = &proc_dointvec_jiffies,
648 },
649 {
650 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
651 .procname = "ip_conntrack_tcp_timeout_fin_wait",
652 .data = &ip_ct_tcp_timeout_fin_wait,
653 .maxlen = sizeof(unsigned int),
654 .mode = 0644,
655 .proc_handler = &proc_dointvec_jiffies,
656 },
657 {
658 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
659 .procname = "ip_conntrack_tcp_timeout_close_wait",
660 .data = &ip_ct_tcp_timeout_close_wait,
661 .maxlen = sizeof(unsigned int),
662 .mode = 0644,
663 .proc_handler = &proc_dointvec_jiffies,
664 },
665 {
666 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
667 .procname = "ip_conntrack_tcp_timeout_last_ack",
668 .data = &ip_ct_tcp_timeout_last_ack,
669 .maxlen = sizeof(unsigned int),
670 .mode = 0644,
671 .proc_handler = &proc_dointvec_jiffies,
672 },
673 {
674 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
675 .procname = "ip_conntrack_tcp_timeout_time_wait",
676 .data = &ip_ct_tcp_timeout_time_wait,
677 .maxlen = sizeof(unsigned int),
678 .mode = 0644,
679 .proc_handler = &proc_dointvec_jiffies,
680 },
681 {
682 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
683 .procname = "ip_conntrack_tcp_timeout_close",
684 .data = &ip_ct_tcp_timeout_close,
685 .maxlen = sizeof(unsigned int),
686 .mode = 0644,
687 .proc_handler = &proc_dointvec_jiffies,
688 },
689 {
690 .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT,
691 .procname = "ip_conntrack_udp_timeout",
692 .data = &ip_ct_udp_timeout,
693 .maxlen = sizeof(unsigned int),
694 .mode = 0644,
695 .proc_handler = &proc_dointvec_jiffies,
696 },
697 {
698 .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM,
699 .procname = "ip_conntrack_udp_timeout_stream",
700 .data = &ip_ct_udp_timeout_stream,
701 .maxlen = sizeof(unsigned int),
702 .mode = 0644,
703 .proc_handler = &proc_dointvec_jiffies,
704 },
705 {
706 .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
707 .procname = "ip_conntrack_icmp_timeout",
708 .data = &ip_ct_icmp_timeout,
709 .maxlen = sizeof(unsigned int),
710 .mode = 0644,
711 .proc_handler = &proc_dointvec_jiffies,
712 },
713 {
714 .ctl_name = NET_IPV4_NF_CONNTRACK_GENERIC_TIMEOUT,
715 .procname = "ip_conntrack_generic_timeout",
716 .data = &ip_ct_generic_timeout,
717 .maxlen = sizeof(unsigned int),
718 .mode = 0644,
719 .proc_handler = &proc_dointvec_jiffies,
720 },
721 {
722 .ctl_name = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
723 .procname = "ip_conntrack_log_invalid",
724 .data = &ip_ct_log_invalid,
725 .maxlen = sizeof(unsigned int),
726 .mode = 0644,
727 .proc_handler = &proc_dointvec_minmax,
728 .strategy = &sysctl_intvec,
729 .extra1 = &log_invalid_proto_min,
730 .extra2 = &log_invalid_proto_max,
731 },
732 {
733 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
734 .procname = "ip_conntrack_tcp_timeout_max_retrans",
735 .data = &ip_ct_tcp_timeout_max_retrans,
736 .maxlen = sizeof(unsigned int),
737 .mode = 0644,
738 .proc_handler = &proc_dointvec_jiffies,
739 },
740 {
741 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
742 .procname = "ip_conntrack_tcp_loose",
743 .data = &ip_ct_tcp_loose,
744 .maxlen = sizeof(unsigned int),
745 .mode = 0644,
746 .proc_handler = &proc_dointvec,
747 },
748 {
749 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
750 .procname = "ip_conntrack_tcp_be_liberal",
751 .data = &ip_ct_tcp_be_liberal,
752 .maxlen = sizeof(unsigned int),
753 .mode = 0644,
754 .proc_handler = &proc_dointvec,
755 },
756 {
757 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
758 .procname = "ip_conntrack_tcp_max_retrans",
759 .data = &ip_ct_tcp_max_retrans,
760 .maxlen = sizeof(unsigned int),
761 .mode = 0644,
762 .proc_handler = &proc_dointvec,
763 },
764 { .ctl_name = 0 }
765 };
766
767 #define NET_IP_CONNTRACK_MAX 2089
768
769 static ctl_table ip_ct_netfilter_table[] = {
770 {
771 .ctl_name = NET_IPV4_NETFILTER,
772 .procname = "netfilter",
773 .mode = 0555,
774 .child = ip_ct_sysctl_table,
775 },
776 {
777 .ctl_name = NET_IP_CONNTRACK_MAX,
778 .procname = "ip_conntrack_max",
779 .data = &ip_conntrack_max,
780 .maxlen = sizeof(int),
781 .mode = 0644,
782 .proc_handler = &proc_dointvec
783 },
784 { .ctl_name = 0 }
785 };
786
787 static ctl_table ip_ct_ipv4_table[] = {
788 {
789 .ctl_name = NET_IPV4,
790 .procname = "ipv4",
791 .mode = 0555,
792 .child = ip_ct_netfilter_table,
793 },
794 { .ctl_name = 0 }
795 };
796
797 static ctl_table ip_ct_net_table[] = {
798 {
799 .ctl_name = CTL_NET,
800 .procname = "net",
801 .mode = 0555,
802 .child = ip_ct_ipv4_table,
803 },
804 { .ctl_name = 0 }
805 };
806
807 EXPORT_SYMBOL(ip_ct_log_invalid);
808 #endif /* CONFIG_SYSCTL */
809
810 static int init_or_cleanup(int init)
811 {
812 #ifdef CONFIG_PROC_FS
813 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
814 #endif
815 int ret = 0;
816
817 if (!init) goto cleanup;
818
819 ret = ip_conntrack_init();
820 if (ret < 0)
821 goto cleanup_nothing;
822
823 #ifdef CONFIG_PROC_FS
824 ret = -ENOMEM;
825 proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops);
826 if (!proc) goto cleanup_init;
827
828 proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440,
829 &exp_file_ops);
830 if (!proc_exp) goto cleanup_proc;
831
832 proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat);
833 if (!proc_stat)
834 goto cleanup_proc_exp;
835
836 proc_stat->proc_fops = &ct_cpu_seq_fops;
837 proc_stat->owner = THIS_MODULE;
838 #endif
839
840 ret = nf_register_hook(&ip_conntrack_defrag_ops);
841 if (ret < 0) {
842 printk("ip_conntrack: can't register pre-routing defrag hook.\n");
843 goto cleanup_proc_stat;
844 }
845 ret = nf_register_hook(&ip_conntrack_defrag_local_out_ops);
846 if (ret < 0) {
847 printk("ip_conntrack: can't register local_out defrag hook.\n");
848 goto cleanup_defragops;
849 }
850 ret = nf_register_hook(&ip_conntrack_in_ops);
851 if (ret < 0) {
852 printk("ip_conntrack: can't register pre-routing hook.\n");
853 goto cleanup_defraglocalops;
854 }
855 ret = nf_register_hook(&ip_conntrack_local_out_ops);
856 if (ret < 0) {
857 printk("ip_conntrack: can't register local out hook.\n");
858 goto cleanup_inops;
859 }
860 ret = nf_register_hook(&ip_conntrack_helper_in_ops);
861 if (ret < 0) {
862 printk("ip_conntrack: can't register local in helper hook.\n");
863 goto cleanup_inandlocalops;
864 }
865 ret = nf_register_hook(&ip_conntrack_helper_out_ops);
866 if (ret < 0) {
867 printk("ip_conntrack: can't register postrouting helper hook.\n");
868 goto cleanup_helperinops;
869 }
870 ret = nf_register_hook(&ip_conntrack_out_ops);
871 if (ret < 0) {
872 printk("ip_conntrack: can't register post-routing hook.\n");
873 goto cleanup_helperoutops;
874 }
875 ret = nf_register_hook(&ip_conntrack_local_in_ops);
876 if (ret < 0) {
877 printk("ip_conntrack: can't register local in hook.\n");
878 goto cleanup_inoutandlocalops;
879 }
880 #ifdef CONFIG_SYSCTL
881 ip_ct_sysctl_header = register_sysctl_table(ip_ct_net_table, 0);
882 if (ip_ct_sysctl_header == NULL) {
883 printk("ip_conntrack: can't register to sysctl.\n");
884 ret = -ENOMEM;
885 goto cleanup_localinops;
886 }
887 #endif
888
889 return ret;
890
891 cleanup:
892 synchronize_net();
893 #ifdef CONFIG_SYSCTL
894 unregister_sysctl_table(ip_ct_sysctl_header);
895 cleanup_localinops:
896 #endif
897 nf_unregister_hook(&ip_conntrack_local_in_ops);
898 cleanup_inoutandlocalops:
899 nf_unregister_hook(&ip_conntrack_out_ops);
900 cleanup_helperoutops:
901 nf_unregister_hook(&ip_conntrack_helper_out_ops);
902 cleanup_helperinops:
903 nf_unregister_hook(&ip_conntrack_helper_in_ops);
904 cleanup_inandlocalops:
905 nf_unregister_hook(&ip_conntrack_local_out_ops);
906 cleanup_inops:
907 nf_unregister_hook(&ip_conntrack_in_ops);
908 cleanup_defraglocalops:
909 nf_unregister_hook(&ip_conntrack_defrag_local_out_ops);
910 cleanup_defragops:
911 nf_unregister_hook(&ip_conntrack_defrag_ops);
912 cleanup_proc_stat:
913 #ifdef CONFIG_PROC_FS
914 remove_proc_entry("ip_conntrack", proc_net_stat);
915 cleanup_proc_exp:
916 proc_net_remove("ip_conntrack_expect");
917 cleanup_proc:
918 proc_net_remove("ip_conntrack");
919 cleanup_init:
920 #endif /* CONFIG_PROC_FS */
921 ip_conntrack_cleanup();
922 cleanup_nothing:
923 return ret;
924 }
925
926 /* FIXME: Allow NULL functions and sub in pointers to generic for
927 them. --RR */
928 int ip_conntrack_protocol_register(struct ip_conntrack_protocol *proto)
929 {
930 int ret = 0;
931
932 write_lock_bh(&ip_conntrack_lock);
933 if (ip_ct_protos[proto->proto] != &ip_conntrack_generic_protocol) {
934 ret = -EBUSY;
935 goto out;
936 }
937 ip_ct_protos[proto->proto] = proto;
938 out:
939 write_unlock_bh(&ip_conntrack_lock);
940 return ret;
941 }
942
943 void ip_conntrack_protocol_unregister(struct ip_conntrack_protocol *proto)
944 {
945 write_lock_bh(&ip_conntrack_lock);
946 ip_ct_protos[proto->proto] = &ip_conntrack_generic_protocol;
947 write_unlock_bh(&ip_conntrack_lock);
948
949 /* Somebody could be still looking at the proto in bh. */
950 synchronize_net();
951
952 /* Remove all contrack entries for this protocol */
953 ip_ct_iterate_cleanup(kill_proto, &proto->proto);
954 }
955
956 static int __init init(void)
957 {
958 return init_or_cleanup(1);
959 }
960
961 static void __exit fini(void)
962 {
963 init_or_cleanup(0);
964 }
965
966 module_init(init);
967 module_exit(fini);
968
969 /* Some modules need us, but don't depend directly on any symbol.
970 They should call this. */
971 void need_ip_conntrack(void)
972 {
973 }
974
975 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
976 EXPORT_SYMBOL_GPL(ip_conntrack_chain);
977 EXPORT_SYMBOL_GPL(ip_conntrack_expect_chain);
978 EXPORT_SYMBOL_GPL(ip_conntrack_register_notifier);
979 EXPORT_SYMBOL_GPL(ip_conntrack_unregister_notifier);
980 EXPORT_SYMBOL_GPL(__ip_ct_event_cache_init);
981 EXPORT_PER_CPU_SYMBOL_GPL(ip_conntrack_ecache);
982 #endif
983 EXPORT_SYMBOL(ip_conntrack_protocol_register);
984 EXPORT_SYMBOL(ip_conntrack_protocol_unregister);
985 EXPORT_SYMBOL(ip_ct_get_tuple);
986 EXPORT_SYMBOL(invert_tuplepr);
987 EXPORT_SYMBOL(ip_conntrack_alter_reply);
988 EXPORT_SYMBOL(ip_conntrack_destroyed);
989 EXPORT_SYMBOL(need_ip_conntrack);
990 EXPORT_SYMBOL(ip_conntrack_helper_register);
991 EXPORT_SYMBOL(ip_conntrack_helper_unregister);
992 EXPORT_SYMBOL(ip_ct_iterate_cleanup);
993 EXPORT_SYMBOL(__ip_ct_refresh_acct);
994
995 EXPORT_SYMBOL(ip_conntrack_expect_alloc);
996 EXPORT_SYMBOL(ip_conntrack_expect_put);
997 EXPORT_SYMBOL_GPL(__ip_conntrack_expect_find);
998 EXPORT_SYMBOL_GPL(ip_conntrack_expect_find);
999 EXPORT_SYMBOL(ip_conntrack_expect_related);
1000 EXPORT_SYMBOL(ip_conntrack_unexpect_related);
1001 EXPORT_SYMBOL_GPL(ip_conntrack_expect_list);
1002 EXPORT_SYMBOL_GPL(ip_ct_unlink_expect);
1003
1004 EXPORT_SYMBOL(ip_conntrack_tuple_taken);
1005 EXPORT_SYMBOL(ip_ct_gather_frags);
1006 EXPORT_SYMBOL(ip_conntrack_htable_size);
1007 EXPORT_SYMBOL(ip_conntrack_lock);
1008 EXPORT_SYMBOL(ip_conntrack_hash);
1009 EXPORT_SYMBOL(ip_conntrack_untracked);
1010 EXPORT_SYMBOL_GPL(ip_conntrack_find_get);
1011 #ifdef CONFIG_IP_NF_NAT_NEEDED
1012 EXPORT_SYMBOL(ip_conntrack_tcp_update);
1013 #endif
1014
1015 EXPORT_SYMBOL_GPL(ip_conntrack_flush);
1016 EXPORT_SYMBOL_GPL(__ip_conntrack_find);
1017
1018 EXPORT_SYMBOL_GPL(ip_conntrack_alloc);
1019 EXPORT_SYMBOL_GPL(ip_conntrack_free);
1020 EXPORT_SYMBOL_GPL(ip_conntrack_hash_insert);
1021
1022 EXPORT_SYMBOL_GPL(ip_ct_remove_expectations);
1023
1024 EXPORT_SYMBOL_GPL(ip_conntrack_helper_find_get);
1025 EXPORT_SYMBOL_GPL(ip_conntrack_helper_put);
1026 EXPORT_SYMBOL_GPL(__ip_conntrack_helper_find_byname);
1027
1028 EXPORT_SYMBOL_GPL(ip_conntrack_proto_find_get);
1029 EXPORT_SYMBOL_GPL(ip_conntrack_proto_put);
1030 EXPORT_SYMBOL_GPL(__ip_conntrack_proto_find);
1031 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
1032 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
1033 EXPORT_SYMBOL_GPL(ip_ct_port_tuple_to_nfattr);
1034 EXPORT_SYMBOL_GPL(ip_ct_port_nfattr_to_tuple);
1035 #endif