xref: /linux/include/net/flow_dissector.h (revision 005438a8eef063495ac059d128eea71b58de50e5)
1 #ifndef _NET_FLOW_DISSECTOR_H
2 #define _NET_FLOW_DISSECTOR_H
3 
4 #include <linux/types.h>
5 #include <linux/skbuff.h>
6 #include <linux/in6.h>
7 #include <uapi/linux/if_ether.h>
8 
9 /**
10  * struct flow_dissector_key_control:
11  * @thoff: Transport header offset
12  */
13 struct flow_dissector_key_control {
14 	u16	thoff;
15 	u16	addr_type;
16 };
17 
18 /**
19  * 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  */
24 struct flow_dissector_key_basic {
25 	__be16	n_proto;
26 	u8	ip_proto;
27 	u8	padding;
28 };
29 
30 struct flow_dissector_key_tags {
31 	u32	vlan_id:12,
32 		flow_label:20;
33 };
34 
35 struct flow_dissector_key_keyid {
36 	__be32	keyid;
37 };
38 
39 /**
40  * struct flow_dissector_key_ipv4_addrs:
41  * @src: source ip address
42  * @dst: destination ip address
43  */
44 struct flow_dissector_key_ipv4_addrs {
45 	/* (src,dst) must be grouped, in the same way than in IP header */
46 	__be32 src;
47 	__be32 dst;
48 };
49 
50 /**
51  * struct flow_dissector_key_ipv6_addrs:
52  * @src: source ip address
53  * @dst: destination ip address
54  */
55 struct 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 /**
62  * struct flow_dissector_key_tipc_addrs:
63  * @srcnode: source node address
64  */
65 struct flow_dissector_key_tipc_addrs {
66 	__be32 srcnode;
67 };
68 
69 /**
70  * struct flow_dissector_key_addrs:
71  * @v4addrs: IPv4 addresses
72  * @v6addrs: IPv6 addresses
73  */
74 struct flow_dissector_key_addrs {
75 	union {
76 		struct flow_dissector_key_ipv4_addrs v4addrs;
77 		struct flow_dissector_key_ipv6_addrs v6addrs;
78 		struct flow_dissector_key_tipc_addrs tipcaddrs;
79 	};
80 };
81 
82 /**
83  * flow_dissector_key_tp_ports:
84  *	@ports: port numbers of Transport header
85  *		src: source port number
86  *		dst: destination port number
87  */
88 struct flow_dissector_key_ports {
89 	union {
90 		__be32 ports;
91 		struct {
92 			__be16 src;
93 			__be16 dst;
94 		};
95 	};
96 };
97 
98 
99 /**
100  * struct flow_dissector_key_eth_addrs:
101  * @src: source Ethernet address
102  * @dst: destination Ethernet address
103  */
104 struct 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 
110 enum flow_dissector_key_id {
111 	FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
112 	FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
113 	FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
114 	FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
115 	FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
116 	FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
117 	FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
118 	FLOW_DISSECTOR_KEY_VLANID, /* struct flow_dissector_key_flow_tags */
119 	FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
120 	FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
121 	FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
122 
123 	FLOW_DISSECTOR_KEY_MAX,
124 };
125 
126 struct flow_dissector_key {
127 	enum flow_dissector_key_id key_id;
128 	size_t offset; /* offset of struct flow_dissector_key_*
129 			  in target the struct */
130 };
131 
132 struct flow_dissector {
133 	unsigned int used_keys; /* each bit repesents presence of one key id */
134 	unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
135 };
136 
137 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
138 			     const struct flow_dissector_key *key,
139 			     unsigned int key_count);
140 
141 bool __skb_flow_dissect(const struct sk_buff *skb,
142 			struct flow_dissector *flow_dissector,
143 			void *target_container,
144 			void *data, __be16 proto, int nhoff, int hlen);
145 
146 static inline bool skb_flow_dissect(const struct sk_buff *skb,
147 				    struct flow_dissector *flow_dissector,
148 				    void *target_container)
149 {
150 	return __skb_flow_dissect(skb, flow_dissector, target_container,
151 				  NULL, 0, 0, 0);
152 }
153 
154 struct flow_keys {
155 	struct flow_dissector_key_control control;
156 #define FLOW_KEYS_HASH_START_FIELD basic
157 	struct flow_dissector_key_basic basic;
158 	struct flow_dissector_key_tags tags;
159 	struct flow_dissector_key_keyid keyid;
160 	struct flow_dissector_key_ports ports;
161 	struct flow_dissector_key_addrs addrs;
162 };
163 
164 #define FLOW_KEYS_HASH_OFFSET		\
165 	offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
166 
167 __be32 flow_get_u32_src(const struct flow_keys *flow);
168 __be32 flow_get_u32_dst(const struct flow_keys *flow);
169 
170 extern struct flow_dissector flow_keys_dissector;
171 extern struct flow_dissector flow_keys_buf_dissector;
172 
173 static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb,
174 					      struct flow_keys *flow)
175 {
176 	memset(flow, 0, sizeof(*flow));
177 	return __skb_flow_dissect(skb, &flow_keys_dissector, flow,
178 				  NULL, 0, 0, 0);
179 }
180 
181 static inline bool skb_flow_dissect_flow_keys_buf(struct flow_keys *flow,
182 						  void *data, __be16 proto,
183 						  int nhoff, int hlen)
184 {
185 	memset(flow, 0, sizeof(*flow));
186 	return __skb_flow_dissect(NULL, &flow_keys_buf_dissector, flow,
187 				  data, proto, nhoff, hlen);
188 }
189 
190 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
191 			    void *data, int hlen_proto);
192 
193 static inline __be32 skb_flow_get_ports(const struct sk_buff *skb,
194 					int thoff, u8 ip_proto)
195 {
196 	return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0);
197 }
198 
199 u32 flow_hash_from_keys(struct flow_keys *keys);
200 void __skb_get_hash(struct sk_buff *skb);
201 u32 skb_get_poff(const struct sk_buff *skb);
202 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
203 		   const struct flow_keys *keys, int hlen);
204 
205 /* struct flow_keys_digest:
206  *
207  * This structure is used to hold a digest of the full flow keys. This is a
208  * larger "hash" of a flow to allow definitively matching specific flows where
209  * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
210  * that it can by used in CB of skb (see sch_choke for an example).
211  */
212 #define FLOW_KEYS_DIGEST_LEN	16
213 struct flow_keys_digest {
214 	u8	data[FLOW_KEYS_DIGEST_LEN];
215 };
216 
217 void make_flow_keys_digest(struct flow_keys_digest *digest,
218 			   const struct flow_keys *flow);
219 
220 #endif
221