blob: 7c5a8d9a8d2a77eb65d11b5358a4d26a11591bdb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Jiri Pirko1bd758e2015-05-12 14:56:07 +02002#ifndef _NET_FLOW_DISSECTOR_H
3#define _NET_FLOW_DISSECTOR_H
Eric Dumazet0744dd02011-11-28 05:22:18 +00004
Jiri Pirkoc3f8eae2015-05-12 14:56:17 +02005#include <linux/types.h>
Jiri Pirkob9249332015-05-12 14:56:18 +02006#include <linux/in6.h>
Jiri Pirko67a900c2015-05-12 14:56:19 +02007#include <uapi/linux/if_ether.h>
Jiri Pirkoc3f8eae2015-05-12 14:56:17 +02008
Jiri Pirkofbff9492015-05-12 14:56:15 +02009/**
Tom Herbert42aecaa2015-06-04 09:16:39 -070010 * struct flow_dissector_key_control:
11 * @thoff: Transport header offset
12 */
13struct flow_dissector_key_control {
14 u16 thoff;
Tom Herbertc3f83242015-06-04 09:16:40 -070015 u16 addr_type;
David S. Miller4b369932015-09-01 16:46:08 -070016 u32 flags;
Tom Herbert42aecaa2015-06-04 09:16:39 -070017};
18
David S. Miller4b369932015-09-01 16:46:08 -070019#define FLOW_DIS_IS_FRAGMENT BIT(0)
20#define FLOW_DIS_FIRST_FRAG BIT(1)
21#define FLOW_DIS_ENCAPSULATION BIT(2)
22
Tom Herbert3a1214e2017-09-01 14:04:11 -070023enum 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
Tom Herbert42aecaa2015-06-04 09:16:39 -070031/**
Jiri Pirkofbff9492015-05-12 14:56:15 +020032 * 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 */
37struct flow_dissector_key_basic {
Jiri Pirkofbff9492015-05-12 14:56:15 +020038 __be16 n_proto;
39 u8 ip_proto;
Tom Herbert42aecaa2015-06-04 09:16:39 -070040 u8 padding;
Jiri Pirkofbff9492015-05-12 14:56:15 +020041};
42
Tom Herbertd34af822015-06-04 09:16:43 -070043struct flow_dissector_key_tags {
Hadar Hen Zionf6a66922016-08-17 13:36:11 +030044 u32 flow_label;
45};
46
47struct flow_dissector_key_vlan {
48 u16 vlan_id:12,
49 vlan_priority:3;
Jianbo Liu2064c3d2018-07-06 05:38:12 +000050 __be16 vlan_tpid;
Tom Herbertd34af822015-06-04 09:16:43 -070051};
52
Benjamin LaHaise029c1ec2017-04-22 16:52:46 -040053struct flow_dissector_key_mpls {
54 u32 mpls_ttl:8,
55 mpls_bos:1,
56 mpls_tc:3,
57 mpls_label:20;
58};
59
Simon Horman92e2c402018-08-07 17:36:00 +020060#define FLOW_DIS_TUN_OPTS_MAX 255
61/**
62 * struct flow_dissector_key_enc_opts:
63 * @data: tunnel option data
64 * @len: length of tunnel option data
65 * @dst_opt_type: tunnel option type
66 */
67struct flow_dissector_key_enc_opts {
68 u8 data[FLOW_DIS_TUN_OPTS_MAX]; /* Using IP_TUNNEL_OPTS_MAX is desired
69 * here but seems difficult to #include
70 */
71 u8 len;
72 __be16 dst_opt_type;
73};
74
Tom Herbert1fdd5122015-06-04 09:16:45 -070075struct flow_dissector_key_keyid {
76 __be32 keyid;
77};
78
Jiri Pirkofbff9492015-05-12 14:56:15 +020079/**
Tom Herbertc3f83242015-06-04 09:16:40 -070080 * struct flow_dissector_key_ipv4_addrs:
81 * @src: source ip address
82 * @dst: destination ip address
Jiri Pirkofbff9492015-05-12 14:56:15 +020083 */
Tom Herbertc3f83242015-06-04 09:16:40 -070084struct flow_dissector_key_ipv4_addrs {
Jiri Pirkofbff9492015-05-12 14:56:15 +020085 /* (src,dst) must be grouped, in the same way than in IP header */
86 __be32 src;
87 __be32 dst;
88};
89
90/**
Tom Herbertc3f83242015-06-04 09:16:40 -070091 * struct flow_dissector_key_ipv6_addrs:
92 * @src: source ip address
93 * @dst: destination ip address
94 */
95struct flow_dissector_key_ipv6_addrs {
96 /* (src,dst) must be grouped, in the same way than in IP header */
97 struct in6_addr src;
98 struct in6_addr dst;
99};
100
101/**
Jon Maloy8d6e79d2017-11-08 09:59:26 +0100102 * struct flow_dissector_key_tipc:
103 * @key: source node address combined with selector
Tom Herbert9f249082015-06-04 09:16:41 -0700104 */
Jon Maloy8d6e79d2017-11-08 09:59:26 +0100105struct flow_dissector_key_tipc {
106 __be32 key;
Tom Herbert9f249082015-06-04 09:16:41 -0700107};
108
109/**
Tom Herbertc3f83242015-06-04 09:16:40 -0700110 * struct flow_dissector_key_addrs:
111 * @v4addrs: IPv4 addresses
112 * @v6addrs: IPv6 addresses
113 */
114struct flow_dissector_key_addrs {
115 union {
116 struct flow_dissector_key_ipv4_addrs v4addrs;
117 struct flow_dissector_key_ipv6_addrs v6addrs;
Jon Maloy8d6e79d2017-11-08 09:59:26 +0100118 struct flow_dissector_key_tipc tipckey;
Tom Herbertc3f83242015-06-04 09:16:40 -0700119 };
120};
121
122/**
Simon Horman55733352017-01-11 14:05:42 +0100123 * flow_dissector_key_arp:
124 * @ports: Operation, source and target addresses for an ARP header
125 * for Ethernet hardware addresses and IPv4 protocol addresses
126 * sip: Sender IP address
127 * tip: Target IP address
128 * op: Operation
129 * sha: Sender hardware address
130 * tpa: Target hardware address
131 */
132struct flow_dissector_key_arp {
133 __u32 sip;
134 __u32 tip;
135 __u8 op;
136 unsigned char sha[ETH_ALEN];
137 unsigned char tha[ETH_ALEN];
138};
139
140/**
Jiri Pirkofbff9492015-05-12 14:56:15 +0200141 * flow_dissector_key_tp_ports:
142 * @ports: port numbers of Transport header
Jiri Pirko59346af2015-05-12 14:56:20 +0200143 * src: source port number
144 * dst: destination port number
Jiri Pirkofbff9492015-05-12 14:56:15 +0200145 */
146struct flow_dissector_key_ports {
147 union {
148 __be32 ports;
Jiri Pirko59346af2015-05-12 14:56:20 +0200149 struct {
150 __be16 src;
151 __be16 dst;
152 };
Jiri Pirkofbff9492015-05-12 14:56:15 +0200153 };
154};
155
Simon Horman972d3872016-12-07 13:48:27 +0100156/**
157 * flow_dissector_key_icmp:
158 * @ports: type and code of ICMP header
159 * icmp: ICMP type (high) and code (low)
160 * type: ICMP type
161 * code: ICMP code
162 */
163struct flow_dissector_key_icmp {
164 union {
165 __be16 icmp;
166 struct {
167 u8 type;
168 u8 code;
169 };
170 };
171};
Jiri Pirkob9249332015-05-12 14:56:18 +0200172
Jiri Pirko67a900c2015-05-12 14:56:19 +0200173/**
174 * struct flow_dissector_key_eth_addrs:
175 * @src: source Ethernet address
176 * @dst: destination Ethernet address
177 */
178struct flow_dissector_key_eth_addrs {
179 /* (dst,src) must be grouped, in the same way than in ETH header */
180 unsigned char dst[ETH_ALEN];
181 unsigned char src[ETH_ALEN];
182};
183
Jiri Pirkoac4bb5d2017-05-23 18:40:44 +0200184/**
185 * struct flow_dissector_key_tcp:
186 * @flags: flags
187 */
188struct flow_dissector_key_tcp {
189 __be16 flags;
190};
191
Or Gerlitz518d8a22017-06-01 21:37:37 +0300192/**
193 * struct flow_dissector_key_ip:
194 * @tos: tos
195 * @ttl: ttl
196 */
197struct flow_dissector_key_ip {
198 __u8 tos;
199 __u8 ttl;
200};
201
Jiri Pirkofbff9492015-05-12 14:56:15 +0200202enum flow_dissector_key_id {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700203 FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200204 FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
Tom Herbertc3f83242015-06-04 09:16:40 -0700205 FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
206 FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200207 FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
Simon Horman972d3872016-12-07 13:48:27 +0100208 FLOW_DISSECTOR_KEY_ICMP, /* struct flow_dissector_key_icmp */
Jiri Pirko67a900c2015-05-12 14:56:19 +0200209 FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
Jon Maloy8d6e79d2017-11-08 09:59:26 +0100210 FLOW_DISSECTOR_KEY_TIPC, /* struct flow_dissector_key_tipc */
Simon Horman55733352017-01-11 14:05:42 +0100211 FLOW_DISSECTOR_KEY_ARP, /* struct flow_dissector_key_arp */
Edward Cree91c45952018-11-27 15:40:59 +0000212 FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_vlan */
213 FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_tags */
Tom Herbert1fdd5122015-06-04 09:16:45 -0700214 FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
Tom Herbertb3baa0f2015-06-04 09:16:46 -0700215 FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
Hadar Hen Zion9ba6a9a2016-11-07 15:14:37 +0200216 FLOW_DISSECTOR_KEY_ENC_KEYID, /* struct flow_dissector_key_keyid */
217 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
218 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
219 FLOW_DISSECTOR_KEY_ENC_CONTROL, /* struct flow_dissector_key_control */
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200220 FLOW_DISSECTOR_KEY_ENC_PORTS, /* struct flow_dissector_key_ports */
Benjamin LaHaise029c1ec2017-04-22 16:52:46 -0400221 FLOW_DISSECTOR_KEY_MPLS, /* struct flow_dissector_key_mpls */
Jiri Pirkoac4bb5d2017-05-23 18:40:44 +0200222 FLOW_DISSECTOR_KEY_TCP, /* struct flow_dissector_key_tcp */
Or Gerlitz518d8a22017-06-01 21:37:37 +0300223 FLOW_DISSECTOR_KEY_IP, /* struct flow_dissector_key_ip */
Edward Cree91c45952018-11-27 15:40:59 +0000224 FLOW_DISSECTOR_KEY_CVLAN, /* struct flow_dissector_key_vlan */
Or Gerlitz5544adb2018-07-17 19:27:17 +0300225 FLOW_DISSECTOR_KEY_ENC_IP, /* struct flow_dissector_key_ip */
Simon Horman92e2c402018-08-07 17:36:00 +0200226 FLOW_DISSECTOR_KEY_ENC_OPTS, /* struct flow_dissector_key_enc_opts */
227
Jiri Pirkofbff9492015-05-12 14:56:15 +0200228 FLOW_DISSECTOR_KEY_MAX,
229};
230
Tom Herbert807e1652015-09-01 09:24:28 -0700231#define FLOW_DISSECTOR_F_PARSE_1ST_FRAG BIT(0)
Tom Herbert8306b682015-09-01 09:24:30 -0700232#define FLOW_DISSECTOR_F_STOP_AT_L3 BIT(1)
Tom Herbert872b1ab2015-09-01 09:24:31 -0700233#define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL BIT(2)
Tom Herbert823b9692015-09-01 09:24:32 -0700234#define FLOW_DISSECTOR_F_STOP_AT_ENCAP BIT(3)
Tom Herbert807e1652015-09-01 09:24:28 -0700235
Jiri Pirkofbff9492015-05-12 14:56:15 +0200236struct flow_dissector_key {
237 enum flow_dissector_key_id key_id;
238 size_t offset; /* offset of struct flow_dissector_key_*
239 in target the struct */
240};
241
242struct flow_dissector {
243 unsigned int used_keys; /* each bit repesents presence of one key id */
244 unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
245};
246
Paolo Abeni72a338b2018-05-04 11:32:59 +0200247struct flow_keys_basic {
248 struct flow_dissector_key_control control;
249 struct flow_dissector_key_basic basic;
250};
251
Jiri Pirko06635a32015-05-12 14:56:16 +0200252struct flow_keys {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700253 struct flow_dissector_key_control control;
254#define FLOW_KEYS_HASH_START_FIELD basic
Jiri Pirko06635a32015-05-12 14:56:16 +0200255 struct flow_dissector_key_basic basic;
Tom Herbertd34af822015-06-04 09:16:43 -0700256 struct flow_dissector_key_tags tags;
Hadar Hen Zionf6a66922016-08-17 13:36:11 +0300257 struct flow_dissector_key_vlan vlan;
Jianbo Liu24c590e2018-07-06 05:38:14 +0000258 struct flow_dissector_key_vlan cvlan;
Tom Herbert1fdd5122015-06-04 09:16:45 -0700259 struct flow_dissector_key_keyid keyid;
Tom Herbert42aecaa2015-06-04 09:16:39 -0700260 struct flow_dissector_key_ports ports;
261 struct flow_dissector_key_addrs addrs;
Jiri Pirko06635a32015-05-12 14:56:16 +0200262};
263
Tom Herbert42aecaa2015-06-04 09:16:39 -0700264#define FLOW_KEYS_HASH_OFFSET \
265 offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
266
Tom Herbertc3f83242015-06-04 09:16:40 -0700267__be32 flow_get_u32_src(const struct flow_keys *flow);
268__be32 flow_get_u32_dst(const struct flow_keys *flow);
269
Jiri Pirko06635a32015-05-12 14:56:16 +0200270extern struct flow_dissector flow_keys_dissector;
Paolo Abeni72a338b2018-05-04 11:32:59 +0200271extern struct flow_dissector flow_keys_basic_dissector;
Jiri Pirko06635a32015-05-12 14:56:16 +0200272
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700273/* struct flow_keys_digest:
274 *
275 * This structure is used to hold a digest of the full flow keys. This is a
276 * larger "hash" of a flow to allow definitively matching specific flows where
277 * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
Wolfram Sang53bc0172018-05-06 13:23:52 +0200278 * that it can be used in CB of skb (see sch_choke for an example).
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700279 */
280#define FLOW_KEYS_DIGEST_LEN 16
281struct flow_keys_digest {
282 u8 data[FLOW_KEYS_DIGEST_LEN];
283};
284
285void make_flow_keys_digest(struct flow_keys_digest *digest,
286 const struct flow_keys *flow);
287
Gao Feng66fdd052016-08-31 11:16:22 +0800288static inline bool flow_keys_have_l4(const struct flow_keys *keys)
Tom Herbertbcc83832015-09-01 09:24:24 -0700289{
290 return (keys->ports.ports || keys->tags.flow_label);
291}
292
Tom Herbertc6cc1ca2015-09-01 09:24:25 -0700293u32 flow_hash_from_keys(struct flow_keys *keys);
294
Amir Vadai8de2d792016-03-08 12:42:30 +0200295static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector,
296 enum flow_dissector_key_id key_id)
297{
298 return flow_dissector->used_keys & (1 << key_id);
299}
300
301static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
302 enum flow_dissector_key_id key_id,
303 void *target_container)
304{
305 return ((char *)target_container) + flow_dissector->offset[key_id];
306}
307
Stanislav Fomichev089b19a2019-04-22 08:55:44 -0700308struct bpf_flow_dissector {
309 struct bpf_flow_keys *flow_keys;
310 const struct sk_buff *skb;
311 void *data;
312 void *data_end;
313};
314
Eric Dumazet0744dd02011-11-28 05:22:18 +0000315#endif