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