• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_FLOW_DISSECTOR_H
3 #define _NET_FLOW_DISSECTOR_H
4 
5 #include <linux/types.h>
6 #include <linux/in6.h>
7 #include <linux/siphash.h>
8 #include <linux/string.h>
9 #include <uapi/linux/if_ether.h>
10 
11 /**
12  * struct flow_dissector_key_control:
13  * @thoff: Transport header offset
14  */
15 struct flow_dissector_key_control {
16 	u16	thoff;
17 	u16	addr_type;
18 	u32	flags;
19 };
20 
21 #define FLOW_DIS_IS_FRAGMENT	BIT(0)
22 #define FLOW_DIS_FIRST_FRAG	BIT(1)
23 #define FLOW_DIS_ENCAPSULATION	BIT(2)
24 
25 enum flow_dissect_ret {
26 	FLOW_DISSECT_RET_OUT_GOOD,
27 	FLOW_DISSECT_RET_OUT_BAD,
28 	FLOW_DISSECT_RET_PROTO_AGAIN,
29 	FLOW_DISSECT_RET_IPPROTO_AGAIN,
30 	FLOW_DISSECT_RET_CONTINUE,
31 };
32 
33 /**
34  * struct flow_dissector_key_basic:
35  * @thoff: Transport header offset
36  * @n_proto: Network header protocol (eg. IPv4/IPv6)
37  * @ip_proto: Transport header protocol (eg. TCP/UDP)
38  */
39 struct flow_dissector_key_basic {
40 	__be16	n_proto;
41 	u8	ip_proto;
42 	u8	padding;
43 };
44 
45 struct flow_dissector_key_tags {
46 	u32	flow_label;
47 };
48 
49 struct flow_dissector_key_vlan {
50 	u16	vlan_id:12,
51 		vlan_dei:1,
52 		vlan_priority:3;
53 	__be16	vlan_tpid;
54 	__be16	vlan_eth_type;
55 	u16	padding;
56 };
57 
58 struct flow_dissector_key_mpls {
59 	u32	mpls_ttl:8,
60 		mpls_bos:1,
61 		mpls_tc:3,
62 		mpls_label:20;
63 };
64 
65 #define FLOW_DIS_TUN_OPTS_MAX 255
66 /**
67  * struct flow_dissector_key_enc_opts:
68  * @data: tunnel option data
69  * @len: length of tunnel option data
70  * @dst_opt_type: tunnel option type
71  */
72 struct flow_dissector_key_enc_opts {
73 	u8 data[FLOW_DIS_TUN_OPTS_MAX];	/* Using IP_TUNNEL_OPTS_MAX is desired
74 					 * here but seems difficult to #include
75 					 */
76 	u8 len;
77 	__be16 dst_opt_type;
78 };
79 
80 struct flow_dissector_key_keyid {
81 	__be32	keyid;
82 };
83 
84 /**
85  * struct flow_dissector_key_ipv4_addrs:
86  * @src: source ip address
87  * @dst: destination ip address
88  */
89 struct flow_dissector_key_ipv4_addrs {
90 	/* (src,dst) must be grouped, in the same way than in IP header */
91 	__be32 src;
92 	__be32 dst;
93 };
94 
95 /**
96  * struct flow_dissector_key_ipv6_addrs:
97  * @src: source ip address
98  * @dst: destination ip address
99  */
100 struct flow_dissector_key_ipv6_addrs {
101 	/* (src,dst) must be grouped, in the same way than in IP header */
102 	struct in6_addr src;
103 	struct in6_addr dst;
104 };
105 
106 /**
107  * struct flow_dissector_key_tipc:
108  * @key: source node address combined with selector
109  */
110 struct flow_dissector_key_tipc {
111 	__be32 key;
112 };
113 
114 /**
115  * struct flow_dissector_key_addrs:
116  * @v4addrs: IPv4 addresses
117  * @v6addrs: IPv6 addresses
118  */
119 struct flow_dissector_key_addrs {
120 	union {
121 		struct flow_dissector_key_ipv4_addrs v4addrs;
122 		struct flow_dissector_key_ipv6_addrs v6addrs;
123 		struct flow_dissector_key_tipc tipckey;
124 	};
125 };
126 
127 /**
128  * flow_dissector_key_arp:
129  *	@ports: Operation, source and target addresses for an ARP header
130  *              for Ethernet hardware addresses and IPv4 protocol addresses
131  *		sip: Sender IP address
132  *		tip: Target IP address
133  *		op:  Operation
134  *		sha: Sender hardware address
135  *		tpa: Target hardware address
136  */
137 struct flow_dissector_key_arp {
138 	__u32 sip;
139 	__u32 tip;
140 	__u8 op;
141 	unsigned char sha[ETH_ALEN];
142 	unsigned char tha[ETH_ALEN];
143 };
144 
145 /**
146  * flow_dissector_key_tp_ports:
147  *	@ports: port numbers of Transport header
148  *		src: source port number
149  *		dst: destination port number
150  */
151 struct flow_dissector_key_ports {
152 	union {
153 		__be32 ports;
154 		struct {
155 			__be16 src;
156 			__be16 dst;
157 		};
158 	};
159 };
160 
161 /**
162  * flow_dissector_key_icmp:
163  *	@ports: type and code of ICMP header
164  *		icmp: ICMP type (high) and code (low)
165  *		type: ICMP type
166  *		code: ICMP code
167  */
168 struct flow_dissector_key_icmp {
169 	union {
170 		__be16 icmp;
171 		struct {
172 			u8 type;
173 			u8 code;
174 		};
175 	};
176 };
177 
178 /**
179  * struct flow_dissector_key_eth_addrs:
180  * @src: source Ethernet address
181  * @dst: destination Ethernet address
182  */
183 struct flow_dissector_key_eth_addrs {
184 	/* (dst,src) must be grouped, in the same way than in ETH header */
185 	unsigned char dst[ETH_ALEN];
186 	unsigned char src[ETH_ALEN];
187 };
188 
189 /**
190  * struct flow_dissector_key_tcp:
191  * @flags: flags
192  */
193 struct flow_dissector_key_tcp {
194 	__be16 flags;
195 };
196 
197 /**
198  * struct flow_dissector_key_ip:
199  * @tos: tos
200  * @ttl: ttl
201  */
202 struct flow_dissector_key_ip {
203 	__u8	tos;
204 	__u8	ttl;
205 };
206 
207 /**
208  * struct flow_dissector_key_meta:
209  * @ingress_ifindex: ingress ifindex
210  */
211 struct flow_dissector_key_meta {
212 	int ingress_ifindex;
213 };
214 
215 /**
216  * struct flow_dissector_key_ct:
217  * @ct_state: conntrack state after converting with map
218  * @ct_mark: conttrack mark
219  * @ct_zone: conntrack zone
220  * @ct_labels: conntrack labels
221  */
222 struct flow_dissector_key_ct {
223 	u16	ct_state;
224 	u16	ct_zone;
225 	u32	ct_mark;
226 	u32	ct_labels[4];
227 };
228 
229 enum flow_dissector_key_id {
230 	FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
231 	FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
232 	FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
233 	FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
234 	FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
235 	FLOW_DISSECTOR_KEY_PORTS_RANGE, /* struct flow_dissector_key_ports */
236 	FLOW_DISSECTOR_KEY_ICMP, /* struct flow_dissector_key_icmp */
237 	FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
238 	FLOW_DISSECTOR_KEY_TIPC, /* struct flow_dissector_key_tipc */
239 	FLOW_DISSECTOR_KEY_ARP, /* struct flow_dissector_key_arp */
240 	FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_vlan */
241 	FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_tags */
242 	FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
243 	FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
244 	FLOW_DISSECTOR_KEY_ENC_KEYID, /* struct flow_dissector_key_keyid */
245 	FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
246 	FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
247 	FLOW_DISSECTOR_KEY_ENC_CONTROL, /* struct flow_dissector_key_control */
248 	FLOW_DISSECTOR_KEY_ENC_PORTS, /* struct flow_dissector_key_ports */
249 	FLOW_DISSECTOR_KEY_MPLS, /* struct flow_dissector_key_mpls */
250 	FLOW_DISSECTOR_KEY_TCP, /* struct flow_dissector_key_tcp */
251 	FLOW_DISSECTOR_KEY_IP, /* struct flow_dissector_key_ip */
252 	FLOW_DISSECTOR_KEY_CVLAN, /* struct flow_dissector_key_vlan */
253 	FLOW_DISSECTOR_KEY_ENC_IP, /* struct flow_dissector_key_ip */
254 	FLOW_DISSECTOR_KEY_ENC_OPTS, /* struct flow_dissector_key_enc_opts */
255 	FLOW_DISSECTOR_KEY_META, /* struct flow_dissector_key_meta */
256 	FLOW_DISSECTOR_KEY_CT, /* struct flow_dissector_key_ct */
257 
258 	FLOW_DISSECTOR_KEY_MAX,
259 };
260 
261 #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG		BIT(0)
262 #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL	BIT(1)
263 #define FLOW_DISSECTOR_F_STOP_AT_ENCAP		BIT(2)
264 
265 struct flow_dissector_key {
266 	enum flow_dissector_key_id key_id;
267 	size_t offset; /* offset of struct flow_dissector_key_*
268 			  in target the struct */
269 };
270 
271 struct flow_dissector {
272 	unsigned int used_keys; /* each bit repesents presence of one key id */
273 	unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
274 };
275 
276 struct flow_keys_basic {
277 	struct flow_dissector_key_control control;
278 	struct flow_dissector_key_basic basic;
279 };
280 
281 struct flow_keys {
282 	struct flow_dissector_key_control control;
283 #define FLOW_KEYS_HASH_START_FIELD basic
284 	struct flow_dissector_key_basic basic __aligned(SIPHASH_ALIGNMENT);
285 	struct flow_dissector_key_tags tags;
286 	struct flow_dissector_key_vlan vlan;
287 	struct flow_dissector_key_vlan cvlan;
288 	struct flow_dissector_key_keyid keyid;
289 	struct flow_dissector_key_ports ports;
290 	struct flow_dissector_key_addrs addrs;
291 };
292 
293 #define FLOW_KEYS_HASH_OFFSET		\
294 	offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
295 
296 __be32 flow_get_u32_src(const struct flow_keys *flow);
297 __be32 flow_get_u32_dst(const struct flow_keys *flow);
298 
299 extern struct flow_dissector flow_keys_dissector;
300 extern struct flow_dissector flow_keys_basic_dissector;
301 
302 /* struct flow_keys_digest:
303  *
304  * This structure is used to hold a digest of the full flow keys. This is a
305  * larger "hash" of a flow to allow definitively matching specific flows where
306  * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
307  * that it can be used in CB of skb (see sch_choke for an example).
308  */
309 #define FLOW_KEYS_DIGEST_LEN	16
310 struct flow_keys_digest {
311 	u8	data[FLOW_KEYS_DIGEST_LEN];
312 };
313 
314 void make_flow_keys_digest(struct flow_keys_digest *digest,
315 			   const struct flow_keys *flow);
316 
flow_keys_have_l4(const struct flow_keys * keys)317 static inline bool flow_keys_have_l4(const struct flow_keys *keys)
318 {
319 	return (keys->ports.ports || keys->tags.flow_label);
320 }
321 
322 u32 flow_hash_from_keys(struct flow_keys *keys);
323 
dissector_uses_key(const struct flow_dissector * flow_dissector,enum flow_dissector_key_id key_id)324 static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector,
325 				      enum flow_dissector_key_id key_id)
326 {
327 	return flow_dissector->used_keys & (1 << key_id);
328 }
329 
skb_flow_dissector_target(struct flow_dissector * flow_dissector,enum flow_dissector_key_id key_id,void * target_container)330 static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
331 					      enum flow_dissector_key_id key_id,
332 					      void *target_container)
333 {
334 	return ((char *)target_container) + flow_dissector->offset[key_id];
335 }
336 
337 struct bpf_flow_dissector {
338 	struct bpf_flow_keys	*flow_keys;
339 	const struct sk_buff	*skb;
340 	void			*data;
341 	void			*data_end;
342 };
343 
344 static inline void
flow_dissector_init_keys(struct flow_dissector_key_control * key_control,struct flow_dissector_key_basic * key_basic)345 flow_dissector_init_keys(struct flow_dissector_key_control *key_control,
346 			 struct flow_dissector_key_basic *key_basic)
347 {
348 	memset(key_control, 0, sizeof(*key_control));
349 	memset(key_basic, 0, sizeof(*key_basic));
350 }
351 
352 #endif
353