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_proc_name(const char *name, unsigned int size);
259 int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,
261 int xt_check_target(struct xt_tgchk_param *, unsigned int size, u_int8_t proto,
264 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
265 struct xt_counters_info *info, bool compat);
267 struct xt_table *xt_register_table(struct net *net,
268 const struct xt_table *table,
269 struct xt_table_info *bootstrap,
270 struct xt_table_info *newinfo);
271 void *xt_unregister_table(struct xt_table *table);
273 struct xt_table_info *xt_replace_table(struct xt_table *table,
274 unsigned int num_counters,
275 struct xt_table_info *newinfo,
278 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision);
279 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision);
280 struct xt_match *xt_request_find_match(u8 af, const char *name, u8 revision);
281 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision);
282 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
285 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
287 void xt_table_unlock(struct xt_table *t);
289 int xt_proto_init(struct net *net, u_int8_t af);
290 void xt_proto_fini(struct net *net, u_int8_t af);
292 struct xt_table_info *xt_alloc_table_info(unsigned int size);
293 void xt_free_table_info(struct xt_table_info *info);
296 * xt_recseq - recursive seqcount for netfilter use
298 * Packet processing changes the seqcount only if no recursion happened
299 * get_counters() can use read_seqcount_begin()/read_seqcount_retry(),
300 * because we use the normal seqcount convention :
301 * Low order bit set to 1 if a writer is active.
303 DECLARE_PER_CPU(seqcount_t, xt_recseq);
305 /* xt_tee_enabled - true if x_tables needs to handle reentrancy
307 * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
309 extern struct static_key xt_tee_enabled;
312 * xt_write_recseq_begin - start of a write section
314 * Begin packet processing : all readers must wait the end
315 * 1) Must be called with preemption disabled
316 * 2) softirqs must be disabled too (or we should use this_cpu_add())
318 * 1 if no recursion on this cpu
319 * 0 if recursion detected
321 static inline unsigned int xt_write_recseq_begin(void)
326 * Low order bit of sequence is set if we already
327 * called xt_write_recseq_begin().
329 addend = (__this_cpu_read(xt_recseq.sequence) + 1) & 1;
332 * This is kind of a write_seqcount_begin(), but addend is 0 or 1
333 * We dont check addend value to avoid a test and conditional jump,
334 * since addend is most likely 1
336 __this_cpu_add(xt_recseq.sequence, addend);
343 * xt_write_recseq_end - end of a write section
344 * @addend: return value from previous xt_write_recseq_begin()
346 * End packet processing : all readers can proceed
347 * 1) Must be called with preemption disabled
348 * 2) softirqs must be disabled too (or we should use this_cpu_add())
350 static inline void xt_write_recseq_end(unsigned int addend)
352 /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
354 __this_cpu_add(xt_recseq.sequence, addend);
358 * This helper is performance critical and must be inlined
360 static inline unsigned long ifname_compare_aligned(const char *_a,
364 const unsigned long *a = (const unsigned long *)_a;
365 const unsigned long *b = (const unsigned long *)_b;
366 const unsigned long *mask = (const unsigned long *)_mask;
369 ret = (a[0] ^ b[0]) & mask[0];
370 if (IFNAMSIZ > sizeof(unsigned long))
371 ret |= (a[1] ^ b[1]) & mask[1];
372 if (IFNAMSIZ > 2 * sizeof(unsigned long))
373 ret |= (a[2] ^ b[2]) & mask[2];
374 if (IFNAMSIZ > 3 * sizeof(unsigned long))
375 ret |= (a[3] ^ b[3]) & mask[3];
376 BUILD_BUG_ON(IFNAMSIZ > 4 * sizeof(unsigned long));
380 struct xt_percpu_counter_alloc_state {
382 const char __percpu *mem;
385 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
386 struct xt_counters *counter);
387 void xt_percpu_counter_free(struct xt_counters *cnt);
389 static inline struct xt_counters *
390 xt_get_this_cpu_counter(struct xt_counters *cnt)
393 return this_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt);
398 static inline struct xt_counters *
399 xt_get_per_cpu_counter(struct xt_counters *cnt, unsigned int cpu)
402 return per_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt, cpu);
407 struct nf_hook_ops *xt_hook_ops_alloc(const struct xt_table *, nf_hookfn *);
410 #include <net/compat.h>
412 struct compat_xt_entry_match {
415 u_int16_t match_size;
416 char name[XT_FUNCTION_MAXNAMELEN - 1];
420 u_int16_t match_size;
423 u_int16_t match_size;
425 unsigned char data[0];
428 struct compat_xt_entry_target {
431 u_int16_t target_size;
432 char name[XT_FUNCTION_MAXNAMELEN - 1];
436 u_int16_t target_size;
437 compat_uptr_t target;
439 u_int16_t target_size;
441 unsigned char data[0];
444 /* FIXME: this works only on 32 bit tasks
445 * need to change whole approach in order to calculate align as function of
446 * current task alignment */
448 struct compat_xt_counters {
449 compat_u64 pcnt, bcnt; /* Packet and byte counters */
452 struct compat_xt_counters_info {
453 char name[XT_TABLE_MAXNAMELEN];
454 compat_uint_t num_counters;
455 struct compat_xt_counters counters[0];
458 struct _compat_xt_align {
465 #define COMPAT_XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _compat_xt_align))
467 void xt_compat_lock(u_int8_t af);
468 void xt_compat_unlock(u_int8_t af);
470 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta);
471 void xt_compat_flush_offsets(u_int8_t af);
472 void xt_compat_init_offsets(u_int8_t af, unsigned int number);
473 int xt_compat_calc_jump(u_int8_t af, unsigned int offset);
475 int xt_compat_match_offset(const struct xt_match *match);
476 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
478 int xt_compat_match_to_user(const struct xt_entry_match *m,
479 void __user **dstptr, unsigned int *size);
481 int xt_compat_target_offset(const struct xt_target *target);
482 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
484 int xt_compat_target_to_user(const struct xt_entry_target *t,
485 void __user **dstptr, unsigned int *size);
486 int xt_compat_check_entry_offsets(const void *base, const char *elems,
487 unsigned int target_offset,
488 unsigned int next_offset);
490 #endif /* CONFIG_COMPAT */
491 #endif /* _X_TABLES_H */