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