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>
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))
41 unsigned int offset
; /* offset in kernel */
42 int delta
; /* delta in 32bit user land */
47 struct list_head match
;
48 struct list_head target
;
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[] */
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. */
72 xt_register_target(struct xt_target
*target
)
74 u_int8_t af
= target
->family
;
77 ret
= mutex_lock_interruptible(&xt
[af
].mutex
);
80 list_add(&target
->list
, &xt
[af
].target
);
81 mutex_unlock(&xt
[af
].mutex
);
84 EXPORT_SYMBOL(xt_register_target
);
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
);
98 xt_register_targets(struct xt_target
*target
, unsigned int n
)
103 for (i
= 0; i
< n
; i
++) {
104 err
= xt_register_target(&target
[i
]);
112 xt_unregister_targets(target
, i
);
115 EXPORT_SYMBOL(xt_register_targets
);
118 xt_unregister_targets(struct xt_target
*target
, unsigned int n
)
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
;
131 ret
= mutex_lock_interruptible(&xt
[af
].mutex
);
135 list_add(&match
->list
, &xt
[af
].match
);
136 mutex_unlock(&xt
[af
].mutex
);
140 EXPORT_SYMBOL(xt_register_match
);
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
)
159 for (i
= 0; i
< n
; i
++) {
160 err
= xt_register_match(&match
[i
]);
168 xt_unregister_matches(match
, i
);
171 EXPORT_SYMBOL(xt_register_matches
);
174 xt_unregister_matches(struct xt_match
*match
, unsigned int n
)
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
)
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
);
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
);
216 EXPORT_SYMBOL(xt_find_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
)
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
);
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
);
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
;
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
)
283 if (af
!= NFPROTO_UNSPEC
&& !have_rev
)
284 return match_revfn(NFPROTO_UNSPEC
, name
, revision
, bestp
);
289 static int target_revfn(u8 af
, const char *name
, u8 revision
, int *bestp
)
291 const struct xt_target
*t
;
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
)
303 if (af
!= NFPROTO_UNSPEC
&& !have_rev
)
304 return target_revfn(NFPROTO_UNSPEC
, name
, revision
, bestp
);
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
,
313 int have_rev
, best
= -1;
315 if (mutex_lock_interruptible(&xt
[af
].mutex
) != 0) {
320 have_rev
= target_revfn(af
, name
, revision
, &best
);
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. */
333 *err
= -EPROTONOSUPPORT
;
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",
350 for (i
= 0; i
< ARRAY_SIZE(names
); ++i
) {
351 if (!(mask
& (1 << i
)))
353 res
= snprintf(p
, size
, "%s%s", np
? "/" : "", names
[i
]);
364 int xt_check_match(struct xt_mtchk_param
*par
,
365 unsigned int size
, u_int8_t proto
, bool inv_proto
)
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
);
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
);
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 "
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
));
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
,
405 if (par
->match
->checkentry
!= NULL
) {
406 ret
= par
->match
->checkentry(par
);
410 /* Flag up potential errors. */
415 EXPORT_SYMBOL_GPL(xt_check_match
);
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
) {
425 xp
->compat_tab
= vmalloc(sizeof(struct compat_delta
) * xp
->number
);
431 if (xp
->cur
>= xp
->number
)
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
;
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
;
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
)
462 else if (offset
< tmp
[mid
].offset
)
465 return mid
? tmp
[mid
- 1].delta
: 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
;
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
,
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
;
495 memcpy(m
, cm
, sizeof(*cm
));
496 if (match
->compat_from_user
)
497 match
->compat_from_user(m
->data
, cm
->data
);
499 memcpy(m
->data
, cm
->data
, msize
- sizeof(*cm
));
500 pad
= XT_ALIGN(match
->matchsize
) - match
->matchsize
;
502 memset(m
->data
+ match
->matchsize
, 0, pad
);
505 m
->u
.user
.match_size
= msize
;
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))
527 if (match
->compat_to_user
) {
528 if (match
->compat_to_user((void __user
*)cm
->data
, m
->data
))
531 if (copy_to_user(cm
->data
, m
->data
, msize
- sizeof(*cm
)))
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
)
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
);
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
);
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 "
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
));
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
,
578 if (par
->target
->checkentry
!= NULL
) {
579 ret
= par
->target
->checkentry(par
);
583 /* Flag up potential errors. */
588 EXPORT_SYMBOL_GPL(xt_check_target
);
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
,
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
;
607 memcpy(t
, ct
, sizeof(*ct
));
608 if (target
->compat_from_user
)
609 target
->compat_from_user(t
->data
, ct
->data
);
611 memcpy(t
->data
, ct
->data
, tsize
- sizeof(*ct
));
612 pad
= XT_ALIGN(target
->targetsize
) - target
->targetsize
;
614 memset(t
->data
+ target
->targetsize
, 0, pad
);
617 t
->u
.user
.target_size
= 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))
638 if (target
->compat_to_user
) {
639 if (target
->compat_to_user((void __user
*)ct
->data
, t
->data
))
642 if (copy_to_user(ct
->data
, t
->data
, tsize
- sizeof(*ct
)))
650 EXPORT_SYMBOL_GPL(xt_compat_target_to_user
);
653 struct xt_table_info
*xt_alloc_table_info(unsigned int size
)
655 struct xt_table_info
*newinfo
;
658 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
659 if ((SMP_ALIGN(size
) >> PAGE_SHIFT
) + 2 > totalram_pages
)
662 newinfo
= kzalloc(XT_TABLE_INFO_SZ
, GFP_KERNEL
);
666 newinfo
->size
= size
;
668 for_each_possible_cpu(cpu
) {
669 if (size
<= PAGE_SIZE
)
670 newinfo
->entries
[cpu
] = kmalloc_node(size
,
674 newinfo
->entries
[cpu
] = vmalloc_node(size
,
677 if (newinfo
->entries
[cpu
] == NULL
) {
678 xt_free_table_info(newinfo
);
685 EXPORT_SYMBOL(xt_alloc_table_info
);
687 void xt_free_table_info(struct xt_table_info
*info
)
691 for_each_possible_cpu(cpu
) {
692 if (info
->size
<= PAGE_SIZE
)
693 kfree(info
->entries
[cpu
]);
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
]);
703 for_each_possible_cpu(cpu
)
704 kfree(info
->jumpstack
[cpu
]);
708 if (sizeof(void **) * nr_cpu_ids
> PAGE_SIZE
)
709 vfree(info
->jumpstack
);
711 kfree(info
->jumpstack
);
713 free_percpu(info
->stackptr
);
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
,
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
))
731 mutex_unlock(&xt
[af
].mutex
);
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
);
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
);
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
)
764 i
->stackptr
= alloc_percpu(unsigned int);
765 if (i
->stackptr
== NULL
)
768 size
= sizeof(void **) * nr_cpu_ids
;
769 if (size
> PAGE_SIZE
)
770 i
->jumpstack
= vmalloc(size
);
772 i
->jumpstack
= kmalloc(size
, GFP_KERNEL
);
773 if (i
->jumpstack
== NULL
)
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
,
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.
798 struct xt_table_info
*
799 xt_replace_table(struct xt_table
*table
,
800 unsigned int num_counters
,
801 struct xt_table_info
*newinfo
,
804 struct xt_table_info
*private;
807 ret
= xt_jumpstack_alloc(newinfo
);
813 /* Do the substitution. */
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
);
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.
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
)
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
);
857 ret
= mutex_lock_interruptible(&xt
[table
->af
].mutex
);
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) {
869 /* Simplifies replace_table code. */
870 table
->private = bootstrap
;
872 if (!xt_replace_table(table
, 0, newinfo
, &ret
))
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
);
886 mutex_unlock(&xt
[table
->af
].mutex
);
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
);
906 EXPORT_SYMBOL_GPL(xt_unregister_table
);
908 #ifdef CONFIG_PROC_FS
909 struct xt_names_priv
{
910 struct seq_net_private p
;
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
);
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
)
960 struct xt_names_priv
*priv
;
962 ret
= seq_open_net(inode
, file
, &xt_table_seq_ops
,
963 sizeof(struct xt_names_priv
));
965 priv
= ((struct seq_file
*)file
->private_data
)->private;
966 priv
->af
= (unsigned long)PDE(inode
)->data
;
971 static const struct file_operations xt_table_ops
= {
972 .owner
= THIS_MODULE
,
973 .open
= xt_table_open
,
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
;
990 MTTG_TRAV_NFP_UNSPEC
,
995 static void *xt_mttg_seq_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
,
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
;
1011 case MTTG_TRAV_NFP_UNSPEC
:
1012 trav
->curr
= trav
->curr
->next
;
1013 if (trav
->curr
!= trav
->head
)
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];
1021 case MTTG_TRAV_NFP_SPEC
:
1022 trav
->curr
= trav
->curr
->next
;
1023 if (trav
->curr
!= trav
->head
)
1025 /* fallthru, _stop will unlock */
1035 static void *xt_mttg_seq_start(struct seq_file
*seq
, loff_t
*pos
,
1038 struct nf_mttg_trav
*trav
= seq
->private;
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
)
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
);
1056 case MTTG_TRAV_NFP_SPEC
:
1057 mutex_unlock(&xt
[trav
->nfproto
].mutex
);
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
)
1082 match
= list_entry(trav
->curr
, struct xt_match
, list
);
1083 return (*match
->name
== '\0') ? 0 :
1084 seq_printf(seq
, "%s\n", match
->name
);
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
;
1102 trav
= kmalloc(sizeof(*trav
), GFP_KERNEL
);
1106 ret
= seq_open(file
, &xt_match_seq_ops
);
1112 seq
= file
->private_data
;
1113 seq
->private = trav
;
1114 trav
->nfproto
= (unsigned long)PDE(inode
)->data
;
1118 static const struct file_operations xt_match_ops
= {
1119 .owner
= THIS_MODULE
,
1120 .open
= xt_match_open
,
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
)
1146 target
= list_entry(trav
->curr
, struct xt_target
, list
);
1147 return (*target
->name
== '\0') ? 0 :
1148 seq_printf(seq
, "%s\n", target
->name
);
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
;
1166 trav
= kmalloc(sizeof(*trav
), GFP_KERNEL
);
1170 ret
= seq_open(file
, &xt_target_seq_ops
);
1176 seq
= file
->private_data
;
1177 seq
->private = trav
;
1178 trav
->nfproto
= (unsigned long)PDE(inode
)->data
;
1182 static const struct file_operations xt_target_ops
= {
1183 .owner
= THIS_MODULE
,
1184 .open
= xt_target_open
,
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
);
1209 struct nf_hook_ops
*ops
;
1212 ops
= kmalloc(sizeof(*ops
) * num_hooks
, GFP_KERNEL
);
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))
1221 ops
[i
].owner
= table
->me
;
1222 ops
[i
].pf
= table
->af
;
1223 ops
[i
].hooknum
= hooknum
;
1224 ops
[i
].priority
= table
->priority
;
1228 ret
= nf_register_hooks(ops
, num_hooks
);
1231 return ERR_PTR(ret
);
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
));
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
;
1257 if (af
>= ARRAY_SIZE(xt_prefix
))
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
);
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
);
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
);
1281 goto out_remove_matches
;
1286 #ifdef CONFIG_PROC_FS
1288 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1289 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
1290 proc_net_remove(net
, buf
);
1293 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1294 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
1295 proc_net_remove(net
, buf
);
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
)
1326 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++)
1327 INIT_LIST_HEAD(&net
->xt
.tables
[i
]);
1331 static struct pernet_operations xt_net_ops
= {
1332 .init
= xt_net_init
,
1335 static int __init
xt_init(void)
1340 for_each_possible_cpu(i
) {
1341 struct xt_info_lock
*lock
= &per_cpu(xt_info_locks
, i
);
1342 spin_lock_init(&lock
->lock
);
1346 xt
= kmalloc(sizeof(struct xt_af
) * NFPROTO_NUMPROTO
, GFP_KERNEL
);
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
;
1356 INIT_LIST_HEAD(&xt
[i
].target
);
1357 INIT_LIST_HEAD(&xt
[i
].match
);
1359 rv
= register_pernet_subsys(&xt_net_ops
);
1365 static void __exit
xt_fini(void)
1367 unregister_pernet_subsys(&xt_net_ops
);
1371 module_init(xt_init
);
1372 module_exit(xt_fini
);