x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / net / core / flow_dissector.c
blobe2e716003ede769e8250571f6ece862c842a649a
1 #include <linux/kernel.h>
2 #include <linux/skbuff.h>
3 #include <linux/export.h>
4 #include <linux/ip.h>
5 #include <linux/ipv6.h>
6 #include <linux/if_vlan.h>
7 #include <net/dsa.h>
8 #include <net/ip.h>
9 #include <net/ipv6.h>
10 #include <net/gre.h>
11 #include <net/pptp.h>
12 #include <linux/igmp.h>
13 #include <linux/icmp.h>
14 #include <linux/sctp.h>
15 #include <linux/dccp.h>
16 #include <linux/if_tunnel.h>
17 #include <linux/if_pppox.h>
18 #include <linux/ppp_defs.h>
19 #include <linux/stddef.h>
20 #include <linux/if_ether.h>
21 #include <linux/mpls.h>
22 #include <linux/tcp.h>
23 #include <net/flow_dissector.h>
24 #include <scsi/fc/fc_fcoe.h>
26 static void dissector_set_key(struct flow_dissector *flow_dissector,
27 enum flow_dissector_key_id key_id)
29 flow_dissector->used_keys |= (1 << key_id);
32 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
33 const struct flow_dissector_key *key,
34 unsigned int key_count)
36 unsigned int i;
38 memset(flow_dissector, 0, sizeof(*flow_dissector));
40 for (i = 0; i < key_count; i++, key++) {
41 /* User should make sure that every key target offset is withing
42 * boundaries of unsigned short.
44 BUG_ON(key->offset > USHRT_MAX);
45 BUG_ON(dissector_uses_key(flow_dissector,
46 key->key_id));
48 dissector_set_key(flow_dissector, key->key_id);
49 flow_dissector->offset[key->key_id] = key->offset;
52 /* Ensure that the dissector always includes control and basic key.
53 * That way we are able to avoid handling lack of these in fast path.
55 BUG_ON(!dissector_uses_key(flow_dissector,
56 FLOW_DISSECTOR_KEY_CONTROL));
57 BUG_ON(!dissector_uses_key(flow_dissector,
58 FLOW_DISSECTOR_KEY_BASIC));
60 EXPORT_SYMBOL(skb_flow_dissector_init);
62 /**
63 * skb_flow_get_be16 - extract be16 entity
64 * @skb: sk_buff to extract from
65 * @poff: offset to extract at
66 * @data: raw buffer pointer to the packet
67 * @hlen: packet header length
69 * The function will try to retrieve a be32 entity at
70 * offset poff
72 static __be16 skb_flow_get_be16(const struct sk_buff *skb, int poff,
73 void *data, int hlen)
75 __be16 *u, _u;
77 u = __skb_header_pointer(skb, poff, sizeof(_u), data, hlen, &_u);
78 if (u)
79 return *u;
81 return 0;
84 /**
85 * __skb_flow_get_ports - extract the upper layer ports and return them
86 * @skb: sk_buff to extract the ports from
87 * @thoff: transport header offset
88 * @ip_proto: protocol for which to get port offset
89 * @data: raw buffer pointer to the packet, if NULL use skb->data
90 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
92 * The function will try to retrieve the ports at offset thoff + poff where poff
93 * is the protocol port offset returned from proto_ports_offset
95 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
96 void *data, int hlen)
98 int poff = proto_ports_offset(ip_proto);
100 if (!data) {
101 data = skb->data;
102 hlen = skb_headlen(skb);
105 if (poff >= 0) {
106 __be32 *ports, _ports;
108 ports = __skb_header_pointer(skb, thoff + poff,
109 sizeof(_ports), data, hlen, &_ports);
110 if (ports)
111 return *ports;
114 return 0;
116 EXPORT_SYMBOL(__skb_flow_get_ports);
118 static enum flow_dissect_ret
119 __skb_flow_dissect_mpls(const struct sk_buff *skb,
120 struct flow_dissector *flow_dissector,
121 void *target_container, void *data, int nhoff, int hlen)
123 struct flow_dissector_key_keyid *key_keyid;
124 struct mpls_label *hdr, _hdr[2];
125 u32 entry, label;
127 if (!dissector_uses_key(flow_dissector,
128 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
129 !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
130 return FLOW_DISSECT_RET_OUT_GOOD;
132 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
133 hlen, &_hdr);
134 if (!hdr)
135 return FLOW_DISSECT_RET_OUT_BAD;
137 entry = ntohl(hdr[0].entry);
138 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
140 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
141 struct flow_dissector_key_mpls *key_mpls;
143 key_mpls = skb_flow_dissector_target(flow_dissector,
144 FLOW_DISSECTOR_KEY_MPLS,
145 target_container);
146 key_mpls->mpls_label = label;
147 key_mpls->mpls_ttl = (entry & MPLS_LS_TTL_MASK)
148 >> MPLS_LS_TTL_SHIFT;
149 key_mpls->mpls_tc = (entry & MPLS_LS_TC_MASK)
150 >> MPLS_LS_TC_SHIFT;
151 key_mpls->mpls_bos = (entry & MPLS_LS_S_MASK)
152 >> MPLS_LS_S_SHIFT;
155 if (label == MPLS_LABEL_ENTROPY) {
156 key_keyid = skb_flow_dissector_target(flow_dissector,
157 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
158 target_container);
159 key_keyid->keyid = hdr[1].entry & htonl(MPLS_LS_LABEL_MASK);
161 return FLOW_DISSECT_RET_OUT_GOOD;
164 static enum flow_dissect_ret
165 __skb_flow_dissect_arp(const struct sk_buff *skb,
166 struct flow_dissector *flow_dissector,
167 void *target_container, void *data, int nhoff, int hlen)
169 struct flow_dissector_key_arp *key_arp;
170 struct {
171 unsigned char ar_sha[ETH_ALEN];
172 unsigned char ar_sip[4];
173 unsigned char ar_tha[ETH_ALEN];
174 unsigned char ar_tip[4];
175 } *arp_eth, _arp_eth;
176 const struct arphdr *arp;
177 struct arphdr _arp;
179 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
180 return FLOW_DISSECT_RET_OUT_GOOD;
182 arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
183 hlen, &_arp);
184 if (!arp)
185 return FLOW_DISSECT_RET_OUT_BAD;
187 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
188 arp->ar_pro != htons(ETH_P_IP) ||
189 arp->ar_hln != ETH_ALEN ||
190 arp->ar_pln != 4 ||
191 (arp->ar_op != htons(ARPOP_REPLY) &&
192 arp->ar_op != htons(ARPOP_REQUEST)))
193 return FLOW_DISSECT_RET_OUT_BAD;
195 arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
196 sizeof(_arp_eth), data,
197 hlen, &_arp_eth);
198 if (!arp_eth)
199 return FLOW_DISSECT_RET_OUT_BAD;
201 key_arp = skb_flow_dissector_target(flow_dissector,
202 FLOW_DISSECTOR_KEY_ARP,
203 target_container);
205 memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
206 memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
208 /* Only store the lower byte of the opcode;
209 * this covers ARPOP_REPLY and ARPOP_REQUEST.
211 key_arp->op = ntohs(arp->ar_op) & 0xff;
213 ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
214 ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
216 return FLOW_DISSECT_RET_OUT_GOOD;
219 static enum flow_dissect_ret
220 __skb_flow_dissect_gre(const struct sk_buff *skb,
221 struct flow_dissector_key_control *key_control,
222 struct flow_dissector *flow_dissector,
223 void *target_container, void *data,
224 __be16 *p_proto, int *p_nhoff, int *p_hlen,
225 unsigned int flags)
227 struct flow_dissector_key_keyid *key_keyid;
228 struct gre_base_hdr *hdr, _hdr;
229 int offset = 0;
230 u16 gre_ver;
232 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
233 data, *p_hlen, &_hdr);
234 if (!hdr)
235 return FLOW_DISSECT_RET_OUT_BAD;
237 /* Only look inside GRE without routing */
238 if (hdr->flags & GRE_ROUTING)
239 return FLOW_DISSECT_RET_OUT_GOOD;
241 /* Only look inside GRE for version 0 and 1 */
242 gre_ver = ntohs(hdr->flags & GRE_VERSION);
243 if (gre_ver > 1)
244 return FLOW_DISSECT_RET_OUT_GOOD;
246 *p_proto = hdr->protocol;
247 if (gre_ver) {
248 /* Version1 must be PPTP, and check the flags */
249 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
250 return FLOW_DISSECT_RET_OUT_GOOD;
253 offset += sizeof(struct gre_base_hdr);
255 if (hdr->flags & GRE_CSUM)
256 offset += sizeof(((struct gre_full_hdr *) 0)->csum) +
257 sizeof(((struct gre_full_hdr *) 0)->reserved1);
259 if (hdr->flags & GRE_KEY) {
260 const __be32 *keyid;
261 __be32 _keyid;
263 keyid = __skb_header_pointer(skb, *p_nhoff + offset,
264 sizeof(_keyid),
265 data, *p_hlen, &_keyid);
266 if (!keyid)
267 return FLOW_DISSECT_RET_OUT_BAD;
269 if (dissector_uses_key(flow_dissector,
270 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
271 key_keyid = skb_flow_dissector_target(flow_dissector,
272 FLOW_DISSECTOR_KEY_GRE_KEYID,
273 target_container);
274 if (gre_ver == 0)
275 key_keyid->keyid = *keyid;
276 else
277 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
279 offset += sizeof(((struct gre_full_hdr *) 0)->key);
282 if (hdr->flags & GRE_SEQ)
283 offset += sizeof(((struct pptp_gre_header *) 0)->seq);
285 if (gre_ver == 0) {
286 if (*p_proto == htons(ETH_P_TEB)) {
287 const struct ethhdr *eth;
288 struct ethhdr _eth;
290 eth = __skb_header_pointer(skb, *p_nhoff + offset,
291 sizeof(_eth),
292 data, *p_hlen, &_eth);
293 if (!eth)
294 return FLOW_DISSECT_RET_OUT_BAD;
295 *p_proto = eth->h_proto;
296 offset += sizeof(*eth);
298 /* Cap headers that we access via pointers at the
299 * end of the Ethernet header as our maximum alignment
300 * at that point is only 2 bytes.
302 if (NET_IP_ALIGN)
303 *p_hlen = *p_nhoff + offset;
305 } else { /* version 1, must be PPTP */
306 u8 _ppp_hdr[PPP_HDRLEN];
307 u8 *ppp_hdr;
309 if (hdr->flags & GRE_ACK)
310 offset += sizeof(((struct pptp_gre_header *) 0)->ack);
312 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
313 sizeof(_ppp_hdr),
314 data, *p_hlen, _ppp_hdr);
315 if (!ppp_hdr)
316 return FLOW_DISSECT_RET_OUT_BAD;
318 switch (PPP_PROTOCOL(ppp_hdr)) {
319 case PPP_IP:
320 *p_proto = htons(ETH_P_IP);
321 break;
322 case PPP_IPV6:
323 *p_proto = htons(ETH_P_IPV6);
324 break;
325 default:
326 /* Could probably catch some more like MPLS */
327 break;
330 offset += PPP_HDRLEN;
333 *p_nhoff += offset;
334 key_control->flags |= FLOW_DIS_ENCAPSULATION;
335 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
336 return FLOW_DISSECT_RET_OUT_GOOD;
338 return FLOW_DISSECT_RET_PROTO_AGAIN;
341 static void
342 __skb_flow_dissect_tcp(const struct sk_buff *skb,
343 struct flow_dissector *flow_dissector,
344 void *target_container, void *data, int thoff, int hlen)
346 struct flow_dissector_key_tcp *key_tcp;
347 struct tcphdr *th, _th;
349 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
350 return;
352 th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
353 if (!th)
354 return;
356 if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
357 return;
359 key_tcp = skb_flow_dissector_target(flow_dissector,
360 FLOW_DISSECTOR_KEY_TCP,
361 target_container);
362 key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
365 static void
366 __skb_flow_dissect_ipv4(const struct sk_buff *skb,
367 struct flow_dissector *flow_dissector,
368 void *target_container, void *data, const struct iphdr *iph)
370 struct flow_dissector_key_ip *key_ip;
372 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
373 return;
375 key_ip = skb_flow_dissector_target(flow_dissector,
376 FLOW_DISSECTOR_KEY_IP,
377 target_container);
378 key_ip->tos = iph->tos;
379 key_ip->ttl = iph->ttl;
382 static void
383 __skb_flow_dissect_ipv6(const struct sk_buff *skb,
384 struct flow_dissector *flow_dissector,
385 void *target_container, void *data, const struct ipv6hdr *iph)
387 struct flow_dissector_key_ip *key_ip;
389 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
390 return;
392 key_ip = skb_flow_dissector_target(flow_dissector,
393 FLOW_DISSECTOR_KEY_IP,
394 target_container);
395 key_ip->tos = ipv6_get_dsfield(iph);
396 key_ip->ttl = iph->hop_limit;
399 /* Maximum number of protocol headers that can be parsed in
400 * __skb_flow_dissect
402 #define MAX_FLOW_DISSECT_HDRS 15
404 static bool skb_flow_dissect_allowed(int *num_hdrs)
406 ++*num_hdrs;
408 return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
412 * __skb_flow_dissect - extract the flow_keys struct and return it
413 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
414 * @flow_dissector: list of keys to dissect
415 * @target_container: target structure to put dissected values into
416 * @data: raw buffer pointer to the packet, if NULL use skb->data
417 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
418 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
419 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
421 * The function will try to retrieve individual keys into target specified
422 * by flow_dissector from either the skbuff or a raw buffer specified by the
423 * rest parameters.
425 * Caller must take care of zeroing target container memory.
427 bool __skb_flow_dissect(const struct sk_buff *skb,
428 struct flow_dissector *flow_dissector,
429 void *target_container,
430 void *data, __be16 proto, int nhoff, int hlen,
431 unsigned int flags)
433 struct flow_dissector_key_control *key_control;
434 struct flow_dissector_key_basic *key_basic;
435 struct flow_dissector_key_addrs *key_addrs;
436 struct flow_dissector_key_ports *key_ports;
437 struct flow_dissector_key_icmp *key_icmp;
438 struct flow_dissector_key_tags *key_tags;
439 struct flow_dissector_key_vlan *key_vlan;
440 enum flow_dissect_ret fdret;
441 bool skip_vlan = false;
442 int num_hdrs = 0;
443 u8 ip_proto = 0;
444 bool ret;
446 if (!data) {
447 data = skb->data;
448 proto = skb_vlan_tag_present(skb) ?
449 skb->vlan_proto : skb->protocol;
450 nhoff = skb_network_offset(skb);
451 hlen = skb_headlen(skb);
452 #if IS_ENABLED(CONFIG_NET_DSA)
453 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev))) {
454 const struct dsa_device_ops *ops;
455 int offset;
457 ops = skb->dev->dsa_ptr->tag_ops;
458 if (ops->flow_dissect &&
459 !ops->flow_dissect(skb, &proto, &offset)) {
460 hlen -= offset;
461 nhoff += offset;
464 #endif
467 /* It is ensured by skb_flow_dissector_init() that control key will
468 * be always present.
470 key_control = skb_flow_dissector_target(flow_dissector,
471 FLOW_DISSECTOR_KEY_CONTROL,
472 target_container);
474 /* It is ensured by skb_flow_dissector_init() that basic key will
475 * be always present.
477 key_basic = skb_flow_dissector_target(flow_dissector,
478 FLOW_DISSECTOR_KEY_BASIC,
479 target_container);
481 if (dissector_uses_key(flow_dissector,
482 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
483 struct ethhdr *eth = eth_hdr(skb);
484 struct flow_dissector_key_eth_addrs *key_eth_addrs;
486 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
487 FLOW_DISSECTOR_KEY_ETH_ADDRS,
488 target_container);
489 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
492 proto_again:
493 fdret = FLOW_DISSECT_RET_CONTINUE;
495 switch (proto) {
496 case htons(ETH_P_IP): {
497 const struct iphdr *iph;
498 struct iphdr _iph;
500 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
501 if (!iph || iph->ihl < 5) {
502 fdret = FLOW_DISSECT_RET_OUT_BAD;
503 break;
506 nhoff += iph->ihl * 4;
508 ip_proto = iph->protocol;
510 if (dissector_uses_key(flow_dissector,
511 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
512 key_addrs = skb_flow_dissector_target(flow_dissector,
513 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
514 target_container);
516 memcpy(&key_addrs->v4addrs, &iph->saddr,
517 sizeof(key_addrs->v4addrs));
518 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
521 if (ip_is_fragment(iph)) {
522 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
524 if (iph->frag_off & htons(IP_OFFSET)) {
525 fdret = FLOW_DISSECT_RET_OUT_GOOD;
526 break;
527 } else {
528 key_control->flags |= FLOW_DIS_FIRST_FRAG;
529 if (!(flags &
530 FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
531 fdret = FLOW_DISSECT_RET_OUT_GOOD;
532 break;
537 __skb_flow_dissect_ipv4(skb, flow_dissector,
538 target_container, data, iph);
540 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3) {
541 fdret = FLOW_DISSECT_RET_OUT_GOOD;
542 break;
545 break;
547 case htons(ETH_P_IPV6): {
548 const struct ipv6hdr *iph;
549 struct ipv6hdr _iph;
551 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
552 if (!iph) {
553 fdret = FLOW_DISSECT_RET_OUT_BAD;
554 break;
557 ip_proto = iph->nexthdr;
558 nhoff += sizeof(struct ipv6hdr);
560 if (dissector_uses_key(flow_dissector,
561 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
562 key_addrs = skb_flow_dissector_target(flow_dissector,
563 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
564 target_container);
566 memcpy(&key_addrs->v6addrs, &iph->saddr,
567 sizeof(key_addrs->v6addrs));
568 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
571 if ((dissector_uses_key(flow_dissector,
572 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
573 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
574 ip6_flowlabel(iph)) {
575 __be32 flow_label = ip6_flowlabel(iph);
577 if (dissector_uses_key(flow_dissector,
578 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
579 key_tags = skb_flow_dissector_target(flow_dissector,
580 FLOW_DISSECTOR_KEY_FLOW_LABEL,
581 target_container);
582 key_tags->flow_label = ntohl(flow_label);
584 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
585 fdret = FLOW_DISSECT_RET_OUT_GOOD;
586 break;
590 __skb_flow_dissect_ipv6(skb, flow_dissector,
591 target_container, data, iph);
593 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
594 fdret = FLOW_DISSECT_RET_OUT_GOOD;
596 break;
598 case htons(ETH_P_8021AD):
599 case htons(ETH_P_8021Q): {
600 const struct vlan_hdr *vlan;
601 struct vlan_hdr _vlan;
602 bool vlan_tag_present = skb && skb_vlan_tag_present(skb);
604 if (vlan_tag_present)
605 proto = skb->protocol;
607 if (!vlan_tag_present || eth_type_vlan(skb->protocol)) {
608 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
609 data, hlen, &_vlan);
610 if (!vlan) {
611 fdret = FLOW_DISSECT_RET_OUT_BAD;
612 break;
615 proto = vlan->h_vlan_encapsulated_proto;
616 nhoff += sizeof(*vlan);
617 if (skip_vlan) {
618 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
619 break;
623 skip_vlan = true;
624 if (dissector_uses_key(flow_dissector,
625 FLOW_DISSECTOR_KEY_VLAN)) {
626 key_vlan = skb_flow_dissector_target(flow_dissector,
627 FLOW_DISSECTOR_KEY_VLAN,
628 target_container);
630 if (vlan_tag_present) {
631 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
632 key_vlan->vlan_priority =
633 (skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT);
634 } else {
635 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
636 VLAN_VID_MASK;
637 key_vlan->vlan_priority =
638 (ntohs(vlan->h_vlan_TCI) &
639 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
643 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
644 break;
646 case htons(ETH_P_PPP_SES): {
647 struct {
648 struct pppoe_hdr hdr;
649 __be16 proto;
650 } *hdr, _hdr;
651 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
652 if (!hdr) {
653 fdret = FLOW_DISSECT_RET_OUT_BAD;
654 break;
657 proto = hdr->proto;
658 nhoff += PPPOE_SES_HLEN;
659 switch (proto) {
660 case htons(PPP_IP):
661 proto = htons(ETH_P_IP);
662 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
663 break;
664 case htons(PPP_IPV6):
665 proto = htons(ETH_P_IPV6);
666 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
667 break;
668 default:
669 fdret = FLOW_DISSECT_RET_OUT_BAD;
670 break;
672 break;
674 case htons(ETH_P_TIPC): {
675 struct {
676 __be32 pre[3];
677 __be32 srcnode;
678 } *hdr, _hdr;
679 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
680 if (!hdr) {
681 fdret = FLOW_DISSECT_RET_OUT_BAD;
682 break;
685 if (dissector_uses_key(flow_dissector,
686 FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
687 key_addrs = skb_flow_dissector_target(flow_dissector,
688 FLOW_DISSECTOR_KEY_TIPC_ADDRS,
689 target_container);
690 key_addrs->tipcaddrs.srcnode = hdr->srcnode;
691 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
693 fdret = FLOW_DISSECT_RET_OUT_GOOD;
694 break;
697 case htons(ETH_P_MPLS_UC):
698 case htons(ETH_P_MPLS_MC):
699 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
700 target_container, data,
701 nhoff, hlen);
702 break;
703 case htons(ETH_P_FCOE):
704 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
705 fdret = FLOW_DISSECT_RET_OUT_BAD;
706 break;
709 nhoff += FCOE_HEADER_LEN;
710 fdret = FLOW_DISSECT_RET_OUT_GOOD;
711 break;
713 case htons(ETH_P_ARP):
714 case htons(ETH_P_RARP):
715 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
716 target_container, data,
717 nhoff, hlen);
718 break;
720 default:
721 fdret = FLOW_DISSECT_RET_OUT_BAD;
722 break;
725 /* Process result of proto processing */
726 switch (fdret) {
727 case FLOW_DISSECT_RET_OUT_GOOD:
728 goto out_good;
729 case FLOW_DISSECT_RET_PROTO_AGAIN:
730 if (skb_flow_dissect_allowed(&num_hdrs))
731 goto proto_again;
732 goto out_good;
733 case FLOW_DISSECT_RET_CONTINUE:
734 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
735 break;
736 case FLOW_DISSECT_RET_OUT_BAD:
737 default:
738 goto out_bad;
741 ip_proto_again:
742 fdret = FLOW_DISSECT_RET_CONTINUE;
744 switch (ip_proto) {
745 case IPPROTO_GRE:
746 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
747 target_container, data,
748 &proto, &nhoff, &hlen, flags);
749 break;
751 case NEXTHDR_HOP:
752 case NEXTHDR_ROUTING:
753 case NEXTHDR_DEST: {
754 u8 _opthdr[2], *opthdr;
756 if (proto != htons(ETH_P_IPV6))
757 break;
759 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
760 data, hlen, &_opthdr);
761 if (!opthdr) {
762 fdret = FLOW_DISSECT_RET_OUT_BAD;
763 break;
766 ip_proto = opthdr[0];
767 nhoff += (opthdr[1] + 1) << 3;
769 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
770 break;
772 case NEXTHDR_FRAGMENT: {
773 struct frag_hdr _fh, *fh;
775 if (proto != htons(ETH_P_IPV6))
776 break;
778 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
779 data, hlen, &_fh);
781 if (!fh) {
782 fdret = FLOW_DISSECT_RET_OUT_BAD;
783 break;
786 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
788 nhoff += sizeof(_fh);
789 ip_proto = fh->nexthdr;
791 if (!(fh->frag_off & htons(IP6_OFFSET))) {
792 key_control->flags |= FLOW_DIS_FIRST_FRAG;
793 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
794 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
795 break;
799 fdret = FLOW_DISSECT_RET_OUT_GOOD;
800 break;
802 case IPPROTO_IPIP:
803 proto = htons(ETH_P_IP);
805 key_control->flags |= FLOW_DIS_ENCAPSULATION;
806 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
807 fdret = FLOW_DISSECT_RET_OUT_GOOD;
808 break;
811 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
812 break;
814 case IPPROTO_IPV6:
815 proto = htons(ETH_P_IPV6);
817 key_control->flags |= FLOW_DIS_ENCAPSULATION;
818 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
819 fdret = FLOW_DISSECT_RET_OUT_GOOD;
820 break;
823 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
824 break;
827 case IPPROTO_MPLS:
828 proto = htons(ETH_P_MPLS_UC);
829 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
830 break;
832 case IPPROTO_TCP:
833 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
834 data, nhoff, hlen);
835 break;
837 default:
838 break;
841 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS) &&
842 !(key_control->flags & FLOW_DIS_IS_FRAGMENT)) {
843 key_ports = skb_flow_dissector_target(flow_dissector,
844 FLOW_DISSECTOR_KEY_PORTS,
845 target_container);
846 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
847 data, hlen);
850 if (dissector_uses_key(flow_dissector,
851 FLOW_DISSECTOR_KEY_ICMP)) {
852 key_icmp = skb_flow_dissector_target(flow_dissector,
853 FLOW_DISSECTOR_KEY_ICMP,
854 target_container);
855 key_icmp->icmp = skb_flow_get_be16(skb, nhoff, data, hlen);
858 /* Process result of IP proto processing */
859 switch (fdret) {
860 case FLOW_DISSECT_RET_PROTO_AGAIN:
861 if (skb_flow_dissect_allowed(&num_hdrs))
862 goto proto_again;
863 break;
864 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
865 if (skb_flow_dissect_allowed(&num_hdrs))
866 goto ip_proto_again;
867 break;
868 case FLOW_DISSECT_RET_OUT_GOOD:
869 case FLOW_DISSECT_RET_CONTINUE:
870 break;
871 case FLOW_DISSECT_RET_OUT_BAD:
872 default:
873 goto out_bad;
876 out_good:
877 ret = true;
879 out:
880 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
881 key_basic->n_proto = proto;
882 key_basic->ip_proto = ip_proto;
884 return ret;
886 out_bad:
887 ret = false;
888 goto out;
890 EXPORT_SYMBOL(__skb_flow_dissect);
892 static u32 hashrnd __read_mostly;
893 static __always_inline void __flow_hash_secret_init(void)
895 net_get_random_once(&hashrnd, sizeof(hashrnd));
898 static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
899 u32 keyval)
901 return jhash2(words, length, keyval);
904 static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
906 const void *p = flow;
908 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
909 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
912 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
914 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
915 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
916 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
917 sizeof(*flow) - sizeof(flow->addrs));
919 switch (flow->control.addr_type) {
920 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
921 diff -= sizeof(flow->addrs.v4addrs);
922 break;
923 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
924 diff -= sizeof(flow->addrs.v6addrs);
925 break;
926 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
927 diff -= sizeof(flow->addrs.tipcaddrs);
928 break;
930 return (sizeof(*flow) - diff) / sizeof(u32);
933 __be32 flow_get_u32_src(const struct flow_keys *flow)
935 switch (flow->control.addr_type) {
936 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
937 return flow->addrs.v4addrs.src;
938 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
939 return (__force __be32)ipv6_addr_hash(
940 &flow->addrs.v6addrs.src);
941 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
942 return flow->addrs.tipcaddrs.srcnode;
943 default:
944 return 0;
947 EXPORT_SYMBOL(flow_get_u32_src);
949 __be32 flow_get_u32_dst(const struct flow_keys *flow)
951 switch (flow->control.addr_type) {
952 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
953 return flow->addrs.v4addrs.dst;
954 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
955 return (__force __be32)ipv6_addr_hash(
956 &flow->addrs.v6addrs.dst);
957 default:
958 return 0;
961 EXPORT_SYMBOL(flow_get_u32_dst);
963 static inline void __flow_hash_consistentify(struct flow_keys *keys)
965 int addr_diff, i;
967 switch (keys->control.addr_type) {
968 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
969 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
970 (__force u32)keys->addrs.v4addrs.src;
971 if ((addr_diff < 0) ||
972 (addr_diff == 0 &&
973 ((__force u16)keys->ports.dst <
974 (__force u16)keys->ports.src))) {
975 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
976 swap(keys->ports.src, keys->ports.dst);
978 break;
979 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
980 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
981 &keys->addrs.v6addrs.src,
982 sizeof(keys->addrs.v6addrs.dst));
983 if ((addr_diff < 0) ||
984 (addr_diff == 0 &&
985 ((__force u16)keys->ports.dst <
986 (__force u16)keys->ports.src))) {
987 for (i = 0; i < 4; i++)
988 swap(keys->addrs.v6addrs.src.s6_addr32[i],
989 keys->addrs.v6addrs.dst.s6_addr32[i]);
990 swap(keys->ports.src, keys->ports.dst);
992 break;
996 static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
998 u32 hash;
1000 __flow_hash_consistentify(keys);
1002 hash = __flow_hash_words(flow_keys_hash_start(keys),
1003 flow_keys_hash_length(keys), keyval);
1004 if (!hash)
1005 hash = 1;
1007 return hash;
1010 u32 flow_hash_from_keys(struct flow_keys *keys)
1012 __flow_hash_secret_init();
1013 return __flow_hash_from_keys(keys, hashrnd);
1015 EXPORT_SYMBOL(flow_hash_from_keys);
1017 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1018 struct flow_keys *keys, u32 keyval)
1020 skb_flow_dissect_flow_keys(skb, keys,
1021 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1023 return __flow_hash_from_keys(keys, keyval);
1026 struct _flow_keys_digest_data {
1027 __be16 n_proto;
1028 u8 ip_proto;
1029 u8 padding;
1030 __be32 ports;
1031 __be32 src;
1032 __be32 dst;
1035 void make_flow_keys_digest(struct flow_keys_digest *digest,
1036 const struct flow_keys *flow)
1038 struct _flow_keys_digest_data *data =
1039 (struct _flow_keys_digest_data *)digest;
1041 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1043 memset(digest, 0, sizeof(*digest));
1045 data->n_proto = flow->basic.n_proto;
1046 data->ip_proto = flow->basic.ip_proto;
1047 data->ports = flow->ports.ports;
1048 data->src = flow->addrs.v4addrs.src;
1049 data->dst = flow->addrs.v4addrs.dst;
1051 EXPORT_SYMBOL(make_flow_keys_digest);
1053 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1055 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1057 struct flow_keys keys;
1059 __flow_hash_secret_init();
1061 memset(&keys, 0, sizeof(keys));
1062 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
1063 NULL, 0, 0, 0,
1064 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1066 return __flow_hash_from_keys(&keys, hashrnd);
1068 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1071 * __skb_get_hash: calculate a flow hash
1072 * @skb: sk_buff to calculate flow hash from
1074 * This function calculates a flow hash based on src/dst addresses
1075 * and src/dst port numbers. Sets hash in skb to non-zero hash value
1076 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
1077 * if hash is a canonical 4-tuple hash over transport ports.
1079 void __skb_get_hash(struct sk_buff *skb)
1081 struct flow_keys keys;
1082 u32 hash;
1084 __flow_hash_secret_init();
1086 hash = ___skb_get_hash(skb, &keys, hashrnd);
1088 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1090 EXPORT_SYMBOL(__skb_get_hash);
1092 __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
1094 struct flow_keys keys;
1096 return ___skb_get_hash(skb, &keys, perturb);
1098 EXPORT_SYMBOL(skb_get_hash_perturb);
1100 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
1101 const struct flow_keys *keys, int hlen)
1103 u32 poff = keys->control.thoff;
1105 /* skip L4 headers for fragments after the first */
1106 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1107 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1108 return poff;
1110 switch (keys->basic.ip_proto) {
1111 case IPPROTO_TCP: {
1112 /* access doff as u8 to avoid unaligned access */
1113 const u8 *doff;
1114 u8 _doff;
1116 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1117 data, hlen, &_doff);
1118 if (!doff)
1119 return poff;
1121 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1122 break;
1124 case IPPROTO_UDP:
1125 case IPPROTO_UDPLITE:
1126 poff += sizeof(struct udphdr);
1127 break;
1128 /* For the rest, we do not really care about header
1129 * extensions at this point for now.
1131 case IPPROTO_ICMP:
1132 poff += sizeof(struct icmphdr);
1133 break;
1134 case IPPROTO_ICMPV6:
1135 poff += sizeof(struct icmp6hdr);
1136 break;
1137 case IPPROTO_IGMP:
1138 poff += sizeof(struct igmphdr);
1139 break;
1140 case IPPROTO_DCCP:
1141 poff += sizeof(struct dccp_hdr);
1142 break;
1143 case IPPROTO_SCTP:
1144 poff += sizeof(struct sctphdr);
1145 break;
1148 return poff;
1152 * skb_get_poff - get the offset to the payload
1153 * @skb: sk_buff to get the payload offset from
1155 * The function will get the offset to the payload as far as it could
1156 * be dissected. The main user is currently BPF, so that we can dynamically
1157 * truncate packets without needing to push actual payload to the user
1158 * space and can analyze headers only, instead.
1160 u32 skb_get_poff(const struct sk_buff *skb)
1162 struct flow_keys keys;
1164 if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
1165 return 0;
1167 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1170 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1172 memset(keys, 0, sizeof(*keys));
1174 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1175 sizeof(keys->addrs.v6addrs.src));
1176 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1177 sizeof(keys->addrs.v6addrs.dst));
1178 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1179 keys->ports.src = fl6->fl6_sport;
1180 keys->ports.dst = fl6->fl6_dport;
1181 keys->keyid.keyid = fl6->fl6_gre_key;
1182 keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1183 keys->basic.ip_proto = fl6->flowi6_proto;
1185 return flow_hash_from_keys(keys);
1187 EXPORT_SYMBOL(__get_hash_from_flowi6);
1189 __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
1191 memset(keys, 0, sizeof(*keys));
1193 keys->addrs.v4addrs.src = fl4->saddr;
1194 keys->addrs.v4addrs.dst = fl4->daddr;
1195 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1196 keys->ports.src = fl4->fl4_sport;
1197 keys->ports.dst = fl4->fl4_dport;
1198 keys->keyid.keyid = fl4->fl4_gre_key;
1199 keys->basic.ip_proto = fl4->flowi4_proto;
1201 return flow_hash_from_keys(keys);
1203 EXPORT_SYMBOL(__get_hash_from_flowi4);
1205 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1207 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1208 .offset = offsetof(struct flow_keys, control),
1211 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1212 .offset = offsetof(struct flow_keys, basic),
1215 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1216 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1219 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1220 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1223 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
1224 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
1227 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1228 .offset = offsetof(struct flow_keys, ports),
1231 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1232 .offset = offsetof(struct flow_keys, vlan),
1235 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1236 .offset = offsetof(struct flow_keys, tags),
1239 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1240 .offset = offsetof(struct flow_keys, keyid),
1244 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1246 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1247 .offset = offsetof(struct flow_keys, control),
1250 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1251 .offset = offsetof(struct flow_keys, basic),
1254 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1255 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1258 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1259 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1262 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1263 .offset = offsetof(struct flow_keys, ports),
1267 static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
1269 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1270 .offset = offsetof(struct flow_keys, control),
1273 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1274 .offset = offsetof(struct flow_keys, basic),
1278 struct flow_dissector flow_keys_dissector __read_mostly;
1279 EXPORT_SYMBOL(flow_keys_dissector);
1281 struct flow_dissector flow_keys_buf_dissector __read_mostly;
1283 static int __init init_default_flow_dissectors(void)
1285 skb_flow_dissector_init(&flow_keys_dissector,
1286 flow_keys_dissector_keys,
1287 ARRAY_SIZE(flow_keys_dissector_keys));
1288 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1289 flow_keys_dissector_symmetric_keys,
1290 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1291 skb_flow_dissector_init(&flow_keys_buf_dissector,
1292 flow_keys_buf_dissector_keys,
1293 ARRAY_SIZE(flow_keys_buf_dissector_keys));
1294 return 0;
1297 core_initcall(init_default_flow_dissectors);