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
19 struct flow_dissector_key_control
{
25 #define FLOW_DIS_IS_FRAGMENT BIT(0)
26 #define FLOW_DIS_FIRST_FRAG BIT(1)
27 #define FLOW_DIS_ENCAPSULATION BIT(2)
29 enum flow_dissect_ret
{
30 FLOW_DISSECT_RET_OUT_GOOD
,
31 FLOW_DISSECT_RET_OUT_BAD
,
32 FLOW_DISSECT_RET_PROTO_AGAIN
,
33 FLOW_DISSECT_RET_IPPROTO_AGAIN
,
34 FLOW_DISSECT_RET_CONTINUE
,
38 * struct flow_dissector_key_basic:
39 * @n_proto: Network header protocol (eg. IPv4/IPv6)
40 * @ip_proto: Transport header protocol (eg. TCP/UDP)
42 struct flow_dissector_key_basic
{
48 struct flow_dissector_key_tags
{
52 struct flow_dissector_key_vlan
{
64 struct flow_dissector_mpls_lse
{
71 #define FLOW_DIS_MPLS_MAX 7
72 struct flow_dissector_key_mpls
{
73 struct flow_dissector_mpls_lse ls
[FLOW_DIS_MPLS_MAX
]; /* Label Stack */
74 u8 used_lses
; /* One bit set for each Label Stack Entry in use */
77 static inline void dissector_set_mpls_lse(struct flow_dissector_key_mpls
*mpls
,
80 mpls
->used_lses
|= 1 << lse_index
;
83 #define FLOW_DIS_TUN_OPTS_MAX 255
85 * struct flow_dissector_key_enc_opts:
86 * @data: tunnel option data
87 * @len: length of tunnel option data
88 * @dst_opt_type: tunnel option type
90 struct flow_dissector_key_enc_opts
{
91 u8 data
[FLOW_DIS_TUN_OPTS_MAX
]; /* Using IP_TUNNEL_OPTS_MAX is desired
92 * here but seems difficult to #include
98 struct flow_dissector_key_keyid
{
103 * struct flow_dissector_key_ipv4_addrs:
104 * @src: source ip address
105 * @dst: destination ip address
107 struct flow_dissector_key_ipv4_addrs
{
108 /* (src,dst) must be grouped, in the same way than in IP header */
114 * struct flow_dissector_key_ipv6_addrs:
115 * @src: source ip address
116 * @dst: destination ip address
118 struct flow_dissector_key_ipv6_addrs
{
119 /* (src,dst) must be grouped, in the same way than in IP header */
125 * struct flow_dissector_key_tipc:
126 * @key: source node address combined with selector
128 struct flow_dissector_key_tipc
{
133 * struct flow_dissector_key_addrs:
134 * @v4addrs: IPv4 addresses
135 * @v6addrs: IPv6 addresses
137 struct flow_dissector_key_addrs
{
139 struct flow_dissector_key_ipv4_addrs v4addrs
;
140 struct flow_dissector_key_ipv6_addrs v6addrs
;
141 struct flow_dissector_key_tipc tipckey
;
146 * flow_dissector_key_arp:
147 * @ports: Operation, source and target addresses for an ARP header
148 * for Ethernet hardware addresses and IPv4 protocol addresses
149 * sip: Sender IP address
150 * tip: Target IP address
152 * sha: Sender hardware address
153 * tpa: Target hardware address
155 struct flow_dissector_key_arp
{
159 unsigned char sha
[ETH_ALEN
];
160 unsigned char tha
[ETH_ALEN
];
164 * flow_dissector_key_tp_ports:
165 * @ports: port numbers of Transport header
166 * src: source port number
167 * dst: destination port number
169 struct flow_dissector_key_ports
{
180 * flow_dissector_key_icmp:
183 * id: session identifier
185 struct flow_dissector_key_icmp
{
194 * struct flow_dissector_key_eth_addrs:
195 * @src: source Ethernet address
196 * @dst: destination Ethernet address
198 struct flow_dissector_key_eth_addrs
{
199 /* (dst,src) must be grouped, in the same way than in ETH header */
200 unsigned char dst
[ETH_ALEN
];
201 unsigned char src
[ETH_ALEN
];
205 * struct flow_dissector_key_tcp:
208 struct flow_dissector_key_tcp
{
213 * struct flow_dissector_key_ip:
217 struct flow_dissector_key_ip
{
223 * struct flow_dissector_key_meta:
224 * @ingress_ifindex: ingress ifindex
225 * @ingress_iftype: ingress interface type
227 struct flow_dissector_key_meta
{
233 * struct flow_dissector_key_ct:
234 * @ct_state: conntrack state after converting with map
235 * @ct_mark: conttrack mark
236 * @ct_zone: conntrack zone
237 * @ct_labels: conntrack labels
239 struct flow_dissector_key_ct
{
246 enum flow_dissector_key_id
{
247 FLOW_DISSECTOR_KEY_CONTROL
, /* struct flow_dissector_key_control */
248 FLOW_DISSECTOR_KEY_BASIC
, /* struct flow_dissector_key_basic */
249 FLOW_DISSECTOR_KEY_IPV4_ADDRS
, /* struct flow_dissector_key_ipv4_addrs */
250 FLOW_DISSECTOR_KEY_IPV6_ADDRS
, /* struct flow_dissector_key_ipv6_addrs */
251 FLOW_DISSECTOR_KEY_PORTS
, /* struct flow_dissector_key_ports */
252 FLOW_DISSECTOR_KEY_PORTS_RANGE
, /* struct flow_dissector_key_ports */
253 FLOW_DISSECTOR_KEY_ICMP
, /* struct flow_dissector_key_icmp */
254 FLOW_DISSECTOR_KEY_ETH_ADDRS
, /* struct flow_dissector_key_eth_addrs */
255 FLOW_DISSECTOR_KEY_TIPC
, /* struct flow_dissector_key_tipc */
256 FLOW_DISSECTOR_KEY_ARP
, /* struct flow_dissector_key_arp */
257 FLOW_DISSECTOR_KEY_VLAN
, /* struct flow_dissector_key_vlan */
258 FLOW_DISSECTOR_KEY_FLOW_LABEL
, /* struct flow_dissector_key_tags */
259 FLOW_DISSECTOR_KEY_GRE_KEYID
, /* struct flow_dissector_key_keyid */
260 FLOW_DISSECTOR_KEY_MPLS_ENTROPY
, /* struct flow_dissector_key_keyid */
261 FLOW_DISSECTOR_KEY_ENC_KEYID
, /* struct flow_dissector_key_keyid */
262 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS
, /* struct flow_dissector_key_ipv4_addrs */
263 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS
, /* struct flow_dissector_key_ipv6_addrs */
264 FLOW_DISSECTOR_KEY_ENC_CONTROL
, /* struct flow_dissector_key_control */
265 FLOW_DISSECTOR_KEY_ENC_PORTS
, /* struct flow_dissector_key_ports */
266 FLOW_DISSECTOR_KEY_MPLS
, /* struct flow_dissector_key_mpls */
267 FLOW_DISSECTOR_KEY_TCP
, /* struct flow_dissector_key_tcp */
268 FLOW_DISSECTOR_KEY_IP
, /* struct flow_dissector_key_ip */
269 FLOW_DISSECTOR_KEY_CVLAN
, /* struct flow_dissector_key_vlan */
270 FLOW_DISSECTOR_KEY_ENC_IP
, /* struct flow_dissector_key_ip */
271 FLOW_DISSECTOR_KEY_ENC_OPTS
, /* struct flow_dissector_key_enc_opts */
272 FLOW_DISSECTOR_KEY_META
, /* struct flow_dissector_key_meta */
273 FLOW_DISSECTOR_KEY_CT
, /* struct flow_dissector_key_ct */
275 FLOW_DISSECTOR_KEY_MAX
,
278 #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG BIT(0)
279 #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL BIT(1)
280 #define FLOW_DISSECTOR_F_STOP_AT_ENCAP BIT(2)
282 struct flow_dissector_key
{
283 enum flow_dissector_key_id key_id
;
284 size_t offset
; /* offset of struct flow_dissector_key_*
285 in target the struct */
288 struct flow_dissector
{
289 unsigned int used_keys
; /* each bit repesents presence of one key id */
290 unsigned short int offset
[FLOW_DISSECTOR_KEY_MAX
];
293 struct flow_keys_basic
{
294 struct flow_dissector_key_control control
;
295 struct flow_dissector_key_basic basic
;
299 struct flow_dissector_key_control control
;
300 #define FLOW_KEYS_HASH_START_FIELD basic
301 struct flow_dissector_key_basic basic
__aligned(SIPHASH_ALIGNMENT
);
302 struct flow_dissector_key_tags tags
;
303 struct flow_dissector_key_vlan vlan
;
304 struct flow_dissector_key_vlan cvlan
;
305 struct flow_dissector_key_keyid keyid
;
306 struct flow_dissector_key_ports ports
;
307 struct flow_dissector_key_icmp icmp
;
308 /* 'addrs' must be the last member */
309 struct flow_dissector_key_addrs addrs
;
312 #define FLOW_KEYS_HASH_OFFSET \
313 offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
315 __be32
flow_get_u32_src(const struct flow_keys
*flow
);
316 __be32
flow_get_u32_dst(const struct flow_keys
*flow
);
318 extern struct flow_dissector flow_keys_dissector
;
319 extern struct flow_dissector flow_keys_basic_dissector
;
321 /* struct flow_keys_digest:
323 * This structure is used to hold a digest of the full flow keys. This is a
324 * larger "hash" of a flow to allow definitively matching specific flows where
325 * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
326 * that it can be used in CB of skb (see sch_choke for an example).
328 #define FLOW_KEYS_DIGEST_LEN 16
329 struct flow_keys_digest
{
330 u8 data
[FLOW_KEYS_DIGEST_LEN
];
333 void make_flow_keys_digest(struct flow_keys_digest
*digest
,
334 const struct flow_keys
*flow
);
336 static inline bool flow_keys_have_l4(const struct flow_keys
*keys
)
338 return (keys
->ports
.ports
|| keys
->tags
.flow_label
);
341 u32
flow_hash_from_keys(struct flow_keys
*keys
);
342 void skb_flow_get_icmp_tci(const struct sk_buff
*skb
,
343 struct flow_dissector_key_icmp
*key_icmp
,
344 void *data
, int thoff
, int hlen
);
346 static inline bool dissector_uses_key(const struct flow_dissector
*flow_dissector
,
347 enum flow_dissector_key_id key_id
)
349 return flow_dissector
->used_keys
& (1 << key_id
);
352 static inline void *skb_flow_dissector_target(struct flow_dissector
*flow_dissector
,
353 enum flow_dissector_key_id key_id
,
354 void *target_container
)
356 return ((char *)target_container
) + flow_dissector
->offset
[key_id
];
359 struct bpf_flow_dissector
{
360 struct bpf_flow_keys
*flow_keys
;
361 const struct sk_buff
*skb
;
367 flow_dissector_init_keys(struct flow_dissector_key_control
*key_control
,
368 struct flow_dissector_key_basic
*key_basic
)
370 memset(key_control
, 0, sizeof(*key_control
));
371 memset(key_basic
, 0, sizeof(*key_basic
));
374 #ifdef CONFIG_BPF_SYSCALL
375 int flow_dissector_bpf_prog_attach_check(struct net
*net
,
376 struct bpf_prog
*prog
);
377 #endif /* CONFIG_BPF_SYSCALL */