1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/netdevice.h>
7 #include <linux/static_key.h>
8 #include <linux/netfilter.h>
9 #include <uapi/linux/netfilter/x_tables.h>
11 /* Test a struct->invflags and a boolean for inequality */
12 #define NF_INVF(ptr, flag, boolean) \
13 ((boolean) ^ !!((ptr)->invflags & (flag)))
16 * struct xt_action_param - parameters for matches/targets
18 * @match: the match extension
19 * @target: the target extension
20 * @matchinfo: per-match data
21 * @targetinfo: per-target data
22 * @state: pointer to hook state this packet came from
23 * @fragoff: packet is a fragment, this is the data offset
24 * @thoff: position of transport header relative to skb->data
26 * Fields written to by extensions:
28 * @hotdrop: drop packet if we had inspection problems
30 struct xt_action_param {
32 const struct xt_match *match;
33 const struct xt_target *target;
36 const void *matchinfo, *targinfo;
38 const struct nf_hook_state *state;
44 static inline struct net *xt_net(const struct xt_action_param *par)
46 return par->state->net;
49 static inline struct net_device *xt_in(const struct xt_action_param *par)
51 return par->state->in;
54 static inline const char *xt_inname(const struct xt_action_param *par)
56 return par->state->in->name;
59 static inline struct net_device *xt_out(const struct xt_action_param *par)
61 return par->state->out;
64 static inline const char *xt_outname(const struct xt_action_param *par)
66 return par->state->out->name;
69 static inline unsigned int xt_hooknum(const struct xt_action_param *par)
71 return par->state->hook;
74 static inline u_int8_t xt_family(const struct xt_action_param *par)
76 return par->state->pf;
80 * struct xt_mtchk_param - parameters for match extensions'
81 * checkentry functions
83 * @net: network namespace through which the check was invoked
84 * @table: table the rule is tried to be inserted into
85 * @entryinfo: the family-specific rule data
86 * (struct ipt_ip, ip6t_ip, arpt_arp or (note) ebt_entry)
87 * @match: struct xt_match through which this function was invoked
88 * @matchinfo: per-match data
89 * @hook_mask: via which hooks the new rule is reachable
90 * Other fields as above.
92 struct xt_mtchk_param {
95 const void *entryinfo;
96 const struct xt_match *match;
98 unsigned int hook_mask;
104 * struct xt_mdtor_param - match destructor parameters
107 struct xt_mtdtor_param {
109 const struct xt_match *match;
115 * struct xt_tgchk_param - parameters for target extensions'
116 * checkentry functions
118 * @entryinfo: the family-specific rule data
119 * (struct ipt_entry, ip6t_entry, arpt_entry, ebt_entry)
121 * Other fields see above.
123 struct xt_tgchk_param {
126 const void *entryinfo;
127 const struct xt_target *target;
129 unsigned int hook_mask;
134 /* Target destructor parameters */
135 struct xt_tgdtor_param {
137 const struct xt_target *target;
143 struct list_head list;
145 const char name[XT_EXTENSION_MAXNAMELEN];
148 /* Return true or false: return FALSE and set *hotdrop = 1 to
149 force immediate packet drop. */
150 /* Arguments changed since 2.6.9, as this must now handle
151 non-linear skb, using skb_header_pointer and
152 skb_ip_make_writable. */
153 bool (*match)(const struct sk_buff *skb,
154 struct xt_action_param *);
156 /* Called when user tries to insert an entry of this type. */
157 int (*checkentry)(const struct xt_mtchk_param *);
159 /* Called when entry of this type deleted. */
160 void (*destroy)(const struct xt_mtdtor_param *);
162 /* Called when userspace align differs from kernel space one */
163 void (*compat_from_user)(void *dst, const void *src);
164 int (*compat_to_user)(void __user *dst, const void *src);
166 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
170 unsigned int matchsize;
171 unsigned int usersize;
173 unsigned int compatsize;
176 unsigned short proto;
178 unsigned short family;
181 /* Registration hooks for targets. */
183 struct list_head list;
185 const char name[XT_EXTENSION_MAXNAMELEN];
188 /* Returns verdict. Argument order changed since 2.6.9, as this
189 must now handle non-linear skbs, using skb_copy_bits and
190 skb_ip_make_writable. */
191 unsigned int (*target)(struct sk_buff *skb,
192 const struct xt_action_param *);
194 /* Called when user tries to insert an entry of this type:
195 hook_mask is a bitmask of hooks from which it can be
197 /* Should return 0 on success or an error code otherwise (-Exxxx). */
198 int (*checkentry)(const struct xt_tgchk_param *);
200 /* Called when entry of this type deleted. */
201 void (*destroy)(const struct xt_tgdtor_param *);
203 /* Called when userspace align differs from kernel space one */
204 void (*compat_from_user)(void *dst, const void *src);
205 int (*compat_to_user)(void __user *dst, const void *src);
207 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
211 unsigned int targetsize;
212 unsigned int usersize;
214 unsigned int compatsize;
217 unsigned short proto;
219 unsigned short family;
222 /* Furniture shopping... */
224 struct list_head list;
226 /* What hooks you will enter on */
227 unsigned int valid_hooks;
229 /* Man behind the curtain... */
230 struct xt_table_info __rcu *private;
232 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
235 u_int8_t af; /* address/protocol family */
236 int priority; /* hook order */
238 /* called when table is needed in the given netns */
239 int (*table_init)(struct net *net);
241 /* A unique name... */
242 const char name[XT_TABLE_MAXNAMELEN];
245 #include <linux/netfilter_ipv4.h>
247 /* The table itself */
248 struct xt_table_info {
251 /* Number of entries: FIXME. --RR */
253 /* Initial number of entries. Needed for module usage count */
254 unsigned int initial_entries;
256 /* Entry points and underflows */
257 unsigned int hook_entry[NF_INET_NUMHOOKS];
258 unsigned int underflow[NF_INET_NUMHOOKS];
261 * Number of user chains. Since tables cannot have loops, at most
262 * @stacksize jumps (number of user chains) can possibly be made.
264 unsigned int stacksize;
267 unsigned char entries[] __aligned(8);
270 int xt_register_target(struct xt_target *target);
271 void xt_unregister_target(struct xt_target *target);
272 int xt_register_targets(struct xt_target *target, unsigned int n);
273 void xt_unregister_targets(struct xt_target *target, unsigned int n);
275 int xt_register_match(struct xt_match *target);
276 void xt_unregister_match(struct xt_match *target);
277 int xt_register_matches(struct xt_match *match, unsigned int n);
278 void xt_unregister_matches(struct xt_match *match, unsigned int n);
280 int xt_check_entry_offsets(const void *base, const char *elems,
281 unsigned int target_offset,
282 unsigned int next_offset);
284 int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks);
286 unsigned int *xt_alloc_entry_offsets(unsigned int size);
287 bool xt_find_jump_offset(const unsigned int *offsets,
288 unsigned int target, unsigned int size);
290 int xt_check_proc_name(const char *name, unsigned int size);
292 int xt_check_match(struct xt_mtchk_param *, unsigned int size, u16 proto,
294 int xt_check_target(struct xt_tgchk_param *, unsigned int size, u16 proto,
297 int xt_match_to_user(const struct xt_entry_match *m,
298 struct xt_entry_match __user *u);
299 int xt_target_to_user(const struct xt_entry_target *t,
300 struct xt_entry_target __user *u);
301 int xt_data_to_user(void __user *dst, const void *src,
302 int usersize, int size, int aligned_size);
304 void *xt_copy_counters(sockptr_t arg, unsigned int len,
305 struct xt_counters_info *info);
306 struct xt_counters *xt_counters_alloc(unsigned int counters);
308 struct xt_table *xt_register_table(struct net *net,
309 const struct xt_table *table,
310 struct xt_table_info *bootstrap,
311 struct xt_table_info *newinfo);
312 void *xt_unregister_table(struct xt_table *table);
314 struct xt_table_info *xt_replace_table(struct xt_table *table,
315 unsigned int num_counters,
316 struct xt_table_info *newinfo,
319 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision);
320 struct xt_match *xt_request_find_match(u8 af, const char *name, u8 revision);
321 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision);
322 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
325 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
327 struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
329 void xt_table_unlock(struct xt_table *t);
331 int xt_proto_init(struct net *net, u_int8_t af);
332 void xt_proto_fini(struct net *net, u_int8_t af);
334 struct xt_table_info *xt_alloc_table_info(unsigned int size);
335 void xt_free_table_info(struct xt_table_info *info);
338 * xt_recseq - recursive seqcount for netfilter use
340 * Packet processing changes the seqcount only if no recursion happened
341 * get_counters() can use read_seqcount_begin()/read_seqcount_retry(),
342 * because we use the normal seqcount convention :
343 * Low order bit set to 1 if a writer is active.
345 DECLARE_PER_CPU(seqcount_t, xt_recseq);
347 /* xt_tee_enabled - true if x_tables needs to handle reentrancy
349 * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
351 extern struct static_key xt_tee_enabled;
354 * xt_write_recseq_begin - start of a write section
356 * Begin packet processing : all readers must wait the end
357 * 1) Must be called with preemption disabled
358 * 2) softirqs must be disabled too (or we should use this_cpu_add())
360 * 1 if no recursion on this cpu
361 * 0 if recursion detected
363 static inline unsigned int xt_write_recseq_begin(void)
368 * Low order bit of sequence is set if we already
369 * called xt_write_recseq_begin().
371 addend = (__this_cpu_read(xt_recseq.sequence) + 1) & 1;
374 * This is kind of a write_seqcount_begin(), but addend is 0 or 1
375 * We dont check addend value to avoid a test and conditional jump,
376 * since addend is most likely 1
378 __this_cpu_add(xt_recseq.sequence, addend);
385 * xt_write_recseq_end - end of a write section
386 * @addend: return value from previous xt_write_recseq_begin()
388 * End packet processing : all readers can proceed
389 * 1) Must be called with preemption disabled
390 * 2) softirqs must be disabled too (or we should use this_cpu_add())
392 static inline void xt_write_recseq_end(unsigned int addend)
394 /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
396 __this_cpu_add(xt_recseq.sequence, addend);
400 * This helper is performance critical and must be inlined
402 static inline unsigned long ifname_compare_aligned(const char *_a,
406 const unsigned long *a = (const unsigned long *)_a;
407 const unsigned long *b = (const unsigned long *)_b;
408 const unsigned long *mask = (const unsigned long *)_mask;
411 ret = (a[0] ^ b[0]) & mask[0];
412 if (IFNAMSIZ > sizeof(unsigned long))
413 ret |= (a[1] ^ b[1]) & mask[1];
414 if (IFNAMSIZ > 2 * sizeof(unsigned long))
415 ret |= (a[2] ^ b[2]) & mask[2];
416 if (IFNAMSIZ > 3 * sizeof(unsigned long))
417 ret |= (a[3] ^ b[3]) & mask[3];
418 BUILD_BUG_ON(IFNAMSIZ > 4 * sizeof(unsigned long));
422 struct xt_percpu_counter_alloc_state {
424 const char __percpu *mem;
427 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
428 struct xt_counters *counter);
429 void xt_percpu_counter_free(struct xt_counters *cnt);
431 static inline struct xt_counters *
432 xt_get_this_cpu_counter(struct xt_counters *cnt)
435 return this_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt);
440 static inline struct xt_counters *
441 xt_get_per_cpu_counter(struct xt_counters *cnt, unsigned int cpu)
444 return per_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt, cpu);
449 struct nf_hook_ops *xt_hook_ops_alloc(const struct xt_table *, nf_hookfn *);
452 *xt_table_get_private_protected(const struct xt_table *table);
455 #include <net/compat.h>
457 struct compat_xt_entry_match {
460 u_int16_t match_size;
461 char name[XT_FUNCTION_MAXNAMELEN - 1];
465 u_int16_t match_size;
468 u_int16_t match_size;
470 unsigned char data[];
473 struct compat_xt_entry_target {
476 u_int16_t target_size;
477 char name[XT_FUNCTION_MAXNAMELEN - 1];
481 u_int16_t target_size;
482 compat_uptr_t target;
484 u_int16_t target_size;
486 unsigned char data[];
489 /* FIXME: this works only on 32 bit tasks
490 * need to change whole approach in order to calculate align as function of
491 * current task alignment */
493 struct compat_xt_counters {
494 compat_u64 pcnt, bcnt; /* Packet and byte counters */
497 struct compat_xt_counters_info {
498 char name[XT_TABLE_MAXNAMELEN];
499 compat_uint_t num_counters;
500 struct compat_xt_counters counters[];
503 struct _compat_xt_align {
510 #define COMPAT_XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _compat_xt_align))
512 void xt_compat_lock(u_int8_t af);
513 void xt_compat_unlock(u_int8_t af);
515 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta);
516 void xt_compat_flush_offsets(u_int8_t af);
517 int xt_compat_init_offsets(u8 af, unsigned int number);
518 int xt_compat_calc_jump(u_int8_t af, unsigned int offset);
520 int xt_compat_match_offset(const struct xt_match *match);
521 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
523 int xt_compat_match_to_user(const struct xt_entry_match *m,
524 void __user **dstptr, unsigned int *size);
526 int xt_compat_target_offset(const struct xt_target *target);
527 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
529 int xt_compat_target_to_user(const struct xt_entry_target *t,
530 void __user **dstptr, unsigned int *size);
531 int xt_compat_check_entry_offsets(const void *base, const char *elems,
532 unsigned int target_offset,
533 unsigned int next_offset);
535 #endif /* CONFIG_COMPAT */
536 #endif /* _X_TABLES_H */