Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 2 | #ifndef _NET_FLOW_DISSECTOR_H |
| 3 | #define _NET_FLOW_DISSECTOR_H |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 4 | |
Jiri Pirko | c3f8eae | 2015-05-12 14:56:17 +0200 | [diff] [blame] | 5 | #include <linux/types.h> |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 6 | #include <linux/in6.h> |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 7 | #include <uapi/linux/if_ether.h> |
Jiri Pirko | c3f8eae | 2015-05-12 14:56:17 +0200 | [diff] [blame] | 8 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 9 | /** |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 10 | * struct flow_dissector_key_control: |
| 11 | * @thoff: Transport header offset |
| 12 | */ |
| 13 | struct flow_dissector_key_control { |
| 14 | u16 thoff; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 15 | u16 addr_type; |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 16 | u32 flags; |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 17 | }; |
| 18 | |
David S. Miller | 4b36993 | 2015-09-01 16:46:08 -0700 | [diff] [blame] | 19 | #define FLOW_DIS_IS_FRAGMENT BIT(0) |
| 20 | #define FLOW_DIS_FIRST_FRAG BIT(1) |
| 21 | #define FLOW_DIS_ENCAPSULATION BIT(2) |
| 22 | |
Tom Herbert | 3a1214e | 2017-09-01 14:04:11 -0700 | [diff] [blame] | 23 | enum 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 Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 31 | /** |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 32 | * 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 | */ |
| 37 | struct flow_dissector_key_basic { |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 38 | __be16 n_proto; |
| 39 | u8 ip_proto; |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 40 | u8 padding; |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 43 | struct flow_dissector_key_tags { |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 44 | u32 flow_label; |
| 45 | }; |
| 46 | |
| 47 | struct flow_dissector_key_vlan { |
| 48 | u16 vlan_id:12, |
| 49 | vlan_priority:3; |
| 50 | u16 padding; |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Benjamin LaHaise | 029c1ec | 2017-04-22 16:52:46 -0400 | [diff] [blame] | 53 | struct flow_dissector_key_mpls { |
| 54 | u32 mpls_ttl:8, |
| 55 | mpls_bos:1, |
| 56 | mpls_tc:3, |
| 57 | mpls_label:20; |
| 58 | }; |
| 59 | |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 60 | struct flow_dissector_key_keyid { |
| 61 | __be32 keyid; |
| 62 | }; |
| 63 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 64 | /** |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 65 | * struct flow_dissector_key_ipv4_addrs: |
| 66 | * @src: source ip address |
| 67 | * @dst: destination ip address |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 68 | */ |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 69 | struct flow_dissector_key_ipv4_addrs { |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 70 | /* (src,dst) must be grouped, in the same way than in IP header */ |
| 71 | __be32 src; |
| 72 | __be32 dst; |
| 73 | }; |
| 74 | |
| 75 | /** |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 76 | * struct flow_dissector_key_ipv6_addrs: |
| 77 | * @src: source ip address |
| 78 | * @dst: destination ip address |
| 79 | */ |
| 80 | struct flow_dissector_key_ipv6_addrs { |
| 81 | /* (src,dst) must be grouped, in the same way than in IP header */ |
| 82 | struct in6_addr src; |
| 83 | struct in6_addr dst; |
| 84 | }; |
| 85 | |
| 86 | /** |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 87 | * struct flow_dissector_key_tipc: |
| 88 | * @key: source node address combined with selector |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 89 | */ |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 90 | struct flow_dissector_key_tipc { |
| 91 | __be32 key; |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /** |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 95 | * struct flow_dissector_key_addrs: |
| 96 | * @v4addrs: IPv4 addresses |
| 97 | * @v6addrs: IPv6 addresses |
| 98 | */ |
| 99 | struct flow_dissector_key_addrs { |
| 100 | union { |
| 101 | struct flow_dissector_key_ipv4_addrs v4addrs; |
| 102 | struct flow_dissector_key_ipv6_addrs v6addrs; |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 103 | struct flow_dissector_key_tipc tipckey; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 104 | }; |
| 105 | }; |
| 106 | |
| 107 | /** |
Simon Horman | 5573335 | 2017-01-11 14:05:42 +0100 | [diff] [blame] | 108 | * flow_dissector_key_arp: |
| 109 | * @ports: Operation, source and target addresses for an ARP header |
| 110 | * for Ethernet hardware addresses and IPv4 protocol addresses |
| 111 | * sip: Sender IP address |
| 112 | * tip: Target IP address |
| 113 | * op: Operation |
| 114 | * sha: Sender hardware address |
| 115 | * tpa: Target hardware address |
| 116 | */ |
| 117 | struct flow_dissector_key_arp { |
| 118 | __u32 sip; |
| 119 | __u32 tip; |
| 120 | __u8 op; |
| 121 | unsigned char sha[ETH_ALEN]; |
| 122 | unsigned char tha[ETH_ALEN]; |
| 123 | }; |
| 124 | |
| 125 | /** |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 126 | * flow_dissector_key_tp_ports: |
| 127 | * @ports: port numbers of Transport header |
Jiri Pirko | 59346af | 2015-05-12 14:56:20 +0200 | [diff] [blame] | 128 | * src: source port number |
| 129 | * dst: destination port number |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 130 | */ |
| 131 | struct flow_dissector_key_ports { |
| 132 | union { |
| 133 | __be32 ports; |
Jiri Pirko | 59346af | 2015-05-12 14:56:20 +0200 | [diff] [blame] | 134 | struct { |
| 135 | __be16 src; |
| 136 | __be16 dst; |
| 137 | }; |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 138 | }; |
| 139 | }; |
| 140 | |
Simon Horman | 972d387 | 2016-12-07 13:48:27 +0100 | [diff] [blame] | 141 | /** |
| 142 | * flow_dissector_key_icmp: |
| 143 | * @ports: type and code of ICMP header |
| 144 | * icmp: ICMP type (high) and code (low) |
| 145 | * type: ICMP type |
| 146 | * code: ICMP code |
| 147 | */ |
| 148 | struct flow_dissector_key_icmp { |
| 149 | union { |
| 150 | __be16 icmp; |
| 151 | struct { |
| 152 | u8 type; |
| 153 | u8 code; |
| 154 | }; |
| 155 | }; |
| 156 | }; |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 157 | |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 158 | /** |
| 159 | * struct flow_dissector_key_eth_addrs: |
| 160 | * @src: source Ethernet address |
| 161 | * @dst: destination Ethernet address |
| 162 | */ |
| 163 | struct flow_dissector_key_eth_addrs { |
| 164 | /* (dst,src) must be grouped, in the same way than in ETH header */ |
| 165 | unsigned char dst[ETH_ALEN]; |
| 166 | unsigned char src[ETH_ALEN]; |
| 167 | }; |
| 168 | |
Jiri Pirko | ac4bb5d | 2017-05-23 18:40:44 +0200 | [diff] [blame] | 169 | /** |
| 170 | * struct flow_dissector_key_tcp: |
| 171 | * @flags: flags |
| 172 | */ |
| 173 | struct flow_dissector_key_tcp { |
| 174 | __be16 flags; |
| 175 | }; |
| 176 | |
Or Gerlitz | 518d8a2 | 2017-06-01 21:37:37 +0300 | [diff] [blame] | 177 | /** |
| 178 | * struct flow_dissector_key_ip: |
| 179 | * @tos: tos |
| 180 | * @ttl: ttl |
| 181 | */ |
| 182 | struct flow_dissector_key_ip { |
| 183 | __u8 tos; |
| 184 | __u8 ttl; |
| 185 | }; |
| 186 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 187 | enum flow_dissector_key_id { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 188 | FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */ |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 189 | FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */ |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 190 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */ |
| 191 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */ |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 192 | FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */ |
Simon Horman | 972d387 | 2016-12-07 13:48:27 +0100 | [diff] [blame] | 193 | FLOW_DISSECTOR_KEY_ICMP, /* struct flow_dissector_key_icmp */ |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 194 | FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */ |
Jon Maloy | 8d6e79d | 2017-11-08 09:59:26 +0100 | [diff] [blame] | 195 | FLOW_DISSECTOR_KEY_TIPC, /* struct flow_dissector_key_tipc */ |
Simon Horman | 5573335 | 2017-01-11 14:05:42 +0100 | [diff] [blame] | 196 | FLOW_DISSECTOR_KEY_ARP, /* struct flow_dissector_key_arp */ |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 197 | FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_flow_vlan */ |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 198 | FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */ |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 199 | FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */ |
Tom Herbert | b3baa0f | 2015-06-04 09:16:46 -0700 | [diff] [blame] | 200 | FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */ |
Hadar Hen Zion | 9ba6a9a | 2016-11-07 15:14:37 +0200 | [diff] [blame] | 201 | FLOW_DISSECTOR_KEY_ENC_KEYID, /* struct flow_dissector_key_keyid */ |
| 202 | FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */ |
| 203 | FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */ |
| 204 | FLOW_DISSECTOR_KEY_ENC_CONTROL, /* struct flow_dissector_key_control */ |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 205 | FLOW_DISSECTOR_KEY_ENC_PORTS, /* struct flow_dissector_key_ports */ |
Benjamin LaHaise | 029c1ec | 2017-04-22 16:52:46 -0400 | [diff] [blame] | 206 | FLOW_DISSECTOR_KEY_MPLS, /* struct flow_dissector_key_mpls */ |
Jiri Pirko | ac4bb5d | 2017-05-23 18:40:44 +0200 | [diff] [blame] | 207 | FLOW_DISSECTOR_KEY_TCP, /* struct flow_dissector_key_tcp */ |
Or Gerlitz | 518d8a2 | 2017-06-01 21:37:37 +0300 | [diff] [blame] | 208 | FLOW_DISSECTOR_KEY_IP, /* struct flow_dissector_key_ip */ |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 209 | |
| 210 | FLOW_DISSECTOR_KEY_MAX, |
| 211 | }; |
| 212 | |
Tom Herbert | 807e165 | 2015-09-01 09:24:28 -0700 | [diff] [blame] | 213 | #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG BIT(0) |
Tom Herbert | 8306b68 | 2015-09-01 09:24:30 -0700 | [diff] [blame] | 214 | #define FLOW_DISSECTOR_F_STOP_AT_L3 BIT(1) |
Tom Herbert | 872b1ab | 2015-09-01 09:24:31 -0700 | [diff] [blame] | 215 | #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL BIT(2) |
Tom Herbert | 823b969 | 2015-09-01 09:24:32 -0700 | [diff] [blame] | 216 | #define FLOW_DISSECTOR_F_STOP_AT_ENCAP BIT(3) |
Tom Herbert | 807e165 | 2015-09-01 09:24:28 -0700 | [diff] [blame] | 217 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 218 | struct flow_dissector_key { |
| 219 | enum flow_dissector_key_id key_id; |
| 220 | size_t offset; /* offset of struct flow_dissector_key_* |
| 221 | in target the struct */ |
| 222 | }; |
| 223 | |
| 224 | struct flow_dissector { |
| 225 | unsigned int used_keys; /* each bit repesents presence of one key id */ |
| 226 | unsigned short int offset[FLOW_DISSECTOR_KEY_MAX]; |
| 227 | }; |
| 228 | |
Paolo Abeni | 72a338b | 2018-05-04 11:32:59 +0200 | [diff] [blame] | 229 | struct flow_keys_basic { |
| 230 | struct flow_dissector_key_control control; |
| 231 | struct flow_dissector_key_basic basic; |
| 232 | }; |
| 233 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 234 | struct flow_keys { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 235 | struct flow_dissector_key_control control; |
| 236 | #define FLOW_KEYS_HASH_START_FIELD basic |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 237 | struct flow_dissector_key_basic basic; |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 238 | struct flow_dissector_key_tags tags; |
Hadar Hen Zion | f6a6692 | 2016-08-17 13:36:11 +0300 | [diff] [blame] | 239 | struct flow_dissector_key_vlan vlan; |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame] | 240 | struct flow_dissector_key_keyid keyid; |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 241 | struct flow_dissector_key_ports ports; |
| 242 | struct flow_dissector_key_addrs addrs; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 243 | }; |
| 244 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 245 | #define FLOW_KEYS_HASH_OFFSET \ |
| 246 | offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD) |
| 247 | |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 248 | __be32 flow_get_u32_src(const struct flow_keys *flow); |
| 249 | __be32 flow_get_u32_dst(const struct flow_keys *flow); |
| 250 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 251 | extern struct flow_dissector flow_keys_dissector; |
Paolo Abeni | 72a338b | 2018-05-04 11:32:59 +0200 | [diff] [blame] | 252 | extern struct flow_dissector flow_keys_basic_dissector; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 253 | |
Tom Herbert | 2f59e1e | 2015-05-01 11:30:17 -0700 | [diff] [blame] | 254 | /* struct flow_keys_digest: |
| 255 | * |
| 256 | * This structure is used to hold a digest of the full flow keys. This is a |
| 257 | * larger "hash" of a flow to allow definitively matching specific flows where |
| 258 | * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so |
Wolfram Sang | 53bc017 | 2018-05-06 13:23:52 +0200 | [diff] [blame] | 259 | * that it can be used in CB of skb (see sch_choke for an example). |
Tom Herbert | 2f59e1e | 2015-05-01 11:30:17 -0700 | [diff] [blame] | 260 | */ |
| 261 | #define FLOW_KEYS_DIGEST_LEN 16 |
| 262 | struct flow_keys_digest { |
| 263 | u8 data[FLOW_KEYS_DIGEST_LEN]; |
| 264 | }; |
| 265 | |
| 266 | void make_flow_keys_digest(struct flow_keys_digest *digest, |
| 267 | const struct flow_keys *flow); |
| 268 | |
Gao Feng | 66fdd05 | 2016-08-31 11:16:22 +0800 | [diff] [blame] | 269 | static inline bool flow_keys_have_l4(const struct flow_keys *keys) |
Tom Herbert | bcc8383 | 2015-09-01 09:24:24 -0700 | [diff] [blame] | 270 | { |
| 271 | return (keys->ports.ports || keys->tags.flow_label); |
| 272 | } |
| 273 | |
Tom Herbert | c6cc1ca | 2015-09-01 09:24:25 -0700 | [diff] [blame] | 274 | u32 flow_hash_from_keys(struct flow_keys *keys); |
| 275 | |
Amir Vadai | 8de2d79 | 2016-03-08 12:42:30 +0200 | [diff] [blame] | 276 | static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector, |
| 277 | enum flow_dissector_key_id key_id) |
| 278 | { |
| 279 | return flow_dissector->used_keys & (1 << key_id); |
| 280 | } |
| 281 | |
| 282 | static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissector, |
| 283 | enum flow_dissector_key_id key_id, |
| 284 | void *target_container) |
| 285 | { |
| 286 | return ((char *)target_container) + flow_dissector->offset[key_id]; |
| 287 | } |
| 288 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 289 | #endif |