]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/netfilter/x_tables.c
netfilter: x_tables: avoid stack-out-of-bounds read in xt_copy_counters_from_user
[thirdparty/kernel/stable.git] / net / netfilter / x_tables.c
CommitLineData
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>
457c4cbc 29#include <net/net_namespace.h>
2e4e6a17
HW
30
31#include <linux/netfilter/x_tables.h>
32#include <linux/netfilter_arp.h>
e3eaa991
JE
33#include <linux/netfilter_ipv4/ip_tables.h>
34#include <linux/netfilter_ipv6/ip6_tables.h>
35#include <linux/netfilter_arp/arp_tables.h>
9e19bb6d 36
2e4e6a17
HW
37MODULE_LICENSE("GPL");
38MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
043ef46c 39MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
2e4e6a17 40
b386d9f5 41struct compat_delta {
255d0dc3
ED
42 unsigned int offset; /* offset in kernel */
43 int delta; /* delta in 32bit user land */
b386d9f5
PM
44};
45
2e4e6a17 46struct 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
58static struct xt_af *xt;
59
7e9c6eeb
JE
60static 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. */
69static const unsigned int xt_jumpstack_multiplier = 2;
70
2e4e6a17 71/* Registration hooks for targets. */
7926dbfa 72int 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}
81EXPORT_SYMBOL(xt_register_target);
82
83void
a45049c5 84xt_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}
92EXPORT_SYMBOL(xt_unregister_target);
93
52d9c42e
PM
94int
95xt_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
107err:
108 if (i > 0)
109 xt_unregister_targets(target, i);
110 return err;
111}
112EXPORT_SYMBOL(xt_register_targets);
113
114void
115xt_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}
120EXPORT_SYMBOL(xt_unregister_targets);
121
7926dbfa 122int 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}
131EXPORT_SYMBOL(xt_register_match);
132
133void
a45049c5 134xt_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}
142EXPORT_SYMBOL(xt_unregister_match);
143
52d9c42e
PM
144int
145xt_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
157err:
158 if (i > 0)
159 xt_unregister_matches(match, i);
160 return err;
161}
162EXPORT_SYMBOL(xt_register_matches);
163
164void
165xt_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}
170EXPORT_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 180struct 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}
205EXPORT_SYMBOL(xt_find_match);
206
fd0ec0e6
JE
207struct xt_match *
208xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
209{
210 struct xt_match *match;
211
eaae500a
ED
212 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
213 return ERR_PTR(-EINVAL);
214
adb00ae2
SH
215 match = xt_find_match(nfproto, name, revision);
216 if (IS_ERR(match)) {
217 request_module("%st_%s", xt_prefix[nfproto], name);
218 match = xt_find_match(nfproto, name, revision);
219 }
220
221 return match;
fd0ec0e6
JE
222}
223EXPORT_SYMBOL_GPL(xt_request_find_match);
224
2e4e6a17 225/* Find target, grabs ref. Returns ERR_PTR() on error. */
76108cea 226struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
2e4e6a17
HW
227{
228 struct xt_target *t;
42046e2e 229 int err = -ENOENT;
2e4e6a17 230
7926dbfa 231 mutex_lock(&xt[af].mutex);
2e4e6a17
HW
232 list_for_each_entry(t, &xt[af].target, list) {
233 if (strcmp(t->name, name) == 0) {
234 if (t->revision == revision) {
235 if (try_module_get(t->me)) {
9e19bb6d 236 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
237 return t;
238 }
239 } else
240 err = -EPROTOTYPE; /* Found something. */
241 }
242 }
9e19bb6d 243 mutex_unlock(&xt[af].mutex);
55b69e91
JE
244
245 if (af != NFPROTO_UNSPEC)
246 /* Try searching again in the family-independent list */
247 return xt_find_target(NFPROTO_UNSPEC, name, revision);
248
2e4e6a17
HW
249 return ERR_PTR(err);
250}
251EXPORT_SYMBOL(xt_find_target);
252
76108cea 253struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
2e4e6a17
HW
254{
255 struct xt_target *target;
256
eaae500a
ED
257 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
258 return ERR_PTR(-EINVAL);
259
adb00ae2
SH
260 target = xt_find_target(af, name, revision);
261 if (IS_ERR(target)) {
262 request_module("%st_%s", xt_prefix[af], name);
263 target = xt_find_target(af, name, revision);
264 }
265
266 return target;
2e4e6a17
HW
267}
268EXPORT_SYMBOL_GPL(xt_request_find_target);
269
76108cea 270static 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 290static 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 311int 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
7926dbfa 316 mutex_lock(&xt[af].mutex);
2e4e6a17
HW
317 if (target == 1)
318 have_rev = target_revfn(af, name, revision, &best);
319 else
320 have_rev = match_revfn(af, name, revision, &best);
9e19bb6d 321 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
322
323 /* Nothing at all? Return 0 to try loading module. */
324 if (best == -1) {
325 *err = -ENOENT;
326 return 0;
327 }
328
329 *err = best;
330 if (!have_rev)
331 *err = -EPROTONOSUPPORT;
332 return 1;
333}
334EXPORT_SYMBOL_GPL(xt_find_revision);
335
5b76c494
JE
336static char *
337textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
45185364 338{
5b76c494 339 static const char *const inetbr_names[] = {
45185364
JE
340 "PREROUTING", "INPUT", "FORWARD",
341 "OUTPUT", "POSTROUTING", "BROUTING",
342 };
5b76c494
JE
343 static const char *const arp_names[] = {
344 "INPUT", "FORWARD", "OUTPUT",
345 };
346 const char *const *names;
347 unsigned int i, max;
45185364
JE
348 char *p = buf;
349 bool np = false;
350 int res;
351
5b76c494
JE
352 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
353 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
354 ARRAY_SIZE(inetbr_names);
45185364 355 *p = '\0';
5b76c494 356 for (i = 0; i < max; ++i) {
45185364
JE
357 if (!(mask & (1 << i)))
358 continue;
359 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
360 if (res > 0) {
361 size -= res;
362 p += res;
363 }
364 np = true;
365 }
366
367 return buf;
368}
369
42b41149
FW
370/**
371 * xt_check_proc_name - check that name is suitable for /proc file creation
372 *
373 * @name: file name candidate
374 * @size: length of buffer
375 *
376 * some x_tables modules wish to create a file in /proc.
377 * This function makes sure that the name is suitable for this
378 * purpose, it checks that name is NUL terminated and isn't a 'special'
379 * name, like "..".
380 *
381 * returns negative number on error or 0 if name is useable.
382 */
383int xt_check_proc_name(const char *name, unsigned int size)
384{
385 if (name[0] == '\0')
386 return -EINVAL;
387
388 if (strnlen(name, size) == size)
389 return -ENAMETOOLONG;
390
391 if (strcmp(name, ".") == 0 ||
392 strcmp(name, "..") == 0 ||
393 strchr(name, '/'))
394 return -EINVAL;
395
396 return 0;
397}
398EXPORT_SYMBOL(xt_check_proc_name);
399
916a917d 400int xt_check_match(struct xt_mtchk_param *par,
9b4fce7a 401 unsigned int size, u_int8_t proto, bool inv_proto)
37f9f733 402{
bd414ee6
JE
403 int ret;
404
9b4fce7a
JE
405 if (XT_ALIGN(par->match->matchsize) != size &&
406 par->match->matchsize != -1) {
043ef46c
JE
407 /*
408 * ebt_among is exempt from centralized matchsize checking
409 * because it uses a dynamic-size data set.
410 */
b402405d
JE
411 pr_err("%s_tables: %s.%u match: invalid size "
412 "%u (kernel) != (user) %u\n",
916a917d 413 xt_prefix[par->family], par->match->name,
b402405d 414 par->match->revision,
9b4fce7a 415 XT_ALIGN(par->match->matchsize), size);
37f9f733
PM
416 return -EINVAL;
417 }
9b4fce7a
JE
418 if (par->match->table != NULL &&
419 strcmp(par->match->table, par->table) != 0) {
3dd5d7e3 420 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
916a917d 421 xt_prefix[par->family], par->match->name,
9b4fce7a 422 par->match->table, par->table);
37f9f733
PM
423 return -EINVAL;
424 }
9b4fce7a 425 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
45185364
JE
426 char used[64], allow[64];
427
3dd5d7e3 428 pr_err("%s_tables: %s match: used from hooks %s, but only "
45185364 429 "valid from %s\n",
916a917d 430 xt_prefix[par->family], par->match->name,
5b76c494
JE
431 textify_hooks(used, sizeof(used), par->hook_mask,
432 par->family),
433 textify_hooks(allow, sizeof(allow), par->match->hooks,
434 par->family));
37f9f733
PM
435 return -EINVAL;
436 }
9b4fce7a 437 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
3dd5d7e3 438 pr_err("%s_tables: %s match: only valid for protocol %u\n",
916a917d
JE
439 xt_prefix[par->family], par->match->name,
440 par->match->proto);
37f9f733
PM
441 return -EINVAL;
442 }
bd414ee6
JE
443 if (par->match->checkentry != NULL) {
444 ret = par->match->checkentry(par);
445 if (ret < 0)
446 return ret;
447 else if (ret > 0)
448 /* Flag up potential errors. */
449 return -EIO;
450 }
37f9f733
PM
451 return 0;
452}
453EXPORT_SYMBOL_GPL(xt_check_match);
454
3b94ada4
FW
455/** xt_check_entry_match - check that matches end before start of target
456 *
457 * @match: beginning of xt_entry_match
458 * @target: beginning of this rules target (alleged end of matches)
459 * @alignment: alignment requirement of match structures
460 *
461 * Validates that all matches add up to the beginning of the target,
462 * and that each match covers at least the base structure size.
463 *
464 * Return: 0 on success, negative errno on failure.
465 */
466static int xt_check_entry_match(const char *match, const char *target,
467 const size_t alignment)
468{
469 const struct xt_entry_match *pos;
470 int length = target - match;
471
472 if (length == 0) /* no matches */
473 return 0;
474
475 pos = (struct xt_entry_match *)match;
476 do {
477 if ((unsigned long)pos % alignment)
478 return -EINVAL;
479
480 if (length < (int)sizeof(struct xt_entry_match))
481 return -EINVAL;
482
483 if (pos->u.match_size < sizeof(struct xt_entry_match))
484 return -EINVAL;
485
486 if (pos->u.match_size > length)
487 return -EINVAL;
488
489 length -= pos->u.match_size;
490 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
491 } while (length > 0);
492
493 return 0;
494}
495
2722971c 496#ifdef CONFIG_COMPAT
255d0dc3 497int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
b386d9f5 498{
255d0dc3 499 struct xt_af *xp = &xt[af];
b386d9f5 500
255d0dc3
ED
501 if (!xp->compat_tab) {
502 if (!xp->number)
503 return -EINVAL;
504 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
505 if (!xp->compat_tab)
506 return -ENOMEM;
507 xp->cur = 0;
508 }
b386d9f5 509
255d0dc3
ED
510 if (xp->cur >= xp->number)
511 return -EINVAL;
b386d9f5 512
255d0dc3
ED
513 if (xp->cur)
514 delta += xp->compat_tab[xp->cur - 1].delta;
515 xp->compat_tab[xp->cur].offset = offset;
516 xp->compat_tab[xp->cur].delta = delta;
517 xp->cur++;
b386d9f5
PM
518 return 0;
519}
520EXPORT_SYMBOL_GPL(xt_compat_add_offset);
521
76108cea 522void xt_compat_flush_offsets(u_int8_t af)
b386d9f5 523{
255d0dc3
ED
524 if (xt[af].compat_tab) {
525 vfree(xt[af].compat_tab);
526 xt[af].compat_tab = NULL;
527 xt[af].number = 0;
5a6351ee 528 xt[af].cur = 0;
b386d9f5
PM
529 }
530}
531EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
532
3e5e524f 533int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
b386d9f5 534{
255d0dc3
ED
535 struct compat_delta *tmp = xt[af].compat_tab;
536 int mid, left = 0, right = xt[af].cur - 1;
537
538 while (left <= right) {
539 mid = (left + right) >> 1;
540 if (offset > tmp[mid].offset)
541 left = mid + 1;
542 else if (offset < tmp[mid].offset)
543 right = mid - 1;
544 else
545 return mid ? tmp[mid - 1].delta : 0;
546 }
5a6351ee 547 return left ? tmp[left - 1].delta : 0;
b386d9f5
PM
548}
549EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
550
255d0dc3
ED
551void xt_compat_init_offsets(u_int8_t af, unsigned int number)
552{
553 xt[af].number = number;
554 xt[af].cur = 0;
555}
556EXPORT_SYMBOL(xt_compat_init_offsets);
557
5452e425 558int xt_compat_match_offset(const struct xt_match *match)
2722971c 559{
9fa492cd
PM
560 u_int16_t csize = match->compatsize ? : match->matchsize;
561 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
562}
563EXPORT_SYMBOL_GPL(xt_compat_match_offset);
564
edd8ae36
FW
565void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
566 unsigned int *size)
9fa492cd 567{
5452e425 568 const struct xt_match *match = m->u.kernel.match;
9fa492cd
PM
569 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
570 int pad, off = xt_compat_match_offset(match);
571 u_int16_t msize = cm->u.user.match_size;
aaa155f8 572 char name[sizeof(m->u.user.name)];
9fa492cd
PM
573
574 m = *dstptr;
575 memcpy(m, cm, sizeof(*cm));
576 if (match->compat_from_user)
577 match->compat_from_user(m->data, cm->data);
578 else
579 memcpy(m->data, cm->data, msize - sizeof(*cm));
580 pad = XT_ALIGN(match->matchsize) - match->matchsize;
581 if (pad > 0)
582 memset(m->data + match->matchsize, 0, pad);
583
584 msize += off;
585 m->u.user.match_size = msize;
aaa155f8
FW
586 strlcpy(name, match->name, sizeof(name));
587 module_put(match->me);
588 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
9fa492cd
PM
589
590 *size += off;
591 *dstptr += msize;
592}
593EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
594
739674fb
JE
595int xt_compat_match_to_user(const struct xt_entry_match *m,
596 void __user **dstptr, unsigned int *size)
9fa492cd 597{
5452e425 598 const struct xt_match *match = m->u.kernel.match;
9fa492cd
PM
599 struct compat_xt_entry_match __user *cm = *dstptr;
600 int off = xt_compat_match_offset(match);
601 u_int16_t msize = m->u.user.match_size - off;
602
603 if (copy_to_user(cm, m, sizeof(*cm)) ||
a18aa31b
PM
604 put_user(msize, &cm->u.user.match_size) ||
605 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
606 strlen(m->u.kernel.match->name) + 1))
601e68e1 607 return -EFAULT;
9fa492cd
PM
608
609 if (match->compat_to_user) {
610 if (match->compat_to_user((void __user *)cm->data, m->data))
611 return -EFAULT;
612 } else {
613 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
614 return -EFAULT;
2722971c 615 }
9fa492cd
PM
616
617 *size -= off;
618 *dstptr += msize;
619 return 0;
2722971c 620}
9fa492cd 621EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
7ef13f49 622
c1380ecb
FW
623/* non-compat version may have padding after verdict */
624struct compat_xt_standard_target {
625 struct compat_xt_entry_target t;
626 compat_uint_t verdict;
627};
628
7ba6a7df 629int xt_compat_check_entry_offsets(const void *base, const char *elems,
7ef13f49
FW
630 unsigned int target_offset,
631 unsigned int next_offset)
632{
7ba6a7df 633 long size_of_base_struct = elems - (const char *)base;
7ef13f49
FW
634 const struct compat_xt_entry_target *t;
635 const char *e = base;
636
7ba6a7df
FW
637 if (target_offset < size_of_base_struct)
638 return -EINVAL;
639
7ef13f49
FW
640 if (target_offset + sizeof(*t) > next_offset)
641 return -EINVAL;
642
643 t = (void *)(e + target_offset);
644 if (t->u.target_size < sizeof(*t))
645 return -EINVAL;
646
647 if (target_offset + t->u.target_size > next_offset)
648 return -EINVAL;
649
c1380ecb 650 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
bbb7ecc8 651 COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
c1380ecb
FW
652 return -EINVAL;
653
3b94ada4
FW
654 /* compat_xt_entry match has less strict aligment requirements,
655 * otherwise they are identical. In case of padding differences
656 * we need to add compat version of xt_check_entry_match.
657 */
658 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
659
660 return xt_check_entry_match(elems, base + target_offset,
661 __alignof__(struct compat_xt_entry_match));
7ef13f49
FW
662}
663EXPORT_SYMBOL(xt_compat_check_entry_offsets);
9fa492cd 664#endif /* CONFIG_COMPAT */
2722971c 665
62e6fd20
FW
666/**
667 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
668 *
669 * @base: pointer to arp/ip/ip6t_entry
7ba6a7df 670 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
62e6fd20
FW
671 * @target_offset: the arp/ip/ip6_t->target_offset
672 * @next_offset: the arp/ip/ip6_t->next_offset
673 *
3b94ada4
FW
674 * validates that target_offset and next_offset are sane and that all
675 * match sizes (if any) align with the target offset.
62e6fd20 676 *
7ba6a7df 677 * This function does not validate the targets or matches themselves, it
3b94ada4
FW
678 * only tests that all the offsets and sizes are correct, that all
679 * match structures are aligned, and that the last structure ends where
680 * the target structure begins.
681 *
682 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
7ba6a7df 683 *
62e6fd20
FW
684 * The arp/ip/ip6t_entry structure @base must have passed following tests:
685 * - it must point to a valid memory location
686 * - base to base + next_offset must be accessible, i.e. not exceed allocated
687 * length.
688 *
3b94ada4
FW
689 * A well-formed entry looks like this:
690 *
691 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
692 * e->elems[]-----' | |
693 * matchsize | |
694 * matchsize | |
695 * | |
696 * target_offset---------------------------------' |
697 * next_offset---------------------------------------------------'
698 *
699 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
700 * This is where matches (if any) and the target reside.
701 * target_offset: beginning of target.
702 * next_offset: start of the next rule; also: size of this rule.
703 * Since targets have a minimum size, target_offset + minlen <= next_offset.
704 *
705 * Every match stores its size, sum of sizes must not exceed target_offset.
706 *
62e6fd20
FW
707 * Return: 0 on success, negative errno on failure.
708 */
709int xt_check_entry_offsets(const void *base,
7ba6a7df 710 const char *elems,
62e6fd20
FW
711 unsigned int target_offset,
712 unsigned int next_offset)
713{
7ba6a7df 714 long size_of_base_struct = elems - (const char *)base;
62e6fd20
FW
715 const struct xt_entry_target *t;
716 const char *e = base;
717
7ba6a7df
FW
718 /* target start is within the ip/ip6/arpt_entry struct */
719 if (target_offset < size_of_base_struct)
720 return -EINVAL;
721
62e6fd20
FW
722 if (target_offset + sizeof(*t) > next_offset)
723 return -EINVAL;
724
725 t = (void *)(e + target_offset);
37a6fed6
FW
726 if (t->u.target_size < sizeof(*t))
727 return -EINVAL;
728
62e6fd20
FW
729 if (target_offset + t->u.target_size > next_offset)
730 return -EINVAL;
731
c1380ecb 732 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
bbb7ecc8 733 XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
c1380ecb
FW
734 return -EINVAL;
735
3b94ada4
FW
736 return xt_check_entry_match(elems, base + target_offset,
737 __alignof__(struct xt_entry_match));
62e6fd20
FW
738}
739EXPORT_SYMBOL(xt_check_entry_offsets);
740
f5bba514
FW
741/**
742 * xt_alloc_entry_offsets - allocate array to store rule head offsets
743 *
744 * @size: number of entries
745 *
746 * Return: NULL or kmalloc'd or vmalloc'd array
747 */
748unsigned int *xt_alloc_entry_offsets(unsigned int size)
749{
750 unsigned int *off;
751
752 off = kcalloc(size, sizeof(unsigned int), GFP_KERNEL | __GFP_NOWARN);
753
754 if (off)
755 return off;
756
757 if (size < (SIZE_MAX / sizeof(unsigned int)))
758 off = vmalloc(size * sizeof(unsigned int));
759
760 return off;
761}
762EXPORT_SYMBOL(xt_alloc_entry_offsets);
763
764/**
765 * xt_find_jump_offset - check if target is a valid jump offset
766 *
767 * @offsets: array containing all valid rule start offsets of a rule blob
768 * @target: the jump target to search for
769 * @size: entries in @offset
770 */
771bool xt_find_jump_offset(const unsigned int *offsets,
772 unsigned int target, unsigned int size)
773{
774 int m, low = 0, hi = size;
775
776 while (hi > low) {
777 m = (low + hi) / 2u;
778
779 if (offsets[m] > target)
780 hi = m;
781 else if (offsets[m] < target)
782 low = m + 1;
783 else
784 return true;
785 }
786
787 return false;
788}
789EXPORT_SYMBOL(xt_find_jump_offset);
790
916a917d 791int xt_check_target(struct xt_tgchk_param *par,
af5d6dc2 792 unsigned int size, u_int8_t proto, bool inv_proto)
37f9f733 793{
d6b00a53
JE
794 int ret;
795
af5d6dc2 796 if (XT_ALIGN(par->target->targetsize) != size) {
b402405d
JE
797 pr_err("%s_tables: %s.%u target: invalid size "
798 "%u (kernel) != (user) %u\n",
916a917d 799 xt_prefix[par->family], par->target->name,
b402405d 800 par->target->revision,
af5d6dc2 801 XT_ALIGN(par->target->targetsize), size);
37f9f733
PM
802 return -EINVAL;
803 }
af5d6dc2
JE
804 if (par->target->table != NULL &&
805 strcmp(par->target->table, par->table) != 0) {
3dd5d7e3 806 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
916a917d 807 xt_prefix[par->family], par->target->name,
af5d6dc2 808 par->target->table, par->table);
37f9f733
PM
809 return -EINVAL;
810 }
af5d6dc2 811 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
45185364
JE
812 char used[64], allow[64];
813
3dd5d7e3 814 pr_err("%s_tables: %s target: used from hooks %s, but only "
45185364 815 "usable from %s\n",
916a917d 816 xt_prefix[par->family], par->target->name,
5b76c494
JE
817 textify_hooks(used, sizeof(used), par->hook_mask,
818 par->family),
819 textify_hooks(allow, sizeof(allow), par->target->hooks,
820 par->family));
37f9f733
PM
821 return -EINVAL;
822 }
af5d6dc2 823 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
3dd5d7e3 824 pr_err("%s_tables: %s target: only valid for protocol %u\n",
916a917d 825 xt_prefix[par->family], par->target->name,
af5d6dc2 826 par->target->proto);
37f9f733
PM
827 return -EINVAL;
828 }
d6b00a53
JE
829 if (par->target->checkentry != NULL) {
830 ret = par->target->checkentry(par);
831 if (ret < 0)
832 return ret;
833 else if (ret > 0)
834 /* Flag up potential errors. */
835 return -EIO;
836 }
37f9f733
PM
837 return 0;
838}
839EXPORT_SYMBOL_GPL(xt_check_target);
840
6a9f9d4e
FW
841/**
842 * xt_copy_counters_from_user - copy counters and metadata from userspace
843 *
844 * @user: src pointer to userspace memory
845 * @len: alleged size of userspace memory
846 * @info: where to store the xt_counters_info metadata
847 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
848 *
849 * Copies counter meta data from @user and stores it in @info.
850 *
851 * vmallocs memory to hold the counters, then copies the counter data
852 * from @user to the new memory and returns a pointer to it.
853 *
854 * If @compat is true, @info gets converted automatically to the 64bit
855 * representation.
856 *
857 * The metadata associated with the counters is stored in @info.
858 *
859 * Return: returns pointer that caller has to test via IS_ERR().
860 * If IS_ERR is false, caller has to vfree the pointer.
861 */
862void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
863 struct xt_counters_info *info, bool compat)
864{
865 void *mem;
866 u64 size;
867
868#ifdef CONFIG_COMPAT
869 if (compat) {
870 /* structures only differ in size due to alignment */
871 struct compat_xt_counters_info compat_tmp;
872
873 if (len <= sizeof(compat_tmp))
874 return ERR_PTR(-EINVAL);
875
876 len -= sizeof(compat_tmp);
877 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
878 return ERR_PTR(-EFAULT);
879
a4508e03 880 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
6a9f9d4e
FW
881 info->num_counters = compat_tmp.num_counters;
882 user += sizeof(compat_tmp);
883 } else
884#endif
885 {
886 if (len <= sizeof(*info))
887 return ERR_PTR(-EINVAL);
888
889 len -= sizeof(*info);
890 if (copy_from_user(info, user, sizeof(*info)) != 0)
891 return ERR_PTR(-EFAULT);
892
6a9f9d4e
FW
893 user += sizeof(*info);
894 }
a4508e03 895 info->name[sizeof(info->name) - 1] = '\0';
6a9f9d4e
FW
896
897 size = sizeof(struct xt_counters);
898 size *= info->num_counters;
899
900 if (size != (u64)len)
901 return ERR_PTR(-EINVAL);
902
903 mem = vmalloc(len);
904 if (!mem)
905 return ERR_PTR(-ENOMEM);
906
907 if (copy_from_user(mem, user, len) == 0)
908 return mem;
909
910 vfree(mem);
911 return ERR_PTR(-EFAULT);
912}
913EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
914
2722971c 915#ifdef CONFIG_COMPAT
5452e425 916int xt_compat_target_offset(const struct xt_target *target)
2722971c 917{
9fa492cd
PM
918 u_int16_t csize = target->compatsize ? : target->targetsize;
919 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
920}
921EXPORT_SYMBOL_GPL(xt_compat_target_offset);
922
923void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
b0a6363c 924 unsigned int *size)
9fa492cd 925{
5452e425 926 const struct xt_target *target = t->u.kernel.target;
9fa492cd
PM
927 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
928 int pad, off = xt_compat_target_offset(target);
929 u_int16_t tsize = ct->u.user.target_size;
aaa155f8 930 char name[sizeof(t->u.user.name)];
9fa492cd
PM
931
932 t = *dstptr;
933 memcpy(t, ct, sizeof(*ct));
934 if (target->compat_from_user)
935 target->compat_from_user(t->data, ct->data);
936 else
937 memcpy(t->data, ct->data, tsize - sizeof(*ct));
938 pad = XT_ALIGN(target->targetsize) - target->targetsize;
939 if (pad > 0)
940 memset(t->data + target->targetsize, 0, pad);
941
942 tsize += off;
943 t->u.user.target_size = tsize;
aaa155f8
FW
944 strlcpy(name, target->name, sizeof(name));
945 module_put(target->me);
946 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
9fa492cd
PM
947
948 *size += off;
949 *dstptr += tsize;
950}
951EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
952
739674fb
JE
953int xt_compat_target_to_user(const struct xt_entry_target *t,
954 void __user **dstptr, unsigned int *size)
9fa492cd 955{
5452e425 956 const struct xt_target *target = t->u.kernel.target;
9fa492cd
PM
957 struct compat_xt_entry_target __user *ct = *dstptr;
958 int off = xt_compat_target_offset(target);
959 u_int16_t tsize = t->u.user.target_size - off;
960
961 if (copy_to_user(ct, t, sizeof(*ct)) ||
a18aa31b
PM
962 put_user(tsize, &ct->u.user.target_size) ||
963 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
964 strlen(t->u.kernel.target->name) + 1))
601e68e1 965 return -EFAULT;
9fa492cd
PM
966
967 if (target->compat_to_user) {
968 if (target->compat_to_user((void __user *)ct->data, t->data))
969 return -EFAULT;
970 } else {
971 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
972 return -EFAULT;
2722971c 973 }
9fa492cd
PM
974
975 *size -= off;
976 *dstptr += tsize;
977 return 0;
2722971c 978}
9fa492cd 979EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
2722971c
DM
980#endif
981
2e4e6a17
HW
982struct xt_table_info *xt_alloc_table_info(unsigned int size)
983{
984 struct xt_table_info *newinfo;
985 int cpu;
986
987 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
ab24a3d5 988 if ((size >> PAGE_SHIFT) + 2 > totalram_pages)
2e4e6a17
HW
989 return NULL;
990
259d4e41 991 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
2e4e6a17
HW
992 if (!newinfo)
993 return NULL;
994
995 newinfo->size = size;
996
6f912042 997 for_each_possible_cpu(cpu) {
2e4e6a17
HW
998 if (size <= PAGE_SIZE)
999 newinfo->entries[cpu] = kmalloc_node(size,
1000 GFP_KERNEL,
1001 cpu_to_node(cpu));
1002 else
1003 newinfo->entries[cpu] = vmalloc_node(size,
1004 cpu_to_node(cpu));
1005
1006 if (newinfo->entries[cpu] == NULL) {
1007 xt_free_table_info(newinfo);
1008 return NULL;
1009 }
1010 }
1011
1012 return newinfo;
1013}
1014EXPORT_SYMBOL(xt_alloc_table_info);
1015
1016void xt_free_table_info(struct xt_table_info *info)
1017{
1018 int cpu;
1019
f6b50824
ED
1020 for_each_possible_cpu(cpu)
1021 kvfree(info->entries[cpu]);
f3c5c1bf
JE
1022
1023 if (info->jumpstack != NULL) {
f6b50824
ED
1024 for_each_possible_cpu(cpu)
1025 kvfree(info->jumpstack[cpu]);
1026 kvfree(info->jumpstack);
f3c5c1bf
JE
1027 }
1028
7489aec8 1029 free_percpu(info->stackptr);
f3c5c1bf 1030
2e4e6a17
HW
1031 kfree(info);
1032}
1033EXPORT_SYMBOL(xt_free_table_info);
1034
1035/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
76108cea
JE
1036struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1037 const char *name)
2e4e6a17
HW
1038{
1039 struct xt_table *t;
1040
7926dbfa 1041 mutex_lock(&xt[af].mutex);
8d870052 1042 list_for_each_entry(t, &net->xt.tables[af], list)
2e4e6a17
HW
1043 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1044 return t;
9e19bb6d 1045 mutex_unlock(&xt[af].mutex);
2e4e6a17
HW
1046 return NULL;
1047}
1048EXPORT_SYMBOL_GPL(xt_find_table_lock);
1049
1050void xt_table_unlock(struct xt_table *table)
1051{
9e19bb6d 1052 mutex_unlock(&xt[table->af].mutex);
2e4e6a17
HW
1053}
1054EXPORT_SYMBOL_GPL(xt_table_unlock);
1055
2722971c 1056#ifdef CONFIG_COMPAT
76108cea 1057void xt_compat_lock(u_int8_t af)
2722971c
DM
1058{
1059 mutex_lock(&xt[af].compat_mutex);
1060}
1061EXPORT_SYMBOL_GPL(xt_compat_lock);
1062
76108cea 1063void xt_compat_unlock(u_int8_t af)
2722971c
DM
1064{
1065 mutex_unlock(&xt[af].compat_mutex);
1066}
1067EXPORT_SYMBOL_GPL(xt_compat_unlock);
1068#endif
2e4e6a17 1069
7f5c6d4f
ED
1070DEFINE_PER_CPU(seqcount_t, xt_recseq);
1071EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
942e4a2b 1072
f3c5c1bf
JE
1073static int xt_jumpstack_alloc(struct xt_table_info *i)
1074{
1075 unsigned int size;
1076 int cpu;
1077
7489aec8 1078 i->stackptr = alloc_percpu(unsigned int);
f3c5c1bf
JE
1079 if (i->stackptr == NULL)
1080 return -ENOMEM;
f3c5c1bf
JE
1081
1082 size = sizeof(void **) * nr_cpu_ids;
1083 if (size > PAGE_SIZE)
3dbd4439 1084 i->jumpstack = vzalloc(size);
f3c5c1bf 1085 else
3dbd4439 1086 i->jumpstack = kzalloc(size, GFP_KERNEL);
f3c5c1bf
JE
1087 if (i->jumpstack == NULL)
1088 return -ENOMEM;
f3c5c1bf
JE
1089
1090 i->stacksize *= xt_jumpstack_multiplier;
1091 size = sizeof(void *) * i->stacksize;
1092 for_each_possible_cpu(cpu) {
1093 if (size > PAGE_SIZE)
1094 i->jumpstack[cpu] = vmalloc_node(size,
1095 cpu_to_node(cpu));
1096 else
1097 i->jumpstack[cpu] = kmalloc_node(size,
1098 GFP_KERNEL, cpu_to_node(cpu));
1099 if (i->jumpstack[cpu] == NULL)
1100 /*
1101 * Freeing will be done later on by the callers. The
1102 * chain is: xt_replace_table -> __do_replace ->
1103 * do_replace -> xt_free_table_info.
1104 */
1105 return -ENOMEM;
1106 }
1107
1108 return 0;
1109}
942e4a2b 1110
2e4e6a17
HW
1111struct xt_table_info *
1112xt_replace_table(struct xt_table *table,
1113 unsigned int num_counters,
1114 struct xt_table_info *newinfo,
1115 int *error)
1116{
942e4a2b 1117 struct xt_table_info *private;
f3c5c1bf 1118 int ret;
2e4e6a17 1119
d97a9e47
JE
1120 ret = xt_jumpstack_alloc(newinfo);
1121 if (ret < 0) {
1122 *error = ret;
1123 return NULL;
1124 }
1125
2e4e6a17 1126 /* Do the substitution. */
942e4a2b 1127 local_bh_disable();
2e4e6a17 1128 private = table->private;
942e4a2b 1129
2e4e6a17
HW
1130 /* Check inside lock: is the old number correct? */
1131 if (num_counters != private->number) {
be91fd5e 1132 pr_debug("num_counters != table->private->number (%u/%u)\n",
2e4e6a17 1133 num_counters, private->number);
942e4a2b 1134 local_bh_enable();
2e4e6a17
HW
1135 *error = -EAGAIN;
1136 return NULL;
1137 }
2e4e6a17 1138
942e4a2b 1139 newinfo->initial_entries = private->initial_entries;
b416c144
WD
1140 /*
1141 * Ensure contents of newinfo are visible before assigning to
1142 * private.
1143 */
1144 smp_wmb();
1145 table->private = newinfo;
942e4a2b
SH
1146
1147 /*
1148 * Even though table entries have now been swapped, other CPU's
1149 * may still be using the old entries. This is okay, because
1150 * resynchronization happens because of the locking done
1151 * during the get_counters() routine.
1152 */
1153 local_bh_enable();
1154
fbabf31e
TG
1155#ifdef CONFIG_AUDIT
1156 if (audit_enabled) {
1157 struct audit_buffer *ab;
1158
1159 ab = audit_log_start(current->audit_context, GFP_KERNEL,
1160 AUDIT_NETFILTER_CFG);
1161 if (ab) {
1162 audit_log_format(ab, "table=%s family=%u entries=%u",
1163 table->name, table->af,
1164 private->number);
1165 audit_log_end(ab);
1166 }
1167 }
1168#endif
1169
942e4a2b 1170 return private;
2e4e6a17
HW
1171}
1172EXPORT_SYMBOL_GPL(xt_replace_table);
1173
35aad0ff
JE
1174struct xt_table *xt_register_table(struct net *net,
1175 const struct xt_table *input_table,
a98da11d
AD
1176 struct xt_table_info *bootstrap,
1177 struct xt_table_info *newinfo)
2e4e6a17
HW
1178{
1179 int ret;
1180 struct xt_table_info *private;
35aad0ff 1181 struct xt_table *t, *table;
2e4e6a17 1182
44d34e72 1183 /* Don't add one object to multiple lists. */
35aad0ff 1184 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
44d34e72
AD
1185 if (!table) {
1186 ret = -ENOMEM;
1187 goto out;
1188 }
1189
7926dbfa 1190 mutex_lock(&xt[table->af].mutex);
2e4e6a17 1191 /* Don't autoload: we'd eat our tail... */
8d870052 1192 list_for_each_entry(t, &net->xt.tables[table->af], list) {
df0933dc
PM
1193 if (strcmp(t->name, table->name) == 0) {
1194 ret = -EEXIST;
1195 goto unlock;
1196 }
2e4e6a17
HW
1197 }
1198
1199 /* Simplifies replace_table code. */
1200 table->private = bootstrap;
78454473 1201
2e4e6a17
HW
1202 if (!xt_replace_table(table, 0, newinfo, &ret))
1203 goto unlock;
1204
1205 private = table->private;
be91fd5e 1206 pr_debug("table->private->number = %u\n", private->number);
2e4e6a17
HW
1207
1208 /* save number of initial entries */
1209 private->initial_entries = private->number;
1210
8d870052 1211 list_add(&table->list, &net->xt.tables[table->af]);
a98da11d
AD
1212 mutex_unlock(&xt[table->af].mutex);
1213 return table;
2e4e6a17 1214
7926dbfa 1215unlock:
9e19bb6d 1216 mutex_unlock(&xt[table->af].mutex);
44d34e72 1217 kfree(table);
a98da11d
AD
1218out:
1219 return ERR_PTR(ret);
2e4e6a17
HW
1220}
1221EXPORT_SYMBOL_GPL(xt_register_table);
1222
1223void *xt_unregister_table(struct xt_table *table)
1224{
1225 struct xt_table_info *private;
1226
9e19bb6d 1227 mutex_lock(&xt[table->af].mutex);
2e4e6a17 1228 private = table->private;
df0933dc 1229 list_del(&table->list);
9e19bb6d 1230 mutex_unlock(&xt[table->af].mutex);
44d34e72 1231 kfree(table);
2e4e6a17
HW
1232
1233 return private;
1234}
1235EXPORT_SYMBOL_GPL(xt_unregister_table);
1236
1237#ifdef CONFIG_PROC_FS
715cf35a
AD
1238struct xt_names_priv {
1239 struct seq_net_private p;
76108cea 1240 u_int8_t af;
715cf35a 1241};
025d93d1 1242static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
2e4e6a17 1243{
715cf35a 1244 struct xt_names_priv *priv = seq->private;
1218854a 1245 struct net *net = seq_file_net(seq);
76108cea 1246 u_int8_t af = priv->af;
2e4e6a17 1247
025d93d1 1248 mutex_lock(&xt[af].mutex);
715cf35a 1249 return seq_list_start(&net->xt.tables[af], *pos);
025d93d1 1250}
2e4e6a17 1251
025d93d1
AD
1252static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1253{
715cf35a 1254 struct xt_names_priv *priv = seq->private;
1218854a 1255 struct net *net = seq_file_net(seq);
76108cea 1256 u_int8_t af = priv->af;
2e4e6a17 1257
715cf35a 1258 return seq_list_next(v, &net->xt.tables[af], pos);
2e4e6a17
HW
1259}
1260
025d93d1 1261static void xt_table_seq_stop(struct seq_file *seq, void *v)
2e4e6a17 1262{
715cf35a 1263 struct xt_names_priv *priv = seq->private;
76108cea 1264 u_int8_t af = priv->af;
2e4e6a17 1265
025d93d1
AD
1266 mutex_unlock(&xt[af].mutex);
1267}
2e4e6a17 1268
025d93d1
AD
1269static int xt_table_seq_show(struct seq_file *seq, void *v)
1270{
1271 struct xt_table *table = list_entry(v, struct xt_table, list);
2e4e6a17 1272
025d93d1
AD
1273 if (strlen(table->name))
1274 return seq_printf(seq, "%s\n", table->name);
1275 else
1276 return 0;
1277}
601e68e1 1278
025d93d1
AD
1279static const struct seq_operations xt_table_seq_ops = {
1280 .start = xt_table_seq_start,
1281 .next = xt_table_seq_next,
1282 .stop = xt_table_seq_stop,
1283 .show = xt_table_seq_show,
1284};
1285
1286static int xt_table_open(struct inode *inode, struct file *file)
1287{
1288 int ret;
715cf35a 1289 struct xt_names_priv *priv;
025d93d1 1290
715cf35a
AD
1291 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1292 sizeof(struct xt_names_priv));
025d93d1 1293 if (!ret) {
715cf35a 1294 priv = ((struct seq_file *)file->private_data)->private;
d9dda78b 1295 priv->af = (unsigned long)PDE_DATA(inode);
025d93d1
AD
1296 }
1297 return ret;
2e4e6a17
HW
1298}
1299
025d93d1
AD
1300static const struct file_operations xt_table_ops = {
1301 .owner = THIS_MODULE,
1302 .open = xt_table_open,
1303 .read = seq_read,
1304 .llseek = seq_lseek,
0e93bb94 1305 .release = seq_release_net,
025d93d1
AD
1306};
1307
eb132205
JE
1308/*
1309 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1310 * the multi-AF mutexes.
1311 */
1312struct nf_mttg_trav {
1313 struct list_head *head, *curr;
1314 uint8_t class, nfproto;
1315};
1316
1317enum {
1318 MTTG_TRAV_INIT,
1319 MTTG_TRAV_NFP_UNSPEC,
1320 MTTG_TRAV_NFP_SPEC,
1321 MTTG_TRAV_DONE,
1322};
1323
1324static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1325 bool is_target)
2e4e6a17 1326{
eb132205
JE
1327 static const uint8_t next_class[] = {
1328 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1329 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1330 };
1331 struct nf_mttg_trav *trav = seq->private;
1332
1333 switch (trav->class) {
1334 case MTTG_TRAV_INIT:
1335 trav->class = MTTG_TRAV_NFP_UNSPEC;
1336 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1337 trav->head = trav->curr = is_target ?
1338 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1339 break;
1340 case MTTG_TRAV_NFP_UNSPEC:
1341 trav->curr = trav->curr->next;
1342 if (trav->curr != trav->head)
1343 break;
1344 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1345 mutex_lock(&xt[trav->nfproto].mutex);
1346 trav->head = trav->curr = is_target ?
1347 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1348 trav->class = next_class[trav->class];
1349 break;
1350 case MTTG_TRAV_NFP_SPEC:
1351 trav->curr = trav->curr->next;
1352 if (trav->curr != trav->head)
1353 break;
1354 /* fallthru, _stop will unlock */
1355 default:
1356 return NULL;
1357 }
2e4e6a17 1358
eb132205
JE
1359 if (ppos != NULL)
1360 ++*ppos;
1361 return trav;
025d93d1 1362}
601e68e1 1363
eb132205
JE
1364static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1365 bool is_target)
025d93d1 1366{
eb132205
JE
1367 struct nf_mttg_trav *trav = seq->private;
1368 unsigned int j;
2e4e6a17 1369
eb132205
JE
1370 trav->class = MTTG_TRAV_INIT;
1371 for (j = 0; j < *pos; ++j)
1372 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1373 return NULL;
1374 return trav;
2e4e6a17
HW
1375}
1376
eb132205 1377static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
2e4e6a17 1378{
eb132205
JE
1379 struct nf_mttg_trav *trav = seq->private;
1380
1381 switch (trav->class) {
1382 case MTTG_TRAV_NFP_UNSPEC:
1383 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1384 break;
1385 case MTTG_TRAV_NFP_SPEC:
1386 mutex_unlock(&xt[trav->nfproto].mutex);
1387 break;
1388 }
1389}
2e4e6a17 1390
eb132205
JE
1391static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1392{
1393 return xt_mttg_seq_start(seq, pos, false);
2e4e6a17
HW
1394}
1395
eb132205 1396static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
2e4e6a17 1397{
eb132205
JE
1398 return xt_mttg_seq_next(seq, v, ppos, false);
1399}
2e4e6a17 1400
eb132205
JE
1401static int xt_match_seq_show(struct seq_file *seq, void *v)
1402{
1403 const struct nf_mttg_trav *trav = seq->private;
1404 const struct xt_match *match;
1405
1406 switch (trav->class) {
1407 case MTTG_TRAV_NFP_UNSPEC:
1408 case MTTG_TRAV_NFP_SPEC:
1409 if (trav->curr == trav->head)
1410 return 0;
1411 match = list_entry(trav->curr, struct xt_match, list);
1412 return (*match->name == '\0') ? 0 :
1413 seq_printf(seq, "%s\n", match->name);
1414 }
1415 return 0;
2e4e6a17
HW
1416}
1417
025d93d1
AD
1418static const struct seq_operations xt_match_seq_ops = {
1419 .start = xt_match_seq_start,
1420 .next = xt_match_seq_next,
eb132205 1421 .stop = xt_mttg_seq_stop,
025d93d1 1422 .show = xt_match_seq_show,
2e4e6a17
HW
1423};
1424
025d93d1 1425static int xt_match_open(struct inode *inode, struct file *file)
2e4e6a17 1426{
eb132205 1427 struct nf_mttg_trav *trav;
772476df
RJ
1428 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1429 if (!trav)
eb132205 1430 return -ENOMEM;
2e4e6a17 1431
d9dda78b 1432 trav->nfproto = (unsigned long)PDE_DATA(inode);
eb132205 1433 return 0;
025d93d1
AD
1434}
1435
1436static const struct file_operations xt_match_ops = {
1437 .owner = THIS_MODULE,
1438 .open = xt_match_open,
1439 .read = seq_read,
1440 .llseek = seq_lseek,
eb132205 1441 .release = seq_release_private,
025d93d1 1442};
2e4e6a17 1443
025d93d1
AD
1444static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1445{
eb132205 1446 return xt_mttg_seq_start(seq, pos, true);
025d93d1
AD
1447}
1448
eb132205 1449static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
025d93d1 1450{
eb132205 1451 return xt_mttg_seq_next(seq, v, ppos, true);
025d93d1
AD
1452}
1453
1454static int xt_target_seq_show(struct seq_file *seq, void *v)
1455{
eb132205
JE
1456 const struct nf_mttg_trav *trav = seq->private;
1457 const struct xt_target *target;
1458
1459 switch (trav->class) {
1460 case MTTG_TRAV_NFP_UNSPEC:
1461 case MTTG_TRAV_NFP_SPEC:
1462 if (trav->curr == trav->head)
1463 return 0;
1464 target = list_entry(trav->curr, struct xt_target, list);
1465 return (*target->name == '\0') ? 0 :
1466 seq_printf(seq, "%s\n", target->name);
1467 }
1468 return 0;
025d93d1
AD
1469}
1470
1471static const struct seq_operations xt_target_seq_ops = {
1472 .start = xt_target_seq_start,
1473 .next = xt_target_seq_next,
eb132205 1474 .stop = xt_mttg_seq_stop,
025d93d1
AD
1475 .show = xt_target_seq_show,
1476};
1477
1478static int xt_target_open(struct inode *inode, struct file *file)
1479{
eb132205 1480 struct nf_mttg_trav *trav;
772476df
RJ
1481 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1482 if (!trav)
eb132205 1483 return -ENOMEM;
025d93d1 1484
d9dda78b 1485 trav->nfproto = (unsigned long)PDE_DATA(inode);
eb132205 1486 return 0;
2e4e6a17
HW
1487}
1488
025d93d1 1489static const struct file_operations xt_target_ops = {
2e4e6a17 1490 .owner = THIS_MODULE,
025d93d1 1491 .open = xt_target_open,
2e4e6a17
HW
1492 .read = seq_read,
1493 .llseek = seq_lseek,
eb132205 1494 .release = seq_release_private,
2e4e6a17
HW
1495};
1496
1497#define FORMAT_TABLES "_tables_names"
1498#define FORMAT_MATCHES "_tables_matches"
1499#define FORMAT_TARGETS "_tables_targets"
1500
1501#endif /* CONFIG_PROC_FS */
1502
2b95efe7
JE
1503/**
1504 * xt_hook_link - set up hooks for a new table
1505 * @table: table with metadata needed to set up hooks
1506 * @fn: Hook function
1507 *
1508 * This function will take care of creating and registering the necessary
1509 * Netfilter hooks for XT tables.
1510 */
1511struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1512{
1513 unsigned int hook_mask = table->valid_hooks;
1514 uint8_t i, num_hooks = hweight32(hook_mask);
1515 uint8_t hooknum;
1516 struct nf_hook_ops *ops;
1517 int ret;
1518
1519 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1520 if (ops == NULL)
1521 return ERR_PTR(-ENOMEM);
1522
1523 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1524 hook_mask >>= 1, ++hooknum) {
1525 if (!(hook_mask & 1))
1526 continue;
1527 ops[i].hook = fn;
1528 ops[i].owner = table->me;
1529 ops[i].pf = table->af;
1530 ops[i].hooknum = hooknum;
1531 ops[i].priority = table->priority;
1532 ++i;
1533 }
1534
1535 ret = nf_register_hooks(ops, num_hooks);
1536 if (ret < 0) {
1537 kfree(ops);
1538 return ERR_PTR(ret);
1539 }
1540
1541 return ops;
1542}
1543EXPORT_SYMBOL_GPL(xt_hook_link);
1544
1545/**
1546 * xt_hook_unlink - remove hooks for a table
1547 * @ops: nf_hook_ops array as returned by nf_hook_link
1548 * @hook_mask: the very same mask that was passed to nf_hook_link
1549 */
1550void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1551{
1552 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1553 kfree(ops);
1554}
1555EXPORT_SYMBOL_GPL(xt_hook_unlink);
1556
76108cea 1557int xt_proto_init(struct net *net, u_int8_t af)
2e4e6a17
HW
1558{
1559#ifdef CONFIG_PROC_FS
1560 char buf[XT_FUNCTION_MAXNAMELEN];
1561 struct proc_dir_entry *proc;
1562#endif
1563
7e9c6eeb 1564 if (af >= ARRAY_SIZE(xt_prefix))
2e4e6a17
HW
1565 return -EINVAL;
1566
1567
1568#ifdef CONFIG_PROC_FS
ce18afe5 1569 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 1570 strlcat(buf, FORMAT_TABLES, sizeof(buf));
8b169240
DL
1571 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1572 (void *)(unsigned long)af);
2e4e6a17
HW
1573 if (!proc)
1574 goto out;
2e4e6a17 1575
ce18afe5 1576 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 1577 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
8b169240
DL
1578 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1579 (void *)(unsigned long)af);
2e4e6a17
HW
1580 if (!proc)
1581 goto out_remove_tables;
2e4e6a17 1582
ce18afe5 1583 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 1584 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
8b169240
DL
1585 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1586 (void *)(unsigned long)af);
2e4e6a17
HW
1587 if (!proc)
1588 goto out_remove_matches;
2e4e6a17
HW
1589#endif
1590
1591 return 0;
1592
1593#ifdef CONFIG_PROC_FS
1594out_remove_matches:
ce18afe5 1595 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 1596 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
ece31ffd 1597 remove_proc_entry(buf, net->proc_net);
2e4e6a17
HW
1598
1599out_remove_tables:
ce18afe5 1600 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 1601 strlcat(buf, FORMAT_TABLES, sizeof(buf));
ece31ffd 1602 remove_proc_entry(buf, net->proc_net);
2e4e6a17
HW
1603out:
1604 return -1;
1605#endif
1606}
1607EXPORT_SYMBOL_GPL(xt_proto_init);
1608
76108cea 1609void xt_proto_fini(struct net *net, u_int8_t af)
2e4e6a17
HW
1610{
1611#ifdef CONFIG_PROC_FS
1612 char buf[XT_FUNCTION_MAXNAMELEN];
1613
ce18afe5 1614 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 1615 strlcat(buf, FORMAT_TABLES, sizeof(buf));
ece31ffd 1616 remove_proc_entry(buf, net->proc_net);
2e4e6a17 1617
ce18afe5 1618 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 1619 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
ece31ffd 1620 remove_proc_entry(buf, net->proc_net);
2e4e6a17 1621
ce18afe5 1622 strlcpy(buf, xt_prefix[af], sizeof(buf));
2e4e6a17 1623 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
ece31ffd 1624 remove_proc_entry(buf, net->proc_net);
2e4e6a17
HW
1625#endif /*CONFIG_PROC_FS*/
1626}
1627EXPORT_SYMBOL_GPL(xt_proto_fini);
1628
8d870052
AD
1629static int __net_init xt_net_init(struct net *net)
1630{
1631 int i;
1632
7e9c6eeb 1633 for (i = 0; i < NFPROTO_NUMPROTO; i++)
8d870052
AD
1634 INIT_LIST_HEAD(&net->xt.tables[i]);
1635 return 0;
1636}
1637
1638static struct pernet_operations xt_net_ops = {
1639 .init = xt_net_init,
1640};
2e4e6a17
HW
1641
1642static int __init xt_init(void)
1643{
942e4a2b
SH
1644 unsigned int i;
1645 int rv;
1646
1647 for_each_possible_cpu(i) {
7f5c6d4f 1648 seqcount_init(&per_cpu(xt_recseq, i));
942e4a2b 1649 }
2e4e6a17 1650
7e9c6eeb 1651 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
2e4e6a17
HW
1652 if (!xt)
1653 return -ENOMEM;
1654
7e9c6eeb 1655 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
9e19bb6d 1656 mutex_init(&xt[i].mutex);
2722971c
DM
1657#ifdef CONFIG_COMPAT
1658 mutex_init(&xt[i].compat_mutex);
255d0dc3 1659 xt[i].compat_tab = NULL;
2722971c 1660#endif
2e4e6a17
HW
1661 INIT_LIST_HEAD(&xt[i].target);
1662 INIT_LIST_HEAD(&xt[i].match);
2e4e6a17 1663 }
8d870052
AD
1664 rv = register_pernet_subsys(&xt_net_ops);
1665 if (rv < 0)
1666 kfree(xt);
1667 return rv;
2e4e6a17
HW
1668}
1669
1670static void __exit xt_fini(void)
1671{
8d870052 1672 unregister_pernet_subsys(&xt_net_ops);
2e4e6a17
HW
1673 kfree(xt);
1674}
1675
1676module_init(xt_init);
1677module_exit(xt_fini);
1678