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