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