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