blob: 6c5e8d262607278e2b4934bcd81cc3b6f0ee579e [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>
5#include <linux/skbuff.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;
Tom Herbert42aecaa2015-06-04 09:16:39 -070016};
17
18/**
Jiri Pirkofbff9492015-05-12 14:56:15 +020019 * struct flow_dissector_key_basic:
20 * @thoff: Transport header offset
21 * @n_proto: Network header protocol (eg. IPv4/IPv6)
22 * @ip_proto: Transport header protocol (eg. TCP/UDP)
23 */
24struct flow_dissector_key_basic {
Jiri Pirkofbff9492015-05-12 14:56:15 +020025 __be16 n_proto;
26 u8 ip_proto;
Tom Herbert42aecaa2015-06-04 09:16:39 -070027 u8 padding;
Jiri Pirkofbff9492015-05-12 14:56:15 +020028};
29
Tom Herbertd34af822015-06-04 09:16:43 -070030struct flow_dissector_key_tags {
Tom Herbert87ee9e52015-06-04 09:16:44 -070031 u32 vlan_id:12,
32 flow_label:20;
Tom Herbertd34af822015-06-04 09:16:43 -070033};
34
Tom Herbert1fdd5122015-06-04 09:16:45 -070035struct flow_dissector_key_keyid {
36 __be32 keyid;
37};
38
Jiri Pirkofbff9492015-05-12 14:56:15 +020039/**
Tom Herbertc3f83242015-06-04 09:16:40 -070040 * struct flow_dissector_key_ipv4_addrs:
41 * @src: source ip address
42 * @dst: destination ip address
Jiri Pirkofbff9492015-05-12 14:56:15 +020043 */
Tom Herbertc3f83242015-06-04 09:16:40 -070044struct flow_dissector_key_ipv4_addrs {
Jiri Pirkofbff9492015-05-12 14:56:15 +020045 /* (src,dst) must be grouped, in the same way than in IP header */
46 __be32 src;
47 __be32 dst;
48};
49
50/**
Tom Herbertc3f83242015-06-04 09:16:40 -070051 * struct flow_dissector_key_ipv6_addrs:
52 * @src: source ip address
53 * @dst: destination ip address
54 */
55struct flow_dissector_key_ipv6_addrs {
56 /* (src,dst) must be grouped, in the same way than in IP header */
57 struct in6_addr src;
58 struct in6_addr dst;
59};
60
61/**
Tom Herbert9f249082015-06-04 09:16:41 -070062 * struct flow_dissector_key_tipc_addrs:
63 * @srcnode: source node address
64 */
65struct flow_dissector_key_tipc_addrs {
66 __be32 srcnode;
67};
68
69/**
Tom Herbertc3f83242015-06-04 09:16:40 -070070 * struct flow_dissector_key_addrs:
71 * @v4addrs: IPv4 addresses
72 * @v6addrs: IPv6 addresses
73 */
74struct flow_dissector_key_addrs {
75 union {
76 struct flow_dissector_key_ipv4_addrs v4addrs;
77 struct flow_dissector_key_ipv6_addrs v6addrs;
Tom Herbert9f249082015-06-04 09:16:41 -070078 struct flow_dissector_key_tipc_addrs tipcaddrs;
Tom Herbertc3f83242015-06-04 09:16:40 -070079 };
80};
81
82/**
Jiri Pirkofbff9492015-05-12 14:56:15 +020083 * flow_dissector_key_tp_ports:
84 * @ports: port numbers of Transport header
Jiri Pirko59346af2015-05-12 14:56:20 +020085 * src: source port number
86 * dst: destination port number
Jiri Pirkofbff9492015-05-12 14:56:15 +020087 */
88struct flow_dissector_key_ports {
89 union {
90 __be32 ports;
Jiri Pirko59346af2015-05-12 14:56:20 +020091 struct {
92 __be16 src;
93 __be16 dst;
94 };
Jiri Pirkofbff9492015-05-12 14:56:15 +020095 };
96};
97
Jiri Pirkob9249332015-05-12 14:56:18 +020098
Jiri Pirko67a900c2015-05-12 14:56:19 +020099/**
100 * struct flow_dissector_key_eth_addrs:
101 * @src: source Ethernet address
102 * @dst: destination Ethernet address
103 */
104struct flow_dissector_key_eth_addrs {
105 /* (dst,src) must be grouped, in the same way than in ETH header */
106 unsigned char dst[ETH_ALEN];
107 unsigned char src[ETH_ALEN];
108};
109
Jiri Pirkofbff9492015-05-12 14:56:15 +0200110enum flow_dissector_key_id {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700111 FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200112 FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
Tom Herbertc3f83242015-06-04 09:16:40 -0700113 FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
114 FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200115 FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
Jiri Pirko67a900c2015-05-12 14:56:19 +0200116 FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
Tom Herbert9f249082015-06-04 09:16:41 -0700117 FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
Tom Herbertd34af822015-06-04 09:16:43 -0700118 FLOW_DISSECTOR_KEY_VLANID, /* struct flow_dissector_key_flow_tags */
Tom Herbert87ee9e52015-06-04 09:16:44 -0700119 FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
Tom Herbert1fdd5122015-06-04 09:16:45 -0700120 FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200121
122 FLOW_DISSECTOR_KEY_MAX,
123};
124
125struct flow_dissector_key {
126 enum flow_dissector_key_id key_id;
127 size_t offset; /* offset of struct flow_dissector_key_*
128 in target the struct */
129};
130
131struct flow_dissector {
132 unsigned int used_keys; /* each bit repesents presence of one key id */
133 unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
134};
135
Jiri Pirkofbff9492015-05-12 14:56:15 +0200136void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
137 const struct flow_dissector_key *key,
138 unsigned int key_count);
Jiri Pirko06635a32015-05-12 14:56:16 +0200139
140bool __skb_flow_dissect(const struct sk_buff *skb,
141 struct flow_dissector *flow_dissector,
142 void *target_container,
WANG Cong453a9402014-08-25 17:03:47 -0700143 void *data, __be16 proto, int nhoff, int hlen);
Jiri Pirko1bd758e2015-05-12 14:56:07 +0200144
145static inline bool skb_flow_dissect(const struct sk_buff *skb,
Jiri Pirko06635a32015-05-12 14:56:16 +0200146 struct flow_dissector *flow_dissector,
147 void *target_container)
David S. Miller690e36e2014-08-23 12:13:41 -0700148{
Jiri Pirko06635a32015-05-12 14:56:16 +0200149 return __skb_flow_dissect(skb, flow_dissector, target_container,
150 NULL, 0, 0, 0);
151}
152
153struct flow_keys {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700154 struct flow_dissector_key_control control;
155#define FLOW_KEYS_HASH_START_FIELD basic
Jiri Pirko06635a32015-05-12 14:56:16 +0200156 struct flow_dissector_key_basic basic;
Tom Herbertd34af822015-06-04 09:16:43 -0700157 struct flow_dissector_key_tags tags;
Tom Herbert1fdd5122015-06-04 09:16:45 -0700158 struct flow_dissector_key_keyid keyid;
Tom Herbert42aecaa2015-06-04 09:16:39 -0700159 struct flow_dissector_key_ports ports;
160 struct flow_dissector_key_addrs addrs;
Jiri Pirko06635a32015-05-12 14:56:16 +0200161};
162
Tom Herbert42aecaa2015-06-04 09:16:39 -0700163#define FLOW_KEYS_HASH_OFFSET \
164 offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
165
Tom Herbertc3f83242015-06-04 09:16:40 -0700166__be32 flow_get_u32_src(const struct flow_keys *flow);
167__be32 flow_get_u32_dst(const struct flow_keys *flow);
168
Jiri Pirko06635a32015-05-12 14:56:16 +0200169extern struct flow_dissector flow_keys_dissector;
170extern struct flow_dissector flow_keys_buf_dissector;
171
172static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb,
173 struct flow_keys *flow)
174{
175 memset(flow, 0, sizeof(*flow));
176 return __skb_flow_dissect(skb, &flow_keys_dissector, flow,
177 NULL, 0, 0, 0);
178}
179
180static inline bool skb_flow_dissect_flow_keys_buf(struct flow_keys *flow,
181 void *data, __be16 proto,
182 int nhoff, int hlen)
183{
184 memset(flow, 0, sizeof(*flow));
185 return __skb_flow_dissect(NULL, &flow_keys_buf_dissector, flow,
186 data, proto, nhoff, hlen);
David S. Miller690e36e2014-08-23 12:13:41 -0700187}
Jiri Pirko1bd758e2015-05-12 14:56:07 +0200188
David S. Miller690e36e2014-08-23 12:13:41 -0700189__be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
190 void *data, int hlen_proto);
Jiri Pirko1bd758e2015-05-12 14:56:07 +0200191
192static inline __be32 skb_flow_get_ports(const struct sk_buff *skb,
193 int thoff, u8 ip_proto)
David S. Miller690e36e2014-08-23 12:13:41 -0700194{
195 return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0);
196}
Jiri Pirko1bd758e2015-05-12 14:56:07 +0200197
Tom Herbert5ed20a62014-07-01 21:32:05 -0700198u32 flow_hash_from_keys(struct flow_keys *keys);
Jiri Pirko9c684b52015-05-12 14:56:11 +0200199void __skb_get_hash(struct sk_buff *skb);
Jiri Pirko10b89ee42015-05-12 14:56:09 +0200200u32 skb_get_poff(const struct sk_buff *skb);
201u32 __skb_get_poff(const struct sk_buff *skb, void *data,
202 const struct flow_keys *keys, int hlen);
Jiri Pirko1bd758e2015-05-12 14:56:07 +0200203
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700204/* struct flow_keys_digest:
205 *
206 * This structure is used to hold a digest of the full flow keys. This is a
207 * larger "hash" of a flow to allow definitively matching specific flows where
208 * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
209 * that it can by used in CB of skb (see sch_choke for an example).
210 */
211#define FLOW_KEYS_DIGEST_LEN 16
212struct flow_keys_digest {
213 u8 data[FLOW_KEYS_DIGEST_LEN];
214};
215
216void make_flow_keys_digest(struct flow_keys_digest *digest,
217 const struct flow_keys *flow);
218
Eric Dumazet0744dd02011-11-28 05:22:18 +0000219#endif