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