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 <linux/audit.h>
27 #include <net/net_namespace.h>
29 #include <linux/netfilter/x_tables.h>
30 #include <linux/netfilter_arp.h>
31 #include <linux/netfilter_ipv4/ip_tables.h>
32 #include <linux/netfilter_ipv6/ip6_tables.h>
33 #include <linux/netfilter_arp/arp_tables.h>
35 MODULE_LICENSE("GPL");
36 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
37 MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
39 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
42 unsigned int offset
; /* offset in kernel */
43 int delta
; /* delta in 32bit user land */
48 struct list_head match
;
49 struct list_head target
;
51 struct mutex compat_mutex
;
52 struct compat_delta
*compat_tab
;
53 unsigned int number
; /* number of slots in compat_tab[] */
54 unsigned int cur
; /* number of used slots in compat_tab[] */
58 static struct xt_af
*xt
;
60 static const char *const xt_prefix
[NFPROTO_NUMPROTO
] = {
61 [NFPROTO_UNSPEC
] = "x",
62 [NFPROTO_IPV4
] = "ip",
63 [NFPROTO_ARP
] = "arp",
64 [NFPROTO_BRIDGE
] = "eb",
65 [NFPROTO_IPV6
] = "ip6",
68 /* Allow this many total (re)entries. */
69 static const unsigned int xt_jumpstack_multiplier
= 2;
71 /* Registration hooks for targets. */
73 xt_register_target(struct xt_target
*target
)
75 u_int8_t af
= target
->family
;
78 ret
= mutex_lock_interruptible(&xt
[af
].mutex
);
81 list_add(&target
->list
, &xt
[af
].target
);
82 mutex_unlock(&xt
[af
].mutex
);
85 EXPORT_SYMBOL(xt_register_target
);
88 xt_unregister_target(struct xt_target
*target
)
90 u_int8_t af
= target
->family
;
92 mutex_lock(&xt
[af
].mutex
);
93 list_del(&target
->list
);
94 mutex_unlock(&xt
[af
].mutex
);
96 EXPORT_SYMBOL(xt_unregister_target
);
99 xt_register_targets(struct xt_target
*target
, unsigned int n
)
104 for (i
= 0; i
< n
; i
++) {
105 err
= xt_register_target(&target
[i
]);
113 xt_unregister_targets(target
, i
);
116 EXPORT_SYMBOL(xt_register_targets
);
119 xt_unregister_targets(struct xt_target
*target
, unsigned int n
)
122 xt_unregister_target(&target
[n
]);
124 EXPORT_SYMBOL(xt_unregister_targets
);
127 xt_register_match(struct xt_match
*match
)
129 u_int8_t af
= match
->family
;
132 ret
= mutex_lock_interruptible(&xt
[af
].mutex
);
136 list_add(&match
->list
, &xt
[af
].match
);
137 mutex_unlock(&xt
[af
].mutex
);
141 EXPORT_SYMBOL(xt_register_match
);
144 xt_unregister_match(struct xt_match
*match
)
146 u_int8_t af
= match
->family
;
148 mutex_lock(&xt
[af
].mutex
);
149 list_del(&match
->list
);
150 mutex_unlock(&xt
[af
].mutex
);
152 EXPORT_SYMBOL(xt_unregister_match
);
155 xt_register_matches(struct xt_match
*match
, unsigned int n
)
160 for (i
= 0; i
< n
; i
++) {
161 err
= xt_register_match(&match
[i
]);
169 xt_unregister_matches(match
, i
);
172 EXPORT_SYMBOL(xt_register_matches
);
175 xt_unregister_matches(struct xt_match
*match
, unsigned int n
)
178 xt_unregister_match(&match
[n
]);
180 EXPORT_SYMBOL(xt_unregister_matches
);
184 * These are weird, but module loading must not be done with mutex
185 * held (since they will register), and we have to have a single
189 /* Find match, grabs ref. Returns ERR_PTR() on error. */
190 struct xt_match
*xt_find_match(u8 af
, const char *name
, u8 revision
)
195 if (mutex_lock_interruptible(&xt
[af
].mutex
) != 0)
196 return ERR_PTR(-EINTR
);
198 list_for_each_entry(m
, &xt
[af
].match
, list
) {
199 if (strcmp(m
->name
, name
) == 0) {
200 if (m
->revision
== revision
) {
201 if (try_module_get(m
->me
)) {
202 mutex_unlock(&xt
[af
].mutex
);
206 err
= -EPROTOTYPE
; /* Found something. */
209 mutex_unlock(&xt
[af
].mutex
);
211 if (af
!= NFPROTO_UNSPEC
)
212 /* Try searching again in the family-independent list */
213 return xt_find_match(NFPROTO_UNSPEC
, name
, revision
);
217 EXPORT_SYMBOL(xt_find_match
);
220 xt_request_find_match(uint8_t nfproto
, const char *name
, uint8_t revision
)
222 struct xt_match
*match
;
224 match
= xt_find_match(nfproto
, name
, revision
);
226 request_module("%st_%s", xt_prefix
[nfproto
], name
);
227 match
= xt_find_match(nfproto
, name
, revision
);
232 EXPORT_SYMBOL_GPL(xt_request_find_match
);
234 /* Find target, grabs ref. Returns ERR_PTR() on error. */
235 struct xt_target
*xt_find_target(u8 af
, const char *name
, u8 revision
)
240 if (mutex_lock_interruptible(&xt
[af
].mutex
) != 0)
241 return ERR_PTR(-EINTR
);
243 list_for_each_entry(t
, &xt
[af
].target
, list
) {
244 if (strcmp(t
->name
, name
) == 0) {
245 if (t
->revision
== revision
) {
246 if (try_module_get(t
->me
)) {
247 mutex_unlock(&xt
[af
].mutex
);
251 err
= -EPROTOTYPE
; /* Found something. */
254 mutex_unlock(&xt
[af
].mutex
);
256 if (af
!= NFPROTO_UNSPEC
)
257 /* Try searching again in the family-independent list */
258 return xt_find_target(NFPROTO_UNSPEC
, name
, revision
);
262 EXPORT_SYMBOL(xt_find_target
);
264 struct xt_target
*xt_request_find_target(u8 af
, const char *name
, u8 revision
)
266 struct xt_target
*target
;
268 target
= xt_find_target(af
, name
, revision
);
269 if (IS_ERR(target
)) {
270 request_module("%st_%s", xt_prefix
[af
], name
);
271 target
= xt_find_target(af
, name
, revision
);
276 EXPORT_SYMBOL_GPL(xt_request_find_target
);
278 static int match_revfn(u8 af
, const char *name
, u8 revision
, int *bestp
)
280 const struct xt_match
*m
;
283 list_for_each_entry(m
, &xt
[af
].match
, list
) {
284 if (strcmp(m
->name
, name
) == 0) {
285 if (m
->revision
> *bestp
)
286 *bestp
= m
->revision
;
287 if (m
->revision
== revision
)
292 if (af
!= NFPROTO_UNSPEC
&& !have_rev
)
293 return match_revfn(NFPROTO_UNSPEC
, name
, revision
, bestp
);
298 static int target_revfn(u8 af
, const char *name
, u8 revision
, int *bestp
)
300 const struct xt_target
*t
;
303 list_for_each_entry(t
, &xt
[af
].target
, list
) {
304 if (strcmp(t
->name
, name
) == 0) {
305 if (t
->revision
> *bestp
)
306 *bestp
= t
->revision
;
307 if (t
->revision
== revision
)
312 if (af
!= NFPROTO_UNSPEC
&& !have_rev
)
313 return target_revfn(NFPROTO_UNSPEC
, name
, revision
, bestp
);
318 /* Returns true or false (if no such extension at all) */
319 int xt_find_revision(u8 af
, const char *name
, u8 revision
, int target
,
322 int have_rev
, best
= -1;
324 if (mutex_lock_interruptible(&xt
[af
].mutex
) != 0) {
329 have_rev
= target_revfn(af
, name
, revision
, &best
);
331 have_rev
= match_revfn(af
, name
, revision
, &best
);
332 mutex_unlock(&xt
[af
].mutex
);
334 /* Nothing at all? Return 0 to try loading module. */
342 *err
= -EPROTONOSUPPORT
;
345 EXPORT_SYMBOL_GPL(xt_find_revision
);
347 static char *textify_hooks(char *buf
, size_t size
, unsigned int mask
)
349 static const char *const names
[] = {
350 "PREROUTING", "INPUT", "FORWARD",
351 "OUTPUT", "POSTROUTING", "BROUTING",
359 for (i
= 0; i
< ARRAY_SIZE(names
); ++i
) {
360 if (!(mask
& (1 << i
)))
362 res
= snprintf(p
, size
, "%s%s", np
? "/" : "", names
[i
]);
373 int xt_check_match(struct xt_mtchk_param
*par
,
374 unsigned int size
, u_int8_t proto
, bool inv_proto
)
378 if (XT_ALIGN(par
->match
->matchsize
) != size
&&
379 par
->match
->matchsize
!= -1) {
381 * ebt_among is exempt from centralized matchsize checking
382 * because it uses a dynamic-size data set.
384 pr_err("%s_tables: %s.%u match: invalid size "
385 "%u (kernel) != (user) %u\n",
386 xt_prefix
[par
->family
], par
->match
->name
,
387 par
->match
->revision
,
388 XT_ALIGN(par
->match
->matchsize
), size
);
391 if (par
->match
->table
!= NULL
&&
392 strcmp(par
->match
->table
, par
->table
) != 0) {
393 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
394 xt_prefix
[par
->family
], par
->match
->name
,
395 par
->match
->table
, par
->table
);
398 if (par
->match
->hooks
&& (par
->hook_mask
& ~par
->match
->hooks
) != 0) {
399 char used
[64], allow
[64];
401 pr_err("%s_tables: %s match: used from hooks %s, but only "
403 xt_prefix
[par
->family
], par
->match
->name
,
404 textify_hooks(used
, sizeof(used
), par
->hook_mask
),
405 textify_hooks(allow
, sizeof(allow
), par
->match
->hooks
));
408 if (par
->match
->proto
&& (par
->match
->proto
!= proto
|| inv_proto
)) {
409 pr_err("%s_tables: %s match: only valid for protocol %u\n",
410 xt_prefix
[par
->family
], par
->match
->name
,
414 if (par
->match
->checkentry
!= NULL
) {
415 ret
= par
->match
->checkentry(par
);
419 /* Flag up potential errors. */
424 EXPORT_SYMBOL_GPL(xt_check_match
);
427 int xt_compat_add_offset(u_int8_t af
, unsigned int offset
, int delta
)
429 struct xt_af
*xp
= &xt
[af
];
431 if (!xp
->compat_tab
) {
434 xp
->compat_tab
= vmalloc(sizeof(struct compat_delta
) * xp
->number
);
440 if (xp
->cur
>= xp
->number
)
444 delta
+= xp
->compat_tab
[xp
->cur
- 1].delta
;
445 xp
->compat_tab
[xp
->cur
].offset
= offset
;
446 xp
->compat_tab
[xp
->cur
].delta
= delta
;
450 EXPORT_SYMBOL_GPL(xt_compat_add_offset
);
452 void xt_compat_flush_offsets(u_int8_t af
)
454 if (xt
[af
].compat_tab
) {
455 vfree(xt
[af
].compat_tab
);
456 xt
[af
].compat_tab
= NULL
;
461 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets
);
463 int xt_compat_calc_jump(u_int8_t af
, unsigned int offset
)
465 struct compat_delta
*tmp
= xt
[af
].compat_tab
;
466 int mid
, left
= 0, right
= xt
[af
].cur
- 1;
468 while (left
<= right
) {
469 mid
= (left
+ right
) >> 1;
470 if (offset
> tmp
[mid
].offset
)
472 else if (offset
< tmp
[mid
].offset
)
475 return mid
? tmp
[mid
- 1].delta
: 0;
477 return left
? tmp
[left
- 1].delta
: 0;
479 EXPORT_SYMBOL_GPL(xt_compat_calc_jump
);
481 void xt_compat_init_offsets(u_int8_t af
, unsigned int number
)
483 xt
[af
].number
= number
;
486 EXPORT_SYMBOL(xt_compat_init_offsets
);
488 int xt_compat_match_offset(const struct xt_match
*match
)
490 u_int16_t csize
= match
->compatsize
? : match
->matchsize
;
491 return XT_ALIGN(match
->matchsize
) - COMPAT_XT_ALIGN(csize
);
493 EXPORT_SYMBOL_GPL(xt_compat_match_offset
);
495 int xt_compat_match_from_user(struct xt_entry_match
*m
, void **dstptr
,
498 const struct xt_match
*match
= m
->u
.kernel
.match
;
499 struct compat_xt_entry_match
*cm
= (struct compat_xt_entry_match
*)m
;
500 int pad
, off
= xt_compat_match_offset(match
);
501 u_int16_t msize
= cm
->u
.user
.match_size
;
504 memcpy(m
, cm
, sizeof(*cm
));
505 if (match
->compat_from_user
)
506 match
->compat_from_user(m
->data
, cm
->data
);
508 memcpy(m
->data
, cm
->data
, msize
- sizeof(*cm
));
509 pad
= XT_ALIGN(match
->matchsize
) - match
->matchsize
;
511 memset(m
->data
+ match
->matchsize
, 0, pad
);
514 m
->u
.user
.match_size
= msize
;
520 EXPORT_SYMBOL_GPL(xt_compat_match_from_user
);
522 int xt_compat_match_to_user(const struct xt_entry_match
*m
,
523 void __user
**dstptr
, unsigned int *size
)
525 const struct xt_match
*match
= m
->u
.kernel
.match
;
526 struct compat_xt_entry_match __user
*cm
= *dstptr
;
527 int off
= xt_compat_match_offset(match
);
528 u_int16_t msize
= m
->u
.user
.match_size
- off
;
530 if (copy_to_user(cm
, m
, sizeof(*cm
)) ||
531 put_user(msize
, &cm
->u
.user
.match_size
) ||
532 copy_to_user(cm
->u
.user
.name
, m
->u
.kernel
.match
->name
,
533 strlen(m
->u
.kernel
.match
->name
) + 1))
536 if (match
->compat_to_user
) {
537 if (match
->compat_to_user((void __user
*)cm
->data
, m
->data
))
540 if (copy_to_user(cm
->data
, m
->data
, msize
- sizeof(*cm
)))
548 EXPORT_SYMBOL_GPL(xt_compat_match_to_user
);
549 #endif /* CONFIG_COMPAT */
551 int xt_check_target(struct xt_tgchk_param
*par
,
552 unsigned int size
, u_int8_t proto
, bool inv_proto
)
556 if (XT_ALIGN(par
->target
->targetsize
) != size
) {
557 pr_err("%s_tables: %s.%u target: invalid size "
558 "%u (kernel) != (user) %u\n",
559 xt_prefix
[par
->family
], par
->target
->name
,
560 par
->target
->revision
,
561 XT_ALIGN(par
->target
->targetsize
), size
);
564 if (par
->target
->table
!= NULL
&&
565 strcmp(par
->target
->table
, par
->table
) != 0) {
566 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
567 xt_prefix
[par
->family
], par
->target
->name
,
568 par
->target
->table
, par
->table
);
571 if (par
->target
->hooks
&& (par
->hook_mask
& ~par
->target
->hooks
) != 0) {
572 char used
[64], allow
[64];
574 pr_err("%s_tables: %s target: used from hooks %s, but only "
576 xt_prefix
[par
->family
], par
->target
->name
,
577 textify_hooks(used
, sizeof(used
), par
->hook_mask
),
578 textify_hooks(allow
, sizeof(allow
), par
->target
->hooks
));
581 if (par
->target
->proto
&& (par
->target
->proto
!= proto
|| inv_proto
)) {
582 pr_err("%s_tables: %s target: only valid for protocol %u\n",
583 xt_prefix
[par
->family
], par
->target
->name
,
587 if (par
->target
->checkentry
!= NULL
) {
588 ret
= par
->target
->checkentry(par
);
592 /* Flag up potential errors. */
597 EXPORT_SYMBOL_GPL(xt_check_target
);
600 int xt_compat_target_offset(const struct xt_target
*target
)
602 u_int16_t csize
= target
->compatsize
? : target
->targetsize
;
603 return XT_ALIGN(target
->targetsize
) - COMPAT_XT_ALIGN(csize
);
605 EXPORT_SYMBOL_GPL(xt_compat_target_offset
);
607 void xt_compat_target_from_user(struct xt_entry_target
*t
, void **dstptr
,
610 const struct xt_target
*target
= t
->u
.kernel
.target
;
611 struct compat_xt_entry_target
*ct
= (struct compat_xt_entry_target
*)t
;
612 int pad
, off
= xt_compat_target_offset(target
);
613 u_int16_t tsize
= ct
->u
.user
.target_size
;
616 memcpy(t
, ct
, sizeof(*ct
));
617 if (target
->compat_from_user
)
618 target
->compat_from_user(t
->data
, ct
->data
);
620 memcpy(t
->data
, ct
->data
, tsize
- sizeof(*ct
));
621 pad
= XT_ALIGN(target
->targetsize
) - target
->targetsize
;
623 memset(t
->data
+ target
->targetsize
, 0, pad
);
626 t
->u
.user
.target_size
= tsize
;
631 EXPORT_SYMBOL_GPL(xt_compat_target_from_user
);
633 int xt_compat_target_to_user(const struct xt_entry_target
*t
,
634 void __user
**dstptr
, unsigned int *size
)
636 const struct xt_target
*target
= t
->u
.kernel
.target
;
637 struct compat_xt_entry_target __user
*ct
= *dstptr
;
638 int off
= xt_compat_target_offset(target
);
639 u_int16_t tsize
= t
->u
.user
.target_size
- off
;
641 if (copy_to_user(ct
, t
, sizeof(*ct
)) ||
642 put_user(tsize
, &ct
->u
.user
.target_size
) ||
643 copy_to_user(ct
->u
.user
.name
, t
->u
.kernel
.target
->name
,
644 strlen(t
->u
.kernel
.target
->name
) + 1))
647 if (target
->compat_to_user
) {
648 if (target
->compat_to_user((void __user
*)ct
->data
, t
->data
))
651 if (copy_to_user(ct
->data
, t
->data
, tsize
- sizeof(*ct
)))
659 EXPORT_SYMBOL_GPL(xt_compat_target_to_user
);
662 struct xt_table_info
*xt_alloc_table_info(unsigned int size
)
664 struct xt_table_info
*newinfo
;
667 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
668 if ((SMP_ALIGN(size
) >> PAGE_SHIFT
) + 2 > totalram_pages
)
671 newinfo
= kzalloc(XT_TABLE_INFO_SZ
, GFP_KERNEL
);
675 newinfo
->size
= size
;
677 for_each_possible_cpu(cpu
) {
678 if (size
<= PAGE_SIZE
)
679 newinfo
->entries
[cpu
] = kmalloc_node(size
,
683 newinfo
->entries
[cpu
] = vmalloc_node(size
,
686 if (newinfo
->entries
[cpu
] == NULL
) {
687 xt_free_table_info(newinfo
);
694 EXPORT_SYMBOL(xt_alloc_table_info
);
696 void xt_free_table_info(struct xt_table_info
*info
)
700 for_each_possible_cpu(cpu
) {
701 if (info
->size
<= PAGE_SIZE
)
702 kfree(info
->entries
[cpu
]);
704 vfree(info
->entries
[cpu
]);
707 if (info
->jumpstack
!= NULL
) {
708 if (sizeof(void *) * info
->stacksize
> PAGE_SIZE
) {
709 for_each_possible_cpu(cpu
)
710 vfree(info
->jumpstack
[cpu
]);
712 for_each_possible_cpu(cpu
)
713 kfree(info
->jumpstack
[cpu
]);
717 if (sizeof(void **) * nr_cpu_ids
> PAGE_SIZE
)
718 vfree(info
->jumpstack
);
720 kfree(info
->jumpstack
);
722 free_percpu(info
->stackptr
);
726 EXPORT_SYMBOL(xt_free_table_info
);
728 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
729 struct xt_table
*xt_find_table_lock(struct net
*net
, u_int8_t af
,
734 if (mutex_lock_interruptible(&xt
[af
].mutex
) != 0)
735 return ERR_PTR(-EINTR
);
737 list_for_each_entry(t
, &net
->xt
.tables
[af
], list
)
738 if (strcmp(t
->name
, name
) == 0 && try_module_get(t
->me
))
740 mutex_unlock(&xt
[af
].mutex
);
743 EXPORT_SYMBOL_GPL(xt_find_table_lock
);
745 void xt_table_unlock(struct xt_table
*table
)
747 mutex_unlock(&xt
[table
->af
].mutex
);
749 EXPORT_SYMBOL_GPL(xt_table_unlock
);
752 void xt_compat_lock(u_int8_t af
)
754 mutex_lock(&xt
[af
].compat_mutex
);
756 EXPORT_SYMBOL_GPL(xt_compat_lock
);
758 void xt_compat_unlock(u_int8_t af
)
760 mutex_unlock(&xt
[af
].compat_mutex
);
762 EXPORT_SYMBOL_GPL(xt_compat_unlock
);
765 DEFINE_PER_CPU(seqcount_t
, xt_recseq
);
766 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq
);
768 static int xt_jumpstack_alloc(struct xt_table_info
*i
)
773 i
->stackptr
= alloc_percpu(unsigned int);
774 if (i
->stackptr
== NULL
)
777 size
= sizeof(void **) * nr_cpu_ids
;
778 if (size
> PAGE_SIZE
)
779 i
->jumpstack
= vzalloc(size
);
781 i
->jumpstack
= kzalloc(size
, GFP_KERNEL
);
782 if (i
->jumpstack
== NULL
)
785 i
->stacksize
*= xt_jumpstack_multiplier
;
786 size
= sizeof(void *) * i
->stacksize
;
787 for_each_possible_cpu(cpu
) {
788 if (size
> PAGE_SIZE
)
789 i
->jumpstack
[cpu
] = vmalloc_node(size
,
792 i
->jumpstack
[cpu
] = kmalloc_node(size
,
793 GFP_KERNEL
, cpu_to_node(cpu
));
794 if (i
->jumpstack
[cpu
] == NULL
)
796 * Freeing will be done later on by the callers. The
797 * chain is: xt_replace_table -> __do_replace ->
798 * do_replace -> xt_free_table_info.
806 struct xt_table_info
*
807 xt_replace_table(struct xt_table
*table
,
808 unsigned int num_counters
,
809 struct xt_table_info
*newinfo
,
812 struct xt_table_info
*private;
815 ret
= xt_jumpstack_alloc(newinfo
);
821 /* Do the substitution. */
823 private = table
->private;
825 /* Check inside lock: is the old number correct? */
826 if (num_counters
!= private->number
) {
827 pr_debug("num_counters != table->private->number (%u/%u)\n",
828 num_counters
, private->number
);
834 table
->private = newinfo
;
835 newinfo
->initial_entries
= private->initial_entries
;
838 * Even though table entries have now been swapped, other CPU's
839 * may still be using the old entries. This is okay, because
840 * resynchronization happens because of the locking done
841 * during the get_counters() routine.
847 struct audit_buffer
*ab
;
849 ab
= audit_log_start(current
->audit_context
, GFP_KERNEL
,
850 AUDIT_NETFILTER_CFG
);
852 audit_log_format(ab
, "table=%s family=%u entries=%u",
853 table
->name
, table
->af
,
862 EXPORT_SYMBOL_GPL(xt_replace_table
);
864 struct xt_table
*xt_register_table(struct net
*net
,
865 const struct xt_table
*input_table
,
866 struct xt_table_info
*bootstrap
,
867 struct xt_table_info
*newinfo
)
870 struct xt_table_info
*private;
871 struct xt_table
*t
, *table
;
873 /* Don't add one object to multiple lists. */
874 table
= kmemdup(input_table
, sizeof(struct xt_table
), GFP_KERNEL
);
880 ret
= mutex_lock_interruptible(&xt
[table
->af
].mutex
);
884 /* Don't autoload: we'd eat our tail... */
885 list_for_each_entry(t
, &net
->xt
.tables
[table
->af
], list
) {
886 if (strcmp(t
->name
, table
->name
) == 0) {
892 /* Simplifies replace_table code. */
893 table
->private = bootstrap
;
895 if (!xt_replace_table(table
, 0, newinfo
, &ret
))
898 private = table
->private;
899 pr_debug("table->private->number = %u\n", private->number
);
901 /* save number of initial entries */
902 private->initial_entries
= private->number
;
904 list_add(&table
->list
, &net
->xt
.tables
[table
->af
]);
905 mutex_unlock(&xt
[table
->af
].mutex
);
909 mutex_unlock(&xt
[table
->af
].mutex
);
915 EXPORT_SYMBOL_GPL(xt_register_table
);
917 void *xt_unregister_table(struct xt_table
*table
)
919 struct xt_table_info
*private;
921 mutex_lock(&xt
[table
->af
].mutex
);
922 private = table
->private;
923 list_del(&table
->list
);
924 mutex_unlock(&xt
[table
->af
].mutex
);
929 EXPORT_SYMBOL_GPL(xt_unregister_table
);
931 #ifdef CONFIG_PROC_FS
932 struct xt_names_priv
{
933 struct seq_net_private p
;
936 static void *xt_table_seq_start(struct seq_file
*seq
, loff_t
*pos
)
938 struct xt_names_priv
*priv
= seq
->private;
939 struct net
*net
= seq_file_net(seq
);
940 u_int8_t af
= priv
->af
;
942 mutex_lock(&xt
[af
].mutex
);
943 return seq_list_start(&net
->xt
.tables
[af
], *pos
);
946 static void *xt_table_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
948 struct xt_names_priv
*priv
= seq
->private;
949 struct net
*net
= seq_file_net(seq
);
950 u_int8_t af
= priv
->af
;
952 return seq_list_next(v
, &net
->xt
.tables
[af
], pos
);
955 static void xt_table_seq_stop(struct seq_file
*seq
, void *v
)
957 struct xt_names_priv
*priv
= seq
->private;
958 u_int8_t af
= priv
->af
;
960 mutex_unlock(&xt
[af
].mutex
);
963 static int xt_table_seq_show(struct seq_file
*seq
, void *v
)
965 struct xt_table
*table
= list_entry(v
, struct xt_table
, list
);
967 if (strlen(table
->name
))
968 return seq_printf(seq
, "%s\n", table
->name
);
973 static const struct seq_operations xt_table_seq_ops
= {
974 .start
= xt_table_seq_start
,
975 .next
= xt_table_seq_next
,
976 .stop
= xt_table_seq_stop
,
977 .show
= xt_table_seq_show
,
980 static int xt_table_open(struct inode
*inode
, struct file
*file
)
983 struct xt_names_priv
*priv
;
985 ret
= seq_open_net(inode
, file
, &xt_table_seq_ops
,
986 sizeof(struct xt_names_priv
));
988 priv
= ((struct seq_file
*)file
->private_data
)->private;
989 priv
->af
= (unsigned long)PDE(inode
)->data
;
994 static const struct file_operations xt_table_ops
= {
995 .owner
= THIS_MODULE
,
996 .open
= xt_table_open
,
999 .release
= seq_release_net
,
1003 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1004 * the multi-AF mutexes.
1006 struct nf_mttg_trav
{
1007 struct list_head
*head
, *curr
;
1008 uint8_t class, nfproto
;
1013 MTTG_TRAV_NFP_UNSPEC
,
1018 static void *xt_mttg_seq_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
,
1021 static const uint8_t next_class
[] = {
1022 [MTTG_TRAV_NFP_UNSPEC
] = MTTG_TRAV_NFP_SPEC
,
1023 [MTTG_TRAV_NFP_SPEC
] = MTTG_TRAV_DONE
,
1025 struct nf_mttg_trav
*trav
= seq
->private;
1027 switch (trav
->class) {
1028 case MTTG_TRAV_INIT
:
1029 trav
->class = MTTG_TRAV_NFP_UNSPEC
;
1030 mutex_lock(&xt
[NFPROTO_UNSPEC
].mutex
);
1031 trav
->head
= trav
->curr
= is_target
?
1032 &xt
[NFPROTO_UNSPEC
].target
: &xt
[NFPROTO_UNSPEC
].match
;
1034 case MTTG_TRAV_NFP_UNSPEC
:
1035 trav
->curr
= trav
->curr
->next
;
1036 if (trav
->curr
!= trav
->head
)
1038 mutex_unlock(&xt
[NFPROTO_UNSPEC
].mutex
);
1039 mutex_lock(&xt
[trav
->nfproto
].mutex
);
1040 trav
->head
= trav
->curr
= is_target
?
1041 &xt
[trav
->nfproto
].target
: &xt
[trav
->nfproto
].match
;
1042 trav
->class = next_class
[trav
->class];
1044 case MTTG_TRAV_NFP_SPEC
:
1045 trav
->curr
= trav
->curr
->next
;
1046 if (trav
->curr
!= trav
->head
)
1048 /* fallthru, _stop will unlock */
1058 static void *xt_mttg_seq_start(struct seq_file
*seq
, loff_t
*pos
,
1061 struct nf_mttg_trav
*trav
= seq
->private;
1064 trav
->class = MTTG_TRAV_INIT
;
1065 for (j
= 0; j
< *pos
; ++j
)
1066 if (xt_mttg_seq_next(seq
, NULL
, NULL
, is_target
) == NULL
)
1071 static void xt_mttg_seq_stop(struct seq_file
*seq
, void *v
)
1073 struct nf_mttg_trav
*trav
= seq
->private;
1075 switch (trav
->class) {
1076 case MTTG_TRAV_NFP_UNSPEC
:
1077 mutex_unlock(&xt
[NFPROTO_UNSPEC
].mutex
);
1079 case MTTG_TRAV_NFP_SPEC
:
1080 mutex_unlock(&xt
[trav
->nfproto
].mutex
);
1085 static void *xt_match_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1087 return xt_mttg_seq_start(seq
, pos
, false);
1090 static void *xt_match_seq_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
1092 return xt_mttg_seq_next(seq
, v
, ppos
, false);
1095 static int xt_match_seq_show(struct seq_file
*seq
, void *v
)
1097 const struct nf_mttg_trav
*trav
= seq
->private;
1098 const struct xt_match
*match
;
1100 switch (trav
->class) {
1101 case MTTG_TRAV_NFP_UNSPEC
:
1102 case MTTG_TRAV_NFP_SPEC
:
1103 if (trav
->curr
== trav
->head
)
1105 match
= list_entry(trav
->curr
, struct xt_match
, list
);
1106 return (*match
->name
== '\0') ? 0 :
1107 seq_printf(seq
, "%s\n", match
->name
);
1112 static const struct seq_operations xt_match_seq_ops
= {
1113 .start
= xt_match_seq_start
,
1114 .next
= xt_match_seq_next
,
1115 .stop
= xt_mttg_seq_stop
,
1116 .show
= xt_match_seq_show
,
1119 static int xt_match_open(struct inode
*inode
, struct file
*file
)
1121 struct seq_file
*seq
;
1122 struct nf_mttg_trav
*trav
;
1125 trav
= kmalloc(sizeof(*trav
), GFP_KERNEL
);
1129 ret
= seq_open(file
, &xt_match_seq_ops
);
1135 seq
= file
->private_data
;
1136 seq
->private = trav
;
1137 trav
->nfproto
= (unsigned long)PDE(inode
)->data
;
1141 static const struct file_operations xt_match_ops
= {
1142 .owner
= THIS_MODULE
,
1143 .open
= xt_match_open
,
1145 .llseek
= seq_lseek
,
1146 .release
= seq_release_private
,
1149 static void *xt_target_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1151 return xt_mttg_seq_start(seq
, pos
, true);
1154 static void *xt_target_seq_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
1156 return xt_mttg_seq_next(seq
, v
, ppos
, true);
1159 static int xt_target_seq_show(struct seq_file
*seq
, void *v
)
1161 const struct nf_mttg_trav
*trav
= seq
->private;
1162 const struct xt_target
*target
;
1164 switch (trav
->class) {
1165 case MTTG_TRAV_NFP_UNSPEC
:
1166 case MTTG_TRAV_NFP_SPEC
:
1167 if (trav
->curr
== trav
->head
)
1169 target
= list_entry(trav
->curr
, struct xt_target
, list
);
1170 return (*target
->name
== '\0') ? 0 :
1171 seq_printf(seq
, "%s\n", target
->name
);
1176 static const struct seq_operations xt_target_seq_ops
= {
1177 .start
= xt_target_seq_start
,
1178 .next
= xt_target_seq_next
,
1179 .stop
= xt_mttg_seq_stop
,
1180 .show
= xt_target_seq_show
,
1183 static int xt_target_open(struct inode
*inode
, struct file
*file
)
1185 struct seq_file
*seq
;
1186 struct nf_mttg_trav
*trav
;
1189 trav
= kmalloc(sizeof(*trav
), GFP_KERNEL
);
1193 ret
= seq_open(file
, &xt_target_seq_ops
);
1199 seq
= file
->private_data
;
1200 seq
->private = trav
;
1201 trav
->nfproto
= (unsigned long)PDE(inode
)->data
;
1205 static const struct file_operations xt_target_ops
= {
1206 .owner
= THIS_MODULE
,
1207 .open
= xt_target_open
,
1209 .llseek
= seq_lseek
,
1210 .release
= seq_release_private
,
1213 #define FORMAT_TABLES "_tables_names"
1214 #define FORMAT_MATCHES "_tables_matches"
1215 #define FORMAT_TARGETS "_tables_targets"
1217 #endif /* CONFIG_PROC_FS */
1220 * xt_hook_link - set up hooks for a new table
1221 * @table: table with metadata needed to set up hooks
1222 * @fn: Hook function
1224 * This function will take care of creating and registering the necessary
1225 * Netfilter hooks for XT tables.
1227 struct nf_hook_ops
*xt_hook_link(const struct xt_table
*table
, nf_hookfn
*fn
)
1229 unsigned int hook_mask
= table
->valid_hooks
;
1230 uint8_t i
, num_hooks
= hweight32(hook_mask
);
1232 struct nf_hook_ops
*ops
;
1235 ops
= kmalloc(sizeof(*ops
) * num_hooks
, GFP_KERNEL
);
1237 return ERR_PTR(-ENOMEM
);
1239 for (i
= 0, hooknum
= 0; i
< num_hooks
&& hook_mask
!= 0;
1240 hook_mask
>>= 1, ++hooknum
) {
1241 if (!(hook_mask
& 1))
1244 ops
[i
].owner
= table
->me
;
1245 ops
[i
].pf
= table
->af
;
1246 ops
[i
].hooknum
= hooknum
;
1247 ops
[i
].priority
= table
->priority
;
1251 ret
= nf_register_hooks(ops
, num_hooks
);
1254 return ERR_PTR(ret
);
1259 EXPORT_SYMBOL_GPL(xt_hook_link
);
1262 * xt_hook_unlink - remove hooks for a table
1263 * @ops: nf_hook_ops array as returned by nf_hook_link
1264 * @hook_mask: the very same mask that was passed to nf_hook_link
1266 void xt_hook_unlink(const struct xt_table
*table
, struct nf_hook_ops
*ops
)
1268 nf_unregister_hooks(ops
, hweight32(table
->valid_hooks
));
1271 EXPORT_SYMBOL_GPL(xt_hook_unlink
);
1273 int xt_proto_init(struct net
*net
, u_int8_t af
)
1275 #ifdef CONFIG_PROC_FS
1276 char buf
[XT_FUNCTION_MAXNAMELEN
];
1277 struct proc_dir_entry
*proc
;
1280 if (af
>= ARRAY_SIZE(xt_prefix
))
1284 #ifdef CONFIG_PROC_FS
1285 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1286 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
1287 proc
= proc_create_data(buf
, 0440, net
->proc_net
, &xt_table_ops
,
1288 (void *)(unsigned long)af
);
1292 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1293 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
1294 proc
= proc_create_data(buf
, 0440, net
->proc_net
, &xt_match_ops
,
1295 (void *)(unsigned long)af
);
1297 goto out_remove_tables
;
1299 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1300 strlcat(buf
, FORMAT_TARGETS
, sizeof(buf
));
1301 proc
= proc_create_data(buf
, 0440, net
->proc_net
, &xt_target_ops
,
1302 (void *)(unsigned long)af
);
1304 goto out_remove_matches
;
1309 #ifdef CONFIG_PROC_FS
1311 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1312 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
1313 proc_net_remove(net
, buf
);
1316 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1317 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
1318 proc_net_remove(net
, buf
);
1323 EXPORT_SYMBOL_GPL(xt_proto_init
);
1325 void xt_proto_fini(struct net
*net
, u_int8_t af
)
1327 #ifdef CONFIG_PROC_FS
1328 char buf
[XT_FUNCTION_MAXNAMELEN
];
1330 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1331 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
1332 proc_net_remove(net
, buf
);
1334 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1335 strlcat(buf
, FORMAT_TARGETS
, sizeof(buf
));
1336 proc_net_remove(net
, buf
);
1338 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1339 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
1340 proc_net_remove(net
, buf
);
1341 #endif /*CONFIG_PROC_FS*/
1343 EXPORT_SYMBOL_GPL(xt_proto_fini
);
1345 static int __net_init
xt_net_init(struct net
*net
)
1349 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++)
1350 INIT_LIST_HEAD(&net
->xt
.tables
[i
]);
1354 static struct pernet_operations xt_net_ops
= {
1355 .init
= xt_net_init
,
1358 static int __init
xt_init(void)
1363 for_each_possible_cpu(i
) {
1364 seqcount_init(&per_cpu(xt_recseq
, i
));
1367 xt
= kmalloc(sizeof(struct xt_af
) * NFPROTO_NUMPROTO
, GFP_KERNEL
);
1371 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++) {
1372 mutex_init(&xt
[i
].mutex
);
1373 #ifdef CONFIG_COMPAT
1374 mutex_init(&xt
[i
].compat_mutex
);
1375 xt
[i
].compat_tab
= NULL
;
1377 INIT_LIST_HEAD(&xt
[i
].target
);
1378 INIT_LIST_HEAD(&xt
[i
].match
);
1380 rv
= register_pernet_subsys(&xt_net_ops
);
1386 static void __exit
xt_fini(void)
1388 unregister_pernet_subsys(&xt_net_ops
);
1392 module_init(xt_init
);
1393 module_exit(xt_fini
);