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
186 * function to use try_then_request_module().
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
= try_then_request_module(xt_find_match(nfproto
, name
, revision
),
225 "%st_%s", xt_prefix
[nfproto
], name
);
226 return (match
!= NULL
) ? match
: ERR_PTR(-ENOENT
);
228 EXPORT_SYMBOL_GPL(xt_request_find_match
);
230 /* Find target, grabs ref. Returns ERR_PTR() on error. */
231 struct xt_target
*xt_find_target(u8 af
, const char *name
, u8 revision
)
236 if (mutex_lock_interruptible(&xt
[af
].mutex
) != 0)
237 return ERR_PTR(-EINTR
);
239 list_for_each_entry(t
, &xt
[af
].target
, list
) {
240 if (strcmp(t
->name
, name
) == 0) {
241 if (t
->revision
== revision
) {
242 if (try_module_get(t
->me
)) {
243 mutex_unlock(&xt
[af
].mutex
);
247 err
= -EPROTOTYPE
; /* Found something. */
250 mutex_unlock(&xt
[af
].mutex
);
252 if (af
!= NFPROTO_UNSPEC
)
253 /* Try searching again in the family-independent list */
254 return xt_find_target(NFPROTO_UNSPEC
, name
, revision
);
258 EXPORT_SYMBOL(xt_find_target
);
260 struct xt_target
*xt_request_find_target(u8 af
, const char *name
, u8 revision
)
262 struct xt_target
*target
;
264 target
= try_then_request_module(xt_find_target(af
, name
, revision
),
265 "%st_%s", xt_prefix
[af
], name
);
266 return (target
!= NULL
) ? target
: ERR_PTR(-ENOENT
);
268 EXPORT_SYMBOL_GPL(xt_request_find_target
);
270 static int match_revfn(u8 af
, const char *name
, u8 revision
, int *bestp
)
272 const struct xt_match
*m
;
275 list_for_each_entry(m
, &xt
[af
].match
, list
) {
276 if (strcmp(m
->name
, name
) == 0) {
277 if (m
->revision
> *bestp
)
278 *bestp
= m
->revision
;
279 if (m
->revision
== revision
)
284 if (af
!= NFPROTO_UNSPEC
&& !have_rev
)
285 return match_revfn(NFPROTO_UNSPEC
, name
, revision
, bestp
);
290 static int target_revfn(u8 af
, const char *name
, u8 revision
, int *bestp
)
292 const struct xt_target
*t
;
295 list_for_each_entry(t
, &xt
[af
].target
, list
) {
296 if (strcmp(t
->name
, name
) == 0) {
297 if (t
->revision
> *bestp
)
298 *bestp
= t
->revision
;
299 if (t
->revision
== revision
)
304 if (af
!= NFPROTO_UNSPEC
&& !have_rev
)
305 return target_revfn(NFPROTO_UNSPEC
, name
, revision
, bestp
);
310 /* Returns true or false (if no such extension at all) */
311 int xt_find_revision(u8 af
, const char *name
, u8 revision
, int target
,
314 int have_rev
, best
= -1;
316 if (mutex_lock_interruptible(&xt
[af
].mutex
) != 0) {
321 have_rev
= target_revfn(af
, name
, revision
, &best
);
323 have_rev
= match_revfn(af
, name
, revision
, &best
);
324 mutex_unlock(&xt
[af
].mutex
);
326 /* Nothing at all? Return 0 to try loading module. */
334 *err
= -EPROTONOSUPPORT
;
337 EXPORT_SYMBOL_GPL(xt_find_revision
);
339 static char *textify_hooks(char *buf
, size_t size
, unsigned int mask
)
341 static const char *const names
[] = {
342 "PREROUTING", "INPUT", "FORWARD",
343 "OUTPUT", "POSTROUTING", "BROUTING",
351 for (i
= 0; i
< ARRAY_SIZE(names
); ++i
) {
352 if (!(mask
& (1 << i
)))
354 res
= snprintf(p
, size
, "%s%s", np
? "/" : "", names
[i
]);
365 int xt_check_match(struct xt_mtchk_param
*par
,
366 unsigned int size
, u_int8_t proto
, bool inv_proto
)
370 if (XT_ALIGN(par
->match
->matchsize
) != size
&&
371 par
->match
->matchsize
!= -1) {
373 * ebt_among is exempt from centralized matchsize checking
374 * because it uses a dynamic-size data set.
376 pr_err("%s_tables: %s.%u match: invalid size "
377 "%u (kernel) != (user) %u\n",
378 xt_prefix
[par
->family
], par
->match
->name
,
379 par
->match
->revision
,
380 XT_ALIGN(par
->match
->matchsize
), size
);
383 if (par
->match
->table
!= NULL
&&
384 strcmp(par
->match
->table
, par
->table
) != 0) {
385 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
386 xt_prefix
[par
->family
], par
->match
->name
,
387 par
->match
->table
, par
->table
);
390 if (par
->match
->hooks
&& (par
->hook_mask
& ~par
->match
->hooks
) != 0) {
391 char used
[64], allow
[64];
393 pr_err("%s_tables: %s match: used from hooks %s, but only "
395 xt_prefix
[par
->family
], par
->match
->name
,
396 textify_hooks(used
, sizeof(used
), par
->hook_mask
),
397 textify_hooks(allow
, sizeof(allow
), par
->match
->hooks
));
400 if (par
->match
->proto
&& (par
->match
->proto
!= proto
|| inv_proto
)) {
401 pr_err("%s_tables: %s match: only valid for protocol %u\n",
402 xt_prefix
[par
->family
], par
->match
->name
,
406 if (par
->match
->checkentry
!= NULL
) {
407 ret
= par
->match
->checkentry(par
);
411 /* Flag up potential errors. */
416 EXPORT_SYMBOL_GPL(xt_check_match
);
419 int xt_compat_add_offset(u_int8_t af
, unsigned int offset
, int delta
)
421 struct xt_af
*xp
= &xt
[af
];
423 if (!xp
->compat_tab
) {
426 xp
->compat_tab
= vmalloc(sizeof(struct compat_delta
) * xp
->number
);
432 if (xp
->cur
>= xp
->number
)
436 delta
+= xp
->compat_tab
[xp
->cur
- 1].delta
;
437 xp
->compat_tab
[xp
->cur
].offset
= offset
;
438 xp
->compat_tab
[xp
->cur
].delta
= delta
;
442 EXPORT_SYMBOL_GPL(xt_compat_add_offset
);
444 void xt_compat_flush_offsets(u_int8_t af
)
446 if (xt
[af
].compat_tab
) {
447 vfree(xt
[af
].compat_tab
);
448 xt
[af
].compat_tab
= NULL
;
452 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets
);
454 int xt_compat_calc_jump(u_int8_t af
, unsigned int offset
)
456 struct compat_delta
*tmp
= xt
[af
].compat_tab
;
457 int mid
, left
= 0, right
= xt
[af
].cur
- 1;
459 while (left
<= right
) {
460 mid
= (left
+ right
) >> 1;
461 if (offset
> tmp
[mid
].offset
)
463 else if (offset
< tmp
[mid
].offset
)
466 return mid
? tmp
[mid
- 1].delta
: 0;
471 EXPORT_SYMBOL_GPL(xt_compat_calc_jump
);
473 void xt_compat_init_offsets(u_int8_t af
, unsigned int number
)
475 xt
[af
].number
= number
;
478 EXPORT_SYMBOL(xt_compat_init_offsets
);
480 int xt_compat_match_offset(const struct xt_match
*match
)
482 u_int16_t csize
= match
->compatsize
? : match
->matchsize
;
483 return XT_ALIGN(match
->matchsize
) - COMPAT_XT_ALIGN(csize
);
485 EXPORT_SYMBOL_GPL(xt_compat_match_offset
);
487 int xt_compat_match_from_user(struct xt_entry_match
*m
, void **dstptr
,
490 const struct xt_match
*match
= m
->u
.kernel
.match
;
491 struct compat_xt_entry_match
*cm
= (struct compat_xt_entry_match
*)m
;
492 int pad
, off
= xt_compat_match_offset(match
);
493 u_int16_t msize
= cm
->u
.user
.match_size
;
496 memcpy(m
, cm
, sizeof(*cm
));
497 if (match
->compat_from_user
)
498 match
->compat_from_user(m
->data
, cm
->data
);
500 memcpy(m
->data
, cm
->data
, msize
- sizeof(*cm
));
501 pad
= XT_ALIGN(match
->matchsize
) - match
->matchsize
;
503 memset(m
->data
+ match
->matchsize
, 0, pad
);
506 m
->u
.user
.match_size
= msize
;
512 EXPORT_SYMBOL_GPL(xt_compat_match_from_user
);
514 int xt_compat_match_to_user(const struct xt_entry_match
*m
,
515 void __user
**dstptr
, unsigned int *size
)
517 const struct xt_match
*match
= m
->u
.kernel
.match
;
518 struct compat_xt_entry_match __user
*cm
= *dstptr
;
519 int off
= xt_compat_match_offset(match
);
520 u_int16_t msize
= m
->u
.user
.match_size
- off
;
522 if (copy_to_user(cm
, m
, sizeof(*cm
)) ||
523 put_user(msize
, &cm
->u
.user
.match_size
) ||
524 copy_to_user(cm
->u
.user
.name
, m
->u
.kernel
.match
->name
,
525 strlen(m
->u
.kernel
.match
->name
) + 1))
528 if (match
->compat_to_user
) {
529 if (match
->compat_to_user((void __user
*)cm
->data
, m
->data
))
532 if (copy_to_user(cm
->data
, m
->data
, msize
- sizeof(*cm
)))
540 EXPORT_SYMBOL_GPL(xt_compat_match_to_user
);
541 #endif /* CONFIG_COMPAT */
543 int xt_check_target(struct xt_tgchk_param
*par
,
544 unsigned int size
, u_int8_t proto
, bool inv_proto
)
548 if (XT_ALIGN(par
->target
->targetsize
) != size
) {
549 pr_err("%s_tables: %s.%u target: invalid size "
550 "%u (kernel) != (user) %u\n",
551 xt_prefix
[par
->family
], par
->target
->name
,
552 par
->target
->revision
,
553 XT_ALIGN(par
->target
->targetsize
), size
);
556 if (par
->target
->table
!= NULL
&&
557 strcmp(par
->target
->table
, par
->table
) != 0) {
558 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
559 xt_prefix
[par
->family
], par
->target
->name
,
560 par
->target
->table
, par
->table
);
563 if (par
->target
->hooks
&& (par
->hook_mask
& ~par
->target
->hooks
) != 0) {
564 char used
[64], allow
[64];
566 pr_err("%s_tables: %s target: used from hooks %s, but only "
568 xt_prefix
[par
->family
], par
->target
->name
,
569 textify_hooks(used
, sizeof(used
), par
->hook_mask
),
570 textify_hooks(allow
, sizeof(allow
), par
->target
->hooks
));
573 if (par
->target
->proto
&& (par
->target
->proto
!= proto
|| inv_proto
)) {
574 pr_err("%s_tables: %s target: only valid for protocol %u\n",
575 xt_prefix
[par
->family
], par
->target
->name
,
579 if (par
->target
->checkentry
!= NULL
) {
580 ret
= par
->target
->checkentry(par
);
584 /* Flag up potential errors. */
589 EXPORT_SYMBOL_GPL(xt_check_target
);
592 int xt_compat_target_offset(const struct xt_target
*target
)
594 u_int16_t csize
= target
->compatsize
? : target
->targetsize
;
595 return XT_ALIGN(target
->targetsize
) - COMPAT_XT_ALIGN(csize
);
597 EXPORT_SYMBOL_GPL(xt_compat_target_offset
);
599 void xt_compat_target_from_user(struct xt_entry_target
*t
, void **dstptr
,
602 const struct xt_target
*target
= t
->u
.kernel
.target
;
603 struct compat_xt_entry_target
*ct
= (struct compat_xt_entry_target
*)t
;
604 int pad
, off
= xt_compat_target_offset(target
);
605 u_int16_t tsize
= ct
->u
.user
.target_size
;
608 memcpy(t
, ct
, sizeof(*ct
));
609 if (target
->compat_from_user
)
610 target
->compat_from_user(t
->data
, ct
->data
);
612 memcpy(t
->data
, ct
->data
, tsize
- sizeof(*ct
));
613 pad
= XT_ALIGN(target
->targetsize
) - target
->targetsize
;
615 memset(t
->data
+ target
->targetsize
, 0, pad
);
618 t
->u
.user
.target_size
= tsize
;
623 EXPORT_SYMBOL_GPL(xt_compat_target_from_user
);
625 int xt_compat_target_to_user(const struct xt_entry_target
*t
,
626 void __user
**dstptr
, unsigned int *size
)
628 const struct xt_target
*target
= t
->u
.kernel
.target
;
629 struct compat_xt_entry_target __user
*ct
= *dstptr
;
630 int off
= xt_compat_target_offset(target
);
631 u_int16_t tsize
= t
->u
.user
.target_size
- off
;
633 if (copy_to_user(ct
, t
, sizeof(*ct
)) ||
634 put_user(tsize
, &ct
->u
.user
.target_size
) ||
635 copy_to_user(ct
->u
.user
.name
, t
->u
.kernel
.target
->name
,
636 strlen(t
->u
.kernel
.target
->name
) + 1))
639 if (target
->compat_to_user
) {
640 if (target
->compat_to_user((void __user
*)ct
->data
, t
->data
))
643 if (copy_to_user(ct
->data
, t
->data
, tsize
- sizeof(*ct
)))
651 EXPORT_SYMBOL_GPL(xt_compat_target_to_user
);
654 struct xt_table_info
*xt_alloc_table_info(unsigned int size
)
656 struct xt_table_info
*newinfo
;
659 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
660 if ((SMP_ALIGN(size
) >> PAGE_SHIFT
) + 2 > totalram_pages
)
663 newinfo
= kzalloc(XT_TABLE_INFO_SZ
, GFP_KERNEL
);
667 newinfo
->size
= size
;
669 for_each_possible_cpu(cpu
) {
670 if (size
<= PAGE_SIZE
)
671 newinfo
->entries
[cpu
] = kmalloc_node(size
,
675 newinfo
->entries
[cpu
] = vmalloc_node(size
,
678 if (newinfo
->entries
[cpu
] == NULL
) {
679 xt_free_table_info(newinfo
);
686 EXPORT_SYMBOL(xt_alloc_table_info
);
688 void xt_free_table_info(struct xt_table_info
*info
)
692 for_each_possible_cpu(cpu
) {
693 if (info
->size
<= PAGE_SIZE
)
694 kfree(info
->entries
[cpu
]);
696 vfree(info
->entries
[cpu
]);
699 if (info
->jumpstack
!= NULL
) {
700 if (sizeof(void *) * info
->stacksize
> PAGE_SIZE
) {
701 for_each_possible_cpu(cpu
)
702 vfree(info
->jumpstack
[cpu
]);
704 for_each_possible_cpu(cpu
)
705 kfree(info
->jumpstack
[cpu
]);
709 if (sizeof(void **) * nr_cpu_ids
> PAGE_SIZE
)
710 vfree(info
->jumpstack
);
712 kfree(info
->jumpstack
);
714 free_percpu(info
->stackptr
);
718 EXPORT_SYMBOL(xt_free_table_info
);
720 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
721 struct xt_table
*xt_find_table_lock(struct net
*net
, u_int8_t af
,
726 if (mutex_lock_interruptible(&xt
[af
].mutex
) != 0)
727 return ERR_PTR(-EINTR
);
729 list_for_each_entry(t
, &net
->xt
.tables
[af
], list
)
730 if (strcmp(t
->name
, name
) == 0 && try_module_get(t
->me
))
732 mutex_unlock(&xt
[af
].mutex
);
735 EXPORT_SYMBOL_GPL(xt_find_table_lock
);
737 void xt_table_unlock(struct xt_table
*table
)
739 mutex_unlock(&xt
[table
->af
].mutex
);
741 EXPORT_SYMBOL_GPL(xt_table_unlock
);
744 void xt_compat_lock(u_int8_t af
)
746 mutex_lock(&xt
[af
].compat_mutex
);
748 EXPORT_SYMBOL_GPL(xt_compat_lock
);
750 void xt_compat_unlock(u_int8_t af
)
752 mutex_unlock(&xt
[af
].compat_mutex
);
754 EXPORT_SYMBOL_GPL(xt_compat_unlock
);
757 DEFINE_PER_CPU(struct xt_info_lock
, xt_info_locks
);
758 EXPORT_PER_CPU_SYMBOL_GPL(xt_info_locks
);
760 static int xt_jumpstack_alloc(struct xt_table_info
*i
)
765 i
->stackptr
= alloc_percpu(unsigned int);
766 if (i
->stackptr
== NULL
)
769 size
= sizeof(void **) * nr_cpu_ids
;
770 if (size
> PAGE_SIZE
)
771 i
->jumpstack
= vmalloc(size
);
773 i
->jumpstack
= kmalloc(size
, GFP_KERNEL
);
774 if (i
->jumpstack
== NULL
)
776 memset(i
->jumpstack
, 0, size
);
778 i
->stacksize
*= xt_jumpstack_multiplier
;
779 size
= sizeof(void *) * i
->stacksize
;
780 for_each_possible_cpu(cpu
) {
781 if (size
> PAGE_SIZE
)
782 i
->jumpstack
[cpu
] = vmalloc_node(size
,
785 i
->jumpstack
[cpu
] = kmalloc_node(size
,
786 GFP_KERNEL
, cpu_to_node(cpu
));
787 if (i
->jumpstack
[cpu
] == NULL
)
789 * Freeing will be done later on by the callers. The
790 * chain is: xt_replace_table -> __do_replace ->
791 * do_replace -> xt_free_table_info.
799 struct xt_table_info
*
800 xt_replace_table(struct xt_table
*table
,
801 unsigned int num_counters
,
802 struct xt_table_info
*newinfo
,
805 struct xt_table_info
*private;
808 ret
= xt_jumpstack_alloc(newinfo
);
814 /* Do the substitution. */
816 private = table
->private;
818 /* Check inside lock: is the old number correct? */
819 if (num_counters
!= private->number
) {
820 pr_debug("num_counters != table->private->number (%u/%u)\n",
821 num_counters
, private->number
);
827 table
->private = newinfo
;
828 newinfo
->initial_entries
= private->initial_entries
;
831 * Even though table entries have now been swapped, other CPU's
832 * may still be using the old entries. This is okay, because
833 * resynchronization happens because of the locking done
834 * during the get_counters() routine.
840 struct audit_buffer
*ab
;
842 ab
= audit_log_start(current
->audit_context
, GFP_KERNEL
,
843 AUDIT_NETFILTER_CFG
);
845 audit_log_format(ab
, "table=%s family=%u entries=%u",
846 table
->name
, table
->af
,
855 EXPORT_SYMBOL_GPL(xt_replace_table
);
857 struct xt_table
*xt_register_table(struct net
*net
,
858 const struct xt_table
*input_table
,
859 struct xt_table_info
*bootstrap
,
860 struct xt_table_info
*newinfo
)
863 struct xt_table_info
*private;
864 struct xt_table
*t
, *table
;
866 /* Don't add one object to multiple lists. */
867 table
= kmemdup(input_table
, sizeof(struct xt_table
), GFP_KERNEL
);
873 ret
= mutex_lock_interruptible(&xt
[table
->af
].mutex
);
877 /* Don't autoload: we'd eat our tail... */
878 list_for_each_entry(t
, &net
->xt
.tables
[table
->af
], list
) {
879 if (strcmp(t
->name
, table
->name
) == 0) {
885 /* Simplifies replace_table code. */
886 table
->private = bootstrap
;
888 if (!xt_replace_table(table
, 0, newinfo
, &ret
))
891 private = table
->private;
892 pr_debug("table->private->number = %u\n", private->number
);
894 /* save number of initial entries */
895 private->initial_entries
= private->number
;
897 list_add(&table
->list
, &net
->xt
.tables
[table
->af
]);
898 mutex_unlock(&xt
[table
->af
].mutex
);
902 mutex_unlock(&xt
[table
->af
].mutex
);
908 EXPORT_SYMBOL_GPL(xt_register_table
);
910 void *xt_unregister_table(struct xt_table
*table
)
912 struct xt_table_info
*private;
914 mutex_lock(&xt
[table
->af
].mutex
);
915 private = table
->private;
916 list_del(&table
->list
);
917 mutex_unlock(&xt
[table
->af
].mutex
);
922 EXPORT_SYMBOL_GPL(xt_unregister_table
);
924 #ifdef CONFIG_PROC_FS
925 struct xt_names_priv
{
926 struct seq_net_private p
;
929 static void *xt_table_seq_start(struct seq_file
*seq
, loff_t
*pos
)
931 struct xt_names_priv
*priv
= seq
->private;
932 struct net
*net
= seq_file_net(seq
);
933 u_int8_t af
= priv
->af
;
935 mutex_lock(&xt
[af
].mutex
);
936 return seq_list_start(&net
->xt
.tables
[af
], *pos
);
939 static void *xt_table_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
941 struct xt_names_priv
*priv
= seq
->private;
942 struct net
*net
= seq_file_net(seq
);
943 u_int8_t af
= priv
->af
;
945 return seq_list_next(v
, &net
->xt
.tables
[af
], pos
);
948 static void xt_table_seq_stop(struct seq_file
*seq
, void *v
)
950 struct xt_names_priv
*priv
= seq
->private;
951 u_int8_t af
= priv
->af
;
953 mutex_unlock(&xt
[af
].mutex
);
956 static int xt_table_seq_show(struct seq_file
*seq
, void *v
)
958 struct xt_table
*table
= list_entry(v
, struct xt_table
, list
);
960 if (strlen(table
->name
))
961 return seq_printf(seq
, "%s\n", table
->name
);
966 static const struct seq_operations xt_table_seq_ops
= {
967 .start
= xt_table_seq_start
,
968 .next
= xt_table_seq_next
,
969 .stop
= xt_table_seq_stop
,
970 .show
= xt_table_seq_show
,
973 static int xt_table_open(struct inode
*inode
, struct file
*file
)
976 struct xt_names_priv
*priv
;
978 ret
= seq_open_net(inode
, file
, &xt_table_seq_ops
,
979 sizeof(struct xt_names_priv
));
981 priv
= ((struct seq_file
*)file
->private_data
)->private;
982 priv
->af
= (unsigned long)PDE(inode
)->data
;
987 static const struct file_operations xt_table_ops
= {
988 .owner
= THIS_MODULE
,
989 .open
= xt_table_open
,
992 .release
= seq_release_net
,
996 * Traverse state for ip{,6}_{tables,matches} for helping crossing
997 * the multi-AF mutexes.
999 struct nf_mttg_trav
{
1000 struct list_head
*head
, *curr
;
1001 uint8_t class, nfproto
;
1006 MTTG_TRAV_NFP_UNSPEC
,
1011 static void *xt_mttg_seq_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
,
1014 static const uint8_t next_class
[] = {
1015 [MTTG_TRAV_NFP_UNSPEC
] = MTTG_TRAV_NFP_SPEC
,
1016 [MTTG_TRAV_NFP_SPEC
] = MTTG_TRAV_DONE
,
1018 struct nf_mttg_trav
*trav
= seq
->private;
1020 switch (trav
->class) {
1021 case MTTG_TRAV_INIT
:
1022 trav
->class = MTTG_TRAV_NFP_UNSPEC
;
1023 mutex_lock(&xt
[NFPROTO_UNSPEC
].mutex
);
1024 trav
->head
= trav
->curr
= is_target
?
1025 &xt
[NFPROTO_UNSPEC
].target
: &xt
[NFPROTO_UNSPEC
].match
;
1027 case MTTG_TRAV_NFP_UNSPEC
:
1028 trav
->curr
= trav
->curr
->next
;
1029 if (trav
->curr
!= trav
->head
)
1031 mutex_unlock(&xt
[NFPROTO_UNSPEC
].mutex
);
1032 mutex_lock(&xt
[trav
->nfproto
].mutex
);
1033 trav
->head
= trav
->curr
= is_target
?
1034 &xt
[trav
->nfproto
].target
: &xt
[trav
->nfproto
].match
;
1035 trav
->class = next_class
[trav
->class];
1037 case MTTG_TRAV_NFP_SPEC
:
1038 trav
->curr
= trav
->curr
->next
;
1039 if (trav
->curr
!= trav
->head
)
1041 /* fallthru, _stop will unlock */
1051 static void *xt_mttg_seq_start(struct seq_file
*seq
, loff_t
*pos
,
1054 struct nf_mttg_trav
*trav
= seq
->private;
1057 trav
->class = MTTG_TRAV_INIT
;
1058 for (j
= 0; j
< *pos
; ++j
)
1059 if (xt_mttg_seq_next(seq
, NULL
, NULL
, is_target
) == NULL
)
1064 static void xt_mttg_seq_stop(struct seq_file
*seq
, void *v
)
1066 struct nf_mttg_trav
*trav
= seq
->private;
1068 switch (trav
->class) {
1069 case MTTG_TRAV_NFP_UNSPEC
:
1070 mutex_unlock(&xt
[NFPROTO_UNSPEC
].mutex
);
1072 case MTTG_TRAV_NFP_SPEC
:
1073 mutex_unlock(&xt
[trav
->nfproto
].mutex
);
1078 static void *xt_match_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1080 return xt_mttg_seq_start(seq
, pos
, false);
1083 static void *xt_match_seq_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
1085 return xt_mttg_seq_next(seq
, v
, ppos
, false);
1088 static int xt_match_seq_show(struct seq_file
*seq
, void *v
)
1090 const struct nf_mttg_trav
*trav
= seq
->private;
1091 const struct xt_match
*match
;
1093 switch (trav
->class) {
1094 case MTTG_TRAV_NFP_UNSPEC
:
1095 case MTTG_TRAV_NFP_SPEC
:
1096 if (trav
->curr
== trav
->head
)
1098 match
= list_entry(trav
->curr
, struct xt_match
, list
);
1099 return (*match
->name
== '\0') ? 0 :
1100 seq_printf(seq
, "%s\n", match
->name
);
1105 static const struct seq_operations xt_match_seq_ops
= {
1106 .start
= xt_match_seq_start
,
1107 .next
= xt_match_seq_next
,
1108 .stop
= xt_mttg_seq_stop
,
1109 .show
= xt_match_seq_show
,
1112 static int xt_match_open(struct inode
*inode
, struct file
*file
)
1114 struct seq_file
*seq
;
1115 struct nf_mttg_trav
*trav
;
1118 trav
= kmalloc(sizeof(*trav
), GFP_KERNEL
);
1122 ret
= seq_open(file
, &xt_match_seq_ops
);
1128 seq
= file
->private_data
;
1129 seq
->private = trav
;
1130 trav
->nfproto
= (unsigned long)PDE(inode
)->data
;
1134 static const struct file_operations xt_match_ops
= {
1135 .owner
= THIS_MODULE
,
1136 .open
= xt_match_open
,
1138 .llseek
= seq_lseek
,
1139 .release
= seq_release_private
,
1142 static void *xt_target_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1144 return xt_mttg_seq_start(seq
, pos
, true);
1147 static void *xt_target_seq_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
1149 return xt_mttg_seq_next(seq
, v
, ppos
, true);
1152 static int xt_target_seq_show(struct seq_file
*seq
, void *v
)
1154 const struct nf_mttg_trav
*trav
= seq
->private;
1155 const struct xt_target
*target
;
1157 switch (trav
->class) {
1158 case MTTG_TRAV_NFP_UNSPEC
:
1159 case MTTG_TRAV_NFP_SPEC
:
1160 if (trav
->curr
== trav
->head
)
1162 target
= list_entry(trav
->curr
, struct xt_target
, list
);
1163 return (*target
->name
== '\0') ? 0 :
1164 seq_printf(seq
, "%s\n", target
->name
);
1169 static const struct seq_operations xt_target_seq_ops
= {
1170 .start
= xt_target_seq_start
,
1171 .next
= xt_target_seq_next
,
1172 .stop
= xt_mttg_seq_stop
,
1173 .show
= xt_target_seq_show
,
1176 static int xt_target_open(struct inode
*inode
, struct file
*file
)
1178 struct seq_file
*seq
;
1179 struct nf_mttg_trav
*trav
;
1182 trav
= kmalloc(sizeof(*trav
), GFP_KERNEL
);
1186 ret
= seq_open(file
, &xt_target_seq_ops
);
1192 seq
= file
->private_data
;
1193 seq
->private = trav
;
1194 trav
->nfproto
= (unsigned long)PDE(inode
)->data
;
1198 static const struct file_operations xt_target_ops
= {
1199 .owner
= THIS_MODULE
,
1200 .open
= xt_target_open
,
1202 .llseek
= seq_lseek
,
1203 .release
= seq_release_private
,
1206 #define FORMAT_TABLES "_tables_names"
1207 #define FORMAT_MATCHES "_tables_matches"
1208 #define FORMAT_TARGETS "_tables_targets"
1210 #endif /* CONFIG_PROC_FS */
1213 * xt_hook_link - set up hooks for a new table
1214 * @table: table with metadata needed to set up hooks
1215 * @fn: Hook function
1217 * This function will take care of creating and registering the necessary
1218 * Netfilter hooks for XT tables.
1220 struct nf_hook_ops
*xt_hook_link(const struct xt_table
*table
, nf_hookfn
*fn
)
1222 unsigned int hook_mask
= table
->valid_hooks
;
1223 uint8_t i
, num_hooks
= hweight32(hook_mask
);
1225 struct nf_hook_ops
*ops
;
1228 ops
= kmalloc(sizeof(*ops
) * num_hooks
, GFP_KERNEL
);
1230 return ERR_PTR(-ENOMEM
);
1232 for (i
= 0, hooknum
= 0; i
< num_hooks
&& hook_mask
!= 0;
1233 hook_mask
>>= 1, ++hooknum
) {
1234 if (!(hook_mask
& 1))
1237 ops
[i
].owner
= table
->me
;
1238 ops
[i
].pf
= table
->af
;
1239 ops
[i
].hooknum
= hooknum
;
1240 ops
[i
].priority
= table
->priority
;
1244 ret
= nf_register_hooks(ops
, num_hooks
);
1247 return ERR_PTR(ret
);
1252 EXPORT_SYMBOL_GPL(xt_hook_link
);
1255 * xt_hook_unlink - remove hooks for a table
1256 * @ops: nf_hook_ops array as returned by nf_hook_link
1257 * @hook_mask: the very same mask that was passed to nf_hook_link
1259 void xt_hook_unlink(const struct xt_table
*table
, struct nf_hook_ops
*ops
)
1261 nf_unregister_hooks(ops
, hweight32(table
->valid_hooks
));
1264 EXPORT_SYMBOL_GPL(xt_hook_unlink
);
1266 int xt_proto_init(struct net
*net
, u_int8_t af
)
1268 #ifdef CONFIG_PROC_FS
1269 char buf
[XT_FUNCTION_MAXNAMELEN
];
1270 struct proc_dir_entry
*proc
;
1273 if (af
>= ARRAY_SIZE(xt_prefix
))
1277 #ifdef CONFIG_PROC_FS
1278 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1279 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
1280 proc
= proc_create_data(buf
, 0440, net
->proc_net
, &xt_table_ops
,
1281 (void *)(unsigned long)af
);
1285 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1286 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
1287 proc
= proc_create_data(buf
, 0440, net
->proc_net
, &xt_match_ops
,
1288 (void *)(unsigned long)af
);
1290 goto out_remove_tables
;
1292 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1293 strlcat(buf
, FORMAT_TARGETS
, sizeof(buf
));
1294 proc
= proc_create_data(buf
, 0440, net
->proc_net
, &xt_target_ops
,
1295 (void *)(unsigned long)af
);
1297 goto out_remove_matches
;
1302 #ifdef CONFIG_PROC_FS
1304 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1305 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
1306 proc_net_remove(net
, buf
);
1309 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1310 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
1311 proc_net_remove(net
, buf
);
1316 EXPORT_SYMBOL_GPL(xt_proto_init
);
1318 void xt_proto_fini(struct net
*net
, u_int8_t af
)
1320 #ifdef CONFIG_PROC_FS
1321 char buf
[XT_FUNCTION_MAXNAMELEN
];
1323 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1324 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
1325 proc_net_remove(net
, buf
);
1327 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1328 strlcat(buf
, FORMAT_TARGETS
, sizeof(buf
));
1329 proc_net_remove(net
, buf
);
1331 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1332 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
1333 proc_net_remove(net
, buf
);
1334 #endif /*CONFIG_PROC_FS*/
1336 EXPORT_SYMBOL_GPL(xt_proto_fini
);
1338 static int __net_init
xt_net_init(struct net
*net
)
1342 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++)
1343 INIT_LIST_HEAD(&net
->xt
.tables
[i
]);
1347 static struct pernet_operations xt_net_ops
= {
1348 .init
= xt_net_init
,
1351 static int __init
xt_init(void)
1356 for_each_possible_cpu(i
) {
1357 struct xt_info_lock
*lock
= &per_cpu(xt_info_locks
, i
);
1359 seqlock_init(&lock
->lock
);
1363 xt
= kmalloc(sizeof(struct xt_af
) * NFPROTO_NUMPROTO
, GFP_KERNEL
);
1367 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++) {
1368 mutex_init(&xt
[i
].mutex
);
1369 #ifdef CONFIG_COMPAT
1370 mutex_init(&xt
[i
].compat_mutex
);
1371 xt
[i
].compat_tab
= NULL
;
1373 INIT_LIST_HEAD(&xt
[i
].target
);
1374 INIT_LIST_HEAD(&xt
[i
].match
);
1376 rv
= register_pernet_subsys(&xt_net_ops
);
1382 static void __exit
xt_fini(void)
1384 unregister_pernet_subsys(&xt_net_ops
);
1388 module_init(xt_init
);
1389 module_exit(xt_fini
);