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