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