]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | |
080774a2 | 8 | * (C) 2002-2005 Netfilter Core Team <coreteam@netfilter.org> |
1da177e4 LT |
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> | |
14c85021 | 30 | #include <net/route.h> |
1da177e4 | 31 | |
e45b1be8 PM |
32 | #define ASSERT_READ_LOCK(x) |
33 | #define ASSERT_WRITE_LOCK(x) | |
1da177e4 LT |
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 | { | |
e45b1be8 | 123 | read_lock_bh(&ip_conntrack_lock); |
1da177e4 LT |
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 | { | |
e45b1be8 | 135 | read_unlock_bh(&ip_conntrack_lock); |
1da177e4 LT |
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 | ||
e45b1be8 | 144 | ASSERT_READ_LOCK(&ip_conntrack_lock); |
1da177e4 LT |
145 | IP_NF_ASSERT(conntrack); |
146 | ||
147 | /* we only want to print DIR_ORIGINAL */ | |
148 | if (DIRECTION(hash)) | |
149 | return 0; | |
150 | ||
080774a2 | 151 | proto = __ip_conntrack_proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum); |
1da177e4 LT |
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) | |
bf3a46aa | 188 | if (seq_printf(s, "mark=%u ", conntrack->mark)) |
1da177e4 LT |
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 */ | |
e45b1be8 | 242 | read_lock_bh(&ip_conntrack_lock); |
1da177e4 LT |
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 | ||
a96aca88 | 259 | ++*pos; |
1da177e4 LT |
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 | { | |
e45b1be8 | 270 | read_unlock_bh(&ip_conntrack_lock); |
1da177e4 LT |
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, | |
080774a2 | 286 | __ip_conntrack_proto_find(expect->tuple.dst.protonum)); |
1da177e4 LT |
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 *)) | |
e281e3ac PM |
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 *)) | |
1da177e4 LT |
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 | } | |
e281e3ac | 426 | return NF_ACCEPT; |
1da177e4 LT |
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 | { | |
84531c24 PO |
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 | ||
1da177e4 LT |
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 | ||
1da177e4 LT |
454 | static unsigned int ip_conntrack_local(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 | /* root is playing with raw sockets. */ | |
461 | if ((*pskb)->len < sizeof(struct iphdr) | |
462 | || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) { | |
463 | if (net_ratelimit()) | |
464 | printk("ipt_hook: happy cracking.\n"); | |
465 | return NF_ACCEPT; | |
466 | } | |
467 | return ip_conntrack_in(hooknum, pskb, in, out, okfn); | |
468 | } | |
469 | ||
470 | /* Connection tracking may drop packets, but never alters them, so | |
471 | make it the first hook. */ | |
964ddaa1 PM |
472 | static struct nf_hook_ops ip_conntrack_ops[] = { |
473 | { | |
474 | .hook = ip_conntrack_defrag, | |
475 | .owner = THIS_MODULE, | |
476 | .pf = PF_INET, | |
477 | .hooknum = NF_IP_PRE_ROUTING, | |
478 | .priority = NF_IP_PRI_CONNTRACK_DEFRAG, | |
479 | }, | |
480 | { | |
481 | .hook = ip_conntrack_in, | |
482 | .owner = THIS_MODULE, | |
483 | .pf = PF_INET, | |
484 | .hooknum = NF_IP_PRE_ROUTING, | |
485 | .priority = NF_IP_PRI_CONNTRACK, | |
486 | }, | |
487 | { | |
488 | .hook = ip_conntrack_defrag, | |
489 | .owner = THIS_MODULE, | |
490 | .pf = PF_INET, | |
491 | .hooknum = NF_IP_LOCAL_OUT, | |
492 | .priority = NF_IP_PRI_CONNTRACK_DEFRAG, | |
493 | }, | |
494 | { | |
495 | .hook = ip_conntrack_local, | |
496 | .owner = THIS_MODULE, | |
497 | .pf = PF_INET, | |
498 | .hooknum = NF_IP_LOCAL_OUT, | |
499 | .priority = NF_IP_PRI_CONNTRACK, | |
500 | }, | |
501 | { | |
502 | .hook = ip_conntrack_help, | |
503 | .owner = THIS_MODULE, | |
504 | .pf = PF_INET, | |
505 | .hooknum = NF_IP_POST_ROUTING, | |
506 | .priority = NF_IP_PRI_CONNTRACK_HELPER, | |
507 | }, | |
508 | { | |
509 | .hook = ip_conntrack_help, | |
510 | .owner = THIS_MODULE, | |
511 | .pf = PF_INET, | |
512 | .hooknum = NF_IP_LOCAL_IN, | |
513 | .priority = NF_IP_PRI_CONNTRACK_HELPER, | |
514 | }, | |
515 | { | |
516 | .hook = ip_confirm, | |
517 | .owner = THIS_MODULE, | |
518 | .pf = PF_INET, | |
519 | .hooknum = NF_IP_POST_ROUTING, | |
520 | .priority = NF_IP_PRI_CONNTRACK_CONFIRM, | |
521 | }, | |
522 | { | |
523 | .hook = ip_confirm, | |
524 | .owner = THIS_MODULE, | |
525 | .pf = PF_INET, | |
526 | .hooknum = NF_IP_LOCAL_IN, | |
527 | .priority = NF_IP_PRI_CONNTRACK_CONFIRM, | |
528 | }, | |
1da177e4 LT |
529 | }; |
530 | ||
531 | /* Sysctl support */ | |
532 | ||
533 | #ifdef CONFIG_SYSCTL | |
534 | ||
535 | /* From ip_conntrack_core.c */ | |
536 | extern int ip_conntrack_max; | |
537 | extern unsigned int ip_conntrack_htable_size; | |
538 | ||
539 | /* From ip_conntrack_proto_tcp.c */ | |
babbdb1a PM |
540 | extern unsigned int ip_ct_tcp_timeout_syn_sent; |
541 | extern unsigned int ip_ct_tcp_timeout_syn_recv; | |
542 | extern unsigned int ip_ct_tcp_timeout_established; | |
543 | extern unsigned int ip_ct_tcp_timeout_fin_wait; | |
544 | extern unsigned int ip_ct_tcp_timeout_close_wait; | |
545 | extern unsigned int ip_ct_tcp_timeout_last_ack; | |
546 | extern unsigned int ip_ct_tcp_timeout_time_wait; | |
547 | extern unsigned int ip_ct_tcp_timeout_close; | |
548 | extern unsigned int ip_ct_tcp_timeout_max_retrans; | |
1da177e4 LT |
549 | extern int ip_ct_tcp_loose; |
550 | extern int ip_ct_tcp_be_liberal; | |
551 | extern int ip_ct_tcp_max_retrans; | |
552 | ||
553 | /* From ip_conntrack_proto_udp.c */ | |
babbdb1a PM |
554 | extern unsigned int ip_ct_udp_timeout; |
555 | extern unsigned int ip_ct_udp_timeout_stream; | |
1da177e4 LT |
556 | |
557 | /* From ip_conntrack_proto_icmp.c */ | |
babbdb1a | 558 | extern unsigned int ip_ct_icmp_timeout; |
1da177e4 LT |
559 | |
560 | /* From ip_conntrack_proto_icmp.c */ | |
babbdb1a | 561 | extern unsigned int ip_ct_generic_timeout; |
1da177e4 LT |
562 | |
563 | /* Log invalid packets of a given protocol */ | |
564 | static int log_invalid_proto_min = 0; | |
565 | static int log_invalid_proto_max = 255; | |
566 | ||
567 | static struct ctl_table_header *ip_ct_sysctl_header; | |
568 | ||
569 | static ctl_table ip_ct_sysctl_table[] = { | |
570 | { | |
571 | .ctl_name = NET_IPV4_NF_CONNTRACK_MAX, | |
572 | .procname = "ip_conntrack_max", | |
573 | .data = &ip_conntrack_max, | |
574 | .maxlen = sizeof(int), | |
575 | .mode = 0644, | |
576 | .proc_handler = &proc_dointvec, | |
577 | }, | |
578 | { | |
579 | .ctl_name = NET_IPV4_NF_CONNTRACK_COUNT, | |
580 | .procname = "ip_conntrack_count", | |
581 | .data = &ip_conntrack_count, | |
582 | .maxlen = sizeof(int), | |
583 | .mode = 0444, | |
584 | .proc_handler = &proc_dointvec, | |
585 | }, | |
586 | { | |
587 | .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS, | |
588 | .procname = "ip_conntrack_buckets", | |
589 | .data = &ip_conntrack_htable_size, | |
590 | .maxlen = sizeof(unsigned int), | |
591 | .mode = 0444, | |
592 | .proc_handler = &proc_dointvec, | |
593 | }, | |
594 | { | |
595 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT, | |
596 | .procname = "ip_conntrack_tcp_timeout_syn_sent", | |
597 | .data = &ip_ct_tcp_timeout_syn_sent, | |
598 | .maxlen = sizeof(unsigned int), | |
599 | .mode = 0644, | |
600 | .proc_handler = &proc_dointvec_jiffies, | |
601 | }, | |
602 | { | |
603 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV, | |
604 | .procname = "ip_conntrack_tcp_timeout_syn_recv", | |
605 | .data = &ip_ct_tcp_timeout_syn_recv, | |
606 | .maxlen = sizeof(unsigned int), | |
607 | .mode = 0644, | |
608 | .proc_handler = &proc_dointvec_jiffies, | |
609 | }, | |
610 | { | |
611 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED, | |
612 | .procname = "ip_conntrack_tcp_timeout_established", | |
613 | .data = &ip_ct_tcp_timeout_established, | |
614 | .maxlen = sizeof(unsigned int), | |
615 | .mode = 0644, | |
616 | .proc_handler = &proc_dointvec_jiffies, | |
617 | }, | |
618 | { | |
619 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT, | |
620 | .procname = "ip_conntrack_tcp_timeout_fin_wait", | |
621 | .data = &ip_ct_tcp_timeout_fin_wait, | |
622 | .maxlen = sizeof(unsigned int), | |
623 | .mode = 0644, | |
624 | .proc_handler = &proc_dointvec_jiffies, | |
625 | }, | |
626 | { | |
627 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT, | |
628 | .procname = "ip_conntrack_tcp_timeout_close_wait", | |
629 | .data = &ip_ct_tcp_timeout_close_wait, | |
630 | .maxlen = sizeof(unsigned int), | |
631 | .mode = 0644, | |
632 | .proc_handler = &proc_dointvec_jiffies, | |
633 | }, | |
634 | { | |
635 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK, | |
636 | .procname = "ip_conntrack_tcp_timeout_last_ack", | |
637 | .data = &ip_ct_tcp_timeout_last_ack, | |
638 | .maxlen = sizeof(unsigned int), | |
639 | .mode = 0644, | |
640 | .proc_handler = &proc_dointvec_jiffies, | |
641 | }, | |
642 | { | |
643 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT, | |
644 | .procname = "ip_conntrack_tcp_timeout_time_wait", | |
645 | .data = &ip_ct_tcp_timeout_time_wait, | |
646 | .maxlen = sizeof(unsigned int), | |
647 | .mode = 0644, | |
648 | .proc_handler = &proc_dointvec_jiffies, | |
649 | }, | |
650 | { | |
651 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE, | |
652 | .procname = "ip_conntrack_tcp_timeout_close", | |
653 | .data = &ip_ct_tcp_timeout_close, | |
654 | .maxlen = sizeof(unsigned int), | |
655 | .mode = 0644, | |
656 | .proc_handler = &proc_dointvec_jiffies, | |
657 | }, | |
658 | { | |
659 | .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT, | |
660 | .procname = "ip_conntrack_udp_timeout", | |
661 | .data = &ip_ct_udp_timeout, | |
662 | .maxlen = sizeof(unsigned int), | |
663 | .mode = 0644, | |
664 | .proc_handler = &proc_dointvec_jiffies, | |
665 | }, | |
666 | { | |
667 | .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM, | |
668 | .procname = "ip_conntrack_udp_timeout_stream", | |
669 | .data = &ip_ct_udp_timeout_stream, | |
670 | .maxlen = sizeof(unsigned int), | |
671 | .mode = 0644, | |
672 | .proc_handler = &proc_dointvec_jiffies, | |
673 | }, | |
674 | { | |
675 | .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT, | |
676 | .procname = "ip_conntrack_icmp_timeout", | |
677 | .data = &ip_ct_icmp_timeout, | |
678 | .maxlen = sizeof(unsigned int), | |
679 | .mode = 0644, | |
680 | .proc_handler = &proc_dointvec_jiffies, | |
681 | }, | |
682 | { | |
683 | .ctl_name = NET_IPV4_NF_CONNTRACK_GENERIC_TIMEOUT, | |
684 | .procname = "ip_conntrack_generic_timeout", | |
685 | .data = &ip_ct_generic_timeout, | |
686 | .maxlen = sizeof(unsigned int), | |
687 | .mode = 0644, | |
688 | .proc_handler = &proc_dointvec_jiffies, | |
689 | }, | |
690 | { | |
691 | .ctl_name = NET_IPV4_NF_CONNTRACK_LOG_INVALID, | |
692 | .procname = "ip_conntrack_log_invalid", | |
693 | .data = &ip_ct_log_invalid, | |
694 | .maxlen = sizeof(unsigned int), | |
695 | .mode = 0644, | |
696 | .proc_handler = &proc_dointvec_minmax, | |
697 | .strategy = &sysctl_intvec, | |
698 | .extra1 = &log_invalid_proto_min, | |
699 | .extra2 = &log_invalid_proto_max, | |
700 | }, | |
701 | { | |
702 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS, | |
703 | .procname = "ip_conntrack_tcp_timeout_max_retrans", | |
704 | .data = &ip_ct_tcp_timeout_max_retrans, | |
705 | .maxlen = sizeof(unsigned int), | |
706 | .mode = 0644, | |
707 | .proc_handler = &proc_dointvec_jiffies, | |
708 | }, | |
709 | { | |
710 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_LOOSE, | |
711 | .procname = "ip_conntrack_tcp_loose", | |
712 | .data = &ip_ct_tcp_loose, | |
713 | .maxlen = sizeof(unsigned int), | |
714 | .mode = 0644, | |
715 | .proc_handler = &proc_dointvec, | |
716 | }, | |
717 | { | |
718 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL, | |
719 | .procname = "ip_conntrack_tcp_be_liberal", | |
720 | .data = &ip_ct_tcp_be_liberal, | |
721 | .maxlen = sizeof(unsigned int), | |
722 | .mode = 0644, | |
723 | .proc_handler = &proc_dointvec, | |
724 | }, | |
725 | { | |
726 | .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS, | |
727 | .procname = "ip_conntrack_tcp_max_retrans", | |
728 | .data = &ip_ct_tcp_max_retrans, | |
729 | .maxlen = sizeof(unsigned int), | |
730 | .mode = 0644, | |
731 | .proc_handler = &proc_dointvec, | |
732 | }, | |
733 | { .ctl_name = 0 } | |
734 | }; | |
735 | ||
736 | #define NET_IP_CONNTRACK_MAX 2089 | |
737 | ||
738 | static ctl_table ip_ct_netfilter_table[] = { | |
739 | { | |
740 | .ctl_name = NET_IPV4_NETFILTER, | |
741 | .procname = "netfilter", | |
742 | .mode = 0555, | |
743 | .child = ip_ct_sysctl_table, | |
744 | }, | |
745 | { | |
746 | .ctl_name = NET_IP_CONNTRACK_MAX, | |
747 | .procname = "ip_conntrack_max", | |
748 | .data = &ip_conntrack_max, | |
749 | .maxlen = sizeof(int), | |
750 | .mode = 0644, | |
751 | .proc_handler = &proc_dointvec | |
752 | }, | |
753 | { .ctl_name = 0 } | |
754 | }; | |
755 | ||
756 | static ctl_table ip_ct_ipv4_table[] = { | |
757 | { | |
758 | .ctl_name = NET_IPV4, | |
759 | .procname = "ipv4", | |
760 | .mode = 0555, | |
761 | .child = ip_ct_netfilter_table, | |
762 | }, | |
763 | { .ctl_name = 0 } | |
764 | }; | |
765 | ||
766 | static ctl_table ip_ct_net_table[] = { | |
767 | { | |
768 | .ctl_name = CTL_NET, | |
769 | .procname = "net", | |
770 | .mode = 0555, | |
771 | .child = ip_ct_ipv4_table, | |
772 | }, | |
773 | { .ctl_name = 0 } | |
774 | }; | |
775 | ||
776 | EXPORT_SYMBOL(ip_ct_log_invalid); | |
777 | #endif /* CONFIG_SYSCTL */ | |
778 | ||
32292a7f PM |
779 | /* FIXME: Allow NULL functions and sub in pointers to generic for |
780 | them. --RR */ | |
781 | int ip_conntrack_protocol_register(struct ip_conntrack_protocol *proto) | |
782 | { | |
783 | int ret = 0; | |
784 | ||
785 | write_lock_bh(&ip_conntrack_lock); | |
786 | if (ip_ct_protos[proto->proto] != &ip_conntrack_generic_protocol) { | |
787 | ret = -EBUSY; | |
788 | goto out; | |
789 | } | |
790 | ip_ct_protos[proto->proto] = proto; | |
791 | out: | |
792 | write_unlock_bh(&ip_conntrack_lock); | |
793 | return ret; | |
794 | } | |
795 | ||
796 | void ip_conntrack_protocol_unregister(struct ip_conntrack_protocol *proto) | |
797 | { | |
798 | write_lock_bh(&ip_conntrack_lock); | |
799 | ip_ct_protos[proto->proto] = &ip_conntrack_generic_protocol; | |
800 | write_unlock_bh(&ip_conntrack_lock); | |
801 | ||
802 | /* Somebody could be still looking at the proto in bh. */ | |
803 | synchronize_net(); | |
804 | ||
805 | /* Remove all contrack entries for this protocol */ | |
806 | ip_ct_iterate_cleanup(kill_proto, &proto->proto); | |
807 | } | |
808 | ||
809 | static int __init ip_conntrack_standalone_init(void) | |
1da177e4 LT |
810 | { |
811 | #ifdef CONFIG_PROC_FS | |
812 | struct proc_dir_entry *proc, *proc_exp, *proc_stat; | |
813 | #endif | |
814 | int ret = 0; | |
815 | ||
1da177e4 LT |
816 | ret = ip_conntrack_init(); |
817 | if (ret < 0) | |
32292a7f | 818 | return ret; |
1da177e4 LT |
819 | |
820 | #ifdef CONFIG_PROC_FS | |
821 | ret = -ENOMEM; | |
822 | proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops); | |
823 | if (!proc) goto cleanup_init; | |
824 | ||
825 | proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440, | |
826 | &exp_file_ops); | |
827 | if (!proc_exp) goto cleanup_proc; | |
828 | ||
829 | proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat); | |
830 | if (!proc_stat) | |
831 | goto cleanup_proc_exp; | |
832 | ||
833 | proc_stat->proc_fops = &ct_cpu_seq_fops; | |
834 | proc_stat->owner = THIS_MODULE; | |
835 | #endif | |
836 | ||
964ddaa1 | 837 | ret = nf_register_hooks(ip_conntrack_ops, ARRAY_SIZE(ip_conntrack_ops)); |
1da177e4 | 838 | if (ret < 0) { |
964ddaa1 | 839 | printk("ip_conntrack: can't register hooks.\n"); |
1da177e4 LT |
840 | goto cleanup_proc_stat; |
841 | } | |
1da177e4 LT |
842 | #ifdef CONFIG_SYSCTL |
843 | ip_ct_sysctl_header = register_sysctl_table(ip_ct_net_table, 0); | |
844 | if (ip_ct_sysctl_header == NULL) { | |
845 | printk("ip_conntrack: can't register to sysctl.\n"); | |
846 | ret = -ENOMEM; | |
964ddaa1 | 847 | goto cleanup_hooks; |
1da177e4 LT |
848 | } |
849 | #endif | |
1da177e4 LT |
850 | return ret; |
851 | ||
1da177e4 | 852 | #ifdef CONFIG_SYSCTL |
964ddaa1 | 853 | cleanup_hooks: |
964ddaa1 | 854 | nf_unregister_hooks(ip_conntrack_ops, ARRAY_SIZE(ip_conntrack_ops)); |
32292a7f | 855 | #endif |
1da177e4 LT |
856 | cleanup_proc_stat: |
857 | #ifdef CONFIG_PROC_FS | |
858 | remove_proc_entry("ip_conntrack", proc_net_stat); | |
859 | cleanup_proc_exp: | |
860 | proc_net_remove("ip_conntrack_expect"); | |
861 | cleanup_proc: | |
862 | proc_net_remove("ip_conntrack"); | |
863 | cleanup_init: | |
864 | #endif /* CONFIG_PROC_FS */ | |
865 | ip_conntrack_cleanup(); | |
1da177e4 LT |
866 | return ret; |
867 | } | |
868 | ||
65b4b4e8 | 869 | static void __exit ip_conntrack_standalone_fini(void) |
1da177e4 | 870 | { |
32292a7f PM |
871 | synchronize_net(); |
872 | #ifdef CONFIG_SYSCTL | |
873 | unregister_sysctl_table(ip_ct_sysctl_header); | |
874 | #endif | |
875 | nf_unregister_hooks(ip_conntrack_ops, ARRAY_SIZE(ip_conntrack_ops)); | |
876 | #ifdef CONFIG_PROC_FS | |
877 | remove_proc_entry("ip_conntrack", proc_net_stat); | |
878 | proc_net_remove("ip_conntrack_expect"); | |
879 | proc_net_remove("ip_conntrack"); | |
880 | #endif /* CONFIG_PROC_FS */ | |
881 | ip_conntrack_cleanup(); | |
1da177e4 LT |
882 | } |
883 | ||
65b4b4e8 AM |
884 | module_init(ip_conntrack_standalone_init); |
885 | module_exit(ip_conntrack_standalone_fini); | |
1da177e4 LT |
886 | |
887 | /* Some modules need us, but don't depend directly on any symbol. | |
888 | They should call this. */ | |
2e4e6a17 | 889 | void need_conntrack(void) |
1da177e4 LT |
890 | { |
891 | } | |
892 | ||
ac3247ba HW |
893 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS |
894 | EXPORT_SYMBOL_GPL(ip_conntrack_chain); | |
895 | EXPORT_SYMBOL_GPL(ip_conntrack_expect_chain); | |
896 | EXPORT_SYMBOL_GPL(ip_conntrack_register_notifier); | |
897 | EXPORT_SYMBOL_GPL(ip_conntrack_unregister_notifier); | |
a86888b9 | 898 | EXPORT_SYMBOL_GPL(__ip_ct_event_cache_init); |
ac3247ba HW |
899 | EXPORT_PER_CPU_SYMBOL_GPL(ip_conntrack_ecache); |
900 | #endif | |
1da177e4 LT |
901 | EXPORT_SYMBOL(ip_conntrack_protocol_register); |
902 | EXPORT_SYMBOL(ip_conntrack_protocol_unregister); | |
903 | EXPORT_SYMBOL(ip_ct_get_tuple); | |
904 | EXPORT_SYMBOL(invert_tuplepr); | |
905 | EXPORT_SYMBOL(ip_conntrack_alter_reply); | |
906 | EXPORT_SYMBOL(ip_conntrack_destroyed); | |
2e4e6a17 | 907 | EXPORT_SYMBOL(need_conntrack); |
1da177e4 LT |
908 | EXPORT_SYMBOL(ip_conntrack_helper_register); |
909 | EXPORT_SYMBOL(ip_conntrack_helper_unregister); | |
910 | EXPORT_SYMBOL(ip_ct_iterate_cleanup); | |
1dfbab59 | 911 | EXPORT_SYMBOL(__ip_ct_refresh_acct); |
080774a2 | 912 | |
1da177e4 | 913 | EXPORT_SYMBOL(ip_conntrack_expect_alloc); |
4acdbdbe | 914 | EXPORT_SYMBOL(ip_conntrack_expect_put); |
a41bc002 PM |
915 | EXPORT_SYMBOL_GPL(__ip_conntrack_expect_find); |
916 | EXPORT_SYMBOL_GPL(ip_conntrack_expect_find); | |
1da177e4 LT |
917 | EXPORT_SYMBOL(ip_conntrack_expect_related); |
918 | EXPORT_SYMBOL(ip_conntrack_unexpect_related); | |
080774a2 | 919 | EXPORT_SYMBOL_GPL(ip_conntrack_expect_list); |
49719eb3 | 920 | EXPORT_SYMBOL_GPL(ip_ct_unlink_expect); |
080774a2 | 921 | |
1da177e4 LT |
922 | EXPORT_SYMBOL(ip_conntrack_tuple_taken); |
923 | EXPORT_SYMBOL(ip_ct_gather_frags); | |
924 | EXPORT_SYMBOL(ip_conntrack_htable_size); | |
925 | EXPORT_SYMBOL(ip_conntrack_lock); | |
926 | EXPORT_SYMBOL(ip_conntrack_hash); | |
927 | EXPORT_SYMBOL(ip_conntrack_untracked); | |
928 | EXPORT_SYMBOL_GPL(ip_conntrack_find_get); | |
1da177e4 LT |
929 | #ifdef CONFIG_IP_NF_NAT_NEEDED |
930 | EXPORT_SYMBOL(ip_conntrack_tcp_update); | |
931 | #endif | |
080774a2 HW |
932 | |
933 | EXPORT_SYMBOL_GPL(ip_conntrack_flush); | |
934 | EXPORT_SYMBOL_GPL(__ip_conntrack_find); | |
935 | ||
936 | EXPORT_SYMBOL_GPL(ip_conntrack_alloc); | |
937 | EXPORT_SYMBOL_GPL(ip_conntrack_free); | |
938 | EXPORT_SYMBOL_GPL(ip_conntrack_hash_insert); | |
939 | ||
940 | EXPORT_SYMBOL_GPL(ip_ct_remove_expectations); | |
941 | ||
942 | EXPORT_SYMBOL_GPL(ip_conntrack_helper_find_get); | |
943 | EXPORT_SYMBOL_GPL(ip_conntrack_helper_put); | |
944 | EXPORT_SYMBOL_GPL(__ip_conntrack_helper_find_byname); | |
945 | ||
946 | EXPORT_SYMBOL_GPL(ip_conntrack_proto_find_get); | |
947 | EXPORT_SYMBOL_GPL(ip_conntrack_proto_put); | |
948 | EXPORT_SYMBOL_GPL(__ip_conntrack_proto_find); | |
949 | #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \ | |
950 | defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE) | |
951 | EXPORT_SYMBOL_GPL(ip_ct_port_tuple_to_nfattr); | |
952 | EXPORT_SYMBOL_GPL(ip_ct_port_nfattr_to_tuple); | |
953 | #endif |