Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
1 | /* (C) 1999-2001 Paul `Rusty' Russell |
2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
9fb9cbb1 YK |
7 | */ |
8 | ||
9fb9cbb1 YK |
9 | #include <linux/types.h> |
10 | #include <linux/netfilter.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/skbuff.h> | |
13 | #include <linux/proc_fs.h> | |
14 | #include <linux/seq_file.h> | |
15 | #include <linux/percpu.h> | |
16 | #include <linux/netdevice.h> | |
17 | #ifdef CONFIG_SYSCTL | |
18 | #include <linux/sysctl.h> | |
19 | #endif | |
20 | ||
9fb9cbb1 | 21 | #include <net/netfilter/nf_conntrack.h> |
f6180121 | 22 | #include <net/netfilter/nf_conntrack_core.h> |
9fb9cbb1 | 23 | #include <net/netfilter/nf_conntrack_l3proto.h> |
605dcad6 | 24 | #include <net/netfilter/nf_conntrack_l4proto.h> |
77ab9cff | 25 | #include <net/netfilter/nf_conntrack_expect.h> |
9fb9cbb1 | 26 | #include <net/netfilter/nf_conntrack_helper.h> |
9fb9cbb1 | 27 | |
9fb9cbb1 YK |
28 | MODULE_LICENSE("GPL"); |
29 | ||
9fb9cbb1 | 30 | #ifdef CONFIG_PROC_FS |
77ab9cff | 31 | int |
9fb9cbb1 YK |
32 | print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple, |
33 | struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 34 | struct nf_conntrack_l4proto *l4proto) |
9fb9cbb1 | 35 | { |
605dcad6 | 36 | return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple); |
9fb9cbb1 | 37 | } |
e4bd8bce | 38 | EXPORT_SYMBOL_GPL(print_tuple); |
9fb9cbb1 YK |
39 | |
40 | #ifdef CONFIG_NF_CT_ACCT | |
41 | static unsigned int | |
42 | seq_print_counters(struct seq_file *s, | |
43 | const struct ip_conntrack_counter *counter) | |
44 | { | |
45 | return seq_printf(s, "packets=%llu bytes=%llu ", | |
46 | (unsigned long long)counter->packets, | |
47 | (unsigned long long)counter->bytes); | |
48 | } | |
49 | #else | |
50 | #define seq_print_counters(x, y) 0 | |
51 | #endif | |
52 | ||
53 | struct ct_iter_state { | |
54 | unsigned int bucket; | |
55 | }; | |
56 | ||
f205c5e0 | 57 | static struct hlist_node *ct_get_first(struct seq_file *seq) |
9fb9cbb1 YK |
58 | { |
59 | struct ct_iter_state *st = seq->private; | |
60 | ||
61 | for (st->bucket = 0; | |
62 | st->bucket < nf_conntrack_htable_size; | |
63 | st->bucket++) { | |
f205c5e0 PM |
64 | if (!hlist_empty(&nf_conntrack_hash[st->bucket])) |
65 | return nf_conntrack_hash[st->bucket].first; | |
9fb9cbb1 YK |
66 | } |
67 | return NULL; | |
68 | } | |
69 | ||
f205c5e0 PM |
70 | static struct hlist_node *ct_get_next(struct seq_file *seq, |
71 | struct hlist_node *head) | |
9fb9cbb1 YK |
72 | { |
73 | struct ct_iter_state *st = seq->private; | |
74 | ||
75 | head = head->next; | |
f205c5e0 | 76 | while (head == NULL) { |
9fb9cbb1 YK |
77 | if (++st->bucket >= nf_conntrack_htable_size) |
78 | return NULL; | |
f205c5e0 | 79 | head = nf_conntrack_hash[st->bucket].first; |
9fb9cbb1 YK |
80 | } |
81 | return head; | |
82 | } | |
83 | ||
f205c5e0 | 84 | static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos) |
9fb9cbb1 | 85 | { |
f205c5e0 | 86 | struct hlist_node *head = ct_get_first(seq); |
9fb9cbb1 YK |
87 | |
88 | if (head) | |
89 | while (pos && (head = ct_get_next(seq, head))) | |
90 | pos--; | |
91 | return pos ? NULL : head; | |
92 | } | |
93 | ||
94 | static void *ct_seq_start(struct seq_file *seq, loff_t *pos) | |
95 | { | |
96 | read_lock_bh(&nf_conntrack_lock); | |
97 | return ct_get_idx(seq, *pos); | |
98 | } | |
99 | ||
100 | static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
101 | { | |
102 | (*pos)++; | |
103 | return ct_get_next(s, v); | |
104 | } | |
105 | ||
106 | static void ct_seq_stop(struct seq_file *s, void *v) | |
107 | { | |
108 | read_unlock_bh(&nf_conntrack_lock); | |
109 | } | |
110 | ||
111 | /* return 0 on success, 1 in case of error */ | |
112 | static int ct_seq_show(struct seq_file *s, void *v) | |
113 | { | |
114 | const struct nf_conntrack_tuple_hash *hash = v; | |
115 | const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash); | |
116 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 117 | struct nf_conntrack_l4proto *l4proto; |
9fb9cbb1 | 118 | |
9fb9cbb1 YK |
119 | NF_CT_ASSERT(conntrack); |
120 | ||
121 | /* we only want to print DIR_ORIGINAL */ | |
122 | if (NF_CT_DIRECTION(hash)) | |
123 | return 0; | |
124 | ||
c1d10adb PNA |
125 | l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL] |
126 | .tuple.src.l3num); | |
9fb9cbb1 YK |
127 | |
128 | NF_CT_ASSERT(l3proto); | |
605dcad6 | 129 | l4proto = __nf_ct_l4proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL] |
c1d10adb PNA |
130 | .tuple.src.l3num, |
131 | conntrack->tuplehash[IP_CT_DIR_ORIGINAL] | |
132 | .tuple.dst.protonum); | |
605dcad6 | 133 | NF_CT_ASSERT(l4proto); |
9fb9cbb1 YK |
134 | |
135 | if (seq_printf(s, "%-8s %u %-8s %u %ld ", | |
136 | l3proto->name, | |
137 | conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num, | |
605dcad6 | 138 | l4proto->name, |
9fb9cbb1 YK |
139 | conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum, |
140 | timer_pending(&conntrack->timeout) | |
141 | ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0) | |
142 | return -ENOSPC; | |
143 | ||
144 | if (l3proto->print_conntrack(s, conntrack)) | |
145 | return -ENOSPC; | |
146 | ||
605dcad6 | 147 | if (l4proto->print_conntrack(s, conntrack)) |
9fb9cbb1 YK |
148 | return -ENOSPC; |
149 | ||
150 | if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
605dcad6 | 151 | l3proto, l4proto)) |
9fb9cbb1 YK |
152 | return -ENOSPC; |
153 | ||
154 | if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL])) | |
155 | return -ENOSPC; | |
156 | ||
157 | if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status))) | |
158 | if (seq_printf(s, "[UNREPLIED] ")) | |
159 | return -ENOSPC; | |
160 | ||
161 | if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple, | |
605dcad6 | 162 | l3proto, l4proto)) |
9fb9cbb1 YK |
163 | return -ENOSPC; |
164 | ||
165 | if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY])) | |
166 | return -ENOSPC; | |
167 | ||
168 | if (test_bit(IPS_ASSURED_BIT, &conntrack->status)) | |
169 | if (seq_printf(s, "[ASSURED] ")) | |
170 | return -ENOSPC; | |
171 | ||
172 | #if defined(CONFIG_NF_CONNTRACK_MARK) | |
173 | if (seq_printf(s, "mark=%u ", conntrack->mark)) | |
174 | return -ENOSPC; | |
175 | #endif | |
176 | ||
7c9728c3 JM |
177 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
178 | if (seq_printf(s, "secmark=%u ", conntrack->secmark)) | |
179 | return -ENOSPC; | |
180 | #endif | |
181 | ||
9fb9cbb1 YK |
182 | if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use))) |
183 | return -ENOSPC; | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
188 | static struct seq_operations ct_seq_ops = { | |
189 | .start = ct_seq_start, | |
190 | .next = ct_seq_next, | |
191 | .stop = ct_seq_stop, | |
192 | .show = ct_seq_show | |
193 | }; | |
194 | ||
195 | static int ct_open(struct inode *inode, struct file *file) | |
196 | { | |
197 | struct seq_file *seq; | |
198 | struct ct_iter_state *st; | |
199 | int ret; | |
200 | ||
201 | st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL); | |
202 | if (st == NULL) | |
203 | return -ENOMEM; | |
204 | ret = seq_open(file, &ct_seq_ops); | |
205 | if (ret) | |
206 | goto out_free; | |
207 | seq = file->private_data; | |
208 | seq->private = st; | |
209 | memset(st, 0, sizeof(struct ct_iter_state)); | |
210 | return ret; | |
211 | out_free: | |
212 | kfree(st); | |
213 | return ret; | |
214 | } | |
215 | ||
da7071d7 | 216 | static const struct file_operations ct_file_ops = { |
9fb9cbb1 YK |
217 | .owner = THIS_MODULE, |
218 | .open = ct_open, | |
219 | .read = seq_read, | |
220 | .llseek = seq_lseek, | |
221 | .release = seq_release_private, | |
222 | }; | |
223 | ||
9fb9cbb1 YK |
224 | static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos) |
225 | { | |
226 | int cpu; | |
227 | ||
228 | if (*pos == 0) | |
229 | return SEQ_START_TOKEN; | |
230 | ||
231 | for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) { | |
232 | if (!cpu_possible(cpu)) | |
233 | continue; | |
234 | *pos = cpu + 1; | |
235 | return &per_cpu(nf_conntrack_stat, cpu); | |
236 | } | |
237 | ||
238 | return NULL; | |
239 | } | |
240 | ||
241 | static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
242 | { | |
243 | int cpu; | |
244 | ||
245 | for (cpu = *pos; cpu < NR_CPUS; ++cpu) { | |
246 | if (!cpu_possible(cpu)) | |
247 | continue; | |
248 | *pos = cpu + 1; | |
249 | return &per_cpu(nf_conntrack_stat, cpu); | |
250 | } | |
251 | ||
252 | return NULL; | |
253 | } | |
254 | ||
255 | static void ct_cpu_seq_stop(struct seq_file *seq, void *v) | |
256 | { | |
257 | } | |
258 | ||
259 | static int ct_cpu_seq_show(struct seq_file *seq, void *v) | |
260 | { | |
261 | unsigned int nr_conntracks = atomic_read(&nf_conntrack_count); | |
262 | struct ip_conntrack_stat *st = v; | |
263 | ||
264 | if (v == SEQ_START_TOKEN) { | |
265 | 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"); | |
266 | return 0; | |
267 | } | |
268 | ||
269 | seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x " | |
270 | "%08x %08x %08x %08x %08x %08x %08x %08x \n", | |
271 | nr_conntracks, | |
272 | st->searched, | |
273 | st->found, | |
274 | st->new, | |
275 | st->invalid, | |
276 | st->ignore, | |
277 | st->delete, | |
278 | st->delete_list, | |
279 | st->insert, | |
280 | st->insert_failed, | |
281 | st->drop, | |
282 | st->early_drop, | |
283 | st->error, | |
284 | ||
285 | st->expect_new, | |
286 | st->expect_create, | |
287 | st->expect_delete | |
288 | ); | |
289 | return 0; | |
290 | } | |
291 | ||
292 | static struct seq_operations ct_cpu_seq_ops = { | |
293 | .start = ct_cpu_seq_start, | |
294 | .next = ct_cpu_seq_next, | |
295 | .stop = ct_cpu_seq_stop, | |
296 | .show = ct_cpu_seq_show, | |
297 | }; | |
298 | ||
299 | static int ct_cpu_seq_open(struct inode *inode, struct file *file) | |
300 | { | |
301 | return seq_open(file, &ct_cpu_seq_ops); | |
302 | } | |
303 | ||
da7071d7 | 304 | static const struct file_operations ct_cpu_seq_fops = { |
9fb9cbb1 YK |
305 | .owner = THIS_MODULE, |
306 | .open = ct_cpu_seq_open, | |
307 | .read = seq_read, | |
308 | .llseek = seq_lseek, | |
309 | .release = seq_release_private, | |
310 | }; | |
311 | #endif /* CONFIG_PROC_FS */ | |
312 | ||
313 | /* Sysctl support */ | |
314 | ||
94aec08e | 315 | int nf_conntrack_checksum __read_mostly = 1; |
13b18339 | 316 | EXPORT_SYMBOL_GPL(nf_conntrack_checksum); |
72b55823 | 317 | |
9fb9cbb1 | 318 | #ifdef CONFIG_SYSCTL |
9fb9cbb1 YK |
319 | /* Log invalid packets of a given protocol */ |
320 | static int log_invalid_proto_min = 0; | |
321 | static int log_invalid_proto_max = 255; | |
322 | ||
323 | static struct ctl_table_header *nf_ct_sysctl_header; | |
324 | ||
325 | static ctl_table nf_ct_sysctl_table[] = { | |
326 | { | |
327 | .ctl_name = NET_NF_CONNTRACK_MAX, | |
328 | .procname = "nf_conntrack_max", | |
329 | .data = &nf_conntrack_max, | |
330 | .maxlen = sizeof(int), | |
331 | .mode = 0644, | |
332 | .proc_handler = &proc_dointvec, | |
333 | }, | |
334 | { | |
335 | .ctl_name = NET_NF_CONNTRACK_COUNT, | |
336 | .procname = "nf_conntrack_count", | |
337 | .data = &nf_conntrack_count, | |
338 | .maxlen = sizeof(int), | |
339 | .mode = 0444, | |
340 | .proc_handler = &proc_dointvec, | |
341 | }, | |
342 | { | |
343 | .ctl_name = NET_NF_CONNTRACK_BUCKETS, | |
344 | .procname = "nf_conntrack_buckets", | |
345 | .data = &nf_conntrack_htable_size, | |
346 | .maxlen = sizeof(unsigned int), | |
347 | .mode = 0444, | |
348 | .proc_handler = &proc_dointvec, | |
349 | }, | |
39a27a35 PM |
350 | { |
351 | .ctl_name = NET_NF_CONNTRACK_CHECKSUM, | |
352 | .procname = "nf_conntrack_checksum", | |
353 | .data = &nf_conntrack_checksum, | |
354 | .maxlen = sizeof(unsigned int), | |
355 | .mode = 0644, | |
356 | .proc_handler = &proc_dointvec, | |
357 | }, | |
9fb9cbb1 YK |
358 | { |
359 | .ctl_name = NET_NF_CONNTRACK_LOG_INVALID, | |
360 | .procname = "nf_conntrack_log_invalid", | |
361 | .data = &nf_ct_log_invalid, | |
362 | .maxlen = sizeof(unsigned int), | |
363 | .mode = 0644, | |
364 | .proc_handler = &proc_dointvec_minmax, | |
365 | .strategy = &sysctl_intvec, | |
366 | .extra1 = &log_invalid_proto_min, | |
367 | .extra2 = &log_invalid_proto_max, | |
368 | }, | |
f264a7df PM |
369 | { |
370 | .ctl_name = CTL_UNNUMBERED, | |
371 | .procname = "nf_conntrack_expect_max", | |
372 | .data = &nf_ct_expect_max, | |
373 | .maxlen = sizeof(int), | |
374 | .mode = 0644, | |
375 | .proc_handler = &proc_dointvec, | |
376 | }, | |
9fb9cbb1 YK |
377 | { .ctl_name = 0 } |
378 | }; | |
379 | ||
380 | #define NET_NF_CONNTRACK_MAX 2089 | |
381 | ||
382 | static ctl_table nf_ct_netfilter_table[] = { | |
383 | { | |
384 | .ctl_name = NET_NETFILTER, | |
385 | .procname = "netfilter", | |
386 | .mode = 0555, | |
387 | .child = nf_ct_sysctl_table, | |
388 | }, | |
389 | { | |
390 | .ctl_name = NET_NF_CONNTRACK_MAX, | |
391 | .procname = "nf_conntrack_max", | |
392 | .data = &nf_conntrack_max, | |
393 | .maxlen = sizeof(int), | |
394 | .mode = 0644, | |
395 | .proc_handler = &proc_dointvec, | |
396 | }, | |
397 | { .ctl_name = 0 } | |
398 | }; | |
399 | ||
400 | static ctl_table nf_ct_net_table[] = { | |
401 | { | |
402 | .ctl_name = CTL_NET, | |
403 | .procname = "net", | |
404 | .mode = 0555, | |
405 | .child = nf_ct_netfilter_table, | |
406 | }, | |
407 | { .ctl_name = 0 } | |
408 | }; | |
13b18339 | 409 | EXPORT_SYMBOL_GPL(nf_ct_log_invalid); |
9fb9cbb1 YK |
410 | #endif /* CONFIG_SYSCTL */ |
411 | ||
65b4b4e8 | 412 | static int __init nf_conntrack_standalone_init(void) |
9fb9cbb1 | 413 | { |
32292a7f | 414 | #ifdef CONFIG_PROC_FS |
e9c1b084 | 415 | struct proc_dir_entry *proc, *proc_stat; |
32292a7f PM |
416 | #endif |
417 | int ret = 0; | |
418 | ||
419 | ret = nf_conntrack_init(); | |
420 | if (ret < 0) | |
421 | return ret; | |
422 | ||
423 | #ifdef CONFIG_PROC_FS | |
424 | proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops); | |
425 | if (!proc) goto cleanup_init; | |
426 | ||
32292a7f PM |
427 | proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat); |
428 | if (!proc_stat) | |
e9c1b084 | 429 | goto cleanup_proc; |
32292a7f PM |
430 | |
431 | proc_stat->proc_fops = &ct_cpu_seq_fops; | |
432 | proc_stat->owner = THIS_MODULE; | |
433 | #endif | |
434 | #ifdef CONFIG_SYSCTL | |
0b4d4147 | 435 | nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table); |
32292a7f PM |
436 | if (nf_ct_sysctl_header == NULL) { |
437 | printk("nf_conntrack: can't register to sysctl.\n"); | |
438 | ret = -ENOMEM; | |
439 | goto cleanup_proc_stat; | |
440 | } | |
441 | #endif | |
442 | return ret; | |
443 | ||
444 | #ifdef CONFIG_SYSCTL | |
445 | cleanup_proc_stat: | |
446 | #endif | |
447 | #ifdef CONFIG_PROC_FS | |
448 | remove_proc_entry("nf_conntrack", proc_net_stat); | |
32292a7f PM |
449 | cleanup_proc: |
450 | proc_net_remove("nf_conntrack"); | |
451 | cleanup_init: | |
452 | #endif /* CNFIG_PROC_FS */ | |
453 | nf_conntrack_cleanup(); | |
454 | return ret; | |
9fb9cbb1 YK |
455 | } |
456 | ||
65b4b4e8 | 457 | static void __exit nf_conntrack_standalone_fini(void) |
9fb9cbb1 | 458 | { |
32292a7f | 459 | #ifdef CONFIG_SYSCTL |
601e68e1 | 460 | unregister_sysctl_table(nf_ct_sysctl_header); |
32292a7f PM |
461 | #endif |
462 | #ifdef CONFIG_PROC_FS | |
463 | remove_proc_entry("nf_conntrack", proc_net_stat); | |
32292a7f PM |
464 | proc_net_remove("nf_conntrack"); |
465 | #endif /* CNFIG_PROC_FS */ | |
466 | nf_conntrack_cleanup(); | |
9fb9cbb1 YK |
467 | } |
468 | ||
65b4b4e8 AM |
469 | module_init(nf_conntrack_standalone_init); |
470 | module_exit(nf_conntrack_standalone_fini); | |
9fb9cbb1 YK |
471 | |
472 | /* Some modules need us, but don't depend directly on any symbol. | |
473 | They should call this. */ | |
2e4e6a17 | 474 | void need_conntrack(void) |
9fb9cbb1 YK |
475 | { |
476 | } | |
13b18339 | 477 | EXPORT_SYMBOL_GPL(need_conntrack); |