blob: ca9d22488cfbc3eef0ab97085aa6872043b2233d [file] [log] [blame]
Jiri Pirkofbff9492015-05-12 14:56:15 +02001#include <linux/kernel.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +00002#include <linux/skbuff.h>
Jesper Dangaard Brouerc452ed72012-01-24 16:03:33 -05003#include <linux/export.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +00004#include <linux/ip.h>
5#include <linux/ipv6.h>
6#include <linux/if_vlan.h>
7#include <net/ip.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +00008#include <net/ipv6.h>
Daniel Borkmannf77668d2013-03-19 06:39:30 +00009#include <linux/igmp.h>
10#include <linux/icmp.h>
11#include <linux/sctp.h>
12#include <linux/dccp.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +000013#include <linux/if_tunnel.h>
14#include <linux/if_pppox.h>
15#include <linux/ppp_defs.h>
Jiri Pirko06635a32015-05-12 14:56:16 +020016#include <linux/stddef.h>
Jiri Pirko67a900c2015-05-12 14:56:19 +020017#include <linux/if_ether.h>
Jiri Pirko1bd758e2015-05-12 14:56:07 +020018#include <net/flow_dissector.h>
Alexander Duyck56193d12014-09-05 19:20:26 -040019#include <scsi/fc/fc_fcoe.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +000020
Jiri Pirkofbff9492015-05-12 14:56:15 +020021static bool skb_flow_dissector_uses_key(struct flow_dissector *flow_dissector,
22 enum flow_dissector_key_id key_id)
23{
24 return flow_dissector->used_keys & (1 << key_id);
25}
26
27static void skb_flow_dissector_set_key(struct flow_dissector *flow_dissector,
28 enum flow_dissector_key_id key_id)
29{
30 flow_dissector->used_keys |= (1 << key_id);
31}
32
33static void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
34 enum flow_dissector_key_id key_id,
35 void *target_container)
36{
37 return ((char *) target_container) + flow_dissector->offset[key_id];
38}
39
40void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
41 const struct flow_dissector_key *key,
42 unsigned int key_count)
43{
44 unsigned int i;
45
46 memset(flow_dissector, 0, sizeof(*flow_dissector));
47
48 for (i = 0; i < key_count; i++, key++) {
49 /* User should make sure that every key target offset is withing
50 * boundaries of unsigned short.
51 */
52 BUG_ON(key->offset > USHRT_MAX);
53 BUG_ON(skb_flow_dissector_uses_key(flow_dissector,
54 key->key_id));
55
56 skb_flow_dissector_set_key(flow_dissector, key->key_id);
57 flow_dissector->offset[key->key_id] = key->offset;
58 }
59
Tom Herbert42aecaa2015-06-04 09:16:39 -070060 /* Ensure that the dissector always includes control and basic key.
61 * That way we are able to avoid handling lack of these in fast path.
Jiri Pirkofbff9492015-05-12 14:56:15 +020062 */
63 BUG_ON(!skb_flow_dissector_uses_key(flow_dissector,
Tom Herbert42aecaa2015-06-04 09:16:39 -070064 FLOW_DISSECTOR_KEY_CONTROL));
65 BUG_ON(!skb_flow_dissector_uses_key(flow_dissector,
Jiri Pirkofbff9492015-05-12 14:56:15 +020066 FLOW_DISSECTOR_KEY_BASIC));
67}
68EXPORT_SYMBOL(skb_flow_dissector_init);
69
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020070/**
WANG Cong6451b3f2014-08-25 17:03:46 -070071 * __skb_flow_get_ports - extract the upper layer ports and return them
72 * @skb: sk_buff to extract the ports from
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020073 * @thoff: transport header offset
74 * @ip_proto: protocol for which to get port offset
WANG Cong6451b3f2014-08-25 17:03:46 -070075 * @data: raw buffer pointer to the packet, if NULL use skb->data
76 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020077 *
78 * The function will try to retrieve the ports at offset thoff + poff where poff
79 * is the protocol port offset returned from proto_ports_offset
80 */
David S. Miller690e36e2014-08-23 12:13:41 -070081__be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
82 void *data, int hlen)
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020083{
84 int poff = proto_ports_offset(ip_proto);
85
David S. Miller690e36e2014-08-23 12:13:41 -070086 if (!data) {
87 data = skb->data;
88 hlen = skb_headlen(skb);
89 }
90
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020091 if (poff >= 0) {
92 __be32 *ports, _ports;
93
David S. Miller690e36e2014-08-23 12:13:41 -070094 ports = __skb_header_pointer(skb, thoff + poff,
95 sizeof(_ports), data, hlen, &_ports);
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020096 if (ports)
97 return *ports;
98 }
99
100 return 0;
101}
David S. Miller690e36e2014-08-23 12:13:41 -0700102EXPORT_SYMBOL(__skb_flow_get_ports);
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +0200103
WANG Cong453a9402014-08-25 17:03:47 -0700104/**
105 * __skb_flow_dissect - extract the flow_keys struct and return it
106 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
Jiri Pirko06635a32015-05-12 14:56:16 +0200107 * @flow_dissector: list of keys to dissect
108 * @target_container: target structure to put dissected values into
WANG Cong453a9402014-08-25 17:03:47 -0700109 * @data: raw buffer pointer to the packet, if NULL use skb->data
110 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
111 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
112 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
113 *
Jiri Pirko06635a32015-05-12 14:56:16 +0200114 * The function will try to retrieve individual keys into target specified
115 * by flow_dissector from either the skbuff or a raw buffer specified by the
116 * rest parameters.
117 *
118 * Caller must take care of zeroing target container memory.
WANG Cong453a9402014-08-25 17:03:47 -0700119 */
Jiri Pirko06635a32015-05-12 14:56:16 +0200120bool __skb_flow_dissect(const struct sk_buff *skb,
121 struct flow_dissector *flow_dissector,
122 void *target_container,
WANG Cong453a9402014-08-25 17:03:47 -0700123 void *data, __be16 proto, int nhoff, int hlen)
Eric Dumazet0744dd02011-11-28 05:22:18 +0000124{
Tom Herbert42aecaa2015-06-04 09:16:39 -0700125 struct flow_dissector_key_control *key_control;
Jiri Pirko06635a32015-05-12 14:56:16 +0200126 struct flow_dissector_key_basic *key_basic;
127 struct flow_dissector_key_addrs *key_addrs;
128 struct flow_dissector_key_ports *key_ports;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000129 u8 ip_proto;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000130
David S. Miller690e36e2014-08-23 12:13:41 -0700131 if (!data) {
132 data = skb->data;
WANG Cong453a9402014-08-25 17:03:47 -0700133 proto = skb->protocol;
134 nhoff = skb_network_offset(skb);
David S. Miller690e36e2014-08-23 12:13:41 -0700135 hlen = skb_headlen(skb);
136 }
137
Tom Herbert42aecaa2015-06-04 09:16:39 -0700138 /* It is ensured by skb_flow_dissector_init() that control key will
139 * be always present.
140 */
141 key_control = skb_flow_dissector_target(flow_dissector,
142 FLOW_DISSECTOR_KEY_CONTROL,
143 target_container);
144
Jiri Pirko06635a32015-05-12 14:56:16 +0200145 /* It is ensured by skb_flow_dissector_init() that basic key will
146 * be always present.
147 */
148 key_basic = skb_flow_dissector_target(flow_dissector,
149 FLOW_DISSECTOR_KEY_BASIC,
150 target_container);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000151
Jiri Pirko67a900c2015-05-12 14:56:19 +0200152 if (skb_flow_dissector_uses_key(flow_dissector,
153 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
154 struct ethhdr *eth = eth_hdr(skb);
155 struct flow_dissector_key_eth_addrs *key_eth_addrs;
156
157 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
158 FLOW_DISSECTOR_KEY_ETH_ADDRS,
159 target_container);
160 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
161 }
162
Eric Dumazet0744dd02011-11-28 05:22:18 +0000163again:
164 switch (proto) {
Joe Perches2b8837a2014-03-12 10:04:17 -0700165 case htons(ETH_P_IP): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000166 const struct iphdr *iph;
167 struct iphdr _iph;
168ip:
David S. Miller690e36e2014-08-23 12:13:41 -0700169 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
Jason Wang6f092342013-11-01 15:01:10 +0800170 if (!iph || iph->ihl < 5)
Eric Dumazet0744dd02011-11-28 05:22:18 +0000171 return false;
Eric Dumazet3797d3e2013-11-07 08:37:28 -0800172 nhoff += iph->ihl * 4;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000173
Eric Dumazet3797d3e2013-11-07 08:37:28 -0800174 ip_proto = iph->protocol;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000175 if (ip_is_fragment(iph))
176 ip_proto = 0;
Eric Dumazet3797d3e2013-11-07 08:37:28 -0800177
Jiri Pirko06635a32015-05-12 14:56:16 +0200178 if (!skb_flow_dissector_uses_key(flow_dissector,
179 FLOW_DISSECTOR_KEY_IPV4_ADDRS))
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700180 break;
Tom Herbertc3f83242015-06-04 09:16:40 -0700181
Jiri Pirko06635a32015-05-12 14:56:16 +0200182 key_addrs = skb_flow_dissector_target(flow_dissector,
Tom Herbertc3f83242015-06-04 09:16:40 -0700183 FLOW_DISSECTOR_KEY_IPV4_ADDRS, target_container);
184 memcpy(&key_addrs->v4addrs, &iph->saddr,
185 sizeof(key_addrs->v4addrs));
186 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000187 break;
188 }
Joe Perches2b8837a2014-03-12 10:04:17 -0700189 case htons(ETH_P_IPV6): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000190 const struct ipv6hdr *iph;
191 struct ipv6hdr _iph;
Tom Herbert19469a82014-07-01 21:33:01 -0700192 __be32 flow_label;
193
Eric Dumazet0744dd02011-11-28 05:22:18 +0000194ipv6:
David S. Miller690e36e2014-08-23 12:13:41 -0700195 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000196 if (!iph)
197 return false;
198
199 ip_proto = iph->nexthdr;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000200 nhoff += sizeof(struct ipv6hdr);
Tom Herbert19469a82014-07-01 21:33:01 -0700201
Jiri Pirkob9249332015-05-12 14:56:18 +0200202 if (skb_flow_dissector_uses_key(flow_dissector,
203 FLOW_DISSECTOR_KEY_IPV6_HASH_ADDRS)) {
204 key_addrs = skb_flow_dissector_target(flow_dissector,
205 FLOW_DISSECTOR_KEY_IPV6_HASH_ADDRS,
206 target_container);
Alexander Duyck56193d12014-09-05 19:20:26 -0400207
Tom Herbertc3f83242015-06-04 09:16:40 -0700208 key_addrs->v4addrs.src =
209 (__force __be32)ipv6_addr_hash(&iph->saddr);
210 key_addrs->v4addrs.dst =
211 (__force __be32)ipv6_addr_hash(&iph->daddr);
212 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Jiri Pirkob9249332015-05-12 14:56:18 +0200213 goto flow_label;
214 }
215 if (skb_flow_dissector_uses_key(flow_dissector,
216 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
217 struct flow_dissector_key_ipv6_addrs *key_ipv6_addrs;
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700218
Jiri Pirkob9249332015-05-12 14:56:18 +0200219 key_ipv6_addrs = skb_flow_dissector_target(flow_dissector,
220 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
221 target_container);
222
223 memcpy(key_ipv6_addrs, &iph->saddr, sizeof(*key_ipv6_addrs));
Tom Herbertc3f83242015-06-04 09:16:40 -0700224 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Jiri Pirkob9249332015-05-12 14:56:18 +0200225 goto flow_label;
226 }
227 break;
228flow_label:
Tom Herbert19469a82014-07-01 21:33:01 -0700229 flow_label = ip6_flowlabel(iph);
230 if (flow_label) {
231 /* Awesome, IPv6 packet has a flow label so we can
232 * use that to represent the ports without any
233 * further dissection.
234 */
Jiri Pirko06635a32015-05-12 14:56:16 +0200235
236 key_basic->n_proto = proto;
237 key_basic->ip_proto = ip_proto;
Tom Herbert42aecaa2015-06-04 09:16:39 -0700238 key_control->thoff = (u16)nhoff;
Jiri Pirko06635a32015-05-12 14:56:16 +0200239
Jiri Pirko12c227e2015-05-22 11:05:58 +0200240 if (skb_flow_dissector_uses_key(flow_dissector,
241 FLOW_DISSECTOR_KEY_PORTS)) {
242 key_ports = skb_flow_dissector_target(flow_dissector,
243 FLOW_DISSECTOR_KEY_PORTS,
244 target_container);
245 key_ports->ports = flow_label;
246 }
Tom Herbert19469a82014-07-01 21:33:01 -0700247
248 return true;
249 }
250
Eric Dumazet0744dd02011-11-28 05:22:18 +0000251 break;
252 }
Joe Perches2b8837a2014-03-12 10:04:17 -0700253 case htons(ETH_P_8021AD):
254 case htons(ETH_P_8021Q): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000255 const struct vlan_hdr *vlan;
256 struct vlan_hdr _vlan;
257
David S. Miller690e36e2014-08-23 12:13:41 -0700258 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000259 if (!vlan)
260 return false;
261
262 proto = vlan->h_vlan_encapsulated_proto;
263 nhoff += sizeof(*vlan);
264 goto again;
265 }
Joe Perches2b8837a2014-03-12 10:04:17 -0700266 case htons(ETH_P_PPP_SES): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000267 struct {
268 struct pppoe_hdr hdr;
269 __be16 proto;
270 } *hdr, _hdr;
David S. Miller690e36e2014-08-23 12:13:41 -0700271 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000272 if (!hdr)
273 return false;
274 proto = hdr->proto;
275 nhoff += PPPOE_SES_HLEN;
276 switch (proto) {
Joe Perches2b8837a2014-03-12 10:04:17 -0700277 case htons(PPP_IP):
Eric Dumazet0744dd02011-11-28 05:22:18 +0000278 goto ip;
Joe Perches2b8837a2014-03-12 10:04:17 -0700279 case htons(PPP_IPV6):
Eric Dumazet0744dd02011-11-28 05:22:18 +0000280 goto ipv6;
281 default:
282 return false;
283 }
284 }
Erik Hugne08bfc9c2015-01-22 17:10:32 +0100285 case htons(ETH_P_TIPC): {
286 struct {
287 __be32 pre[3];
288 __be32 srcnode;
289 } *hdr, _hdr;
290 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
291 if (!hdr)
292 return false;
Jiri Pirko06635a32015-05-12 14:56:16 +0200293 key_basic->n_proto = proto;
Tom Herbert42aecaa2015-06-04 09:16:39 -0700294 key_control->thoff = (u16)nhoff;
Jiri Pirko06635a32015-05-12 14:56:16 +0200295
296 if (skb_flow_dissector_uses_key(flow_dissector,
297 FLOW_DISSECTOR_KEY_IPV6_HASH_ADDRS)) {
Jiri Pirko06635a32015-05-12 14:56:16 +0200298 key_addrs = skb_flow_dissector_target(flow_dissector,
299 FLOW_DISSECTOR_KEY_IPV6_HASH_ADDRS,
300 target_container);
Tom Herbertc3f83242015-06-04 09:16:40 -0700301 key_addrs->v4addrs.src = hdr->srcnode;
302 key_addrs->v4addrs.dst = 0;
303 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Jiri Pirko06635a32015-05-12 14:56:16 +0200304 }
Erik Hugne08bfc9c2015-01-22 17:10:32 +0100305 return true;
306 }
Alexander Duyck56193d12014-09-05 19:20:26 -0400307 case htons(ETH_P_FCOE):
Tom Herbert42aecaa2015-06-04 09:16:39 -0700308 key_control->thoff = (u16)(nhoff + FCOE_HEADER_LEN);
Alexander Duyck56193d12014-09-05 19:20:26 -0400309 /* fall through */
Eric Dumazet0744dd02011-11-28 05:22:18 +0000310 default:
311 return false;
312 }
313
314 switch (ip_proto) {
315 case IPPROTO_GRE: {
316 struct gre_hdr {
317 __be16 flags;
318 __be16 proto;
319 } *hdr, _hdr;
320
David S. Miller690e36e2014-08-23 12:13:41 -0700321 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000322 if (!hdr)
323 return false;
324 /*
325 * Only look inside GRE if version zero and no
326 * routing
327 */
Tom Herbertce3b5352015-06-04 09:16:36 -0700328 if (hdr->flags & (GRE_VERSION | GRE_ROUTING))
329 break;
Michael Daltone1733de2013-03-11 06:52:28 +0000330
Tom Herbertce3b5352015-06-04 09:16:36 -0700331 proto = hdr->proto;
332 nhoff += 4;
333 if (hdr->flags & GRE_CSUM)
334 nhoff += 4;
335 if (hdr->flags & GRE_KEY)
336 nhoff += 4;
337 if (hdr->flags & GRE_SEQ)
338 nhoff += 4;
339 if (proto == htons(ETH_P_TEB)) {
340 const struct ethhdr *eth;
341 struct ethhdr _eth;
342
343 eth = __skb_header_pointer(skb, nhoff,
344 sizeof(_eth),
345 data, hlen, &_eth);
346 if (!eth)
347 return false;
348 proto = eth->h_proto;
349 nhoff += sizeof(*eth);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000350 }
Tom Herbertce3b5352015-06-04 09:16:36 -0700351 goto again;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000352 }
353 case IPPROTO_IPIP:
Tom Herbertfca41892013-07-29 11:07:36 -0700354 proto = htons(ETH_P_IP);
355 goto ip;
Tom Herbertb438f942013-07-29 11:07:42 -0700356 case IPPROTO_IPV6:
357 proto = htons(ETH_P_IPV6);
358 goto ipv6;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000359 default:
360 break;
361 }
362
Jiri Pirko06635a32015-05-12 14:56:16 +0200363 key_basic->n_proto = proto;
364 key_basic->ip_proto = ip_proto;
Tom Herbert42aecaa2015-06-04 09:16:39 -0700365 key_control->thoff = (u16)nhoff;
Daniel Borkmann8ed78162013-03-19 06:39:29 +0000366
Jiri Pirko06635a32015-05-12 14:56:16 +0200367 if (skb_flow_dissector_uses_key(flow_dissector,
368 FLOW_DISSECTOR_KEY_PORTS)) {
369 key_ports = skb_flow_dissector_target(flow_dissector,
370 FLOW_DISSECTOR_KEY_PORTS,
371 target_container);
372 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
373 data, hlen);
374 }
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700375
Eric Dumazet0744dd02011-11-28 05:22:18 +0000376 return true;
377}
David S. Miller690e36e2014-08-23 12:13:41 -0700378EXPORT_SYMBOL(__skb_flow_dissect);
Cong Wang441d9d32013-01-21 00:39:24 +0000379
380static u32 hashrnd __read_mostly;
Hannes Frederic Sowa66415cf2013-10-23 20:06:00 +0200381static __always_inline void __flow_hash_secret_init(void)
382{
383 net_get_random_once(&hashrnd, sizeof(hashrnd));
384}
385
Tom Herbert42aecaa2015-06-04 09:16:39 -0700386static __always_inline u32 __flow_hash_words(u32 *words, u32 length, u32 keyval)
Hannes Frederic Sowa66415cf2013-10-23 20:06:00 +0200387{
Tom Herbert42aecaa2015-06-04 09:16:39 -0700388 return jhash2(words, length, keyval);
389}
390
391static inline void *flow_keys_hash_start(struct flow_keys *flow)
392{
393 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
394 return (void *)flow + FLOW_KEYS_HASH_OFFSET;
395}
396
397static inline size_t flow_keys_hash_length(struct flow_keys *flow)
398{
Tom Herbertc3f83242015-06-04 09:16:40 -0700399 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
Tom Herbert42aecaa2015-06-04 09:16:39 -0700400 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
Tom Herbertc3f83242015-06-04 09:16:40 -0700401 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
402 sizeof(*flow) - sizeof(flow->addrs));
403
404 switch (flow->control.addr_type) {
405 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
406 diff -= sizeof(flow->addrs.v4addrs);
407 break;
408 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
409 diff -= sizeof(flow->addrs.v6addrs);
410 break;
411 }
412 return (sizeof(*flow) - diff) / sizeof(u32);
413}
414
415__be32 flow_get_u32_src(const struct flow_keys *flow)
416{
417 switch (flow->control.addr_type) {
418 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
419 return flow->addrs.v4addrs.src;
420 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
421 return (__force __be32)ipv6_addr_hash(
422 &flow->addrs.v6addrs.src);
423 default:
424 return 0;
425 }
426}
427EXPORT_SYMBOL(flow_get_u32_src);
428
429__be32 flow_get_u32_dst(const struct flow_keys *flow)
430{
431 switch (flow->control.addr_type) {
432 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
433 return flow->addrs.v4addrs.dst;
434 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
435 return (__force __be32)ipv6_addr_hash(
436 &flow->addrs.v6addrs.dst);
437 default:
438 return 0;
439 }
440}
441EXPORT_SYMBOL(flow_get_u32_dst);
442
443static inline void __flow_hash_consistentify(struct flow_keys *keys)
444{
445 int addr_diff, i;
446
447 switch (keys->control.addr_type) {
448 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
449 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
450 (__force u32)keys->addrs.v4addrs.src;
451 if ((addr_diff < 0) ||
452 (addr_diff == 0 &&
453 ((__force u16)keys->ports.dst <
454 (__force u16)keys->ports.src))) {
455 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
456 swap(keys->ports.src, keys->ports.dst);
457 }
458 break;
459 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
460 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
461 &keys->addrs.v6addrs.src,
462 sizeof(keys->addrs.v6addrs.dst));
463 if ((addr_diff < 0) ||
464 (addr_diff == 0 &&
465 ((__force u16)keys->ports.dst <
466 (__force u16)keys->ports.src))) {
467 for (i = 0; i < 4; i++)
468 swap(keys->addrs.v6addrs.src.s6_addr32[i],
469 keys->addrs.v6addrs.dst.s6_addr32[i]);
470 swap(keys->ports.src, keys->ports.dst);
471 }
472 break;
473 }
Hannes Frederic Sowa66415cf2013-10-23 20:06:00 +0200474}
475
Tom Herbert50fb7992015-05-01 11:30:12 -0700476static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
Tom Herbert5ed20a62014-07-01 21:32:05 -0700477{
478 u32 hash;
479
Tom Herbertc3f83242015-06-04 09:16:40 -0700480 __flow_hash_consistentify(keys);
Tom Herbert5ed20a62014-07-01 21:32:05 -0700481
Tom Herbert42aecaa2015-06-04 09:16:39 -0700482 hash = __flow_hash_words((u32 *)flow_keys_hash_start(keys),
483 flow_keys_hash_length(keys), keyval);
Tom Herbert5ed20a62014-07-01 21:32:05 -0700484 if (!hash)
485 hash = 1;
486
487 return hash;
488}
489
490u32 flow_hash_from_keys(struct flow_keys *keys)
491{
Tom Herbert50fb7992015-05-01 11:30:12 -0700492 __flow_hash_secret_init();
493 return __flow_hash_from_keys(keys, hashrnd);
Tom Herbert5ed20a62014-07-01 21:32:05 -0700494}
495EXPORT_SYMBOL(flow_hash_from_keys);
496
Tom Herbert50fb7992015-05-01 11:30:12 -0700497static inline u32 ___skb_get_hash(const struct sk_buff *skb,
498 struct flow_keys *keys, u32 keyval)
499{
Jiri Pirko06635a32015-05-12 14:56:16 +0200500 if (!skb_flow_dissect_flow_keys(skb, keys))
Tom Herbert50fb7992015-05-01 11:30:12 -0700501 return 0;
502
503 return __flow_hash_from_keys(keys, keyval);
504}
505
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700506struct _flow_keys_digest_data {
507 __be16 n_proto;
508 u8 ip_proto;
509 u8 padding;
510 __be32 ports;
511 __be32 src;
512 __be32 dst;
513};
514
515void make_flow_keys_digest(struct flow_keys_digest *digest,
516 const struct flow_keys *flow)
517{
518 struct _flow_keys_digest_data *data =
519 (struct _flow_keys_digest_data *)digest;
520
521 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
522
523 memset(digest, 0, sizeof(*digest));
524
Jiri Pirko06635a32015-05-12 14:56:16 +0200525 data->n_proto = flow->basic.n_proto;
526 data->ip_proto = flow->basic.ip_proto;
527 data->ports = flow->ports.ports;
Tom Herbertc3f83242015-06-04 09:16:40 -0700528 data->src = flow->addrs.v4addrs.src;
529 data->dst = flow->addrs.v4addrs.dst;
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700530}
531EXPORT_SYMBOL(make_flow_keys_digest);
532
Jiri Pirkod4fd3272015-05-12 14:56:10 +0200533/**
534 * __skb_get_hash: calculate a flow hash
535 * @skb: sk_buff to calculate flow hash from
536 *
537 * This function calculates a flow hash based on src/dst addresses
Tom Herbert61b905d2014-03-24 15:34:47 -0700538 * and src/dst port numbers. Sets hash in skb to non-zero hash value
539 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
Cong Wang441d9d32013-01-21 00:39:24 +0000540 * if hash is a canonical 4-tuple hash over transport ports.
541 */
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800542void __skb_get_hash(struct sk_buff *skb)
Cong Wang441d9d32013-01-21 00:39:24 +0000543{
544 struct flow_keys keys;
Tom Herbert50fb7992015-05-01 11:30:12 -0700545 u32 hash;
Cong Wang441d9d32013-01-21 00:39:24 +0000546
Tom Herbert50fb7992015-05-01 11:30:12 -0700547 __flow_hash_secret_init();
548
549 hash = ___skb_get_hash(skb, &keys, hashrnd);
550 if (!hash)
Cong Wang441d9d32013-01-21 00:39:24 +0000551 return;
Jiri Pirko06635a32015-05-12 14:56:16 +0200552 if (keys.ports.ports)
Tom Herbert61b905d2014-03-24 15:34:47 -0700553 skb->l4_hash = 1;
Tom Herberta3b18dd2014-07-01 21:33:17 -0700554 skb->sw_hash = 1;
Tom Herbert50fb7992015-05-01 11:30:12 -0700555 skb->hash = hash;
Cong Wang441d9d32013-01-21 00:39:24 +0000556}
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800557EXPORT_SYMBOL(__skb_get_hash);
Cong Wang441d9d32013-01-21 00:39:24 +0000558
Tom Herbert50fb7992015-05-01 11:30:12 -0700559__u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
560{
561 struct flow_keys keys;
562
563 return ___skb_get_hash(skb, &keys, perturb);
564}
565EXPORT_SYMBOL(skb_get_hash_perturb);
566
Alexander Duyck56193d12014-09-05 19:20:26 -0400567u32 __skb_get_poff(const struct sk_buff *skb, void *data,
568 const struct flow_keys *keys, int hlen)
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000569{
Tom Herbert42aecaa2015-06-04 09:16:39 -0700570 u32 poff = keys->control.thoff;
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000571
Jiri Pirko06635a32015-05-12 14:56:16 +0200572 switch (keys->basic.ip_proto) {
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000573 case IPPROTO_TCP: {
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700574 /* access doff as u8 to avoid unaligned access */
575 const u8 *doff;
576 u8 _doff;
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000577
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700578 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
579 data, hlen, &_doff);
580 if (!doff)
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000581 return poff;
582
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700583 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000584 break;
585 }
586 case IPPROTO_UDP:
587 case IPPROTO_UDPLITE:
588 poff += sizeof(struct udphdr);
589 break;
590 /* For the rest, we do not really care about header
591 * extensions at this point for now.
592 */
593 case IPPROTO_ICMP:
594 poff += sizeof(struct icmphdr);
595 break;
596 case IPPROTO_ICMPV6:
597 poff += sizeof(struct icmp6hdr);
598 break;
599 case IPPROTO_IGMP:
600 poff += sizeof(struct igmphdr);
601 break;
602 case IPPROTO_DCCP:
603 poff += sizeof(struct dccp_hdr);
604 break;
605 case IPPROTO_SCTP:
606 poff += sizeof(struct sctphdr);
607 break;
608 }
609
610 return poff;
611}
612
Jiri Pirko0db89b82015-05-12 14:56:14 +0200613/**
614 * skb_get_poff - get the offset to the payload
615 * @skb: sk_buff to get the payload offset from
616 *
617 * The function will get the offset to the payload as far as it could
618 * be dissected. The main user is currently BPF, so that we can dynamically
Alexander Duyck56193d12014-09-05 19:20:26 -0400619 * truncate packets without needing to push actual payload to the user
620 * space and can analyze headers only, instead.
621 */
622u32 skb_get_poff(const struct sk_buff *skb)
623{
624 struct flow_keys keys;
625
Jiri Pirko06635a32015-05-12 14:56:16 +0200626 if (!skb_flow_dissect_flow_keys(skb, &keys))
Alexander Duyck56193d12014-09-05 19:20:26 -0400627 return 0;
628
629 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
630}
Jiri Pirko06635a32015-05-12 14:56:16 +0200631
632static const struct flow_dissector_key flow_keys_dissector_keys[] = {
633 {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700634 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
635 .offset = offsetof(struct flow_keys, control),
636 },
637 {
Jiri Pirko06635a32015-05-12 14:56:16 +0200638 .key_id = FLOW_DISSECTOR_KEY_BASIC,
639 .offset = offsetof(struct flow_keys, basic),
640 },
641 {
642 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
Tom Herbertc3f83242015-06-04 09:16:40 -0700643 .offset = offsetof(struct flow_keys, addrs.v4addrs),
644 },
645 {
646 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
647 .offset = offsetof(struct flow_keys, addrs.v6addrs),
Jiri Pirko06635a32015-05-12 14:56:16 +0200648 },
649 {
650 .key_id = FLOW_DISSECTOR_KEY_IPV6_HASH_ADDRS,
Tom Herbertc3f83242015-06-04 09:16:40 -0700651 .offset = offsetof(struct flow_keys, addrs.v4addrs),
Jiri Pirko06635a32015-05-12 14:56:16 +0200652 },
653 {
654 .key_id = FLOW_DISSECTOR_KEY_PORTS,
655 .offset = offsetof(struct flow_keys, ports),
656 },
657};
658
659static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
660 {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700661 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
662 .offset = offsetof(struct flow_keys, control),
663 },
664 {
Jiri Pirko06635a32015-05-12 14:56:16 +0200665 .key_id = FLOW_DISSECTOR_KEY_BASIC,
666 .offset = offsetof(struct flow_keys, basic),
667 },
668};
669
670struct flow_dissector flow_keys_dissector __read_mostly;
671EXPORT_SYMBOL(flow_keys_dissector);
672
673struct flow_dissector flow_keys_buf_dissector __read_mostly;
674
675static int __init init_default_flow_dissectors(void)
676{
677 skb_flow_dissector_init(&flow_keys_dissector,
678 flow_keys_dissector_keys,
679 ARRAY_SIZE(flow_keys_dissector_keys));
680 skb_flow_dissector_init(&flow_keys_buf_dissector,
681 flow_keys_buf_dissector_keys,
682 ARRAY_SIZE(flow_keys_buf_dissector_keys));
683 return 0;
684}
685
686late_initcall_sync(init_default_flow_dissectors);