2 * x_tables core - Backend for {ip,ip6,arp}_tables
4 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
5 * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
7 * Based on existing ip_tables code which is
8 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
9 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/socket.h>
20 #include <linux/net.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/string.h>
24 #include <linux/vmalloc.h>
25 #include <linux/mutex.h>
27 #include <linux/slab.h>
28 #include <linux/audit.h>
29 #include <linux/user_namespace.h>
30 #include <net/net_namespace.h>
32 #include <linux/netfilter/x_tables.h>
33 #include <linux/netfilter_arp.h>
34 #include <linux/netfilter_ipv4/ip_tables.h>
35 #include <linux/netfilter_ipv6/ip6_tables.h>
36 #include <linux/netfilter_arp/arp_tables.h>
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
40 MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
42 #define XT_PCPU_BLOCK_SIZE 4096
45 unsigned int offset
; /* offset in kernel */
46 int delta
; /* delta in 32bit user land */
51 struct list_head match
;
52 struct list_head target
;
54 struct mutex compat_mutex
;
55 struct compat_delta
*compat_tab
;
56 unsigned int number
; /* number of slots in compat_tab[] */
57 unsigned int cur
; /* number of used slots in compat_tab[] */
61 static struct xt_af
*xt
;
63 static const char *const xt_prefix
[NFPROTO_NUMPROTO
] = {
64 [NFPROTO_UNSPEC
] = "x",
65 [NFPROTO_IPV4
] = "ip",
66 [NFPROTO_ARP
] = "arp",
67 [NFPROTO_BRIDGE
] = "eb",
68 [NFPROTO_IPV6
] = "ip6",
71 /* Registration hooks for targets. */
72 int xt_register_target(struct xt_target
*target
)
74 u_int8_t af
= target
->family
;
76 mutex_lock(&xt
[af
].mutex
);
77 list_add(&target
->list
, &xt
[af
].target
);
78 mutex_unlock(&xt
[af
].mutex
);
81 EXPORT_SYMBOL(xt_register_target
);
84 xt_unregister_target(struct xt_target
*target
)
86 u_int8_t af
= target
->family
;
88 mutex_lock(&xt
[af
].mutex
);
89 list_del(&target
->list
);
90 mutex_unlock(&xt
[af
].mutex
);
92 EXPORT_SYMBOL(xt_unregister_target
);
95 xt_register_targets(struct xt_target
*target
, unsigned int n
)
100 for (i
= 0; i
< n
; i
++) {
101 err
= xt_register_target(&target
[i
]);
109 xt_unregister_targets(target
, i
);
112 EXPORT_SYMBOL(xt_register_targets
);
115 xt_unregister_targets(struct xt_target
*target
, unsigned int n
)
118 xt_unregister_target(&target
[n
]);
120 EXPORT_SYMBOL(xt_unregister_targets
);
122 int xt_register_match(struct xt_match
*match
)
124 u_int8_t af
= match
->family
;
126 mutex_lock(&xt
[af
].mutex
);
127 list_add(&match
->list
, &xt
[af
].match
);
128 mutex_unlock(&xt
[af
].mutex
);
131 EXPORT_SYMBOL(xt_register_match
);
134 xt_unregister_match(struct xt_match
*match
)
136 u_int8_t af
= match
->family
;
138 mutex_lock(&xt
[af
].mutex
);
139 list_del(&match
->list
);
140 mutex_unlock(&xt
[af
].mutex
);
142 EXPORT_SYMBOL(xt_unregister_match
);
145 xt_register_matches(struct xt_match
*match
, unsigned int n
)
150 for (i
= 0; i
< n
; i
++) {
151 err
= xt_register_match(&match
[i
]);
159 xt_unregister_matches(match
, i
);
162 EXPORT_SYMBOL(xt_register_matches
);
165 xt_unregister_matches(struct xt_match
*match
, unsigned int n
)
168 xt_unregister_match(&match
[n
]);
170 EXPORT_SYMBOL(xt_unregister_matches
);
174 * These are weird, but module loading must not be done with mutex
175 * held (since they will register), and we have to have a single
179 /* Find match, grabs ref. Returns ERR_PTR() on error. */
180 struct xt_match
*xt_find_match(u8 af
, const char *name
, u8 revision
)
185 mutex_lock(&xt
[af
].mutex
);
186 list_for_each_entry(m
, &xt
[af
].match
, list
) {
187 if (strcmp(m
->name
, name
) == 0) {
188 if (m
->revision
== revision
) {
189 if (try_module_get(m
->me
)) {
190 mutex_unlock(&xt
[af
].mutex
);
194 err
= -EPROTOTYPE
; /* Found something. */
197 mutex_unlock(&xt
[af
].mutex
);
199 if (af
!= NFPROTO_UNSPEC
)
200 /* Try searching again in the family-independent list */
201 return xt_find_match(NFPROTO_UNSPEC
, name
, revision
);
205 EXPORT_SYMBOL(xt_find_match
);
208 xt_request_find_match(uint8_t nfproto
, const char *name
, uint8_t revision
)
210 struct xt_match
*match
;
212 if (strnlen(name
, XT_EXTENSION_MAXNAMELEN
) == XT_EXTENSION_MAXNAMELEN
)
213 return ERR_PTR(-EINVAL
);
215 match
= xt_find_match(nfproto
, name
, revision
);
217 request_module("%st_%s", xt_prefix
[nfproto
], name
);
218 match
= xt_find_match(nfproto
, name
, revision
);
223 EXPORT_SYMBOL_GPL(xt_request_find_match
);
225 /* Find target, grabs ref. Returns ERR_PTR() on error. */
226 struct xt_target
*xt_find_target(u8 af
, const char *name
, u8 revision
)
231 mutex_lock(&xt
[af
].mutex
);
232 list_for_each_entry(t
, &xt
[af
].target
, list
) {
233 if (strcmp(t
->name
, name
) == 0) {
234 if (t
->revision
== revision
) {
235 if (try_module_get(t
->me
)) {
236 mutex_unlock(&xt
[af
].mutex
);
240 err
= -EPROTOTYPE
; /* Found something. */
243 mutex_unlock(&xt
[af
].mutex
);
245 if (af
!= NFPROTO_UNSPEC
)
246 /* Try searching again in the family-independent list */
247 return xt_find_target(NFPROTO_UNSPEC
, name
, revision
);
251 EXPORT_SYMBOL(xt_find_target
);
253 struct xt_target
*xt_request_find_target(u8 af
, const char *name
, u8 revision
)
255 struct xt_target
*target
;
257 if (strnlen(name
, XT_EXTENSION_MAXNAMELEN
) == XT_EXTENSION_MAXNAMELEN
)
258 return ERR_PTR(-EINVAL
);
260 target
= xt_find_target(af
, name
, revision
);
261 if (IS_ERR(target
)) {
262 request_module("%st_%s", xt_prefix
[af
], name
);
263 target
= xt_find_target(af
, name
, revision
);
268 EXPORT_SYMBOL_GPL(xt_request_find_target
);
271 static int xt_obj_to_user(u16 __user
*psize
, u16 size
,
272 void __user
*pname
, const char *name
,
273 u8 __user
*prev
, u8 rev
)
275 if (put_user(size
, psize
))
277 if (copy_to_user(pname
, name
, strlen(name
) + 1))
279 if (put_user(rev
, prev
))
285 #define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE) \
286 xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size, \
287 U->u.user.name, K->u.kernel.TYPE->name, \
288 &U->u.user.revision, K->u.kernel.TYPE->revision)
290 int xt_data_to_user(void __user
*dst
, const void *src
,
291 int usersize
, int size
, int aligned_size
)
293 usersize
= usersize
? : size
;
294 if (copy_to_user(dst
, src
, usersize
))
296 if (usersize
!= aligned_size
&&
297 clear_user(dst
+ usersize
, aligned_size
- usersize
))
302 EXPORT_SYMBOL_GPL(xt_data_to_user
);
304 #define XT_DATA_TO_USER(U, K, TYPE) \
305 xt_data_to_user(U->data, K->data, \
306 K->u.kernel.TYPE->usersize, \
307 K->u.kernel.TYPE->TYPE##size, \
308 XT_ALIGN(K->u.kernel.TYPE->TYPE##size))
310 int xt_match_to_user(const struct xt_entry_match
*m
,
311 struct xt_entry_match __user
*u
)
313 return XT_OBJ_TO_USER(u
, m
, match
, 0) ||
314 XT_DATA_TO_USER(u
, m
, match
);
316 EXPORT_SYMBOL_GPL(xt_match_to_user
);
318 int xt_target_to_user(const struct xt_entry_target
*t
,
319 struct xt_entry_target __user
*u
)
321 return XT_OBJ_TO_USER(u
, t
, target
, 0) ||
322 XT_DATA_TO_USER(u
, t
, target
);
324 EXPORT_SYMBOL_GPL(xt_target_to_user
);
326 static int match_revfn(u8 af
, const char *name
, u8 revision
, int *bestp
)
328 const struct xt_match
*m
;
331 list_for_each_entry(m
, &xt
[af
].match
, list
) {
332 if (strcmp(m
->name
, name
) == 0) {
333 if (m
->revision
> *bestp
)
334 *bestp
= m
->revision
;
335 if (m
->revision
== revision
)
340 if (af
!= NFPROTO_UNSPEC
&& !have_rev
)
341 return match_revfn(NFPROTO_UNSPEC
, name
, revision
, bestp
);
346 static int target_revfn(u8 af
, const char *name
, u8 revision
, int *bestp
)
348 const struct xt_target
*t
;
351 list_for_each_entry(t
, &xt
[af
].target
, list
) {
352 if (strcmp(t
->name
, name
) == 0) {
353 if (t
->revision
> *bestp
)
354 *bestp
= t
->revision
;
355 if (t
->revision
== revision
)
360 if (af
!= NFPROTO_UNSPEC
&& !have_rev
)
361 return target_revfn(NFPROTO_UNSPEC
, name
, revision
, bestp
);
366 /* Returns true or false (if no such extension at all) */
367 int xt_find_revision(u8 af
, const char *name
, u8 revision
, int target
,
370 int have_rev
, best
= -1;
372 mutex_lock(&xt
[af
].mutex
);
374 have_rev
= target_revfn(af
, name
, revision
, &best
);
376 have_rev
= match_revfn(af
, name
, revision
, &best
);
377 mutex_unlock(&xt
[af
].mutex
);
379 /* Nothing at all? Return 0 to try loading module. */
387 *err
= -EPROTONOSUPPORT
;
390 EXPORT_SYMBOL_GPL(xt_find_revision
);
393 textify_hooks(char *buf
, size_t size
, unsigned int mask
, uint8_t nfproto
)
395 static const char *const inetbr_names
[] = {
396 "PREROUTING", "INPUT", "FORWARD",
397 "OUTPUT", "POSTROUTING", "BROUTING",
399 static const char *const arp_names
[] = {
400 "INPUT", "FORWARD", "OUTPUT",
402 const char *const *names
;
408 names
= (nfproto
== NFPROTO_ARP
) ? arp_names
: inetbr_names
;
409 max
= (nfproto
== NFPROTO_ARP
) ? ARRAY_SIZE(arp_names
) :
410 ARRAY_SIZE(inetbr_names
);
412 for (i
= 0; i
< max
; ++i
) {
413 if (!(mask
& (1 << i
)))
415 res
= snprintf(p
, size
, "%s%s", np
? "/" : "", names
[i
]);
426 int xt_check_match(struct xt_mtchk_param
*par
,
427 unsigned int size
, u_int8_t proto
, bool inv_proto
)
431 if (XT_ALIGN(par
->match
->matchsize
) != size
&&
432 par
->match
->matchsize
!= -1) {
434 * ebt_among is exempt from centralized matchsize checking
435 * because it uses a dynamic-size data set.
437 pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n",
438 xt_prefix
[par
->family
], par
->match
->name
,
439 par
->match
->revision
,
440 XT_ALIGN(par
->match
->matchsize
), size
);
443 if (par
->match
->table
!= NULL
&&
444 strcmp(par
->match
->table
, par
->table
) != 0) {
445 pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n",
446 xt_prefix
[par
->family
], par
->match
->name
,
447 par
->match
->table
, par
->table
);
450 if (par
->match
->hooks
&& (par
->hook_mask
& ~par
->match
->hooks
) != 0) {
451 char used
[64], allow
[64];
453 pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
454 xt_prefix
[par
->family
], par
->match
->name
,
455 textify_hooks(used
, sizeof(used
),
456 par
->hook_mask
, par
->family
),
457 textify_hooks(allow
, sizeof(allow
),
462 if (par
->match
->proto
&& (par
->match
->proto
!= proto
|| inv_proto
)) {
463 pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
464 xt_prefix
[par
->family
], par
->match
->name
,
468 if (par
->match
->checkentry
!= NULL
) {
469 ret
= par
->match
->checkentry(par
);
473 /* Flag up potential errors. */
478 EXPORT_SYMBOL_GPL(xt_check_match
);
480 /** xt_check_entry_match - check that matches end before start of target
482 * @match: beginning of xt_entry_match
483 * @target: beginning of this rules target (alleged end of matches)
484 * @alignment: alignment requirement of match structures
486 * Validates that all matches add up to the beginning of the target,
487 * and that each match covers at least the base structure size.
489 * Return: 0 on success, negative errno on failure.
491 static int xt_check_entry_match(const char *match
, const char *target
,
492 const size_t alignment
)
494 const struct xt_entry_match
*pos
;
495 int length
= target
- match
;
497 if (length
== 0) /* no matches */
500 pos
= (struct xt_entry_match
*)match
;
502 if ((unsigned long)pos
% alignment
)
505 if (length
< (int)sizeof(struct xt_entry_match
))
508 if (pos
->u
.match_size
< sizeof(struct xt_entry_match
))
511 if (pos
->u
.match_size
> length
)
514 length
-= pos
->u
.match_size
;
515 pos
= ((void *)((char *)(pos
) + (pos
)->u
.match_size
));
516 } while (length
> 0);
522 int xt_compat_add_offset(u_int8_t af
, unsigned int offset
, int delta
)
524 struct xt_af
*xp
= &xt
[af
];
526 if (!xp
->compat_tab
) {
529 xp
->compat_tab
= vmalloc(sizeof(struct compat_delta
) * xp
->number
);
535 if (xp
->cur
>= xp
->number
)
539 delta
+= xp
->compat_tab
[xp
->cur
- 1].delta
;
540 xp
->compat_tab
[xp
->cur
].offset
= offset
;
541 xp
->compat_tab
[xp
->cur
].delta
= delta
;
545 EXPORT_SYMBOL_GPL(xt_compat_add_offset
);
547 void xt_compat_flush_offsets(u_int8_t af
)
549 if (xt
[af
].compat_tab
) {
550 vfree(xt
[af
].compat_tab
);
551 xt
[af
].compat_tab
= NULL
;
556 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets
);
558 int xt_compat_calc_jump(u_int8_t af
, unsigned int offset
)
560 struct compat_delta
*tmp
= xt
[af
].compat_tab
;
561 int mid
, left
= 0, right
= xt
[af
].cur
- 1;
563 while (left
<= right
) {
564 mid
= (left
+ right
) >> 1;
565 if (offset
> tmp
[mid
].offset
)
567 else if (offset
< tmp
[mid
].offset
)
570 return mid
? tmp
[mid
- 1].delta
: 0;
572 return left
? tmp
[left
- 1].delta
: 0;
574 EXPORT_SYMBOL_GPL(xt_compat_calc_jump
);
576 void xt_compat_init_offsets(u_int8_t af
, unsigned int number
)
578 xt
[af
].number
= number
;
581 EXPORT_SYMBOL(xt_compat_init_offsets
);
583 int xt_compat_match_offset(const struct xt_match
*match
)
585 u_int16_t csize
= match
->compatsize
? : match
->matchsize
;
586 return XT_ALIGN(match
->matchsize
) - COMPAT_XT_ALIGN(csize
);
588 EXPORT_SYMBOL_GPL(xt_compat_match_offset
);
590 void xt_compat_match_from_user(struct xt_entry_match
*m
, void **dstptr
,
593 const struct xt_match
*match
= m
->u
.kernel
.match
;
594 struct compat_xt_entry_match
*cm
= (struct compat_xt_entry_match
*)m
;
595 int pad
, off
= xt_compat_match_offset(match
);
596 u_int16_t msize
= cm
->u
.user
.match_size
;
597 char name
[sizeof(m
->u
.user
.name
)];
600 memcpy(m
, cm
, sizeof(*cm
));
601 if (match
->compat_from_user
)
602 match
->compat_from_user(m
->data
, cm
->data
);
604 memcpy(m
->data
, cm
->data
, msize
- sizeof(*cm
));
605 pad
= XT_ALIGN(match
->matchsize
) - match
->matchsize
;
607 memset(m
->data
+ match
->matchsize
, 0, pad
);
610 m
->u
.user
.match_size
= msize
;
611 strlcpy(name
, match
->name
, sizeof(name
));
612 module_put(match
->me
);
613 strncpy(m
->u
.user
.name
, name
, sizeof(m
->u
.user
.name
));
618 EXPORT_SYMBOL_GPL(xt_compat_match_from_user
);
620 #define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE) \
621 xt_data_to_user(U->data, K->data, \
622 K->u.kernel.TYPE->usersize, \
624 COMPAT_XT_ALIGN(C_SIZE))
626 int xt_compat_match_to_user(const struct xt_entry_match
*m
,
627 void __user
**dstptr
, unsigned int *size
)
629 const struct xt_match
*match
= m
->u
.kernel
.match
;
630 struct compat_xt_entry_match __user
*cm
= *dstptr
;
631 int off
= xt_compat_match_offset(match
);
632 u_int16_t msize
= m
->u
.user
.match_size
- off
;
634 if (XT_OBJ_TO_USER(cm
, m
, match
, msize
))
637 if (match
->compat_to_user
) {
638 if (match
->compat_to_user((void __user
*)cm
->data
, m
->data
))
641 if (COMPAT_XT_DATA_TO_USER(cm
, m
, match
, msize
- sizeof(*cm
)))
649 EXPORT_SYMBOL_GPL(xt_compat_match_to_user
);
651 /* non-compat version may have padding after verdict */
652 struct compat_xt_standard_target
{
653 struct compat_xt_entry_target t
;
654 compat_uint_t verdict
;
657 int xt_compat_check_entry_offsets(const void *base
, const char *elems
,
658 unsigned int target_offset
,
659 unsigned int next_offset
)
661 long size_of_base_struct
= elems
- (const char *)base
;
662 const struct compat_xt_entry_target
*t
;
663 const char *e
= base
;
665 if (target_offset
< size_of_base_struct
)
668 if (target_offset
+ sizeof(*t
) > next_offset
)
671 t
= (void *)(e
+ target_offset
);
672 if (t
->u
.target_size
< sizeof(*t
))
675 if (target_offset
+ t
->u
.target_size
> next_offset
)
678 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) == 0 &&
679 COMPAT_XT_ALIGN(target_offset
+ sizeof(struct compat_xt_standard_target
)) != next_offset
)
682 /* compat_xt_entry match has less strict alignment requirements,
683 * otherwise they are identical. In case of padding differences
684 * we need to add compat version of xt_check_entry_match.
686 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match
) != sizeof(struct xt_entry_match
));
688 return xt_check_entry_match(elems
, base
+ target_offset
,
689 __alignof__(struct compat_xt_entry_match
));
691 EXPORT_SYMBOL(xt_compat_check_entry_offsets
);
692 #endif /* CONFIG_COMPAT */
695 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
697 * @base: pointer to arp/ip/ip6t_entry
698 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
699 * @target_offset: the arp/ip/ip6_t->target_offset
700 * @next_offset: the arp/ip/ip6_t->next_offset
702 * validates that target_offset and next_offset are sane and that all
703 * match sizes (if any) align with the target offset.
705 * This function does not validate the targets or matches themselves, it
706 * only tests that all the offsets and sizes are correct, that all
707 * match structures are aligned, and that the last structure ends where
708 * the target structure begins.
710 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
712 * The arp/ip/ip6t_entry structure @base must have passed following tests:
713 * - it must point to a valid memory location
714 * - base to base + next_offset must be accessible, i.e. not exceed allocated
717 * A well-formed entry looks like this:
719 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
720 * e->elems[]-----' | |
724 * target_offset---------------------------------' |
725 * next_offset---------------------------------------------------'
727 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
728 * This is where matches (if any) and the target reside.
729 * target_offset: beginning of target.
730 * next_offset: start of the next rule; also: size of this rule.
731 * Since targets have a minimum size, target_offset + minlen <= next_offset.
733 * Every match stores its size, sum of sizes must not exceed target_offset.
735 * Return: 0 on success, negative errno on failure.
737 int xt_check_entry_offsets(const void *base
,
739 unsigned int target_offset
,
740 unsigned int next_offset
)
742 long size_of_base_struct
= elems
- (const char *)base
;
743 const struct xt_entry_target
*t
;
744 const char *e
= base
;
746 /* target start is within the ip/ip6/arpt_entry struct */
747 if (target_offset
< size_of_base_struct
)
750 if (target_offset
+ sizeof(*t
) > next_offset
)
753 t
= (void *)(e
+ target_offset
);
754 if (t
->u
.target_size
< sizeof(*t
))
757 if (target_offset
+ t
->u
.target_size
> next_offset
)
760 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) == 0 &&
761 XT_ALIGN(target_offset
+ sizeof(struct xt_standard_target
)) != next_offset
)
764 return xt_check_entry_match(elems
, base
+ target_offset
,
765 __alignof__(struct xt_entry_match
));
767 EXPORT_SYMBOL(xt_check_entry_offsets
);
770 * xt_alloc_entry_offsets - allocate array to store rule head offsets
772 * @size: number of entries
774 * Return: NULL or kmalloc'd or vmalloc'd array
776 unsigned int *xt_alloc_entry_offsets(unsigned int size
)
778 return kvmalloc_array(size
, sizeof(unsigned int), GFP_KERNEL
| __GFP_ZERO
);
781 EXPORT_SYMBOL(xt_alloc_entry_offsets
);
784 * xt_find_jump_offset - check if target is a valid jump offset
786 * @offsets: array containing all valid rule start offsets of a rule blob
787 * @target: the jump target to search for
788 * @size: entries in @offset
790 bool xt_find_jump_offset(const unsigned int *offsets
,
791 unsigned int target
, unsigned int size
)
793 int m
, low
= 0, hi
= size
;
798 if (offsets
[m
] > target
)
800 else if (offsets
[m
] < target
)
808 EXPORT_SYMBOL(xt_find_jump_offset
);
810 int xt_check_target(struct xt_tgchk_param
*par
,
811 unsigned int size
, u_int8_t proto
, bool inv_proto
)
815 if (XT_ALIGN(par
->target
->targetsize
) != size
) {
816 pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
817 xt_prefix
[par
->family
], par
->target
->name
,
818 par
->target
->revision
,
819 XT_ALIGN(par
->target
->targetsize
), size
);
822 if (par
->target
->table
!= NULL
&&
823 strcmp(par
->target
->table
, par
->table
) != 0) {
824 pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
825 xt_prefix
[par
->family
], par
->target
->name
,
826 par
->target
->table
, par
->table
);
829 if (par
->target
->hooks
&& (par
->hook_mask
& ~par
->target
->hooks
) != 0) {
830 char used
[64], allow
[64];
832 pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
833 xt_prefix
[par
->family
], par
->target
->name
,
834 textify_hooks(used
, sizeof(used
),
835 par
->hook_mask
, par
->family
),
836 textify_hooks(allow
, sizeof(allow
),
841 if (par
->target
->proto
&& (par
->target
->proto
!= proto
|| inv_proto
)) {
842 pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
843 xt_prefix
[par
->family
], par
->target
->name
,
847 if (par
->target
->checkentry
!= NULL
) {
848 ret
= par
->target
->checkentry(par
);
852 /* Flag up potential errors. */
857 EXPORT_SYMBOL_GPL(xt_check_target
);
860 * xt_copy_counters_from_user - copy counters and metadata from userspace
862 * @user: src pointer to userspace memory
863 * @len: alleged size of userspace memory
864 * @info: where to store the xt_counters_info metadata
865 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
867 * Copies counter meta data from @user and stores it in @info.
869 * vmallocs memory to hold the counters, then copies the counter data
870 * from @user to the new memory and returns a pointer to it.
872 * If @compat is true, @info gets converted automatically to the 64bit
875 * The metadata associated with the counters is stored in @info.
877 * Return: returns pointer that caller has to test via IS_ERR().
878 * If IS_ERR is false, caller has to vfree the pointer.
880 void *xt_copy_counters_from_user(const void __user
*user
, unsigned int len
,
881 struct xt_counters_info
*info
, bool compat
)
888 /* structures only differ in size due to alignment */
889 struct compat_xt_counters_info compat_tmp
;
891 if (len
<= sizeof(compat_tmp
))
892 return ERR_PTR(-EINVAL
);
894 len
-= sizeof(compat_tmp
);
895 if (copy_from_user(&compat_tmp
, user
, sizeof(compat_tmp
)) != 0)
896 return ERR_PTR(-EFAULT
);
898 memcpy(info
->name
, compat_tmp
.name
, sizeof(info
->name
) - 1);
899 info
->num_counters
= compat_tmp
.num_counters
;
900 user
+= sizeof(compat_tmp
);
904 if (len
<= sizeof(*info
))
905 return ERR_PTR(-EINVAL
);
907 len
-= sizeof(*info
);
908 if (copy_from_user(info
, user
, sizeof(*info
)) != 0)
909 return ERR_PTR(-EFAULT
);
911 user
+= sizeof(*info
);
913 info
->name
[sizeof(info
->name
) - 1] = '\0';
915 size
= sizeof(struct xt_counters
);
916 size
*= info
->num_counters
;
918 if (size
!= (u64
)len
)
919 return ERR_PTR(-EINVAL
);
923 return ERR_PTR(-ENOMEM
);
925 if (copy_from_user(mem
, user
, len
) == 0)
929 return ERR_PTR(-EFAULT
);
931 EXPORT_SYMBOL_GPL(xt_copy_counters_from_user
);
934 int xt_compat_target_offset(const struct xt_target
*target
)
936 u_int16_t csize
= target
->compatsize
? : target
->targetsize
;
937 return XT_ALIGN(target
->targetsize
) - COMPAT_XT_ALIGN(csize
);
939 EXPORT_SYMBOL_GPL(xt_compat_target_offset
);
941 void xt_compat_target_from_user(struct xt_entry_target
*t
, void **dstptr
,
944 const struct xt_target
*target
= t
->u
.kernel
.target
;
945 struct compat_xt_entry_target
*ct
= (struct compat_xt_entry_target
*)t
;
946 int pad
, off
= xt_compat_target_offset(target
);
947 u_int16_t tsize
= ct
->u
.user
.target_size
;
948 char name
[sizeof(t
->u
.user
.name
)];
951 memcpy(t
, ct
, sizeof(*ct
));
952 if (target
->compat_from_user
)
953 target
->compat_from_user(t
->data
, ct
->data
);
955 memcpy(t
->data
, ct
->data
, tsize
- sizeof(*ct
));
956 pad
= XT_ALIGN(target
->targetsize
) - target
->targetsize
;
958 memset(t
->data
+ target
->targetsize
, 0, pad
);
961 t
->u
.user
.target_size
= tsize
;
962 strlcpy(name
, target
->name
, sizeof(name
));
963 module_put(target
->me
);
964 strncpy(t
->u
.user
.name
, name
, sizeof(t
->u
.user
.name
));
969 EXPORT_SYMBOL_GPL(xt_compat_target_from_user
);
971 int xt_compat_target_to_user(const struct xt_entry_target
*t
,
972 void __user
**dstptr
, unsigned int *size
)
974 const struct xt_target
*target
= t
->u
.kernel
.target
;
975 struct compat_xt_entry_target __user
*ct
= *dstptr
;
976 int off
= xt_compat_target_offset(target
);
977 u_int16_t tsize
= t
->u
.user
.target_size
- off
;
979 if (XT_OBJ_TO_USER(ct
, t
, target
, tsize
))
982 if (target
->compat_to_user
) {
983 if (target
->compat_to_user((void __user
*)ct
->data
, t
->data
))
986 if (COMPAT_XT_DATA_TO_USER(ct
, t
, target
, tsize
- sizeof(*ct
)))
994 EXPORT_SYMBOL_GPL(xt_compat_target_to_user
);
997 struct xt_table_info
*xt_alloc_table_info(unsigned int size
)
999 struct xt_table_info
*info
= NULL
;
1000 size_t sz
= sizeof(*info
) + size
;
1002 if (sz
< sizeof(*info
))
1005 /* __GFP_NORETRY is not fully supported by kvmalloc but it should
1006 * work reasonably well if sz is too large and bail out rather
1007 * than shoot all processes down before realizing there is nothing
1010 info
= kvmalloc(sz
, GFP_KERNEL
| __GFP_NORETRY
);
1014 memset(info
, 0, sizeof(*info
));
1018 EXPORT_SYMBOL(xt_alloc_table_info
);
1020 void xt_free_table_info(struct xt_table_info
*info
)
1024 if (info
->jumpstack
!= NULL
) {
1025 for_each_possible_cpu(cpu
)
1026 kvfree(info
->jumpstack
[cpu
]);
1027 kvfree(info
->jumpstack
);
1032 EXPORT_SYMBOL(xt_free_table_info
);
1034 /* Find table by name, grabs mutex & ref. Returns ERR_PTR on error. */
1035 struct xt_table
*xt_find_table_lock(struct net
*net
, u_int8_t af
,
1038 struct xt_table
*t
, *found
= NULL
;
1040 mutex_lock(&xt
[af
].mutex
);
1041 list_for_each_entry(t
, &net
->xt
.tables
[af
], list
)
1042 if (strcmp(t
->name
, name
) == 0 && try_module_get(t
->me
))
1045 if (net
== &init_net
)
1048 /* Table doesn't exist in this netns, re-try init */
1049 list_for_each_entry(t
, &init_net
.xt
.tables
[af
], list
) {
1052 if (strcmp(t
->name
, name
))
1054 if (!try_module_get(t
->me
))
1056 mutex_unlock(&xt
[af
].mutex
);
1057 err
= t
->table_init(net
);
1060 return ERR_PTR(err
);
1065 mutex_lock(&xt
[af
].mutex
);
1072 /* and once again: */
1073 list_for_each_entry(t
, &net
->xt
.tables
[af
], list
)
1074 if (strcmp(t
->name
, name
) == 0)
1077 module_put(found
->me
);
1079 mutex_unlock(&xt
[af
].mutex
);
1080 return ERR_PTR(-ENOENT
);
1082 EXPORT_SYMBOL_GPL(xt_find_table_lock
);
1084 struct xt_table
*xt_request_find_table_lock(struct net
*net
, u_int8_t af
,
1087 struct xt_table
*t
= xt_find_table_lock(net
, af
, name
);
1089 #ifdef CONFIG_MODULES
1091 int err
= request_module("%stable_%s", xt_prefix
[af
], name
);
1093 return ERR_PTR(err
);
1094 t
= xt_find_table_lock(net
, af
, name
);
1100 EXPORT_SYMBOL_GPL(xt_request_find_table_lock
);
1102 void xt_table_unlock(struct xt_table
*table
)
1104 mutex_unlock(&xt
[table
->af
].mutex
);
1106 EXPORT_SYMBOL_GPL(xt_table_unlock
);
1108 #ifdef CONFIG_COMPAT
1109 void xt_compat_lock(u_int8_t af
)
1111 mutex_lock(&xt
[af
].compat_mutex
);
1113 EXPORT_SYMBOL_GPL(xt_compat_lock
);
1115 void xt_compat_unlock(u_int8_t af
)
1117 mutex_unlock(&xt
[af
].compat_mutex
);
1119 EXPORT_SYMBOL_GPL(xt_compat_unlock
);
1122 DEFINE_PER_CPU(seqcount_t
, xt_recseq
);
1123 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq
);
1125 struct static_key xt_tee_enabled __read_mostly
;
1126 EXPORT_SYMBOL_GPL(xt_tee_enabled
);
1128 static int xt_jumpstack_alloc(struct xt_table_info
*i
)
1133 size
= sizeof(void **) * nr_cpu_ids
;
1134 if (size
> PAGE_SIZE
)
1135 i
->jumpstack
= kvzalloc(size
, GFP_KERNEL
);
1137 i
->jumpstack
= kzalloc(size
, GFP_KERNEL
);
1138 if (i
->jumpstack
== NULL
)
1141 /* ruleset without jumps -- no stack needed */
1142 if (i
->stacksize
== 0)
1145 /* Jumpstack needs to be able to record two full callchains, one
1146 * from the first rule set traversal, plus one table reentrancy
1147 * via -j TEE without clobbering the callchain that brought us to
1150 * This is done by allocating two jumpstacks per cpu, on reentry
1151 * the upper half of the stack is used.
1153 * see the jumpstack setup in ipt_do_table() for more details.
1155 size
= sizeof(void *) * i
->stacksize
* 2u;
1156 for_each_possible_cpu(cpu
) {
1157 i
->jumpstack
[cpu
] = kvmalloc_node(size
, GFP_KERNEL
,
1159 if (i
->jumpstack
[cpu
] == NULL
)
1161 * Freeing will be done later on by the callers. The
1162 * chain is: xt_replace_table -> __do_replace ->
1163 * do_replace -> xt_free_table_info.
1171 struct xt_table_info
*
1172 xt_replace_table(struct xt_table
*table
,
1173 unsigned int num_counters
,
1174 struct xt_table_info
*newinfo
,
1177 struct xt_table_info
*private;
1181 ret
= xt_jumpstack_alloc(newinfo
);
1187 /* Do the substitution. */
1189 private = table
->private;
1191 /* Check inside lock: is the old number correct? */
1192 if (num_counters
!= private->number
) {
1193 pr_debug("num_counters != table->private->number (%u/%u)\n",
1194 num_counters
, private->number
);
1200 newinfo
->initial_entries
= private->initial_entries
;
1202 * Ensure contents of newinfo are visible before assigning to
1206 table
->private = newinfo
;
1208 /* make sure all cpus see new ->private value */
1212 * Even though table entries have now been swapped, other CPU's
1213 * may still be using the old entries...
1217 /* ... so wait for even xt_recseq on all cpus */
1218 for_each_possible_cpu(cpu
) {
1219 seqcount_t
*s
= &per_cpu(xt_recseq
, cpu
);
1220 u32 seq
= raw_read_seqcount(s
);
1226 } while (seq
== raw_read_seqcount(s
));
1231 if (audit_enabled
) {
1232 audit_log(current
->audit_context
, GFP_KERNEL
,
1233 AUDIT_NETFILTER_CFG
,
1234 "table=%s family=%u entries=%u",
1235 table
->name
, table
->af
, private->number
);
1241 EXPORT_SYMBOL_GPL(xt_replace_table
);
1243 struct xt_table
*xt_register_table(struct net
*net
,
1244 const struct xt_table
*input_table
,
1245 struct xt_table_info
*bootstrap
,
1246 struct xt_table_info
*newinfo
)
1249 struct xt_table_info
*private;
1250 struct xt_table
*t
, *table
;
1252 /* Don't add one object to multiple lists. */
1253 table
= kmemdup(input_table
, sizeof(struct xt_table
), GFP_KERNEL
);
1259 mutex_lock(&xt
[table
->af
].mutex
);
1260 /* Don't autoload: we'd eat our tail... */
1261 list_for_each_entry(t
, &net
->xt
.tables
[table
->af
], list
) {
1262 if (strcmp(t
->name
, table
->name
) == 0) {
1268 /* Simplifies replace_table code. */
1269 table
->private = bootstrap
;
1271 if (!xt_replace_table(table
, 0, newinfo
, &ret
))
1274 private = table
->private;
1275 pr_debug("table->private->number = %u\n", private->number
);
1277 /* save number of initial entries */
1278 private->initial_entries
= private->number
;
1280 list_add(&table
->list
, &net
->xt
.tables
[table
->af
]);
1281 mutex_unlock(&xt
[table
->af
].mutex
);
1285 mutex_unlock(&xt
[table
->af
].mutex
);
1288 return ERR_PTR(ret
);
1290 EXPORT_SYMBOL_GPL(xt_register_table
);
1292 void *xt_unregister_table(struct xt_table
*table
)
1294 struct xt_table_info
*private;
1296 mutex_lock(&xt
[table
->af
].mutex
);
1297 private = table
->private;
1298 list_del(&table
->list
);
1299 mutex_unlock(&xt
[table
->af
].mutex
);
1304 EXPORT_SYMBOL_GPL(xt_unregister_table
);
1306 #ifdef CONFIG_PROC_FS
1307 struct xt_names_priv
{
1308 struct seq_net_private p
;
1311 static void *xt_table_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1313 struct xt_names_priv
*priv
= seq
->private;
1314 struct net
*net
= seq_file_net(seq
);
1315 u_int8_t af
= priv
->af
;
1317 mutex_lock(&xt
[af
].mutex
);
1318 return seq_list_start(&net
->xt
.tables
[af
], *pos
);
1321 static void *xt_table_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1323 struct xt_names_priv
*priv
= seq
->private;
1324 struct net
*net
= seq_file_net(seq
);
1325 u_int8_t af
= priv
->af
;
1327 return seq_list_next(v
, &net
->xt
.tables
[af
], pos
);
1330 static void xt_table_seq_stop(struct seq_file
*seq
, void *v
)
1332 struct xt_names_priv
*priv
= seq
->private;
1333 u_int8_t af
= priv
->af
;
1335 mutex_unlock(&xt
[af
].mutex
);
1338 static int xt_table_seq_show(struct seq_file
*seq
, void *v
)
1340 struct xt_table
*table
= list_entry(v
, struct xt_table
, list
);
1343 seq_printf(seq
, "%s\n", table
->name
);
1347 static const struct seq_operations xt_table_seq_ops
= {
1348 .start
= xt_table_seq_start
,
1349 .next
= xt_table_seq_next
,
1350 .stop
= xt_table_seq_stop
,
1351 .show
= xt_table_seq_show
,
1354 static int xt_table_open(struct inode
*inode
, struct file
*file
)
1357 struct xt_names_priv
*priv
;
1359 ret
= seq_open_net(inode
, file
, &xt_table_seq_ops
,
1360 sizeof(struct xt_names_priv
));
1362 priv
= ((struct seq_file
*)file
->private_data
)->private;
1363 priv
->af
= (unsigned long)PDE_DATA(inode
);
1368 static const struct file_operations xt_table_ops
= {
1369 .open
= xt_table_open
,
1371 .llseek
= seq_lseek
,
1372 .release
= seq_release_net
,
1376 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1377 * the multi-AF mutexes.
1379 struct nf_mttg_trav
{
1380 struct list_head
*head
, *curr
;
1381 uint8_t class, nfproto
;
1386 MTTG_TRAV_NFP_UNSPEC
,
1391 static void *xt_mttg_seq_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
,
1394 static const uint8_t next_class
[] = {
1395 [MTTG_TRAV_NFP_UNSPEC
] = MTTG_TRAV_NFP_SPEC
,
1396 [MTTG_TRAV_NFP_SPEC
] = MTTG_TRAV_DONE
,
1398 struct nf_mttg_trav
*trav
= seq
->private;
1400 switch (trav
->class) {
1401 case MTTG_TRAV_INIT
:
1402 trav
->class = MTTG_TRAV_NFP_UNSPEC
;
1403 mutex_lock(&xt
[NFPROTO_UNSPEC
].mutex
);
1404 trav
->head
= trav
->curr
= is_target
?
1405 &xt
[NFPROTO_UNSPEC
].target
: &xt
[NFPROTO_UNSPEC
].match
;
1407 case MTTG_TRAV_NFP_UNSPEC
:
1408 trav
->curr
= trav
->curr
->next
;
1409 if (trav
->curr
!= trav
->head
)
1411 mutex_unlock(&xt
[NFPROTO_UNSPEC
].mutex
);
1412 mutex_lock(&xt
[trav
->nfproto
].mutex
);
1413 trav
->head
= trav
->curr
= is_target
?
1414 &xt
[trav
->nfproto
].target
: &xt
[trav
->nfproto
].match
;
1415 trav
->class = next_class
[trav
->class];
1417 case MTTG_TRAV_NFP_SPEC
:
1418 trav
->curr
= trav
->curr
->next
;
1419 if (trav
->curr
!= trav
->head
)
1431 static void *xt_mttg_seq_start(struct seq_file
*seq
, loff_t
*pos
,
1434 struct nf_mttg_trav
*trav
= seq
->private;
1437 trav
->class = MTTG_TRAV_INIT
;
1438 for (j
= 0; j
< *pos
; ++j
)
1439 if (xt_mttg_seq_next(seq
, NULL
, NULL
, is_target
) == NULL
)
1444 static void xt_mttg_seq_stop(struct seq_file
*seq
, void *v
)
1446 struct nf_mttg_trav
*trav
= seq
->private;
1448 switch (trav
->class) {
1449 case MTTG_TRAV_NFP_UNSPEC
:
1450 mutex_unlock(&xt
[NFPROTO_UNSPEC
].mutex
);
1452 case MTTG_TRAV_NFP_SPEC
:
1453 mutex_unlock(&xt
[trav
->nfproto
].mutex
);
1458 static void *xt_match_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1460 return xt_mttg_seq_start(seq
, pos
, false);
1463 static void *xt_match_seq_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
1465 return xt_mttg_seq_next(seq
, v
, ppos
, false);
1468 static int xt_match_seq_show(struct seq_file
*seq
, void *v
)
1470 const struct nf_mttg_trav
*trav
= seq
->private;
1471 const struct xt_match
*match
;
1473 switch (trav
->class) {
1474 case MTTG_TRAV_NFP_UNSPEC
:
1475 case MTTG_TRAV_NFP_SPEC
:
1476 if (trav
->curr
== trav
->head
)
1478 match
= list_entry(trav
->curr
, struct xt_match
, list
);
1480 seq_printf(seq
, "%s\n", match
->name
);
1485 static const struct seq_operations xt_match_seq_ops
= {
1486 .start
= xt_match_seq_start
,
1487 .next
= xt_match_seq_next
,
1488 .stop
= xt_mttg_seq_stop
,
1489 .show
= xt_match_seq_show
,
1492 static int xt_match_open(struct inode
*inode
, struct file
*file
)
1494 struct nf_mttg_trav
*trav
;
1495 trav
= __seq_open_private(file
, &xt_match_seq_ops
, sizeof(*trav
));
1499 trav
->nfproto
= (unsigned long)PDE_DATA(inode
);
1503 static const struct file_operations xt_match_ops
= {
1504 .open
= xt_match_open
,
1506 .llseek
= seq_lseek
,
1507 .release
= seq_release_private
,
1510 static void *xt_target_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1512 return xt_mttg_seq_start(seq
, pos
, true);
1515 static void *xt_target_seq_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
1517 return xt_mttg_seq_next(seq
, v
, ppos
, true);
1520 static int xt_target_seq_show(struct seq_file
*seq
, void *v
)
1522 const struct nf_mttg_trav
*trav
= seq
->private;
1523 const struct xt_target
*target
;
1525 switch (trav
->class) {
1526 case MTTG_TRAV_NFP_UNSPEC
:
1527 case MTTG_TRAV_NFP_SPEC
:
1528 if (trav
->curr
== trav
->head
)
1530 target
= list_entry(trav
->curr
, struct xt_target
, list
);
1532 seq_printf(seq
, "%s\n", target
->name
);
1537 static const struct seq_operations xt_target_seq_ops
= {
1538 .start
= xt_target_seq_start
,
1539 .next
= xt_target_seq_next
,
1540 .stop
= xt_mttg_seq_stop
,
1541 .show
= xt_target_seq_show
,
1544 static int xt_target_open(struct inode
*inode
, struct file
*file
)
1546 struct nf_mttg_trav
*trav
;
1547 trav
= __seq_open_private(file
, &xt_target_seq_ops
, sizeof(*trav
));
1551 trav
->nfproto
= (unsigned long)PDE_DATA(inode
);
1555 static const struct file_operations xt_target_ops
= {
1556 .open
= xt_target_open
,
1558 .llseek
= seq_lseek
,
1559 .release
= seq_release_private
,
1562 #define FORMAT_TABLES "_tables_names"
1563 #define FORMAT_MATCHES "_tables_matches"
1564 #define FORMAT_TARGETS "_tables_targets"
1566 #endif /* CONFIG_PROC_FS */
1569 * xt_hook_ops_alloc - set up hooks for a new table
1570 * @table: table with metadata needed to set up hooks
1571 * @fn: Hook function
1573 * This function will create the nf_hook_ops that the x_table needs
1574 * to hand to xt_hook_link_net().
1576 struct nf_hook_ops
*
1577 xt_hook_ops_alloc(const struct xt_table
*table
, nf_hookfn
*fn
)
1579 unsigned int hook_mask
= table
->valid_hooks
;
1580 uint8_t i
, num_hooks
= hweight32(hook_mask
);
1582 struct nf_hook_ops
*ops
;
1585 return ERR_PTR(-EINVAL
);
1587 ops
= kcalloc(num_hooks
, sizeof(*ops
), GFP_KERNEL
);
1589 return ERR_PTR(-ENOMEM
);
1591 for (i
= 0, hooknum
= 0; i
< num_hooks
&& hook_mask
!= 0;
1592 hook_mask
>>= 1, ++hooknum
) {
1593 if (!(hook_mask
& 1))
1596 ops
[i
].pf
= table
->af
;
1597 ops
[i
].hooknum
= hooknum
;
1598 ops
[i
].priority
= table
->priority
;
1604 EXPORT_SYMBOL_GPL(xt_hook_ops_alloc
);
1606 int xt_proto_init(struct net
*net
, u_int8_t af
)
1608 #ifdef CONFIG_PROC_FS
1609 char buf
[XT_FUNCTION_MAXNAMELEN
];
1610 struct proc_dir_entry
*proc
;
1615 if (af
>= ARRAY_SIZE(xt_prefix
))
1619 #ifdef CONFIG_PROC_FS
1620 root_uid
= make_kuid(net
->user_ns
, 0);
1621 root_gid
= make_kgid(net
->user_ns
, 0);
1623 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1624 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
1625 proc
= proc_create_data(buf
, 0440, net
->proc_net
, &xt_table_ops
,
1626 (void *)(unsigned long)af
);
1629 if (uid_valid(root_uid
) && gid_valid(root_gid
))
1630 proc_set_user(proc
, root_uid
, root_gid
);
1632 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1633 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
1634 proc
= proc_create_data(buf
, 0440, net
->proc_net
, &xt_match_ops
,
1635 (void *)(unsigned long)af
);
1637 goto out_remove_tables
;
1638 if (uid_valid(root_uid
) && gid_valid(root_gid
))
1639 proc_set_user(proc
, root_uid
, root_gid
);
1641 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1642 strlcat(buf
, FORMAT_TARGETS
, sizeof(buf
));
1643 proc
= proc_create_data(buf
, 0440, net
->proc_net
, &xt_target_ops
,
1644 (void *)(unsigned long)af
);
1646 goto out_remove_matches
;
1647 if (uid_valid(root_uid
) && gid_valid(root_gid
))
1648 proc_set_user(proc
, root_uid
, root_gid
);
1653 #ifdef CONFIG_PROC_FS
1655 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1656 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
1657 remove_proc_entry(buf
, net
->proc_net
);
1660 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1661 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
1662 remove_proc_entry(buf
, net
->proc_net
);
1667 EXPORT_SYMBOL_GPL(xt_proto_init
);
1669 void xt_proto_fini(struct net
*net
, u_int8_t af
)
1671 #ifdef CONFIG_PROC_FS
1672 char buf
[XT_FUNCTION_MAXNAMELEN
];
1674 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1675 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
1676 remove_proc_entry(buf
, net
->proc_net
);
1678 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1679 strlcat(buf
, FORMAT_TARGETS
, sizeof(buf
));
1680 remove_proc_entry(buf
, net
->proc_net
);
1682 strlcpy(buf
, xt_prefix
[af
], sizeof(buf
));
1683 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
1684 remove_proc_entry(buf
, net
->proc_net
);
1685 #endif /*CONFIG_PROC_FS*/
1687 EXPORT_SYMBOL_GPL(xt_proto_fini
);
1690 * xt_percpu_counter_alloc - allocate x_tables rule counter
1692 * @state: pointer to xt_percpu allocation state
1693 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1695 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1696 * contain the address of the real (percpu) counter.
1698 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1699 * to fetch the real percpu counter.
1701 * To speed up allocation and improve data locality, a 4kb block is
1704 * xt_percpu_counter_alloc_state contains the base address of the
1705 * allocated page and the current sub-offset.
1707 * returns false on error.
1709 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state
*state
,
1710 struct xt_counters
*counter
)
1712 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE
< (sizeof(*counter
) * 2));
1714 if (nr_cpu_ids
<= 1)
1718 state
->mem
= __alloc_percpu(XT_PCPU_BLOCK_SIZE
,
1719 XT_PCPU_BLOCK_SIZE
);
1723 counter
->pcnt
= (__force
unsigned long)(state
->mem
+ state
->off
);
1724 state
->off
+= sizeof(*counter
);
1725 if (state
->off
> (XT_PCPU_BLOCK_SIZE
- sizeof(*counter
))) {
1731 EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc
);
1733 void xt_percpu_counter_free(struct xt_counters
*counters
)
1735 unsigned long pcnt
= counters
->pcnt
;
1737 if (nr_cpu_ids
> 1 && (pcnt
& (XT_PCPU_BLOCK_SIZE
- 1)) == 0)
1738 free_percpu((void __percpu
*)pcnt
);
1740 EXPORT_SYMBOL_GPL(xt_percpu_counter_free
);
1742 static int __net_init
xt_net_init(struct net
*net
)
1746 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++)
1747 INIT_LIST_HEAD(&net
->xt
.tables
[i
]);
1751 static void __net_exit
xt_net_exit(struct net
*net
)
1755 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++)
1756 WARN_ON_ONCE(!list_empty(&net
->xt
.tables
[i
]));
1759 static struct pernet_operations xt_net_ops
= {
1760 .init
= xt_net_init
,
1761 .exit
= xt_net_exit
,
1764 static int __init
xt_init(void)
1769 for_each_possible_cpu(i
) {
1770 seqcount_init(&per_cpu(xt_recseq
, i
));
1773 xt
= kmalloc(sizeof(struct xt_af
) * NFPROTO_NUMPROTO
, GFP_KERNEL
);
1777 for (i
= 0; i
< NFPROTO_NUMPROTO
; i
++) {
1778 mutex_init(&xt
[i
].mutex
);
1779 #ifdef CONFIG_COMPAT
1780 mutex_init(&xt
[i
].compat_mutex
);
1781 xt
[i
].compat_tab
= NULL
;
1783 INIT_LIST_HEAD(&xt
[i
].target
);
1784 INIT_LIST_HEAD(&xt
[i
].match
);
1786 rv
= register_pernet_subsys(&xt_net_ops
);
1792 static void __exit
xt_fini(void)
1794 unregister_pernet_subsys(&xt_net_ops
);
1798 module_init(xt_init
);
1799 module_exit(xt_fini
);