1 #include <linux/kernel.h>
2 #include <linux/skbuff.h>
3 #include <linux/export.h>
5 #include <linux/ipv6.h>
6 #include <linux/if_vlan.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)
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,
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);
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
72 static __be16 skb_flow_get_be16(const struct sk_buff *skb, int poff,
77 u = __skb_header_pointer(skb, poff, sizeof(_u), data, hlen, &_u);
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,
98 int poff = proto_ports_offset(ip_proto);
102 hlen = skb_headlen(skb);
106 __be32 *ports, _ports;
108 ports = __skb_header_pointer(skb, thoff + poff,
109 sizeof(_ports), data, hlen, &_ports);
116 EXPORT_SYMBOL(__skb_flow_get_ports);
118 enum flow_dissect_ret {
119 FLOW_DISSECT_RET_OUT_GOOD,
120 FLOW_DISSECT_RET_OUT_BAD,
121 FLOW_DISSECT_RET_OUT_PROTO_AGAIN,
124 static enum flow_dissect_ret
125 __skb_flow_dissect_mpls(const struct sk_buff *skb,
126 struct flow_dissector *flow_dissector,
127 void *target_container, void *data, int nhoff, int hlen)
129 struct flow_dissector_key_keyid *key_keyid;
130 struct mpls_label *hdr, _hdr[2];
133 if (!dissector_uses_key(flow_dissector,
134 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
135 !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
136 return FLOW_DISSECT_RET_OUT_GOOD;
138 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
141 return FLOW_DISSECT_RET_OUT_BAD;
143 entry = ntohl(hdr[0].entry);
144 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
146 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
147 struct flow_dissector_key_mpls *key_mpls;
149 key_mpls = skb_flow_dissector_target(flow_dissector,
150 FLOW_DISSECTOR_KEY_MPLS,
152 key_mpls->mpls_label = label;
153 key_mpls->mpls_ttl = (entry & MPLS_LS_TTL_MASK)
154 >> MPLS_LS_TTL_SHIFT;
155 key_mpls->mpls_tc = (entry & MPLS_LS_TC_MASK)
157 key_mpls->mpls_bos = (entry & MPLS_LS_S_MASK)
161 if (label == MPLS_LABEL_ENTROPY) {
162 key_keyid = skb_flow_dissector_target(flow_dissector,
163 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
165 key_keyid->keyid = hdr[1].entry & htonl(MPLS_LS_LABEL_MASK);
167 return FLOW_DISSECT_RET_OUT_GOOD;
170 static enum flow_dissect_ret
171 __skb_flow_dissect_arp(const struct sk_buff *skb,
172 struct flow_dissector *flow_dissector,
173 void *target_container, void *data, int nhoff, int hlen)
175 struct flow_dissector_key_arp *key_arp;
177 unsigned char ar_sha[ETH_ALEN];
178 unsigned char ar_sip[4];
179 unsigned char ar_tha[ETH_ALEN];
180 unsigned char ar_tip[4];
181 } *arp_eth, _arp_eth;
182 const struct arphdr *arp;
185 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
186 return FLOW_DISSECT_RET_OUT_GOOD;
188 arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
191 return FLOW_DISSECT_RET_OUT_BAD;
193 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
194 arp->ar_pro != htons(ETH_P_IP) ||
195 arp->ar_hln != ETH_ALEN ||
197 (arp->ar_op != htons(ARPOP_REPLY) &&
198 arp->ar_op != htons(ARPOP_REQUEST)))
199 return FLOW_DISSECT_RET_OUT_BAD;
201 arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
202 sizeof(_arp_eth), data,
205 return FLOW_DISSECT_RET_OUT_BAD;
207 key_arp = skb_flow_dissector_target(flow_dissector,
208 FLOW_DISSECTOR_KEY_ARP,
211 memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
212 memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
214 /* Only store the lower byte of the opcode;
215 * this covers ARPOP_REPLY and ARPOP_REQUEST.
217 key_arp->op = ntohs(arp->ar_op) & 0xff;
219 ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
220 ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
222 return FLOW_DISSECT_RET_OUT_GOOD;
225 static enum flow_dissect_ret
226 __skb_flow_dissect_gre(const struct sk_buff *skb,
227 struct flow_dissector_key_control *key_control,
228 struct flow_dissector *flow_dissector,
229 void *target_container, void *data,
230 __be16 *p_proto, int *p_nhoff, int *p_hlen,
233 struct flow_dissector_key_keyid *key_keyid;
234 struct gre_base_hdr *hdr, _hdr;
238 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
239 data, *p_hlen, &_hdr);
241 return FLOW_DISSECT_RET_OUT_BAD;
243 /* Only look inside GRE without routing */
244 if (hdr->flags & GRE_ROUTING)
245 return FLOW_DISSECT_RET_OUT_GOOD;
247 /* Only look inside GRE for version 0 and 1 */
248 gre_ver = ntohs(hdr->flags & GRE_VERSION);
250 return FLOW_DISSECT_RET_OUT_GOOD;
252 *p_proto = hdr->protocol;
254 /* Version1 must be PPTP, and check the flags */
255 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
256 return FLOW_DISSECT_RET_OUT_GOOD;
259 offset += sizeof(struct gre_base_hdr);
261 if (hdr->flags & GRE_CSUM)
262 offset += sizeof(((struct gre_full_hdr *) 0)->csum) +
263 sizeof(((struct gre_full_hdr *) 0)->reserved1);
265 if (hdr->flags & GRE_KEY) {
269 keyid = __skb_header_pointer(skb, *p_nhoff + offset,
271 data, *p_hlen, &_keyid);
273 return FLOW_DISSECT_RET_OUT_BAD;
275 if (dissector_uses_key(flow_dissector,
276 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
277 key_keyid = skb_flow_dissector_target(flow_dissector,
278 FLOW_DISSECTOR_KEY_GRE_KEYID,
281 key_keyid->keyid = *keyid;
283 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
285 offset += sizeof(((struct gre_full_hdr *) 0)->key);
288 if (hdr->flags & GRE_SEQ)
289 offset += sizeof(((struct pptp_gre_header *) 0)->seq);
292 if (*p_proto == htons(ETH_P_TEB)) {
293 const struct ethhdr *eth;
296 eth = __skb_header_pointer(skb, *p_nhoff + offset,
298 data, *p_hlen, &_eth);
300 return FLOW_DISSECT_RET_OUT_BAD;
301 *p_proto = eth->h_proto;
302 offset += sizeof(*eth);
304 /* Cap headers that we access via pointers at the
305 * end of the Ethernet header as our maximum alignment
306 * at that point is only 2 bytes.
309 *p_hlen = *p_nhoff + offset;
311 } else { /* version 1, must be PPTP */
312 u8 _ppp_hdr[PPP_HDRLEN];
315 if (hdr->flags & GRE_ACK)
316 offset += sizeof(((struct pptp_gre_header *) 0)->ack);
318 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
320 data, *p_hlen, _ppp_hdr);
322 return FLOW_DISSECT_RET_OUT_BAD;
324 switch (PPP_PROTOCOL(ppp_hdr)) {
326 *p_proto = htons(ETH_P_IP);
329 *p_proto = htons(ETH_P_IPV6);
332 /* Could probably catch some more like MPLS */
336 offset += PPP_HDRLEN;
340 key_control->flags |= FLOW_DIS_ENCAPSULATION;
341 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
342 return FLOW_DISSECT_RET_OUT_GOOD;
344 return FLOW_DISSECT_RET_OUT_PROTO_AGAIN;
348 __skb_flow_dissect_tcp(const struct sk_buff *skb,
349 struct flow_dissector *flow_dissector,
350 void *target_container, void *data, int thoff, int hlen)
352 struct flow_dissector_key_tcp *key_tcp;
353 struct tcphdr *th, _th;
355 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
358 th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
362 if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
365 key_tcp = skb_flow_dissector_target(flow_dissector,
366 FLOW_DISSECTOR_KEY_TCP,
368 key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
372 __skb_flow_dissect_ipv4(const struct sk_buff *skb,
373 struct flow_dissector *flow_dissector,
374 void *target_container, void *data, const struct iphdr *iph)
376 struct flow_dissector_key_ip *key_ip;
378 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
381 key_ip = skb_flow_dissector_target(flow_dissector,
382 FLOW_DISSECTOR_KEY_IP,
384 key_ip->tos = iph->tos;
385 key_ip->ttl = iph->ttl;
389 __skb_flow_dissect_ipv6(const struct sk_buff *skb,
390 struct flow_dissector *flow_dissector,
391 void *target_container, void *data, const struct ipv6hdr *iph)
393 struct flow_dissector_key_ip *key_ip;
395 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
398 key_ip = skb_flow_dissector_target(flow_dissector,
399 FLOW_DISSECTOR_KEY_IP,
401 key_ip->tos = ipv6_get_dsfield(iph);
402 key_ip->ttl = iph->hop_limit;
406 * __skb_flow_dissect - extract the flow_keys struct and return it
407 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
408 * @flow_dissector: list of keys to dissect
409 * @target_container: target structure to put dissected values into
410 * @data: raw buffer pointer to the packet, if NULL use skb->data
411 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
412 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
413 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
415 * The function will try to retrieve individual keys into target specified
416 * by flow_dissector from either the skbuff or a raw buffer specified by the
419 * Caller must take care of zeroing target container memory.
421 bool __skb_flow_dissect(const struct sk_buff *skb,
422 struct flow_dissector *flow_dissector,
423 void *target_container,
424 void *data, __be16 proto, int nhoff, int hlen,
427 struct flow_dissector_key_control *key_control;
428 struct flow_dissector_key_basic *key_basic;
429 struct flow_dissector_key_addrs *key_addrs;
430 struct flow_dissector_key_ports *key_ports;
431 struct flow_dissector_key_icmp *key_icmp;
432 struct flow_dissector_key_tags *key_tags;
433 struct flow_dissector_key_vlan *key_vlan;
434 bool skip_vlan = false;
440 proto = skb_vlan_tag_present(skb) ?
441 skb->vlan_proto : skb->protocol;
442 nhoff = skb_network_offset(skb);
443 hlen = skb_headlen(skb);
444 #if IS_ENABLED(CONFIG_NET_DSA)
445 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev))) {
446 const struct dsa_device_ops *ops;
449 ops = skb->dev->dsa_ptr->tag_ops;
450 if (ops->flow_dissect &&
451 !ops->flow_dissect(skb, &proto, &offset)) {
459 /* It is ensured by skb_flow_dissector_init() that control key will
462 key_control = skb_flow_dissector_target(flow_dissector,
463 FLOW_DISSECTOR_KEY_CONTROL,
466 /* It is ensured by skb_flow_dissector_init() that basic key will
469 key_basic = skb_flow_dissector_target(flow_dissector,
470 FLOW_DISSECTOR_KEY_BASIC,
473 if (dissector_uses_key(flow_dissector,
474 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
475 struct ethhdr *eth = eth_hdr(skb);
476 struct flow_dissector_key_eth_addrs *key_eth_addrs;
478 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
479 FLOW_DISSECTOR_KEY_ETH_ADDRS,
481 memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs));
486 case htons(ETH_P_IP): {
487 const struct iphdr *iph;
490 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
491 if (!iph || iph->ihl < 5)
493 nhoff += iph->ihl * 4;
495 ip_proto = iph->protocol;
497 if (dissector_uses_key(flow_dissector,
498 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
499 key_addrs = skb_flow_dissector_target(flow_dissector,
500 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
503 memcpy(&key_addrs->v4addrs, &iph->saddr,
504 sizeof(key_addrs->v4addrs));
505 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
508 if (ip_is_fragment(iph)) {
509 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
511 if (iph->frag_off & htons(IP_OFFSET)) {
514 key_control->flags |= FLOW_DIS_FIRST_FRAG;
515 if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
520 __skb_flow_dissect_ipv4(skb, flow_dissector,
521 target_container, data, iph);
523 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
528 case htons(ETH_P_IPV6): {
529 const struct ipv6hdr *iph;
533 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
537 ip_proto = iph->nexthdr;
538 nhoff += sizeof(struct ipv6hdr);
540 if (dissector_uses_key(flow_dissector,
541 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
542 key_addrs = skb_flow_dissector_target(flow_dissector,
543 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
546 memcpy(&key_addrs->v6addrs, &iph->saddr,
547 sizeof(key_addrs->v6addrs));
548 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
551 if ((dissector_uses_key(flow_dissector,
552 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
553 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
554 ip6_flowlabel(iph)) {
555 __be32 flow_label = ip6_flowlabel(iph);
557 if (dissector_uses_key(flow_dissector,
558 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
559 key_tags = skb_flow_dissector_target(flow_dissector,
560 FLOW_DISSECTOR_KEY_FLOW_LABEL,
562 key_tags->flow_label = ntohl(flow_label);
564 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
568 __skb_flow_dissect_ipv6(skb, flow_dissector,
569 target_container, data, iph);
571 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
576 case htons(ETH_P_8021AD):
577 case htons(ETH_P_8021Q): {
578 const struct vlan_hdr *vlan;
579 struct vlan_hdr _vlan;
580 bool vlan_tag_present = skb && skb_vlan_tag_present(skb);
582 if (vlan_tag_present)
583 proto = skb->protocol;
585 if (!vlan_tag_present || eth_type_vlan(skb->protocol)) {
586 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
590 proto = vlan->h_vlan_encapsulated_proto;
591 nhoff += sizeof(*vlan);
597 if (dissector_uses_key(flow_dissector,
598 FLOW_DISSECTOR_KEY_VLAN)) {
599 key_vlan = skb_flow_dissector_target(flow_dissector,
600 FLOW_DISSECTOR_KEY_VLAN,
603 if (vlan_tag_present) {
604 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
605 key_vlan->vlan_priority =
606 (skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT);
608 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
610 key_vlan->vlan_priority =
611 (ntohs(vlan->h_vlan_TCI) &
612 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
618 case htons(ETH_P_PPP_SES): {
620 struct pppoe_hdr hdr;
623 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
627 nhoff += PPPOE_SES_HLEN;
631 case htons(PPP_IPV6):
637 case htons(ETH_P_TIPC): {
642 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
646 if (dissector_uses_key(flow_dissector,
647 FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
648 key_addrs = skb_flow_dissector_target(flow_dissector,
649 FLOW_DISSECTOR_KEY_TIPC_ADDRS,
651 key_addrs->tipcaddrs.srcnode = hdr->srcnode;
652 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
657 case htons(ETH_P_MPLS_UC):
658 case htons(ETH_P_MPLS_MC):
660 switch (__skb_flow_dissect_mpls(skb, flow_dissector,
661 target_container, data,
663 case FLOW_DISSECT_RET_OUT_GOOD:
665 case FLOW_DISSECT_RET_OUT_BAD:
669 case htons(ETH_P_FCOE):
670 if ((hlen - nhoff) < FCOE_HEADER_LEN)
673 nhoff += FCOE_HEADER_LEN;
676 case htons(ETH_P_ARP):
677 case htons(ETH_P_RARP):
678 switch (__skb_flow_dissect_arp(skb, flow_dissector,
679 target_container, data,
681 case FLOW_DISSECT_RET_OUT_GOOD:
683 case FLOW_DISSECT_RET_OUT_BAD:
694 switch (__skb_flow_dissect_gre(skb, key_control, flow_dissector,
695 target_container, data,
696 &proto, &nhoff, &hlen, flags)) {
697 case FLOW_DISSECT_RET_OUT_GOOD:
699 case FLOW_DISSECT_RET_OUT_BAD:
701 case FLOW_DISSECT_RET_OUT_PROTO_AGAIN:
705 case NEXTHDR_ROUTING:
707 u8 _opthdr[2], *opthdr;
709 if (proto != htons(ETH_P_IPV6))
712 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
713 data, hlen, &_opthdr);
717 ip_proto = opthdr[0];
718 nhoff += (opthdr[1] + 1) << 3;
722 case NEXTHDR_FRAGMENT: {
723 struct frag_hdr _fh, *fh;
725 if (proto != htons(ETH_P_IPV6))
728 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
734 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
736 nhoff += sizeof(_fh);
737 ip_proto = fh->nexthdr;
739 if (!(fh->frag_off & htons(IP6_OFFSET))) {
740 key_control->flags |= FLOW_DIS_FIRST_FRAG;
741 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG)
747 proto = htons(ETH_P_IP);
749 key_control->flags |= FLOW_DIS_ENCAPSULATION;
750 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
755 proto = htons(ETH_P_IPV6);
757 key_control->flags |= FLOW_DIS_ENCAPSULATION;
758 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
763 proto = htons(ETH_P_MPLS_UC);
766 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
773 if (dissector_uses_key(flow_dissector,
774 FLOW_DISSECTOR_KEY_PORTS)) {
775 key_ports = skb_flow_dissector_target(flow_dissector,
776 FLOW_DISSECTOR_KEY_PORTS,
778 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
782 if (dissector_uses_key(flow_dissector,
783 FLOW_DISSECTOR_KEY_ICMP)) {
784 key_icmp = skb_flow_dissector_target(flow_dissector,
785 FLOW_DISSECTOR_KEY_ICMP,
787 key_icmp->icmp = skb_flow_get_be16(skb, nhoff, data, hlen);
793 key_control->thoff = (u16)nhoff;
795 key_basic->n_proto = proto;
796 key_basic->ip_proto = ip_proto;
802 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
805 EXPORT_SYMBOL(__skb_flow_dissect);
807 static u32 hashrnd __read_mostly;
808 static __always_inline void __flow_hash_secret_init(void)
810 net_get_random_once(&hashrnd, sizeof(hashrnd));
813 static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
816 return jhash2(words, length, keyval);
819 static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
821 const void *p = flow;
823 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
824 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
827 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
829 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
830 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
831 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
832 sizeof(*flow) - sizeof(flow->addrs));
834 switch (flow->control.addr_type) {
835 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
836 diff -= sizeof(flow->addrs.v4addrs);
838 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
839 diff -= sizeof(flow->addrs.v6addrs);
841 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
842 diff -= sizeof(flow->addrs.tipcaddrs);
845 return (sizeof(*flow) - diff) / sizeof(u32);
848 __be32 flow_get_u32_src(const struct flow_keys *flow)
850 switch (flow->control.addr_type) {
851 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
852 return flow->addrs.v4addrs.src;
853 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
854 return (__force __be32)ipv6_addr_hash(
855 &flow->addrs.v6addrs.src);
856 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
857 return flow->addrs.tipcaddrs.srcnode;
862 EXPORT_SYMBOL(flow_get_u32_src);
864 __be32 flow_get_u32_dst(const struct flow_keys *flow)
866 switch (flow->control.addr_type) {
867 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
868 return flow->addrs.v4addrs.dst;
869 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
870 return (__force __be32)ipv6_addr_hash(
871 &flow->addrs.v6addrs.dst);
876 EXPORT_SYMBOL(flow_get_u32_dst);
878 static inline void __flow_hash_consistentify(struct flow_keys *keys)
882 switch (keys->control.addr_type) {
883 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
884 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
885 (__force u32)keys->addrs.v4addrs.src;
886 if ((addr_diff < 0) ||
888 ((__force u16)keys->ports.dst <
889 (__force u16)keys->ports.src))) {
890 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
891 swap(keys->ports.src, keys->ports.dst);
894 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
895 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
896 &keys->addrs.v6addrs.src,
897 sizeof(keys->addrs.v6addrs.dst));
898 if ((addr_diff < 0) ||
900 ((__force u16)keys->ports.dst <
901 (__force u16)keys->ports.src))) {
902 for (i = 0; i < 4; i++)
903 swap(keys->addrs.v6addrs.src.s6_addr32[i],
904 keys->addrs.v6addrs.dst.s6_addr32[i]);
905 swap(keys->ports.src, keys->ports.dst);
911 static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
915 __flow_hash_consistentify(keys);
917 hash = __flow_hash_words(flow_keys_hash_start(keys),
918 flow_keys_hash_length(keys), keyval);
925 u32 flow_hash_from_keys(struct flow_keys *keys)
927 __flow_hash_secret_init();
928 return __flow_hash_from_keys(keys, hashrnd);
930 EXPORT_SYMBOL(flow_hash_from_keys);
932 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
933 struct flow_keys *keys, u32 keyval)
935 skb_flow_dissect_flow_keys(skb, keys,
936 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
938 return __flow_hash_from_keys(keys, keyval);
941 struct _flow_keys_digest_data {
950 void make_flow_keys_digest(struct flow_keys_digest *digest,
951 const struct flow_keys *flow)
953 struct _flow_keys_digest_data *data =
954 (struct _flow_keys_digest_data *)digest;
956 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
958 memset(digest, 0, sizeof(*digest));
960 data->n_proto = flow->basic.n_proto;
961 data->ip_proto = flow->basic.ip_proto;
962 data->ports = flow->ports.ports;
963 data->src = flow->addrs.v4addrs.src;
964 data->dst = flow->addrs.v4addrs.dst;
966 EXPORT_SYMBOL(make_flow_keys_digest);
968 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
970 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
972 struct flow_keys keys;
974 __flow_hash_secret_init();
976 memset(&keys, 0, sizeof(keys));
977 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
979 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
981 return __flow_hash_from_keys(&keys, hashrnd);
983 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
986 * __skb_get_hash: calculate a flow hash
987 * @skb: sk_buff to calculate flow hash from
989 * This function calculates a flow hash based on src/dst addresses
990 * and src/dst port numbers. Sets hash in skb to non-zero hash value
991 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
992 * if hash is a canonical 4-tuple hash over transport ports.
994 void __skb_get_hash(struct sk_buff *skb)
996 struct flow_keys keys;
999 __flow_hash_secret_init();
1001 hash = ___skb_get_hash(skb, &keys, hashrnd);
1003 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1005 EXPORT_SYMBOL(__skb_get_hash);
1007 __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
1009 struct flow_keys keys;
1011 return ___skb_get_hash(skb, &keys, perturb);
1013 EXPORT_SYMBOL(skb_get_hash_perturb);
1015 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
1016 const struct flow_keys *keys, int hlen)
1018 u32 poff = keys->control.thoff;
1020 /* skip L4 headers for fragments after the first */
1021 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1022 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1025 switch (keys->basic.ip_proto) {
1027 /* access doff as u8 to avoid unaligned access */
1031 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1032 data, hlen, &_doff);
1036 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1040 case IPPROTO_UDPLITE:
1041 poff += sizeof(struct udphdr);
1043 /* For the rest, we do not really care about header
1044 * extensions at this point for now.
1047 poff += sizeof(struct icmphdr);
1049 case IPPROTO_ICMPV6:
1050 poff += sizeof(struct icmp6hdr);
1053 poff += sizeof(struct igmphdr);
1056 poff += sizeof(struct dccp_hdr);
1059 poff += sizeof(struct sctphdr);
1067 * skb_get_poff - get the offset to the payload
1068 * @skb: sk_buff to get the payload offset from
1070 * The function will get the offset to the payload as far as it could
1071 * be dissected. The main user is currently BPF, so that we can dynamically
1072 * truncate packets without needing to push actual payload to the user
1073 * space and can analyze headers only, instead.
1075 u32 skb_get_poff(const struct sk_buff *skb)
1077 struct flow_keys keys;
1079 if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
1082 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1085 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1087 memset(keys, 0, sizeof(*keys));
1089 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1090 sizeof(keys->addrs.v6addrs.src));
1091 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1092 sizeof(keys->addrs.v6addrs.dst));
1093 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1094 keys->ports.src = fl6->fl6_sport;
1095 keys->ports.dst = fl6->fl6_dport;
1096 keys->keyid.keyid = fl6->fl6_gre_key;
1097 keys->tags.flow_label = (__force u32)fl6->flowlabel;
1098 keys->basic.ip_proto = fl6->flowi6_proto;
1100 return flow_hash_from_keys(keys);
1102 EXPORT_SYMBOL(__get_hash_from_flowi6);
1104 __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
1106 memset(keys, 0, sizeof(*keys));
1108 keys->addrs.v4addrs.src = fl4->saddr;
1109 keys->addrs.v4addrs.dst = fl4->daddr;
1110 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1111 keys->ports.src = fl4->fl4_sport;
1112 keys->ports.dst = fl4->fl4_dport;
1113 keys->keyid.keyid = fl4->fl4_gre_key;
1114 keys->basic.ip_proto = fl4->flowi4_proto;
1116 return flow_hash_from_keys(keys);
1118 EXPORT_SYMBOL(__get_hash_from_flowi4);
1120 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1122 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1123 .offset = offsetof(struct flow_keys, control),
1126 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1127 .offset = offsetof(struct flow_keys, basic),
1130 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1131 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1134 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1135 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1138 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
1139 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
1142 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1143 .offset = offsetof(struct flow_keys, ports),
1146 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1147 .offset = offsetof(struct flow_keys, vlan),
1150 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1151 .offset = offsetof(struct flow_keys, tags),
1154 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1155 .offset = offsetof(struct flow_keys, keyid),
1159 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1161 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1162 .offset = offsetof(struct flow_keys, control),
1165 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1166 .offset = offsetof(struct flow_keys, basic),
1169 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1170 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1173 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1174 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1177 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1178 .offset = offsetof(struct flow_keys, ports),
1182 static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
1184 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1185 .offset = offsetof(struct flow_keys, control),
1188 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1189 .offset = offsetof(struct flow_keys, basic),
1193 struct flow_dissector flow_keys_dissector __read_mostly;
1194 EXPORT_SYMBOL(flow_keys_dissector);
1196 struct flow_dissector flow_keys_buf_dissector __read_mostly;
1198 static int __init init_default_flow_dissectors(void)
1200 skb_flow_dissector_init(&flow_keys_dissector,
1201 flow_keys_dissector_keys,
1202 ARRAY_SIZE(flow_keys_dissector_keys));
1203 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1204 flow_keys_dissector_symmetric_keys,
1205 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1206 skb_flow_dissector_init(&flow_keys_buf_dissector,
1207 flow_keys_buf_dissector_keys,
1208 ARRAY_SIZE(flow_keys_buf_dissector_keys));
1212 core_initcall(init_default_flow_dissectors);