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