Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
1 | /* (C) 1999-2001 Paul `Rusty' Russell |
2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> | |
f229f6ce | 3 | * (C) 2005-2012 Patrick McHardy <kaber@trash.net> |
9fb9cbb1 YK |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
9fb9cbb1 YK |
8 | */ |
9 | ||
9fb9cbb1 YK |
10 | #include <linux/types.h> |
11 | #include <linux/netfilter.h> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
9fb9cbb1 YK |
13 | #include <linux/module.h> |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/proc_fs.h> | |
16 | #include <linux/seq_file.h> | |
17 | #include <linux/percpu.h> | |
18 | #include <linux/netdevice.h> | |
1ae4de0c | 19 | #include <linux/security.h> |
457c4cbc | 20 | #include <net/net_namespace.h> |
9fb9cbb1 YK |
21 | #ifdef CONFIG_SYSCTL |
22 | #include <linux/sysctl.h> | |
23 | #endif | |
24 | ||
9fb9cbb1 | 25 | #include <net/netfilter/nf_conntrack.h> |
f6180121 | 26 | #include <net/netfilter/nf_conntrack_core.h> |
9fb9cbb1 | 27 | #include <net/netfilter/nf_conntrack_l3proto.h> |
605dcad6 | 28 | #include <net/netfilter/nf_conntrack_l4proto.h> |
77ab9cff | 29 | #include <net/netfilter/nf_conntrack_expect.h> |
9fb9cbb1 | 30 | #include <net/netfilter/nf_conntrack_helper.h> |
58401572 | 31 | #include <net/netfilter/nf_conntrack_acct.h> |
5d0aa2cc | 32 | #include <net/netfilter/nf_conntrack_zones.h> |
a992ca2a | 33 | #include <net/netfilter/nf_conntrack_timestamp.h> |
0e60ebe0 | 34 | #include <linux/rculist_nulls.h> |
9fb9cbb1 | 35 | |
9fb9cbb1 YK |
36 | MODULE_LICENSE("GPL"); |
37 | ||
54b07dca | 38 | #ifdef CONFIG_NF_CONNTRACK_PROCFS |
824f1fbe | 39 | void |
9fb9cbb1 | 40 | print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple, |
32948588 JE |
41 | const struct nf_conntrack_l3proto *l3proto, |
42 | const struct nf_conntrack_l4proto *l4proto) | |
9fb9cbb1 | 43 | { |
824f1fbe JP |
44 | l3proto->print_tuple(s, tuple); |
45 | l4proto->print_tuple(s, tuple); | |
9fb9cbb1 | 46 | } |
e4bd8bce | 47 | EXPORT_SYMBOL_GPL(print_tuple); |
9fb9cbb1 | 48 | |
9fb9cbb1 | 49 | struct ct_iter_state { |
b2ce2c74 | 50 | struct seq_net_private p; |
64b87639 LZ |
51 | struct hlist_nulls_head *hash; |
52 | unsigned int htable_size; | |
9fb9cbb1 | 53 | unsigned int bucket; |
a992ca2a | 54 | u_int64_t time_now; |
9fb9cbb1 YK |
55 | }; |
56 | ||
ea781f19 | 57 | static struct hlist_nulls_node *ct_get_first(struct seq_file *seq) |
9fb9cbb1 YK |
58 | { |
59 | struct ct_iter_state *st = seq->private; | |
ea781f19 | 60 | struct hlist_nulls_node *n; |
9fb9cbb1 YK |
61 | |
62 | for (st->bucket = 0; | |
64b87639 | 63 | st->bucket < st->htable_size; |
9fb9cbb1 | 64 | st->bucket++) { |
64b87639 LZ |
65 | n = rcu_dereference( |
66 | hlist_nulls_first_rcu(&st->hash[st->bucket])); | |
ea781f19 | 67 | if (!is_a_nulls(n)) |
76507f69 | 68 | return n; |
9fb9cbb1 YK |
69 | } |
70 | return NULL; | |
71 | } | |
72 | ||
ea781f19 ED |
73 | static struct hlist_nulls_node *ct_get_next(struct seq_file *seq, |
74 | struct hlist_nulls_node *head) | |
9fb9cbb1 YK |
75 | { |
76 | struct ct_iter_state *st = seq->private; | |
77 | ||
0e60ebe0 | 78 | head = rcu_dereference(hlist_nulls_next_rcu(head)); |
ea781f19 ED |
79 | while (is_a_nulls(head)) { |
80 | if (likely(get_nulls_value(head) == st->bucket)) { | |
64b87639 | 81 | if (++st->bucket >= st->htable_size) |
ea781f19 ED |
82 | return NULL; |
83 | } | |
0e60ebe0 | 84 | head = rcu_dereference( |
64b87639 | 85 | hlist_nulls_first_rcu(&st->hash[st->bucket])); |
9fb9cbb1 YK |
86 | } |
87 | return head; | |
88 | } | |
89 | ||
ea781f19 | 90 | static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos) |
9fb9cbb1 | 91 | { |
ea781f19 | 92 | struct hlist_nulls_node *head = ct_get_first(seq); |
9fb9cbb1 YK |
93 | |
94 | if (head) | |
95 | while (pos && (head = ct_get_next(seq, head))) | |
96 | pos--; | |
97 | return pos ? NULL : head; | |
98 | } | |
99 | ||
100 | static void *ct_seq_start(struct seq_file *seq, loff_t *pos) | |
76507f69 | 101 | __acquires(RCU) |
9fb9cbb1 | 102 | { |
a992ca2a PNA |
103 | struct ct_iter_state *st = seq->private; |
104 | ||
d2de875c | 105 | st->time_now = ktime_get_real_ns(); |
76507f69 | 106 | rcu_read_lock(); |
64b87639 LZ |
107 | |
108 | nf_conntrack_get_ht(&st->hash, &st->htable_size); | |
9fb9cbb1 YK |
109 | return ct_get_idx(seq, *pos); |
110 | } | |
111 | ||
112 | static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
113 | { | |
114 | (*pos)++; | |
115 | return ct_get_next(s, v); | |
116 | } | |
117 | ||
118 | static void ct_seq_stop(struct seq_file *s, void *v) | |
76507f69 | 119 | __releases(RCU) |
9fb9cbb1 | 120 | { |
76507f69 | 121 | rcu_read_unlock(); |
9fb9cbb1 YK |
122 | } |
123 | ||
1ae4de0c | 124 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
e71456ae | 125 | static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) |
1ae4de0c EP |
126 | { |
127 | int ret; | |
128 | u32 len; | |
129 | char *secctx; | |
130 | ||
131 | ret = security_secid_to_secctx(ct->secmark, &secctx, &len); | |
132 | if (ret) | |
e71456ae | 133 | return; |
1ae4de0c | 134 | |
e71456ae | 135 | seq_printf(s, "secctx=%s ", secctx); |
1ae4de0c EP |
136 | |
137 | security_release_secctx(secctx, len); | |
1ae4de0c EP |
138 | } |
139 | #else | |
e71456ae | 140 | static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) |
1ae4de0c | 141 | { |
1ae4de0c EP |
142 | } |
143 | #endif | |
144 | ||
308ac914 | 145 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
deedb590 DB |
146 | static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct, |
147 | int dir) | |
308ac914 | 148 | { |
deedb590 DB |
149 | const struct nf_conntrack_zone *zone = nf_ct_zone(ct); |
150 | ||
151 | if (zone->dir != dir) | |
152 | return; | |
153 | switch (zone->dir) { | |
154 | case NF_CT_DEFAULT_ZONE_DIR: | |
155 | seq_printf(s, "zone=%u ", zone->id); | |
156 | break; | |
157 | case NF_CT_ZONE_DIR_ORIG: | |
158 | seq_printf(s, "zone-orig=%u ", zone->id); | |
159 | break; | |
160 | case NF_CT_ZONE_DIR_REPL: | |
161 | seq_printf(s, "zone-reply=%u ", zone->id); | |
162 | break; | |
163 | default: | |
164 | break; | |
165 | } | |
308ac914 DB |
166 | } |
167 | #else | |
deedb590 DB |
168 | static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct, |
169 | int dir) | |
308ac914 DB |
170 | { |
171 | } | |
172 | #endif | |
173 | ||
a992ca2a | 174 | #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP |
e71456ae | 175 | static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct) |
a992ca2a | 176 | { |
f5c88f56 | 177 | struct ct_iter_state *st = s->private; |
a992ca2a | 178 | struct nf_conn_tstamp *tstamp; |
f5c88f56 | 179 | s64 delta_time; |
a992ca2a PNA |
180 | |
181 | tstamp = nf_conn_tstamp_find(ct); | |
182 | if (tstamp) { | |
f5c88f56 PM |
183 | delta_time = st->time_now - tstamp->start; |
184 | if (delta_time > 0) | |
185 | delta_time = div_s64(delta_time, NSEC_PER_SEC); | |
186 | else | |
187 | delta_time = 0; | |
188 | ||
e71456ae SRRH |
189 | seq_printf(s, "delta-time=%llu ", |
190 | (unsigned long long)delta_time); | |
a992ca2a | 191 | } |
e71456ae | 192 | return; |
a992ca2a PNA |
193 | } |
194 | #else | |
e71456ae | 195 | static inline void |
a992ca2a PNA |
196 | ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct) |
197 | { | |
a992ca2a PNA |
198 | } |
199 | #endif | |
200 | ||
9fb9cbb1 YK |
201 | /* return 0 on success, 1 in case of error */ |
202 | static int ct_seq_show(struct seq_file *s, void *v) | |
203 | { | |
ea781f19 ED |
204 | struct nf_conntrack_tuple_hash *hash = v; |
205 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash); | |
32948588 JE |
206 | const struct nf_conntrack_l3proto *l3proto; |
207 | const struct nf_conntrack_l4proto *l4proto; | |
e77e6ff5 | 208 | struct net *net = seq_file_net(s); |
ea781f19 | 209 | int ret = 0; |
9fb9cbb1 | 210 | |
c88130bc | 211 | NF_CT_ASSERT(ct); |
ea781f19 ED |
212 | if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use))) |
213 | return 0; | |
9fb9cbb1 YK |
214 | |
215 | /* we only want to print DIR_ORIGINAL */ | |
216 | if (NF_CT_DIRECTION(hash)) | |
ea781f19 | 217 | goto release; |
9fb9cbb1 | 218 | |
e77e6ff5 LZ |
219 | if (!net_eq(nf_ct_net(ct), net)) |
220 | goto release; | |
221 | ||
5e8fbe2a | 222 | l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct)); |
9fb9cbb1 | 223 | NF_CT_ASSERT(l3proto); |
5e8fbe2a | 224 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
605dcad6 | 225 | NF_CT_ASSERT(l4proto); |
9fb9cbb1 | 226 | |
ea781f19 | 227 | ret = -ENOSPC; |
e71456ae SRRH |
228 | seq_printf(s, "%-8s %u %-8s %u %ld ", |
229 | l3proto->name, nf_ct_l3num(ct), | |
230 | l4proto->name, nf_ct_protonum(ct), | |
d0b35b93 | 231 | nf_ct_expires(ct) / HZ); |
9fb9cbb1 | 232 | |
37246a58 SRRH |
233 | if (l4proto->print_conntrack) |
234 | l4proto->print_conntrack(s, ct); | |
9fb9cbb1 | 235 | |
824f1fbe JP |
236 | print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
237 | l3proto, l4proto); | |
9fb9cbb1 | 238 | |
deedb590 DB |
239 | ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG); |
240 | ||
e71456ae SRRH |
241 | if (seq_has_overflowed(s)) |
242 | goto release; | |
243 | ||
58401572 | 244 | if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL)) |
ea781f19 | 245 | goto release; |
9fb9cbb1 | 246 | |
c88130bc | 247 | if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status))) |
e71456ae | 248 | seq_printf(s, "[UNREPLIED] "); |
9fb9cbb1 | 249 | |
824f1fbe JP |
250 | print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, |
251 | l3proto, l4proto); | |
9fb9cbb1 | 252 | |
deedb590 DB |
253 | ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL); |
254 | ||
58401572 | 255 | if (seq_print_acct(s, ct, IP_CT_DIR_REPLY)) |
ea781f19 | 256 | goto release; |
9fb9cbb1 | 257 | |
c88130bc | 258 | if (test_bit(IPS_ASSURED_BIT, &ct->status)) |
e71456ae | 259 | seq_printf(s, "[ASSURED] "); |
9fb9cbb1 | 260 | |
e71456ae | 261 | if (seq_has_overflowed(s)) |
ea781f19 | 262 | goto release; |
e71456ae SRRH |
263 | |
264 | #if defined(CONFIG_NF_CONNTRACK_MARK) | |
265 | seq_printf(s, "mark=%u ", ct->mark); | |
9fb9cbb1 YK |
266 | #endif |
267 | ||
e71456ae | 268 | ct_show_secctx(s, ct); |
deedb590 | 269 | ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR); |
e71456ae SRRH |
270 | ct_show_delta_time(s, ct); |
271 | ||
272 | seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)); | |
a992ca2a | 273 | |
e71456ae | 274 | if (seq_has_overflowed(s)) |
ea781f19 | 275 | goto release; |
a5d29264 | 276 | |
ea781f19 ED |
277 | ret = 0; |
278 | release: | |
279 | nf_ct_put(ct); | |
d88d7de0 | 280 | return ret; |
9fb9cbb1 YK |
281 | } |
282 | ||
56b3d975 | 283 | static const struct seq_operations ct_seq_ops = { |
9fb9cbb1 YK |
284 | .start = ct_seq_start, |
285 | .next = ct_seq_next, | |
286 | .stop = ct_seq_stop, | |
287 | .show = ct_seq_show | |
288 | }; | |
289 | ||
290 | static int ct_open(struct inode *inode, struct file *file) | |
291 | { | |
b2ce2c74 | 292 | return seq_open_net(inode, file, &ct_seq_ops, |
e2da5913 | 293 | sizeof(struct ct_iter_state)); |
9fb9cbb1 YK |
294 | } |
295 | ||
da7071d7 | 296 | static const struct file_operations ct_file_ops = { |
9fb9cbb1 YK |
297 | .owner = THIS_MODULE, |
298 | .open = ct_open, | |
299 | .read = seq_read, | |
300 | .llseek = seq_lseek, | |
b2ce2c74 | 301 | .release = seq_release_net, |
9fb9cbb1 YK |
302 | }; |
303 | ||
9fb9cbb1 YK |
304 | static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos) |
305 | { | |
8e9df801 | 306 | struct net *net = seq_file_net(seq); |
9fb9cbb1 YK |
307 | int cpu; |
308 | ||
309 | if (*pos == 0) | |
310 | return SEQ_START_TOKEN; | |
311 | ||
0f23174a | 312 | for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { |
9fb9cbb1 YK |
313 | if (!cpu_possible(cpu)) |
314 | continue; | |
315 | *pos = cpu + 1; | |
8e9df801 | 316 | return per_cpu_ptr(net->ct.stat, cpu); |
9fb9cbb1 YK |
317 | } |
318 | ||
319 | return NULL; | |
320 | } | |
321 | ||
322 | static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
323 | { | |
8e9df801 | 324 | struct net *net = seq_file_net(seq); |
9fb9cbb1 YK |
325 | int cpu; |
326 | ||
0f23174a | 327 | for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { |
9fb9cbb1 YK |
328 | if (!cpu_possible(cpu)) |
329 | continue; | |
330 | *pos = cpu + 1; | |
8e9df801 | 331 | return per_cpu_ptr(net->ct.stat, cpu); |
9fb9cbb1 YK |
332 | } |
333 | ||
334 | return NULL; | |
335 | } | |
336 | ||
337 | static void ct_cpu_seq_stop(struct seq_file *seq, void *v) | |
338 | { | |
339 | } | |
340 | ||
341 | static int ct_cpu_seq_show(struct seq_file *seq, void *v) | |
342 | { | |
8e9df801 AD |
343 | struct net *net = seq_file_net(seq); |
344 | unsigned int nr_conntracks = atomic_read(&net->ct.count); | |
32948588 | 345 | const struct ip_conntrack_stat *st = v; |
9fb9cbb1 YK |
346 | |
347 | if (v == SEQ_START_TOKEN) { | |
af740b2c | 348 | 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 search_restart\n"); |
9fb9cbb1 YK |
349 | return 0; |
350 | } | |
351 | ||
352 | seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x " | |
af740b2c | 353 | "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n", |
9fb9cbb1 | 354 | nr_conntracks, |
8e8118f8 | 355 | 0, |
9fb9cbb1 | 356 | st->found, |
8e8118f8 | 357 | 0, |
9fb9cbb1 YK |
358 | st->invalid, |
359 | st->ignore, | |
8e8118f8 FW |
360 | 0, |
361 | 0, | |
9fb9cbb1 YK |
362 | st->insert, |
363 | st->insert_failed, | |
364 | st->drop, | |
365 | st->early_drop, | |
366 | st->error, | |
367 | ||
368 | st->expect_new, | |
369 | st->expect_create, | |
af740b2c JDB |
370 | st->expect_delete, |
371 | st->search_restart | |
9fb9cbb1 YK |
372 | ); |
373 | return 0; | |
374 | } | |
375 | ||
56b3d975 | 376 | static const struct seq_operations ct_cpu_seq_ops = { |
9fb9cbb1 YK |
377 | .start = ct_cpu_seq_start, |
378 | .next = ct_cpu_seq_next, | |
379 | .stop = ct_cpu_seq_stop, | |
380 | .show = ct_cpu_seq_show, | |
381 | }; | |
382 | ||
383 | static int ct_cpu_seq_open(struct inode *inode, struct file *file) | |
384 | { | |
8e9df801 AD |
385 | return seq_open_net(inode, file, &ct_cpu_seq_ops, |
386 | sizeof(struct seq_net_private)); | |
9fb9cbb1 YK |
387 | } |
388 | ||
da7071d7 | 389 | static const struct file_operations ct_cpu_seq_fops = { |
9fb9cbb1 YK |
390 | .owner = THIS_MODULE, |
391 | .open = ct_cpu_seq_open, | |
392 | .read = seq_read, | |
393 | .llseek = seq_lseek, | |
8e9df801 | 394 | .release = seq_release_net, |
9fb9cbb1 | 395 | }; |
b916f7d4 | 396 | |
b2ce2c74 | 397 | static int nf_conntrack_standalone_init_proc(struct net *net) |
b916f7d4 AD |
398 | { |
399 | struct proc_dir_entry *pde; | |
f13f2aee PW |
400 | kuid_t root_uid; |
401 | kgid_t root_gid; | |
b916f7d4 | 402 | |
d4beaa66 | 403 | pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops); |
b916f7d4 AD |
404 | if (!pde) |
405 | goto out_nf_conntrack; | |
52c0e111 | 406 | |
f13f2aee PW |
407 | root_uid = make_kuid(net->user_ns, 0); |
408 | root_gid = make_kgid(net->user_ns, 0); | |
409 | if (uid_valid(root_uid) && gid_valid(root_gid)) | |
410 | proc_set_user(pde, root_uid, root_gid); | |
411 | ||
b2ce2c74 | 412 | pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat, |
52c0e111 | 413 | &ct_cpu_seq_fops); |
b916f7d4 AD |
414 | if (!pde) |
415 | goto out_stat_nf_conntrack; | |
b916f7d4 AD |
416 | return 0; |
417 | ||
418 | out_stat_nf_conntrack: | |
ece31ffd | 419 | remove_proc_entry("nf_conntrack", net->proc_net); |
b916f7d4 AD |
420 | out_nf_conntrack: |
421 | return -ENOMEM; | |
422 | } | |
423 | ||
b2ce2c74 | 424 | static void nf_conntrack_standalone_fini_proc(struct net *net) |
b916f7d4 | 425 | { |
b2ce2c74 | 426 | remove_proc_entry("nf_conntrack", net->proc_net_stat); |
ece31ffd | 427 | remove_proc_entry("nf_conntrack", net->proc_net); |
b916f7d4 AD |
428 | } |
429 | #else | |
b2ce2c74 | 430 | static int nf_conntrack_standalone_init_proc(struct net *net) |
b916f7d4 AD |
431 | { |
432 | return 0; | |
433 | } | |
434 | ||
b2ce2c74 | 435 | static void nf_conntrack_standalone_fini_proc(struct net *net) |
b916f7d4 AD |
436 | { |
437 | } | |
54b07dca | 438 | #endif /* CONFIG_NF_CONNTRACK_PROCFS */ |
9fb9cbb1 YK |
439 | |
440 | /* Sysctl support */ | |
441 | ||
442 | #ifdef CONFIG_SYSCTL | |
9fb9cbb1 | 443 | /* Log invalid packets of a given protocol */ |
3183ab89 FW |
444 | static int log_invalid_proto_min __read_mostly; |
445 | static int log_invalid_proto_max __read_mostly = 255; | |
446 | ||
447 | /* size the user *wants to set */ | |
448 | static unsigned int nf_conntrack_htable_size_user __read_mostly; | |
449 | ||
450 | static int | |
451 | nf_conntrack_hash_sysctl(struct ctl_table *table, int write, | |
452 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
453 | { | |
454 | int ret; | |
455 | ||
456 | ret = proc_dointvec(table, write, buffer, lenp, ppos); | |
457 | if (ret < 0 || !write) | |
458 | return ret; | |
459 | ||
460 | /* update ret, we might not be able to satisfy request */ | |
461 | ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user); | |
462 | ||
463 | /* update it to the actual value used by conntrack */ | |
464 | nf_conntrack_htable_size_user = nf_conntrack_htable_size; | |
465 | return ret; | |
466 | } | |
9fb9cbb1 | 467 | |
9714be7d | 468 | static struct ctl_table_header *nf_ct_netfilter_header; |
9fb9cbb1 | 469 | |
fe2c6338 | 470 | static struct ctl_table nf_ct_sysctl_table[] = { |
9fb9cbb1 | 471 | { |
9fb9cbb1 YK |
472 | .procname = "nf_conntrack_max", |
473 | .data = &nf_conntrack_max, | |
474 | .maxlen = sizeof(int), | |
475 | .mode = 0644, | |
6d9f239a | 476 | .proc_handler = proc_dointvec, |
9fb9cbb1 YK |
477 | }, |
478 | { | |
9fb9cbb1 | 479 | .procname = "nf_conntrack_count", |
49ac8713 | 480 | .data = &init_net.ct.count, |
9fb9cbb1 YK |
481 | .maxlen = sizeof(int), |
482 | .mode = 0444, | |
6d9f239a | 483 | .proc_handler = proc_dointvec, |
9fb9cbb1 YK |
484 | }, |
485 | { | |
9fb9cbb1 | 486 | .procname = "nf_conntrack_buckets", |
3183ab89 | 487 | .data = &nf_conntrack_htable_size_user, |
9fb9cbb1 | 488 | .maxlen = sizeof(unsigned int), |
3183ab89 FW |
489 | .mode = 0644, |
490 | .proc_handler = nf_conntrack_hash_sysctl, | |
9fb9cbb1 | 491 | }, |
39a27a35 | 492 | { |
39a27a35 | 493 | .procname = "nf_conntrack_checksum", |
c04d0552 | 494 | .data = &init_net.ct.sysctl_checksum, |
39a27a35 PM |
495 | .maxlen = sizeof(unsigned int), |
496 | .mode = 0644, | |
6d9f239a | 497 | .proc_handler = proc_dointvec, |
39a27a35 | 498 | }, |
9fb9cbb1 | 499 | { |
9fb9cbb1 | 500 | .procname = "nf_conntrack_log_invalid", |
c2a2c7e0 | 501 | .data = &init_net.ct.sysctl_log_invalid, |
9fb9cbb1 YK |
502 | .maxlen = sizeof(unsigned int), |
503 | .mode = 0644, | |
6d9f239a | 504 | .proc_handler = proc_dointvec_minmax, |
9fb9cbb1 YK |
505 | .extra1 = &log_invalid_proto_min, |
506 | .extra2 = &log_invalid_proto_max, | |
507 | }, | |
f264a7df | 508 | { |
f264a7df PM |
509 | .procname = "nf_conntrack_expect_max", |
510 | .data = &nf_ct_expect_max, | |
511 | .maxlen = sizeof(int), | |
512 | .mode = 0644, | |
6d9f239a | 513 | .proc_handler = proc_dointvec, |
f264a7df | 514 | }, |
f8572d8f | 515 | { } |
9fb9cbb1 YK |
516 | }; |
517 | ||
fe2c6338 | 518 | static struct ctl_table nf_ct_netfilter_table[] = { |
9fb9cbb1 | 519 | { |
9fb9cbb1 YK |
520 | .procname = "nf_conntrack_max", |
521 | .data = &nf_conntrack_max, | |
522 | .maxlen = sizeof(int), | |
523 | .mode = 0644, | |
6d9f239a | 524 | .proc_handler = proc_dointvec, |
9fb9cbb1 | 525 | }, |
f8572d8f | 526 | { } |
9fb9cbb1 YK |
527 | }; |
528 | ||
80250707 | 529 | static int nf_conntrack_standalone_init_sysctl(struct net *net) |
b916f7d4 | 530 | { |
80250707 AD |
531 | struct ctl_table *table; |
532 | ||
80250707 AD |
533 | table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table), |
534 | GFP_KERNEL); | |
535 | if (!table) | |
536 | goto out_kmemdup; | |
537 | ||
538 | table[1].data = &net->ct.count; | |
c04d0552 | 539 | table[3].data = &net->ct.sysctl_checksum; |
c2a2c7e0 | 540 | table[4].data = &net->ct.sysctl_log_invalid; |
80250707 | 541 | |
464dc801 EB |
542 | /* Don't export sysctls to unprivileged users */ |
543 | if (net->user_ns != &init_user_ns) | |
544 | table[0].procname = NULL; | |
545 | ||
3183ab89 FW |
546 | if (!net_eq(&init_net, net)) |
547 | table[2].mode = 0444; | |
548 | ||
ec8f23ce | 549 | net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table); |
80250707 | 550 | if (!net->ct.sysctl_header) |
9714be7d KPO |
551 | goto out_unregister_netfilter; |
552 | ||
b916f7d4 AD |
553 | return 0; |
554 | ||
9714be7d | 555 | out_unregister_netfilter: |
80250707 AD |
556 | kfree(table); |
557 | out_kmemdup: | |
9714be7d | 558 | return -ENOMEM; |
b916f7d4 AD |
559 | } |
560 | ||
80250707 | 561 | static void nf_conntrack_standalone_fini_sysctl(struct net *net) |
b916f7d4 | 562 | { |
80250707 AD |
563 | struct ctl_table *table; |
564 | ||
80250707 AD |
565 | table = net->ct.sysctl_header->ctl_table_arg; |
566 | unregister_net_sysctl_table(net->ct.sysctl_header); | |
567 | kfree(table); | |
b916f7d4 AD |
568 | } |
569 | #else | |
80250707 | 570 | static int nf_conntrack_standalone_init_sysctl(struct net *net) |
b916f7d4 AD |
571 | { |
572 | return 0; | |
573 | } | |
574 | ||
80250707 | 575 | static void nf_conntrack_standalone_fini_sysctl(struct net *net) |
b916f7d4 AD |
576 | { |
577 | } | |
9fb9cbb1 YK |
578 | #endif /* CONFIG_SYSCTL */ |
579 | ||
f94161c1 | 580 | static int nf_conntrack_pernet_init(struct net *net) |
dfdb8d79 | 581 | { |
b2ce2c74 AD |
582 | int ret; |
583 | ||
f94161c1 | 584 | ret = nf_conntrack_init_net(net); |
b2ce2c74 AD |
585 | if (ret < 0) |
586 | goto out_init; | |
f94161c1 | 587 | |
b2ce2c74 AD |
588 | ret = nf_conntrack_standalone_init_proc(net); |
589 | if (ret < 0) | |
590 | goto out_proc; | |
f94161c1 | 591 | |
c04d0552 | 592 | net->ct.sysctl_checksum = 1; |
c2a2c7e0 | 593 | net->ct.sysctl_log_invalid = 0; |
80250707 AD |
594 | ret = nf_conntrack_standalone_init_sysctl(net); |
595 | if (ret < 0) | |
596 | goto out_sysctl; | |
f94161c1 | 597 | |
b2ce2c74 AD |
598 | return 0; |
599 | ||
80250707 AD |
600 | out_sysctl: |
601 | nf_conntrack_standalone_fini_proc(net); | |
b2ce2c74 | 602 | out_proc: |
f94161c1 | 603 | nf_conntrack_cleanup_net(net); |
b2ce2c74 AD |
604 | out_init: |
605 | return ret; | |
dfdb8d79 AD |
606 | } |
607 | ||
dece40e8 | 608 | static void nf_conntrack_pernet_exit(struct list_head *net_exit_list) |
dfdb8d79 | 609 | { |
dece40e8 VD |
610 | struct net *net; |
611 | ||
612 | list_for_each_entry(net, net_exit_list, exit_list) { | |
613 | nf_conntrack_standalone_fini_sysctl(net); | |
614 | nf_conntrack_standalone_fini_proc(net); | |
615 | } | |
616 | nf_conntrack_cleanup_net_list(net_exit_list); | |
dfdb8d79 AD |
617 | } |
618 | ||
619 | static struct pernet_operations nf_conntrack_net_ops = { | |
dece40e8 VD |
620 | .init = nf_conntrack_pernet_init, |
621 | .exit_batch = nf_conntrack_pernet_exit, | |
dfdb8d79 AD |
622 | }; |
623 | ||
65b4b4e8 | 624 | static int __init nf_conntrack_standalone_init(void) |
9fb9cbb1 | 625 | { |
f94161c1 G |
626 | int ret = nf_conntrack_init_start(); |
627 | if (ret < 0) | |
628 | goto out_start; | |
629 | ||
5f9f946b | 630 | #ifdef CONFIG_SYSCTL |
f94161c1 G |
631 | nf_ct_netfilter_header = |
632 | register_net_sysctl(&init_net, "net", nf_ct_netfilter_table); | |
5f9f946b PNA |
633 | if (!nf_ct_netfilter_header) { |
634 | pr_err("nf_conntrack: can't register to sysctl.\n"); | |
5389090b | 635 | ret = -ENOMEM; |
f94161c1 | 636 | goto out_sysctl; |
5f9f946b | 637 | } |
3183ab89 FW |
638 | |
639 | nf_conntrack_htable_size_user = nf_conntrack_htable_size; | |
5f9f946b | 640 | #endif |
f94161c1 G |
641 | |
642 | ret = register_pernet_subsys(&nf_conntrack_net_ops); | |
643 | if (ret < 0) | |
644 | goto out_pernet; | |
645 | ||
646 | nf_conntrack_init_end(); | |
647 | return 0; | |
648 | ||
649 | out_pernet: | |
5f9f946b | 650 | #ifdef CONFIG_SYSCTL |
f94161c1 G |
651 | unregister_net_sysctl_table(nf_ct_netfilter_header); |
652 | out_sysctl: | |
5f9f946b | 653 | #endif |
f94161c1 G |
654 | nf_conntrack_cleanup_end(); |
655 | out_start: | |
656 | return ret; | |
9fb9cbb1 YK |
657 | } |
658 | ||
65b4b4e8 | 659 | static void __exit nf_conntrack_standalone_fini(void) |
9fb9cbb1 | 660 | { |
f94161c1 | 661 | nf_conntrack_cleanup_start(); |
dfdb8d79 | 662 | unregister_pernet_subsys(&nf_conntrack_net_ops); |
5f9f946b | 663 | #ifdef CONFIG_SYSCTL |
f94161c1 | 664 | unregister_net_sysctl_table(nf_ct_netfilter_header); |
5f9f946b | 665 | #endif |
1e47ee83 | 666 | nf_conntrack_cleanup_end(); |
9fb9cbb1 YK |
667 | } |
668 | ||
65b4b4e8 AM |
669 | module_init(nf_conntrack_standalone_init); |
670 | module_exit(nf_conntrack_standalone_fini); | |
9fb9cbb1 YK |
671 | |
672 | /* Some modules need us, but don't depend directly on any symbol. | |
673 | They should call this. */ | |
2e4e6a17 | 674 | void need_conntrack(void) |
9fb9cbb1 YK |
675 | { |
676 | } | |
13b18339 | 677 | EXPORT_SYMBOL_GPL(need_conntrack); |