1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_CORE_H
3 #define _NET_NF_TABLES_CORE_H
4 
5 #include <net/netfilter/nf_tables.h>
6 #include <linux/indirect_call_wrapper.h>
7 
8 extern struct nft_expr_type nft_imm_type;
9 extern struct nft_expr_type nft_cmp_type;
10 extern struct nft_expr_type nft_counter_type;
11 extern struct nft_expr_type nft_lookup_type;
12 extern struct nft_expr_type nft_bitwise_type;
13 extern struct nft_expr_type nft_byteorder_type;
14 extern struct nft_expr_type nft_payload_type;
15 extern struct nft_expr_type nft_dynset_type;
16 extern struct nft_expr_type nft_range_type;
17 extern struct nft_expr_type nft_meta_type;
18 extern struct nft_expr_type nft_rt_type;
19 extern struct nft_expr_type nft_exthdr_type;
20 extern struct nft_expr_type nft_last_type;
21 extern struct nft_expr_type nft_objref_type;
22 extern struct nft_expr_type nft_inner_type;
23 
24 #ifdef CONFIG_NETWORK_SECMARK
25 extern struct nft_object_type nft_secmark_obj_type;
26 #endif
27 extern struct nft_object_type nft_counter_obj_type;
28 
29 int nf_tables_core_module_init(void);
30 void nf_tables_core_module_exit(void);
31 
32 struct nft_bitwise_fast_expr {
33 	u32			mask;
34 	u32			xor;
35 	u8			sreg;
36 	u8			dreg;
37 };
38 
39 struct nft_cmp_fast_expr {
40 	u32			data;
41 	u32			mask;
42 	u8			sreg;
43 	u8			len;
44 	bool			inv;
45 };
46 
47 struct nft_cmp16_fast_expr {
48 	struct nft_data		data;
49 	struct nft_data		mask;
50 	u8			sreg;
51 	u8			len;
52 	bool			inv;
53 };
54 
55 struct nft_immediate_expr {
56 	struct nft_data		data;
57 	u8			dreg;
58 	u8			dlen;
59 };
60 
61 extern const struct nft_expr_ops nft_cmp_fast_ops;
62 extern const struct nft_expr_ops nft_cmp16_fast_ops;
63 
64 struct nft_payload {
65 	enum nft_payload_bases	base:8;
66 	u8			offset;
67 	u8			len;
68 	u8			dreg;
69 };
70 
71 extern const struct nft_expr_ops nft_payload_fast_ops;
72 
73 extern const struct nft_expr_ops nft_bitwise_fast_ops;
74 
75 extern struct static_key_false nft_counters_enabled;
76 extern struct static_key_false nft_trace_enabled;
77 
78 extern const struct nft_set_type nft_set_rhash_type;
79 extern const struct nft_set_type nft_set_hash_type;
80 extern const struct nft_set_type nft_set_hash_fast_type;
81 extern const struct nft_set_type nft_set_rbtree_type;
82 extern const struct nft_set_type nft_set_bitmap_type;
83 extern const struct nft_set_type nft_set_pipapo_type;
84 extern const struct nft_set_type nft_set_pipapo_avx2_type;
85 
86 #ifdef CONFIG_RETPOLINE
87 bool nft_rhash_lookup(const struct net *net, const struct nft_set *set,
88 		      const u32 *key, const struct nft_set_ext **ext);
89 bool nft_rbtree_lookup(const struct net *net, const struct nft_set *set,
90 		       const u32 *key, const struct nft_set_ext **ext);
91 bool nft_bitmap_lookup(const struct net *net, const struct nft_set *set,
92 		       const u32 *key, const struct nft_set_ext **ext);
93 bool nft_hash_lookup_fast(const struct net *net,
94 			  const struct nft_set *set,
95 			  const u32 *key, const struct nft_set_ext **ext);
96 bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
97 		     const u32 *key, const struct nft_set_ext **ext);
98 bool nft_set_do_lookup(const struct net *net, const struct nft_set *set,
99 		       const u32 *key, const struct nft_set_ext **ext);
100 #else
101 static inline bool
102 nft_set_do_lookup(const struct net *net, const struct nft_set *set,
103 		  const u32 *key, const struct nft_set_ext **ext)
104 {
105 	return set->ops->lookup(net, set, key, ext);
106 }
107 #endif
108 
109 /* called from nft_pipapo_avx2.c */
110 bool nft_pipapo_lookup(const struct net *net, const struct nft_set *set,
111 		       const u32 *key, const struct nft_set_ext **ext);
112 /* called from nft_set_pipapo.c */
113 bool nft_pipapo_avx2_lookup(const struct net *net, const struct nft_set *set,
114 			    const u32 *key, const struct nft_set_ext **ext);
115 
116 void nft_counter_init_seqcount(void);
117 
118 struct nft_expr;
119 struct nft_regs;
120 struct nft_pktinfo;
121 void nft_meta_get_eval(const struct nft_expr *expr,
122 		       struct nft_regs *regs, const struct nft_pktinfo *pkt);
123 void nft_cmp_eval(const struct nft_expr *expr,
124 		  struct nft_regs *regs, const struct nft_pktinfo *pkt);
125 void nft_lookup_eval(const struct nft_expr *expr,
126 		     struct nft_regs *regs, const struct nft_pktinfo *pkt);
127 void nft_payload_eval(const struct nft_expr *expr,
128 		      struct nft_regs *regs, const struct nft_pktinfo *pkt);
129 void nft_immediate_eval(const struct nft_expr *expr,
130 			struct nft_regs *regs, const struct nft_pktinfo *pkt);
131 void nft_bitwise_eval(const struct nft_expr *expr,
132 		      struct nft_regs *regs, const struct nft_pktinfo *pkt);
133 void nft_range_eval(const struct nft_expr *expr,
134 		    struct nft_regs *regs, const struct nft_pktinfo *pkt);
135 void nft_byteorder_eval(const struct nft_expr *expr,
136 			struct nft_regs *regs, const struct nft_pktinfo *pkt);
137 void nft_dynset_eval(const struct nft_expr *expr,
138 		     struct nft_regs *regs, const struct nft_pktinfo *pkt);
139 void nft_rt_get_eval(const struct nft_expr *expr,
140 		     struct nft_regs *regs, const struct nft_pktinfo *pkt);
141 void nft_counter_eval(const struct nft_expr *expr, struct nft_regs *regs,
142                       const struct nft_pktinfo *pkt);
143 
144 enum {
145 	NFT_PAYLOAD_CTX_INNER_TUN	= (1 << 0),
146 	NFT_PAYLOAD_CTX_INNER_LL	= (1 << 1),
147 	NFT_PAYLOAD_CTX_INNER_NH	= (1 << 2),
148 	NFT_PAYLOAD_CTX_INNER_TH	= (1 << 3),
149 };
150 
151 struct nft_inner_tun_ctx {
152 	u16	type;
153 	u16	inner_tunoff;
154 	u16	inner_lloff;
155 	u16	inner_nhoff;
156 	u16	inner_thoff;
157 	__be16	llproto;
158 	u8	l4proto;
159 	u8      flags;
160 };
161 
162 int nft_payload_inner_offset(const struct nft_pktinfo *pkt);
163 void nft_payload_inner_eval(const struct nft_expr *expr, struct nft_regs *regs,
164 			    const struct nft_pktinfo *pkt,
165 			    struct nft_inner_tun_ctx *ctx);
166 
167 #endif /* _NET_NF_TABLES_CORE_H */
168