blob: adc24df56b907d4598a1b08d4ab5da01eddc0c85 [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;
50 u16 padding;
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
Tom Herbert1fdd5122015-06-04 09:16:45 -070060struct flow_dissector_key_keyid {
61 __be32 keyid;
62};
63
Jiri Pirkofbff9492015-05-12 14:56:15 +020064/**
Tom Herbertc3f83242015-06-04 09:16:40 -070065 * struct flow_dissector_key_ipv4_addrs:
66 * @src: source ip address
67 * @dst: destination ip address
Jiri Pirkofbff9492015-05-12 14:56:15 +020068 */
Tom Herbertc3f83242015-06-04 09:16:40 -070069struct flow_dissector_key_ipv4_addrs {
Jiri Pirkofbff9492015-05-12 14:56:15 +020070 /* (src,dst) must be grouped, in the same way than in IP header */
71 __be32 src;
72 __be32 dst;
73};
74
75/**
Tom Herbertc3f83242015-06-04 09:16:40 -070076 * struct flow_dissector_key_ipv6_addrs:
77 * @src: source ip address
78 * @dst: destination ip address
79 */
80struct 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 Maloy8d6e79d2017-11-08 09:59:26 +010087 * struct flow_dissector_key_tipc:
88 * @key: source node address combined with selector
Tom Herbert9f249082015-06-04 09:16:41 -070089 */
Jon Maloy8d6e79d2017-11-08 09:59:26 +010090struct flow_dissector_key_tipc {
91 __be32 key;
Tom Herbert9f249082015-06-04 09:16:41 -070092};
93
94/**
Tom Herbertc3f83242015-06-04 09:16:40 -070095 * struct flow_dissector_key_addrs:
96 * @v4addrs: IPv4 addresses
97 * @v6addrs: IPv6 addresses
98 */
99struct flow_dissector_key_addrs {
100 union {
101 struct flow_dissector_key_ipv4_addrs v4addrs;
102 struct flow_dissector_key_ipv6_addrs v6addrs;
Jon Maloy8d6e79d2017-11-08 09:59:26 +0100103 struct flow_dissector_key_tipc tipckey;
Tom Herbertc3f83242015-06-04 09:16:40 -0700104 };
105};
106
107/**
Simon Horman55733352017-01-11 14:05:42 +0100108 * 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 */
117struct 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 Pirkofbff9492015-05-12 14:56:15 +0200126 * flow_dissector_key_tp_ports:
127 * @ports: port numbers of Transport header
Jiri Pirko59346af2015-05-12 14:56:20 +0200128 * src: source port number
129 * dst: destination port number
Jiri Pirkofbff9492015-05-12 14:56:15 +0200130 */
131struct flow_dissector_key_ports {
132 union {
133 __be32 ports;
Jiri Pirko59346af2015-05-12 14:56:20 +0200134 struct {
135 __be16 src;
136 __be16 dst;
137 };
Jiri Pirkofbff9492015-05-12 14:56:15 +0200138 };
139};
140
Simon Horman972d3872016-12-07 13:48:27 +0100141/**
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 */
148struct flow_dissector_key_icmp {
149 union {
150 __be16 icmp;
151 struct {
152 u8 type;
153 u8 code;
154 };
155 };
156};
Jiri Pirkob9249332015-05-12 14:56:18 +0200157
Jiri Pirko67a900c2015-05-12 14:56:19 +0200158/**
159 * struct flow_dissector_key_eth_addrs:
160 * @src: source Ethernet address
161 * @dst: destination Ethernet address
162 */
163struct 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 Pirkoac4bb5d2017-05-23 18:40:44 +0200169/**
170 * struct flow_dissector_key_tcp:
171 * @flags: flags
172 */
173struct flow_dissector_key_tcp {
174 __be16 flags;
175};
176
Or Gerlitz518d8a22017-06-01 21:37:37 +0300177/**
178 * struct flow_dissector_key_ip:
179 * @tos: tos
180 * @ttl: ttl
181 */
182struct flow_dissector_key_ip {
183 __u8 tos;
184 __u8 ttl;
185};
186
Jiri Pirkofbff9492015-05-12 14:56:15 +0200187enum flow_dissector_key_id {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700188 FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200189 FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
Tom Herbertc3f83242015-06-04 09:16:40 -0700190 FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
191 FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200192 FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
Simon Horman972d3872016-12-07 13:48:27 +0100193 FLOW_DISSECTOR_KEY_ICMP, /* struct flow_dissector_key_icmp */
Jiri Pirko67a900c2015-05-12 14:56:19 +0200194 FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
Jon Maloy8d6e79d2017-11-08 09:59:26 +0100195 FLOW_DISSECTOR_KEY_TIPC, /* struct flow_dissector_key_tipc */
Simon Horman55733352017-01-11 14:05:42 +0100196 FLOW_DISSECTOR_KEY_ARP, /* struct flow_dissector_key_arp */
Hadar Hen Zionf6a66922016-08-17 13:36:11 +0300197 FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_flow_vlan */
Tom Herbert87ee9e52015-06-04 09:16:44 -0700198 FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
Tom Herbert1fdd5122015-06-04 09:16:45 -0700199 FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
Tom Herbertb3baa0f2015-06-04 09:16:46 -0700200 FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
Hadar Hen Zion9ba6a9a2016-11-07 15:14:37 +0200201 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 Zionf4d997f2016-11-07 15:14:39 +0200205 FLOW_DISSECTOR_KEY_ENC_PORTS, /* struct flow_dissector_key_ports */
Benjamin LaHaise029c1ec2017-04-22 16:52:46 -0400206 FLOW_DISSECTOR_KEY_MPLS, /* struct flow_dissector_key_mpls */
Jiri Pirkoac4bb5d2017-05-23 18:40:44 +0200207 FLOW_DISSECTOR_KEY_TCP, /* struct flow_dissector_key_tcp */
Or Gerlitz518d8a22017-06-01 21:37:37 +0300208 FLOW_DISSECTOR_KEY_IP, /* struct flow_dissector_key_ip */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200209
210 FLOW_DISSECTOR_KEY_MAX,
211};
212
Tom Herbert807e1652015-09-01 09:24:28 -0700213#define FLOW_DISSECTOR_F_PARSE_1ST_FRAG BIT(0)
Tom Herbert8306b682015-09-01 09:24:30 -0700214#define FLOW_DISSECTOR_F_STOP_AT_L3 BIT(1)
Tom Herbert872b1ab2015-09-01 09:24:31 -0700215#define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL BIT(2)
Tom Herbert823b9692015-09-01 09:24:32 -0700216#define FLOW_DISSECTOR_F_STOP_AT_ENCAP BIT(3)
Tom Herbert807e1652015-09-01 09:24:28 -0700217
Jiri Pirkofbff9492015-05-12 14:56:15 +0200218struct 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
224struct 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 Abeni72a338b2018-05-04 11:32:59 +0200229struct flow_keys_basic {
230 struct flow_dissector_key_control control;
231 struct flow_dissector_key_basic basic;
232};
233
Jiri Pirko06635a32015-05-12 14:56:16 +0200234struct flow_keys {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700235 struct flow_dissector_key_control control;
236#define FLOW_KEYS_HASH_START_FIELD basic
Jiri Pirko06635a32015-05-12 14:56:16 +0200237 struct flow_dissector_key_basic basic;
Tom Herbertd34af822015-06-04 09:16:43 -0700238 struct flow_dissector_key_tags tags;
Hadar Hen Zionf6a66922016-08-17 13:36:11 +0300239 struct flow_dissector_key_vlan vlan;
Tom Herbert1fdd5122015-06-04 09:16:45 -0700240 struct flow_dissector_key_keyid keyid;
Tom Herbert42aecaa2015-06-04 09:16:39 -0700241 struct flow_dissector_key_ports ports;
242 struct flow_dissector_key_addrs addrs;
Jiri Pirko06635a32015-05-12 14:56:16 +0200243};
244
Tom Herbert42aecaa2015-06-04 09:16:39 -0700245#define FLOW_KEYS_HASH_OFFSET \
246 offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
247
Tom Herbertc3f83242015-06-04 09:16:40 -0700248__be32 flow_get_u32_src(const struct flow_keys *flow);
249__be32 flow_get_u32_dst(const struct flow_keys *flow);
250
Jiri Pirko06635a32015-05-12 14:56:16 +0200251extern struct flow_dissector flow_keys_dissector;
Paolo Abeni72a338b2018-05-04 11:32:59 +0200252extern struct flow_dissector flow_keys_basic_dissector;
Jiri Pirko06635a32015-05-12 14:56:16 +0200253
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700254/* 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 Sang53bc0172018-05-06 13:23:52 +0200259 * that it can be used in CB of skb (see sch_choke for an example).
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700260 */
261#define FLOW_KEYS_DIGEST_LEN 16
262struct flow_keys_digest {
263 u8 data[FLOW_KEYS_DIGEST_LEN];
264};
265
266void make_flow_keys_digest(struct flow_keys_digest *digest,
267 const struct flow_keys *flow);
268
Gao Feng66fdd052016-08-31 11:16:22 +0800269static inline bool flow_keys_have_l4(const struct flow_keys *keys)
Tom Herbertbcc83832015-09-01 09:24:24 -0700270{
271 return (keys->ports.ports || keys->tags.flow_label);
272}
273
Tom Herbertc6cc1ca2015-09-01 09:24:25 -0700274u32 flow_hash_from_keys(struct flow_keys *keys);
275
Amir Vadai8de2d792016-03-08 12:42:30 +0200276static 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
282static 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 Dumazet0744dd02011-11-28 05:22:18 +0000289#endif