nf_conntrack_standalone.c 10.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* (C) 1999-2001 Paul `Rusty' Russell
 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/types.h>
#include <linux/netfilter.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/percpu.h>
#include <linux/netdevice.h>
17
#include <net/net_namespace.h>
18
19
20
21
22
#ifdef CONFIG_SYSCTL
#include <linux/sysctl.h>
#endif

#include <net/netfilter/nf_conntrack.h>
23
#include <net/netfilter/nf_conntrack_core.h>
24
#include <net/netfilter/nf_conntrack_l3proto.h>
25
#include <net/netfilter/nf_conntrack_l4proto.h>
26
#include <net/netfilter/nf_conntrack_expect.h>
27
28
29
30
31
#include <net/netfilter/nf_conntrack_helper.h>

MODULE_LICENSE("GPL");

#ifdef CONFIG_PROC_FS
32
int
33
34
print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
	    struct nf_conntrack_l3proto *l3proto,
35
	    struct nf_conntrack_l4proto *l4proto)
36
{
37
	return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
38
}
39
EXPORT_SYMBOL_GPL(print_tuple);
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#ifdef CONFIG_NF_CT_ACCT
static unsigned int
seq_print_counters(struct seq_file *s,
		   const struct ip_conntrack_counter *counter)
{
	return seq_printf(s, "packets=%llu bytes=%llu ",
			  (unsigned long long)counter->packets,
			  (unsigned long long)counter->bytes);
}
#else
#define seq_print_counters(x, y)	0
#endif

struct ct_iter_state {
	unsigned int bucket;
};

58
static struct hlist_node *ct_get_first(struct seq_file *seq)
59
60
61
62
63
64
{
	struct ct_iter_state *st = seq->private;

	for (st->bucket = 0;
	     st->bucket < nf_conntrack_htable_size;
	     st->bucket++) {
65
66
		if (!hlist_empty(&nf_conntrack_hash[st->bucket]))
			return nf_conntrack_hash[st->bucket].first;
67
68
69
70
	}
	return NULL;
}

71
72
static struct hlist_node *ct_get_next(struct seq_file *seq,
				      struct hlist_node *head)
73
74
75
76
{
	struct ct_iter_state *st = seq->private;

	head = head->next;
77
	while (head == NULL) {
78
79
		if (++st->bucket >= nf_conntrack_htable_size)
			return NULL;
80
		head = nf_conntrack_hash[st->bucket].first;
81
82
83
84
	}
	return head;
}

85
static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
86
{
87
	struct hlist_node *head = ct_get_first(seq);
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

	if (head)
		while (pos && (head = ct_get_next(seq, head)))
			pos--;
	return pos ? NULL : head;
}

static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
{
	read_lock_bh(&nf_conntrack_lock);
	return ct_get_idx(seq, *pos);
}

static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
	(*pos)++;
	return ct_get_next(s, v);
}

static void ct_seq_stop(struct seq_file *s, void *v)
{
	read_unlock_bh(&nf_conntrack_lock);
}

/* return 0 on success, 1 in case of error */
static int ct_seq_show(struct seq_file *s, void *v)
{
	const struct nf_conntrack_tuple_hash *hash = v;
	const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
	struct nf_conntrack_l3proto *l3proto;
118
	struct nf_conntrack_l4proto *l4proto;
119
120
121
122
123
124
125

	NF_CT_ASSERT(conntrack);

	/* we only want to print DIR_ORIGINAL */
	if (NF_CT_DIRECTION(hash))
		return 0;

126
127
	l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
				       .tuple.src.l3num);
128
129

	NF_CT_ASSERT(l3proto);
130
	l4proto = __nf_ct_l4proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
131
132
133
				   .tuple.src.l3num,
				   conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
				   .tuple.dst.protonum);
134
	NF_CT_ASSERT(l4proto);
135
136
137
138

	if (seq_printf(s, "%-8s %u %-8s %u %ld ",
		       l3proto->name,
		       conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
139
		       l4proto->name,
140
141
142
143
144
		       conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
		       timer_pending(&conntrack->timeout)
		       ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
		return -ENOSPC;

145
	if (l4proto->print_conntrack(s, conntrack))
146
147
148
		return -ENOSPC;

	if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
149
			l3proto, l4proto))
150
151
152
153
154
155
156
157
158
159
		return -ENOSPC;

	if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
		return -ENOSPC;

	if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
		if (seq_printf(s, "[UNREPLIED] "))
			return -ENOSPC;

	if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
160
			l3proto, l4proto))
161
162
163
164
165
166
167
168
169
170
171
172
173
174
		return -ENOSPC;

	if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
		return -ENOSPC;

	if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
		if (seq_printf(s, "[ASSURED] "))
			return -ENOSPC;

#if defined(CONFIG_NF_CONNTRACK_MARK)
	if (seq_printf(s, "mark=%u ", conntrack->mark))
		return -ENOSPC;
#endif

175
176
177
178
179
#ifdef CONFIG_NF_CONNTRACK_SECMARK
	if (seq_printf(s, "secmark=%u ", conntrack->secmark))
		return -ENOSPC;
#endif

180
181
	if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
		return -ENOSPC;
182

183
184
185
	return 0;
}

186
static const struct seq_operations ct_seq_ops = {
187
188
189
190
191
192
193
194
	.start = ct_seq_start,
	.next  = ct_seq_next,
	.stop  = ct_seq_stop,
	.show  = ct_seq_show
};

static int ct_open(struct inode *inode, struct file *file)
{
195
196
	return seq_open_private(file, &ct_seq_ops,
			sizeof(struct ct_iter_state));
197
198
}

199
static const struct file_operations ct_file_ops = {
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
	.owner   = THIS_MODULE,
	.open    = ct_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release_private,
};

static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
{
	int cpu;

	if (*pos == 0)
		return SEQ_START_TOKEN;

	for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
		if (!cpu_possible(cpu))
			continue;
		*pos = cpu + 1;
		return &per_cpu(nf_conntrack_stat, cpu);
	}

	return NULL;
}

static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	int cpu;

	for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
		if (!cpu_possible(cpu))
			continue;
		*pos = cpu + 1;
		return &per_cpu(nf_conntrack_stat, cpu);
	}

	return NULL;
}

static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
{
}

static int ct_cpu_seq_show(struct seq_file *seq, void *v)
{
	unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
	struct ip_conntrack_stat *st = v;

	if (v == SEQ_START_TOKEN) {
		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");
		return 0;
	}

	seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
			"%08x %08x %08x %08x %08x  %08x %08x %08x \n",
		   nr_conntracks,
		   st->searched,
		   st->found,
		   st->new,
		   st->invalid,
		   st->ignore,
		   st->delete,
		   st->delete_list,
		   st->insert,
		   st->insert_failed,
		   st->drop,
		   st->early_drop,
		   st->error,

		   st->expect_new,
		   st->expect_create,
		   st->expect_delete
		);
	return 0;
}

275
static const struct seq_operations ct_cpu_seq_ops = {
276
277
278
279
280
281
282
283
284
285
286
	.start	= ct_cpu_seq_start,
	.next	= ct_cpu_seq_next,
	.stop	= ct_cpu_seq_stop,
	.show	= ct_cpu_seq_show,
};

static int ct_cpu_seq_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &ct_cpu_seq_ops);
}

287
static const struct file_operations ct_cpu_seq_fops = {
288
289
290
291
292
293
294
295
296
297
	.owner	 = THIS_MODULE,
	.open	 = ct_cpu_seq_open,
	.read	 = seq_read,
	.llseek	 = seq_lseek,
	.release = seq_release_private,
};
#endif /* CONFIG_PROC_FS */

/* Sysctl support */

298
int nf_conntrack_checksum __read_mostly = 1;
299
EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
300

301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#ifdef CONFIG_SYSCTL
/* Log invalid packets of a given protocol */
static int log_invalid_proto_min = 0;
static int log_invalid_proto_max = 255;

static struct ctl_table_header *nf_ct_sysctl_header;

static ctl_table nf_ct_sysctl_table[] = {
	{
		.ctl_name	= NET_NF_CONNTRACK_MAX,
		.procname	= "nf_conntrack_max",
		.data		= &nf_conntrack_max,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_dointvec,
	},
	{
		.ctl_name	= NET_NF_CONNTRACK_COUNT,
		.procname	= "nf_conntrack_count",
		.data		= &nf_conntrack_count,
		.maxlen		= sizeof(int),
		.mode		= 0444,
		.proc_handler	= &proc_dointvec,
	},
	{
		.ctl_name       = NET_NF_CONNTRACK_BUCKETS,
		.procname       = "nf_conntrack_buckets",
		.data           = &nf_conntrack_htable_size,
		.maxlen         = sizeof(unsigned int),
		.mode           = 0444,
		.proc_handler   = &proc_dointvec,
	},
333
334
335
336
337
338
339
340
	{
		.ctl_name	= NET_NF_CONNTRACK_CHECKSUM,
		.procname	= "nf_conntrack_checksum",
		.data		= &nf_conntrack_checksum,
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
		.proc_handler	= &proc_dointvec,
	},
341
342
343
344
345
346
347
348
349
350
351
	{
		.ctl_name	= NET_NF_CONNTRACK_LOG_INVALID,
		.procname	= "nf_conntrack_log_invalid",
		.data		= &nf_ct_log_invalid,
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
		.proc_handler	= &proc_dointvec_minmax,
		.strategy	= &sysctl_intvec,
		.extra1		= &log_invalid_proto_min,
		.extra2		= &log_invalid_proto_max,
	},
352
353
354
355
356
357
358
359
	{
		.ctl_name	= CTL_UNNUMBERED,
		.procname	= "nf_conntrack_expect_max",
		.data		= &nf_ct_expect_max,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_dointvec,
	},
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
	{ .ctl_name = 0 }
};

#define NET_NF_CONNTRACK_MAX 2089

static ctl_table nf_ct_netfilter_table[] = {
	{
		.ctl_name	= NET_NETFILTER,
		.procname	= "netfilter",
		.mode		= 0555,
		.child		= nf_ct_sysctl_table,
	},
	{
		.ctl_name	= NET_NF_CONNTRACK_MAX,
		.procname	= "nf_conntrack_max",
		.data		= &nf_conntrack_max,
		.maxlen		= sizeof(int),
		.mode		= 0644,
		.proc_handler	= &proc_dointvec,
	},
	{ .ctl_name = 0 }
};

383
384
385
struct ctl_path nf_ct_path[] = {
	{ .procname = "net", .ctl_name = CTL_NET, },
	{ }
386
};
387

388
EXPORT_SYMBOL_GPL(nf_ct_log_invalid);
389
390
#endif /* CONFIG_SYSCTL */

391
static int __init nf_conntrack_standalone_init(void)
392
{
393
#ifdef CONFIG_PROC_FS
394
	struct proc_dir_entry *proc, *proc_stat;
395
396
397
398
399
400
401
402
#endif
	int ret = 0;

	ret = nf_conntrack_init();
	if (ret < 0)
		return ret;

#ifdef CONFIG_PROC_FS
403
	proc = proc_net_fops_create(&init_net, "nf_conntrack", 0440, &ct_file_ops);
404
405
	if (!proc) goto cleanup_init;

406
	proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, init_net.proc_net_stat);
407
	if (!proc_stat)
408
		goto cleanup_proc;
409
410
411
412
413

	proc_stat->proc_fops = &ct_cpu_seq_fops;
	proc_stat->owner = THIS_MODULE;
#endif
#ifdef CONFIG_SYSCTL
414
415
	nf_ct_sysctl_header = register_sysctl_paths(nf_ct_path,
			nf_ct_netfilter_table);
416
417
418
419
420
421
422
423
424
425
426
427
	if (nf_ct_sysctl_header == NULL) {
		printk("nf_conntrack: can't register to sysctl.\n");
		ret = -ENOMEM;
		goto cleanup_proc_stat;
	}
#endif
	return ret;

#ifdef CONFIG_SYSCTL
 cleanup_proc_stat:
#endif
#ifdef CONFIG_PROC_FS
428
	remove_proc_entry("nf_conntrack", init_net. proc_net_stat);
429
 cleanup_proc:
430
	proc_net_remove(&init_net, "nf_conntrack");
431
432
433
434
 cleanup_init:
#endif /* CNFIG_PROC_FS */
	nf_conntrack_cleanup();
	return ret;
435
436
}

437
static void __exit nf_conntrack_standalone_fini(void)
438
{
439
#ifdef CONFIG_SYSCTL
440
	unregister_sysctl_table(nf_ct_sysctl_header);
441
442
#endif
#ifdef CONFIG_PROC_FS
443
444
	remove_proc_entry("nf_conntrack", init_net.proc_net_stat);
	proc_net_remove(&init_net, "nf_conntrack");
445
446
#endif /* CNFIG_PROC_FS */
	nf_conntrack_cleanup();
447
448
}

449
450
module_init(nf_conntrack_standalone_init);
module_exit(nf_conntrack_standalone_fini);
451
452
453

/* Some modules need us, but don't depend directly on any symbol.
   They should call this. */
454
void need_conntrack(void)
455
456
{
}
457
EXPORT_SYMBOL_GPL(need_conntrack);