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