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