xref: /linux/include/linux/netfilter/x_tables.h (revision fcee7d82f27d6a8b1ddc5bbefda59b4e441e9bc0)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _X_TABLES_H
3 #define _X_TABLES_H
4 
5 
6 #include <linux/netdevice.h>
7 #include <linux/static_key.h>
8 #include <linux/netfilter.h>
9 #include <uapi/linux/netfilter/x_tables.h>
10 
11 /* Test a struct->invflags and a boolean for inequality */
12 #define NF_INVF(ptr, flag, boolean)					\
13 	((boolean) ^ !!((ptr)->invflags & (flag)))
14 
15 /**
16  * struct xt_action_param - parameters for matches/targets
17  *
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
25  *
26  * Fields written to by extensions:
27  *
28  * @hotdrop:	drop packet if we had inspection problems
29  */
30 struct xt_action_param {
31 	union {
32 		const struct xt_match *match;
33 		const struct xt_target *target;
34 	};
35 	union {
36 		const void *matchinfo, *targinfo;
37 	};
38 	const struct nf_hook_state *state;
39 	unsigned int thoff;
40 	u16 fragoff;
41 	bool hotdrop;
42 };
43 
xt_net(const struct xt_action_param * par)44 static inline struct net *xt_net(const struct xt_action_param *par)
45 {
46 	return par->state->net;
47 }
48 
xt_in(const struct xt_action_param * par)49 static inline struct net_device *xt_in(const struct xt_action_param *par)
50 {
51 	return par->state->in;
52 }
53 
xt_out(const struct xt_action_param * par)54 static inline struct net_device *xt_out(const struct xt_action_param *par)
55 {
56 	return par->state->out;
57 }
58 
xt_hooknum(const struct xt_action_param * par)59 static inline unsigned int xt_hooknum(const struct xt_action_param *par)
60 {
61 	return par->state->hook;
62 }
63 
xt_family(const struct xt_action_param * par)64 static inline u_int8_t xt_family(const struct xt_action_param *par)
65 {
66 	return par->state->pf;
67 }
68 
69 /**
70  * struct xt_mtchk_param - parameters for match extensions'
71  * checkentry functions
72  *
73  * @net:	network namespace through which the check was invoked
74  * @table:	table the rule is tried to be inserted into
75  * @entryinfo:	the family-specific rule data
76  * 		(struct ipt_ip, ip6t_ip, arpt_arp or (note) ebt_entry)
77  * @match:	struct xt_match through which this function was invoked
78  * @matchinfo:	per-match data
79  * @hook_mask:	via which hooks the new rule is reachable
80  * Other fields as above.
81  */
82 struct xt_mtchk_param {
83 	struct net *net;
84 	const char *table;
85 	const void *entryinfo;
86 	const struct xt_match *match;
87 	void *matchinfo;
88 	unsigned int hook_mask;
89 	u_int8_t family;
90 	bool nft_compat;
91 };
92 
93 /**
94  * struct xt_mdtor_param - match destructor parameters
95  * Fields as above.
96  */
97 struct xt_mtdtor_param {
98 	struct net *net;
99 	const struct xt_match *match;
100 	void *matchinfo;
101 	u_int8_t family;
102 };
103 
104 /**
105  * struct xt_tgchk_param - parameters for target extensions'
106  * checkentry functions
107  *
108  * @entryinfo:	the family-specific rule data
109  * 		(struct ipt_entry, ip6t_entry, arpt_entry, ebt_entry)
110  *
111  * Other fields see above.
112  */
113 struct xt_tgchk_param {
114 	struct net *net;
115 	const char *table;
116 	const void *entryinfo;
117 	const struct xt_target *target;
118 	void *targinfo;
119 	unsigned int hook_mask;
120 	u_int8_t family;
121 	bool nft_compat;
122 };
123 
124 /* Target destructor parameters */
125 struct xt_tgdtor_param {
126 	struct net *net;
127 	const struct xt_target *target;
128 	void *targinfo;
129 	u_int8_t family;
130 };
131 
132 struct xt_match {
133 	struct list_head list;
134 
135 	const char name[XT_EXTENSION_MAXNAMELEN];
136 	u_int8_t revision;
137 
138 	/* Return true or false: return FALSE and set *hotdrop = 1 to
139            force immediate packet drop. */
140 	/* Arguments changed since 2.6.9, as this must now handle
141 	   non-linear skb, using skb_header_pointer and
142 	   skb_ip_make_writable. */
143 	bool (*match)(const struct sk_buff *skb,
144 		      struct xt_action_param *);
145 
146 	/* Called when user tries to insert an entry of this type. */
147 	int (*checkentry)(const struct xt_mtchk_param *);
148 
149 	/* Called to validate hooks based on the match configuration. */
150 	int (*check_hooks)(const struct xt_mtchk_param *);
151 
152 	/* Called when entry of this type deleted. */
153 	void (*destroy)(const struct xt_mtdtor_param *);
154 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
155 	/* Called when userspace align differs from kernel space one */
156 	void (*compat_from_user)(void *dst, const void *src);
157 	int (*compat_to_user)(void __user *dst, const void *src);
158 #endif
159 	/* Set this to THIS_MODULE if you are a module, otherwise NULL */
160 	struct module *me;
161 
162 	const char *table;
163 	unsigned int matchsize;
164 	unsigned int usersize;
165 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
166 	unsigned int compatsize;
167 #endif
168 	unsigned int hooks;
169 	unsigned short proto;
170 
171 	unsigned short family;
172 };
173 
174 /* Registration hooks for targets. */
175 struct xt_target {
176 	struct list_head list;
177 
178 	const char name[XT_EXTENSION_MAXNAMELEN];
179 	u_int8_t revision;
180 
181 	/* Returns verdict. Argument order changed since 2.6.9, as this
182 	   must now handle non-linear skbs, using skb_copy_bits and
183 	   skb_ip_make_writable. */
184 	unsigned int (*target)(struct sk_buff *skb,
185 			       const struct xt_action_param *);
186 
187 	/* Called when user tries to insert an entry of this type:
188            hook_mask is a bitmask of hooks from which it can be
189            called. */
190 	/* Should return 0 on success or an error code otherwise (-Exxxx). */
191 	int (*checkentry)(const struct xt_tgchk_param *);
192 
193 	/* Called to validate hooks based on the target configuration. */
194 	int (*check_hooks)(const struct xt_tgchk_param *);
195 
196 	/* Called when entry of this type deleted. */
197 	void (*destroy)(const struct xt_tgdtor_param *);
198 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
199 	/* Called when userspace align differs from kernel space one */
200 	void (*compat_from_user)(void *dst, const void *src);
201 	int (*compat_to_user)(void __user *dst, const void *src);
202 #endif
203 	/* Set this to THIS_MODULE if you are a module, otherwise NULL */
204 	struct module *me;
205 
206 	const char *table;
207 	unsigned int targetsize;
208 	unsigned int usersize;
209 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
210 	unsigned int compatsize;
211 #endif
212 	unsigned int hooks;
213 	unsigned short proto;
214 
215 	unsigned short family;
216 };
217 
218 /* Furniture shopping... */
219 struct xt_table {
220 	struct list_head list;
221 
222 	/* What hooks you will enter on */
223 	unsigned int valid_hooks;
224 
225 	/* Man behind the curtain... */
226 	struct xt_table_info *private;
227 
228 	/* hook ops that register the table with the netfilter core */
229 	struct nf_hook_ops *ops;
230 
231 	/* Set this to THIS_MODULE if you are a module, otherwise NULL */
232 	struct module *me;
233 
234 	u_int8_t af;		/* address/protocol family */
235 	int priority;		/* hook order */
236 
237 	/* A unique name... */
238 	const char name[XT_TABLE_MAXNAMELEN];
239 };
240 
241 #include <linux/netfilter_ipv4.h>
242 
243 /* The table itself */
244 struct xt_table_info {
245 	/* Size per table */
246 	unsigned int size;
247 	/* Number of entries: FIXME. --RR */
248 	unsigned int number;
249 	/* Initial number of entries. Needed for module usage count */
250 	unsigned int initial_entries;
251 
252 	/* Entry points and underflows */
253 	unsigned int hook_entry[NF_INET_NUMHOOKS];
254 	unsigned int underflow[NF_INET_NUMHOOKS];
255 
256 	/*
257 	 * Number of user chains. Since tables cannot have loops, at most
258 	 * @stacksize jumps (number of user chains) can possibly be made.
259 	 */
260 	unsigned int stacksize;
261 	void ***jumpstack;
262 
263 	unsigned char entries[] __aligned(8);
264 };
265 
266 int xt_register_target(struct xt_target *target);
267 void xt_unregister_target(struct xt_target *target);
268 int xt_register_targets(struct xt_target *target, unsigned int n);
269 void xt_unregister_targets(struct xt_target *target, unsigned int n);
270 
271 int xt_register_match(struct xt_match *target);
272 void xt_unregister_match(struct xt_match *target);
273 int xt_register_matches(struct xt_match *match, unsigned int n);
274 void xt_unregister_matches(struct xt_match *match, unsigned int n);
275 
276 int xt_check_entry_offsets(const void *base, const char *elems,
277 			   unsigned int target_offset,
278 			   unsigned int next_offset);
279 
280 int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks);
281 
282 unsigned int *xt_alloc_entry_offsets(unsigned int size);
283 bool xt_find_jump_offset(const unsigned int *offsets,
284 			 unsigned int target, unsigned int size);
285 
286 int xt_check_proc_name(const char *name, unsigned int size);
287 
288 int xt_check_hooks_match(struct xt_mtchk_param *par);
289 int xt_check_match(struct xt_mtchk_param *, unsigned int size, u16 proto,
290 		   bool inv_proto);
291 int xt_check_hooks_target(struct xt_tgchk_param *par);
292 int xt_check_target(struct xt_tgchk_param *, unsigned int size, u16 proto,
293 		    bool inv_proto);
294 
295 int xt_match_to_user(const struct xt_entry_match *m,
296 		     struct xt_entry_match __user *u);
297 int xt_target_to_user(const struct xt_entry_target *t,
298 		      struct xt_entry_target __user *u);
299 int xt_data_to_user(void __user *dst, const void *src,
300 		    int usersize, int size, int aligned_size);
301 
302 void *xt_copy_counters(sockptr_t arg, unsigned int len,
303 		       struct xt_counters_info *info);
304 struct xt_counters *xt_counters_alloc(unsigned int counters);
305 
306 struct xt_table *xt_register_table(struct net *net,
307 				   const struct xt_table *table,
308 				   struct xt_table_info *bootstrap,
309 				   struct xt_table_info *newinfo);
310 void *xt_unregister_table(struct xt_table *table);
311 
312 struct xt_table_info *xt_replace_table(struct xt_table *table,
313 				       unsigned int num_counters,
314 				       struct xt_table_info *newinfo,
315 				       int *error);
316 
317 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision);
318 struct xt_match *xt_request_find_match(u8 af, const char *name, u8 revision);
319 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision);
320 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
321 		     int *err);
322 
323 struct xt_table *xt_find_table(struct net *net, u8 af, const char *name);
324 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
325 				    const char *name);
326 struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
327 					    const char *name);
328 void xt_table_unlock(struct xt_table *t);
329 
330 int xt_proto_init(struct net *net, u_int8_t af);
331 void xt_proto_fini(struct net *net, u_int8_t af);
332 
333 struct xt_table_info *xt_alloc_table_info(unsigned int size);
334 void xt_free_table_info(struct xt_table_info *info);
335 
336 /**
337  * xt_recseq - recursive seqcount for netfilter use
338  *
339  * Packet processing changes the seqcount only if no recursion happened
340  * get_counters() can use read_seqcount_begin()/read_seqcount_retry(),
341  * because we use the normal seqcount convention :
342  * Low order bit set to 1 if a writer is active.
343  */
344 DECLARE_PER_CPU(seqcount_t, xt_recseq);
345 
346 /* xt_tee_enabled - true if x_tables needs to handle reentrancy
347  *
348  * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
349  */
350 extern struct static_key xt_tee_enabled;
351 
352 /**
353  * xt_write_recseq_begin - start of a write section
354  *
355  * Begin packet processing : all readers must wait the end
356  * 1) Must be called with preemption disabled
357  * 2) softirqs must be disabled too (or we should use this_cpu_add())
358  * Returns:
359  *  1 if no recursion on this cpu
360  *  0 if recursion detected
361  */
xt_write_recseq_begin(void)362 static inline unsigned int xt_write_recseq_begin(void)
363 {
364 	unsigned int addend;
365 
366 	/*
367 	 * Low order bit of sequence is set if we already
368 	 * called xt_write_recseq_begin().
369 	 */
370 	addend = (__this_cpu_read(xt_recseq.sequence) + 1) & 1;
371 
372 	/*
373 	 * This is kind of a write_seqcount_begin(), but addend is 0 or 1
374 	 * We dont check addend value to avoid a test and conditional jump,
375 	 * since addend is most likely 1
376 	 */
377 	__this_cpu_add(xt_recseq.sequence, addend);
378 	smp_mb();
379 
380 	return addend;
381 }
382 
383 /**
384  * xt_write_recseq_end - end of a write section
385  * @addend: return value from previous xt_write_recseq_begin()
386  *
387  * End packet processing : all readers can proceed
388  * 1) Must be called with preemption disabled
389  * 2) softirqs must be disabled too (or we should use this_cpu_add())
390  */
xt_write_recseq_end(unsigned int addend)391 static inline void xt_write_recseq_end(unsigned int addend)
392 {
393 	/* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
394 	smp_wmb();
395 	__this_cpu_add(xt_recseq.sequence, addend);
396 }
397 
398 /*
399  * This helper is performance critical and must be inlined
400  */
ifname_compare_aligned(const char * _a,const char * _b,const char * _mask)401 static inline unsigned long ifname_compare_aligned(const char *_a,
402 						   const char *_b,
403 						   const char *_mask)
404 {
405 	const unsigned long *a = (const unsigned long *)_a;
406 	const unsigned long *b = (const unsigned long *)_b;
407 	const unsigned long *mask = (const unsigned long *)_mask;
408 	unsigned long ret;
409 
410 	ret = (a[0] ^ b[0]) & mask[0];
411 	if (IFNAMSIZ > sizeof(unsigned long))
412 		ret |= (a[1] ^ b[1]) & mask[1];
413 	if (IFNAMSIZ > 2 * sizeof(unsigned long))
414 		ret |= (a[2] ^ b[2]) & mask[2];
415 	if (IFNAMSIZ > 3 * sizeof(unsigned long))
416 		ret |= (a[3] ^ b[3]) & mask[3];
417 	BUILD_BUG_ON(IFNAMSIZ > 4 * sizeof(unsigned long));
418 	return ret;
419 }
420 
421 struct xt_percpu_counter_alloc_state {
422 	unsigned int off;
423 	const char __percpu *mem;
424 };
425 
426 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
427 			     struct xt_counters *counter);
428 void xt_percpu_counter_free(struct xt_counters *cnt);
429 
430 static inline struct xt_counters *
xt_get_this_cpu_counter(struct xt_counters * cnt)431 xt_get_this_cpu_counter(struct xt_counters *cnt)
432 {
433 	if (nr_cpu_ids > 1)
434 		return this_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt);
435 
436 	return cnt;
437 }
438 
439 static inline struct xt_counters *
xt_get_per_cpu_counter(struct xt_counters * cnt,unsigned int cpu)440 xt_get_per_cpu_counter(struct xt_counters *cnt, unsigned int cpu)
441 {
442 	if (nr_cpu_ids > 1)
443 		return per_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt, cpu);
444 
445 	return cnt;
446 }
447 
448 struct nf_hook_ops *xt_hook_ops_alloc(const struct xt_table *, nf_hookfn *);
449 
450 int xt_register_template(const struct xt_table *t, int(*table_init)(struct net *net));
451 void xt_unregister_template(const struct xt_table *t);
452 
453 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
454 #include <net/compat.h>
455 
456 struct compat_xt_entry_match {
457 	union {
458 		struct {
459 			u_int16_t match_size;
460 			char name[XT_FUNCTION_MAXNAMELEN - 1];
461 			u_int8_t revision;
462 		} user;
463 		struct {
464 			u_int16_t match_size;
465 			compat_uptr_t match;
466 		} kernel;
467 		u_int16_t match_size;
468 	} u;
469 	unsigned char data[];
470 };
471 
472 struct compat_xt_entry_target {
473 	union {
474 		struct {
475 			u_int16_t target_size;
476 			char name[XT_FUNCTION_MAXNAMELEN - 1];
477 			u_int8_t revision;
478 		} user;
479 		struct {
480 			u_int16_t target_size;
481 			compat_uptr_t target;
482 		} kernel;
483 		u_int16_t target_size;
484 	} u;
485 	unsigned char data[];
486 };
487 
488 /* FIXME: this works only on 32 bit tasks
489  * need to change whole approach in order to calculate align as function of
490  * current task alignment */
491 
492 struct compat_xt_counters {
493 	compat_u64 pcnt, bcnt;			/* Packet and byte counters */
494 };
495 
496 struct compat_xt_counters_info {
497 	char name[XT_TABLE_MAXNAMELEN];
498 	compat_uint_t num_counters;
499 	struct compat_xt_counters counters[];
500 };
501 
502 struct _compat_xt_align {
503 	__u8 u8;
504 	__u16 u16;
505 	__u32 u32;
506 	compat_u64 u64;
507 };
508 
509 #define COMPAT_XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _compat_xt_align))
510 
511 void xt_compat_lock(u_int8_t af);
512 void xt_compat_unlock(u_int8_t af);
513 
514 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta);
515 void xt_compat_flush_offsets(u_int8_t af);
516 int xt_compat_init_offsets(u8 af, unsigned int number);
517 int xt_compat_calc_jump(u_int8_t af, unsigned int offset);
518 
519 int xt_compat_match_offset(const struct xt_match *match);
520 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
521 			      unsigned int *size);
522 int xt_compat_match_to_user(const struct xt_entry_match *m,
523 			    void __user **dstptr, unsigned int *size);
524 
525 int xt_compat_target_offset(const struct xt_target *target);
526 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
527 				unsigned int *size);
528 int xt_compat_target_to_user(const struct xt_entry_target *t,
529 			     void __user **dstptr, unsigned int *size);
530 int xt_compat_check_entry_offsets(const void *base, const char *elems,
531 				  unsigned int target_offset,
532 				  unsigned int next_offset);
533 
534 #endif /* CONFIG_NETFILTER_XTABLES_COMPAT */
535 #endif /* _X_TABLES_H */
536