5 #include <linux/netdevice.h>
6 #include <linux/static_key.h>
7 #include <uapi/linux/netfilter/x_tables.h>
9 /* Test a struct->invflags and a boolean for inequality */
10 #define NF_INVF(ptr, flag, boolean) \
11 ((boolean) ^ !!((ptr)->invflags & (flag)))
14 * struct xt_action_param - parameters for matches/targets
16 * @match: the match extension
17 * @target: the target extension
18 * @matchinfo: per-match data
19 * @targetinfo: per-target data
20 * @net network namespace through which the action was invoked
21 * @in: input netdevice
22 * @out: output netdevice
23 * @fragoff: packet is a fragment, this is the data offset
24 * @thoff: position of transport header relative to skb->data
25 * @hook: hook number given packet came from
26 * @family: Actual NFPROTO_* through which the function is invoked
27 * (helpful when match->family == NFPROTO_UNSPEC)
29 * Fields written to by extensions:
31 * @hotdrop: drop packet if we had inspection problems
33 struct xt_action_param
{
35 const struct xt_match
*match
;
36 const struct xt_target
*target
;
39 const void *matchinfo
, *targinfo
;
42 const struct net_device
*in
, *out
;
51 * struct xt_mtchk_param - parameters for match extensions'
52 * checkentry functions
54 * @net: network namespace through which the check was invoked
55 * @table: table the rule is tried to be inserted into
56 * @entryinfo: the family-specific rule data
57 * (struct ipt_ip, ip6t_ip, arpt_arp or (note) ebt_entry)
58 * @match: struct xt_match through which this function was invoked
59 * @matchinfo: per-match data
60 * @hook_mask: via which hooks the new rule is reachable
61 * Other fields as above.
63 struct xt_mtchk_param
{
66 const void *entryinfo
;
67 const struct xt_match
*match
;
69 unsigned int hook_mask
;
75 * struct xt_mdtor_param - match destructor parameters
78 struct xt_mtdtor_param
{
80 const struct xt_match
*match
;
86 * struct xt_tgchk_param - parameters for target extensions'
87 * checkentry functions
89 * @entryinfo: the family-specific rule data
90 * (struct ipt_entry, ip6t_entry, arpt_entry, ebt_entry)
92 * Other fields see above.
94 struct xt_tgchk_param
{
97 const void *entryinfo
;
98 const struct xt_target
*target
;
100 unsigned int hook_mask
;
105 /* Target destructor parameters */
106 struct xt_tgdtor_param
{
108 const struct xt_target
*target
;
114 struct list_head list
;
116 const char name
[XT_EXTENSION_MAXNAMELEN
];
119 /* Return true or false: return FALSE and set *hotdrop = 1 to
120 force immediate packet drop. */
121 /* Arguments changed since 2.6.9, as this must now handle
122 non-linear skb, using skb_header_pointer and
123 skb_ip_make_writable. */
124 bool (*match
)(const struct sk_buff
*skb
,
125 struct xt_action_param
*);
127 /* Called when user tries to insert an entry of this type. */
128 int (*checkentry
)(const struct xt_mtchk_param
*);
130 /* Called when entry of this type deleted. */
131 void (*destroy
)(const struct xt_mtdtor_param
*);
133 /* Called when userspace align differs from kernel space one */
134 void (*compat_from_user
)(void *dst
, const void *src
);
135 int (*compat_to_user
)(void __user
*dst
, const void *src
);
137 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
141 unsigned int matchsize
;
143 unsigned int compatsize
;
146 unsigned short proto
;
148 unsigned short family
;
151 /* Registration hooks for targets. */
153 struct list_head list
;
155 const char name
[XT_EXTENSION_MAXNAMELEN
];
158 /* Returns verdict. Argument order changed since 2.6.9, as this
159 must now handle non-linear skbs, using skb_copy_bits and
160 skb_ip_make_writable. */
161 unsigned int (*target
)(struct sk_buff
*skb
,
162 const struct xt_action_param
*);
164 /* Called when user tries to insert an entry of this type:
165 hook_mask is a bitmask of hooks from which it can be
167 /* Should return 0 on success or an error code otherwise (-Exxxx). */
168 int (*checkentry
)(const struct xt_tgchk_param
*);
170 /* Called when entry of this type deleted. */
171 void (*destroy
)(const struct xt_tgdtor_param
*);
173 /* Called when userspace align differs from kernel space one */
174 void (*compat_from_user
)(void *dst
, const void *src
);
175 int (*compat_to_user
)(void __user
*dst
, const void *src
);
177 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
181 unsigned int targetsize
;
183 unsigned int compatsize
;
186 unsigned short proto
;
188 unsigned short family
;
191 /* Furniture shopping... */
193 struct list_head list
;
195 /* What hooks you will enter on */
196 unsigned int valid_hooks
;
198 /* Man behind the curtain... */
199 struct xt_table_info
*private;
201 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
204 u_int8_t af
; /* address/protocol family */
205 int priority
; /* hook order */
207 /* called when table is needed in the given netns */
208 int (*table_init
)(struct net
*net
);
210 /* A unique name... */
211 const char name
[XT_TABLE_MAXNAMELEN
];
214 #include <linux/netfilter_ipv4.h>
216 /* The table itself */
217 struct xt_table_info
{
220 /* Number of entries: FIXME. --RR */
222 /* Initial number of entries. Needed for module usage count */
223 unsigned int initial_entries
;
225 /* Entry points and underflows */
226 unsigned int hook_entry
[NF_INET_NUMHOOKS
];
227 unsigned int underflow
[NF_INET_NUMHOOKS
];
230 * Number of user chains. Since tables cannot have loops, at most
231 * @stacksize jumps (number of user chains) can possibly be made.
233 unsigned int stacksize
;
236 unsigned char entries
[0] __aligned(8);
239 int xt_register_target(struct xt_target
*target
);
240 void xt_unregister_target(struct xt_target
*target
);
241 int xt_register_targets(struct xt_target
*target
, unsigned int n
);
242 void xt_unregister_targets(struct xt_target
*target
, unsigned int n
);
244 int xt_register_match(struct xt_match
*target
);
245 void xt_unregister_match(struct xt_match
*target
);
246 int xt_register_matches(struct xt_match
*match
, unsigned int n
);
247 void xt_unregister_matches(struct xt_match
*match
, unsigned int n
);
249 int xt_check_entry_offsets(const void *base
, const char *elems
,
250 unsigned int target_offset
,
251 unsigned int next_offset
);
253 unsigned int *xt_alloc_entry_offsets(unsigned int size
);
254 bool xt_find_jump_offset(const unsigned int *offsets
,
255 unsigned int target
, unsigned int size
);
257 int xt_check_match(struct xt_mtchk_param
*, unsigned int size
, u_int8_t proto
,
259 int xt_check_target(struct xt_tgchk_param
*, unsigned int size
, u_int8_t proto
,
262 void *xt_copy_counters_from_user(const void __user
*user
, unsigned int len
,
263 struct xt_counters_info
*info
, bool compat
);
265 struct xt_table
*xt_register_table(struct net
*net
,
266 const struct xt_table
*table
,
267 struct xt_table_info
*bootstrap
,
268 struct xt_table_info
*newinfo
);
269 void *xt_unregister_table(struct xt_table
*table
);
271 struct xt_table_info
*xt_replace_table(struct xt_table
*table
,
272 unsigned int num_counters
,
273 struct xt_table_info
*newinfo
,
276 struct xt_match
*xt_find_match(u8 af
, const char *name
, u8 revision
);
277 struct xt_target
*xt_find_target(u8 af
, const char *name
, u8 revision
);
278 struct xt_match
*xt_request_find_match(u8 af
, const char *name
, u8 revision
);
279 struct xt_target
*xt_request_find_target(u8 af
, const char *name
, u8 revision
);
280 int xt_find_revision(u8 af
, const char *name
, u8 revision
, int target
,
283 struct xt_table
*xt_find_table_lock(struct net
*net
, u_int8_t af
,
285 void xt_table_unlock(struct xt_table
*t
);
287 int xt_proto_init(struct net
*net
, u_int8_t af
);
288 void xt_proto_fini(struct net
*net
, u_int8_t af
);
290 struct xt_table_info
*xt_alloc_table_info(unsigned int size
);
291 void xt_free_table_info(struct xt_table_info
*info
);
294 * xt_recseq - recursive seqcount for netfilter use
296 * Packet processing changes the seqcount only if no recursion happened
297 * get_counters() can use read_seqcount_begin()/read_seqcount_retry(),
298 * because we use the normal seqcount convention :
299 * Low order bit set to 1 if a writer is active.
301 DECLARE_PER_CPU(seqcount_t
, xt_recseq
);
303 /* xt_tee_enabled - true if x_tables needs to handle reentrancy
305 * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
307 extern struct static_key xt_tee_enabled
;
310 * xt_write_recseq_begin - start of a write section
312 * Begin packet processing : all readers must wait the end
313 * 1) Must be called with preemption disabled
314 * 2) softirqs must be disabled too (or we should use this_cpu_add())
316 * 1 if no recursion on this cpu
317 * 0 if recursion detected
319 static inline unsigned int xt_write_recseq_begin(void)
324 * Low order bit of sequence is set if we already
325 * called xt_write_recseq_begin().
327 addend
= (__this_cpu_read(xt_recseq
.sequence
) + 1) & 1;
330 * This is kind of a write_seqcount_begin(), but addend is 0 or 1
331 * We dont check addend value to avoid a test and conditional jump,
332 * since addend is most likely 1
334 __this_cpu_add(xt_recseq
.sequence
, addend
);
341 * xt_write_recseq_end - end of a write section
342 * @addend: return value from previous xt_write_recseq_begin()
344 * End packet processing : all readers can proceed
345 * 1) Must be called with preemption disabled
346 * 2) softirqs must be disabled too (or we should use this_cpu_add())
348 static inline void xt_write_recseq_end(unsigned int addend
)
350 /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
352 __this_cpu_add(xt_recseq
.sequence
, addend
);
356 * This helper is performance critical and must be inlined
358 static inline unsigned long ifname_compare_aligned(const char *_a
,
362 const unsigned long *a
= (const unsigned long *)_a
;
363 const unsigned long *b
= (const unsigned long *)_b
;
364 const unsigned long *mask
= (const unsigned long *)_mask
;
367 ret
= (a
[0] ^ b
[0]) & mask
[0];
368 if (IFNAMSIZ
> sizeof(unsigned long))
369 ret
|= (a
[1] ^ b
[1]) & mask
[1];
370 if (IFNAMSIZ
> 2 * sizeof(unsigned long))
371 ret
|= (a
[2] ^ b
[2]) & mask
[2];
372 if (IFNAMSIZ
> 3 * sizeof(unsigned long))
373 ret
|= (a
[3] ^ b
[3]) & mask
[3];
374 BUILD_BUG_ON(IFNAMSIZ
> 4 * sizeof(unsigned long));
379 /* On SMP, ip(6)t_entry->counters.pcnt holds address of the
380 * real (percpu) counter. On !SMP, its just the packet count,
381 * so nothing needs to be done there.
383 * xt_percpu_counter_alloc returns the address of the percpu
384 * counter, or 0 on !SMP. We force an alignment of 16 bytes
385 * so that bytes/packets share a common cache line.
387 * Hence caller must use IS_ERR_VALUE to check for error, this
388 * allows us to return 0 for single core systems without forcing
389 * callers to deal with SMP vs. NONSMP issues.
391 static inline unsigned long xt_percpu_counter_alloc(void)
393 if (nr_cpu_ids
> 1) {
394 void __percpu
*res
= __alloc_percpu(sizeof(struct xt_counters
),
395 sizeof(struct xt_counters
));
400 return (__force
unsigned long) res
;
405 static inline void xt_percpu_counter_free(u64 pcnt
)
408 free_percpu((void __percpu
*) (unsigned long) pcnt
);
411 static inline struct xt_counters
*
412 xt_get_this_cpu_counter(struct xt_counters
*cnt
)
415 return this_cpu_ptr((void __percpu
*) (unsigned long) cnt
->pcnt
);
420 static inline struct xt_counters
*
421 xt_get_per_cpu_counter(struct xt_counters
*cnt
, unsigned int cpu
)
424 return per_cpu_ptr((void __percpu
*) (unsigned long) cnt
->pcnt
, cpu
);
429 struct nf_hook_ops
*xt_hook_ops_alloc(const struct xt_table
*, nf_hookfn
*);
432 #include <net/compat.h>
434 struct compat_xt_entry_match
{
437 u_int16_t match_size
;
438 char name
[XT_FUNCTION_MAXNAMELEN
- 1];
442 u_int16_t match_size
;
445 u_int16_t match_size
;
447 unsigned char data
[0];
450 struct compat_xt_entry_target
{
453 u_int16_t target_size
;
454 char name
[XT_FUNCTION_MAXNAMELEN
- 1];
458 u_int16_t target_size
;
459 compat_uptr_t target
;
461 u_int16_t target_size
;
463 unsigned char data
[0];
466 /* FIXME: this works only on 32 bit tasks
467 * need to change whole approach in order to calculate align as function of
468 * current task alignment */
470 struct compat_xt_counters
{
471 compat_u64 pcnt
, bcnt
; /* Packet and byte counters */
474 struct compat_xt_counters_info
{
475 char name
[XT_TABLE_MAXNAMELEN
];
476 compat_uint_t num_counters
;
477 struct compat_xt_counters counters
[0];
480 struct _compat_xt_align
{
487 #define COMPAT_XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _compat_xt_align))
489 void xt_compat_lock(u_int8_t af
);
490 void xt_compat_unlock(u_int8_t af
);
492 int xt_compat_add_offset(u_int8_t af
, unsigned int offset
, int delta
);
493 void xt_compat_flush_offsets(u_int8_t af
);
494 void xt_compat_init_offsets(u_int8_t af
, unsigned int number
);
495 int xt_compat_calc_jump(u_int8_t af
, unsigned int offset
);
497 int xt_compat_match_offset(const struct xt_match
*match
);
498 void xt_compat_match_from_user(struct xt_entry_match
*m
, void **dstptr
,
500 int xt_compat_match_to_user(const struct xt_entry_match
*m
,
501 void __user
**dstptr
, unsigned int *size
);
503 int xt_compat_target_offset(const struct xt_target
*target
);
504 void xt_compat_target_from_user(struct xt_entry_target
*t
, void **dstptr
,
506 int xt_compat_target_to_user(const struct xt_entry_target
*t
,
507 void __user
**dstptr
, unsigned int *size
);
508 int xt_compat_check_entry_offsets(const void *base
, const char *elems
,
509 unsigned int target_offset
,
510 unsigned int next_offset
);
512 #endif /* CONFIG_COMPAT */
513 #endif /* _X_TABLES_H */