• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _NET_FLOW_DISSECTOR_H
2 #define _NET_FLOW_DISSECTOR_H
3 
4 #include <linux/types.h>
5 #include <linux/in6.h>
6 #include <linux/siphash.h>
7 #include <linux/string.h>
8 #include <uapi/linux/if_ether.h>
9 
10 /**
11  * struct flow_dissector_key_control:
12  * @thoff: Transport header offset
13  */
14 struct flow_dissector_key_control {
15 	u16	thoff;
16 	u16	addr_type;
17 	u32	flags;
18 };
19 
20 #define FLOW_DIS_IS_FRAGMENT	BIT(0)
21 #define FLOW_DIS_FIRST_FRAG	BIT(1)
22 #define FLOW_DIS_ENCAPSULATION	BIT(2)
23 
24 /**
25  * struct flow_dissector_key_basic:
26  * @thoff: Transport header offset
27  * @n_proto: Network header protocol (eg. IPv4/IPv6)
28  * @ip_proto: Transport header protocol (eg. TCP/UDP)
29  */
30 struct flow_dissector_key_basic {
31 	__be16	n_proto;
32 	u8	ip_proto;
33 	u8	padding;
34 };
35 
36 struct flow_dissector_key_tags {
37 	u32	vlan_id:12,
38 		flow_label:20;
39 };
40 
41 struct flow_dissector_key_keyid {
42 	__be32	keyid;
43 };
44 
45 /**
46  * struct flow_dissector_key_ipv4_addrs:
47  * @src: source ip address
48  * @dst: destination ip address
49  */
50 struct flow_dissector_key_ipv4_addrs {
51 	/* (src,dst) must be grouped, in the same way than in IP header */
52 	__be32 src;
53 	__be32 dst;
54 };
55 
56 /**
57  * struct flow_dissector_key_ipv6_addrs:
58  * @src: source ip address
59  * @dst: destination ip address
60  */
61 struct flow_dissector_key_ipv6_addrs {
62 	/* (src,dst) must be grouped, in the same way than in IP header */
63 	struct in6_addr src;
64 	struct in6_addr dst;
65 };
66 
67 /**
68  * struct flow_dissector_key_tipc_addrs:
69  * @srcnode: source node address
70  */
71 struct flow_dissector_key_tipc_addrs {
72 	__be32 srcnode;
73 };
74 
75 /**
76  * struct flow_dissector_key_addrs:
77  * @v4addrs: IPv4 addresses
78  * @v6addrs: IPv6 addresses
79  */
80 struct flow_dissector_key_addrs {
81 	union {
82 		struct flow_dissector_key_ipv4_addrs v4addrs;
83 		struct flow_dissector_key_ipv6_addrs v6addrs;
84 		struct flow_dissector_key_tipc_addrs tipcaddrs;
85 	};
86 };
87 
88 /**
89  * flow_dissector_key_tp_ports:
90  *	@ports: port numbers of Transport header
91  *		src: source port number
92  *		dst: destination port number
93  */
94 struct flow_dissector_key_ports {
95 	union {
96 		__be32 ports;
97 		struct {
98 			__be16 src;
99 			__be16 dst;
100 		};
101 	};
102 };
103 
104 
105 /**
106  * struct flow_dissector_key_eth_addrs:
107  * @src: source Ethernet address
108  * @dst: destination Ethernet address
109  */
110 struct flow_dissector_key_eth_addrs {
111 	/* (dst,src) must be grouped, in the same way than in ETH header */
112 	unsigned char dst[ETH_ALEN];
113 	unsigned char src[ETH_ALEN];
114 };
115 
116 enum flow_dissector_key_id {
117 	FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
118 	FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
119 	FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
120 	FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
121 	FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
122 	FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
123 	FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
124 	FLOW_DISSECTOR_KEY_VLANID, /* struct flow_dissector_key_flow_tags */
125 	FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
126 	FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
127 	FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
128 
129 	FLOW_DISSECTOR_KEY_MAX,
130 };
131 
132 #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG		BIT(0)
133 #define FLOW_DISSECTOR_F_STOP_AT_L3		BIT(1)
134 #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL	BIT(2)
135 #define FLOW_DISSECTOR_F_STOP_AT_ENCAP		BIT(3)
136 
137 struct flow_dissector_key {
138 	enum flow_dissector_key_id key_id;
139 	size_t offset; /* offset of struct flow_dissector_key_*
140 			  in target the struct */
141 };
142 
143 struct flow_dissector {
144 	unsigned int used_keys; /* each bit repesents presence of one key id */
145 	unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
146 };
147 
148 struct flow_keys {
149 	struct flow_dissector_key_control control;
150 #define FLOW_KEYS_HASH_START_FIELD basic
151 	struct flow_dissector_key_basic basic __aligned(SIPHASH_ALIGNMENT);
152 	struct flow_dissector_key_tags tags;
153 	struct flow_dissector_key_keyid keyid;
154 	struct flow_dissector_key_ports ports;
155 	struct flow_dissector_key_addrs addrs;
156 };
157 
158 #define FLOW_KEYS_HASH_OFFSET		\
159 	offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
160 
161 __be32 flow_get_u32_src(const struct flow_keys *flow);
162 __be32 flow_get_u32_dst(const struct flow_keys *flow);
163 
164 extern struct flow_dissector flow_keys_dissector;
165 extern struct flow_dissector flow_keys_buf_dissector;
166 
167 /* struct flow_keys_digest:
168  *
169  * This structure is used to hold a digest of the full flow keys. This is a
170  * larger "hash" of a flow to allow definitively matching specific flows where
171  * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
172  * that it can by used in CB of skb (see sch_choke for an example).
173  */
174 #define FLOW_KEYS_DIGEST_LEN	16
175 struct flow_keys_digest {
176 	u8	data[FLOW_KEYS_DIGEST_LEN];
177 };
178 
179 void make_flow_keys_digest(struct flow_keys_digest *digest,
180 			   const struct flow_keys *flow);
181 
flow_keys_have_l4(struct flow_keys * keys)182 static inline bool flow_keys_have_l4(struct flow_keys *keys)
183 {
184 	return (keys->ports.ports || keys->tags.flow_label);
185 }
186 
187 u32 flow_hash_from_keys(struct flow_keys *keys);
188 
189 static inline void
flow_dissector_init_keys(struct flow_dissector_key_control * key_control,struct flow_dissector_key_basic * key_basic)190 flow_dissector_init_keys(struct flow_dissector_key_control *key_control,
191 			 struct flow_dissector_key_basic *key_basic)
192 {
193 	memset(key_control, 0, sizeof(*key_control));
194 	memset(key_basic, 0, sizeof(*key_basic));
195 }
196 
197 #endif
198