]>
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> |
f13f2aee | 29 | #include <linux/user_namespace.h> |
457c4cbc | 30 | #include <net/net_namespace.h> |
2e4e6a17 HW |
31 | |
32 | #include <linux/netfilter/x_tables.h> | |
33 | #include <linux/netfilter_arp.h> | |
e3eaa991 JE |
34 | #include <linux/netfilter_ipv4/ip_tables.h> |
35 | #include <linux/netfilter_ipv6/ip6_tables.h> | |
36 | #include <linux/netfilter_arp/arp_tables.h> | |
9e19bb6d | 37 | |
2e4e6a17 HW |
38 | MODULE_LICENSE("GPL"); |
39 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
043ef46c | 40 | MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module"); |
2e4e6a17 | 41 | |
ae0ac0ed | 42 | #define XT_PCPU_BLOCK_SIZE 4096 |
19926968 | 43 | #define XT_MAX_TABLE_SIZE (512 * 1024 * 1024) |
2e4e6a17 | 44 | |
b386d9f5 | 45 | struct compat_delta { |
255d0dc3 ED |
46 | unsigned int offset; /* offset in kernel */ |
47 | int delta; /* delta in 32bit user land */ | |
b386d9f5 PM |
48 | }; |
49 | ||
2e4e6a17 | 50 | struct xt_af { |
9e19bb6d | 51 | struct mutex mutex; |
2e4e6a17 HW |
52 | struct list_head match; |
53 | struct list_head target; | |
b386d9f5 | 54 | #ifdef CONFIG_COMPAT |
2722971c | 55 | struct mutex compat_mutex; |
255d0dc3 ED |
56 | struct compat_delta *compat_tab; |
57 | unsigned int number; /* number of slots in compat_tab[] */ | |
58 | unsigned int cur; /* number of used slots in compat_tab[] */ | |
b386d9f5 | 59 | #endif |
2e4e6a17 HW |
60 | }; |
61 | ||
62 | static struct xt_af *xt; | |
63 | ||
7e9c6eeb JE |
64 | static const char *const xt_prefix[NFPROTO_NUMPROTO] = { |
65 | [NFPROTO_UNSPEC] = "x", | |
66 | [NFPROTO_IPV4] = "ip", | |
67 | [NFPROTO_ARP] = "arp", | |
68 | [NFPROTO_BRIDGE] = "eb", | |
69 | [NFPROTO_IPV6] = "ip6", | |
37f9f733 PM |
70 | }; |
71 | ||
2e4e6a17 | 72 | /* Registration hooks for targets. */ |
7926dbfa | 73 | int xt_register_target(struct xt_target *target) |
2e4e6a17 | 74 | { |
76108cea | 75 | u_int8_t af = target->family; |
2e4e6a17 | 76 | |
7926dbfa | 77 | mutex_lock(&xt[af].mutex); |
2e4e6a17 | 78 | list_add(&target->list, &xt[af].target); |
9e19bb6d | 79 | mutex_unlock(&xt[af].mutex); |
7926dbfa | 80 | return 0; |
2e4e6a17 HW |
81 | } |
82 | EXPORT_SYMBOL(xt_register_target); | |
83 | ||
84 | void | |
a45049c5 | 85 | xt_unregister_target(struct xt_target *target) |
2e4e6a17 | 86 | { |
76108cea | 87 | u_int8_t af = target->family; |
a45049c5 | 88 | |
9e19bb6d | 89 | mutex_lock(&xt[af].mutex); |
df0933dc | 90 | list_del(&target->list); |
9e19bb6d | 91 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
92 | } |
93 | EXPORT_SYMBOL(xt_unregister_target); | |
94 | ||
52d9c42e PM |
95 | int |
96 | xt_register_targets(struct xt_target *target, unsigned int n) | |
97 | { | |
98 | unsigned int i; | |
99 | int err = 0; | |
100 | ||
101 | for (i = 0; i < n; i++) { | |
102 | err = xt_register_target(&target[i]); | |
103 | if (err) | |
104 | goto err; | |
105 | } | |
106 | return err; | |
107 | ||
108 | err: | |
109 | if (i > 0) | |
110 | xt_unregister_targets(target, i); | |
111 | return err; | |
112 | } | |
113 | EXPORT_SYMBOL(xt_register_targets); | |
114 | ||
115 | void | |
116 | xt_unregister_targets(struct xt_target *target, unsigned int n) | |
117 | { | |
f68c5301 CG |
118 | while (n-- > 0) |
119 | xt_unregister_target(&target[n]); | |
52d9c42e PM |
120 | } |
121 | EXPORT_SYMBOL(xt_unregister_targets); | |
122 | ||
7926dbfa | 123 | int xt_register_match(struct xt_match *match) |
2e4e6a17 | 124 | { |
76108cea | 125 | u_int8_t af = match->family; |
2e4e6a17 | 126 | |
7926dbfa | 127 | mutex_lock(&xt[af].mutex); |
2e4e6a17 | 128 | list_add(&match->list, &xt[af].match); |
9e19bb6d | 129 | mutex_unlock(&xt[af].mutex); |
7926dbfa | 130 | return 0; |
2e4e6a17 HW |
131 | } |
132 | EXPORT_SYMBOL(xt_register_match); | |
133 | ||
134 | void | |
a45049c5 | 135 | xt_unregister_match(struct xt_match *match) |
2e4e6a17 | 136 | { |
76108cea | 137 | u_int8_t af = match->family; |
a45049c5 | 138 | |
9e19bb6d | 139 | mutex_lock(&xt[af].mutex); |
df0933dc | 140 | list_del(&match->list); |
9e19bb6d | 141 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
142 | } |
143 | EXPORT_SYMBOL(xt_unregister_match); | |
144 | ||
52d9c42e PM |
145 | int |
146 | xt_register_matches(struct xt_match *match, unsigned int n) | |
147 | { | |
148 | unsigned int i; | |
149 | int err = 0; | |
150 | ||
151 | for (i = 0; i < n; i++) { | |
152 | err = xt_register_match(&match[i]); | |
153 | if (err) | |
154 | goto err; | |
155 | } | |
156 | return err; | |
157 | ||
158 | err: | |
159 | if (i > 0) | |
160 | xt_unregister_matches(match, i); | |
161 | return err; | |
162 | } | |
163 | EXPORT_SYMBOL(xt_register_matches); | |
164 | ||
165 | void | |
166 | xt_unregister_matches(struct xt_match *match, unsigned int n) | |
167 | { | |
f68c5301 CG |
168 | while (n-- > 0) |
169 | xt_unregister_match(&match[n]); | |
52d9c42e PM |
170 | } |
171 | EXPORT_SYMBOL(xt_unregister_matches); | |
172 | ||
2e4e6a17 HW |
173 | |
174 | /* | |
175 | * These are weird, but module loading must not be done with mutex | |
176 | * held (since they will register), and we have to have a single | |
adb00ae2 | 177 | * function to use. |
2e4e6a17 HW |
178 | */ |
179 | ||
180 | /* Find match, grabs ref. Returns ERR_PTR() on error. */ | |
76108cea | 181 | struct xt_match *xt_find_match(u8 af, const char *name, u8 revision) |
2e4e6a17 HW |
182 | { |
183 | struct xt_match *m; | |
42046e2e | 184 | int err = -ENOENT; |
2e4e6a17 | 185 | |
dceb48d8 FW |
186 | if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN) |
187 | return ERR_PTR(-EINVAL); | |
188 | ||
7926dbfa | 189 | mutex_lock(&xt[af].mutex); |
2e4e6a17 HW |
190 | list_for_each_entry(m, &xt[af].match, list) { |
191 | if (strcmp(m->name, name) == 0) { | |
192 | if (m->revision == revision) { | |
193 | if (try_module_get(m->me)) { | |
9e19bb6d | 194 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
195 | return m; |
196 | } | |
197 | } else | |
198 | err = -EPROTOTYPE; /* Found something. */ | |
199 | } | |
200 | } | |
9e19bb6d | 201 | mutex_unlock(&xt[af].mutex); |
55b69e91 JE |
202 | |
203 | if (af != NFPROTO_UNSPEC) | |
204 | /* Try searching again in the family-independent list */ | |
205 | return xt_find_match(NFPROTO_UNSPEC, name, revision); | |
206 | ||
2e4e6a17 HW |
207 | return ERR_PTR(err); |
208 | } | |
209 | EXPORT_SYMBOL(xt_find_match); | |
210 | ||
fd0ec0e6 JE |
211 | struct xt_match * |
212 | xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision) | |
213 | { | |
214 | struct xt_match *match; | |
215 | ||
da17c73b ED |
216 | if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN) |
217 | return ERR_PTR(-EINVAL); | |
218 | ||
adb00ae2 SH |
219 | match = xt_find_match(nfproto, name, revision); |
220 | if (IS_ERR(match)) { | |
221 | request_module("%st_%s", xt_prefix[nfproto], name); | |
222 | match = xt_find_match(nfproto, name, revision); | |
223 | } | |
224 | ||
225 | return match; | |
fd0ec0e6 JE |
226 | } |
227 | EXPORT_SYMBOL_GPL(xt_request_find_match); | |
228 | ||
2e4e6a17 | 229 | /* Find target, grabs ref. Returns ERR_PTR() on error. */ |
76108cea | 230 | struct xt_target *xt_find_target(u8 af, const char *name, u8 revision) |
2e4e6a17 HW |
231 | { |
232 | struct xt_target *t; | |
42046e2e | 233 | int err = -ENOENT; |
2e4e6a17 | 234 | |
dceb48d8 FW |
235 | if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN) |
236 | return ERR_PTR(-EINVAL); | |
237 | ||
7926dbfa | 238 | mutex_lock(&xt[af].mutex); |
2e4e6a17 HW |
239 | list_for_each_entry(t, &xt[af].target, list) { |
240 | if (strcmp(t->name, name) == 0) { | |
241 | if (t->revision == revision) { | |
242 | if (try_module_get(t->me)) { | |
9e19bb6d | 243 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
244 | return t; |
245 | } | |
246 | } else | |
247 | err = -EPROTOTYPE; /* Found something. */ | |
248 | } | |
249 | } | |
9e19bb6d | 250 | mutex_unlock(&xt[af].mutex); |
55b69e91 JE |
251 | |
252 | if (af != NFPROTO_UNSPEC) | |
253 | /* Try searching again in the family-independent list */ | |
254 | return xt_find_target(NFPROTO_UNSPEC, name, revision); | |
255 | ||
2e4e6a17 HW |
256 | return ERR_PTR(err); |
257 | } | |
258 | EXPORT_SYMBOL(xt_find_target); | |
259 | ||
76108cea | 260 | struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision) |
2e4e6a17 HW |
261 | { |
262 | struct xt_target *target; | |
263 | ||
da17c73b ED |
264 | if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN) |
265 | return ERR_PTR(-EINVAL); | |
266 | ||
adb00ae2 SH |
267 | target = xt_find_target(af, name, revision); |
268 | if (IS_ERR(target)) { | |
269 | request_module("%st_%s", xt_prefix[af], name); | |
270 | target = xt_find_target(af, name, revision); | |
271 | } | |
272 | ||
273 | return target; | |
2e4e6a17 HW |
274 | } |
275 | EXPORT_SYMBOL_GPL(xt_request_find_target); | |
276 | ||
f32815d2 WB |
277 | |
278 | static int xt_obj_to_user(u16 __user *psize, u16 size, | |
279 | void __user *pname, const char *name, | |
280 | u8 __user *prev, u8 rev) | |
281 | { | |
282 | if (put_user(size, psize)) | |
283 | return -EFAULT; | |
284 | if (copy_to_user(pname, name, strlen(name) + 1)) | |
285 | return -EFAULT; | |
286 | if (put_user(rev, prev)) | |
287 | return -EFAULT; | |
288 | ||
289 | return 0; | |
290 | } | |
291 | ||
292 | #define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE) \ | |
293 | xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size, \ | |
294 | U->u.user.name, K->u.kernel.TYPE->name, \ | |
295 | &U->u.user.revision, K->u.kernel.TYPE->revision) | |
296 | ||
297 | int xt_data_to_user(void __user *dst, const void *src, | |
324318f0 | 298 | int usersize, int size, int aligned_size) |
f32815d2 WB |
299 | { |
300 | usersize = usersize ? : size; | |
301 | if (copy_to_user(dst, src, usersize)) | |
302 | return -EFAULT; | |
324318f0 WB |
303 | if (usersize != aligned_size && |
304 | clear_user(dst + usersize, aligned_size - usersize)) | |
f32815d2 WB |
305 | return -EFAULT; |
306 | ||
307 | return 0; | |
308 | } | |
309 | EXPORT_SYMBOL_GPL(xt_data_to_user); | |
310 | ||
751a9c76 | 311 | #define XT_DATA_TO_USER(U, K, TYPE) \ |
f32815d2 WB |
312 | xt_data_to_user(U->data, K->data, \ |
313 | K->u.kernel.TYPE->usersize, \ | |
751a9c76 WB |
314 | K->u.kernel.TYPE->TYPE##size, \ |
315 | XT_ALIGN(K->u.kernel.TYPE->TYPE##size)) | |
f32815d2 WB |
316 | |
317 | int xt_match_to_user(const struct xt_entry_match *m, | |
318 | struct xt_entry_match __user *u) | |
319 | { | |
320 | return XT_OBJ_TO_USER(u, m, match, 0) || | |
751a9c76 | 321 | XT_DATA_TO_USER(u, m, match); |
f32815d2 WB |
322 | } |
323 | EXPORT_SYMBOL_GPL(xt_match_to_user); | |
324 | ||
325 | int xt_target_to_user(const struct xt_entry_target *t, | |
326 | struct xt_entry_target __user *u) | |
327 | { | |
328 | return XT_OBJ_TO_USER(u, t, target, 0) || | |
751a9c76 | 329 | XT_DATA_TO_USER(u, t, target); |
f32815d2 WB |
330 | } |
331 | EXPORT_SYMBOL_GPL(xt_target_to_user); | |
332 | ||
76108cea | 333 | static int match_revfn(u8 af, const char *name, u8 revision, int *bestp) |
2e4e6a17 | 334 | { |
5452e425 | 335 | const struct xt_match *m; |
2e4e6a17 HW |
336 | int have_rev = 0; |
337 | ||
338 | list_for_each_entry(m, &xt[af].match, list) { | |
339 | if (strcmp(m->name, name) == 0) { | |
340 | if (m->revision > *bestp) | |
341 | *bestp = m->revision; | |
342 | if (m->revision == revision) | |
343 | have_rev = 1; | |
344 | } | |
345 | } | |
656caff2 PM |
346 | |
347 | if (af != NFPROTO_UNSPEC && !have_rev) | |
348 | return match_revfn(NFPROTO_UNSPEC, name, revision, bestp); | |
349 | ||
2e4e6a17 HW |
350 | return have_rev; |
351 | } | |
352 | ||
76108cea | 353 | static int target_revfn(u8 af, const char *name, u8 revision, int *bestp) |
2e4e6a17 | 354 | { |
5452e425 | 355 | const struct xt_target *t; |
2e4e6a17 HW |
356 | int have_rev = 0; |
357 | ||
358 | list_for_each_entry(t, &xt[af].target, list) { | |
359 | if (strcmp(t->name, name) == 0) { | |
360 | if (t->revision > *bestp) | |
361 | *bestp = t->revision; | |
362 | if (t->revision == revision) | |
363 | have_rev = 1; | |
364 | } | |
365 | } | |
656caff2 PM |
366 | |
367 | if (af != NFPROTO_UNSPEC && !have_rev) | |
368 | return target_revfn(NFPROTO_UNSPEC, name, revision, bestp); | |
369 | ||
2e4e6a17 HW |
370 | return have_rev; |
371 | } | |
372 | ||
373 | /* Returns true or false (if no such extension at all) */ | |
76108cea | 374 | int xt_find_revision(u8 af, const char *name, u8 revision, int target, |
2e4e6a17 HW |
375 | int *err) |
376 | { | |
377 | int have_rev, best = -1; | |
378 | ||
7926dbfa | 379 | mutex_lock(&xt[af].mutex); |
2e4e6a17 HW |
380 | if (target == 1) |
381 | have_rev = target_revfn(af, name, revision, &best); | |
382 | else | |
383 | have_rev = match_revfn(af, name, revision, &best); | |
9e19bb6d | 384 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
385 | |
386 | /* Nothing at all? Return 0 to try loading module. */ | |
387 | if (best == -1) { | |
388 | *err = -ENOENT; | |
389 | return 0; | |
390 | } | |
391 | ||
392 | *err = best; | |
393 | if (!have_rev) | |
394 | *err = -EPROTONOSUPPORT; | |
395 | return 1; | |
396 | } | |
397 | EXPORT_SYMBOL_GPL(xt_find_revision); | |
398 | ||
5b76c494 JE |
399 | static char * |
400 | textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto) | |
45185364 | 401 | { |
5b76c494 | 402 | static const char *const inetbr_names[] = { |
45185364 JE |
403 | "PREROUTING", "INPUT", "FORWARD", |
404 | "OUTPUT", "POSTROUTING", "BROUTING", | |
405 | }; | |
5b76c494 JE |
406 | static const char *const arp_names[] = { |
407 | "INPUT", "FORWARD", "OUTPUT", | |
408 | }; | |
409 | const char *const *names; | |
410 | unsigned int i, max; | |
45185364 JE |
411 | char *p = buf; |
412 | bool np = false; | |
413 | int res; | |
414 | ||
5b76c494 JE |
415 | names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names; |
416 | max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) : | |
417 | ARRAY_SIZE(inetbr_names); | |
45185364 | 418 | *p = '\0'; |
5b76c494 | 419 | for (i = 0; i < max; ++i) { |
45185364 JE |
420 | if (!(mask & (1 << i))) |
421 | continue; | |
422 | res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]); | |
423 | if (res > 0) { | |
424 | size -= res; | |
425 | p += res; | |
426 | } | |
427 | np = true; | |
428 | } | |
429 | ||
430 | return buf; | |
431 | } | |
432 | ||
b1d0a5d0 FW |
433 | /** |
434 | * xt_check_proc_name - check that name is suitable for /proc file creation | |
435 | * | |
436 | * @name: file name candidate | |
437 | * @size: length of buffer | |
438 | * | |
439 | * some x_tables modules wish to create a file in /proc. | |
440 | * This function makes sure that the name is suitable for this | |
441 | * purpose, it checks that name is NUL terminated and isn't a 'special' | |
442 | * name, like "..". | |
443 | * | |
444 | * returns negative number on error or 0 if name is useable. | |
445 | */ | |
446 | int xt_check_proc_name(const char *name, unsigned int size) | |
447 | { | |
448 | if (name[0] == '\0') | |
449 | return -EINVAL; | |
450 | ||
451 | if (strnlen(name, size) == size) | |
452 | return -ENAMETOOLONG; | |
453 | ||
454 | if (strcmp(name, ".") == 0 || | |
455 | strcmp(name, "..") == 0 || | |
456 | strchr(name, '/')) | |
457 | return -EINVAL; | |
458 | ||
459 | return 0; | |
460 | } | |
461 | EXPORT_SYMBOL(xt_check_proc_name); | |
462 | ||
916a917d | 463 | int xt_check_match(struct xt_mtchk_param *par, |
9b4fce7a | 464 | unsigned int size, u_int8_t proto, bool inv_proto) |
37f9f733 | 465 | { |
bd414ee6 JE |
466 | int ret; |
467 | ||
9b4fce7a JE |
468 | if (XT_ALIGN(par->match->matchsize) != size && |
469 | par->match->matchsize != -1) { | |
043ef46c JE |
470 | /* |
471 | * ebt_among is exempt from centralized matchsize checking | |
472 | * because it uses a dynamic-size data set. | |
473 | */ | |
1b6cd671 FW |
474 | pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n", |
475 | xt_prefix[par->family], par->match->name, | |
476 | par->match->revision, | |
477 | XT_ALIGN(par->match->matchsize), size); | |
37f9f733 PM |
478 | return -EINVAL; |
479 | } | |
9b4fce7a JE |
480 | if (par->match->table != NULL && |
481 | strcmp(par->match->table, par->table) != 0) { | |
1b6cd671 FW |
482 | pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n", |
483 | xt_prefix[par->family], par->match->name, | |
484 | par->match->table, par->table); | |
37f9f733 PM |
485 | return -EINVAL; |
486 | } | |
9b4fce7a | 487 | if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) { |
45185364 JE |
488 | char used[64], allow[64]; |
489 | ||
1b6cd671 FW |
490 | pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n", |
491 | xt_prefix[par->family], par->match->name, | |
492 | textify_hooks(used, sizeof(used), | |
493 | par->hook_mask, par->family), | |
494 | textify_hooks(allow, sizeof(allow), | |
495 | par->match->hooks, | |
496 | par->family)); | |
37f9f733 PM |
497 | return -EINVAL; |
498 | } | |
9b4fce7a | 499 | if (par->match->proto && (par->match->proto != proto || inv_proto)) { |
1b6cd671 FW |
500 | pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n", |
501 | xt_prefix[par->family], par->match->name, | |
502 | par->match->proto); | |
37f9f733 PM |
503 | return -EINVAL; |
504 | } | |
bd414ee6 JE |
505 | if (par->match->checkentry != NULL) { |
506 | ret = par->match->checkentry(par); | |
507 | if (ret < 0) | |
508 | return ret; | |
509 | else if (ret > 0) | |
510 | /* Flag up potential errors. */ | |
511 | return -EIO; | |
512 | } | |
37f9f733 PM |
513 | return 0; |
514 | } | |
515 | EXPORT_SYMBOL_GPL(xt_check_match); | |
516 | ||
13631bfc FW |
517 | /** xt_check_entry_match - check that matches end before start of target |
518 | * | |
519 | * @match: beginning of xt_entry_match | |
520 | * @target: beginning of this rules target (alleged end of matches) | |
521 | * @alignment: alignment requirement of match structures | |
522 | * | |
523 | * Validates that all matches add up to the beginning of the target, | |
524 | * and that each match covers at least the base structure size. | |
525 | * | |
526 | * Return: 0 on success, negative errno on failure. | |
527 | */ | |
528 | static int xt_check_entry_match(const char *match, const char *target, | |
529 | const size_t alignment) | |
530 | { | |
531 | const struct xt_entry_match *pos; | |
532 | int length = target - match; | |
533 | ||
534 | if (length == 0) /* no matches */ | |
535 | return 0; | |
536 | ||
537 | pos = (struct xt_entry_match *)match; | |
538 | do { | |
539 | if ((unsigned long)pos % alignment) | |
540 | return -EINVAL; | |
541 | ||
542 | if (length < (int)sizeof(struct xt_entry_match)) | |
543 | return -EINVAL; | |
544 | ||
545 | if (pos->u.match_size < sizeof(struct xt_entry_match)) | |
546 | return -EINVAL; | |
547 | ||
548 | if (pos->u.match_size > length) | |
549 | return -EINVAL; | |
550 | ||
551 | length -= pos->u.match_size; | |
552 | pos = ((void *)((char *)(pos) + (pos)->u.match_size)); | |
553 | } while (length > 0); | |
554 | ||
555 | return 0; | |
556 | } | |
557 | ||
1b293e30 FW |
558 | /** xt_check_table_hooks - check hook entry points are sane |
559 | * | |
560 | * @info xt_table_info to check | |
561 | * @valid_hooks - hook entry points that we can enter from | |
562 | * | |
563 | * Validates that the hook entry and underflows points are set up. | |
564 | * | |
565 | * Return: 0 on success, negative errno on failure. | |
566 | */ | |
567 | int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks) | |
568 | { | |
e816a2ce FW |
569 | const char *err = "unsorted underflow"; |
570 | unsigned int i, max_uflow, max_entry; | |
571 | bool check_hooks = false; | |
1b293e30 FW |
572 | |
573 | BUILD_BUG_ON(ARRAY_SIZE(info->hook_entry) != ARRAY_SIZE(info->underflow)); | |
574 | ||
e816a2ce FW |
575 | max_entry = 0; |
576 | max_uflow = 0; | |
577 | ||
1b293e30 FW |
578 | for (i = 0; i < ARRAY_SIZE(info->hook_entry); i++) { |
579 | if (!(valid_hooks & (1 << i))) | |
580 | continue; | |
581 | ||
582 | if (info->hook_entry[i] == 0xFFFFFFFF) | |
583 | return -EINVAL; | |
584 | if (info->underflow[i] == 0xFFFFFFFF) | |
585 | return -EINVAL; | |
e816a2ce FW |
586 | |
587 | if (check_hooks) { | |
588 | if (max_uflow > info->underflow[i]) | |
589 | goto error; | |
590 | ||
591 | if (max_uflow == info->underflow[i]) { | |
592 | err = "duplicate underflow"; | |
593 | goto error; | |
594 | } | |
595 | if (max_entry > info->hook_entry[i]) { | |
596 | err = "unsorted entry"; | |
597 | goto error; | |
598 | } | |
599 | if (max_entry == info->hook_entry[i]) { | |
600 | err = "duplicate entry"; | |
601 | goto error; | |
602 | } | |
603 | } | |
604 | max_entry = info->hook_entry[i]; | |
605 | max_uflow = info->underflow[i]; | |
606 | check_hooks = true; | |
1b293e30 FW |
607 | } |
608 | ||
609 | return 0; | |
e816a2ce FW |
610 | error: |
611 | pr_err_ratelimited("%s at hook %d\n", err, i); | |
612 | return -EINVAL; | |
1b293e30 FW |
613 | } |
614 | EXPORT_SYMBOL(xt_check_table_hooks); | |
615 | ||
72597135 FW |
616 | static bool verdict_ok(int verdict) |
617 | { | |
618 | if (verdict > 0) | |
619 | return true; | |
620 | ||
621 | if (verdict < 0) { | |
622 | int v = -verdict - 1; | |
623 | ||
624 | if (verdict == XT_RETURN) | |
625 | return true; | |
626 | ||
627 | switch (v) { | |
628 | case NF_ACCEPT: return true; | |
629 | case NF_DROP: return true; | |
630 | case NF_QUEUE: return true; | |
631 | default: | |
632 | break; | |
633 | } | |
634 | ||
635 | return false; | |
636 | } | |
637 | ||
638 | return false; | |
639 | } | |
640 | ||
641 | static bool error_tg_ok(unsigned int usersize, unsigned int kernsize, | |
642 | const char *msg, unsigned int msglen) | |
643 | { | |
644 | return usersize == kernsize && strnlen(msg, msglen) < msglen; | |
645 | } | |
646 | ||
2722971c | 647 | #ifdef CONFIG_COMPAT |
255d0dc3 | 648 | int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta) |
b386d9f5 | 649 | { |
255d0dc3 | 650 | struct xt_af *xp = &xt[af]; |
b386d9f5 | 651 | |
89370860 FW |
652 | WARN_ON(!mutex_is_locked(&xt[af].compat_mutex)); |
653 | ||
7d7d7e02 FW |
654 | if (WARN_ON(!xp->compat_tab)) |
655 | return -ENOMEM; | |
b386d9f5 | 656 | |
255d0dc3 ED |
657 | if (xp->cur >= xp->number) |
658 | return -EINVAL; | |
b386d9f5 | 659 | |
255d0dc3 ED |
660 | if (xp->cur) |
661 | delta += xp->compat_tab[xp->cur - 1].delta; | |
662 | xp->compat_tab[xp->cur].offset = offset; | |
663 | xp->compat_tab[xp->cur].delta = delta; | |
664 | xp->cur++; | |
b386d9f5 PM |
665 | return 0; |
666 | } | |
667 | EXPORT_SYMBOL_GPL(xt_compat_add_offset); | |
668 | ||
76108cea | 669 | void xt_compat_flush_offsets(u_int8_t af) |
b386d9f5 | 670 | { |
89370860 FW |
671 | WARN_ON(!mutex_is_locked(&xt[af].compat_mutex)); |
672 | ||
255d0dc3 ED |
673 | if (xt[af].compat_tab) { |
674 | vfree(xt[af].compat_tab); | |
675 | xt[af].compat_tab = NULL; | |
676 | xt[af].number = 0; | |
5a6351ee | 677 | xt[af].cur = 0; |
b386d9f5 PM |
678 | } |
679 | } | |
680 | EXPORT_SYMBOL_GPL(xt_compat_flush_offsets); | |
681 | ||
3e5e524f | 682 | int xt_compat_calc_jump(u_int8_t af, unsigned int offset) |
b386d9f5 | 683 | { |
255d0dc3 ED |
684 | struct compat_delta *tmp = xt[af].compat_tab; |
685 | int mid, left = 0, right = xt[af].cur - 1; | |
686 | ||
687 | while (left <= right) { | |
688 | mid = (left + right) >> 1; | |
689 | if (offset > tmp[mid].offset) | |
690 | left = mid + 1; | |
691 | else if (offset < tmp[mid].offset) | |
692 | right = mid - 1; | |
693 | else | |
694 | return mid ? tmp[mid - 1].delta : 0; | |
695 | } | |
5a6351ee | 696 | return left ? tmp[left - 1].delta : 0; |
b386d9f5 PM |
697 | } |
698 | EXPORT_SYMBOL_GPL(xt_compat_calc_jump); | |
699 | ||
9782a11e | 700 | int xt_compat_init_offsets(u8 af, unsigned int number) |
255d0dc3 | 701 | { |
7d7d7e02 FW |
702 | size_t mem; |
703 | ||
89370860 FW |
704 | WARN_ON(!mutex_is_locked(&xt[af].compat_mutex)); |
705 | ||
7d7d7e02 FW |
706 | if (!number || number > (INT_MAX / sizeof(struct compat_delta))) |
707 | return -EINVAL; | |
708 | ||
709 | if (WARN_ON(xt[af].compat_tab)) | |
710 | return -EINVAL; | |
711 | ||
712 | mem = sizeof(struct compat_delta) * number; | |
713 | if (mem > XT_MAX_TABLE_SIZE) | |
714 | return -ENOMEM; | |
715 | ||
716 | xt[af].compat_tab = vmalloc(mem); | |
717 | if (!xt[af].compat_tab) | |
718 | return -ENOMEM; | |
719 | ||
255d0dc3 ED |
720 | xt[af].number = number; |
721 | xt[af].cur = 0; | |
9782a11e FW |
722 | |
723 | return 0; | |
255d0dc3 ED |
724 | } |
725 | EXPORT_SYMBOL(xt_compat_init_offsets); | |
726 | ||
5452e425 | 727 | int xt_compat_match_offset(const struct xt_match *match) |
2722971c | 728 | { |
9fa492cd PM |
729 | u_int16_t csize = match->compatsize ? : match->matchsize; |
730 | return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize); | |
731 | } | |
732 | EXPORT_SYMBOL_GPL(xt_compat_match_offset); | |
733 | ||
0188346f FW |
734 | void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr, |
735 | unsigned int *size) | |
9fa492cd | 736 | { |
5452e425 | 737 | const struct xt_match *match = m->u.kernel.match; |
9fa492cd PM |
738 | struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m; |
739 | int pad, off = xt_compat_match_offset(match); | |
740 | u_int16_t msize = cm->u.user.match_size; | |
09d96860 | 741 | char name[sizeof(m->u.user.name)]; |
9fa492cd PM |
742 | |
743 | m = *dstptr; | |
744 | memcpy(m, cm, sizeof(*cm)); | |
745 | if (match->compat_from_user) | |
746 | match->compat_from_user(m->data, cm->data); | |
747 | else | |
748 | memcpy(m->data, cm->data, msize - sizeof(*cm)); | |
749 | pad = XT_ALIGN(match->matchsize) - match->matchsize; | |
750 | if (pad > 0) | |
751 | memset(m->data + match->matchsize, 0, pad); | |
752 | ||
753 | msize += off; | |
754 | m->u.user.match_size = msize; | |
09d96860 FW |
755 | strlcpy(name, match->name, sizeof(name)); |
756 | module_put(match->me); | |
757 | strncpy(m->u.user.name, name, sizeof(m->u.user.name)); | |
9fa492cd PM |
758 | |
759 | *size += off; | |
760 | *dstptr += msize; | |
761 | } | |
762 | EXPORT_SYMBOL_GPL(xt_compat_match_from_user); | |
763 | ||
751a9c76 WB |
764 | #define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE) \ |
765 | xt_data_to_user(U->data, K->data, \ | |
766 | K->u.kernel.TYPE->usersize, \ | |
767 | C_SIZE, \ | |
768 | COMPAT_XT_ALIGN(C_SIZE)) | |
769 | ||
739674fb JE |
770 | int xt_compat_match_to_user(const struct xt_entry_match *m, |
771 | void __user **dstptr, unsigned int *size) | |
9fa492cd | 772 | { |
5452e425 | 773 | const struct xt_match *match = m->u.kernel.match; |
9fa492cd PM |
774 | struct compat_xt_entry_match __user *cm = *dstptr; |
775 | int off = xt_compat_match_offset(match); | |
776 | u_int16_t msize = m->u.user.match_size - off; | |
777 | ||
4915f7bb | 778 | if (XT_OBJ_TO_USER(cm, m, match, msize)) |
601e68e1 | 779 | return -EFAULT; |
9fa492cd PM |
780 | |
781 | if (match->compat_to_user) { | |
782 | if (match->compat_to_user((void __user *)cm->data, m->data)) | |
783 | return -EFAULT; | |
784 | } else { | |
751a9c76 | 785 | if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm))) |
9fa492cd | 786 | return -EFAULT; |
2722971c | 787 | } |
9fa492cd PM |
788 | |
789 | *size -= off; | |
790 | *dstptr += msize; | |
791 | return 0; | |
2722971c | 792 | } |
9fa492cd | 793 | EXPORT_SYMBOL_GPL(xt_compat_match_to_user); |
fc1221b3 | 794 | |
7ed2abdd FW |
795 | /* non-compat version may have padding after verdict */ |
796 | struct compat_xt_standard_target { | |
797 | struct compat_xt_entry_target t; | |
798 | compat_uint_t verdict; | |
799 | }; | |
800 | ||
472ebdcd FW |
801 | struct compat_xt_error_target { |
802 | struct compat_xt_entry_target t; | |
803 | char errorname[XT_FUNCTION_MAXNAMELEN]; | |
804 | }; | |
805 | ||
ce683e5f | 806 | int xt_compat_check_entry_offsets(const void *base, const char *elems, |
fc1221b3 FW |
807 | unsigned int target_offset, |
808 | unsigned int next_offset) | |
809 | { | |
ce683e5f | 810 | long size_of_base_struct = elems - (const char *)base; |
fc1221b3 FW |
811 | const struct compat_xt_entry_target *t; |
812 | const char *e = base; | |
813 | ||
ce683e5f FW |
814 | if (target_offset < size_of_base_struct) |
815 | return -EINVAL; | |
816 | ||
fc1221b3 FW |
817 | if (target_offset + sizeof(*t) > next_offset) |
818 | return -EINVAL; | |
819 | ||
820 | t = (void *)(e + target_offset); | |
821 | if (t->u.target_size < sizeof(*t)) | |
822 | return -EINVAL; | |
823 | ||
824 | if (target_offset + t->u.target_size > next_offset) | |
825 | return -EINVAL; | |
826 | ||
07a9da51 FW |
827 | if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) { |
828 | const struct compat_xt_standard_target *st = (const void *)t; | |
829 | ||
830 | if (COMPAT_XT_ALIGN(target_offset + sizeof(*st)) != next_offset) | |
831 | return -EINVAL; | |
832 | ||
833 | if (!verdict_ok(st->verdict)) | |
834 | return -EINVAL; | |
472ebdcd FW |
835 | } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) { |
836 | const struct compat_xt_error_target *et = (const void *)t; | |
837 | ||
838 | if (!error_tg_ok(t->u.target_size, sizeof(*et), | |
839 | et->errorname, sizeof(et->errorname))) | |
840 | return -EINVAL; | |
07a9da51 | 841 | } |
7ed2abdd | 842 | |
550116d2 | 843 | /* compat_xt_entry match has less strict alignment requirements, |
13631bfc FW |
844 | * otherwise they are identical. In case of padding differences |
845 | * we need to add compat version of xt_check_entry_match. | |
846 | */ | |
847 | BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match)); | |
848 | ||
849 | return xt_check_entry_match(elems, base + target_offset, | |
850 | __alignof__(struct compat_xt_entry_match)); | |
fc1221b3 FW |
851 | } |
852 | EXPORT_SYMBOL(xt_compat_check_entry_offsets); | |
9fa492cd | 853 | #endif /* CONFIG_COMPAT */ |
2722971c | 854 | |
7d35812c FW |
855 | /** |
856 | * xt_check_entry_offsets - validate arp/ip/ip6t_entry | |
857 | * | |
858 | * @base: pointer to arp/ip/ip6t_entry | |
ce683e5f | 859 | * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems |
7d35812c FW |
860 | * @target_offset: the arp/ip/ip6_t->target_offset |
861 | * @next_offset: the arp/ip/ip6_t->next_offset | |
862 | * | |
13631bfc FW |
863 | * validates that target_offset and next_offset are sane and that all |
864 | * match sizes (if any) align with the target offset. | |
7d35812c | 865 | * |
ce683e5f | 866 | * This function does not validate the targets or matches themselves, it |
13631bfc FW |
867 | * only tests that all the offsets and sizes are correct, that all |
868 | * match structures are aligned, and that the last structure ends where | |
869 | * the target structure begins. | |
870 | * | |
871 | * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version. | |
ce683e5f | 872 | * |
7d35812c FW |
873 | * The arp/ip/ip6t_entry structure @base must have passed following tests: |
874 | * - it must point to a valid memory location | |
875 | * - base to base + next_offset must be accessible, i.e. not exceed allocated | |
876 | * length. | |
877 | * | |
13631bfc FW |
878 | * A well-formed entry looks like this: |
879 | * | |
880 | * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry | |
881 | * e->elems[]-----' | | | |
882 | * matchsize | | | |
883 | * matchsize | | | |
884 | * | | | |
885 | * target_offset---------------------------------' | | |
886 | * next_offset---------------------------------------------------' | |
887 | * | |
888 | * elems[]: flexible array member at end of ip(6)/arpt_entry struct. | |
889 | * This is where matches (if any) and the target reside. | |
890 | * target_offset: beginning of target. | |
891 | * next_offset: start of the next rule; also: size of this rule. | |
892 | * Since targets have a minimum size, target_offset + minlen <= next_offset. | |
893 | * | |
894 | * Every match stores its size, sum of sizes must not exceed target_offset. | |
895 | * | |
7d35812c FW |
896 | * Return: 0 on success, negative errno on failure. |
897 | */ | |
898 | int xt_check_entry_offsets(const void *base, | |
ce683e5f | 899 | const char *elems, |
7d35812c FW |
900 | unsigned int target_offset, |
901 | unsigned int next_offset) | |
902 | { | |
ce683e5f | 903 | long size_of_base_struct = elems - (const char *)base; |
7d35812c FW |
904 | const struct xt_entry_target *t; |
905 | const char *e = base; | |
906 | ||
ce683e5f FW |
907 | /* target start is within the ip/ip6/arpt_entry struct */ |
908 | if (target_offset < size_of_base_struct) | |
909 | return -EINVAL; | |
910 | ||
7d35812c FW |
911 | if (target_offset + sizeof(*t) > next_offset) |
912 | return -EINVAL; | |
913 | ||
914 | t = (void *)(e + target_offset); | |
a08e4e19 FW |
915 | if (t->u.target_size < sizeof(*t)) |
916 | return -EINVAL; | |
917 | ||
7d35812c FW |
918 | if (target_offset + t->u.target_size > next_offset) |
919 | return -EINVAL; | |
920 | ||
07a9da51 FW |
921 | if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) { |
922 | const struct xt_standard_target *st = (const void *)t; | |
923 | ||
924 | if (XT_ALIGN(target_offset + sizeof(*st)) != next_offset) | |
925 | return -EINVAL; | |
926 | ||
927 | if (!verdict_ok(st->verdict)) | |
928 | return -EINVAL; | |
472ebdcd FW |
929 | } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) { |
930 | const struct xt_error_target *et = (const void *)t; | |
931 | ||
932 | if (!error_tg_ok(t->u.target_size, sizeof(*et), | |
933 | et->errorname, sizeof(et->errorname))) | |
934 | return -EINVAL; | |
07a9da51 | 935 | } |
7ed2abdd | 936 | |
13631bfc FW |
937 | return xt_check_entry_match(elems, base + target_offset, |
938 | __alignof__(struct xt_entry_match)); | |
7d35812c FW |
939 | } |
940 | EXPORT_SYMBOL(xt_check_entry_offsets); | |
941 | ||
f4dc7771 FW |
942 | /** |
943 | * xt_alloc_entry_offsets - allocate array to store rule head offsets | |
944 | * | |
945 | * @size: number of entries | |
946 | * | |
947 | * Return: NULL or kmalloc'd or vmalloc'd array | |
948 | */ | |
949 | unsigned int *xt_alloc_entry_offsets(unsigned int size) | |
950 | { | |
9d5c12a7 FW |
951 | if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int)) |
952 | return NULL; | |
953 | ||
752ade68 | 954 | return kvmalloc_array(size, sizeof(unsigned int), GFP_KERNEL | __GFP_ZERO); |
f4dc7771 | 955 | |
f4dc7771 FW |
956 | } |
957 | EXPORT_SYMBOL(xt_alloc_entry_offsets); | |
958 | ||
959 | /** | |
960 | * xt_find_jump_offset - check if target is a valid jump offset | |
961 | * | |
962 | * @offsets: array containing all valid rule start offsets of a rule blob | |
963 | * @target: the jump target to search for | |
964 | * @size: entries in @offset | |
965 | */ | |
966 | bool xt_find_jump_offset(const unsigned int *offsets, | |
967 | unsigned int target, unsigned int size) | |
968 | { | |
969 | int m, low = 0, hi = size; | |
970 | ||
971 | while (hi > low) { | |
972 | m = (low + hi) / 2u; | |
973 | ||
974 | if (offsets[m] > target) | |
975 | hi = m; | |
976 | else if (offsets[m] < target) | |
977 | low = m + 1; | |
978 | else | |
979 | return true; | |
980 | } | |
981 | ||
982 | return false; | |
983 | } | |
984 | EXPORT_SYMBOL(xt_find_jump_offset); | |
985 | ||
916a917d | 986 | int xt_check_target(struct xt_tgchk_param *par, |
af5d6dc2 | 987 | unsigned int size, u_int8_t proto, bool inv_proto) |
37f9f733 | 988 | { |
d6b00a53 JE |
989 | int ret; |
990 | ||
af5d6dc2 | 991 | if (XT_ALIGN(par->target->targetsize) != size) { |
1b6cd671 FW |
992 | pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n", |
993 | xt_prefix[par->family], par->target->name, | |
994 | par->target->revision, | |
995 | XT_ALIGN(par->target->targetsize), size); | |
37f9f733 PM |
996 | return -EINVAL; |
997 | } | |
af5d6dc2 JE |
998 | if (par->target->table != NULL && |
999 | strcmp(par->target->table, par->table) != 0) { | |
1b6cd671 FW |
1000 | pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n", |
1001 | xt_prefix[par->family], par->target->name, | |
1002 | par->target->table, par->table); | |
37f9f733 PM |
1003 | return -EINVAL; |
1004 | } | |
af5d6dc2 | 1005 | if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) { |
45185364 JE |
1006 | char used[64], allow[64]; |
1007 | ||
1b6cd671 FW |
1008 | pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n", |
1009 | xt_prefix[par->family], par->target->name, | |
1010 | textify_hooks(used, sizeof(used), | |
1011 | par->hook_mask, par->family), | |
1012 | textify_hooks(allow, sizeof(allow), | |
1013 | par->target->hooks, | |
1014 | par->family)); | |
37f9f733 PM |
1015 | return -EINVAL; |
1016 | } | |
af5d6dc2 | 1017 | if (par->target->proto && (par->target->proto != proto || inv_proto)) { |
1b6cd671 FW |
1018 | pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n", |
1019 | xt_prefix[par->family], par->target->name, | |
1020 | par->target->proto); | |
37f9f733 PM |
1021 | return -EINVAL; |
1022 | } | |
d6b00a53 JE |
1023 | if (par->target->checkentry != NULL) { |
1024 | ret = par->target->checkentry(par); | |
1025 | if (ret < 0) | |
1026 | return ret; | |
1027 | else if (ret > 0) | |
1028 | /* Flag up potential errors. */ | |
1029 | return -EIO; | |
1030 | } | |
37f9f733 PM |
1031 | return 0; |
1032 | } | |
1033 | EXPORT_SYMBOL_GPL(xt_check_target); | |
1034 | ||
d7591f0c FW |
1035 | /** |
1036 | * xt_copy_counters_from_user - copy counters and metadata from userspace | |
1037 | * | |
1038 | * @user: src pointer to userspace memory | |
1039 | * @len: alleged size of userspace memory | |
1040 | * @info: where to store the xt_counters_info metadata | |
1041 | * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel | |
1042 | * | |
1043 | * Copies counter meta data from @user and stores it in @info. | |
1044 | * | |
1045 | * vmallocs memory to hold the counters, then copies the counter data | |
1046 | * from @user to the new memory and returns a pointer to it. | |
1047 | * | |
1048 | * If @compat is true, @info gets converted automatically to the 64bit | |
1049 | * representation. | |
1050 | * | |
1051 | * The metadata associated with the counters is stored in @info. | |
1052 | * | |
1053 | * Return: returns pointer that caller has to test via IS_ERR(). | |
1054 | * If IS_ERR is false, caller has to vfree the pointer. | |
1055 | */ | |
1056 | void *xt_copy_counters_from_user(const void __user *user, unsigned int len, | |
1057 | struct xt_counters_info *info, bool compat) | |
1058 | { | |
1059 | void *mem; | |
1060 | u64 size; | |
1061 | ||
1062 | #ifdef CONFIG_COMPAT | |
1063 | if (compat) { | |
1064 | /* structures only differ in size due to alignment */ | |
1065 | struct compat_xt_counters_info compat_tmp; | |
1066 | ||
1067 | if (len <= sizeof(compat_tmp)) | |
1068 | return ERR_PTR(-EINVAL); | |
1069 | ||
1070 | len -= sizeof(compat_tmp); | |
1071 | if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0) | |
1072 | return ERR_PTR(-EFAULT); | |
1073 | ||
e466af75 | 1074 | memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1); |
d7591f0c FW |
1075 | info->num_counters = compat_tmp.num_counters; |
1076 | user += sizeof(compat_tmp); | |
1077 | } else | |
1078 | #endif | |
1079 | { | |
1080 | if (len <= sizeof(*info)) | |
1081 | return ERR_PTR(-EINVAL); | |
1082 | ||
1083 | len -= sizeof(*info); | |
1084 | if (copy_from_user(info, user, sizeof(*info)) != 0) | |
1085 | return ERR_PTR(-EFAULT); | |
1086 | ||
d7591f0c FW |
1087 | user += sizeof(*info); |
1088 | } | |
e466af75 | 1089 | info->name[sizeof(info->name) - 1] = '\0'; |
d7591f0c FW |
1090 | |
1091 | size = sizeof(struct xt_counters); | |
1092 | size *= info->num_counters; | |
1093 | ||
1094 | if (size != (u64)len) | |
1095 | return ERR_PTR(-EINVAL); | |
1096 | ||
1097 | mem = vmalloc(len); | |
1098 | if (!mem) | |
1099 | return ERR_PTR(-ENOMEM); | |
1100 | ||
1101 | if (copy_from_user(mem, user, len) == 0) | |
1102 | return mem; | |
1103 | ||
1104 | vfree(mem); | |
1105 | return ERR_PTR(-EFAULT); | |
1106 | } | |
1107 | EXPORT_SYMBOL_GPL(xt_copy_counters_from_user); | |
1108 | ||
2722971c | 1109 | #ifdef CONFIG_COMPAT |
5452e425 | 1110 | int xt_compat_target_offset(const struct xt_target *target) |
2722971c | 1111 | { |
9fa492cd PM |
1112 | u_int16_t csize = target->compatsize ? : target->targetsize; |
1113 | return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize); | |
1114 | } | |
1115 | EXPORT_SYMBOL_GPL(xt_compat_target_offset); | |
1116 | ||
1117 | void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr, | |
b0a6363c | 1118 | unsigned int *size) |
9fa492cd | 1119 | { |
5452e425 | 1120 | const struct xt_target *target = t->u.kernel.target; |
9fa492cd PM |
1121 | struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t; |
1122 | int pad, off = xt_compat_target_offset(target); | |
1123 | u_int16_t tsize = ct->u.user.target_size; | |
09d96860 | 1124 | char name[sizeof(t->u.user.name)]; |
9fa492cd PM |
1125 | |
1126 | t = *dstptr; | |
1127 | memcpy(t, ct, sizeof(*ct)); | |
1128 | if (target->compat_from_user) | |
1129 | target->compat_from_user(t->data, ct->data); | |
1130 | else | |
1131 | memcpy(t->data, ct->data, tsize - sizeof(*ct)); | |
1132 | pad = XT_ALIGN(target->targetsize) - target->targetsize; | |
1133 | if (pad > 0) | |
1134 | memset(t->data + target->targetsize, 0, pad); | |
1135 | ||
1136 | tsize += off; | |
1137 | t->u.user.target_size = tsize; | |
09d96860 FW |
1138 | strlcpy(name, target->name, sizeof(name)); |
1139 | module_put(target->me); | |
1140 | strncpy(t->u.user.name, name, sizeof(t->u.user.name)); | |
9fa492cd PM |
1141 | |
1142 | *size += off; | |
1143 | *dstptr += tsize; | |
1144 | } | |
1145 | EXPORT_SYMBOL_GPL(xt_compat_target_from_user); | |
1146 | ||
739674fb JE |
1147 | int xt_compat_target_to_user(const struct xt_entry_target *t, |
1148 | void __user **dstptr, unsigned int *size) | |
9fa492cd | 1149 | { |
5452e425 | 1150 | const struct xt_target *target = t->u.kernel.target; |
9fa492cd PM |
1151 | struct compat_xt_entry_target __user *ct = *dstptr; |
1152 | int off = xt_compat_target_offset(target); | |
1153 | u_int16_t tsize = t->u.user.target_size - off; | |
1154 | ||
4915f7bb | 1155 | if (XT_OBJ_TO_USER(ct, t, target, tsize)) |
601e68e1 | 1156 | return -EFAULT; |
9fa492cd PM |
1157 | |
1158 | if (target->compat_to_user) { | |
1159 | if (target->compat_to_user((void __user *)ct->data, t->data)) | |
1160 | return -EFAULT; | |
1161 | } else { | |
751a9c76 | 1162 | if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct))) |
9fa492cd | 1163 | return -EFAULT; |
2722971c | 1164 | } |
9fa492cd PM |
1165 | |
1166 | *size -= off; | |
1167 | *dstptr += tsize; | |
1168 | return 0; | |
2722971c | 1169 | } |
9fa492cd | 1170 | EXPORT_SYMBOL_GPL(xt_compat_target_to_user); |
2722971c DM |
1171 | #endif |
1172 | ||
2e4e6a17 HW |
1173 | struct xt_table_info *xt_alloc_table_info(unsigned int size) |
1174 | { | |
711bdde6 ED |
1175 | struct xt_table_info *info = NULL; |
1176 | size_t sz = sizeof(*info) + size; | |
2e4e6a17 | 1177 | |
19926968 | 1178 | if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE) |
d157bd76 FW |
1179 | return NULL; |
1180 | ||
a148ce15 | 1181 | info = kvmalloc(sz, GFP_KERNEL_ACCOUNT); |
eacd86ca MH |
1182 | if (!info) |
1183 | return NULL; | |
1184 | ||
711bdde6 ED |
1185 | memset(info, 0, sizeof(*info)); |
1186 | info->size = size; | |
1187 | return info; | |
2e4e6a17 HW |
1188 | } |
1189 | EXPORT_SYMBOL(xt_alloc_table_info); | |
1190 | ||
1191 | void xt_free_table_info(struct xt_table_info *info) | |
1192 | { | |
1193 | int cpu; | |
1194 | ||
f3c5c1bf | 1195 | if (info->jumpstack != NULL) { |
f6b50824 ED |
1196 | for_each_possible_cpu(cpu) |
1197 | kvfree(info->jumpstack[cpu]); | |
1198 | kvfree(info->jumpstack); | |
f3c5c1bf JE |
1199 | } |
1200 | ||
711bdde6 | 1201 | kvfree(info); |
2e4e6a17 HW |
1202 | } |
1203 | EXPORT_SYMBOL(xt_free_table_info); | |
1204 | ||
03d13b68 | 1205 | /* Find table by name, grabs mutex & ref. Returns ERR_PTR on error. */ |
76108cea JE |
1206 | struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af, |
1207 | const char *name) | |
2e4e6a17 | 1208 | { |
b9e69e12 | 1209 | struct xt_table *t, *found = NULL; |
2e4e6a17 | 1210 | |
7926dbfa | 1211 | mutex_lock(&xt[af].mutex); |
8d870052 | 1212 | list_for_each_entry(t, &net->xt.tables[af], list) |
2e4e6a17 HW |
1213 | if (strcmp(t->name, name) == 0 && try_module_get(t->me)) |
1214 | return t; | |
b9e69e12 FW |
1215 | |
1216 | if (net == &init_net) | |
1217 | goto out; | |
1218 | ||
1219 | /* Table doesn't exist in this netns, re-try init */ | |
1220 | list_for_each_entry(t, &init_net.xt.tables[af], list) { | |
03d13b68 FW |
1221 | int err; |
1222 | ||
b9e69e12 FW |
1223 | if (strcmp(t->name, name)) |
1224 | continue; | |
03d13b68 FW |
1225 | if (!try_module_get(t->me)) |
1226 | goto out; | |
b9e69e12 | 1227 | mutex_unlock(&xt[af].mutex); |
03d13b68 FW |
1228 | err = t->table_init(net); |
1229 | if (err < 0) { | |
b9e69e12 | 1230 | module_put(t->me); |
03d13b68 | 1231 | return ERR_PTR(err); |
b9e69e12 FW |
1232 | } |
1233 | ||
1234 | found = t; | |
1235 | ||
1236 | mutex_lock(&xt[af].mutex); | |
1237 | break; | |
1238 | } | |
1239 | ||
1240 | if (!found) | |
1241 | goto out; | |
1242 | ||
1243 | /* and once again: */ | |
1244 | list_for_each_entry(t, &net->xt.tables[af], list) | |
1245 | if (strcmp(t->name, name) == 0) | |
1246 | return t; | |
1247 | ||
1248 | module_put(found->me); | |
1249 | out: | |
9e19bb6d | 1250 | mutex_unlock(&xt[af].mutex); |
03d13b68 | 1251 | return ERR_PTR(-ENOENT); |
2e4e6a17 HW |
1252 | } |
1253 | EXPORT_SYMBOL_GPL(xt_find_table_lock); | |
1254 | ||
03d13b68 FW |
1255 | struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af, |
1256 | const char *name) | |
1257 | { | |
1258 | struct xt_table *t = xt_find_table_lock(net, af, name); | |
1259 | ||
20651cef | 1260 | #ifdef CONFIG_MODULES |
03d13b68 FW |
1261 | if (IS_ERR(t)) { |
1262 | int err = request_module("%stable_%s", xt_prefix[af], name); | |
e3eeacba | 1263 | if (err < 0) |
03d13b68 FW |
1264 | return ERR_PTR(err); |
1265 | t = xt_find_table_lock(net, af, name); | |
1266 | } | |
1267 | #endif | |
1268 | ||
1269 | return t; | |
1270 | } | |
1271 | EXPORT_SYMBOL_GPL(xt_request_find_table_lock); | |
1272 | ||
2e4e6a17 HW |
1273 | void xt_table_unlock(struct xt_table *table) |
1274 | { | |
9e19bb6d | 1275 | mutex_unlock(&xt[table->af].mutex); |
2e4e6a17 HW |
1276 | } |
1277 | EXPORT_SYMBOL_GPL(xt_table_unlock); | |
1278 | ||
2722971c | 1279 | #ifdef CONFIG_COMPAT |
76108cea | 1280 | void xt_compat_lock(u_int8_t af) |
2722971c DM |
1281 | { |
1282 | mutex_lock(&xt[af].compat_mutex); | |
1283 | } | |
1284 | EXPORT_SYMBOL_GPL(xt_compat_lock); | |
1285 | ||
76108cea | 1286 | void xt_compat_unlock(u_int8_t af) |
2722971c DM |
1287 | { |
1288 | mutex_unlock(&xt[af].compat_mutex); | |
1289 | } | |
1290 | EXPORT_SYMBOL_GPL(xt_compat_unlock); | |
1291 | #endif | |
2e4e6a17 | 1292 | |
7f5c6d4f ED |
1293 | DEFINE_PER_CPU(seqcount_t, xt_recseq); |
1294 | EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq); | |
942e4a2b | 1295 | |
dcebd315 FW |
1296 | struct static_key xt_tee_enabled __read_mostly; |
1297 | EXPORT_SYMBOL_GPL(xt_tee_enabled); | |
1298 | ||
f3c5c1bf JE |
1299 | static int xt_jumpstack_alloc(struct xt_table_info *i) |
1300 | { | |
1301 | unsigned int size; | |
1302 | int cpu; | |
1303 | ||
f3c5c1bf JE |
1304 | size = sizeof(void **) * nr_cpu_ids; |
1305 | if (size > PAGE_SIZE) | |
752ade68 | 1306 | i->jumpstack = kvzalloc(size, GFP_KERNEL); |
f3c5c1bf | 1307 | else |
3dbd4439 | 1308 | i->jumpstack = kzalloc(size, GFP_KERNEL); |
f3c5c1bf JE |
1309 | if (i->jumpstack == NULL) |
1310 | return -ENOMEM; | |
f3c5c1bf | 1311 | |
98d1bd80 FW |
1312 | /* ruleset without jumps -- no stack needed */ |
1313 | if (i->stacksize == 0) | |
1314 | return 0; | |
1315 | ||
7814b6ec FW |
1316 | /* Jumpstack needs to be able to record two full callchains, one |
1317 | * from the first rule set traversal, plus one table reentrancy | |
1318 | * via -j TEE without clobbering the callchain that brought us to | |
1319 | * TEE target. | |
1320 | * | |
1321 | * This is done by allocating two jumpstacks per cpu, on reentry | |
1322 | * the upper half of the stack is used. | |
1323 | * | |
1324 | * see the jumpstack setup in ipt_do_table() for more details. | |
1325 | */ | |
1326 | size = sizeof(void *) * i->stacksize * 2u; | |
f3c5c1bf | 1327 | for_each_possible_cpu(cpu) { |
752ade68 MH |
1328 | i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL, |
1329 | cpu_to_node(cpu)); | |
f3c5c1bf JE |
1330 | if (i->jumpstack[cpu] == NULL) |
1331 | /* | |
1332 | * Freeing will be done later on by the callers. The | |
1333 | * chain is: xt_replace_table -> __do_replace -> | |
1334 | * do_replace -> xt_free_table_info. | |
1335 | */ | |
1336 | return -ENOMEM; | |
1337 | } | |
1338 | ||
1339 | return 0; | |
1340 | } | |
942e4a2b | 1341 | |
c84ca954 FW |
1342 | struct xt_counters *xt_counters_alloc(unsigned int counters) |
1343 | { | |
1344 | struct xt_counters *mem; | |
1345 | ||
1346 | if (counters == 0 || counters > INT_MAX / sizeof(*mem)) | |
1347 | return NULL; | |
1348 | ||
1349 | counters *= sizeof(*mem); | |
1350 | if (counters > XT_MAX_TABLE_SIZE) | |
1351 | return NULL; | |
1352 | ||
1353 | return vzalloc(counters); | |
1354 | } | |
1355 | EXPORT_SYMBOL(xt_counters_alloc); | |
1356 | ||
2e4e6a17 HW |
1357 | struct xt_table_info * |
1358 | xt_replace_table(struct xt_table *table, | |
1359 | unsigned int num_counters, | |
1360 | struct xt_table_info *newinfo, | |
1361 | int *error) | |
1362 | { | |
942e4a2b | 1363 | struct xt_table_info *private; |
80055dab | 1364 | unsigned int cpu; |
f3c5c1bf | 1365 | int ret; |
2e4e6a17 | 1366 | |
d97a9e47 JE |
1367 | ret = xt_jumpstack_alloc(newinfo); |
1368 | if (ret < 0) { | |
1369 | *error = ret; | |
1370 | return NULL; | |
1371 | } | |
1372 | ||
2e4e6a17 | 1373 | /* Do the substitution. */ |
942e4a2b | 1374 | local_bh_disable(); |
2e4e6a17 | 1375 | private = table->private; |
942e4a2b | 1376 | |
2e4e6a17 HW |
1377 | /* Check inside lock: is the old number correct? */ |
1378 | if (num_counters != private->number) { | |
be91fd5e | 1379 | pr_debug("num_counters != table->private->number (%u/%u)\n", |
2e4e6a17 | 1380 | num_counters, private->number); |
942e4a2b | 1381 | local_bh_enable(); |
2e4e6a17 HW |
1382 | *error = -EAGAIN; |
1383 | return NULL; | |
1384 | } | |
2e4e6a17 | 1385 | |
942e4a2b | 1386 | newinfo->initial_entries = private->initial_entries; |
b416c144 WD |
1387 | /* |
1388 | * Ensure contents of newinfo are visible before assigning to | |
1389 | * private. | |
1390 | */ | |
1391 | smp_wmb(); | |
1392 | table->private = newinfo; | |
942e4a2b | 1393 | |
80055dab FW |
1394 | /* make sure all cpus see new ->private value */ |
1395 | smp_wmb(); | |
1396 | ||
942e4a2b SH |
1397 | /* |
1398 | * Even though table entries have now been swapped, other CPU's | |
80055dab | 1399 | * may still be using the old entries... |
942e4a2b SH |
1400 | */ |
1401 | local_bh_enable(); | |
1402 | ||
80055dab FW |
1403 | /* ... so wait for even xt_recseq on all cpus */ |
1404 | for_each_possible_cpu(cpu) { | |
1405 | seqcount_t *s = &per_cpu(xt_recseq, cpu); | |
1406 | u32 seq = raw_read_seqcount(s); | |
1407 | ||
1408 | if (seq & 1) { | |
1409 | do { | |
1410 | cond_resched(); | |
1411 | cpu_relax(); | |
1412 | } while (seq == raw_read_seqcount(s)); | |
1413 | } | |
1414 | } | |
1415 | ||
fbabf31e TG |
1416 | #ifdef CONFIG_AUDIT |
1417 | if (audit_enabled) { | |
cdfb6b34 | 1418 | audit_log(audit_context(), GFP_KERNEL, |
46b20c38 GT |
1419 | AUDIT_NETFILTER_CFG, |
1420 | "table=%s family=%u entries=%u", | |
1421 | table->name, table->af, private->number); | |
fbabf31e TG |
1422 | } |
1423 | #endif | |
1424 | ||
942e4a2b | 1425 | return private; |
2e4e6a17 HW |
1426 | } |
1427 | EXPORT_SYMBOL_GPL(xt_replace_table); | |
1428 | ||
35aad0ff JE |
1429 | struct xt_table *xt_register_table(struct net *net, |
1430 | const struct xt_table *input_table, | |
a98da11d AD |
1431 | struct xt_table_info *bootstrap, |
1432 | struct xt_table_info *newinfo) | |
2e4e6a17 HW |
1433 | { |
1434 | int ret; | |
1435 | struct xt_table_info *private; | |
35aad0ff | 1436 | struct xt_table *t, *table; |
2e4e6a17 | 1437 | |
44d34e72 | 1438 | /* Don't add one object to multiple lists. */ |
35aad0ff | 1439 | table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL); |
44d34e72 AD |
1440 | if (!table) { |
1441 | ret = -ENOMEM; | |
1442 | goto out; | |
1443 | } | |
1444 | ||
7926dbfa | 1445 | mutex_lock(&xt[table->af].mutex); |
2e4e6a17 | 1446 | /* Don't autoload: we'd eat our tail... */ |
8d870052 | 1447 | list_for_each_entry(t, &net->xt.tables[table->af], list) { |
df0933dc PM |
1448 | if (strcmp(t->name, table->name) == 0) { |
1449 | ret = -EEXIST; | |
1450 | goto unlock; | |
1451 | } | |
2e4e6a17 HW |
1452 | } |
1453 | ||
1454 | /* Simplifies replace_table code. */ | |
1455 | table->private = bootstrap; | |
78454473 | 1456 | |
2e4e6a17 HW |
1457 | if (!xt_replace_table(table, 0, newinfo, &ret)) |
1458 | goto unlock; | |
1459 | ||
1460 | private = table->private; | |
be91fd5e | 1461 | pr_debug("table->private->number = %u\n", private->number); |
2e4e6a17 HW |
1462 | |
1463 | /* save number of initial entries */ | |
1464 | private->initial_entries = private->number; | |
1465 | ||
8d870052 | 1466 | list_add(&table->list, &net->xt.tables[table->af]); |
a98da11d AD |
1467 | mutex_unlock(&xt[table->af].mutex); |
1468 | return table; | |
2e4e6a17 | 1469 | |
7926dbfa | 1470 | unlock: |
9e19bb6d | 1471 | mutex_unlock(&xt[table->af].mutex); |
44d34e72 | 1472 | kfree(table); |
a98da11d AD |
1473 | out: |
1474 | return ERR_PTR(ret); | |
2e4e6a17 HW |
1475 | } |
1476 | EXPORT_SYMBOL_GPL(xt_register_table); | |
1477 | ||
1478 | void *xt_unregister_table(struct xt_table *table) | |
1479 | { | |
1480 | struct xt_table_info *private; | |
1481 | ||
9e19bb6d | 1482 | mutex_lock(&xt[table->af].mutex); |
2e4e6a17 | 1483 | private = table->private; |
df0933dc | 1484 | list_del(&table->list); |
9e19bb6d | 1485 | mutex_unlock(&xt[table->af].mutex); |
44d34e72 | 1486 | kfree(table); |
2e4e6a17 HW |
1487 | |
1488 | return private; | |
1489 | } | |
1490 | EXPORT_SYMBOL_GPL(xt_unregister_table); | |
1491 | ||
1492 | #ifdef CONFIG_PROC_FS | |
025d93d1 | 1493 | static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos) |
2e4e6a17 | 1494 | { |
1218854a | 1495 | struct net *net = seq_file_net(seq); |
1d98c16d | 1496 | u_int8_t af = (unsigned long)PDE_DATA(file_inode(seq->file)); |
2e4e6a17 | 1497 | |
025d93d1 | 1498 | mutex_lock(&xt[af].mutex); |
715cf35a | 1499 | return seq_list_start(&net->xt.tables[af], *pos); |
025d93d1 | 1500 | } |
2e4e6a17 | 1501 | |
025d93d1 AD |
1502 | static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
1503 | { | |
1218854a | 1504 | struct net *net = seq_file_net(seq); |
1d98c16d | 1505 | u_int8_t af = (unsigned long)PDE_DATA(file_inode(seq->file)); |
2e4e6a17 | 1506 | |
715cf35a | 1507 | return seq_list_next(v, &net->xt.tables[af], pos); |
2e4e6a17 HW |
1508 | } |
1509 | ||
025d93d1 | 1510 | static void xt_table_seq_stop(struct seq_file *seq, void *v) |
2e4e6a17 | 1511 | { |
1d98c16d | 1512 | u_int8_t af = (unsigned long)PDE_DATA(file_inode(seq->file)); |
2e4e6a17 | 1513 | |
025d93d1 AD |
1514 | mutex_unlock(&xt[af].mutex); |
1515 | } | |
2e4e6a17 | 1516 | |
025d93d1 AD |
1517 | static int xt_table_seq_show(struct seq_file *seq, void *v) |
1518 | { | |
1519 | struct xt_table *table = list_entry(v, struct xt_table, list); | |
2e4e6a17 | 1520 | |
861fb107 | 1521 | if (*table->name) |
e71456ae | 1522 | seq_printf(seq, "%s\n", table->name); |
861fb107 | 1523 | return 0; |
025d93d1 | 1524 | } |
601e68e1 | 1525 | |
025d93d1 AD |
1526 | static const struct seq_operations xt_table_seq_ops = { |
1527 | .start = xt_table_seq_start, | |
1528 | .next = xt_table_seq_next, | |
1529 | .stop = xt_table_seq_stop, | |
1530 | .show = xt_table_seq_show, | |
1531 | }; | |
1532 | ||
eb132205 JE |
1533 | /* |
1534 | * Traverse state for ip{,6}_{tables,matches} for helping crossing | |
1535 | * the multi-AF mutexes. | |
1536 | */ | |
1537 | struct nf_mttg_trav { | |
1538 | struct list_head *head, *curr; | |
1d98c16d | 1539 | uint8_t class; |
eb132205 JE |
1540 | }; |
1541 | ||
1542 | enum { | |
1543 | MTTG_TRAV_INIT, | |
1544 | MTTG_TRAV_NFP_UNSPEC, | |
1545 | MTTG_TRAV_NFP_SPEC, | |
1546 | MTTG_TRAV_DONE, | |
1547 | }; | |
1548 | ||
1549 | static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos, | |
1550 | bool is_target) | |
2e4e6a17 | 1551 | { |
eb132205 JE |
1552 | static const uint8_t next_class[] = { |
1553 | [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC, | |
1554 | [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE, | |
1555 | }; | |
1d98c16d | 1556 | uint8_t nfproto = (unsigned long)PDE_DATA(file_inode(seq->file)); |
eb132205 JE |
1557 | struct nf_mttg_trav *trav = seq->private; |
1558 | ||
1559 | switch (trav->class) { | |
1560 | case MTTG_TRAV_INIT: | |
1561 | trav->class = MTTG_TRAV_NFP_UNSPEC; | |
1562 | mutex_lock(&xt[NFPROTO_UNSPEC].mutex); | |
1563 | trav->head = trav->curr = is_target ? | |
1564 | &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match; | |
1565 | break; | |
1566 | case MTTG_TRAV_NFP_UNSPEC: | |
1567 | trav->curr = trav->curr->next; | |
1568 | if (trav->curr != trav->head) | |
1569 | break; | |
1570 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); | |
1d98c16d | 1571 | mutex_lock(&xt[nfproto].mutex); |
eb132205 | 1572 | trav->head = trav->curr = is_target ? |
1d98c16d | 1573 | &xt[nfproto].target : &xt[nfproto].match; |
eb132205 JE |
1574 | trav->class = next_class[trav->class]; |
1575 | break; | |
1576 | case MTTG_TRAV_NFP_SPEC: | |
1577 | trav->curr = trav->curr->next; | |
1578 | if (trav->curr != trav->head) | |
1579 | break; | |
e8542dce | 1580 | /* fall through */ |
eb132205 JE |
1581 | default: |
1582 | return NULL; | |
1583 | } | |
2e4e6a17 | 1584 | |
eb132205 JE |
1585 | if (ppos != NULL) |
1586 | ++*ppos; | |
1587 | return trav; | |
025d93d1 | 1588 | } |
601e68e1 | 1589 | |
eb132205 JE |
1590 | static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos, |
1591 | bool is_target) | |
025d93d1 | 1592 | { |
eb132205 JE |
1593 | struct nf_mttg_trav *trav = seq->private; |
1594 | unsigned int j; | |
2e4e6a17 | 1595 | |
eb132205 JE |
1596 | trav->class = MTTG_TRAV_INIT; |
1597 | for (j = 0; j < *pos; ++j) | |
1598 | if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL) | |
1599 | return NULL; | |
1600 | return trav; | |
2e4e6a17 HW |
1601 | } |
1602 | ||
eb132205 | 1603 | static void xt_mttg_seq_stop(struct seq_file *seq, void *v) |
2e4e6a17 | 1604 | { |
1d98c16d | 1605 | uint8_t nfproto = (unsigned long)PDE_DATA(file_inode(seq->file)); |
eb132205 JE |
1606 | struct nf_mttg_trav *trav = seq->private; |
1607 | ||
1608 | switch (trav->class) { | |
1609 | case MTTG_TRAV_NFP_UNSPEC: | |
1610 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); | |
1611 | break; | |
1612 | case MTTG_TRAV_NFP_SPEC: | |
1d98c16d | 1613 | mutex_unlock(&xt[nfproto].mutex); |
eb132205 JE |
1614 | break; |
1615 | } | |
1616 | } | |
2e4e6a17 | 1617 | |
eb132205 JE |
1618 | static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos) |
1619 | { | |
1620 | return xt_mttg_seq_start(seq, pos, false); | |
2e4e6a17 HW |
1621 | } |
1622 | ||
eb132205 | 1623 | static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos) |
2e4e6a17 | 1624 | { |
eb132205 JE |
1625 | return xt_mttg_seq_next(seq, v, ppos, false); |
1626 | } | |
2e4e6a17 | 1627 | |
eb132205 JE |
1628 | static int xt_match_seq_show(struct seq_file *seq, void *v) |
1629 | { | |
1630 | const struct nf_mttg_trav *trav = seq->private; | |
1631 | const struct xt_match *match; | |
1632 | ||
1633 | switch (trav->class) { | |
1634 | case MTTG_TRAV_NFP_UNSPEC: | |
1635 | case MTTG_TRAV_NFP_SPEC: | |
1636 | if (trav->curr == trav->head) | |
1637 | return 0; | |
1638 | match = list_entry(trav->curr, struct xt_match, list); | |
861fb107 JP |
1639 | if (*match->name) |
1640 | seq_printf(seq, "%s\n", match->name); | |
eb132205 JE |
1641 | } |
1642 | return 0; | |
2e4e6a17 HW |
1643 | } |
1644 | ||
025d93d1 AD |
1645 | static const struct seq_operations xt_match_seq_ops = { |
1646 | .start = xt_match_seq_start, | |
1647 | .next = xt_match_seq_next, | |
eb132205 | 1648 | .stop = xt_mttg_seq_stop, |
025d93d1 | 1649 | .show = xt_match_seq_show, |
2e4e6a17 HW |
1650 | }; |
1651 | ||
025d93d1 AD |
1652 | static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos) |
1653 | { | |
eb132205 | 1654 | return xt_mttg_seq_start(seq, pos, true); |
025d93d1 AD |
1655 | } |
1656 | ||
eb132205 | 1657 | static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos) |
025d93d1 | 1658 | { |
eb132205 | 1659 | return xt_mttg_seq_next(seq, v, ppos, true); |
025d93d1 AD |
1660 | } |
1661 | ||
1662 | static int xt_target_seq_show(struct seq_file *seq, void *v) | |
1663 | { | |
eb132205 JE |
1664 | const struct nf_mttg_trav *trav = seq->private; |
1665 | const struct xt_target *target; | |
1666 | ||
1667 | switch (trav->class) { | |
1668 | case MTTG_TRAV_NFP_UNSPEC: | |
1669 | case MTTG_TRAV_NFP_SPEC: | |
1670 | if (trav->curr == trav->head) | |
1671 | return 0; | |
1672 | target = list_entry(trav->curr, struct xt_target, list); | |
861fb107 JP |
1673 | if (*target->name) |
1674 | seq_printf(seq, "%s\n", target->name); | |
eb132205 JE |
1675 | } |
1676 | return 0; | |
025d93d1 AD |
1677 | } |
1678 | ||
1679 | static const struct seq_operations xt_target_seq_ops = { | |
1680 | .start = xt_target_seq_start, | |
1681 | .next = xt_target_seq_next, | |
eb132205 | 1682 | .stop = xt_mttg_seq_stop, |
025d93d1 AD |
1683 | .show = xt_target_seq_show, |
1684 | }; | |
1685 | ||
2e4e6a17 HW |
1686 | #define FORMAT_TABLES "_tables_names" |
1687 | #define FORMAT_MATCHES "_tables_matches" | |
1688 | #define FORMAT_TARGETS "_tables_targets" | |
1689 | ||
1690 | #endif /* CONFIG_PROC_FS */ | |
1691 | ||
2b95efe7 | 1692 | /** |
b9e69e12 | 1693 | * xt_hook_ops_alloc - set up hooks for a new table |
2b95efe7 JE |
1694 | * @table: table with metadata needed to set up hooks |
1695 | * @fn: Hook function | |
1696 | * | |
b9e69e12 FW |
1697 | * This function will create the nf_hook_ops that the x_table needs |
1698 | * to hand to xt_hook_link_net(). | |
2b95efe7 | 1699 | */ |
b9e69e12 FW |
1700 | struct nf_hook_ops * |
1701 | xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn) | |
2b95efe7 JE |
1702 | { |
1703 | unsigned int hook_mask = table->valid_hooks; | |
1704 | uint8_t i, num_hooks = hweight32(hook_mask); | |
1705 | uint8_t hooknum; | |
1706 | struct nf_hook_ops *ops; | |
2b95efe7 | 1707 | |
a6d0bae1 XL |
1708 | if (!num_hooks) |
1709 | return ERR_PTR(-EINVAL); | |
1710 | ||
1ecc281e | 1711 | ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL); |
2b95efe7 JE |
1712 | if (ops == NULL) |
1713 | return ERR_PTR(-ENOMEM); | |
1714 | ||
1715 | for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0; | |
1716 | hook_mask >>= 1, ++hooknum) { | |
1717 | if (!(hook_mask & 1)) | |
1718 | continue; | |
1719 | ops[i].hook = fn; | |
2b95efe7 JE |
1720 | ops[i].pf = table->af; |
1721 | ops[i].hooknum = hooknum; | |
1722 | ops[i].priority = table->priority; | |
1723 | ++i; | |
1724 | } | |
1725 | ||
2b95efe7 JE |
1726 | return ops; |
1727 | } | |
b9e69e12 | 1728 | EXPORT_SYMBOL_GPL(xt_hook_ops_alloc); |
2b95efe7 | 1729 | |
76108cea | 1730 | int xt_proto_init(struct net *net, u_int8_t af) |
2e4e6a17 HW |
1731 | { |
1732 | #ifdef CONFIG_PROC_FS | |
1733 | char buf[XT_FUNCTION_MAXNAMELEN]; | |
1734 | struct proc_dir_entry *proc; | |
f13f2aee PW |
1735 | kuid_t root_uid; |
1736 | kgid_t root_gid; | |
2e4e6a17 HW |
1737 | #endif |
1738 | ||
7e9c6eeb | 1739 | if (af >= ARRAY_SIZE(xt_prefix)) |
2e4e6a17 HW |
1740 | return -EINVAL; |
1741 | ||
1742 | ||
1743 | #ifdef CONFIG_PROC_FS | |
f13f2aee PW |
1744 | root_uid = make_kuid(net->user_ns, 0); |
1745 | root_gid = make_kgid(net->user_ns, 0); | |
1746 | ||
ce18afe5 | 1747 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1748 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
c3506372 CH |
1749 | proc = proc_create_net_data(buf, 0440, net->proc_net, &xt_table_seq_ops, |
1750 | sizeof(struct seq_net_private), | |
1751 | (void *)(unsigned long)af); | |
2e4e6a17 HW |
1752 | if (!proc) |
1753 | goto out; | |
f13f2aee PW |
1754 | if (uid_valid(root_uid) && gid_valid(root_gid)) |
1755 | proc_set_user(proc, root_uid, root_gid); | |
2e4e6a17 | 1756 | |
ce18afe5 | 1757 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1758 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
1cd67182 CH |
1759 | proc = proc_create_seq_private(buf, 0440, net->proc_net, |
1760 | &xt_match_seq_ops, sizeof(struct nf_mttg_trav), | |
1761 | (void *)(unsigned long)af); | |
2e4e6a17 HW |
1762 | if (!proc) |
1763 | goto out_remove_tables; | |
f13f2aee PW |
1764 | if (uid_valid(root_uid) && gid_valid(root_gid)) |
1765 | proc_set_user(proc, root_uid, root_gid); | |
2e4e6a17 | 1766 | |
ce18afe5 | 1767 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1768 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
1cd67182 CH |
1769 | proc = proc_create_seq_private(buf, 0440, net->proc_net, |
1770 | &xt_target_seq_ops, sizeof(struct nf_mttg_trav), | |
1771 | (void *)(unsigned long)af); | |
2e4e6a17 HW |
1772 | if (!proc) |
1773 | goto out_remove_matches; | |
f13f2aee PW |
1774 | if (uid_valid(root_uid) && gid_valid(root_gid)) |
1775 | proc_set_user(proc, root_uid, root_gid); | |
2e4e6a17 HW |
1776 | #endif |
1777 | ||
1778 | return 0; | |
1779 | ||
1780 | #ifdef CONFIG_PROC_FS | |
1781 | out_remove_matches: | |
ce18afe5 | 1782 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1783 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
ece31ffd | 1784 | remove_proc_entry(buf, net->proc_net); |
2e4e6a17 HW |
1785 | |
1786 | out_remove_tables: | |
ce18afe5 | 1787 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1788 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
ece31ffd | 1789 | remove_proc_entry(buf, net->proc_net); |
2e4e6a17 HW |
1790 | out: |
1791 | return -1; | |
1792 | #endif | |
1793 | } | |
1794 | EXPORT_SYMBOL_GPL(xt_proto_init); | |
1795 | ||
76108cea | 1796 | void xt_proto_fini(struct net *net, u_int8_t af) |
2e4e6a17 HW |
1797 | { |
1798 | #ifdef CONFIG_PROC_FS | |
1799 | char buf[XT_FUNCTION_MAXNAMELEN]; | |
1800 | ||
ce18afe5 | 1801 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1802 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
ece31ffd | 1803 | remove_proc_entry(buf, net->proc_net); |
2e4e6a17 | 1804 | |
ce18afe5 | 1805 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1806 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
ece31ffd | 1807 | remove_proc_entry(buf, net->proc_net); |
2e4e6a17 | 1808 | |
ce18afe5 | 1809 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1810 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
ece31ffd | 1811 | remove_proc_entry(buf, net->proc_net); |
2e4e6a17 HW |
1812 | #endif /*CONFIG_PROC_FS*/ |
1813 | } | |
1814 | EXPORT_SYMBOL_GPL(xt_proto_fini); | |
1815 | ||
f28e15ba FW |
1816 | /** |
1817 | * xt_percpu_counter_alloc - allocate x_tables rule counter | |
1818 | * | |
ae0ac0ed | 1819 | * @state: pointer to xt_percpu allocation state |
f28e15ba FW |
1820 | * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct |
1821 | * | |
1822 | * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then | |
1823 | * contain the address of the real (percpu) counter. | |
1824 | * | |
1825 | * Rule evaluation needs to use xt_get_this_cpu_counter() helper | |
1826 | * to fetch the real percpu counter. | |
1827 | * | |
ae0ac0ed | 1828 | * To speed up allocation and improve data locality, a 4kb block is |
9ba5c404 BH |
1829 | * allocated. Freeing any counter may free an entire block, so all |
1830 | * counters allocated using the same state must be freed at the same | |
1831 | * time. | |
ae0ac0ed FW |
1832 | * |
1833 | * xt_percpu_counter_alloc_state contains the base address of the | |
1834 | * allocated page and the current sub-offset. | |
1835 | * | |
f28e15ba FW |
1836 | * returns false on error. |
1837 | */ | |
ae0ac0ed FW |
1838 | bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state, |
1839 | struct xt_counters *counter) | |
f28e15ba | 1840 | { |
ae0ac0ed | 1841 | BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2)); |
f28e15ba FW |
1842 | |
1843 | if (nr_cpu_ids <= 1) | |
1844 | return true; | |
1845 | ||
ae0ac0ed FW |
1846 | if (!state->mem) { |
1847 | state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE, | |
1848 | XT_PCPU_BLOCK_SIZE); | |
1849 | if (!state->mem) | |
1850 | return false; | |
1851 | } | |
1852 | counter->pcnt = (__force unsigned long)(state->mem + state->off); | |
1853 | state->off += sizeof(*counter); | |
1854 | if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) { | |
1855 | state->mem = NULL; | |
1856 | state->off = 0; | |
1857 | } | |
f28e15ba FW |
1858 | return true; |
1859 | } | |
1860 | EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc); | |
1861 | ||
4d31eef5 FW |
1862 | void xt_percpu_counter_free(struct xt_counters *counters) |
1863 | { | |
1864 | unsigned long pcnt = counters->pcnt; | |
1865 | ||
ae0ac0ed | 1866 | if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0) |
4d31eef5 FW |
1867 | free_percpu((void __percpu *)pcnt); |
1868 | } | |
1869 | EXPORT_SYMBOL_GPL(xt_percpu_counter_free); | |
1870 | ||
8d870052 AD |
1871 | static int __net_init xt_net_init(struct net *net) |
1872 | { | |
1873 | int i; | |
1874 | ||
7e9c6eeb | 1875 | for (i = 0; i < NFPROTO_NUMPROTO; i++) |
8d870052 AD |
1876 | INIT_LIST_HEAD(&net->xt.tables[i]); |
1877 | return 0; | |
1878 | } | |
1879 | ||
613d0776 VA |
1880 | static void __net_exit xt_net_exit(struct net *net) |
1881 | { | |
1882 | int i; | |
1883 | ||
1884 | for (i = 0; i < NFPROTO_NUMPROTO; i++) | |
1885 | WARN_ON_ONCE(!list_empty(&net->xt.tables[i])); | |
1886 | } | |
1887 | ||
8d870052 AD |
1888 | static struct pernet_operations xt_net_ops = { |
1889 | .init = xt_net_init, | |
613d0776 | 1890 | .exit = xt_net_exit, |
8d870052 | 1891 | }; |
2e4e6a17 HW |
1892 | |
1893 | static int __init xt_init(void) | |
1894 | { | |
942e4a2b SH |
1895 | unsigned int i; |
1896 | int rv; | |
1897 | ||
1898 | for_each_possible_cpu(i) { | |
7f5c6d4f | 1899 | seqcount_init(&per_cpu(xt_recseq, i)); |
942e4a2b | 1900 | } |
2e4e6a17 | 1901 | |
8d29d16d | 1902 | xt = kcalloc(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL); |
2e4e6a17 HW |
1903 | if (!xt) |
1904 | return -ENOMEM; | |
1905 | ||
7e9c6eeb | 1906 | for (i = 0; i < NFPROTO_NUMPROTO; i++) { |
9e19bb6d | 1907 | mutex_init(&xt[i].mutex); |
2722971c DM |
1908 | #ifdef CONFIG_COMPAT |
1909 | mutex_init(&xt[i].compat_mutex); | |
255d0dc3 | 1910 | xt[i].compat_tab = NULL; |
2722971c | 1911 | #endif |
2e4e6a17 HW |
1912 | INIT_LIST_HEAD(&xt[i].target); |
1913 | INIT_LIST_HEAD(&xt[i].match); | |
2e4e6a17 | 1914 | } |
8d870052 AD |
1915 | rv = register_pernet_subsys(&xt_net_ops); |
1916 | if (rv < 0) | |
1917 | kfree(xt); | |
1918 | return rv; | |
2e4e6a17 HW |
1919 | } |
1920 | ||
1921 | static void __exit xt_fini(void) | |
1922 | { | |
8d870052 | 1923 | unregister_pernet_subsys(&xt_net_ops); |
2e4e6a17 HW |
1924 | kfree(xt); |
1925 | } | |
1926 | ||
1927 | module_init(xt_init); | |
1928 | module_exit(xt_fini); | |
1929 |