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