3 #include <linux/kernel.h>
4 #include <linux/types.h>
6 #define XT_FUNCTION_MAXNAMELEN 30
7 #define XT_EXTENSION_MAXNAMELEN 29
8 #define XT_TABLE_MAXNAMELEN 32
10 struct xt_entry_match
{
15 /* Used by userspace */
16 char name
[XT_EXTENSION_MAXNAMELEN
];
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_EXTENSION_MAXNAMELEN
];
45 /* Used inside the kernel */
46 struct xt_target
*target
;
53 unsigned char data
[0];
56 #define XT_TARGET_INIT(__name, __size) \
59 .target_size = XT_ALIGN(__size), \
64 struct xt_standard_target
{
65 struct xt_entry_target target
;
69 struct xt_error_target
{
70 struct xt_entry_target target
;
71 char errorname
[XT_FUNCTION_MAXNAMELEN
];
74 /* The argument to IPT_SO_GET_REVISION_*. Returns highest revision
75 * kernel supports, if >= revision. */
76 struct xt_get_revision
{
77 char name
[XT_EXTENSION_MAXNAMELEN
];
81 /* CONTINUE verdict for targets */
82 #define XT_CONTINUE 0xFFFFFFFF
84 /* For standard target */
85 #define XT_RETURN (-NF_REPEAT - 1)
87 /* this is a dummy structure to find out the alignment requirement for a struct
88 * containing all the fundamental data types that are used in ipt_entry,
89 * ip6t_entry and arpt_entry. This sucks, and it is a hack. It will be my
90 * personal pleasure to remove it -HW
99 #define XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _xt_align))
101 /* Standard return verdict, or do jump. */
102 #define XT_STANDARD_TARGET ""
104 #define XT_ERROR_TARGET "ERROR"
106 #define SET_COUNTER(c,b,p) do { (c).bcnt = (b); (c).pcnt = (p); } while(0)
107 #define ADD_COUNTER(c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0)
110 __u64 pcnt
, bcnt
; /* Packet and byte counters */
113 /* The argument to IPT_SO_ADD_COUNTERS. */
114 struct xt_counters_info
{
116 char name
[XT_TABLE_MAXNAMELEN
];
118 unsigned int num_counters
;
120 /* The counters (actually `number' of these). */
121 struct xt_counters counters
[0];
124 #define XT_INV_PROTO 0x40 /* Invert the sense of PROTO. */
127 /* fn returns 0 to continue iteration */
128 #define XT_MATCH_ITERATE(type, e, fn, args...) \
132 struct xt_entry_match *__m; \
134 for (__i = sizeof(type); \
135 __i < (e)->target_offset; \
136 __i += __m->u.match_size) { \
137 __m = (void *)e + __i; \
139 __ret = fn(__m , ## args); \
146 /* fn returns 0 to continue iteration */
147 #define XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
149 unsigned int __i, __n; \
153 for (__i = 0, __n = 0; __i < (size); \
154 __i += __entry->next_offset, __n++) { \
155 __entry = (void *)(entries) + __i; \
159 __ret = fn(__entry , ## args); \
166 /* fn returns 0 to continue iteration */
167 #define XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
168 XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
170 #endif /* !__KERNEL__ */
172 /* pos is normally a struct ipt_entry/ip6t_entry/etc. */
173 #define xt_entry_foreach(pos, ehead, esize) \
174 for ((pos) = (typeof(pos))(ehead); \
175 (pos) < (typeof(pos))((char *)(ehead) + (esize)); \
176 (pos) = (typeof(pos))((char *)(pos) + (pos)->next_offset))
178 /* can only be xt_entry_match, so no use of typeof here */
179 #define xt_ematch_foreach(pos, entry) \
180 for ((pos) = (struct xt_entry_match *)entry->elems; \
181 (pos) < (struct xt_entry_match *)((char *)(entry) + \
182 (entry)->target_offset); \
183 (pos) = (struct xt_entry_match *)((char *)(pos) + \
184 (pos)->u.match_size))
188 #include <linux/netdevice.h>
191 * struct xt_action_param - parameters for matches/targets
193 * @match: the match extension
194 * @target: the target extension
195 * @matchinfo: per-match data
196 * @targetinfo: per-target data
197 * @in: input netdevice
198 * @out: output netdevice
199 * @fragoff: packet is a fragment, this is the data offset
200 * @thoff: position of transport header relative to skb->data
201 * @hook: hook number given packet came from
202 * @family: Actual NFPROTO_* through which the function is invoked
203 * (helpful when match->family == NFPROTO_UNSPEC)
205 * Fields written to by extensions:
207 * @hotdrop: drop packet if we had inspection problems
208 * Network namespace obtainable using dev_net(in/out)
210 struct xt_action_param
{
212 const struct xt_match
*match
;
213 const struct xt_target
*target
;
216 const void *matchinfo
, *targinfo
;
218 const struct net_device
*in
, *out
;
221 unsigned int hooknum
;
227 * struct xt_mtchk_param - parameters for match extensions'
228 * checkentry functions
230 * @net: network namespace through which the check was invoked
231 * @table: table the rule is tried to be inserted into
232 * @entryinfo: the family-specific rule data
233 * (struct ipt_ip, ip6t_ip, arpt_arp or (note) ebt_entry)
234 * @match: struct xt_match through which this function was invoked
235 * @matchinfo: per-match data
236 * @hook_mask: via which hooks the new rule is reachable
237 * Other fields as above.
239 struct xt_mtchk_param
{
242 const void *entryinfo
;
243 const struct xt_match
*match
;
245 unsigned int hook_mask
;
250 * struct xt_mdtor_param - match destructor parameters
253 struct xt_mtdtor_param
{
255 const struct xt_match
*match
;
261 * struct xt_tgchk_param - parameters for target extensions'
262 * checkentry functions
264 * @entryinfo: the family-specific rule data
265 * (struct ipt_entry, ip6t_entry, arpt_entry, ebt_entry)
267 * Other fields see above.
269 struct xt_tgchk_param
{
272 const void *entryinfo
;
273 const struct xt_target
*target
;
275 unsigned int hook_mask
;
279 /* Target destructor parameters */
280 struct xt_tgdtor_param
{
282 const struct xt_target
*target
;
288 struct list_head list
;
290 const char name
[XT_EXTENSION_MAXNAMELEN
];
293 /* Return true or false: return FALSE and set *hotdrop = 1 to
294 force immediate packet drop. */
295 /* Arguments changed since 2.6.9, as this must now handle
296 non-linear skb, using skb_header_pointer and
297 skb_ip_make_writable. */
298 bool (*match
)(const struct sk_buff
*skb
,
299 struct xt_action_param
*);
301 /* Called when user tries to insert an entry of this type. */
302 int (*checkentry
)(const struct xt_mtchk_param
*);
304 /* Called when entry of this type deleted. */
305 void (*destroy
)(const struct xt_mtdtor_param
*);
307 /* Called when userspace align differs from kernel space one */
308 void (*compat_from_user
)(void *dst
, const void *src
);
309 int (*compat_to_user
)(void __user
*dst
, const void *src
);
311 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
315 unsigned int matchsize
;
317 unsigned int compatsize
;
320 unsigned short proto
;
322 unsigned short family
;
325 /* Registration hooks for targets. */
327 struct list_head list
;
329 const char name
[XT_EXTENSION_MAXNAMELEN
];
332 /* Returns verdict. Argument order changed since 2.6.9, as this
333 must now handle non-linear skbs, using skb_copy_bits and
334 skb_ip_make_writable. */
335 unsigned int (*target
)(struct sk_buff
*skb
,
336 const struct xt_action_param
*);
338 /* Called when user tries to insert an entry of this type:
339 hook_mask is a bitmask of hooks from which it can be
341 /* Should return 0 on success or an error code otherwise (-Exxxx). */
342 int (*checkentry
)(const struct xt_tgchk_param
*);
344 /* Called when entry of this type deleted. */
345 void (*destroy
)(const struct xt_tgdtor_param
*);
347 /* Called when userspace align differs from kernel space one */
348 void (*compat_from_user
)(void *dst
, const void *src
);
349 int (*compat_to_user
)(void __user
*dst
, const void *src
);
351 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
355 unsigned int targetsize
;
357 unsigned int compatsize
;
360 unsigned short proto
;
362 unsigned short family
;
365 /* Furniture shopping... */
367 struct list_head list
;
369 /* What hooks you will enter on */
370 unsigned int valid_hooks
;
372 /* Man behind the curtain... */
373 struct xt_table_info
*private;
375 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
378 u_int8_t af
; /* address/protocol family */
379 int priority
; /* hook order */
381 /* A unique name... */
382 const char name
[XT_TABLE_MAXNAMELEN
];
385 #include <linux/netfilter_ipv4.h>
387 /* The table itself */
388 struct xt_table_info
{
391 /* Number of entries: FIXME. --RR */
393 /* Initial number of entries. Needed for module usage count */
394 unsigned int initial_entries
;
396 /* Entry points and underflows */
397 unsigned int hook_entry
[NF_INET_NUMHOOKS
];
398 unsigned int underflow
[NF_INET_NUMHOOKS
];
401 * Number of user chains. Since tables cannot have loops, at most
402 * @stacksize jumps (number of user chains) can possibly be made.
404 unsigned int stacksize
;
405 unsigned int __percpu
*stackptr
;
407 /* ipt_entry tables: one per CPU */
408 /* Note : this field MUST be the last one, see XT_TABLE_INFO_SZ */
412 #define XT_TABLE_INFO_SZ (offsetof(struct xt_table_info, entries) \
413 + nr_cpu_ids * sizeof(char *))
414 extern int xt_register_target(struct xt_target
*target
);
415 extern void xt_unregister_target(struct xt_target
*target
);
416 extern int xt_register_targets(struct xt_target
*target
, unsigned int n
);
417 extern void xt_unregister_targets(struct xt_target
*target
, unsigned int n
);
419 extern int xt_register_match(struct xt_match
*target
);
420 extern void xt_unregister_match(struct xt_match
*target
);
421 extern int xt_register_matches(struct xt_match
*match
, unsigned int n
);
422 extern void xt_unregister_matches(struct xt_match
*match
, unsigned int n
);
424 extern int xt_check_match(struct xt_mtchk_param
*,
425 unsigned int size
, u_int8_t proto
, bool inv_proto
);
426 extern int xt_check_target(struct xt_tgchk_param
*,
427 unsigned int size
, u_int8_t proto
, bool inv_proto
);
429 extern struct xt_table
*xt_register_table(struct net
*net
,
430 const struct xt_table
*table
,
431 struct xt_table_info
*bootstrap
,
432 struct xt_table_info
*newinfo
);
433 extern void *xt_unregister_table(struct xt_table
*table
);
435 extern struct xt_table_info
*xt_replace_table(struct xt_table
*table
,
436 unsigned int num_counters
,
437 struct xt_table_info
*newinfo
,
440 extern struct xt_match
*xt_find_match(u8 af
, const char *name
, u8 revision
);
441 extern struct xt_target
*xt_find_target(u8 af
, const char *name
, u8 revision
);
442 extern struct xt_match
*xt_request_find_match(u8 af
, const char *name
,
444 extern struct xt_target
*xt_request_find_target(u8 af
, const char *name
,
446 extern int xt_find_revision(u8 af
, const char *name
, u8 revision
,
447 int target
, int *err
);
449 extern struct xt_table
*xt_find_table_lock(struct net
*net
, u_int8_t af
,
451 extern void xt_table_unlock(struct xt_table
*t
);
453 extern int xt_proto_init(struct net
*net
, u_int8_t af
);
454 extern void xt_proto_fini(struct net
*net
, u_int8_t af
);
456 extern struct xt_table_info
*xt_alloc_table_info(unsigned int size
);
457 extern void xt_free_table_info(struct xt_table_info
*info
);
460 * Per-CPU spinlock associated with per-cpu table entries, and
461 * with a counter for the "reading" side that allows a recursive
462 * reader to avoid taking the lock and deadlocking.
464 * "reading" is used by ip/arp/ip6 tables rule processing which runs per-cpu.
465 * It needs to ensure that the rules are not being changed while the packet
466 * is being processed. In some cases, the read lock will be acquired
467 * twice on the same CPU; this is okay because of the count.
469 * "writing" is used when reading counters.
470 * During replace any readers that are using the old tables have to complete
471 * before freeing the old table. This is handled by the write locking
472 * necessary for reading the counters.
474 struct xt_info_lock
{
476 unsigned char readers
;
478 DECLARE_PER_CPU(struct xt_info_lock
, xt_info_locks
);
481 * Note: we need to ensure that preemption is disabled before acquiring
482 * the per-cpu-variable, so we do it as a two step process rather than
483 * using "spin_lock_bh()".
485 * We _also_ need to disable bottom half processing before updating our
486 * nesting count, to make sure that the only kind of re-entrancy is this
487 * code being called by itself: since the count+lock is not an atomic
488 * operation, we can allow no races.
490 * _Only_ that special combination of being per-cpu and never getting
491 * re-entered asynchronously means that the count is safe.
493 static inline void xt_info_rdlock_bh(void)
495 struct xt_info_lock
*lock
;
498 lock
= &__get_cpu_var(xt_info_locks
);
499 if (likely(!lock
->readers
++))
500 write_seqlock(&lock
->lock
);
503 static inline void xt_info_rdunlock_bh(void)
505 struct xt_info_lock
*lock
= &__get_cpu_var(xt_info_locks
);
507 if (likely(!--lock
->readers
))
508 write_sequnlock(&lock
->lock
);
513 * The "writer" side needs to get exclusive access to the lock,
514 * regardless of readers. This must be called with bottom half
515 * processing (and thus also preemption) disabled.
517 static inline void xt_info_wrlock(unsigned int cpu
)
519 write_seqlock(&per_cpu(xt_info_locks
, cpu
).lock
);
522 static inline void xt_info_wrunlock(unsigned int cpu
)
524 write_sequnlock(&per_cpu(xt_info_locks
, cpu
).lock
);
528 * This helper is performance critical and must be inlined
530 static inline unsigned long ifname_compare_aligned(const char *_a
,
534 const unsigned long *a
= (const unsigned long *)_a
;
535 const unsigned long *b
= (const unsigned long *)_b
;
536 const unsigned long *mask
= (const unsigned long *)_mask
;
539 ret
= (a
[0] ^ b
[0]) & mask
[0];
540 if (IFNAMSIZ
> sizeof(unsigned long))
541 ret
|= (a
[1] ^ b
[1]) & mask
[1];
542 if (IFNAMSIZ
> 2 * sizeof(unsigned long))
543 ret
|= (a
[2] ^ b
[2]) & mask
[2];
544 if (IFNAMSIZ
> 3 * sizeof(unsigned long))
545 ret
|= (a
[3] ^ b
[3]) & mask
[3];
546 BUILD_BUG_ON(IFNAMSIZ
> 4 * sizeof(unsigned long));
550 extern struct nf_hook_ops
*xt_hook_link(const struct xt_table
*, nf_hookfn
*);
551 extern void xt_hook_unlink(const struct xt_table
*, struct nf_hook_ops
*);
554 #include <net/compat.h>
556 struct compat_xt_entry_match
{
559 u_int16_t match_size
;
560 char name
[XT_FUNCTION_MAXNAMELEN
- 1];
564 u_int16_t match_size
;
567 u_int16_t match_size
;
569 unsigned char data
[0];
572 struct compat_xt_entry_target
{
575 u_int16_t target_size
;
576 char name
[XT_FUNCTION_MAXNAMELEN
- 1];
580 u_int16_t target_size
;
581 compat_uptr_t target
;
583 u_int16_t target_size
;
585 unsigned char data
[0];
588 /* FIXME: this works only on 32 bit tasks
589 * need to change whole approach in order to calculate align as function of
590 * current task alignment */
592 struct compat_xt_counters
{
593 compat_u64 pcnt
, bcnt
; /* Packet and byte counters */
596 struct compat_xt_counters_info
{
597 char name
[XT_TABLE_MAXNAMELEN
];
598 compat_uint_t num_counters
;
599 struct compat_xt_counters counters
[0];
602 struct _compat_xt_align
{
609 #define COMPAT_XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _compat_xt_align))
611 extern void xt_compat_lock(u_int8_t af
);
612 extern void xt_compat_unlock(u_int8_t af
);
614 extern int xt_compat_add_offset(u_int8_t af
, unsigned int offset
, int delta
);
615 extern void xt_compat_flush_offsets(u_int8_t af
);
616 extern void xt_compat_init_offsets(u_int8_t af
, unsigned int number
);
617 extern int xt_compat_calc_jump(u_int8_t af
, unsigned int offset
);
619 extern int xt_compat_match_offset(const struct xt_match
*match
);
620 extern int xt_compat_match_from_user(struct xt_entry_match
*m
,
621 void **dstptr
, unsigned int *size
);
622 extern int xt_compat_match_to_user(const struct xt_entry_match
*m
,
623 void __user
**dstptr
, unsigned int *size
);
625 extern int xt_compat_target_offset(const struct xt_target
*target
);
626 extern void xt_compat_target_from_user(struct xt_entry_target
*t
,
627 void **dstptr
, unsigned int *size
);
628 extern int xt_compat_target_to_user(const struct xt_entry_target
*t
,
629 void __user
**dstptr
, unsigned int *size
);
631 #endif /* CONFIG_COMPAT */
632 #endif /* __KERNEL__ */
634 #endif /* _X_TABLES_H */