4 #include <linux/types.h>
6 #define XT_FUNCTION_MAXNAMELEN 30
7 #define XT_TABLE_MAXNAMELEN 32
9 struct xt_entry_match
{
14 /* Used by userspace */
15 char name
[XT_FUNCTION_MAXNAMELEN
-1];
22 /* Used inside the kernel */
23 struct xt_match
*match
;
30 unsigned char data
[0];
33 struct xt_entry_target
{
38 /* Used by userspace */
39 char name
[XT_FUNCTION_MAXNAMELEN
-1];
46 /* Used inside the kernel */
47 struct xt_target
*target
;
54 unsigned char data
[0];
57 #define XT_TARGET_INIT(__name, __size) \
60 .target_size = XT_ALIGN(__size), \
65 struct xt_standard_target
{
66 struct xt_entry_target target
;
70 /* The argument to IPT_SO_GET_REVISION_*. Returns highest revision
71 * kernel supports, if >= revision. */
72 struct xt_get_revision
{
73 char name
[XT_FUNCTION_MAXNAMELEN
-1];
78 /* CONTINUE verdict for targets */
79 #define XT_CONTINUE 0xFFFFFFFF
81 /* For standard target */
82 #define XT_RETURN (-NF_REPEAT - 1)
84 /* this is a dummy structure to find out the alignment requirement for a struct
85 * containing all the fundamental data types that are used in ipt_entry,
86 * ip6t_entry and arpt_entry. This sucks, and it is a hack. It will be my
87 * personal pleasure to remove it -HW
96 #define XT_ALIGN(s) ALIGN((s), __alignof__(struct _xt_align))
98 /* Standard return verdict, or do jump. */
99 #define XT_STANDARD_TARGET ""
101 #define XT_ERROR_TARGET "ERROR"
103 #define SET_COUNTER(c,b,p) do { (c).bcnt = (b); (c).pcnt = (p); } while(0)
104 #define ADD_COUNTER(c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0)
107 __u64 pcnt
, bcnt
; /* Packet and byte counters */
110 /* The argument to IPT_SO_ADD_COUNTERS. */
111 struct xt_counters_info
{
113 char name
[XT_TABLE_MAXNAMELEN
];
115 unsigned int num_counters
;
117 /* The counters (actually `number' of these). */
118 struct xt_counters counters
[0];
121 #define XT_INV_PROTO 0x40 /* Invert the sense of PROTO. */
123 /* fn returns 0 to continue iteration */
124 #define XT_MATCH_ITERATE(type, e, fn, args...) \
128 struct xt_entry_match *__m; \
130 for (__i = sizeof(type); \
131 __i < (e)->target_offset; \
132 __i += __m->u.match_size) { \
133 __m = (void *)e + __i; \
135 __ret = fn(__m , ## args); \
142 /* fn returns 0 to continue iteration */
143 #define XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
145 unsigned int __i, __n; \
149 for (__i = 0, __n = 0; __i < (size); \
150 __i += __entry->next_offset, __n++) { \
151 __entry = (void *)(entries) + __i; \
155 __ret = fn(__entry , ## args); \
162 /* fn returns 0 to continue iteration */
163 #define XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
164 XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
168 #include <linux/netdevice.h>
171 * struct xt_match_param - parameters for match extensions' match functions
173 * @in: input netdevice
174 * @out: output netdevice
175 * @match: struct xt_match through which this function was invoked
176 * @matchinfo: per-match data
177 * @fragoff: packet is a fragment, this is the data offset
178 * @thoff: position of transport header relative to skb->data
179 * @hook: hook number given packet came from
180 * @family: Actual NFPROTO_* through which the function is invoked
181 * (helpful when match->family == NFPROTO_UNSPEC)
182 * @hotdrop: drop packet if we had inspection problems
184 struct xt_match_param
{
185 const struct net_device
*in
, *out
;
186 const struct xt_match
*match
;
187 const void *matchinfo
;
190 unsigned int hooknum
;
196 * struct xt_mtchk_param - parameters for match extensions'
197 * checkentry functions
199 * @table: table the rule is tried to be inserted into
200 * @entryinfo: the family-specific rule data
201 * (struct ipt_ip, ip6t_ip, ebt_entry)
202 * @match: struct xt_match through which this function was invoked
203 * @matchinfo: per-match data
204 * @hook_mask: via which hooks the new rule is reachable
206 struct xt_mtchk_param
{
209 const void *entryinfo
;
210 const struct xt_match
*match
;
212 unsigned int hook_mask
;
216 /* Match destructor parameters */
217 struct xt_mtdtor_param
{
219 const struct xt_match
*match
;
225 * struct xt_target_param - parameters for target extensions' target functions
227 * @hooknum: hook through which this target was invoked
228 * @target: struct xt_target through which this function was invoked
229 * @targinfo: per-target data
231 * Other fields see above.
233 struct xt_target_param
{
234 const struct net_device
*in
, *out
;
235 const struct xt_target
*target
;
236 const void *targinfo
;
237 unsigned int hooknum
;
242 * struct xt_tgchk_param - parameters for target extensions'
243 * checkentry functions
245 * @entryinfo: the family-specific rule data
246 * (struct ipt_entry, ip6t_entry, arpt_entry, ebt_entry)
248 * Other fields see above.
250 struct xt_tgchk_param
{
253 const void *entryinfo
;
254 const struct xt_target
*target
;
256 unsigned int hook_mask
;
260 /* Target destructor parameters */
261 struct xt_tgdtor_param
{
263 const struct xt_target
*target
;
269 struct list_head list
;
271 const char name
[XT_FUNCTION_MAXNAMELEN
-1];
274 /* Return true or false: return FALSE and set *hotdrop = 1 to
275 force immediate packet drop. */
276 /* Arguments changed since 2.6.9, as this must now handle
277 non-linear skb, using skb_header_pointer and
278 skb_ip_make_writable. */
279 bool (*match
)(const struct sk_buff
*skb
,
280 const struct xt_match_param
*);
282 /* Called when user tries to insert an entry of this type. */
283 bool (*checkentry
)(const struct xt_mtchk_param
*);
285 /* Called when entry of this type deleted. */
286 void (*destroy
)(const struct xt_mtdtor_param
*);
288 /* Called when userspace align differs from kernel space one */
289 void (*compat_from_user
)(void *dst
, const void *src
);
290 int (*compat_to_user
)(void __user
*dst
, const void *src
);
292 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
295 /* Free to use by each match */
299 unsigned int matchsize
;
301 unsigned int compatsize
;
304 unsigned short proto
;
306 unsigned short family
;
309 /* Registration hooks for targets. */
311 struct list_head list
;
313 const char name
[XT_FUNCTION_MAXNAMELEN
-1];
315 /* Returns verdict. Argument order changed since 2.6.9, as this
316 must now handle non-linear skbs, using skb_copy_bits and
317 skb_ip_make_writable. */
318 unsigned int (*target
)(struct sk_buff
*skb
,
319 const struct xt_target_param
*);
321 /* Called when user tries to insert an entry of this type:
322 hook_mask is a bitmask of hooks from which it can be
324 /* Should return true or false. */
325 bool (*checkentry
)(const struct xt_tgchk_param
*);
327 /* Called when entry of this type deleted. */
328 void (*destroy
)(const struct xt_tgdtor_param
*);
330 /* Called when userspace align differs from kernel space one */
331 void (*compat_from_user
)(void *dst
, const void *src
);
332 int (*compat_to_user
)(void __user
*dst
, const void *src
);
334 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
338 unsigned int targetsize
;
340 unsigned int compatsize
;
343 unsigned short proto
;
345 unsigned short family
;
349 /* Furniture shopping... */
351 struct list_head list
;
353 /* What hooks you will enter on */
354 unsigned int valid_hooks
;
356 /* Man behind the curtain... */
357 struct xt_table_info
*private;
359 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
362 u_int8_t af
; /* address/protocol family */
363 int priority
; /* hook order */
365 /* A unique name... */
366 const char name
[XT_TABLE_MAXNAMELEN
];
369 #include <linux/netfilter_ipv4.h>
371 /* The table itself */
372 struct xt_table_info
{
375 /* Number of entries: FIXME. --RR */
377 /* Initial number of entries. Needed for module usage count */
378 unsigned int initial_entries
;
380 /* Entry points and underflows */
381 unsigned int hook_entry
[NF_INET_NUMHOOKS
];
382 unsigned int underflow
[NF_INET_NUMHOOKS
];
384 /* ipt_entry tables: one per CPU */
385 /* Note : this field MUST be the last one, see XT_TABLE_INFO_SZ */
389 #define XT_TABLE_INFO_SZ (offsetof(struct xt_table_info, entries) \
390 + nr_cpu_ids * sizeof(char *))
391 extern int xt_register_target(struct xt_target
*target
);
392 extern void xt_unregister_target(struct xt_target
*target
);
393 extern int xt_register_targets(struct xt_target
*target
, unsigned int n
);
394 extern void xt_unregister_targets(struct xt_target
*target
, unsigned int n
);
396 extern int xt_register_match(struct xt_match
*target
);
397 extern void xt_unregister_match(struct xt_match
*target
);
398 extern int xt_register_matches(struct xt_match
*match
, unsigned int n
);
399 extern void xt_unregister_matches(struct xt_match
*match
, unsigned int n
);
401 extern int xt_check_match(struct xt_mtchk_param
*,
402 unsigned int size
, u_int8_t proto
, bool inv_proto
);
403 extern int xt_check_target(struct xt_tgchk_param
*,
404 unsigned int size
, u_int8_t proto
, bool inv_proto
);
406 extern struct xt_table
*xt_register_table(struct net
*net
,
407 const struct xt_table
*table
,
408 struct xt_table_info
*bootstrap
,
409 struct xt_table_info
*newinfo
);
410 extern void *xt_unregister_table(struct xt_table
*table
);
412 extern struct xt_table_info
*xt_replace_table(struct xt_table
*table
,
413 unsigned int num_counters
,
414 struct xt_table_info
*newinfo
,
417 extern struct xt_match
*xt_find_match(u8 af
, const char *name
, u8 revision
);
418 extern struct xt_target
*xt_find_target(u8 af
, const char *name
, u8 revision
);
419 extern struct xt_target
*xt_request_find_target(u8 af
, const char *name
,
421 extern int xt_find_revision(u8 af
, const char *name
, u8 revision
,
422 int target
, int *err
);
424 extern struct xt_table
*xt_find_table_lock(struct net
*net
, u_int8_t af
,
426 extern void xt_table_unlock(struct xt_table
*t
);
428 extern int xt_proto_init(struct net
*net
, u_int8_t af
);
429 extern void xt_proto_fini(struct net
*net
, u_int8_t af
);
431 extern struct xt_table_info
*xt_alloc_table_info(unsigned int size
);
432 extern void xt_free_table_info(struct xt_table_info
*info
);
435 * Per-CPU spinlock associated with per-cpu table entries, and
436 * with a counter for the "reading" side that allows a recursive
437 * reader to avoid taking the lock and deadlocking.
439 * "reading" is used by ip/arp/ip6 tables rule processing which runs per-cpu.
440 * It needs to ensure that the rules are not being changed while the packet
441 * is being processed. In some cases, the read lock will be acquired
442 * twice on the same CPU; this is okay because of the count.
444 * "writing" is used when reading counters.
445 * During replace any readers that are using the old tables have to complete
446 * before freeing the old table. This is handled by the write locking
447 * necessary for reading the counters.
449 struct xt_info_lock
{
451 unsigned char readers
;
453 DECLARE_PER_CPU(struct xt_info_lock
, xt_info_locks
);
456 * Note: we need to ensure that preemption is disabled before acquiring
457 * the per-cpu-variable, so we do it as a two step process rather than
458 * using "spin_lock_bh()".
460 * We _also_ need to disable bottom half processing before updating our
461 * nesting count, to make sure that the only kind of re-entrancy is this
462 * code being called by itself: since the count+lock is not an atomic
463 * operation, we can allow no races.
465 * _Only_ that special combination of being per-cpu and never getting
466 * re-entered asynchronously means that the count is safe.
468 static inline void xt_info_rdlock_bh(void)
470 struct xt_info_lock
*lock
;
473 lock
= &__get_cpu_var(xt_info_locks
);
474 if (likely(!lock
->readers
++))
475 spin_lock(&lock
->lock
);
478 static inline void xt_info_rdunlock_bh(void)
480 struct xt_info_lock
*lock
= &__get_cpu_var(xt_info_locks
);
482 if (likely(!--lock
->readers
))
483 spin_unlock(&lock
->lock
);
488 * The "writer" side needs to get exclusive access to the lock,
489 * regardless of readers. This must be called with bottom half
490 * processing (and thus also preemption) disabled.
492 static inline void xt_info_wrlock(unsigned int cpu
)
494 spin_lock(&per_cpu(xt_info_locks
, cpu
).lock
);
497 static inline void xt_info_wrunlock(unsigned int cpu
)
499 spin_unlock(&per_cpu(xt_info_locks
, cpu
).lock
);
503 * This helper is performance critical and must be inlined
505 static inline unsigned long ifname_compare_aligned(const char *_a
,
509 const unsigned long *a
= (const unsigned long *)_a
;
510 const unsigned long *b
= (const unsigned long *)_b
;
511 const unsigned long *mask
= (const unsigned long *)_mask
;
514 ret
= (a
[0] ^ b
[0]) & mask
[0];
515 if (IFNAMSIZ
> sizeof(unsigned long))
516 ret
|= (a
[1] ^ b
[1]) & mask
[1];
517 if (IFNAMSIZ
> 2 * sizeof(unsigned long))
518 ret
|= (a
[2] ^ b
[2]) & mask
[2];
519 if (IFNAMSIZ
> 3 * sizeof(unsigned long))
520 ret
|= (a
[3] ^ b
[3]) & mask
[3];
521 BUILD_BUG_ON(IFNAMSIZ
> 4 * sizeof(unsigned long));
525 extern struct nf_hook_ops
*xt_hook_link(const struct xt_table
*, nf_hookfn
*);
526 extern void xt_hook_unlink(const struct xt_table
*, struct nf_hook_ops
*);
529 #include <net/compat.h>
531 struct compat_xt_entry_match
{
534 u_int16_t match_size
;
535 char name
[XT_FUNCTION_MAXNAMELEN
- 1];
539 u_int16_t match_size
;
542 u_int16_t match_size
;
544 unsigned char data
[0];
547 struct compat_xt_entry_target
{
550 u_int16_t target_size
;
551 char name
[XT_FUNCTION_MAXNAMELEN
- 1];
555 u_int16_t target_size
;
556 compat_uptr_t target
;
558 u_int16_t target_size
;
560 unsigned char data
[0];
563 /* FIXME: this works only on 32 bit tasks
564 * need to change whole approach in order to calculate align as function of
565 * current task alignment */
567 struct compat_xt_counters
{
568 compat_u64 pcnt
, bcnt
; /* Packet and byte counters */
571 struct compat_xt_counters_info
{
572 char name
[XT_TABLE_MAXNAMELEN
];
573 compat_uint_t num_counters
;
574 struct compat_xt_counters counters
[0];
577 struct _compat_xt_align
{
584 #define COMPAT_XT_ALIGN(s) ALIGN((s), __alignof__(struct _compat_xt_align))
586 extern void xt_compat_lock(u_int8_t af
);
587 extern void xt_compat_unlock(u_int8_t af
);
589 extern int xt_compat_add_offset(u_int8_t af
, unsigned int offset
, short delta
);
590 extern void xt_compat_flush_offsets(u_int8_t af
);
591 extern short xt_compat_calc_jump(u_int8_t af
, unsigned int offset
);
593 extern int xt_compat_match_offset(const struct xt_match
*match
);
594 extern int xt_compat_match_from_user(struct xt_entry_match
*m
,
595 void **dstptr
, unsigned int *size
);
596 extern int xt_compat_match_to_user(const struct xt_entry_match
*m
,
597 void __user
**dstptr
, unsigned int *size
);
599 extern int xt_compat_target_offset(const struct xt_target
*target
);
600 extern void xt_compat_target_from_user(struct xt_entry_target
*t
,
601 void **dstptr
, unsigned int *size
);
602 extern int xt_compat_target_to_user(const struct xt_entry_target
*t
,
603 void __user
**dstptr
, unsigned int *size
);
605 #endif /* CONFIG_COMPAT */
606 #endif /* __KERNEL__ */
608 #endif /* _X_TABLES_H */