Merge branch 'mlx5-fixes'
[deliverable/linux.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
20a17bf6
DM
22static bool dissector_uses_key(const struct flow_dissector *flow_dissector,
23 enum flow_dissector_key_id key_id)
fbff949e
JP
24{
25 return flow_dissector->used_keys & (1 << key_id);
26}
27
20a17bf6
DM
28static void dissector_set_key(struct flow_dissector *flow_dissector,
29 enum flow_dissector_key_id key_id)
fbff949e
JP
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);
20a17bf6
DM
54 BUG_ON(dissector_uses_key(flow_dissector,
55 key->key_id));
fbff949e 56
20a17bf6 57 dissector_set_key(flow_dissector, key->key_id);
fbff949e
JP
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 */
20a17bf6
DM
64 BUG_ON(!dissector_uses_key(flow_dissector,
65 FLOW_DISSECTOR_KEY_CONTROL));
66 BUG_ON(!dissector_uses_key(flow_dissector,
67 FLOW_DISSECTOR_KEY_BASIC));
fbff949e
JP
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
20a17bf6
DM
157 if (dissector_uses_key(flow_dissector,
158 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
67a900cc
JP
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
20a17bf6
DM
181 if (!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 211
0744dd00 212ipv6:
690e36e7 213 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
0744dd00 214 if (!iph)
a6e544b0 215 goto out_bad;
0744dd00
ED
216
217 ip_proto = iph->nexthdr;
0744dd00 218 nhoff += sizeof(struct ipv6hdr);
19469a87 219
20a17bf6
DM
220 if (dissector_uses_key(flow_dissector,
221 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
b924933c
JP
222 struct flow_dissector_key_ipv6_addrs *key_ipv6_addrs;
223
224 key_ipv6_addrs = skb_flow_dissector_target(flow_dissector,
225 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
226 target_container);
5af7fb6e 227
b924933c 228 memcpy(key_ipv6_addrs, &iph->saddr, sizeof(*key_ipv6_addrs));
c3f83241 229 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
b924933c 230 }
87ee9e52 231
461547f3
AD
232 if ((dissector_uses_key(flow_dissector,
233 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
234 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
235 ip6_flowlabel(iph)) {
236 __be32 flow_label = ip6_flowlabel(iph);
237
20a17bf6
DM
238 if (dissector_uses_key(flow_dissector,
239 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
87ee9e52
TH
240 key_tags = skb_flow_dissector_target(flow_dissector,
241 FLOW_DISSECTOR_KEY_FLOW_LABEL,
242 target_container);
243 key_tags->flow_label = ntohl(flow_label);
12c227ec 244 }
872b1abb
TH
245 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
246 goto out_good;
19469a87
TH
247 }
248
8306b688
TH
249 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
250 goto out_good;
251
0744dd00
ED
252 break;
253 }
2b8837ae
JP
254 case htons(ETH_P_8021AD):
255 case htons(ETH_P_8021Q): {
0744dd00
ED
256 const struct vlan_hdr *vlan;
257 struct vlan_hdr _vlan;
258
690e36e7 259 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan);
0744dd00 260 if (!vlan)
a6e544b0 261 goto out_bad;
0744dd00 262
20a17bf6
DM
263 if (dissector_uses_key(flow_dissector,
264 FLOW_DISSECTOR_KEY_VLANID)) {
d34af823
TH
265 key_tags = skb_flow_dissector_target(flow_dissector,
266 FLOW_DISSECTOR_KEY_VLANID,
267 target_container);
268
269 key_tags->vlan_id = skb_vlan_tag_get_id(skb);
270 }
271
0744dd00
ED
272 proto = vlan->h_vlan_encapsulated_proto;
273 nhoff += sizeof(*vlan);
274 goto again;
275 }
2b8837ae 276 case htons(ETH_P_PPP_SES): {
0744dd00
ED
277 struct {
278 struct pppoe_hdr hdr;
279 __be16 proto;
280 } *hdr, _hdr;
690e36e7 281 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
0744dd00 282 if (!hdr)
a6e544b0 283 goto out_bad;
0744dd00
ED
284 proto = hdr->proto;
285 nhoff += PPPOE_SES_HLEN;
286 switch (proto) {
2b8837ae 287 case htons(PPP_IP):
0744dd00 288 goto ip;
2b8837ae 289 case htons(PPP_IPV6):
0744dd00
ED
290 goto ipv6;
291 default:
a6e544b0 292 goto out_bad;
0744dd00
ED
293 }
294 }
08bfc9cb
EH
295 case htons(ETH_P_TIPC): {
296 struct {
297 __be32 pre[3];
298 __be32 srcnode;
299 } *hdr, _hdr;
300 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
301 if (!hdr)
a6e544b0 302 goto out_bad;
06635a35 303
20a17bf6
DM
304 if (dissector_uses_key(flow_dissector,
305 FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
06635a35 306 key_addrs = skb_flow_dissector_target(flow_dissector,
9f249089 307 FLOW_DISSECTOR_KEY_TIPC_ADDRS,
06635a35 308 target_container);
9f249089
TH
309 key_addrs->tipcaddrs.srcnode = hdr->srcnode;
310 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
06635a35 311 }
a6e544b0 312 goto out_good;
08bfc9cb 313 }
b3baa0fb
TH
314
315 case htons(ETH_P_MPLS_UC):
316 case htons(ETH_P_MPLS_MC): {
317 struct mpls_label *hdr, _hdr[2];
318mpls:
319 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
320 hlen, &_hdr);
321 if (!hdr)
a6e544b0 322 goto out_bad;
b3baa0fb 323
611d23c5
TH
324 if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >>
325 MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) {
20a17bf6
DM
326 if (dissector_uses_key(flow_dissector,
327 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
b3baa0fb
TH
328 key_keyid = skb_flow_dissector_target(flow_dissector,
329 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
330 target_container);
331 key_keyid->keyid = hdr[1].entry &
332 htonl(MPLS_LS_LABEL_MASK);
333 }
334
a6e544b0 335 goto out_good;
b3baa0fb
TH
336 }
337
a6e544b0 338 goto out_good;
b3baa0fb
TH
339 }
340
56193d1b 341 case htons(ETH_P_FCOE):
42aecaa9 342 key_control->thoff = (u16)(nhoff + FCOE_HEADER_LEN);
56193d1b 343 /* fall through */
0744dd00 344 default:
a6e544b0 345 goto out_bad;
0744dd00
ED
346 }
347
6a74fcf4 348ip_proto_again:
0744dd00
ED
349 switch (ip_proto) {
350 case IPPROTO_GRE: {
351 struct gre_hdr {
352 __be16 flags;
353 __be16 proto;
354 } *hdr, _hdr;
355
690e36e7 356 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
0744dd00 357 if (!hdr)
a6e544b0 358 goto out_bad;
0744dd00
ED
359 /*
360 * Only look inside GRE if version zero and no
361 * routing
362 */
ce3b5355
TH
363 if (hdr->flags & (GRE_VERSION | GRE_ROUTING))
364 break;
365
366 proto = hdr->proto;
367 nhoff += 4;
368 if (hdr->flags & GRE_CSUM)
0744dd00 369 nhoff += 4;
1fdd512c
TH
370 if (hdr->flags & GRE_KEY) {
371 const __be32 *keyid;
372 __be32 _keyid;
373
374 keyid = __skb_header_pointer(skb, nhoff, sizeof(_keyid),
375 data, hlen, &_keyid);
376
377 if (!keyid)
a6e544b0 378 goto out_bad;
1fdd512c 379
20a17bf6
DM
380 if (dissector_uses_key(flow_dissector,
381 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
1fdd512c
TH
382 key_keyid = skb_flow_dissector_target(flow_dissector,
383 FLOW_DISSECTOR_KEY_GRE_KEYID,
384 target_container);
385 key_keyid->keyid = *keyid;
386 }
ce3b5355 387 nhoff += 4;
1fdd512c 388 }
ce3b5355
TH
389 if (hdr->flags & GRE_SEQ)
390 nhoff += 4;
391 if (proto == htons(ETH_P_TEB)) {
392 const struct ethhdr *eth;
393 struct ethhdr _eth;
394
395 eth = __skb_header_pointer(skb, nhoff,
396 sizeof(_eth),
397 data, hlen, &_eth);
398 if (!eth)
a6e544b0 399 goto out_bad;
ce3b5355
TH
400 proto = eth->h_proto;
401 nhoff += sizeof(*eth);
0744dd00 402 }
823b9693 403
4b36993d 404 key_control->flags |= FLOW_DIS_ENCAPSULATION;
823b9693
TH
405 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
406 goto out_good;
407
ce3b5355 408 goto again;
0744dd00 409 }
6a74fcf4
TH
410 case NEXTHDR_HOP:
411 case NEXTHDR_ROUTING:
412 case NEXTHDR_DEST: {
413 u8 _opthdr[2], *opthdr;
414
415 if (proto != htons(ETH_P_IPV6))
416 break;
417
418 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
419 data, hlen, &_opthdr);
1e98a0f0 420 if (!opthdr)
a6e544b0 421 goto out_bad;
6a74fcf4 422
1e98a0f0
ED
423 ip_proto = opthdr[0];
424 nhoff += (opthdr[1] + 1) << 3;
6a74fcf4
TH
425
426 goto ip_proto_again;
427 }
b840f28b
TH
428 case NEXTHDR_FRAGMENT: {
429 struct frag_hdr _fh, *fh;
430
431 if (proto != htons(ETH_P_IPV6))
432 break;
433
434 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
435 data, hlen, &_fh);
436
437 if (!fh)
438 goto out_bad;
439
4b36993d 440 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
b840f28b
TH
441
442 nhoff += sizeof(_fh);
443
444 if (!(fh->frag_off & htons(IP6_OFFSET))) {
4b36993d 445 key_control->flags |= FLOW_DIS_FIRST_FRAG;
b840f28b
TH
446 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
447 ip_proto = fh->nexthdr;
448 goto ip_proto_again;
449 }
450 }
451 goto out_good;
452 }
0744dd00 453 case IPPROTO_IPIP:
fca41895 454 proto = htons(ETH_P_IP);
823b9693 455
4b36993d 456 key_control->flags |= FLOW_DIS_ENCAPSULATION;
823b9693
TH
457 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
458 goto out_good;
459
fca41895 460 goto ip;
b438f940
TH
461 case IPPROTO_IPV6:
462 proto = htons(ETH_P_IPV6);
823b9693 463
4b36993d 464 key_control->flags |= FLOW_DIS_ENCAPSULATION;
823b9693
TH
465 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
466 goto out_good;
467
b438f940 468 goto ipv6;
b3baa0fb
TH
469 case IPPROTO_MPLS:
470 proto = htons(ETH_P_MPLS_UC);
471 goto mpls;
0744dd00
ED
472 default:
473 break;
474 }
475
20a17bf6
DM
476 if (dissector_uses_key(flow_dissector,
477 FLOW_DISSECTOR_KEY_PORTS)) {
06635a35
JP
478 key_ports = skb_flow_dissector_target(flow_dissector,
479 FLOW_DISSECTOR_KEY_PORTS,
480 target_container);
481 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
482 data, hlen);
483 }
5af7fb6e 484
a6e544b0
TH
485out_good:
486 ret = true;
487
488out_bad:
489 key_basic->n_proto = proto;
490 key_basic->ip_proto = ip_proto;
491 key_control->thoff = (u16)nhoff;
492
493 return ret;
0744dd00 494}
690e36e7 495EXPORT_SYMBOL(__skb_flow_dissect);
441d9d32
CW
496
497static u32 hashrnd __read_mostly;
66415cf8
HFS
498static __always_inline void __flow_hash_secret_init(void)
499{
500 net_get_random_once(&hashrnd, sizeof(hashrnd));
501}
502
20a17bf6
DM
503static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
504 u32 keyval)
42aecaa9
TH
505{
506 return jhash2(words, length, keyval);
507}
508
20a17bf6 509static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
66415cf8 510{
20a17bf6
DM
511 const void *p = flow;
512
42aecaa9 513 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
20a17bf6 514 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
42aecaa9
TH
515}
516
20a17bf6 517static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
42aecaa9 518{
c3f83241 519 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
42aecaa9 520 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
c3f83241
TH
521 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
522 sizeof(*flow) - sizeof(flow->addrs));
523
524 switch (flow->control.addr_type) {
525 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
526 diff -= sizeof(flow->addrs.v4addrs);
527 break;
528 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
529 diff -= sizeof(flow->addrs.v6addrs);
530 break;
9f249089
TH
531 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
532 diff -= sizeof(flow->addrs.tipcaddrs);
533 break;
c3f83241
TH
534 }
535 return (sizeof(*flow) - diff) / sizeof(u32);
536}
537
538__be32 flow_get_u32_src(const struct flow_keys *flow)
539{
540 switch (flow->control.addr_type) {
541 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
542 return flow->addrs.v4addrs.src;
543 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
544 return (__force __be32)ipv6_addr_hash(
545 &flow->addrs.v6addrs.src);
9f249089
TH
546 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
547 return flow->addrs.tipcaddrs.srcnode;
c3f83241
TH
548 default:
549 return 0;
550 }
551}
552EXPORT_SYMBOL(flow_get_u32_src);
553
554__be32 flow_get_u32_dst(const struct flow_keys *flow)
555{
556 switch (flow->control.addr_type) {
557 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
558 return flow->addrs.v4addrs.dst;
559 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
560 return (__force __be32)ipv6_addr_hash(
561 &flow->addrs.v6addrs.dst);
562 default:
563 return 0;
564 }
565}
566EXPORT_SYMBOL(flow_get_u32_dst);
567
568static inline void __flow_hash_consistentify(struct flow_keys *keys)
569{
570 int addr_diff, i;
571
572 switch (keys->control.addr_type) {
573 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
574 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
575 (__force u32)keys->addrs.v4addrs.src;
576 if ((addr_diff < 0) ||
577 (addr_diff == 0 &&
578 ((__force u16)keys->ports.dst <
579 (__force u16)keys->ports.src))) {
580 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
581 swap(keys->ports.src, keys->ports.dst);
582 }
583 break;
584 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
585 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
586 &keys->addrs.v6addrs.src,
587 sizeof(keys->addrs.v6addrs.dst));
588 if ((addr_diff < 0) ||
589 (addr_diff == 0 &&
590 ((__force u16)keys->ports.dst <
591 (__force u16)keys->ports.src))) {
592 for (i = 0; i < 4; i++)
593 swap(keys->addrs.v6addrs.src.s6_addr32[i],
594 keys->addrs.v6addrs.dst.s6_addr32[i]);
595 swap(keys->ports.src, keys->ports.dst);
596 }
597 break;
598 }
66415cf8
HFS
599}
600
50fb7992 601static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
5ed20a68
TH
602{
603 u32 hash;
604
c3f83241 605 __flow_hash_consistentify(keys);
5ed20a68 606
20a17bf6 607 hash = __flow_hash_words(flow_keys_hash_start(keys),
42aecaa9 608 flow_keys_hash_length(keys), keyval);
5ed20a68
TH
609 if (!hash)
610 hash = 1;
611
612 return hash;
613}
614
615u32 flow_hash_from_keys(struct flow_keys *keys)
616{
50fb7992
TH
617 __flow_hash_secret_init();
618 return __flow_hash_from_keys(keys, hashrnd);
5ed20a68
TH
619}
620EXPORT_SYMBOL(flow_hash_from_keys);
621
50fb7992
TH
622static inline u32 ___skb_get_hash(const struct sk_buff *skb,
623 struct flow_keys *keys, u32 keyval)
624{
6db61d79
TH
625 skb_flow_dissect_flow_keys(skb, keys,
626 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
50fb7992
TH
627
628 return __flow_hash_from_keys(keys, keyval);
629}
630
2f59e1eb
TH
631struct _flow_keys_digest_data {
632 __be16 n_proto;
633 u8 ip_proto;
634 u8 padding;
635 __be32 ports;
636 __be32 src;
637 __be32 dst;
638};
639
640void make_flow_keys_digest(struct flow_keys_digest *digest,
641 const struct flow_keys *flow)
642{
643 struct _flow_keys_digest_data *data =
644 (struct _flow_keys_digest_data *)digest;
645
646 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
647
648 memset(digest, 0, sizeof(*digest));
649
06635a35
JP
650 data->n_proto = flow->basic.n_proto;
651 data->ip_proto = flow->basic.ip_proto;
652 data->ports = flow->ports.ports;
c3f83241
TH
653 data->src = flow->addrs.v4addrs.src;
654 data->dst = flow->addrs.v4addrs.dst;
2f59e1eb
TH
655}
656EXPORT_SYMBOL(make_flow_keys_digest);
657
d4fd3275
JP
658/**
659 * __skb_get_hash: calculate a flow hash
660 * @skb: sk_buff to calculate flow hash from
661 *
662 * This function calculates a flow hash based on src/dst addresses
61b905da
TH
663 * and src/dst port numbers. Sets hash in skb to non-zero hash value
664 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
441d9d32
CW
665 * if hash is a canonical 4-tuple hash over transport ports.
666 */
3958afa1 667void __skb_get_hash(struct sk_buff *skb)
441d9d32
CW
668{
669 struct flow_keys keys;
441d9d32 670
50fb7992
TH
671 __flow_hash_secret_init();
672
6db61d79 673 __skb_set_sw_hash(skb, ___skb_get_hash(skb, &keys, hashrnd),
bcc83839 674 flow_keys_have_l4(&keys));
441d9d32 675}
3958afa1 676EXPORT_SYMBOL(__skb_get_hash);
441d9d32 677
50fb7992
TH
678__u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
679{
680 struct flow_keys keys;
681
682 return ___skb_get_hash(skb, &keys, perturb);
683}
684EXPORT_SYMBOL(skb_get_hash_perturb);
685
20a17bf6 686__u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
f70ea018
TH
687{
688 struct flow_keys keys;
689
690 memset(&keys, 0, sizeof(keys));
691
692 memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
693 sizeof(keys.addrs.v6addrs.src));
694 memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
695 sizeof(keys.addrs.v6addrs.dst));
696 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
697 keys.ports.src = fl6->fl6_sport;
698 keys.ports.dst = fl6->fl6_dport;
699 keys.keyid.keyid = fl6->fl6_gre_key;
700 keys.tags.flow_label = (__force u32)fl6->flowlabel;
701 keys.basic.ip_proto = fl6->flowi6_proto;
702
bcc83839
TH
703 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
704 flow_keys_have_l4(&keys));
f70ea018
TH
705
706 return skb->hash;
707}
708EXPORT_SYMBOL(__skb_get_hash_flowi6);
709
20a17bf6 710__u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4)
f70ea018
TH
711{
712 struct flow_keys keys;
713
714 memset(&keys, 0, sizeof(keys));
715
716 keys.addrs.v4addrs.src = fl4->saddr;
717 keys.addrs.v4addrs.dst = fl4->daddr;
718 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
719 keys.ports.src = fl4->fl4_sport;
720 keys.ports.dst = fl4->fl4_dport;
721 keys.keyid.keyid = fl4->fl4_gre_key;
722 keys.basic.ip_proto = fl4->flowi4_proto;
723
bcc83839
TH
724 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
725 flow_keys_have_l4(&keys));
f70ea018
TH
726
727 return skb->hash;
728}
729EXPORT_SYMBOL(__skb_get_hash_flowi4);
730
56193d1b
AD
731u32 __skb_get_poff(const struct sk_buff *skb, void *data,
732 const struct flow_keys *keys, int hlen)
f77668dc 733{
42aecaa9 734 u32 poff = keys->control.thoff;
f77668dc 735
06635a35 736 switch (keys->basic.ip_proto) {
f77668dc 737 case IPPROTO_TCP: {
5af7fb6e
AD
738 /* access doff as u8 to avoid unaligned access */
739 const u8 *doff;
740 u8 _doff;
f77668dc 741
5af7fb6e
AD
742 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
743 data, hlen, &_doff);
744 if (!doff)
f77668dc
DB
745 return poff;
746
5af7fb6e 747 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
f77668dc
DB
748 break;
749 }
750 case IPPROTO_UDP:
751 case IPPROTO_UDPLITE:
752 poff += sizeof(struct udphdr);
753 break;
754 /* For the rest, we do not really care about header
755 * extensions at this point for now.
756 */
757 case IPPROTO_ICMP:
758 poff += sizeof(struct icmphdr);
759 break;
760 case IPPROTO_ICMPV6:
761 poff += sizeof(struct icmp6hdr);
762 break;
763 case IPPROTO_IGMP:
764 poff += sizeof(struct igmphdr);
765 break;
766 case IPPROTO_DCCP:
767 poff += sizeof(struct dccp_hdr);
768 break;
769 case IPPROTO_SCTP:
770 poff += sizeof(struct sctphdr);
771 break;
772 }
773
774 return poff;
775}
776
0db89b8b
JP
777/**
778 * skb_get_poff - get the offset to the payload
779 * @skb: sk_buff to get the payload offset from
780 *
781 * The function will get the offset to the payload as far as it could
782 * be dissected. The main user is currently BPF, so that we can dynamically
56193d1b
AD
783 * truncate packets without needing to push actual payload to the user
784 * space and can analyze headers only, instead.
785 */
786u32 skb_get_poff(const struct sk_buff *skb)
787{
788 struct flow_keys keys;
789
cd79a238 790 if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
56193d1b
AD
791 return 0;
792
793 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
794}
06635a35 795
20a17bf6 796__u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
a17ace95
DM
797{
798 memset(keys, 0, sizeof(*keys));
799
800 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
801 sizeof(keys->addrs.v6addrs.src));
802 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
803 sizeof(keys->addrs.v6addrs.dst));
804 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
805 keys->ports.src = fl6->fl6_sport;
806 keys->ports.dst = fl6->fl6_dport;
807 keys->keyid.keyid = fl6->fl6_gre_key;
808 keys->tags.flow_label = (__force u32)fl6->flowlabel;
809 keys->basic.ip_proto = fl6->flowi6_proto;
810
811 return flow_hash_from_keys(keys);
812}
813EXPORT_SYMBOL(__get_hash_from_flowi6);
814
20a17bf6 815__u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
a17ace95
DM
816{
817 memset(keys, 0, sizeof(*keys));
818
819 keys->addrs.v4addrs.src = fl4->saddr;
820 keys->addrs.v4addrs.dst = fl4->daddr;
821 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
822 keys->ports.src = fl4->fl4_sport;
823 keys->ports.dst = fl4->fl4_dport;
824 keys->keyid.keyid = fl4->fl4_gre_key;
825 keys->basic.ip_proto = fl4->flowi4_proto;
826
827 return flow_hash_from_keys(keys);
828}
829EXPORT_SYMBOL(__get_hash_from_flowi4);
830
06635a35 831static const struct flow_dissector_key flow_keys_dissector_keys[] = {
42aecaa9
TH
832 {
833 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
834 .offset = offsetof(struct flow_keys, control),
835 },
06635a35
JP
836 {
837 .key_id = FLOW_DISSECTOR_KEY_BASIC,
838 .offset = offsetof(struct flow_keys, basic),
839 },
840 {
841 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
c3f83241
TH
842 .offset = offsetof(struct flow_keys, addrs.v4addrs),
843 },
844 {
845 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
846 .offset = offsetof(struct flow_keys, addrs.v6addrs),
06635a35 847 },
9f249089
TH
848 {
849 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
850 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
851 },
06635a35
JP
852 {
853 .key_id = FLOW_DISSECTOR_KEY_PORTS,
854 .offset = offsetof(struct flow_keys, ports),
855 },
d34af823
TH
856 {
857 .key_id = FLOW_DISSECTOR_KEY_VLANID,
858 .offset = offsetof(struct flow_keys, tags),
859 },
87ee9e52
TH
860 {
861 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
862 .offset = offsetof(struct flow_keys, tags),
863 },
1fdd512c
TH
864 {
865 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
866 .offset = offsetof(struct flow_keys, keyid),
867 },
06635a35
JP
868};
869
870static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
42aecaa9
TH
871 {
872 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
873 .offset = offsetof(struct flow_keys, control),
874 },
06635a35
JP
875 {
876 .key_id = FLOW_DISSECTOR_KEY_BASIC,
877 .offset = offsetof(struct flow_keys, basic),
878 },
879};
880
881struct flow_dissector flow_keys_dissector __read_mostly;
882EXPORT_SYMBOL(flow_keys_dissector);
883
884struct flow_dissector flow_keys_buf_dissector __read_mostly;
885
886static int __init init_default_flow_dissectors(void)
887{
888 skb_flow_dissector_init(&flow_keys_dissector,
889 flow_keys_dissector_keys,
890 ARRAY_SIZE(flow_keys_dissector_keys));
891 skb_flow_dissector_init(&flow_keys_buf_dissector,
892 flow_keys_buf_dissector_keys,
893 ARRAY_SIZE(flow_keys_buf_dissector_keys));
894 return 0;
895}
896
897late_initcall_sync(init_default_flow_dissectors);
This page took 0.274594 seconds and 5 git commands to generate.