Merge branch 'qed-link-fixes'
[linux-2.6-block.git] / net / core / flow_dissector.c
CommitLineData
fbff949e 1#include <linux/kernel.h>
0744dd00 2#include <linux/skbuff.h>
c452ed70 3#include <linux/export.h>
0744dd00
ED
4#include <linux/ip.h>
5#include <linux/ipv6.h>
6#include <linux/if_vlan.h>
7#include <net/ip.h>
ddbe5032 8#include <net/ipv6.h>
ab10dccb
GF
9#include <net/gre.h>
10#include <net/pptp.h>
f77668dc
DB
11#include <linux/igmp.h>
12#include <linux/icmp.h>
13#include <linux/sctp.h>
14#include <linux/dccp.h>
0744dd00
ED
15#include <linux/if_tunnel.h>
16#include <linux/if_pppox.h>
17#include <linux/ppp_defs.h>
06635a35 18#include <linux/stddef.h>
67a900cc 19#include <linux/if_ether.h>
b3baa0fb 20#include <linux/mpls.h>
1bd758eb 21#include <net/flow_dissector.h>
56193d1b 22#include <scsi/fc/fc_fcoe.h>
0744dd00 23
20a17bf6
DM
24static void dissector_set_key(struct flow_dissector *flow_dissector,
25 enum flow_dissector_key_id key_id)
fbff949e
JP
26{
27 flow_dissector->used_keys |= (1 << key_id);
28}
29
fbff949e
JP
30void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
31 const struct flow_dissector_key *key,
32 unsigned int key_count)
33{
34 unsigned int i;
35
36 memset(flow_dissector, 0, sizeof(*flow_dissector));
37
38 for (i = 0; i < key_count; i++, key++) {
39 /* User should make sure that every key target offset is withing
40 * boundaries of unsigned short.
41 */
42 BUG_ON(key->offset > USHRT_MAX);
20a17bf6
DM
43 BUG_ON(dissector_uses_key(flow_dissector,
44 key->key_id));
fbff949e 45
20a17bf6 46 dissector_set_key(flow_dissector, key->key_id);
fbff949e
JP
47 flow_dissector->offset[key->key_id] = key->offset;
48 }
49
42aecaa9
TH
50 /* Ensure that the dissector always includes control and basic key.
51 * That way we are able to avoid handling lack of these in fast path.
fbff949e 52 */
20a17bf6
DM
53 BUG_ON(!dissector_uses_key(flow_dissector,
54 FLOW_DISSECTOR_KEY_CONTROL));
55 BUG_ON(!dissector_uses_key(flow_dissector,
56 FLOW_DISSECTOR_KEY_BASIC));
fbff949e
JP
57}
58EXPORT_SYMBOL(skb_flow_dissector_init);
59
357afe9c 60/**
6451b3f5
WC
61 * __skb_flow_get_ports - extract the upper layer ports and return them
62 * @skb: sk_buff to extract the ports from
357afe9c
NA
63 * @thoff: transport header offset
64 * @ip_proto: protocol for which to get port offset
6451b3f5
WC
65 * @data: raw buffer pointer to the packet, if NULL use skb->data
66 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
357afe9c
NA
67 *
68 * The function will try to retrieve the ports at offset thoff + poff where poff
69 * is the protocol port offset returned from proto_ports_offset
70 */
690e36e7
DM
71__be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
72 void *data, int hlen)
357afe9c
NA
73{
74 int poff = proto_ports_offset(ip_proto);
75
690e36e7
DM
76 if (!data) {
77 data = skb->data;
78 hlen = skb_headlen(skb);
79 }
80
357afe9c
NA
81 if (poff >= 0) {
82 __be32 *ports, _ports;
83
690e36e7
DM
84 ports = __skb_header_pointer(skb, thoff + poff,
85 sizeof(_ports), data, hlen, &_ports);
357afe9c
NA
86 if (ports)
87 return *ports;
88 }
89
90 return 0;
91}
690e36e7 92EXPORT_SYMBOL(__skb_flow_get_ports);
357afe9c 93
453a940e
WC
94/**
95 * __skb_flow_dissect - extract the flow_keys struct and return it
96 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
06635a35
JP
97 * @flow_dissector: list of keys to dissect
98 * @target_container: target structure to put dissected values into
453a940e
WC
99 * @data: raw buffer pointer to the packet, if NULL use skb->data
100 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
101 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
102 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
103 *
06635a35
JP
104 * The function will try to retrieve individual keys into target specified
105 * by flow_dissector from either the skbuff or a raw buffer specified by the
106 * rest parameters.
107 *
108 * Caller must take care of zeroing target container memory.
453a940e 109 */
06635a35
JP
110bool __skb_flow_dissect(const struct sk_buff *skb,
111 struct flow_dissector *flow_dissector,
112 void *target_container,
cd79a238
TH
113 void *data, __be16 proto, int nhoff, int hlen,
114 unsigned int flags)
0744dd00 115{
42aecaa9 116 struct flow_dissector_key_control *key_control;
06635a35
JP
117 struct flow_dissector_key_basic *key_basic;
118 struct flow_dissector_key_addrs *key_addrs;
119 struct flow_dissector_key_ports *key_ports;
d34af823 120 struct flow_dissector_key_tags *key_tags;
1fdd512c 121 struct flow_dissector_key_keyid *key_keyid;
8e690ffd 122 u8 ip_proto = 0;
a6e544b0 123 bool ret = false;
0744dd00 124
690e36e7
DM
125 if (!data) {
126 data = skb->data;
453a940e
WC
127 proto = skb->protocol;
128 nhoff = skb_network_offset(skb);
690e36e7
DM
129 hlen = skb_headlen(skb);
130 }
131
42aecaa9
TH
132 /* It is ensured by skb_flow_dissector_init() that control key will
133 * be always present.
134 */
135 key_control = skb_flow_dissector_target(flow_dissector,
136 FLOW_DISSECTOR_KEY_CONTROL,
137 target_container);
138
06635a35
JP
139 /* It is ensured by skb_flow_dissector_init() that basic key will
140 * be always present.
141 */
142 key_basic = skb_flow_dissector_target(flow_dissector,
143 FLOW_DISSECTOR_KEY_BASIC,
144 target_container);
0744dd00 145
20a17bf6
DM
146 if (dissector_uses_key(flow_dissector,
147 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
67a900cc
JP
148 struct ethhdr *eth = eth_hdr(skb);
149 struct flow_dissector_key_eth_addrs *key_eth_addrs;
150
151 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
152 FLOW_DISSECTOR_KEY_ETH_ADDRS,
153 target_container);
154 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
155 }
156
0744dd00
ED
157again:
158 switch (proto) {
2b8837ae 159 case htons(ETH_P_IP): {
0744dd00
ED
160 const struct iphdr *iph;
161 struct iphdr _iph;
162ip:
690e36e7 163 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
6f092343 164 if (!iph || iph->ihl < 5)
a6e544b0 165 goto out_bad;
3797d3e8 166 nhoff += iph->ihl * 4;
0744dd00 167
3797d3e8 168 ip_proto = iph->protocol;
3797d3e8 169
918c023f
AD
170 if (dissector_uses_key(flow_dissector,
171 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
172 key_addrs = skb_flow_dissector_target(flow_dissector,
173 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
174 target_container);
175
176 memcpy(&key_addrs->v4addrs, &iph->saddr,
177 sizeof(key_addrs->v4addrs));
178 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
179 }
807e165d
TH
180
181 if (ip_is_fragment(iph)) {
4b36993d 182 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
807e165d
TH
183
184 if (iph->frag_off & htons(IP_OFFSET)) {
185 goto out_good;
186 } else {
4b36993d 187 key_control->flags |= FLOW_DIS_FIRST_FRAG;
807e165d
TH
188 if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
189 goto out_good;
190 }
191 }
192
8306b688
TH
193 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
194 goto out_good;
195
0744dd00
ED
196 break;
197 }
2b8837ae 198 case htons(ETH_P_IPV6): {
0744dd00
ED
199 const struct ipv6hdr *iph;
200 struct ipv6hdr _iph;
19469a87 201
0744dd00 202ipv6:
690e36e7 203 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
0744dd00 204 if (!iph)
a6e544b0 205 goto out_bad;
0744dd00
ED
206
207 ip_proto = iph->nexthdr;
0744dd00 208 nhoff += sizeof(struct ipv6hdr);
19469a87 209
20a17bf6
DM
210 if (dissector_uses_key(flow_dissector,
211 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
b3c3106c
AD
212 key_addrs = skb_flow_dissector_target(flow_dissector,
213 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
214 target_container);
5af7fb6e 215
b3c3106c
AD
216 memcpy(&key_addrs->v6addrs, &iph->saddr,
217 sizeof(key_addrs->v6addrs));
c3f83241 218 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
b924933c 219 }
87ee9e52 220
461547f3
AD
221 if ((dissector_uses_key(flow_dissector,
222 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
223 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
224 ip6_flowlabel(iph)) {
225 __be32 flow_label = ip6_flowlabel(iph);
226
20a17bf6
DM
227 if (dissector_uses_key(flow_dissector,
228 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
87ee9e52
TH
229 key_tags = skb_flow_dissector_target(flow_dissector,
230 FLOW_DISSECTOR_KEY_FLOW_LABEL,
231 target_container);
232 key_tags->flow_label = ntohl(flow_label);
12c227ec 233 }
872b1abb
TH
234 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
235 goto out_good;
19469a87
TH
236 }
237
8306b688
TH
238 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
239 goto out_good;
240
0744dd00
ED
241 break;
242 }
2b8837ae
JP
243 case htons(ETH_P_8021AD):
244 case htons(ETH_P_8021Q): {
0744dd00
ED
245 const struct vlan_hdr *vlan;
246 struct vlan_hdr _vlan;
247
690e36e7 248 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan);
0744dd00 249 if (!vlan)
a6e544b0 250 goto out_bad;
0744dd00 251
20a17bf6
DM
252 if (dissector_uses_key(flow_dissector,
253 FLOW_DISSECTOR_KEY_VLANID)) {
d34af823
TH
254 key_tags = skb_flow_dissector_target(flow_dissector,
255 FLOW_DISSECTOR_KEY_VLANID,
256 target_container);
257
258 key_tags->vlan_id = skb_vlan_tag_get_id(skb);
259 }
260
0744dd00
ED
261 proto = vlan->h_vlan_encapsulated_proto;
262 nhoff += sizeof(*vlan);
263 goto again;
264 }
2b8837ae 265 case htons(ETH_P_PPP_SES): {
0744dd00
ED
266 struct {
267 struct pppoe_hdr hdr;
268 __be16 proto;
269 } *hdr, _hdr;
690e36e7 270 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
0744dd00 271 if (!hdr)
a6e544b0 272 goto out_bad;
0744dd00
ED
273 proto = hdr->proto;
274 nhoff += PPPOE_SES_HLEN;
275 switch (proto) {
2b8837ae 276 case htons(PPP_IP):
0744dd00 277 goto ip;
2b8837ae 278 case htons(PPP_IPV6):
0744dd00
ED
279 goto ipv6;
280 default:
a6e544b0 281 goto out_bad;
0744dd00
ED
282 }
283 }
08bfc9cb
EH
284 case htons(ETH_P_TIPC): {
285 struct {
286 __be32 pre[3];
287 __be32 srcnode;
288 } *hdr, _hdr;
289 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
290 if (!hdr)
a6e544b0 291 goto out_bad;
06635a35 292
20a17bf6
DM
293 if (dissector_uses_key(flow_dissector,
294 FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
06635a35 295 key_addrs = skb_flow_dissector_target(flow_dissector,
9f249089 296 FLOW_DISSECTOR_KEY_TIPC_ADDRS,
06635a35 297 target_container);
9f249089
TH
298 key_addrs->tipcaddrs.srcnode = hdr->srcnode;
299 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
06635a35 300 }
a6e544b0 301 goto out_good;
08bfc9cb 302 }
b3baa0fb
TH
303
304 case htons(ETH_P_MPLS_UC):
305 case htons(ETH_P_MPLS_MC): {
306 struct mpls_label *hdr, _hdr[2];
307mpls:
308 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
309 hlen, &_hdr);
310 if (!hdr)
a6e544b0 311 goto out_bad;
b3baa0fb 312
611d23c5
TH
313 if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >>
314 MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) {
20a17bf6
DM
315 if (dissector_uses_key(flow_dissector,
316 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
b3baa0fb
TH
317 key_keyid = skb_flow_dissector_target(flow_dissector,
318 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
319 target_container);
320 key_keyid->keyid = hdr[1].entry &
321 htonl(MPLS_LS_LABEL_MASK);
322 }
323
a6e544b0 324 goto out_good;
b3baa0fb
TH
325 }
326
a6e544b0 327 goto out_good;
b3baa0fb
TH
328 }
329
56193d1b 330 case htons(ETH_P_FCOE):
224516b3
AD
331 if ((hlen - nhoff) < FCOE_HEADER_LEN)
332 goto out_bad;
333
334 nhoff += FCOE_HEADER_LEN;
335 goto out_good;
0744dd00 336 default:
a6e544b0 337 goto out_bad;
0744dd00
ED
338 }
339
6a74fcf4 340ip_proto_again:
0744dd00
ED
341 switch (ip_proto) {
342 case IPPROTO_GRE: {
ab10dccb
GF
343 struct gre_base_hdr *hdr, _hdr;
344 u16 gre_ver;
345 int offset = 0;
0744dd00 346
690e36e7 347 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
0744dd00 348 if (!hdr)
a6e544b0 349 goto out_bad;
ab10dccb
GF
350
351 /* Only look inside GRE without routing */
352 if (hdr->flags & GRE_ROUTING)
ce3b5355
TH
353 break;
354
ab10dccb
GF
355 /* Only look inside GRE for version 0 and 1 */
356 gre_ver = ntohs(hdr->flags & GRE_VERSION);
357 if (gre_ver > 1)
358 break;
359
360 proto = hdr->protocol;
361 if (gre_ver) {
362 /* Version1 must be PPTP, and check the flags */
363 if (!(proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
364 break;
365 }
366
367 offset += sizeof(struct gre_base_hdr);
368
ce3b5355 369 if (hdr->flags & GRE_CSUM)
ab10dccb
GF
370 offset += sizeof(((struct gre_full_hdr *)0)->csum) +
371 sizeof(((struct gre_full_hdr *)0)->reserved1);
372
1fdd512c
TH
373 if (hdr->flags & GRE_KEY) {
374 const __be32 *keyid;
375 __be32 _keyid;
376
ab10dccb 377 keyid = __skb_header_pointer(skb, nhoff + offset, sizeof(_keyid),
1fdd512c 378 data, hlen, &_keyid);
1fdd512c 379 if (!keyid)
a6e544b0 380 goto out_bad;
1fdd512c 381
20a17bf6
DM
382 if (dissector_uses_key(flow_dissector,
383 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
1fdd512c
TH
384 key_keyid = skb_flow_dissector_target(flow_dissector,
385 FLOW_DISSECTOR_KEY_GRE_KEYID,
386 target_container);
ab10dccb
GF
387 if (gre_ver == 0)
388 key_keyid->keyid = *keyid;
389 else
390 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
1fdd512c 391 }
ab10dccb 392 offset += sizeof(((struct gre_full_hdr *)0)->key);
1fdd512c 393 }
ab10dccb 394
ce3b5355 395 if (hdr->flags & GRE_SEQ)
ab10dccb
GF
396 offset += sizeof(((struct pptp_gre_header *)0)->seq);
397
398 if (gre_ver == 0) {
399 if (proto == htons(ETH_P_TEB)) {
400 const struct ethhdr *eth;
401 struct ethhdr _eth;
402
403 eth = __skb_header_pointer(skb, nhoff + offset,
404 sizeof(_eth),
405 data, hlen, &_eth);
406 if (!eth)
407 goto out_bad;
408 proto = eth->h_proto;
409 offset += sizeof(*eth);
410
411 /* Cap headers that we access via pointers at the
412 * end of the Ethernet header as our maximum alignment
413 * at that point is only 2 bytes.
414 */
415 if (NET_IP_ALIGN)
416 hlen = (nhoff + offset);
417 }
418 } else { /* version 1, must be PPTP */
419 u8 _ppp_hdr[PPP_HDRLEN];
420 u8 *ppp_hdr;
421
422 if (hdr->flags & GRE_ACK)
423 offset += sizeof(((struct pptp_gre_header *)0)->ack);
424
425 ppp_hdr = skb_header_pointer(skb, nhoff + offset,
426 sizeof(_ppp_hdr), _ppp_hdr);
427 if (!ppp_hdr)
a6e544b0 428 goto out_bad;
ab10dccb
GF
429
430 switch (PPP_PROTOCOL(ppp_hdr)) {
431 case PPP_IP:
432 proto = htons(ETH_P_IP);
433 break;
434 case PPP_IPV6:
435 proto = htons(ETH_P_IPV6);
436 break;
437 default:
438 /* Could probably catch some more like MPLS */
439 break;
440 }
441
442 offset += PPP_HDRLEN;
0744dd00 443 }
823b9693 444
ab10dccb 445 nhoff += offset;
4b36993d 446 key_control->flags |= FLOW_DIS_ENCAPSULATION;
823b9693
TH
447 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
448 goto out_good;
449
ce3b5355 450 goto again;
0744dd00 451 }
6a74fcf4
TH
452 case NEXTHDR_HOP:
453 case NEXTHDR_ROUTING:
454 case NEXTHDR_DEST: {
455 u8 _opthdr[2], *opthdr;
456
457 if (proto != htons(ETH_P_IPV6))
458 break;
459
460 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
461 data, hlen, &_opthdr);
1e98a0f0 462 if (!opthdr)
a6e544b0 463 goto out_bad;
6a74fcf4 464
1e98a0f0
ED
465 ip_proto = opthdr[0];
466 nhoff += (opthdr[1] + 1) << 3;
6a74fcf4
TH
467
468 goto ip_proto_again;
469 }
b840f28b
TH
470 case NEXTHDR_FRAGMENT: {
471 struct frag_hdr _fh, *fh;
472
473 if (proto != htons(ETH_P_IPV6))
474 break;
475
476 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
477 data, hlen, &_fh);
478
479 if (!fh)
480 goto out_bad;
481
4b36993d 482 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
b840f28b
TH
483
484 nhoff += sizeof(_fh);
43d2ccb3 485 ip_proto = fh->nexthdr;
b840f28b
TH
486
487 if (!(fh->frag_off & htons(IP6_OFFSET))) {
4b36993d 488 key_control->flags |= FLOW_DIS_FIRST_FRAG;
43d2ccb3 489 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG)
b840f28b 490 goto ip_proto_again;
b840f28b
TH
491 }
492 goto out_good;
493 }
0744dd00 494 case IPPROTO_IPIP:
fca41895 495 proto = htons(ETH_P_IP);
823b9693 496
4b36993d 497 key_control->flags |= FLOW_DIS_ENCAPSULATION;
823b9693
TH
498 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
499 goto out_good;
500
fca41895 501 goto ip;
b438f940
TH
502 case IPPROTO_IPV6:
503 proto = htons(ETH_P_IPV6);
823b9693 504
4b36993d 505 key_control->flags |= FLOW_DIS_ENCAPSULATION;
823b9693
TH
506 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
507 goto out_good;
508
b438f940 509 goto ipv6;
b3baa0fb
TH
510 case IPPROTO_MPLS:
511 proto = htons(ETH_P_MPLS_UC);
512 goto mpls;
0744dd00
ED
513 default:
514 break;
515 }
516
20a17bf6
DM
517 if (dissector_uses_key(flow_dissector,
518 FLOW_DISSECTOR_KEY_PORTS)) {
06635a35
JP
519 key_ports = skb_flow_dissector_target(flow_dissector,
520 FLOW_DISSECTOR_KEY_PORTS,
521 target_container);
522 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
523 data, hlen);
524 }
5af7fb6e 525
a6e544b0
TH
526out_good:
527 ret = true;
528
529out_bad:
530 key_basic->n_proto = proto;
531 key_basic->ip_proto = ip_proto;
532 key_control->thoff = (u16)nhoff;
533
534 return ret;
0744dd00 535}
690e36e7 536EXPORT_SYMBOL(__skb_flow_dissect);
441d9d32
CW
537
538static u32 hashrnd __read_mostly;
66415cf8
HFS
539static __always_inline void __flow_hash_secret_init(void)
540{
541 net_get_random_once(&hashrnd, sizeof(hashrnd));
542}
543
20a17bf6
DM
544static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
545 u32 keyval)
42aecaa9
TH
546{
547 return jhash2(words, length, keyval);
548}
549
20a17bf6 550static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
66415cf8 551{
20a17bf6
DM
552 const void *p = flow;
553
42aecaa9 554 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
20a17bf6 555 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
42aecaa9
TH
556}
557
20a17bf6 558static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
42aecaa9 559{
c3f83241 560 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
42aecaa9 561 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
c3f83241
TH
562 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
563 sizeof(*flow) - sizeof(flow->addrs));
564
565 switch (flow->control.addr_type) {
566 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
567 diff -= sizeof(flow->addrs.v4addrs);
568 break;
569 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
570 diff -= sizeof(flow->addrs.v6addrs);
571 break;
9f249089
TH
572 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
573 diff -= sizeof(flow->addrs.tipcaddrs);
574 break;
c3f83241
TH
575 }
576 return (sizeof(*flow) - diff) / sizeof(u32);
577}
578
579__be32 flow_get_u32_src(const struct flow_keys *flow)
580{
581 switch (flow->control.addr_type) {
582 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
583 return flow->addrs.v4addrs.src;
584 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
585 return (__force __be32)ipv6_addr_hash(
586 &flow->addrs.v6addrs.src);
9f249089
TH
587 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
588 return flow->addrs.tipcaddrs.srcnode;
c3f83241
TH
589 default:
590 return 0;
591 }
592}
593EXPORT_SYMBOL(flow_get_u32_src);
594
595__be32 flow_get_u32_dst(const struct flow_keys *flow)
596{
597 switch (flow->control.addr_type) {
598 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
599 return flow->addrs.v4addrs.dst;
600 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
601 return (__force __be32)ipv6_addr_hash(
602 &flow->addrs.v6addrs.dst);
603 default:
604 return 0;
605 }
606}
607EXPORT_SYMBOL(flow_get_u32_dst);
608
609static inline void __flow_hash_consistentify(struct flow_keys *keys)
610{
611 int addr_diff, i;
612
613 switch (keys->control.addr_type) {
614 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
615 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
616 (__force u32)keys->addrs.v4addrs.src;
617 if ((addr_diff < 0) ||
618 (addr_diff == 0 &&
619 ((__force u16)keys->ports.dst <
620 (__force u16)keys->ports.src))) {
621 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
622 swap(keys->ports.src, keys->ports.dst);
623 }
624 break;
625 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
626 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
627 &keys->addrs.v6addrs.src,
628 sizeof(keys->addrs.v6addrs.dst));
629 if ((addr_diff < 0) ||
630 (addr_diff == 0 &&
631 ((__force u16)keys->ports.dst <
632 (__force u16)keys->ports.src))) {
633 for (i = 0; i < 4; i++)
634 swap(keys->addrs.v6addrs.src.s6_addr32[i],
635 keys->addrs.v6addrs.dst.s6_addr32[i]);
636 swap(keys->ports.src, keys->ports.dst);
637 }
638 break;
639 }
66415cf8
HFS
640}
641
50fb7992 642static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
5ed20a68
TH
643{
644 u32 hash;
645
c3f83241 646 __flow_hash_consistentify(keys);
5ed20a68 647
20a17bf6 648 hash = __flow_hash_words(flow_keys_hash_start(keys),
42aecaa9 649 flow_keys_hash_length(keys), keyval);
5ed20a68
TH
650 if (!hash)
651 hash = 1;
652
653 return hash;
654}
655
656u32 flow_hash_from_keys(struct flow_keys *keys)
657{
50fb7992
TH
658 __flow_hash_secret_init();
659 return __flow_hash_from_keys(keys, hashrnd);
5ed20a68
TH
660}
661EXPORT_SYMBOL(flow_hash_from_keys);
662
50fb7992
TH
663static inline u32 ___skb_get_hash(const struct sk_buff *skb,
664 struct flow_keys *keys, u32 keyval)
665{
6db61d79
TH
666 skb_flow_dissect_flow_keys(skb, keys,
667 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
50fb7992
TH
668
669 return __flow_hash_from_keys(keys, keyval);
670}
671
2f59e1eb
TH
672struct _flow_keys_digest_data {
673 __be16 n_proto;
674 u8 ip_proto;
675 u8 padding;
676 __be32 ports;
677 __be32 src;
678 __be32 dst;
679};
680
681void make_flow_keys_digest(struct flow_keys_digest *digest,
682 const struct flow_keys *flow)
683{
684 struct _flow_keys_digest_data *data =
685 (struct _flow_keys_digest_data *)digest;
686
687 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
688
689 memset(digest, 0, sizeof(*digest));
690
06635a35
JP
691 data->n_proto = flow->basic.n_proto;
692 data->ip_proto = flow->basic.ip_proto;
693 data->ports = flow->ports.ports;
c3f83241
TH
694 data->src = flow->addrs.v4addrs.src;
695 data->dst = flow->addrs.v4addrs.dst;
2f59e1eb
TH
696}
697EXPORT_SYMBOL(make_flow_keys_digest);
698
eb70db87
DM
699static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
700
701u32 __skb_get_hash_symmetric(struct sk_buff *skb)
702{
703 struct flow_keys keys;
704
705 __flow_hash_secret_init();
706
707 memset(&keys, 0, sizeof(keys));
708 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
709 NULL, 0, 0, 0,
710 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
711
712 return __flow_hash_from_keys(&keys, hashrnd);
713}
714EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
715
d4fd3275
JP
716/**
717 * __skb_get_hash: calculate a flow hash
718 * @skb: sk_buff to calculate flow hash from
719 *
720 * This function calculates a flow hash based on src/dst addresses
61b905da
TH
721 * and src/dst port numbers. Sets hash in skb to non-zero hash value
722 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
441d9d32
CW
723 * if hash is a canonical 4-tuple hash over transport ports.
724 */
3958afa1 725void __skb_get_hash(struct sk_buff *skb)
441d9d32
CW
726{
727 struct flow_keys keys;
441d9d32 728
50fb7992
TH
729 __flow_hash_secret_init();
730
6db61d79 731 __skb_set_sw_hash(skb, ___skb_get_hash(skb, &keys, hashrnd),
bcc83839 732 flow_keys_have_l4(&keys));
441d9d32 733}
3958afa1 734EXPORT_SYMBOL(__skb_get_hash);
441d9d32 735
50fb7992
TH
736__u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
737{
738 struct flow_keys keys;
739
740 return ___skb_get_hash(skb, &keys, perturb);
741}
742EXPORT_SYMBOL(skb_get_hash_perturb);
743
20a17bf6 744__u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
f70ea018
TH
745{
746 struct flow_keys keys;
747
748 memset(&keys, 0, sizeof(keys));
749
750 memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
751 sizeof(keys.addrs.v6addrs.src));
752 memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
753 sizeof(keys.addrs.v6addrs.dst));
754 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
755 keys.ports.src = fl6->fl6_sport;
756 keys.ports.dst = fl6->fl6_dport;
757 keys.keyid.keyid = fl6->fl6_gre_key;
758 keys.tags.flow_label = (__force u32)fl6->flowlabel;
759 keys.basic.ip_proto = fl6->flowi6_proto;
760
bcc83839
TH
761 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
762 flow_keys_have_l4(&keys));
f70ea018
TH
763
764 return skb->hash;
765}
766EXPORT_SYMBOL(__skb_get_hash_flowi6);
767
20a17bf6 768__u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4)
f70ea018
TH
769{
770 struct flow_keys keys;
771
772 memset(&keys, 0, sizeof(keys));
773
774 keys.addrs.v4addrs.src = fl4->saddr;
775 keys.addrs.v4addrs.dst = fl4->daddr;
776 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
777 keys.ports.src = fl4->fl4_sport;
778 keys.ports.dst = fl4->fl4_dport;
779 keys.keyid.keyid = fl4->fl4_gre_key;
780 keys.basic.ip_proto = fl4->flowi4_proto;
781
bcc83839
TH
782 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
783 flow_keys_have_l4(&keys));
f70ea018
TH
784
785 return skb->hash;
786}
787EXPORT_SYMBOL(__skb_get_hash_flowi4);
788
56193d1b
AD
789u32 __skb_get_poff(const struct sk_buff *skb, void *data,
790 const struct flow_keys *keys, int hlen)
f77668dc 791{
42aecaa9 792 u32 poff = keys->control.thoff;
f77668dc 793
43d2ccb3
AD
794 /* skip L4 headers for fragments after the first */
795 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
796 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
797 return poff;
798
06635a35 799 switch (keys->basic.ip_proto) {
f77668dc 800 case IPPROTO_TCP: {
5af7fb6e
AD
801 /* access doff as u8 to avoid unaligned access */
802 const u8 *doff;
803 u8 _doff;
f77668dc 804
5af7fb6e
AD
805 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
806 data, hlen, &_doff);
807 if (!doff)
f77668dc
DB
808 return poff;
809
5af7fb6e 810 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
f77668dc
DB
811 break;
812 }
813 case IPPROTO_UDP:
814 case IPPROTO_UDPLITE:
815 poff += sizeof(struct udphdr);
816 break;
817 /* For the rest, we do not really care about header
818 * extensions at this point for now.
819 */
820 case IPPROTO_ICMP:
821 poff += sizeof(struct icmphdr);
822 break;
823 case IPPROTO_ICMPV6:
824 poff += sizeof(struct icmp6hdr);
825 break;
826 case IPPROTO_IGMP:
827 poff += sizeof(struct igmphdr);
828 break;
829 case IPPROTO_DCCP:
830 poff += sizeof(struct dccp_hdr);
831 break;
832 case IPPROTO_SCTP:
833 poff += sizeof(struct sctphdr);
834 break;
835 }
836
837 return poff;
838}
839
0db89b8b
JP
840/**
841 * skb_get_poff - get the offset to the payload
842 * @skb: sk_buff to get the payload offset from
843 *
844 * The function will get the offset to the payload as far as it could
845 * be dissected. The main user is currently BPF, so that we can dynamically
56193d1b
AD
846 * truncate packets without needing to push actual payload to the user
847 * space and can analyze headers only, instead.
848 */
849u32 skb_get_poff(const struct sk_buff *skb)
850{
851 struct flow_keys keys;
852
cd79a238 853 if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
56193d1b
AD
854 return 0;
855
856 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
857}
06635a35 858
20a17bf6 859__u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
a17ace95
DM
860{
861 memset(keys, 0, sizeof(*keys));
862
863 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
864 sizeof(keys->addrs.v6addrs.src));
865 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
866 sizeof(keys->addrs.v6addrs.dst));
867 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
868 keys->ports.src = fl6->fl6_sport;
869 keys->ports.dst = fl6->fl6_dport;
870 keys->keyid.keyid = fl6->fl6_gre_key;
871 keys->tags.flow_label = (__force u32)fl6->flowlabel;
872 keys->basic.ip_proto = fl6->flowi6_proto;
873
874 return flow_hash_from_keys(keys);
875}
876EXPORT_SYMBOL(__get_hash_from_flowi6);
877
20a17bf6 878__u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
a17ace95
DM
879{
880 memset(keys, 0, sizeof(*keys));
881
882 keys->addrs.v4addrs.src = fl4->saddr;
883 keys->addrs.v4addrs.dst = fl4->daddr;
884 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
885 keys->ports.src = fl4->fl4_sport;
886 keys->ports.dst = fl4->fl4_dport;
887 keys->keyid.keyid = fl4->fl4_gre_key;
888 keys->basic.ip_proto = fl4->flowi4_proto;
889
890 return flow_hash_from_keys(keys);
891}
892EXPORT_SYMBOL(__get_hash_from_flowi4);
893
06635a35 894static const struct flow_dissector_key flow_keys_dissector_keys[] = {
42aecaa9
TH
895 {
896 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
897 .offset = offsetof(struct flow_keys, control),
898 },
06635a35
JP
899 {
900 .key_id = FLOW_DISSECTOR_KEY_BASIC,
901 .offset = offsetof(struct flow_keys, basic),
902 },
903 {
904 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
c3f83241
TH
905 .offset = offsetof(struct flow_keys, addrs.v4addrs),
906 },
907 {
908 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
909 .offset = offsetof(struct flow_keys, addrs.v6addrs),
06635a35 910 },
9f249089
TH
911 {
912 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
913 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
914 },
06635a35
JP
915 {
916 .key_id = FLOW_DISSECTOR_KEY_PORTS,
917 .offset = offsetof(struct flow_keys, ports),
918 },
d34af823
TH
919 {
920 .key_id = FLOW_DISSECTOR_KEY_VLANID,
921 .offset = offsetof(struct flow_keys, tags),
922 },
87ee9e52
TH
923 {
924 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
925 .offset = offsetof(struct flow_keys, tags),
926 },
1fdd512c
TH
927 {
928 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
929 .offset = offsetof(struct flow_keys, keyid),
930 },
06635a35
JP
931};
932
eb70db87
DM
933static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
934 {
935 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
936 .offset = offsetof(struct flow_keys, control),
937 },
938 {
939 .key_id = FLOW_DISSECTOR_KEY_BASIC,
940 .offset = offsetof(struct flow_keys, basic),
941 },
942 {
943 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
944 .offset = offsetof(struct flow_keys, addrs.v4addrs),
945 },
946 {
947 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
948 .offset = offsetof(struct flow_keys, addrs.v6addrs),
949 },
950 {
951 .key_id = FLOW_DISSECTOR_KEY_PORTS,
952 .offset = offsetof(struct flow_keys, ports),
953 },
954};
955
06635a35 956static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
42aecaa9
TH
957 {
958 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
959 .offset = offsetof(struct flow_keys, control),
960 },
06635a35
JP
961 {
962 .key_id = FLOW_DISSECTOR_KEY_BASIC,
963 .offset = offsetof(struct flow_keys, basic),
964 },
965};
966
967struct flow_dissector flow_keys_dissector __read_mostly;
968EXPORT_SYMBOL(flow_keys_dissector);
969
970struct flow_dissector flow_keys_buf_dissector __read_mostly;
971
972static int __init init_default_flow_dissectors(void)
973{
974 skb_flow_dissector_init(&flow_keys_dissector,
975 flow_keys_dissector_keys,
976 ARRAY_SIZE(flow_keys_dissector_keys));
eb70db87
DM
977 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
978 flow_keys_dissector_symmetric_keys,
979 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
06635a35
JP
980 skb_flow_dissector_init(&flow_keys_buf_dissector,
981 flow_keys_buf_dissector_keys,
982 ARRAY_SIZE(flow_keys_buf_dissector_keys));
983 return 0;
984}
985
986late_initcall_sync(init_default_flow_dissectors);