1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_FLOW_DISSECTOR_H
3 #define _NET_FLOW_DISSECTOR_H
5 #include <linux/types.h>
7 #include <linux/siphash.h>
8 #include <uapi/linux/if_ether.h>
13 * struct flow_dissector_key_control:
14 * @thoff: Transport header offset
16 struct flow_dissector_key_control {
22 #define FLOW_DIS_IS_FRAGMENT BIT(0)
23 #define FLOW_DIS_FIRST_FRAG BIT(1)
24 #define FLOW_DIS_ENCAPSULATION BIT(2)
26 enum flow_dissect_ret {
27 FLOW_DISSECT_RET_OUT_GOOD,
28 FLOW_DISSECT_RET_OUT_BAD,
29 FLOW_DISSECT_RET_PROTO_AGAIN,
30 FLOW_DISSECT_RET_IPPROTO_AGAIN,
31 FLOW_DISSECT_RET_CONTINUE,
35 * struct flow_dissector_key_basic:
36 * @thoff: Transport header offset
37 * @n_proto: Network header protocol (eg. IPv4/IPv6)
38 * @ip_proto: Transport header protocol (eg. TCP/UDP)
40 struct flow_dissector_key_basic {
46 struct flow_dissector_key_tags {
50 struct flow_dissector_key_vlan {
62 struct flow_dissector_key_mpls {
69 #define FLOW_DIS_TUN_OPTS_MAX 255
71 * struct flow_dissector_key_enc_opts:
72 * @data: tunnel option data
73 * @len: length of tunnel option data
74 * @dst_opt_type: tunnel option type
76 struct flow_dissector_key_enc_opts {
77 u8 data[FLOW_DIS_TUN_OPTS_MAX]; /* Using IP_TUNNEL_OPTS_MAX is desired
78 * here but seems difficult to #include
84 struct flow_dissector_key_keyid {
89 * struct flow_dissector_key_ipv4_addrs:
90 * @src: source ip address
91 * @dst: destination ip address
93 struct flow_dissector_key_ipv4_addrs {
94 /* (src,dst) must be grouped, in the same way than in IP header */
100 * struct flow_dissector_key_ipv6_addrs:
101 * @src: source ip address
102 * @dst: destination ip address
104 struct flow_dissector_key_ipv6_addrs {
105 /* (src,dst) must be grouped, in the same way than in IP header */
111 * struct flow_dissector_key_tipc:
112 * @key: source node address combined with selector
114 struct flow_dissector_key_tipc {
119 * struct flow_dissector_key_addrs:
120 * @v4addrs: IPv4 addresses
121 * @v6addrs: IPv6 addresses
123 struct flow_dissector_key_addrs {
125 struct flow_dissector_key_ipv4_addrs v4addrs;
126 struct flow_dissector_key_ipv6_addrs v6addrs;
127 struct flow_dissector_key_tipc tipckey;
132 * flow_dissector_key_arp:
133 * @ports: Operation, source and target addresses for an ARP header
134 * for Ethernet hardware addresses and IPv4 protocol addresses
135 * sip: Sender IP address
136 * tip: Target IP address
138 * sha: Sender hardware address
139 * tpa: Target hardware address
141 struct flow_dissector_key_arp {
145 unsigned char sha[ETH_ALEN];
146 unsigned char tha[ETH_ALEN];
150 * flow_dissector_key_tp_ports:
151 * @ports: port numbers of Transport header
152 * src: source port number
153 * dst: destination port number
155 struct flow_dissector_key_ports {
166 * flow_dissector_key_icmp:
169 * id: session identifier
171 struct flow_dissector_key_icmp {
180 * struct flow_dissector_key_eth_addrs:
181 * @src: source Ethernet address
182 * @dst: destination Ethernet address
184 struct flow_dissector_key_eth_addrs {
185 /* (dst,src) must be grouped, in the same way than in ETH header */
186 unsigned char dst[ETH_ALEN];
187 unsigned char src[ETH_ALEN];
191 * struct flow_dissector_key_tcp:
194 struct flow_dissector_key_tcp {
199 * struct flow_dissector_key_ip:
203 struct flow_dissector_key_ip {
209 * struct flow_dissector_key_meta:
210 * @ingress_ifindex: ingress ifindex
211 * @ingress_iftype: ingress interface type
213 struct flow_dissector_key_meta {
219 * struct flow_dissector_key_ct:
220 * @ct_state: conntrack state after converting with map
221 * @ct_mark: conttrack mark
222 * @ct_zone: conntrack zone
223 * @ct_labels: conntrack labels
225 struct flow_dissector_key_ct {
232 enum flow_dissector_key_id {
233 FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
234 FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
235 FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
236 FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
237 FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
238 FLOW_DISSECTOR_KEY_ICMP, /* struct flow_dissector_key_icmp */
239 FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
240 FLOW_DISSECTOR_KEY_TIPC, /* struct flow_dissector_key_tipc */
241 FLOW_DISSECTOR_KEY_ARP, /* struct flow_dissector_key_arp */
242 FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_vlan */
243 FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_tags */
244 FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
245 FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
246 FLOW_DISSECTOR_KEY_ENC_KEYID, /* struct flow_dissector_key_keyid */
247 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
248 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
249 FLOW_DISSECTOR_KEY_ENC_CONTROL, /* struct flow_dissector_key_control */
250 FLOW_DISSECTOR_KEY_ENC_PORTS, /* struct flow_dissector_key_ports */
251 FLOW_DISSECTOR_KEY_MPLS, /* struct flow_dissector_key_mpls */
252 FLOW_DISSECTOR_KEY_TCP, /* struct flow_dissector_key_tcp */
253 FLOW_DISSECTOR_KEY_IP, /* struct flow_dissector_key_ip */
254 FLOW_DISSECTOR_KEY_CVLAN, /* struct flow_dissector_key_vlan */
255 FLOW_DISSECTOR_KEY_ENC_IP, /* struct flow_dissector_key_ip */
256 FLOW_DISSECTOR_KEY_ENC_OPTS, /* struct flow_dissector_key_enc_opts */
257 FLOW_DISSECTOR_KEY_META, /* struct flow_dissector_key_meta */
258 FLOW_DISSECTOR_KEY_CT, /* struct flow_dissector_key_ct */
260 FLOW_DISSECTOR_KEY_MAX,
263 #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG BIT(0)
264 #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL BIT(1)
265 #define FLOW_DISSECTOR_F_STOP_AT_ENCAP BIT(2)
267 struct flow_dissector_key {
268 enum flow_dissector_key_id key_id;
269 size_t offset; /* offset of struct flow_dissector_key_*
270 in target the struct */
273 struct flow_dissector {
274 unsigned int used_keys; /* each bit repesents presence of one key id */
275 unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
278 struct flow_keys_basic {
279 struct flow_dissector_key_control control;
280 struct flow_dissector_key_basic basic;
284 struct flow_dissector_key_control control;
285 #define FLOW_KEYS_HASH_START_FIELD basic
286 struct flow_dissector_key_basic basic __aligned(SIPHASH_ALIGNMENT);
287 struct flow_dissector_key_tags tags;
288 struct flow_dissector_key_vlan vlan;
289 struct flow_dissector_key_vlan cvlan;
290 struct flow_dissector_key_keyid keyid;
291 struct flow_dissector_key_ports ports;
292 struct flow_dissector_key_icmp icmp;
293 /* 'addrs' must be the last member */
294 struct flow_dissector_key_addrs addrs;
297 #define FLOW_KEYS_HASH_OFFSET \
298 offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
300 __be32 flow_get_u32_src(const struct flow_keys *flow);
301 __be32 flow_get_u32_dst(const struct flow_keys *flow);
303 extern struct flow_dissector flow_keys_dissector;
304 extern struct flow_dissector flow_keys_basic_dissector;
306 /* struct flow_keys_digest:
308 * This structure is used to hold a digest of the full flow keys. This is a
309 * larger "hash" of a flow to allow definitively matching specific flows where
310 * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
311 * that it can be used in CB of skb (see sch_choke for an example).
313 #define FLOW_KEYS_DIGEST_LEN 16
314 struct flow_keys_digest {
315 u8 data[FLOW_KEYS_DIGEST_LEN];
318 void make_flow_keys_digest(struct flow_keys_digest *digest,
319 const struct flow_keys *flow);
321 static inline bool flow_keys_have_l4(const struct flow_keys *keys)
323 return (keys->ports.ports || keys->tags.flow_label);
326 u32 flow_hash_from_keys(struct flow_keys *keys);
327 void skb_flow_get_icmp_tci(const struct sk_buff *skb,
328 struct flow_dissector_key_icmp *key_icmp,
329 void *data, int thoff, int hlen);
331 static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector,
332 enum flow_dissector_key_id key_id)
334 return flow_dissector->used_keys & (1 << key_id);
337 static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
338 enum flow_dissector_key_id key_id,
339 void *target_container)
341 return ((char *)target_container) + flow_dissector->offset[key_id];
344 struct bpf_flow_dissector {
345 struct bpf_flow_keys *flow_keys;
346 const struct sk_buff *skb;