Commit | Line | Data |
---|---|---|
2e4e6a17 HW |
1 | /* |
2 | * x_tables core - Backend for {ip,ip6,arp}_tables | |
3 | * | |
4 | * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org> | |
5 | * | |
6 | * Based on existing ip_tables code which is | |
7 | * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling | |
8 | * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | */ | |
be91fd5e | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
2e4e6a17 HW |
16 | #include <linux/kernel.h> |
17 | #include <linux/socket.h> | |
18 | #include <linux/net.h> | |
19 | #include <linux/proc_fs.h> | |
20 | #include <linux/seq_file.h> | |
21 | #include <linux/string.h> | |
22 | #include <linux/vmalloc.h> | |
9e19bb6d | 23 | #include <linux/mutex.h> |
d7fe0f24 | 24 | #include <linux/mm.h> |
5a0e3ad6 | 25 | #include <linux/slab.h> |
457c4cbc | 26 | #include <net/net_namespace.h> |
2e4e6a17 HW |
27 | |
28 | #include <linux/netfilter/x_tables.h> | |
29 | #include <linux/netfilter_arp.h> | |
e3eaa991 JE |
30 | #include <linux/netfilter_ipv4/ip_tables.h> |
31 | #include <linux/netfilter_ipv6/ip6_tables.h> | |
32 | #include <linux/netfilter_arp/arp_tables.h> | |
9e19bb6d | 33 | |
2e4e6a17 HW |
34 | MODULE_LICENSE("GPL"); |
35 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
043ef46c | 36 | MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module"); |
2e4e6a17 HW |
37 | |
38 | #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1)) | |
39 | ||
b386d9f5 PM |
40 | struct compat_delta { |
41 | struct compat_delta *next; | |
42 | unsigned int offset; | |
3e5e524f | 43 | int delta; |
b386d9f5 PM |
44 | }; |
45 | ||
2e4e6a17 | 46 | struct xt_af { |
9e19bb6d | 47 | struct mutex mutex; |
2e4e6a17 HW |
48 | struct list_head match; |
49 | struct list_head target; | |
b386d9f5 | 50 | #ifdef CONFIG_COMPAT |
2722971c | 51 | struct mutex compat_mutex; |
b386d9f5 PM |
52 | struct compat_delta *compat_offsets; |
53 | #endif | |
2e4e6a17 HW |
54 | }; |
55 | ||
56 | static struct xt_af *xt; | |
57 | ||
7e9c6eeb JE |
58 | static const char *const xt_prefix[NFPROTO_NUMPROTO] = { |
59 | [NFPROTO_UNSPEC] = "x", | |
60 | [NFPROTO_IPV4] = "ip", | |
61 | [NFPROTO_ARP] = "arp", | |
62 | [NFPROTO_BRIDGE] = "eb", | |
63 | [NFPROTO_IPV6] = "ip6", | |
37f9f733 PM |
64 | }; |
65 | ||
f3c5c1bf JE |
66 | /* Allow this many total (re)entries. */ |
67 | static const unsigned int xt_jumpstack_multiplier = 2; | |
68 | ||
2e4e6a17 HW |
69 | /* Registration hooks for targets. */ |
70 | int | |
a45049c5 | 71 | xt_register_target(struct xt_target *target) |
2e4e6a17 | 72 | { |
76108cea JE |
73 | u_int8_t af = target->family; |
74 | int ret; | |
2e4e6a17 | 75 | |
9e19bb6d | 76 | ret = mutex_lock_interruptible(&xt[af].mutex); |
2e4e6a17 HW |
77 | if (ret != 0) |
78 | return ret; | |
79 | list_add(&target->list, &xt[af].target); | |
9e19bb6d | 80 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
81 | return ret; |
82 | } | |
83 | EXPORT_SYMBOL(xt_register_target); | |
84 | ||
85 | void | |
a45049c5 | 86 | xt_unregister_target(struct xt_target *target) |
2e4e6a17 | 87 | { |
76108cea | 88 | u_int8_t af = target->family; |
a45049c5 | 89 | |
9e19bb6d | 90 | mutex_lock(&xt[af].mutex); |
df0933dc | 91 | list_del(&target->list); |
9e19bb6d | 92 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
93 | } |
94 | EXPORT_SYMBOL(xt_unregister_target); | |
95 | ||
52d9c42e PM |
96 | int |
97 | xt_register_targets(struct xt_target *target, unsigned int n) | |
98 | { | |
99 | unsigned int i; | |
100 | int err = 0; | |
101 | ||
102 | for (i = 0; i < n; i++) { | |
103 | err = xt_register_target(&target[i]); | |
104 | if (err) | |
105 | goto err; | |
106 | } | |
107 | return err; | |
108 | ||
109 | err: | |
110 | if (i > 0) | |
111 | xt_unregister_targets(target, i); | |
112 | return err; | |
113 | } | |
114 | EXPORT_SYMBOL(xt_register_targets); | |
115 | ||
116 | void | |
117 | xt_unregister_targets(struct xt_target *target, unsigned int n) | |
118 | { | |
119 | unsigned int i; | |
120 | ||
121 | for (i = 0; i < n; i++) | |
122 | xt_unregister_target(&target[i]); | |
123 | } | |
124 | EXPORT_SYMBOL(xt_unregister_targets); | |
125 | ||
2e4e6a17 | 126 | int |
a45049c5 | 127 | xt_register_match(struct xt_match *match) |
2e4e6a17 | 128 | { |
76108cea JE |
129 | u_int8_t af = match->family; |
130 | int ret; | |
2e4e6a17 | 131 | |
9e19bb6d | 132 | ret = mutex_lock_interruptible(&xt[af].mutex); |
2e4e6a17 HW |
133 | if (ret != 0) |
134 | return ret; | |
135 | ||
136 | list_add(&match->list, &xt[af].match); | |
9e19bb6d | 137 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
138 | |
139 | return ret; | |
140 | } | |
141 | EXPORT_SYMBOL(xt_register_match); | |
142 | ||
143 | void | |
a45049c5 | 144 | xt_unregister_match(struct xt_match *match) |
2e4e6a17 | 145 | { |
76108cea | 146 | u_int8_t af = match->family; |
a45049c5 | 147 | |
9e19bb6d | 148 | mutex_lock(&xt[af].mutex); |
df0933dc | 149 | list_del(&match->list); |
9e19bb6d | 150 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
151 | } |
152 | EXPORT_SYMBOL(xt_unregister_match); | |
153 | ||
52d9c42e PM |
154 | int |
155 | xt_register_matches(struct xt_match *match, unsigned int n) | |
156 | { | |
157 | unsigned int i; | |
158 | int err = 0; | |
159 | ||
160 | for (i = 0; i < n; i++) { | |
161 | err = xt_register_match(&match[i]); | |
162 | if (err) | |
163 | goto err; | |
164 | } | |
165 | return err; | |
166 | ||
167 | err: | |
168 | if (i > 0) | |
169 | xt_unregister_matches(match, i); | |
170 | return err; | |
171 | } | |
172 | EXPORT_SYMBOL(xt_register_matches); | |
173 | ||
174 | void | |
175 | xt_unregister_matches(struct xt_match *match, unsigned int n) | |
176 | { | |
177 | unsigned int i; | |
178 | ||
179 | for (i = 0; i < n; i++) | |
180 | xt_unregister_match(&match[i]); | |
181 | } | |
182 | EXPORT_SYMBOL(xt_unregister_matches); | |
183 | ||
2e4e6a17 HW |
184 | |
185 | /* | |
186 | * These are weird, but module loading must not be done with mutex | |
187 | * held (since they will register), and we have to have a single | |
188 | * function to use try_then_request_module(). | |
189 | */ | |
190 | ||
191 | /* Find match, grabs ref. Returns ERR_PTR() on error. */ | |
76108cea | 192 | struct xt_match *xt_find_match(u8 af, const char *name, u8 revision) |
2e4e6a17 HW |
193 | { |
194 | struct xt_match *m; | |
195 | int err = 0; | |
196 | ||
9e19bb6d | 197 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
2e4e6a17 HW |
198 | return ERR_PTR(-EINTR); |
199 | ||
200 | list_for_each_entry(m, &xt[af].match, list) { | |
201 | if (strcmp(m->name, name) == 0) { | |
202 | if (m->revision == revision) { | |
203 | if (try_module_get(m->me)) { | |
9e19bb6d | 204 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
205 | return m; |
206 | } | |
207 | } else | |
208 | err = -EPROTOTYPE; /* Found something. */ | |
209 | } | |
210 | } | |
9e19bb6d | 211 | mutex_unlock(&xt[af].mutex); |
55b69e91 JE |
212 | |
213 | if (af != NFPROTO_UNSPEC) | |
214 | /* Try searching again in the family-independent list */ | |
215 | return xt_find_match(NFPROTO_UNSPEC, name, revision); | |
216 | ||
2e4e6a17 HW |
217 | return ERR_PTR(err); |
218 | } | |
219 | EXPORT_SYMBOL(xt_find_match); | |
220 | ||
fd0ec0e6 JE |
221 | struct xt_match * |
222 | xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision) | |
223 | { | |
224 | struct xt_match *match; | |
225 | ||
226 | match = try_then_request_module(xt_find_match(nfproto, name, revision), | |
227 | "%st_%s", xt_prefix[nfproto], name); | |
228 | return (match != NULL) ? match : ERR_PTR(-ENOENT); | |
229 | } | |
230 | EXPORT_SYMBOL_GPL(xt_request_find_match); | |
231 | ||
2e4e6a17 | 232 | /* Find target, grabs ref. Returns ERR_PTR() on error. */ |
76108cea | 233 | struct xt_target *xt_find_target(u8 af, const char *name, u8 revision) |
2e4e6a17 HW |
234 | { |
235 | struct xt_target *t; | |
236 | int err = 0; | |
237 | ||
9e19bb6d | 238 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
2e4e6a17 HW |
239 | return ERR_PTR(-EINTR); |
240 | ||
241 | list_for_each_entry(t, &xt[af].target, list) { | |
242 | if (strcmp(t->name, name) == 0) { | |
243 | if (t->revision == revision) { | |
244 | if (try_module_get(t->me)) { | |
9e19bb6d | 245 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
246 | return t; |
247 | } | |
248 | } else | |
249 | err = -EPROTOTYPE; /* Found something. */ | |
250 | } | |
251 | } | |
9e19bb6d | 252 | mutex_unlock(&xt[af].mutex); |
55b69e91 JE |
253 | |
254 | if (af != NFPROTO_UNSPEC) | |
255 | /* Try searching again in the family-independent list */ | |
256 | return xt_find_target(NFPROTO_UNSPEC, name, revision); | |
257 | ||
2e4e6a17 HW |
258 | return ERR_PTR(err); |
259 | } | |
260 | EXPORT_SYMBOL(xt_find_target); | |
261 | ||
76108cea | 262 | struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision) |
2e4e6a17 HW |
263 | { |
264 | struct xt_target *target; | |
265 | ||
266 | target = try_then_request_module(xt_find_target(af, name, revision), | |
37f9f733 | 267 | "%st_%s", xt_prefix[af], name); |
d2a7b6ba | 268 | return (target != NULL) ? target : ERR_PTR(-ENOENT); |
2e4e6a17 HW |
269 | } |
270 | EXPORT_SYMBOL_GPL(xt_request_find_target); | |
271 | ||
76108cea | 272 | static int match_revfn(u8 af, const char *name, u8 revision, int *bestp) |
2e4e6a17 | 273 | { |
5452e425 | 274 | const struct xt_match *m; |
2e4e6a17 HW |
275 | int have_rev = 0; |
276 | ||
277 | list_for_each_entry(m, &xt[af].match, list) { | |
278 | if (strcmp(m->name, name) == 0) { | |
279 | if (m->revision > *bestp) | |
280 | *bestp = m->revision; | |
281 | if (m->revision == revision) | |
282 | have_rev = 1; | |
283 | } | |
284 | } | |
656caff2 PM |
285 | |
286 | if (af != NFPROTO_UNSPEC && !have_rev) | |
287 | return match_revfn(NFPROTO_UNSPEC, name, revision, bestp); | |
288 | ||
2e4e6a17 HW |
289 | return have_rev; |
290 | } | |
291 | ||
76108cea | 292 | static int target_revfn(u8 af, const char *name, u8 revision, int *bestp) |
2e4e6a17 | 293 | { |
5452e425 | 294 | const struct xt_target *t; |
2e4e6a17 HW |
295 | int have_rev = 0; |
296 | ||
297 | list_for_each_entry(t, &xt[af].target, list) { | |
298 | if (strcmp(t->name, name) == 0) { | |
299 | if (t->revision > *bestp) | |
300 | *bestp = t->revision; | |
301 | if (t->revision == revision) | |
302 | have_rev = 1; | |
303 | } | |
304 | } | |
656caff2 PM |
305 | |
306 | if (af != NFPROTO_UNSPEC && !have_rev) | |
307 | return target_revfn(NFPROTO_UNSPEC, name, revision, bestp); | |
308 | ||
2e4e6a17 HW |
309 | return have_rev; |
310 | } | |
311 | ||
312 | /* Returns true or false (if no such extension at all) */ | |
76108cea | 313 | int xt_find_revision(u8 af, const char *name, u8 revision, int target, |
2e4e6a17 HW |
314 | int *err) |
315 | { | |
316 | int have_rev, best = -1; | |
317 | ||
9e19bb6d | 318 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) { |
2e4e6a17 HW |
319 | *err = -EINTR; |
320 | return 1; | |
321 | } | |
322 | if (target == 1) | |
323 | have_rev = target_revfn(af, name, revision, &best); | |
324 | else | |
325 | have_rev = match_revfn(af, name, revision, &best); | |
9e19bb6d | 326 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
327 | |
328 | /* Nothing at all? Return 0 to try loading module. */ | |
329 | if (best == -1) { | |
330 | *err = -ENOENT; | |
331 | return 0; | |
332 | } | |
333 | ||
334 | *err = best; | |
335 | if (!have_rev) | |
336 | *err = -EPROTONOSUPPORT; | |
337 | return 1; | |
338 | } | |
339 | EXPORT_SYMBOL_GPL(xt_find_revision); | |
340 | ||
45185364 JE |
341 | static char *textify_hooks(char *buf, size_t size, unsigned int mask) |
342 | { | |
343 | static const char *const names[] = { | |
344 | "PREROUTING", "INPUT", "FORWARD", | |
345 | "OUTPUT", "POSTROUTING", "BROUTING", | |
346 | }; | |
347 | unsigned int i; | |
348 | char *p = buf; | |
349 | bool np = false; | |
350 | int res; | |
351 | ||
352 | *p = '\0'; | |
353 | for (i = 0; i < ARRAY_SIZE(names); ++i) { | |
354 | if (!(mask & (1 << i))) | |
355 | continue; | |
356 | res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]); | |
357 | if (res > 0) { | |
358 | size -= res; | |
359 | p += res; | |
360 | } | |
361 | np = true; | |
362 | } | |
363 | ||
364 | return buf; | |
365 | } | |
366 | ||
916a917d | 367 | int xt_check_match(struct xt_mtchk_param *par, |
9b4fce7a | 368 | unsigned int size, u_int8_t proto, bool inv_proto) |
37f9f733 | 369 | { |
bd414ee6 JE |
370 | int ret; |
371 | ||
9b4fce7a JE |
372 | if (XT_ALIGN(par->match->matchsize) != size && |
373 | par->match->matchsize != -1) { | |
043ef46c JE |
374 | /* |
375 | * ebt_among is exempt from centralized matchsize checking | |
376 | * because it uses a dynamic-size data set. | |
377 | */ | |
b402405d JE |
378 | pr_err("%s_tables: %s.%u match: invalid size " |
379 | "%u (kernel) != (user) %u\n", | |
916a917d | 380 | xt_prefix[par->family], par->match->name, |
b402405d | 381 | par->match->revision, |
9b4fce7a | 382 | XT_ALIGN(par->match->matchsize), size); |
37f9f733 PM |
383 | return -EINVAL; |
384 | } | |
9b4fce7a JE |
385 | if (par->match->table != NULL && |
386 | strcmp(par->match->table, par->table) != 0) { | |
3dd5d7e3 | 387 | pr_err("%s_tables: %s match: only valid in %s table, not %s\n", |
916a917d | 388 | xt_prefix[par->family], par->match->name, |
9b4fce7a | 389 | par->match->table, par->table); |
37f9f733 PM |
390 | return -EINVAL; |
391 | } | |
9b4fce7a | 392 | if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) { |
45185364 JE |
393 | char used[64], allow[64]; |
394 | ||
3dd5d7e3 | 395 | pr_err("%s_tables: %s match: used from hooks %s, but only " |
45185364 | 396 | "valid from %s\n", |
916a917d | 397 | xt_prefix[par->family], par->match->name, |
45185364 JE |
398 | textify_hooks(used, sizeof(used), par->hook_mask), |
399 | textify_hooks(allow, sizeof(allow), par->match->hooks)); | |
37f9f733 PM |
400 | return -EINVAL; |
401 | } | |
9b4fce7a | 402 | if (par->match->proto && (par->match->proto != proto || inv_proto)) { |
3dd5d7e3 | 403 | pr_err("%s_tables: %s match: only valid for protocol %u\n", |
916a917d JE |
404 | xt_prefix[par->family], par->match->name, |
405 | par->match->proto); | |
37f9f733 PM |
406 | return -EINVAL; |
407 | } | |
bd414ee6 JE |
408 | if (par->match->checkentry != NULL) { |
409 | ret = par->match->checkentry(par); | |
410 | if (ret < 0) | |
411 | return ret; | |
412 | else if (ret > 0) | |
413 | /* Flag up potential errors. */ | |
414 | return -EIO; | |
415 | } | |
37f9f733 PM |
416 | return 0; |
417 | } | |
418 | EXPORT_SYMBOL_GPL(xt_check_match); | |
419 | ||
2722971c | 420 | #ifdef CONFIG_COMPAT |
76108cea | 421 | int xt_compat_add_offset(u_int8_t af, unsigned int offset, short delta) |
b386d9f5 PM |
422 | { |
423 | struct compat_delta *tmp; | |
424 | ||
425 | tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL); | |
426 | if (!tmp) | |
427 | return -ENOMEM; | |
428 | ||
429 | tmp->offset = offset; | |
430 | tmp->delta = delta; | |
431 | ||
432 | if (xt[af].compat_offsets) { | |
433 | tmp->next = xt[af].compat_offsets->next; | |
434 | xt[af].compat_offsets->next = tmp; | |
435 | } else { | |
436 | xt[af].compat_offsets = tmp; | |
437 | tmp->next = NULL; | |
438 | } | |
439 | return 0; | |
440 | } | |
441 | EXPORT_SYMBOL_GPL(xt_compat_add_offset); | |
442 | ||
76108cea | 443 | void xt_compat_flush_offsets(u_int8_t af) |
b386d9f5 PM |
444 | { |
445 | struct compat_delta *tmp, *next; | |
446 | ||
447 | if (xt[af].compat_offsets) { | |
448 | for (tmp = xt[af].compat_offsets; tmp; tmp = next) { | |
449 | next = tmp->next; | |
450 | kfree(tmp); | |
451 | } | |
452 | xt[af].compat_offsets = NULL; | |
453 | } | |
454 | } | |
455 | EXPORT_SYMBOL_GPL(xt_compat_flush_offsets); | |
456 | ||
3e5e524f | 457 | int xt_compat_calc_jump(u_int8_t af, unsigned int offset) |
b386d9f5 PM |
458 | { |
459 | struct compat_delta *tmp; | |
3e5e524f | 460 | int delta; |
b386d9f5 PM |
461 | |
462 | for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next) | |
463 | if (tmp->offset < offset) | |
464 | delta += tmp->delta; | |
465 | return delta; | |
466 | } | |
467 | EXPORT_SYMBOL_GPL(xt_compat_calc_jump); | |
468 | ||
5452e425 | 469 | int xt_compat_match_offset(const struct xt_match *match) |
2722971c | 470 | { |
9fa492cd PM |
471 | u_int16_t csize = match->compatsize ? : match->matchsize; |
472 | return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize); | |
473 | } | |
474 | EXPORT_SYMBOL_GPL(xt_compat_match_offset); | |
475 | ||
89566951 | 476 | int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr, |
b0a6363c | 477 | unsigned int *size) |
9fa492cd | 478 | { |
5452e425 | 479 | const struct xt_match *match = m->u.kernel.match; |
9fa492cd PM |
480 | struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m; |
481 | int pad, off = xt_compat_match_offset(match); | |
482 | u_int16_t msize = cm->u.user.match_size; | |
483 | ||
484 | m = *dstptr; | |
485 | memcpy(m, cm, sizeof(*cm)); | |
486 | if (match->compat_from_user) | |
487 | match->compat_from_user(m->data, cm->data); | |
488 | else | |
489 | memcpy(m->data, cm->data, msize - sizeof(*cm)); | |
490 | pad = XT_ALIGN(match->matchsize) - match->matchsize; | |
491 | if (pad > 0) | |
492 | memset(m->data + match->matchsize, 0, pad); | |
493 | ||
494 | msize += off; | |
495 | m->u.user.match_size = msize; | |
496 | ||
497 | *size += off; | |
498 | *dstptr += msize; | |
89566951 | 499 | return 0; |
9fa492cd PM |
500 | } |
501 | EXPORT_SYMBOL_GPL(xt_compat_match_from_user); | |
502 | ||
739674fb JE |
503 | int xt_compat_match_to_user(const struct xt_entry_match *m, |
504 | void __user **dstptr, unsigned int *size) | |
9fa492cd | 505 | { |
5452e425 | 506 | const struct xt_match *match = m->u.kernel.match; |
9fa492cd PM |
507 | struct compat_xt_entry_match __user *cm = *dstptr; |
508 | int off = xt_compat_match_offset(match); | |
509 | u_int16_t msize = m->u.user.match_size - off; | |
510 | ||
511 | if (copy_to_user(cm, m, sizeof(*cm)) || | |
a18aa31b PM |
512 | put_user(msize, &cm->u.user.match_size) || |
513 | copy_to_user(cm->u.user.name, m->u.kernel.match->name, | |
514 | strlen(m->u.kernel.match->name) + 1)) | |
601e68e1 | 515 | return -EFAULT; |
9fa492cd PM |
516 | |
517 | if (match->compat_to_user) { | |
518 | if (match->compat_to_user((void __user *)cm->data, m->data)) | |
519 | return -EFAULT; | |
520 | } else { | |
521 | if (copy_to_user(cm->data, m->data, msize - sizeof(*cm))) | |
522 | return -EFAULT; | |
2722971c | 523 | } |
9fa492cd PM |
524 | |
525 | *size -= off; | |
526 | *dstptr += msize; | |
527 | return 0; | |
2722971c | 528 | } |
9fa492cd PM |
529 | EXPORT_SYMBOL_GPL(xt_compat_match_to_user); |
530 | #endif /* CONFIG_COMPAT */ | |
2722971c | 531 | |
916a917d | 532 | int xt_check_target(struct xt_tgchk_param *par, |
af5d6dc2 | 533 | unsigned int size, u_int8_t proto, bool inv_proto) |
37f9f733 | 534 | { |
d6b00a53 JE |
535 | int ret; |
536 | ||
af5d6dc2 | 537 | if (XT_ALIGN(par->target->targetsize) != size) { |
b402405d JE |
538 | pr_err("%s_tables: %s.%u target: invalid size " |
539 | "%u (kernel) != (user) %u\n", | |
916a917d | 540 | xt_prefix[par->family], par->target->name, |
b402405d | 541 | par->target->revision, |
af5d6dc2 | 542 | XT_ALIGN(par->target->targetsize), size); |
37f9f733 PM |
543 | return -EINVAL; |
544 | } | |
af5d6dc2 JE |
545 | if (par->target->table != NULL && |
546 | strcmp(par->target->table, par->table) != 0) { | |
3dd5d7e3 | 547 | pr_err("%s_tables: %s target: only valid in %s table, not %s\n", |
916a917d | 548 | xt_prefix[par->family], par->target->name, |
af5d6dc2 | 549 | par->target->table, par->table); |
37f9f733 PM |
550 | return -EINVAL; |
551 | } | |
af5d6dc2 | 552 | if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) { |
45185364 JE |
553 | char used[64], allow[64]; |
554 | ||
3dd5d7e3 | 555 | pr_err("%s_tables: %s target: used from hooks %s, but only " |
45185364 | 556 | "usable from %s\n", |
916a917d | 557 | xt_prefix[par->family], par->target->name, |
45185364 JE |
558 | textify_hooks(used, sizeof(used), par->hook_mask), |
559 | textify_hooks(allow, sizeof(allow), par->target->hooks)); | |
37f9f733 PM |
560 | return -EINVAL; |
561 | } | |
af5d6dc2 | 562 | if (par->target->proto && (par->target->proto != proto || inv_proto)) { |
3dd5d7e3 | 563 | pr_err("%s_tables: %s target: only valid for protocol %u\n", |
916a917d | 564 | xt_prefix[par->family], par->target->name, |
af5d6dc2 | 565 | par->target->proto); |
37f9f733 PM |
566 | return -EINVAL; |
567 | } | |
d6b00a53 JE |
568 | if (par->target->checkentry != NULL) { |
569 | ret = par->target->checkentry(par); | |
570 | if (ret < 0) | |
571 | return ret; | |
572 | else if (ret > 0) | |
573 | /* Flag up potential errors. */ | |
574 | return -EIO; | |
575 | } | |
37f9f733 PM |
576 | return 0; |
577 | } | |
578 | EXPORT_SYMBOL_GPL(xt_check_target); | |
579 | ||
2722971c | 580 | #ifdef CONFIG_COMPAT |
5452e425 | 581 | int xt_compat_target_offset(const struct xt_target *target) |
2722971c | 582 | { |
9fa492cd PM |
583 | u_int16_t csize = target->compatsize ? : target->targetsize; |
584 | return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize); | |
585 | } | |
586 | EXPORT_SYMBOL_GPL(xt_compat_target_offset); | |
587 | ||
588 | void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr, | |
b0a6363c | 589 | unsigned int *size) |
9fa492cd | 590 | { |
5452e425 | 591 | const struct xt_target *target = t->u.kernel.target; |
9fa492cd PM |
592 | struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t; |
593 | int pad, off = xt_compat_target_offset(target); | |
594 | u_int16_t tsize = ct->u.user.target_size; | |
595 | ||
596 | t = *dstptr; | |
597 | memcpy(t, ct, sizeof(*ct)); | |
598 | if (target->compat_from_user) | |
599 | target->compat_from_user(t->data, ct->data); | |
600 | else | |
601 | memcpy(t->data, ct->data, tsize - sizeof(*ct)); | |
602 | pad = XT_ALIGN(target->targetsize) - target->targetsize; | |
603 | if (pad > 0) | |
604 | memset(t->data + target->targetsize, 0, pad); | |
605 | ||
606 | tsize += off; | |
607 | t->u.user.target_size = tsize; | |
608 | ||
609 | *size += off; | |
610 | *dstptr += tsize; | |
611 | } | |
612 | EXPORT_SYMBOL_GPL(xt_compat_target_from_user); | |
613 | ||
739674fb JE |
614 | int xt_compat_target_to_user(const struct xt_entry_target *t, |
615 | void __user **dstptr, unsigned int *size) | |
9fa492cd | 616 | { |
5452e425 | 617 | const struct xt_target *target = t->u.kernel.target; |
9fa492cd PM |
618 | struct compat_xt_entry_target __user *ct = *dstptr; |
619 | int off = xt_compat_target_offset(target); | |
620 | u_int16_t tsize = t->u.user.target_size - off; | |
621 | ||
622 | if (copy_to_user(ct, t, sizeof(*ct)) || | |
a18aa31b PM |
623 | put_user(tsize, &ct->u.user.target_size) || |
624 | copy_to_user(ct->u.user.name, t->u.kernel.target->name, | |
625 | strlen(t->u.kernel.target->name) + 1)) | |
601e68e1 | 626 | return -EFAULT; |
9fa492cd PM |
627 | |
628 | if (target->compat_to_user) { | |
629 | if (target->compat_to_user((void __user *)ct->data, t->data)) | |
630 | return -EFAULT; | |
631 | } else { | |
632 | if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct))) | |
633 | return -EFAULT; | |
2722971c | 634 | } |
9fa492cd PM |
635 | |
636 | *size -= off; | |
637 | *dstptr += tsize; | |
638 | return 0; | |
2722971c | 639 | } |
9fa492cd | 640 | EXPORT_SYMBOL_GPL(xt_compat_target_to_user); |
2722971c DM |
641 | #endif |
642 | ||
2e4e6a17 HW |
643 | struct xt_table_info *xt_alloc_table_info(unsigned int size) |
644 | { | |
645 | struct xt_table_info *newinfo; | |
646 | int cpu; | |
647 | ||
648 | /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */ | |
4481374c | 649 | if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages) |
2e4e6a17 HW |
650 | return NULL; |
651 | ||
259d4e41 | 652 | newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL); |
2e4e6a17 HW |
653 | if (!newinfo) |
654 | return NULL; | |
655 | ||
656 | newinfo->size = size; | |
657 | ||
6f912042 | 658 | for_each_possible_cpu(cpu) { |
2e4e6a17 HW |
659 | if (size <= PAGE_SIZE) |
660 | newinfo->entries[cpu] = kmalloc_node(size, | |
661 | GFP_KERNEL, | |
662 | cpu_to_node(cpu)); | |
663 | else | |
664 | newinfo->entries[cpu] = vmalloc_node(size, | |
665 | cpu_to_node(cpu)); | |
666 | ||
667 | if (newinfo->entries[cpu] == NULL) { | |
668 | xt_free_table_info(newinfo); | |
669 | return NULL; | |
670 | } | |
671 | } | |
672 | ||
673 | return newinfo; | |
674 | } | |
675 | EXPORT_SYMBOL(xt_alloc_table_info); | |
676 | ||
677 | void xt_free_table_info(struct xt_table_info *info) | |
678 | { | |
679 | int cpu; | |
680 | ||
6f912042 | 681 | for_each_possible_cpu(cpu) { |
2e4e6a17 HW |
682 | if (info->size <= PAGE_SIZE) |
683 | kfree(info->entries[cpu]); | |
684 | else | |
685 | vfree(info->entries[cpu]); | |
686 | } | |
f3c5c1bf JE |
687 | |
688 | if (info->jumpstack != NULL) { | |
689 | if (sizeof(void *) * info->stacksize > PAGE_SIZE) { | |
690 | for_each_possible_cpu(cpu) | |
691 | vfree(info->jumpstack[cpu]); | |
692 | } else { | |
693 | for_each_possible_cpu(cpu) | |
694 | kfree(info->jumpstack[cpu]); | |
695 | } | |
696 | } | |
697 | ||
698 | if (sizeof(void **) * nr_cpu_ids > PAGE_SIZE) | |
699 | vfree(info->jumpstack); | |
700 | else | |
701 | kfree(info->jumpstack); | |
7489aec8 ED |
702 | |
703 | free_percpu(info->stackptr); | |
f3c5c1bf | 704 | |
2e4e6a17 HW |
705 | kfree(info); |
706 | } | |
707 | EXPORT_SYMBOL(xt_free_table_info); | |
708 | ||
709 | /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */ | |
76108cea JE |
710 | struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af, |
711 | const char *name) | |
2e4e6a17 HW |
712 | { |
713 | struct xt_table *t; | |
714 | ||
9e19bb6d | 715 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
2e4e6a17 HW |
716 | return ERR_PTR(-EINTR); |
717 | ||
8d870052 | 718 | list_for_each_entry(t, &net->xt.tables[af], list) |
2e4e6a17 HW |
719 | if (strcmp(t->name, name) == 0 && try_module_get(t->me)) |
720 | return t; | |
9e19bb6d | 721 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
722 | return NULL; |
723 | } | |
724 | EXPORT_SYMBOL_GPL(xt_find_table_lock); | |
725 | ||
726 | void xt_table_unlock(struct xt_table *table) | |
727 | { | |
9e19bb6d | 728 | mutex_unlock(&xt[table->af].mutex); |
2e4e6a17 HW |
729 | } |
730 | EXPORT_SYMBOL_GPL(xt_table_unlock); | |
731 | ||
2722971c | 732 | #ifdef CONFIG_COMPAT |
76108cea | 733 | void xt_compat_lock(u_int8_t af) |
2722971c DM |
734 | { |
735 | mutex_lock(&xt[af].compat_mutex); | |
736 | } | |
737 | EXPORT_SYMBOL_GPL(xt_compat_lock); | |
738 | ||
76108cea | 739 | void xt_compat_unlock(u_int8_t af) |
2722971c DM |
740 | { |
741 | mutex_unlock(&xt[af].compat_mutex); | |
742 | } | |
743 | EXPORT_SYMBOL_GPL(xt_compat_unlock); | |
744 | #endif | |
2e4e6a17 | 745 | |
942e4a2b SH |
746 | DEFINE_PER_CPU(struct xt_info_lock, xt_info_locks); |
747 | EXPORT_PER_CPU_SYMBOL_GPL(xt_info_locks); | |
748 | ||
f3c5c1bf JE |
749 | static int xt_jumpstack_alloc(struct xt_table_info *i) |
750 | { | |
751 | unsigned int size; | |
752 | int cpu; | |
753 | ||
7489aec8 | 754 | i->stackptr = alloc_percpu(unsigned int); |
f3c5c1bf JE |
755 | if (i->stackptr == NULL) |
756 | return -ENOMEM; | |
f3c5c1bf JE |
757 | |
758 | size = sizeof(void **) * nr_cpu_ids; | |
759 | if (size > PAGE_SIZE) | |
760 | i->jumpstack = vmalloc(size); | |
761 | else | |
762 | i->jumpstack = kmalloc(size, GFP_KERNEL); | |
763 | if (i->jumpstack == NULL) | |
764 | return -ENOMEM; | |
765 | memset(i->jumpstack, 0, size); | |
766 | ||
767 | i->stacksize *= xt_jumpstack_multiplier; | |
768 | size = sizeof(void *) * i->stacksize; | |
769 | for_each_possible_cpu(cpu) { | |
770 | if (size > PAGE_SIZE) | |
771 | i->jumpstack[cpu] = vmalloc_node(size, | |
772 | cpu_to_node(cpu)); | |
773 | else | |
774 | i->jumpstack[cpu] = kmalloc_node(size, | |
775 | GFP_KERNEL, cpu_to_node(cpu)); | |
776 | if (i->jumpstack[cpu] == NULL) | |
777 | /* | |
778 | * Freeing will be done later on by the callers. The | |
779 | * chain is: xt_replace_table -> __do_replace -> | |
780 | * do_replace -> xt_free_table_info. | |
781 | */ | |
782 | return -ENOMEM; | |
783 | } | |
784 | ||
785 | return 0; | |
786 | } | |
942e4a2b | 787 | |
2e4e6a17 HW |
788 | struct xt_table_info * |
789 | xt_replace_table(struct xt_table *table, | |
790 | unsigned int num_counters, | |
791 | struct xt_table_info *newinfo, | |
792 | int *error) | |
793 | { | |
942e4a2b | 794 | struct xt_table_info *private; |
f3c5c1bf | 795 | int ret; |
2e4e6a17 | 796 | |
d97a9e47 JE |
797 | ret = xt_jumpstack_alloc(newinfo); |
798 | if (ret < 0) { | |
799 | *error = ret; | |
800 | return NULL; | |
801 | } | |
802 | ||
2e4e6a17 | 803 | /* Do the substitution. */ |
942e4a2b | 804 | local_bh_disable(); |
2e4e6a17 | 805 | private = table->private; |
942e4a2b | 806 | |
2e4e6a17 HW |
807 | /* Check inside lock: is the old number correct? */ |
808 | if (num_counters != private->number) { | |
be91fd5e | 809 | pr_debug("num_counters != table->private->number (%u/%u)\n", |
2e4e6a17 | 810 | num_counters, private->number); |
942e4a2b | 811 | local_bh_enable(); |
2e4e6a17 HW |
812 | *error = -EAGAIN; |
813 | return NULL; | |
814 | } | |
2e4e6a17 | 815 | |
942e4a2b SH |
816 | table->private = newinfo; |
817 | newinfo->initial_entries = private->initial_entries; | |
818 | ||
819 | /* | |
820 | * Even though table entries have now been swapped, other CPU's | |
821 | * may still be using the old entries. This is okay, because | |
822 | * resynchronization happens because of the locking done | |
823 | * during the get_counters() routine. | |
824 | */ | |
825 | local_bh_enable(); | |
826 | ||
827 | return private; | |
2e4e6a17 HW |
828 | } |
829 | EXPORT_SYMBOL_GPL(xt_replace_table); | |
830 | ||
35aad0ff JE |
831 | struct xt_table *xt_register_table(struct net *net, |
832 | const struct xt_table *input_table, | |
a98da11d AD |
833 | struct xt_table_info *bootstrap, |
834 | struct xt_table_info *newinfo) | |
2e4e6a17 HW |
835 | { |
836 | int ret; | |
837 | struct xt_table_info *private; | |
35aad0ff | 838 | struct xt_table *t, *table; |
2e4e6a17 | 839 | |
44d34e72 | 840 | /* Don't add one object to multiple lists. */ |
35aad0ff | 841 | table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL); |
44d34e72 AD |
842 | if (!table) { |
843 | ret = -ENOMEM; | |
844 | goto out; | |
845 | } | |
846 | ||
9e19bb6d | 847 | ret = mutex_lock_interruptible(&xt[table->af].mutex); |
2e4e6a17 | 848 | if (ret != 0) |
44d34e72 | 849 | goto out_free; |
2e4e6a17 HW |
850 | |
851 | /* Don't autoload: we'd eat our tail... */ | |
8d870052 | 852 | list_for_each_entry(t, &net->xt.tables[table->af], list) { |
df0933dc PM |
853 | if (strcmp(t->name, table->name) == 0) { |
854 | ret = -EEXIST; | |
855 | goto unlock; | |
856 | } | |
2e4e6a17 HW |
857 | } |
858 | ||
859 | /* Simplifies replace_table code. */ | |
860 | table->private = bootstrap; | |
78454473 | 861 | |
2e4e6a17 HW |
862 | if (!xt_replace_table(table, 0, newinfo, &ret)) |
863 | goto unlock; | |
864 | ||
865 | private = table->private; | |
be91fd5e | 866 | pr_debug("table->private->number = %u\n", private->number); |
2e4e6a17 HW |
867 | |
868 | /* save number of initial entries */ | |
869 | private->initial_entries = private->number; | |
870 | ||
8d870052 | 871 | list_add(&table->list, &net->xt.tables[table->af]); |
a98da11d AD |
872 | mutex_unlock(&xt[table->af].mutex); |
873 | return table; | |
2e4e6a17 | 874 | |
2e4e6a17 | 875 | unlock: |
9e19bb6d | 876 | mutex_unlock(&xt[table->af].mutex); |
44d34e72 AD |
877 | out_free: |
878 | kfree(table); | |
a98da11d AD |
879 | out: |
880 | return ERR_PTR(ret); | |
2e4e6a17 HW |
881 | } |
882 | EXPORT_SYMBOL_GPL(xt_register_table); | |
883 | ||
884 | void *xt_unregister_table(struct xt_table *table) | |
885 | { | |
886 | struct xt_table_info *private; | |
887 | ||
9e19bb6d | 888 | mutex_lock(&xt[table->af].mutex); |
2e4e6a17 | 889 | private = table->private; |
df0933dc | 890 | list_del(&table->list); |
9e19bb6d | 891 | mutex_unlock(&xt[table->af].mutex); |
44d34e72 | 892 | kfree(table); |
2e4e6a17 HW |
893 | |
894 | return private; | |
895 | } | |
896 | EXPORT_SYMBOL_GPL(xt_unregister_table); | |
897 | ||
898 | #ifdef CONFIG_PROC_FS | |
715cf35a AD |
899 | struct xt_names_priv { |
900 | struct seq_net_private p; | |
76108cea | 901 | u_int8_t af; |
715cf35a | 902 | }; |
025d93d1 | 903 | static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos) |
2e4e6a17 | 904 | { |
715cf35a | 905 | struct xt_names_priv *priv = seq->private; |
1218854a | 906 | struct net *net = seq_file_net(seq); |
76108cea | 907 | u_int8_t af = priv->af; |
2e4e6a17 | 908 | |
025d93d1 | 909 | mutex_lock(&xt[af].mutex); |
715cf35a | 910 | return seq_list_start(&net->xt.tables[af], *pos); |
025d93d1 | 911 | } |
2e4e6a17 | 912 | |
025d93d1 AD |
913 | static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
914 | { | |
715cf35a | 915 | struct xt_names_priv *priv = seq->private; |
1218854a | 916 | struct net *net = seq_file_net(seq); |
76108cea | 917 | u_int8_t af = priv->af; |
2e4e6a17 | 918 | |
715cf35a | 919 | return seq_list_next(v, &net->xt.tables[af], pos); |
2e4e6a17 HW |
920 | } |
921 | ||
025d93d1 | 922 | static void xt_table_seq_stop(struct seq_file *seq, void *v) |
2e4e6a17 | 923 | { |
715cf35a | 924 | struct xt_names_priv *priv = seq->private; |
76108cea | 925 | u_int8_t af = priv->af; |
2e4e6a17 | 926 | |
025d93d1 AD |
927 | mutex_unlock(&xt[af].mutex); |
928 | } | |
2e4e6a17 | 929 | |
025d93d1 AD |
930 | static int xt_table_seq_show(struct seq_file *seq, void *v) |
931 | { | |
932 | struct xt_table *table = list_entry(v, struct xt_table, list); | |
2e4e6a17 | 933 | |
025d93d1 AD |
934 | if (strlen(table->name)) |
935 | return seq_printf(seq, "%s\n", table->name); | |
936 | else | |
937 | return 0; | |
938 | } | |
601e68e1 | 939 | |
025d93d1 AD |
940 | static const struct seq_operations xt_table_seq_ops = { |
941 | .start = xt_table_seq_start, | |
942 | .next = xt_table_seq_next, | |
943 | .stop = xt_table_seq_stop, | |
944 | .show = xt_table_seq_show, | |
945 | }; | |
946 | ||
947 | static int xt_table_open(struct inode *inode, struct file *file) | |
948 | { | |
949 | int ret; | |
715cf35a | 950 | struct xt_names_priv *priv; |
025d93d1 | 951 | |
715cf35a AD |
952 | ret = seq_open_net(inode, file, &xt_table_seq_ops, |
953 | sizeof(struct xt_names_priv)); | |
025d93d1 | 954 | if (!ret) { |
715cf35a AD |
955 | priv = ((struct seq_file *)file->private_data)->private; |
956 | priv->af = (unsigned long)PDE(inode)->data; | |
025d93d1 AD |
957 | } |
958 | return ret; | |
2e4e6a17 HW |
959 | } |
960 | ||
025d93d1 AD |
961 | static const struct file_operations xt_table_ops = { |
962 | .owner = THIS_MODULE, | |
963 | .open = xt_table_open, | |
964 | .read = seq_read, | |
965 | .llseek = seq_lseek, | |
0e93bb94 | 966 | .release = seq_release_net, |
025d93d1 AD |
967 | }; |
968 | ||
eb132205 JE |
969 | /* |
970 | * Traverse state for ip{,6}_{tables,matches} for helping crossing | |
971 | * the multi-AF mutexes. | |
972 | */ | |
973 | struct nf_mttg_trav { | |
974 | struct list_head *head, *curr; | |
975 | uint8_t class, nfproto; | |
976 | }; | |
977 | ||
978 | enum { | |
979 | MTTG_TRAV_INIT, | |
980 | MTTG_TRAV_NFP_UNSPEC, | |
981 | MTTG_TRAV_NFP_SPEC, | |
982 | MTTG_TRAV_DONE, | |
983 | }; | |
984 | ||
985 | static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos, | |
986 | bool is_target) | |
2e4e6a17 | 987 | { |
eb132205 JE |
988 | static const uint8_t next_class[] = { |
989 | [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC, | |
990 | [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE, | |
991 | }; | |
992 | struct nf_mttg_trav *trav = seq->private; | |
993 | ||
994 | switch (trav->class) { | |
995 | case MTTG_TRAV_INIT: | |
996 | trav->class = MTTG_TRAV_NFP_UNSPEC; | |
997 | mutex_lock(&xt[NFPROTO_UNSPEC].mutex); | |
998 | trav->head = trav->curr = is_target ? | |
999 | &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match; | |
1000 | break; | |
1001 | case MTTG_TRAV_NFP_UNSPEC: | |
1002 | trav->curr = trav->curr->next; | |
1003 | if (trav->curr != trav->head) | |
1004 | break; | |
1005 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); | |
1006 | mutex_lock(&xt[trav->nfproto].mutex); | |
1007 | trav->head = trav->curr = is_target ? | |
1008 | &xt[trav->nfproto].target : &xt[trav->nfproto].match; | |
1009 | trav->class = next_class[trav->class]; | |
1010 | break; | |
1011 | case MTTG_TRAV_NFP_SPEC: | |
1012 | trav->curr = trav->curr->next; | |
1013 | if (trav->curr != trav->head) | |
1014 | break; | |
1015 | /* fallthru, _stop will unlock */ | |
1016 | default: | |
1017 | return NULL; | |
1018 | } | |
2e4e6a17 | 1019 | |
eb132205 JE |
1020 | if (ppos != NULL) |
1021 | ++*ppos; | |
1022 | return trav; | |
025d93d1 | 1023 | } |
601e68e1 | 1024 | |
eb132205 JE |
1025 | static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos, |
1026 | bool is_target) | |
025d93d1 | 1027 | { |
eb132205 JE |
1028 | struct nf_mttg_trav *trav = seq->private; |
1029 | unsigned int j; | |
2e4e6a17 | 1030 | |
eb132205 JE |
1031 | trav->class = MTTG_TRAV_INIT; |
1032 | for (j = 0; j < *pos; ++j) | |
1033 | if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL) | |
1034 | return NULL; | |
1035 | return trav; | |
2e4e6a17 HW |
1036 | } |
1037 | ||
eb132205 | 1038 | static void xt_mttg_seq_stop(struct seq_file *seq, void *v) |
2e4e6a17 | 1039 | { |
eb132205 JE |
1040 | struct nf_mttg_trav *trav = seq->private; |
1041 | ||
1042 | switch (trav->class) { | |
1043 | case MTTG_TRAV_NFP_UNSPEC: | |
1044 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); | |
1045 | break; | |
1046 | case MTTG_TRAV_NFP_SPEC: | |
1047 | mutex_unlock(&xt[trav->nfproto].mutex); | |
1048 | break; | |
1049 | } | |
1050 | } | |
2e4e6a17 | 1051 | |
eb132205 JE |
1052 | static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos) |
1053 | { | |
1054 | return xt_mttg_seq_start(seq, pos, false); | |
2e4e6a17 HW |
1055 | } |
1056 | ||
eb132205 | 1057 | static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos) |
2e4e6a17 | 1058 | { |
eb132205 JE |
1059 | return xt_mttg_seq_next(seq, v, ppos, false); |
1060 | } | |
2e4e6a17 | 1061 | |
eb132205 JE |
1062 | static int xt_match_seq_show(struct seq_file *seq, void *v) |
1063 | { | |
1064 | const struct nf_mttg_trav *trav = seq->private; | |
1065 | const struct xt_match *match; | |
1066 | ||
1067 | switch (trav->class) { | |
1068 | case MTTG_TRAV_NFP_UNSPEC: | |
1069 | case MTTG_TRAV_NFP_SPEC: | |
1070 | if (trav->curr == trav->head) | |
1071 | return 0; | |
1072 | match = list_entry(trav->curr, struct xt_match, list); | |
1073 | return (*match->name == '\0') ? 0 : | |
1074 | seq_printf(seq, "%s\n", match->name); | |
1075 | } | |
1076 | return 0; | |
2e4e6a17 HW |
1077 | } |
1078 | ||
025d93d1 AD |
1079 | static const struct seq_operations xt_match_seq_ops = { |
1080 | .start = xt_match_seq_start, | |
1081 | .next = xt_match_seq_next, | |
eb132205 | 1082 | .stop = xt_mttg_seq_stop, |
025d93d1 | 1083 | .show = xt_match_seq_show, |
2e4e6a17 HW |
1084 | }; |
1085 | ||
025d93d1 | 1086 | static int xt_match_open(struct inode *inode, struct file *file) |
2e4e6a17 | 1087 | { |
eb132205 JE |
1088 | struct seq_file *seq; |
1089 | struct nf_mttg_trav *trav; | |
2e4e6a17 HW |
1090 | int ret; |
1091 | ||
eb132205 JE |
1092 | trav = kmalloc(sizeof(*trav), GFP_KERNEL); |
1093 | if (trav == NULL) | |
1094 | return -ENOMEM; | |
2e4e6a17 | 1095 | |
eb132205 JE |
1096 | ret = seq_open(file, &xt_match_seq_ops); |
1097 | if (ret < 0) { | |
1098 | kfree(trav); | |
1099 | return ret; | |
2e4e6a17 | 1100 | } |
eb132205 JE |
1101 | |
1102 | seq = file->private_data; | |
1103 | seq->private = trav; | |
1104 | trav->nfproto = (unsigned long)PDE(inode)->data; | |
1105 | return 0; | |
025d93d1 AD |
1106 | } |
1107 | ||
1108 | static const struct file_operations xt_match_ops = { | |
1109 | .owner = THIS_MODULE, | |
1110 | .open = xt_match_open, | |
1111 | .read = seq_read, | |
1112 | .llseek = seq_lseek, | |
eb132205 | 1113 | .release = seq_release_private, |
025d93d1 | 1114 | }; |
2e4e6a17 | 1115 | |
025d93d1 AD |
1116 | static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos) |
1117 | { | |
eb132205 | 1118 | return xt_mttg_seq_start(seq, pos, true); |
025d93d1 AD |
1119 | } |
1120 | ||
eb132205 | 1121 | static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos) |
025d93d1 | 1122 | { |
eb132205 | 1123 | return xt_mttg_seq_next(seq, v, ppos, true); |
025d93d1 AD |
1124 | } |
1125 | ||
1126 | static int xt_target_seq_show(struct seq_file *seq, void *v) | |
1127 | { | |
eb132205 JE |
1128 | const struct nf_mttg_trav *trav = seq->private; |
1129 | const struct xt_target *target; | |
1130 | ||
1131 | switch (trav->class) { | |
1132 | case MTTG_TRAV_NFP_UNSPEC: | |
1133 | case MTTG_TRAV_NFP_SPEC: | |
1134 | if (trav->curr == trav->head) | |
1135 | return 0; | |
1136 | target = list_entry(trav->curr, struct xt_target, list); | |
1137 | return (*target->name == '\0') ? 0 : | |
1138 | seq_printf(seq, "%s\n", target->name); | |
1139 | } | |
1140 | return 0; | |
025d93d1 AD |
1141 | } |
1142 | ||
1143 | static const struct seq_operations xt_target_seq_ops = { | |
1144 | .start = xt_target_seq_start, | |
1145 | .next = xt_target_seq_next, | |
eb132205 | 1146 | .stop = xt_mttg_seq_stop, |
025d93d1 AD |
1147 | .show = xt_target_seq_show, |
1148 | }; | |
1149 | ||
1150 | static int xt_target_open(struct inode *inode, struct file *file) | |
1151 | { | |
eb132205 JE |
1152 | struct seq_file *seq; |
1153 | struct nf_mttg_trav *trav; | |
025d93d1 AD |
1154 | int ret; |
1155 | ||
eb132205 JE |
1156 | trav = kmalloc(sizeof(*trav), GFP_KERNEL); |
1157 | if (trav == NULL) | |
1158 | return -ENOMEM; | |
025d93d1 | 1159 | |
eb132205 JE |
1160 | ret = seq_open(file, &xt_target_seq_ops); |
1161 | if (ret < 0) { | |
1162 | kfree(trav); | |
1163 | return ret; | |
025d93d1 | 1164 | } |
eb132205 JE |
1165 | |
1166 | seq = file->private_data; | |
1167 | seq->private = trav; | |
1168 | trav->nfproto = (unsigned long)PDE(inode)->data; | |
1169 | return 0; | |
2e4e6a17 HW |
1170 | } |
1171 | ||
025d93d1 | 1172 | static const struct file_operations xt_target_ops = { |
2e4e6a17 | 1173 | .owner = THIS_MODULE, |
025d93d1 | 1174 | .open = xt_target_open, |
2e4e6a17 HW |
1175 | .read = seq_read, |
1176 | .llseek = seq_lseek, | |
eb132205 | 1177 | .release = seq_release_private, |
2e4e6a17 HW |
1178 | }; |
1179 | ||
1180 | #define FORMAT_TABLES "_tables_names" | |
1181 | #define FORMAT_MATCHES "_tables_matches" | |
1182 | #define FORMAT_TARGETS "_tables_targets" | |
1183 | ||
1184 | #endif /* CONFIG_PROC_FS */ | |
1185 | ||
2b95efe7 JE |
1186 | /** |
1187 | * xt_hook_link - set up hooks for a new table | |
1188 | * @table: table with metadata needed to set up hooks | |
1189 | * @fn: Hook function | |
1190 | * | |
1191 | * This function will take care of creating and registering the necessary | |
1192 | * Netfilter hooks for XT tables. | |
1193 | */ | |
1194 | struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn) | |
1195 | { | |
1196 | unsigned int hook_mask = table->valid_hooks; | |
1197 | uint8_t i, num_hooks = hweight32(hook_mask); | |
1198 | uint8_t hooknum; | |
1199 | struct nf_hook_ops *ops; | |
1200 | int ret; | |
1201 | ||
1202 | ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL); | |
1203 | if (ops == NULL) | |
1204 | return ERR_PTR(-ENOMEM); | |
1205 | ||
1206 | for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0; | |
1207 | hook_mask >>= 1, ++hooknum) { | |
1208 | if (!(hook_mask & 1)) | |
1209 | continue; | |
1210 | ops[i].hook = fn; | |
1211 | ops[i].owner = table->me; | |
1212 | ops[i].pf = table->af; | |
1213 | ops[i].hooknum = hooknum; | |
1214 | ops[i].priority = table->priority; | |
1215 | ++i; | |
1216 | } | |
1217 | ||
1218 | ret = nf_register_hooks(ops, num_hooks); | |
1219 | if (ret < 0) { | |
1220 | kfree(ops); | |
1221 | return ERR_PTR(ret); | |
1222 | } | |
1223 | ||
1224 | return ops; | |
1225 | } | |
1226 | EXPORT_SYMBOL_GPL(xt_hook_link); | |
1227 | ||
1228 | /** | |
1229 | * xt_hook_unlink - remove hooks for a table | |
1230 | * @ops: nf_hook_ops array as returned by nf_hook_link | |
1231 | * @hook_mask: the very same mask that was passed to nf_hook_link | |
1232 | */ | |
1233 | void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops) | |
1234 | { | |
1235 | nf_unregister_hooks(ops, hweight32(table->valid_hooks)); | |
1236 | kfree(ops); | |
1237 | } | |
1238 | EXPORT_SYMBOL_GPL(xt_hook_unlink); | |
1239 | ||
76108cea | 1240 | int xt_proto_init(struct net *net, u_int8_t af) |
2e4e6a17 HW |
1241 | { |
1242 | #ifdef CONFIG_PROC_FS | |
1243 | char buf[XT_FUNCTION_MAXNAMELEN]; | |
1244 | struct proc_dir_entry *proc; | |
1245 | #endif | |
1246 | ||
7e9c6eeb | 1247 | if (af >= ARRAY_SIZE(xt_prefix)) |
2e4e6a17 HW |
1248 | return -EINVAL; |
1249 | ||
1250 | ||
1251 | #ifdef CONFIG_PROC_FS | |
ce18afe5 | 1252 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1253 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
8b169240 DL |
1254 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops, |
1255 | (void *)(unsigned long)af); | |
2e4e6a17 HW |
1256 | if (!proc) |
1257 | goto out; | |
2e4e6a17 | 1258 | |
ce18afe5 | 1259 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1260 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
8b169240 DL |
1261 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops, |
1262 | (void *)(unsigned long)af); | |
2e4e6a17 HW |
1263 | if (!proc) |
1264 | goto out_remove_tables; | |
2e4e6a17 | 1265 | |
ce18afe5 | 1266 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1267 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
8b169240 DL |
1268 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops, |
1269 | (void *)(unsigned long)af); | |
2e4e6a17 HW |
1270 | if (!proc) |
1271 | goto out_remove_matches; | |
2e4e6a17 HW |
1272 | #endif |
1273 | ||
1274 | return 0; | |
1275 | ||
1276 | #ifdef CONFIG_PROC_FS | |
1277 | out_remove_matches: | |
ce18afe5 | 1278 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1279 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
3cb609d5 | 1280 | proc_net_remove(net, buf); |
2e4e6a17 HW |
1281 | |
1282 | out_remove_tables: | |
ce18afe5 | 1283 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1284 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
3cb609d5 | 1285 | proc_net_remove(net, buf); |
2e4e6a17 HW |
1286 | out: |
1287 | return -1; | |
1288 | #endif | |
1289 | } | |
1290 | EXPORT_SYMBOL_GPL(xt_proto_init); | |
1291 | ||
76108cea | 1292 | void xt_proto_fini(struct net *net, u_int8_t af) |
2e4e6a17 HW |
1293 | { |
1294 | #ifdef CONFIG_PROC_FS | |
1295 | char buf[XT_FUNCTION_MAXNAMELEN]; | |
1296 | ||
ce18afe5 | 1297 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1298 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
3cb609d5 | 1299 | proc_net_remove(net, buf); |
2e4e6a17 | 1300 | |
ce18afe5 | 1301 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1302 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
3cb609d5 | 1303 | proc_net_remove(net, buf); |
2e4e6a17 | 1304 | |
ce18afe5 | 1305 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1306 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
3cb609d5 | 1307 | proc_net_remove(net, buf); |
2e4e6a17 HW |
1308 | #endif /*CONFIG_PROC_FS*/ |
1309 | } | |
1310 | EXPORT_SYMBOL_GPL(xt_proto_fini); | |
1311 | ||
8d870052 AD |
1312 | static int __net_init xt_net_init(struct net *net) |
1313 | { | |
1314 | int i; | |
1315 | ||
7e9c6eeb | 1316 | for (i = 0; i < NFPROTO_NUMPROTO; i++) |
8d870052 AD |
1317 | INIT_LIST_HEAD(&net->xt.tables[i]); |
1318 | return 0; | |
1319 | } | |
1320 | ||
1321 | static struct pernet_operations xt_net_ops = { | |
1322 | .init = xt_net_init, | |
1323 | }; | |
2e4e6a17 HW |
1324 | |
1325 | static int __init xt_init(void) | |
1326 | { | |
942e4a2b SH |
1327 | unsigned int i; |
1328 | int rv; | |
1329 | ||
1330 | for_each_possible_cpu(i) { | |
1331 | struct xt_info_lock *lock = &per_cpu(xt_info_locks, i); | |
1332 | spin_lock_init(&lock->lock); | |
1333 | lock->readers = 0; | |
1334 | } | |
2e4e6a17 | 1335 | |
7e9c6eeb | 1336 | xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL); |
2e4e6a17 HW |
1337 | if (!xt) |
1338 | return -ENOMEM; | |
1339 | ||
7e9c6eeb | 1340 | for (i = 0; i < NFPROTO_NUMPROTO; i++) { |
9e19bb6d | 1341 | mutex_init(&xt[i].mutex); |
2722971c DM |
1342 | #ifdef CONFIG_COMPAT |
1343 | mutex_init(&xt[i].compat_mutex); | |
b386d9f5 | 1344 | xt[i].compat_offsets = NULL; |
2722971c | 1345 | #endif |
2e4e6a17 HW |
1346 | INIT_LIST_HEAD(&xt[i].target); |
1347 | INIT_LIST_HEAD(&xt[i].match); | |
2e4e6a17 | 1348 | } |
8d870052 AD |
1349 | rv = register_pernet_subsys(&xt_net_ops); |
1350 | if (rv < 0) | |
1351 | kfree(xt); | |
1352 | return rv; | |
2e4e6a17 HW |
1353 | } |
1354 | ||
1355 | static void __exit xt_fini(void) | |
1356 | { | |
8d870052 | 1357 | unregister_pernet_subsys(&xt_net_ops); |
2e4e6a17 HW |
1358 | kfree(xt); |
1359 | } | |
1360 | ||
1361 | module_init(xt_init); | |
1362 | module_exit(xt_fini); | |
1363 |