blob: 8d21d448daa9d19ad58a753e373035f2a1be59bb [file] [log] [blame]
Jiri Pirko1bd758e2015-05-12 14:56:07 +02001#ifndef _NET_FLOW_DISSECTOR_H
2#define _NET_FLOW_DISSECTOR_H
Eric Dumazet0744dd02011-11-28 05:22:18 +00003
Jiri Pirkoc3f8eae2015-05-12 14:56:17 +02004#include <linux/types.h>
Jiri Pirkob9249332015-05-12 14:56:18 +02005#include <linux/in6.h>
Jiri Pirko67a900c2015-05-12 14:56:19 +02006#include <uapi/linux/if_ether.h>
Jiri Pirkoc3f8eae2015-05-12 14:56:17 +02007
Jiri Pirkofbff9492015-05-12 14:56:15 +02008/**
Tom Herbert42aecaa2015-06-04 09:16:39 -07009 * struct flow_dissector_key_control:
10 * @thoff: Transport header offset
11 */
12struct flow_dissector_key_control {
13 u16 thoff;
Tom Herbertc3f83242015-06-04 09:16:40 -070014 u16 addr_type;
David S. Miller4b369932015-09-01 16:46:08 -070015 u32 flags;
Tom Herbert42aecaa2015-06-04 09:16:39 -070016};
17
David S. Miller4b369932015-09-01 16:46:08 -070018#define FLOW_DIS_IS_FRAGMENT BIT(0)
19#define FLOW_DIS_FIRST_FRAG BIT(1)
20#define FLOW_DIS_ENCAPSULATION BIT(2)
21
Tom Herbert42aecaa2015-06-04 09:16:39 -070022/**
Jiri Pirkofbff9492015-05-12 14:56:15 +020023 * struct flow_dissector_key_basic:
24 * @thoff: Transport header offset
25 * @n_proto: Network header protocol (eg. IPv4/IPv6)
26 * @ip_proto: Transport header protocol (eg. TCP/UDP)
27 */
28struct flow_dissector_key_basic {
Jiri Pirkofbff9492015-05-12 14:56:15 +020029 __be16 n_proto;
30 u8 ip_proto;
Tom Herbert42aecaa2015-06-04 09:16:39 -070031 u8 padding;
Jiri Pirkofbff9492015-05-12 14:56:15 +020032};
33
Tom Herbertd34af822015-06-04 09:16:43 -070034struct flow_dissector_key_tags {
Hadar Hen Zionf6a66922016-08-17 13:36:11 +030035 u32 flow_label;
36};
37
38struct flow_dissector_key_vlan {
39 u16 vlan_id:12,
40 vlan_priority:3;
41 u16 padding;
Tom Herbertd34af822015-06-04 09:16:43 -070042};
43
Benjamin LaHaise029c1ec2017-04-22 16:52:46 -040044struct flow_dissector_key_mpls {
45 u32 mpls_ttl:8,
46 mpls_bos:1,
47 mpls_tc:3,
48 mpls_label:20;
49};
50
Tom Herbert1fdd5122015-06-04 09:16:45 -070051struct flow_dissector_key_keyid {
52 __be32 keyid;
53};
54
Jiri Pirkofbff9492015-05-12 14:56:15 +020055/**
Tom Herbertc3f83242015-06-04 09:16:40 -070056 * struct flow_dissector_key_ipv4_addrs:
57 * @src: source ip address
58 * @dst: destination ip address
Jiri Pirkofbff9492015-05-12 14:56:15 +020059 */
Tom Herbertc3f83242015-06-04 09:16:40 -070060struct flow_dissector_key_ipv4_addrs {
Jiri Pirkofbff9492015-05-12 14:56:15 +020061 /* (src,dst) must be grouped, in the same way than in IP header */
62 __be32 src;
63 __be32 dst;
64};
65
66/**
Tom Herbertc3f83242015-06-04 09:16:40 -070067 * struct flow_dissector_key_ipv6_addrs:
68 * @src: source ip address
69 * @dst: destination ip address
70 */
71struct flow_dissector_key_ipv6_addrs {
72 /* (src,dst) must be grouped, in the same way than in IP header */
73 struct in6_addr src;
74 struct in6_addr dst;
75};
76
77/**
Tom Herbert9f249082015-06-04 09:16:41 -070078 * struct flow_dissector_key_tipc_addrs:
79 * @srcnode: source node address
80 */
81struct flow_dissector_key_tipc_addrs {
82 __be32 srcnode;
83};
84
85/**
Tom Herbertc3f83242015-06-04 09:16:40 -070086 * struct flow_dissector_key_addrs:
87 * @v4addrs: IPv4 addresses
88 * @v6addrs: IPv6 addresses
89 */
90struct flow_dissector_key_addrs {
91 union {
92 struct flow_dissector_key_ipv4_addrs v4addrs;
93 struct flow_dissector_key_ipv6_addrs v6addrs;
Tom Herbert9f249082015-06-04 09:16:41 -070094 struct flow_dissector_key_tipc_addrs tipcaddrs;
Tom Herbertc3f83242015-06-04 09:16:40 -070095 };
96};
97
98/**
Simon Horman55733352017-01-11 14:05:42 +010099 * flow_dissector_key_arp:
100 * @ports: Operation, source and target addresses for an ARP header
101 * for Ethernet hardware addresses and IPv4 protocol addresses
102 * sip: Sender IP address
103 * tip: Target IP address
104 * op: Operation
105 * sha: Sender hardware address
106 * tpa: Target hardware address
107 */
108struct flow_dissector_key_arp {
109 __u32 sip;
110 __u32 tip;
111 __u8 op;
112 unsigned char sha[ETH_ALEN];
113 unsigned char tha[ETH_ALEN];
114};
115
116/**
Jiri Pirkofbff9492015-05-12 14:56:15 +0200117 * flow_dissector_key_tp_ports:
118 * @ports: port numbers of Transport header
Jiri Pirko59346af2015-05-12 14:56:20 +0200119 * src: source port number
120 * dst: destination port number
Jiri Pirkofbff9492015-05-12 14:56:15 +0200121 */
122struct flow_dissector_key_ports {
123 union {
124 __be32 ports;
Jiri Pirko59346af2015-05-12 14:56:20 +0200125 struct {
126 __be16 src;
127 __be16 dst;
128 };
Jiri Pirkofbff9492015-05-12 14:56:15 +0200129 };
130};
131
Simon Horman972d3872016-12-07 13:48:27 +0100132/**
133 * flow_dissector_key_icmp:
134 * @ports: type and code of ICMP header
135 * icmp: ICMP type (high) and code (low)
136 * type: ICMP type
137 * code: ICMP code
138 */
139struct flow_dissector_key_icmp {
140 union {
141 __be16 icmp;
142 struct {
143 u8 type;
144 u8 code;
145 };
146 };
147};
Jiri Pirkob9249332015-05-12 14:56:18 +0200148
Jiri Pirko67a900c2015-05-12 14:56:19 +0200149/**
150 * struct flow_dissector_key_eth_addrs:
151 * @src: source Ethernet address
152 * @dst: destination Ethernet address
153 */
154struct flow_dissector_key_eth_addrs {
155 /* (dst,src) must be grouped, in the same way than in ETH header */
156 unsigned char dst[ETH_ALEN];
157 unsigned char src[ETH_ALEN];
158};
159
Jiri Pirkofbff9492015-05-12 14:56:15 +0200160enum flow_dissector_key_id {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700161 FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200162 FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
Tom Herbertc3f83242015-06-04 09:16:40 -0700163 FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
164 FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200165 FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
Simon Horman972d3872016-12-07 13:48:27 +0100166 FLOW_DISSECTOR_KEY_ICMP, /* struct flow_dissector_key_icmp */
Jiri Pirko67a900c2015-05-12 14:56:19 +0200167 FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
Tom Herbert9f249082015-06-04 09:16:41 -0700168 FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
Simon Horman55733352017-01-11 14:05:42 +0100169 FLOW_DISSECTOR_KEY_ARP, /* struct flow_dissector_key_arp */
Hadar Hen Zionf6a66922016-08-17 13:36:11 +0300170 FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_flow_vlan */
Tom Herbert87ee9e52015-06-04 09:16:44 -0700171 FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
Tom Herbert1fdd5122015-06-04 09:16:45 -0700172 FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
Tom Herbertb3baa0f2015-06-04 09:16:46 -0700173 FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
Hadar Hen Zion9ba6a9a2016-11-07 15:14:37 +0200174 FLOW_DISSECTOR_KEY_ENC_KEYID, /* struct flow_dissector_key_keyid */
175 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
176 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
177 FLOW_DISSECTOR_KEY_ENC_CONTROL, /* struct flow_dissector_key_control */
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200178 FLOW_DISSECTOR_KEY_ENC_PORTS, /* struct flow_dissector_key_ports */
Benjamin LaHaise029c1ec2017-04-22 16:52:46 -0400179 FLOW_DISSECTOR_KEY_MPLS, /* struct flow_dissector_key_mpls */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200180
181 FLOW_DISSECTOR_KEY_MAX,
182};
183
Tom Herbert807e1652015-09-01 09:24:28 -0700184#define FLOW_DISSECTOR_F_PARSE_1ST_FRAG BIT(0)
Tom Herbert8306b682015-09-01 09:24:30 -0700185#define FLOW_DISSECTOR_F_STOP_AT_L3 BIT(1)
Tom Herbert872b1ab2015-09-01 09:24:31 -0700186#define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL BIT(2)
Tom Herbert823b9692015-09-01 09:24:32 -0700187#define FLOW_DISSECTOR_F_STOP_AT_ENCAP BIT(3)
Tom Herbert807e1652015-09-01 09:24:28 -0700188
Jiri Pirkofbff9492015-05-12 14:56:15 +0200189struct flow_dissector_key {
190 enum flow_dissector_key_id key_id;
191 size_t offset; /* offset of struct flow_dissector_key_*
192 in target the struct */
193};
194
195struct flow_dissector {
196 unsigned int used_keys; /* each bit repesents presence of one key id */
197 unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
198};
199
Jiri Pirko06635a32015-05-12 14:56:16 +0200200struct flow_keys {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700201 struct flow_dissector_key_control control;
202#define FLOW_KEYS_HASH_START_FIELD basic
Jiri Pirko06635a32015-05-12 14:56:16 +0200203 struct flow_dissector_key_basic basic;
Tom Herbertd34af822015-06-04 09:16:43 -0700204 struct flow_dissector_key_tags tags;
Hadar Hen Zionf6a66922016-08-17 13:36:11 +0300205 struct flow_dissector_key_vlan vlan;
Tom Herbert1fdd5122015-06-04 09:16:45 -0700206 struct flow_dissector_key_keyid keyid;
Tom Herbert42aecaa2015-06-04 09:16:39 -0700207 struct flow_dissector_key_ports ports;
208 struct flow_dissector_key_addrs addrs;
Jiri Pirko06635a32015-05-12 14:56:16 +0200209};
210
Tom Herbert42aecaa2015-06-04 09:16:39 -0700211#define FLOW_KEYS_HASH_OFFSET \
212 offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
213
Tom Herbertc3f83242015-06-04 09:16:40 -0700214__be32 flow_get_u32_src(const struct flow_keys *flow);
215__be32 flow_get_u32_dst(const struct flow_keys *flow);
216
Jiri Pirko06635a32015-05-12 14:56:16 +0200217extern struct flow_dissector flow_keys_dissector;
218extern struct flow_dissector flow_keys_buf_dissector;
219
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700220/* struct flow_keys_digest:
221 *
222 * This structure is used to hold a digest of the full flow keys. This is a
223 * larger "hash" of a flow to allow definitively matching specific flows where
224 * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
225 * that it can by used in CB of skb (see sch_choke for an example).
226 */
227#define FLOW_KEYS_DIGEST_LEN 16
228struct flow_keys_digest {
229 u8 data[FLOW_KEYS_DIGEST_LEN];
230};
231
232void make_flow_keys_digest(struct flow_keys_digest *digest,
233 const struct flow_keys *flow);
234
Gao Feng66fdd052016-08-31 11:16:22 +0800235static inline bool flow_keys_have_l4(const struct flow_keys *keys)
Tom Herbertbcc83832015-09-01 09:24:24 -0700236{
237 return (keys->ports.ports || keys->tags.flow_label);
238}
239
Tom Herbertc6cc1ca2015-09-01 09:24:25 -0700240u32 flow_hash_from_keys(struct flow_keys *keys);
241
Amir Vadai8de2d792016-03-08 12:42:30 +0200242static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector,
243 enum flow_dissector_key_id key_id)
244{
245 return flow_dissector->used_keys & (1 << key_id);
246}
247
248static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
249 enum flow_dissector_key_id key_id,
250 void *target_container)
251{
252 return ((char *)target_container) + flow_dissector->offset[key_id];
253}
254
Eric Dumazet0744dd02011-11-28 05:22:18 +0000255#endif