netfilter: x_table: speedup compat operations
[linux-2.6/btrfs-unstable.git] / net / netfilter / x_tables.c
blobee5de3af510a7a8c341d82e084f83627f0461281
1 /*
2 * x_tables core - Backend for {ip,ip6,arp}_tables
4 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
6 * Based on existing ip_tables code which is
7 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
8 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/socket.h>
18 #include <linux/net.h>
19 #include <linux/proc_fs.h>
20 #include <linux/seq_file.h>
21 #include <linux/string.h>
22 #include <linux/vmalloc.h>
23 #include <linux/mutex.h>
24 #include <linux/mm.h>
25 #include <linux/slab.h>
26 #include <net/net_namespace.h>
28 #include <linux/netfilter/x_tables.h>
29 #include <linux/netfilter_arp.h>
30 #include <linux/netfilter_ipv4/ip_tables.h>
31 #include <linux/netfilter_ipv6/ip6_tables.h>
32 #include <linux/netfilter_arp/arp_tables.h>
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
36 MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
38 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
40 struct compat_delta {
41 unsigned int offset; /* offset in kernel */
42 int delta; /* delta in 32bit user land */
45 struct xt_af {
46 struct mutex mutex;
47 struct list_head match;
48 struct list_head target;
49 #ifdef CONFIG_COMPAT
50 struct mutex compat_mutex;
51 struct compat_delta *compat_tab;
52 unsigned int number; /* number of slots in compat_tab[] */
53 unsigned int cur; /* number of used slots in compat_tab[] */
54 #endif
57 static struct xt_af *xt;
59 static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
60 [NFPROTO_UNSPEC] = "x",
61 [NFPROTO_IPV4] = "ip",
62 [NFPROTO_ARP] = "arp",
63 [NFPROTO_BRIDGE] = "eb",
64 [NFPROTO_IPV6] = "ip6",
67 /* Allow this many total (re)entries. */
68 static const unsigned int xt_jumpstack_multiplier = 2;
70 /* Registration hooks for targets. */
71 int
72 xt_register_target(struct xt_target *target)
74 u_int8_t af = target->family;
75 int ret;
77 ret = mutex_lock_interruptible(&xt[af].mutex);
78 if (ret != 0)
79 return ret;
80 list_add(&target->list, &xt[af].target);
81 mutex_unlock(&xt[af].mutex);
82 return ret;
84 EXPORT_SYMBOL(xt_register_target);
86 void
87 xt_unregister_target(struct xt_target *target)
89 u_int8_t af = target->family;
91 mutex_lock(&xt[af].mutex);
92 list_del(&target->list);
93 mutex_unlock(&xt[af].mutex);
95 EXPORT_SYMBOL(xt_unregister_target);
97 int
98 xt_register_targets(struct xt_target *target, unsigned int n)
100 unsigned int i;
101 int err = 0;
103 for (i = 0; i < n; i++) {
104 err = xt_register_target(&target[i]);
105 if (err)
106 goto err;
108 return err;
110 err:
111 if (i > 0)
112 xt_unregister_targets(target, i);
113 return err;
115 EXPORT_SYMBOL(xt_register_targets);
117 void
118 xt_unregister_targets(struct xt_target *target, unsigned int n)
120 while (n-- > 0)
121 xt_unregister_target(&target[n]);
123 EXPORT_SYMBOL(xt_unregister_targets);
126 xt_register_match(struct xt_match *match)
128 u_int8_t af = match->family;
129 int ret;
131 ret = mutex_lock_interruptible(&xt[af].mutex);
132 if (ret != 0)
133 return ret;
135 list_add(&match->list, &xt[af].match);
136 mutex_unlock(&xt[af].mutex);
138 return ret;
140 EXPORT_SYMBOL(xt_register_match);
142 void
143 xt_unregister_match(struct xt_match *match)
145 u_int8_t af = match->family;
147 mutex_lock(&xt[af].mutex);
148 list_del(&match->list);
149 mutex_unlock(&xt[af].mutex);
151 EXPORT_SYMBOL(xt_unregister_match);
154 xt_register_matches(struct xt_match *match, unsigned int n)
156 unsigned int i;
157 int err = 0;
159 for (i = 0; i < n; i++) {
160 err = xt_register_match(&match[i]);
161 if (err)
162 goto err;
164 return err;
166 err:
167 if (i > 0)
168 xt_unregister_matches(match, i);
169 return err;
171 EXPORT_SYMBOL(xt_register_matches);
173 void
174 xt_unregister_matches(struct xt_match *match, unsigned int n)
176 while (n-- > 0)
177 xt_unregister_match(&match[n]);
179 EXPORT_SYMBOL(xt_unregister_matches);
183 * These are weird, but module loading must not be done with mutex
184 * held (since they will register), and we have to have a single
185 * function to use try_then_request_module().
188 /* Find match, grabs ref. Returns ERR_PTR() on error. */
189 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
191 struct xt_match *m;
192 int err = 0;
194 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
195 return ERR_PTR(-EINTR);
197 list_for_each_entry(m, &xt[af].match, list) {
198 if (strcmp(m->name, name) == 0) {
199 if (m->revision == revision) {
200 if (try_module_get(m->me)) {
201 mutex_unlock(&xt[af].mutex);
202 return m;
204 } else
205 err = -EPROTOTYPE; /* Found something. */
208 mutex_unlock(&xt[af].mutex);
210 if (af != NFPROTO_UNSPEC)
211 /* Try searching again in the family-independent list */
212 return xt_find_match(NFPROTO_UNSPEC, name, revision);
214 return ERR_PTR(err);
216 EXPORT_SYMBOL(xt_find_match);
218 struct xt_match *
219 xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
221 struct xt_match *match;
223 match = try_then_request_module(xt_find_match(nfproto, name, revision),
224 "%st_%s", xt_prefix[nfproto], name);
225 return (match != NULL) ? match : ERR_PTR(-ENOENT);
227 EXPORT_SYMBOL_GPL(xt_request_find_match);
229 /* Find target, grabs ref. Returns ERR_PTR() on error. */
230 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
232 struct xt_target *t;
233 int err = 0;
235 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
236 return ERR_PTR(-EINTR);
238 list_for_each_entry(t, &xt[af].target, list) {
239 if (strcmp(t->name, name) == 0) {
240 if (t->revision == revision) {
241 if (try_module_get(t->me)) {
242 mutex_unlock(&xt[af].mutex);
243 return t;
245 } else
246 err = -EPROTOTYPE; /* Found something. */
249 mutex_unlock(&xt[af].mutex);
251 if (af != NFPROTO_UNSPEC)
252 /* Try searching again in the family-independent list */
253 return xt_find_target(NFPROTO_UNSPEC, name, revision);
255 return ERR_PTR(err);
257 EXPORT_SYMBOL(xt_find_target);
259 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
261 struct xt_target *target;
263 target = try_then_request_module(xt_find_target(af, name, revision),
264 "%st_%s", xt_prefix[af], name);
265 return (target != NULL) ? target : ERR_PTR(-ENOENT);
267 EXPORT_SYMBOL_GPL(xt_request_find_target);
269 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
271 const struct xt_match *m;
272 int have_rev = 0;
274 list_for_each_entry(m, &xt[af].match, list) {
275 if (strcmp(m->name, name) == 0) {
276 if (m->revision > *bestp)
277 *bestp = m->revision;
278 if (m->revision == revision)
279 have_rev = 1;
283 if (af != NFPROTO_UNSPEC && !have_rev)
284 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
286 return have_rev;
289 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
291 const struct xt_target *t;
292 int have_rev = 0;
294 list_for_each_entry(t, &xt[af].target, list) {
295 if (strcmp(t->name, name) == 0) {
296 if (t->revision > *bestp)
297 *bestp = t->revision;
298 if (t->revision == revision)
299 have_rev = 1;
303 if (af != NFPROTO_UNSPEC && !have_rev)
304 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
306 return have_rev;
309 /* Returns true or false (if no such extension at all) */
310 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
311 int *err)
313 int have_rev, best = -1;
315 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
316 *err = -EINTR;
317 return 1;
319 if (target == 1)
320 have_rev = target_revfn(af, name, revision, &best);
321 else
322 have_rev = match_revfn(af, name, revision, &best);
323 mutex_unlock(&xt[af].mutex);
325 /* Nothing at all? Return 0 to try loading module. */
326 if (best == -1) {
327 *err = -ENOENT;
328 return 0;
331 *err = best;
332 if (!have_rev)
333 *err = -EPROTONOSUPPORT;
334 return 1;
336 EXPORT_SYMBOL_GPL(xt_find_revision);
338 static char *textify_hooks(char *buf, size_t size, unsigned int mask)
340 static const char *const names[] = {
341 "PREROUTING", "INPUT", "FORWARD",
342 "OUTPUT", "POSTROUTING", "BROUTING",
344 unsigned int i;
345 char *p = buf;
346 bool np = false;
347 int res;
349 *p = '\0';
350 for (i = 0; i < ARRAY_SIZE(names); ++i) {
351 if (!(mask & (1 << i)))
352 continue;
353 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
354 if (res > 0) {
355 size -= res;
356 p += res;
358 np = true;
361 return buf;
364 int xt_check_match(struct xt_mtchk_param *par,
365 unsigned int size, u_int8_t proto, bool inv_proto)
367 int ret;
369 if (XT_ALIGN(par->match->matchsize) != size &&
370 par->match->matchsize != -1) {
372 * ebt_among is exempt from centralized matchsize checking
373 * because it uses a dynamic-size data set.
375 pr_err("%s_tables: %s.%u match: invalid size "
376 "%u (kernel) != (user) %u\n",
377 xt_prefix[par->family], par->match->name,
378 par->match->revision,
379 XT_ALIGN(par->match->matchsize), size);
380 return -EINVAL;
382 if (par->match->table != NULL &&
383 strcmp(par->match->table, par->table) != 0) {
384 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
385 xt_prefix[par->family], par->match->name,
386 par->match->table, par->table);
387 return -EINVAL;
389 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
390 char used[64], allow[64];
392 pr_err("%s_tables: %s match: used from hooks %s, but only "
393 "valid from %s\n",
394 xt_prefix[par->family], par->match->name,
395 textify_hooks(used, sizeof(used), par->hook_mask),
396 textify_hooks(allow, sizeof(allow), par->match->hooks));
397 return -EINVAL;
399 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
400 pr_err("%s_tables: %s match: only valid for protocol %u\n",
401 xt_prefix[par->family], par->match->name,
402 par->match->proto);
403 return -EINVAL;
405 if (par->match->checkentry != NULL) {
406 ret = par->match->checkentry(par);
407 if (ret < 0)
408 return ret;
409 else if (ret > 0)
410 /* Flag up potential errors. */
411 return -EIO;
413 return 0;
415 EXPORT_SYMBOL_GPL(xt_check_match);
417 #ifdef CONFIG_COMPAT
418 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
420 struct xt_af *xp = &xt[af];
422 if (!xp->compat_tab) {
423 if (!xp->number)
424 return -EINVAL;
425 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
426 if (!xp->compat_tab)
427 return -ENOMEM;
428 xp->cur = 0;
431 if (xp->cur >= xp->number)
432 return -EINVAL;
434 if (xp->cur)
435 delta += xp->compat_tab[xp->cur - 1].delta;
436 xp->compat_tab[xp->cur].offset = offset;
437 xp->compat_tab[xp->cur].delta = delta;
438 xp->cur++;
439 return 0;
441 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
443 void xt_compat_flush_offsets(u_int8_t af)
445 if (xt[af].compat_tab) {
446 vfree(xt[af].compat_tab);
447 xt[af].compat_tab = NULL;
448 xt[af].number = 0;
451 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
453 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
455 struct compat_delta *tmp = xt[af].compat_tab;
456 int mid, left = 0, right = xt[af].cur - 1;
458 while (left <= right) {
459 mid = (left + right) >> 1;
460 if (offset > tmp[mid].offset)
461 left = mid + 1;
462 else if (offset < tmp[mid].offset)
463 right = mid - 1;
464 else
465 return mid ? tmp[mid - 1].delta : 0;
467 WARN_ON_ONCE(1);
468 return 0;
470 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
472 void xt_compat_init_offsets(u_int8_t af, unsigned int number)
474 xt[af].number = number;
475 xt[af].cur = 0;
477 EXPORT_SYMBOL(xt_compat_init_offsets);
479 int xt_compat_match_offset(const struct xt_match *match)
481 u_int16_t csize = match->compatsize ? : match->matchsize;
482 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
484 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
486 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
487 unsigned int *size)
489 const struct xt_match *match = m->u.kernel.match;
490 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
491 int pad, off = xt_compat_match_offset(match);
492 u_int16_t msize = cm->u.user.match_size;
494 m = *dstptr;
495 memcpy(m, cm, sizeof(*cm));
496 if (match->compat_from_user)
497 match->compat_from_user(m->data, cm->data);
498 else
499 memcpy(m->data, cm->data, msize - sizeof(*cm));
500 pad = XT_ALIGN(match->matchsize) - match->matchsize;
501 if (pad > 0)
502 memset(m->data + match->matchsize, 0, pad);
504 msize += off;
505 m->u.user.match_size = msize;
507 *size += off;
508 *dstptr += msize;
509 return 0;
511 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
513 int xt_compat_match_to_user(const struct xt_entry_match *m,
514 void __user **dstptr, unsigned int *size)
516 const struct xt_match *match = m->u.kernel.match;
517 struct compat_xt_entry_match __user *cm = *dstptr;
518 int off = xt_compat_match_offset(match);
519 u_int16_t msize = m->u.user.match_size - off;
521 if (copy_to_user(cm, m, sizeof(*cm)) ||
522 put_user(msize, &cm->u.user.match_size) ||
523 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
524 strlen(m->u.kernel.match->name) + 1))
525 return -EFAULT;
527 if (match->compat_to_user) {
528 if (match->compat_to_user((void __user *)cm->data, m->data))
529 return -EFAULT;
530 } else {
531 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
532 return -EFAULT;
535 *size -= off;
536 *dstptr += msize;
537 return 0;
539 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
540 #endif /* CONFIG_COMPAT */
542 int xt_check_target(struct xt_tgchk_param *par,
543 unsigned int size, u_int8_t proto, bool inv_proto)
545 int ret;
547 if (XT_ALIGN(par->target->targetsize) != size) {
548 pr_err("%s_tables: %s.%u target: invalid size "
549 "%u (kernel) != (user) %u\n",
550 xt_prefix[par->family], par->target->name,
551 par->target->revision,
552 XT_ALIGN(par->target->targetsize), size);
553 return -EINVAL;
555 if (par->target->table != NULL &&
556 strcmp(par->target->table, par->table) != 0) {
557 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
558 xt_prefix[par->family], par->target->name,
559 par->target->table, par->table);
560 return -EINVAL;
562 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
563 char used[64], allow[64];
565 pr_err("%s_tables: %s target: used from hooks %s, but only "
566 "usable from %s\n",
567 xt_prefix[par->family], par->target->name,
568 textify_hooks(used, sizeof(used), par->hook_mask),
569 textify_hooks(allow, sizeof(allow), par->target->hooks));
570 return -EINVAL;
572 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
573 pr_err("%s_tables: %s target: only valid for protocol %u\n",
574 xt_prefix[par->family], par->target->name,
575 par->target->proto);
576 return -EINVAL;
578 if (par->target->checkentry != NULL) {
579 ret = par->target->checkentry(par);
580 if (ret < 0)
581 return ret;
582 else if (ret > 0)
583 /* Flag up potential errors. */
584 return -EIO;
586 return 0;
588 EXPORT_SYMBOL_GPL(xt_check_target);
590 #ifdef CONFIG_COMPAT
591 int xt_compat_target_offset(const struct xt_target *target)
593 u_int16_t csize = target->compatsize ? : target->targetsize;
594 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
596 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
598 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
599 unsigned int *size)
601 const struct xt_target *target = t->u.kernel.target;
602 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
603 int pad, off = xt_compat_target_offset(target);
604 u_int16_t tsize = ct->u.user.target_size;
606 t = *dstptr;
607 memcpy(t, ct, sizeof(*ct));
608 if (target->compat_from_user)
609 target->compat_from_user(t->data, ct->data);
610 else
611 memcpy(t->data, ct->data, tsize - sizeof(*ct));
612 pad = XT_ALIGN(target->targetsize) - target->targetsize;
613 if (pad > 0)
614 memset(t->data + target->targetsize, 0, pad);
616 tsize += off;
617 t->u.user.target_size = tsize;
619 *size += off;
620 *dstptr += tsize;
622 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
624 int xt_compat_target_to_user(const struct xt_entry_target *t,
625 void __user **dstptr, unsigned int *size)
627 const struct xt_target *target = t->u.kernel.target;
628 struct compat_xt_entry_target __user *ct = *dstptr;
629 int off = xt_compat_target_offset(target);
630 u_int16_t tsize = t->u.user.target_size - off;
632 if (copy_to_user(ct, t, sizeof(*ct)) ||
633 put_user(tsize, &ct->u.user.target_size) ||
634 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
635 strlen(t->u.kernel.target->name) + 1))
636 return -EFAULT;
638 if (target->compat_to_user) {
639 if (target->compat_to_user((void __user *)ct->data, t->data))
640 return -EFAULT;
641 } else {
642 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
643 return -EFAULT;
646 *size -= off;
647 *dstptr += tsize;
648 return 0;
650 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
651 #endif
653 struct xt_table_info *xt_alloc_table_info(unsigned int size)
655 struct xt_table_info *newinfo;
656 int cpu;
658 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
659 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
660 return NULL;
662 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
663 if (!newinfo)
664 return NULL;
666 newinfo->size = size;
668 for_each_possible_cpu(cpu) {
669 if (size <= PAGE_SIZE)
670 newinfo->entries[cpu] = kmalloc_node(size,
671 GFP_KERNEL,
672 cpu_to_node(cpu));
673 else
674 newinfo->entries[cpu] = vmalloc_node(size,
675 cpu_to_node(cpu));
677 if (newinfo->entries[cpu] == NULL) {
678 xt_free_table_info(newinfo);
679 return NULL;
683 return newinfo;
685 EXPORT_SYMBOL(xt_alloc_table_info);
687 void xt_free_table_info(struct xt_table_info *info)
689 int cpu;
691 for_each_possible_cpu(cpu) {
692 if (info->size <= PAGE_SIZE)
693 kfree(info->entries[cpu]);
694 else
695 vfree(info->entries[cpu]);
698 if (info->jumpstack != NULL) {
699 if (sizeof(void *) * info->stacksize > PAGE_SIZE) {
700 for_each_possible_cpu(cpu)
701 vfree(info->jumpstack[cpu]);
702 } else {
703 for_each_possible_cpu(cpu)
704 kfree(info->jumpstack[cpu]);
708 if (sizeof(void **) * nr_cpu_ids > PAGE_SIZE)
709 vfree(info->jumpstack);
710 else
711 kfree(info->jumpstack);
713 free_percpu(info->stackptr);
715 kfree(info);
717 EXPORT_SYMBOL(xt_free_table_info);
719 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
720 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
721 const char *name)
723 struct xt_table *t;
725 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
726 return ERR_PTR(-EINTR);
728 list_for_each_entry(t, &net->xt.tables[af], list)
729 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
730 return t;
731 mutex_unlock(&xt[af].mutex);
732 return NULL;
734 EXPORT_SYMBOL_GPL(xt_find_table_lock);
736 void xt_table_unlock(struct xt_table *table)
738 mutex_unlock(&xt[table->af].mutex);
740 EXPORT_SYMBOL_GPL(xt_table_unlock);
742 #ifdef CONFIG_COMPAT
743 void xt_compat_lock(u_int8_t af)
745 mutex_lock(&xt[af].compat_mutex);
747 EXPORT_SYMBOL_GPL(xt_compat_lock);
749 void xt_compat_unlock(u_int8_t af)
751 mutex_unlock(&xt[af].compat_mutex);
753 EXPORT_SYMBOL_GPL(xt_compat_unlock);
754 #endif
756 DEFINE_PER_CPU(struct xt_info_lock, xt_info_locks);
757 EXPORT_PER_CPU_SYMBOL_GPL(xt_info_locks);
759 static int xt_jumpstack_alloc(struct xt_table_info *i)
761 unsigned int size;
762 int cpu;
764 i->stackptr = alloc_percpu(unsigned int);
765 if (i->stackptr == NULL)
766 return -ENOMEM;
768 size = sizeof(void **) * nr_cpu_ids;
769 if (size > PAGE_SIZE)
770 i->jumpstack = vmalloc(size);
771 else
772 i->jumpstack = kmalloc(size, GFP_KERNEL);
773 if (i->jumpstack == NULL)
774 return -ENOMEM;
775 memset(i->jumpstack, 0, size);
777 i->stacksize *= xt_jumpstack_multiplier;
778 size = sizeof(void *) * i->stacksize;
779 for_each_possible_cpu(cpu) {
780 if (size > PAGE_SIZE)
781 i->jumpstack[cpu] = vmalloc_node(size,
782 cpu_to_node(cpu));
783 else
784 i->jumpstack[cpu] = kmalloc_node(size,
785 GFP_KERNEL, cpu_to_node(cpu));
786 if (i->jumpstack[cpu] == NULL)
788 * Freeing will be done later on by the callers. The
789 * chain is: xt_replace_table -> __do_replace ->
790 * do_replace -> xt_free_table_info.
792 return -ENOMEM;
795 return 0;
798 struct xt_table_info *
799 xt_replace_table(struct xt_table *table,
800 unsigned int num_counters,
801 struct xt_table_info *newinfo,
802 int *error)
804 struct xt_table_info *private;
805 int ret;
807 ret = xt_jumpstack_alloc(newinfo);
808 if (ret < 0) {
809 *error = ret;
810 return NULL;
813 /* Do the substitution. */
814 local_bh_disable();
815 private = table->private;
817 /* Check inside lock: is the old number correct? */
818 if (num_counters != private->number) {
819 pr_debug("num_counters != table->private->number (%u/%u)\n",
820 num_counters, private->number);
821 local_bh_enable();
822 *error = -EAGAIN;
823 return NULL;
826 table->private = newinfo;
827 newinfo->initial_entries = private->initial_entries;
830 * Even though table entries have now been swapped, other CPU's
831 * may still be using the old entries. This is okay, because
832 * resynchronization happens because of the locking done
833 * during the get_counters() routine.
835 local_bh_enable();
837 return private;
839 EXPORT_SYMBOL_GPL(xt_replace_table);
841 struct xt_table *xt_register_table(struct net *net,
842 const struct xt_table *input_table,
843 struct xt_table_info *bootstrap,
844 struct xt_table_info *newinfo)
846 int ret;
847 struct xt_table_info *private;
848 struct xt_table *t, *table;
850 /* Don't add one object to multiple lists. */
851 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
852 if (!table) {
853 ret = -ENOMEM;
854 goto out;
857 ret = mutex_lock_interruptible(&xt[table->af].mutex);
858 if (ret != 0)
859 goto out_free;
861 /* Don't autoload: we'd eat our tail... */
862 list_for_each_entry(t, &net->xt.tables[table->af], list) {
863 if (strcmp(t->name, table->name) == 0) {
864 ret = -EEXIST;
865 goto unlock;
869 /* Simplifies replace_table code. */
870 table->private = bootstrap;
872 if (!xt_replace_table(table, 0, newinfo, &ret))
873 goto unlock;
875 private = table->private;
876 pr_debug("table->private->number = %u\n", private->number);
878 /* save number of initial entries */
879 private->initial_entries = private->number;
881 list_add(&table->list, &net->xt.tables[table->af]);
882 mutex_unlock(&xt[table->af].mutex);
883 return table;
885 unlock:
886 mutex_unlock(&xt[table->af].mutex);
887 out_free:
888 kfree(table);
889 out:
890 return ERR_PTR(ret);
892 EXPORT_SYMBOL_GPL(xt_register_table);
894 void *xt_unregister_table(struct xt_table *table)
896 struct xt_table_info *private;
898 mutex_lock(&xt[table->af].mutex);
899 private = table->private;
900 list_del(&table->list);
901 mutex_unlock(&xt[table->af].mutex);
902 kfree(table);
904 return private;
906 EXPORT_SYMBOL_GPL(xt_unregister_table);
908 #ifdef CONFIG_PROC_FS
909 struct xt_names_priv {
910 struct seq_net_private p;
911 u_int8_t af;
913 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
915 struct xt_names_priv *priv = seq->private;
916 struct net *net = seq_file_net(seq);
917 u_int8_t af = priv->af;
919 mutex_lock(&xt[af].mutex);
920 return seq_list_start(&net->xt.tables[af], *pos);
923 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
925 struct xt_names_priv *priv = seq->private;
926 struct net *net = seq_file_net(seq);
927 u_int8_t af = priv->af;
929 return seq_list_next(v, &net->xt.tables[af], pos);
932 static void xt_table_seq_stop(struct seq_file *seq, void *v)
934 struct xt_names_priv *priv = seq->private;
935 u_int8_t af = priv->af;
937 mutex_unlock(&xt[af].mutex);
940 static int xt_table_seq_show(struct seq_file *seq, void *v)
942 struct xt_table *table = list_entry(v, struct xt_table, list);
944 if (strlen(table->name))
945 return seq_printf(seq, "%s\n", table->name);
946 else
947 return 0;
950 static const struct seq_operations xt_table_seq_ops = {
951 .start = xt_table_seq_start,
952 .next = xt_table_seq_next,
953 .stop = xt_table_seq_stop,
954 .show = xt_table_seq_show,
957 static int xt_table_open(struct inode *inode, struct file *file)
959 int ret;
960 struct xt_names_priv *priv;
962 ret = seq_open_net(inode, file, &xt_table_seq_ops,
963 sizeof(struct xt_names_priv));
964 if (!ret) {
965 priv = ((struct seq_file *)file->private_data)->private;
966 priv->af = (unsigned long)PDE(inode)->data;
968 return ret;
971 static const struct file_operations xt_table_ops = {
972 .owner = THIS_MODULE,
973 .open = xt_table_open,
974 .read = seq_read,
975 .llseek = seq_lseek,
976 .release = seq_release_net,
980 * Traverse state for ip{,6}_{tables,matches} for helping crossing
981 * the multi-AF mutexes.
983 struct nf_mttg_trav {
984 struct list_head *head, *curr;
985 uint8_t class, nfproto;
988 enum {
989 MTTG_TRAV_INIT,
990 MTTG_TRAV_NFP_UNSPEC,
991 MTTG_TRAV_NFP_SPEC,
992 MTTG_TRAV_DONE,
995 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
996 bool is_target)
998 static const uint8_t next_class[] = {
999 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1000 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1002 struct nf_mttg_trav *trav = seq->private;
1004 switch (trav->class) {
1005 case MTTG_TRAV_INIT:
1006 trav->class = MTTG_TRAV_NFP_UNSPEC;
1007 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1008 trav->head = trav->curr = is_target ?
1009 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1010 break;
1011 case MTTG_TRAV_NFP_UNSPEC:
1012 trav->curr = trav->curr->next;
1013 if (trav->curr != trav->head)
1014 break;
1015 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1016 mutex_lock(&xt[trav->nfproto].mutex);
1017 trav->head = trav->curr = is_target ?
1018 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1019 trav->class = next_class[trav->class];
1020 break;
1021 case MTTG_TRAV_NFP_SPEC:
1022 trav->curr = trav->curr->next;
1023 if (trav->curr != trav->head)
1024 break;
1025 /* fallthru, _stop will unlock */
1026 default:
1027 return NULL;
1030 if (ppos != NULL)
1031 ++*ppos;
1032 return trav;
1035 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1036 bool is_target)
1038 struct nf_mttg_trav *trav = seq->private;
1039 unsigned int j;
1041 trav->class = MTTG_TRAV_INIT;
1042 for (j = 0; j < *pos; ++j)
1043 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1044 return NULL;
1045 return trav;
1048 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1050 struct nf_mttg_trav *trav = seq->private;
1052 switch (trav->class) {
1053 case MTTG_TRAV_NFP_UNSPEC:
1054 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1055 break;
1056 case MTTG_TRAV_NFP_SPEC:
1057 mutex_unlock(&xt[trav->nfproto].mutex);
1058 break;
1062 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1064 return xt_mttg_seq_start(seq, pos, false);
1067 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1069 return xt_mttg_seq_next(seq, v, ppos, false);
1072 static int xt_match_seq_show(struct seq_file *seq, void *v)
1074 const struct nf_mttg_trav *trav = seq->private;
1075 const struct xt_match *match;
1077 switch (trav->class) {
1078 case MTTG_TRAV_NFP_UNSPEC:
1079 case MTTG_TRAV_NFP_SPEC:
1080 if (trav->curr == trav->head)
1081 return 0;
1082 match = list_entry(trav->curr, struct xt_match, list);
1083 return (*match->name == '\0') ? 0 :
1084 seq_printf(seq, "%s\n", match->name);
1086 return 0;
1089 static const struct seq_operations xt_match_seq_ops = {
1090 .start = xt_match_seq_start,
1091 .next = xt_match_seq_next,
1092 .stop = xt_mttg_seq_stop,
1093 .show = xt_match_seq_show,
1096 static int xt_match_open(struct inode *inode, struct file *file)
1098 struct seq_file *seq;
1099 struct nf_mttg_trav *trav;
1100 int ret;
1102 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1103 if (trav == NULL)
1104 return -ENOMEM;
1106 ret = seq_open(file, &xt_match_seq_ops);
1107 if (ret < 0) {
1108 kfree(trav);
1109 return ret;
1112 seq = file->private_data;
1113 seq->private = trav;
1114 trav->nfproto = (unsigned long)PDE(inode)->data;
1115 return 0;
1118 static const struct file_operations xt_match_ops = {
1119 .owner = THIS_MODULE,
1120 .open = xt_match_open,
1121 .read = seq_read,
1122 .llseek = seq_lseek,
1123 .release = seq_release_private,
1126 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1128 return xt_mttg_seq_start(seq, pos, true);
1131 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1133 return xt_mttg_seq_next(seq, v, ppos, true);
1136 static int xt_target_seq_show(struct seq_file *seq, void *v)
1138 const struct nf_mttg_trav *trav = seq->private;
1139 const struct xt_target *target;
1141 switch (trav->class) {
1142 case MTTG_TRAV_NFP_UNSPEC:
1143 case MTTG_TRAV_NFP_SPEC:
1144 if (trav->curr == trav->head)
1145 return 0;
1146 target = list_entry(trav->curr, struct xt_target, list);
1147 return (*target->name == '\0') ? 0 :
1148 seq_printf(seq, "%s\n", target->name);
1150 return 0;
1153 static const struct seq_operations xt_target_seq_ops = {
1154 .start = xt_target_seq_start,
1155 .next = xt_target_seq_next,
1156 .stop = xt_mttg_seq_stop,
1157 .show = xt_target_seq_show,
1160 static int xt_target_open(struct inode *inode, struct file *file)
1162 struct seq_file *seq;
1163 struct nf_mttg_trav *trav;
1164 int ret;
1166 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1167 if (trav == NULL)
1168 return -ENOMEM;
1170 ret = seq_open(file, &xt_target_seq_ops);
1171 if (ret < 0) {
1172 kfree(trav);
1173 return ret;
1176 seq = file->private_data;
1177 seq->private = trav;
1178 trav->nfproto = (unsigned long)PDE(inode)->data;
1179 return 0;
1182 static const struct file_operations xt_target_ops = {
1183 .owner = THIS_MODULE,
1184 .open = xt_target_open,
1185 .read = seq_read,
1186 .llseek = seq_lseek,
1187 .release = seq_release_private,
1190 #define FORMAT_TABLES "_tables_names"
1191 #define FORMAT_MATCHES "_tables_matches"
1192 #define FORMAT_TARGETS "_tables_targets"
1194 #endif /* CONFIG_PROC_FS */
1197 * xt_hook_link - set up hooks for a new table
1198 * @table: table with metadata needed to set up hooks
1199 * @fn: Hook function
1201 * This function will take care of creating and registering the necessary
1202 * Netfilter hooks for XT tables.
1204 struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1206 unsigned int hook_mask = table->valid_hooks;
1207 uint8_t i, num_hooks = hweight32(hook_mask);
1208 uint8_t hooknum;
1209 struct nf_hook_ops *ops;
1210 int ret;
1212 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1213 if (ops == NULL)
1214 return ERR_PTR(-ENOMEM);
1216 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1217 hook_mask >>= 1, ++hooknum) {
1218 if (!(hook_mask & 1))
1219 continue;
1220 ops[i].hook = fn;
1221 ops[i].owner = table->me;
1222 ops[i].pf = table->af;
1223 ops[i].hooknum = hooknum;
1224 ops[i].priority = table->priority;
1225 ++i;
1228 ret = nf_register_hooks(ops, num_hooks);
1229 if (ret < 0) {
1230 kfree(ops);
1231 return ERR_PTR(ret);
1234 return ops;
1236 EXPORT_SYMBOL_GPL(xt_hook_link);
1239 * xt_hook_unlink - remove hooks for a table
1240 * @ops: nf_hook_ops array as returned by nf_hook_link
1241 * @hook_mask: the very same mask that was passed to nf_hook_link
1243 void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1245 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1246 kfree(ops);
1248 EXPORT_SYMBOL_GPL(xt_hook_unlink);
1250 int xt_proto_init(struct net *net, u_int8_t af)
1252 #ifdef CONFIG_PROC_FS
1253 char buf[XT_FUNCTION_MAXNAMELEN];
1254 struct proc_dir_entry *proc;
1255 #endif
1257 if (af >= ARRAY_SIZE(xt_prefix))
1258 return -EINVAL;
1261 #ifdef CONFIG_PROC_FS
1262 strlcpy(buf, xt_prefix[af], sizeof(buf));
1263 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1264 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1265 (void *)(unsigned long)af);
1266 if (!proc)
1267 goto out;
1269 strlcpy(buf, xt_prefix[af], sizeof(buf));
1270 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1271 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1272 (void *)(unsigned long)af);
1273 if (!proc)
1274 goto out_remove_tables;
1276 strlcpy(buf, xt_prefix[af], sizeof(buf));
1277 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1278 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1279 (void *)(unsigned long)af);
1280 if (!proc)
1281 goto out_remove_matches;
1282 #endif
1284 return 0;
1286 #ifdef CONFIG_PROC_FS
1287 out_remove_matches:
1288 strlcpy(buf, xt_prefix[af], sizeof(buf));
1289 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1290 proc_net_remove(net, buf);
1292 out_remove_tables:
1293 strlcpy(buf, xt_prefix[af], sizeof(buf));
1294 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1295 proc_net_remove(net, buf);
1296 out:
1297 return -1;
1298 #endif
1300 EXPORT_SYMBOL_GPL(xt_proto_init);
1302 void xt_proto_fini(struct net *net, u_int8_t af)
1304 #ifdef CONFIG_PROC_FS
1305 char buf[XT_FUNCTION_MAXNAMELEN];
1307 strlcpy(buf, xt_prefix[af], sizeof(buf));
1308 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1309 proc_net_remove(net, buf);
1311 strlcpy(buf, xt_prefix[af], sizeof(buf));
1312 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1313 proc_net_remove(net, buf);
1315 strlcpy(buf, xt_prefix[af], sizeof(buf));
1316 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1317 proc_net_remove(net, buf);
1318 #endif /*CONFIG_PROC_FS*/
1320 EXPORT_SYMBOL_GPL(xt_proto_fini);
1322 static int __net_init xt_net_init(struct net *net)
1324 int i;
1326 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1327 INIT_LIST_HEAD(&net->xt.tables[i]);
1328 return 0;
1331 static struct pernet_operations xt_net_ops = {
1332 .init = xt_net_init,
1335 static int __init xt_init(void)
1337 unsigned int i;
1338 int rv;
1340 for_each_possible_cpu(i) {
1341 struct xt_info_lock *lock = &per_cpu(xt_info_locks, i);
1342 spin_lock_init(&lock->lock);
1343 lock->readers = 0;
1346 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1347 if (!xt)
1348 return -ENOMEM;
1350 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1351 mutex_init(&xt[i].mutex);
1352 #ifdef CONFIG_COMPAT
1353 mutex_init(&xt[i].compat_mutex);
1354 xt[i].compat_tab = NULL;
1355 #endif
1356 INIT_LIST_HEAD(&xt[i].target);
1357 INIT_LIST_HEAD(&xt[i].match);
1359 rv = register_pernet_subsys(&xt_net_ops);
1360 if (rv < 0)
1361 kfree(xt);
1362 return rv;
1365 static void __exit xt_fini(void)
1367 unregister_pernet_subsys(&xt_net_ops);
1368 kfree(xt);
1371 module_init(xt_init);
1372 module_exit(xt_fini);