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