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 <linux/string.h>
9 #include <uapi/linux/if_ether.h>
16 * struct flow_dissector_key_control:
17 * @thoff: Transport header offset
18 * @addr_type: Type of key. One of FLOW_DISSECTOR_KEY_*
19 * @flags: Key flags. Any of FLOW_DIS_(IS_FRAGMENT|FIRST_FRAGENCAPSULATION)
21 struct flow_dissector_key_control {
27 #define FLOW_DIS_IS_FRAGMENT BIT(0)
28 #define FLOW_DIS_FIRST_FRAG BIT(1)
29 #define FLOW_DIS_ENCAPSULATION BIT(2)
31 enum flow_dissect_ret {
32 FLOW_DISSECT_RET_OUT_GOOD,
33 FLOW_DISSECT_RET_OUT_BAD,
34 FLOW_DISSECT_RET_PROTO_AGAIN,
35 FLOW_DISSECT_RET_IPPROTO_AGAIN,
36 FLOW_DISSECT_RET_CONTINUE,
40 * struct flow_dissector_key_basic:
41 * @n_proto: Network header protocol (eg. IPv4/IPv6)
42 * @ip_proto: Transport header protocol (eg. TCP/UDP)
45 struct flow_dissector_key_basic {
51 struct flow_dissector_key_tags {
55 struct flow_dissector_key_vlan {
69 struct flow_dissector_mpls_lse {
76 #define FLOW_DIS_MPLS_MAX 7
77 struct flow_dissector_key_mpls {
78 struct flow_dissector_mpls_lse ls[FLOW_DIS_MPLS_MAX]; /* Label Stack */
79 u8 used_lses; /* One bit set for each Label Stack Entry in use */
82 static inline void dissector_set_mpls_lse(struct flow_dissector_key_mpls *mpls,
85 mpls->used_lses |= 1 << lse_index;
88 #define FLOW_DIS_TUN_OPTS_MAX 255
90 * struct flow_dissector_key_enc_opts:
91 * @data: tunnel option data
92 * @len: length of tunnel option data
93 * @dst_opt_type: tunnel option type
95 struct flow_dissector_key_enc_opts {
96 u8 data[FLOW_DIS_TUN_OPTS_MAX]; /* Using IP_TUNNEL_OPTS_MAX is desired
97 * here but seems difficult to #include
103 struct flow_dissector_key_keyid {
108 * struct flow_dissector_key_ipv4_addrs:
109 * @src: source ip address
110 * @dst: destination ip address
112 struct flow_dissector_key_ipv4_addrs {
113 /* (src,dst) must be grouped, in the same way than in IP header */
119 * struct flow_dissector_key_ipv6_addrs:
120 * @src: source ip address
121 * @dst: destination ip address
123 struct flow_dissector_key_ipv6_addrs {
124 /* (src,dst) must be grouped, in the same way than in IP header */
130 * struct flow_dissector_key_tipc:
131 * @key: source node address combined with selector
133 struct flow_dissector_key_tipc {
138 * struct flow_dissector_key_addrs:
139 * @v4addrs: IPv4 addresses
140 * @v6addrs: IPv6 addresses
143 struct flow_dissector_key_addrs {
145 struct flow_dissector_key_ipv4_addrs v4addrs;
146 struct flow_dissector_key_ipv6_addrs v6addrs;
147 struct flow_dissector_key_tipc tipckey;
152 * struct flow_dissector_key_arp:
153 * @sip: Sender IP address
154 * @tip: Target IP address
156 * @sha: Sender hardware address
157 * @tha: Target hardware address
159 struct flow_dissector_key_arp {
163 unsigned char sha[ETH_ALEN];
164 unsigned char tha[ETH_ALEN];
168 * struct flow_dissector_key_ports:
169 * @ports: port numbers of Transport header
170 * @src: source port number
171 * @dst: destination port number
173 struct flow_dissector_key_ports {
184 * struct flow_dissector_key_ports_range
185 * @tp: port number from packet
186 * @tp_min: min port number in range
187 * @tp_max: max port number in range
189 struct flow_dissector_key_ports_range {
191 struct flow_dissector_key_ports tp;
193 struct flow_dissector_key_ports tp_min;
194 struct flow_dissector_key_ports tp_max;
200 * struct flow_dissector_key_icmp:
203 * @id: Session identifier
205 struct flow_dissector_key_icmp {
214 * struct flow_dissector_key_eth_addrs:
215 * @src: source Ethernet address
216 * @dst: destination Ethernet address
218 struct flow_dissector_key_eth_addrs {
219 /* (dst,src) must be grouped, in the same way than in ETH header */
220 unsigned char dst[ETH_ALEN];
221 unsigned char src[ETH_ALEN];
225 * struct flow_dissector_key_tcp:
228 struct flow_dissector_key_tcp {
233 * struct flow_dissector_key_ip:
237 struct flow_dissector_key_ip {
243 * struct flow_dissector_key_meta:
244 * @ingress_ifindex: ingress ifindex
245 * @ingress_iftype: ingress interface type
247 struct flow_dissector_key_meta {
253 * struct flow_dissector_key_ct:
254 * @ct_state: conntrack state after converting with map
255 * @ct_mark: conttrack mark
256 * @ct_zone: conntrack zone
257 * @ct_labels: conntrack labels
259 struct flow_dissector_key_ct {
267 * struct flow_dissector_key_hash:
270 struct flow_dissector_key_hash {
275 * struct flow_dissector_key_num_of_vlans:
276 * @num_of_vlans: num_of_vlans value
278 struct flow_dissector_key_num_of_vlans {
283 * struct flow_dissector_key_pppoe:
284 * @session_id: pppoe session id
285 * @ppp_proto: ppp protocol
286 * @type: pppoe eth type
288 struct flow_dissector_key_pppoe {
295 * struct flow_dissector_key_l2tpv3:
296 * @session_id: identifier for a l2tp session
298 struct flow_dissector_key_l2tpv3 {
302 enum flow_dissector_key_id {
303 FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
304 FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
305 FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
306 FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
307 FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
308 FLOW_DISSECTOR_KEY_PORTS_RANGE, /* struct flow_dissector_key_ports */
309 FLOW_DISSECTOR_KEY_ICMP, /* struct flow_dissector_key_icmp */
310 FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
311 FLOW_DISSECTOR_KEY_TIPC, /* struct flow_dissector_key_tipc */
312 FLOW_DISSECTOR_KEY_ARP, /* struct flow_dissector_key_arp */
313 FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_vlan */
314 FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_tags */
315 FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
316 FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
317 FLOW_DISSECTOR_KEY_ENC_KEYID, /* struct flow_dissector_key_keyid */
318 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
319 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
320 FLOW_DISSECTOR_KEY_ENC_CONTROL, /* struct flow_dissector_key_control */
321 FLOW_DISSECTOR_KEY_ENC_PORTS, /* struct flow_dissector_key_ports */
322 FLOW_DISSECTOR_KEY_MPLS, /* struct flow_dissector_key_mpls */
323 FLOW_DISSECTOR_KEY_TCP, /* struct flow_dissector_key_tcp */
324 FLOW_DISSECTOR_KEY_IP, /* struct flow_dissector_key_ip */
325 FLOW_DISSECTOR_KEY_CVLAN, /* struct flow_dissector_key_vlan */
326 FLOW_DISSECTOR_KEY_ENC_IP, /* struct flow_dissector_key_ip */
327 FLOW_DISSECTOR_KEY_ENC_OPTS, /* struct flow_dissector_key_enc_opts */
328 FLOW_DISSECTOR_KEY_META, /* struct flow_dissector_key_meta */
329 FLOW_DISSECTOR_KEY_CT, /* struct flow_dissector_key_ct */
330 FLOW_DISSECTOR_KEY_HASH, /* struct flow_dissector_key_hash */
331 FLOW_DISSECTOR_KEY_NUM_OF_VLANS, /* struct flow_dissector_key_num_of_vlans */
332 FLOW_DISSECTOR_KEY_PPPOE, /* struct flow_dissector_key_pppoe */
333 FLOW_DISSECTOR_KEY_L2TPV3, /* struct flow_dissector_key_l2tpv3 */
335 FLOW_DISSECTOR_KEY_MAX,
338 #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG BIT(0)
339 #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL BIT(1)
340 #define FLOW_DISSECTOR_F_STOP_AT_ENCAP BIT(2)
341 #define FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP BIT(3)
343 struct flow_dissector_key {
344 enum flow_dissector_key_id key_id;
345 size_t offset; /* offset of struct flow_dissector_key_*
346 in target the struct */
349 struct flow_dissector {
350 unsigned int used_keys; /* each bit repesents presence of one key id */
351 unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
354 struct flow_keys_basic {
355 struct flow_dissector_key_control control;
356 struct flow_dissector_key_basic basic;
360 struct flow_dissector_key_control control;
361 #define FLOW_KEYS_HASH_START_FIELD basic
362 struct flow_dissector_key_basic basic __aligned(SIPHASH_ALIGNMENT);
363 struct flow_dissector_key_tags tags;
364 struct flow_dissector_key_vlan vlan;
365 struct flow_dissector_key_vlan cvlan;
366 struct flow_dissector_key_keyid keyid;
367 struct flow_dissector_key_ports ports;
368 struct flow_dissector_key_icmp icmp;
369 /* 'addrs' must be the last member */
370 struct flow_dissector_key_addrs addrs;
373 #define FLOW_KEYS_HASH_OFFSET \
374 offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
376 __be32 flow_get_u32_src(const struct flow_keys *flow);
377 __be32 flow_get_u32_dst(const struct flow_keys *flow);
379 extern struct flow_dissector flow_keys_dissector;
380 extern struct flow_dissector flow_keys_basic_dissector;
382 /* struct flow_keys_digest:
384 * This structure is used to hold a digest of the full flow keys. This is a
385 * larger "hash" of a flow to allow definitively matching specific flows where
386 * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
387 * that it can be used in CB of skb (see sch_choke for an example).
389 #define FLOW_KEYS_DIGEST_LEN 16
390 struct flow_keys_digest {
391 u8 data[FLOW_KEYS_DIGEST_LEN];
394 void make_flow_keys_digest(struct flow_keys_digest *digest,
395 const struct flow_keys *flow);
397 static inline bool flow_keys_have_l4(const struct flow_keys *keys)
399 return (keys->ports.ports || keys->tags.flow_label);
402 u32 flow_hash_from_keys(struct flow_keys *keys);
403 void skb_flow_get_icmp_tci(const struct sk_buff *skb,
404 struct flow_dissector_key_icmp *key_icmp,
405 const void *data, int thoff, int hlen);
407 static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector,
408 enum flow_dissector_key_id key_id)
410 return flow_dissector->used_keys & (1 << key_id);
413 static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
414 enum flow_dissector_key_id key_id,
415 void *target_container)
417 return ((char *)target_container) + flow_dissector->offset[key_id];
420 struct bpf_flow_dissector {
421 struct bpf_flow_keys *flow_keys;
422 const struct sk_buff *skb;
424 const void *data_end;
428 flow_dissector_init_keys(struct flow_dissector_key_control *key_control,
429 struct flow_dissector_key_basic *key_basic)
431 memset(key_control, 0, sizeof(*key_control));
432 memset(key_basic, 0, sizeof(*key_basic));
435 #ifdef CONFIG_BPF_SYSCALL
436 int flow_dissector_bpf_prog_attach_check(struct net *net,
437 struct bpf_prog *prog);
438 #endif /* CONFIG_BPF_SYSCALL */