xref: /linux/include/net/flow_dissector.h (revision 5c35a02c545a7bbe77f3a1ae337d9e29beed079b)
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 <uapi/linux/if_ether.h>
8 
9 /**
10  * struct flow_dissector_key_control:
11  * @thoff: Transport header offset
12  */
13 struct flow_dissector_key_control {
14 	u16	thoff;
15 	u16	addr_type;
16 	u32	flags;
17 };
18 
19 #define FLOW_DIS_IS_FRAGMENT	BIT(0)
20 #define FLOW_DIS_FIRST_FRAG	BIT(1)
21 #define FLOW_DIS_ENCAPSULATION	BIT(2)
22 
23 enum flow_dissect_ret {
24 	FLOW_DISSECT_RET_OUT_GOOD,
25 	FLOW_DISSECT_RET_OUT_BAD,
26 	FLOW_DISSECT_RET_PROTO_AGAIN,
27 	FLOW_DISSECT_RET_IPPROTO_AGAIN,
28 	FLOW_DISSECT_RET_CONTINUE,
29 };
30 
31 /**
32  * struct flow_dissector_key_basic:
33  * @thoff: Transport header offset
34  * @n_proto: Network header protocol (eg. IPv4/IPv6)
35  * @ip_proto: Transport header protocol (eg. TCP/UDP)
36  */
37 struct flow_dissector_key_basic {
38 	__be16	n_proto;
39 	u8	ip_proto;
40 	u8	padding;
41 };
42 
43 struct flow_dissector_key_tags {
44 	u32	flow_label;
45 };
46 
47 struct flow_dissector_key_vlan {
48 	u16	vlan_id:12,
49 		vlan_priority:3;
50 	u16	padding;
51 };
52 
53 struct flow_dissector_key_mpls {
54 	u32	mpls_ttl:8,
55 		mpls_bos:1,
56 		mpls_tc:3,
57 		mpls_label:20;
58 };
59 
60 struct flow_dissector_key_keyid {
61 	__be32	keyid;
62 };
63 
64 /**
65  * struct flow_dissector_key_ipv4_addrs:
66  * @src: source ip address
67  * @dst: destination ip address
68  */
69 struct flow_dissector_key_ipv4_addrs {
70 	/* (src,dst) must be grouped, in the same way than in IP header */
71 	__be32 src;
72 	__be32 dst;
73 };
74 
75 /**
76  * struct flow_dissector_key_ipv6_addrs:
77  * @src: source ip address
78  * @dst: destination ip address
79  */
80 struct flow_dissector_key_ipv6_addrs {
81 	/* (src,dst) must be grouped, in the same way than in IP header */
82 	struct in6_addr src;
83 	struct in6_addr dst;
84 };
85 
86 /**
87  * struct flow_dissector_key_tipc:
88  * @key: source node address combined with selector
89  */
90 struct flow_dissector_key_tipc {
91 	__be32 key;
92 };
93 
94 /**
95  * struct flow_dissector_key_addrs:
96  * @v4addrs: IPv4 addresses
97  * @v6addrs: IPv6 addresses
98  */
99 struct flow_dissector_key_addrs {
100 	union {
101 		struct flow_dissector_key_ipv4_addrs v4addrs;
102 		struct flow_dissector_key_ipv6_addrs v6addrs;
103 		struct flow_dissector_key_tipc tipckey;
104 	};
105 };
106 
107 /**
108  * flow_dissector_key_arp:
109  *	@ports: Operation, source and target addresses for an ARP header
110  *              for Ethernet hardware addresses and IPv4 protocol addresses
111  *		sip: Sender IP address
112  *		tip: Target IP address
113  *		op:  Operation
114  *		sha: Sender hardware address
115  *		tpa: Target hardware address
116  */
117 struct flow_dissector_key_arp {
118 	__u32 sip;
119 	__u32 tip;
120 	__u8 op;
121 	unsigned char sha[ETH_ALEN];
122 	unsigned char tha[ETH_ALEN];
123 };
124 
125 /**
126  * flow_dissector_key_tp_ports:
127  *	@ports: port numbers of Transport header
128  *		src: source port number
129  *		dst: destination port number
130  */
131 struct flow_dissector_key_ports {
132 	union {
133 		__be32 ports;
134 		struct {
135 			__be16 src;
136 			__be16 dst;
137 		};
138 	};
139 };
140 
141 /**
142  * flow_dissector_key_icmp:
143  *	@ports: type and code of ICMP header
144  *		icmp: ICMP type (high) and code (low)
145  *		type: ICMP type
146  *		code: ICMP code
147  */
148 struct flow_dissector_key_icmp {
149 	union {
150 		__be16 icmp;
151 		struct {
152 			u8 type;
153 			u8 code;
154 		};
155 	};
156 };
157 
158 /**
159  * struct flow_dissector_key_eth_addrs:
160  * @src: source Ethernet address
161  * @dst: destination Ethernet address
162  */
163 struct flow_dissector_key_eth_addrs {
164 	/* (dst,src) must be grouped, in the same way than in ETH header */
165 	unsigned char dst[ETH_ALEN];
166 	unsigned char src[ETH_ALEN];
167 };
168 
169 /**
170  * struct flow_dissector_key_tcp:
171  * @flags: flags
172  */
173 struct flow_dissector_key_tcp {
174 	__be16 flags;
175 };
176 
177 /**
178  * struct flow_dissector_key_ip:
179  * @tos: tos
180  * @ttl: ttl
181  */
182 struct flow_dissector_key_ip {
183 	__u8	tos;
184 	__u8	ttl;
185 };
186 
187 enum flow_dissector_key_id {
188 	FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
189 	FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
190 	FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
191 	FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
192 	FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
193 	FLOW_DISSECTOR_KEY_ICMP, /* struct flow_dissector_key_icmp */
194 	FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
195 	FLOW_DISSECTOR_KEY_TIPC, /* struct flow_dissector_key_tipc */
196 	FLOW_DISSECTOR_KEY_ARP, /* struct flow_dissector_key_arp */
197 	FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_flow_vlan */
198 	FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
199 	FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
200 	FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
201 	FLOW_DISSECTOR_KEY_ENC_KEYID, /* struct flow_dissector_key_keyid */
202 	FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
203 	FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
204 	FLOW_DISSECTOR_KEY_ENC_CONTROL, /* struct flow_dissector_key_control */
205 	FLOW_DISSECTOR_KEY_ENC_PORTS, /* struct flow_dissector_key_ports */
206 	FLOW_DISSECTOR_KEY_MPLS, /* struct flow_dissector_key_mpls */
207 	FLOW_DISSECTOR_KEY_TCP, /* struct flow_dissector_key_tcp */
208 	FLOW_DISSECTOR_KEY_IP, /* struct flow_dissector_key_ip */
209 
210 	FLOW_DISSECTOR_KEY_MAX,
211 };
212 
213 #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG		BIT(0)
214 #define FLOW_DISSECTOR_F_STOP_AT_L3		BIT(1)
215 #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL	BIT(2)
216 #define FLOW_DISSECTOR_F_STOP_AT_ENCAP		BIT(3)
217 
218 struct flow_dissector_key {
219 	enum flow_dissector_key_id key_id;
220 	size_t offset; /* offset of struct flow_dissector_key_*
221 			  in target the struct */
222 };
223 
224 struct flow_dissector {
225 	unsigned int used_keys; /* each bit repesents presence of one key id */
226 	unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
227 };
228 
229 struct flow_keys_basic {
230 	struct flow_dissector_key_control control;
231 	struct flow_dissector_key_basic basic;
232 };
233 
234 struct flow_keys {
235 	struct flow_dissector_key_control control;
236 #define FLOW_KEYS_HASH_START_FIELD basic
237 	struct flow_dissector_key_basic basic;
238 	struct flow_dissector_key_tags tags;
239 	struct flow_dissector_key_vlan vlan;
240 	struct flow_dissector_key_keyid keyid;
241 	struct flow_dissector_key_ports ports;
242 	struct flow_dissector_key_addrs addrs;
243 };
244 
245 #define FLOW_KEYS_HASH_OFFSET		\
246 	offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
247 
248 __be32 flow_get_u32_src(const struct flow_keys *flow);
249 __be32 flow_get_u32_dst(const struct flow_keys *flow);
250 
251 extern struct flow_dissector flow_keys_dissector;
252 extern struct flow_dissector flow_keys_basic_dissector;
253 
254 /* struct flow_keys_digest:
255  *
256  * This structure is used to hold a digest of the full flow keys. This is a
257  * larger "hash" of a flow to allow definitively matching specific flows where
258  * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
259  * that it can be used in CB of skb (see sch_choke for an example).
260  */
261 #define FLOW_KEYS_DIGEST_LEN	16
262 struct flow_keys_digest {
263 	u8	data[FLOW_KEYS_DIGEST_LEN];
264 };
265 
266 void make_flow_keys_digest(struct flow_keys_digest *digest,
267 			   const struct flow_keys *flow);
268 
269 static inline bool flow_keys_have_l4(const struct flow_keys *keys)
270 {
271 	return (keys->ports.ports || keys->tags.flow_label);
272 }
273 
274 u32 flow_hash_from_keys(struct flow_keys *keys);
275 
276 static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector,
277 				      enum flow_dissector_key_id key_id)
278 {
279 	return flow_dissector->used_keys & (1 << key_id);
280 }
281 
282 static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
283 					      enum flow_dissector_key_id key_id,
284 					      void *target_container)
285 {
286 	return ((char *)target_container) + flow_dissector->offset[key_id];
287 }
288 
289 #endif
290