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>
8 #include <net/dst_metadata.h>
14 #include <linux/igmp.h>
15 #include <linux/icmp.h>
16 #include <linux/sctp.h>
17 #include <linux/dccp.h>
18 #include <linux/if_tunnel.h>
19 #include <linux/if_pppox.h>
20 #include <linux/ppp_defs.h>
21 #include <linux/stddef.h>
22 #include <linux/if_ether.h>
23 #include <linux/mpls.h>
24 #include <linux/tcp.h>
25 #include <net/flow_dissector.h>
26 #include <scsi/fc/fc_fcoe.h>
27 #include <uapi/linux/batadv_packet.h>
29 static void dissector_set_key(struct flow_dissector *flow_dissector,
30 enum flow_dissector_key_id key_id)
32 flow_dissector->used_keys |= (1 << key_id);
35 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
36 const struct flow_dissector_key *key,
37 unsigned int key_count)
41 memset(flow_dissector, 0, sizeof(*flow_dissector));
43 for (i = 0; i < key_count; i++, key++) {
44 /* User should make sure that every key target offset is withing
45 * boundaries of unsigned short.
47 BUG_ON(key->offset > USHRT_MAX);
48 BUG_ON(dissector_uses_key(flow_dissector,
51 dissector_set_key(flow_dissector, key->key_id);
52 flow_dissector->offset[key->key_id] = key->offset;
55 /* Ensure that the dissector always includes control and basic key.
56 * That way we are able to avoid handling lack of these in fast path.
58 BUG_ON(!dissector_uses_key(flow_dissector,
59 FLOW_DISSECTOR_KEY_CONTROL));
60 BUG_ON(!dissector_uses_key(flow_dissector,
61 FLOW_DISSECTOR_KEY_BASIC));
63 EXPORT_SYMBOL(skb_flow_dissector_init);
66 * skb_flow_get_be16 - extract be16 entity
67 * @skb: sk_buff to extract from
68 * @poff: offset to extract at
69 * @data: raw buffer pointer to the packet
70 * @hlen: packet header length
72 * The function will try to retrieve a be32 entity at
75 static __be16 skb_flow_get_be16(const struct sk_buff *skb, int poff,
80 u = __skb_header_pointer(skb, poff, sizeof(_u), data, hlen, &_u);
88 * __skb_flow_get_ports - extract the upper layer ports and return them
89 * @skb: sk_buff to extract the ports from
90 * @thoff: transport header offset
91 * @ip_proto: protocol for which to get port offset
92 * @data: raw buffer pointer to the packet, if NULL use skb->data
93 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
95 * The function will try to retrieve the ports at offset thoff + poff where poff
96 * is the protocol port offset returned from proto_ports_offset
98 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
101 int poff = proto_ports_offset(ip_proto);
105 hlen = skb_headlen(skb);
109 __be32 *ports, _ports;
111 ports = __skb_header_pointer(skb, thoff + poff,
112 sizeof(_ports), data, hlen, &_ports);
119 EXPORT_SYMBOL(__skb_flow_get_ports);
122 skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type,
123 struct flow_dissector *flow_dissector,
124 void *target_container)
126 struct flow_dissector_key_control *ctrl;
128 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL))
131 ctrl = skb_flow_dissector_target(flow_dissector,
132 FLOW_DISSECTOR_KEY_ENC_CONTROL,
134 ctrl->addr_type = type;
138 skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
139 struct flow_dissector *flow_dissector,
140 void *target_container)
142 struct ip_tunnel_info *info;
143 struct ip_tunnel_key *key;
145 /* A quick check to see if there might be something to do. */
146 if (!dissector_uses_key(flow_dissector,
147 FLOW_DISSECTOR_KEY_ENC_KEYID) &&
148 !dissector_uses_key(flow_dissector,
149 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) &&
150 !dissector_uses_key(flow_dissector,
151 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) &&
152 !dissector_uses_key(flow_dissector,
153 FLOW_DISSECTOR_KEY_ENC_CONTROL) &&
154 !dissector_uses_key(flow_dissector,
155 FLOW_DISSECTOR_KEY_ENC_PORTS) &&
156 !dissector_uses_key(flow_dissector,
157 FLOW_DISSECTOR_KEY_ENC_IP) &&
158 !dissector_uses_key(flow_dissector,
159 FLOW_DISSECTOR_KEY_ENC_OPTS))
162 info = skb_tunnel_info(skb);
168 switch (ip_tunnel_info_af(info)) {
170 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS,
173 if (dissector_uses_key(flow_dissector,
174 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
175 struct flow_dissector_key_ipv4_addrs *ipv4;
177 ipv4 = skb_flow_dissector_target(flow_dissector,
178 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
180 ipv4->src = key->u.ipv4.src;
181 ipv4->dst = key->u.ipv4.dst;
185 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS,
188 if (dissector_uses_key(flow_dissector,
189 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
190 struct flow_dissector_key_ipv6_addrs *ipv6;
192 ipv6 = skb_flow_dissector_target(flow_dissector,
193 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
195 ipv6->src = key->u.ipv6.src;
196 ipv6->dst = key->u.ipv6.dst;
201 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
202 struct flow_dissector_key_keyid *keyid;
204 keyid = skb_flow_dissector_target(flow_dissector,
205 FLOW_DISSECTOR_KEY_ENC_KEYID,
207 keyid->keyid = tunnel_id_to_key32(key->tun_id);
210 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
211 struct flow_dissector_key_ports *tp;
213 tp = skb_flow_dissector_target(flow_dissector,
214 FLOW_DISSECTOR_KEY_ENC_PORTS,
216 tp->src = key->tp_src;
217 tp->dst = key->tp_dst;
220 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) {
221 struct flow_dissector_key_ip *ip;
223 ip = skb_flow_dissector_target(flow_dissector,
224 FLOW_DISSECTOR_KEY_ENC_IP,
230 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
231 struct flow_dissector_key_enc_opts *enc_opt;
233 enc_opt = skb_flow_dissector_target(flow_dissector,
234 FLOW_DISSECTOR_KEY_ENC_OPTS,
237 if (info->options_len) {
238 enc_opt->len = info->options_len;
239 ip_tunnel_info_opts_get(enc_opt->data, info);
240 enc_opt->dst_opt_type = info->key.tun_flags &
241 TUNNEL_OPTIONS_PRESENT;
245 EXPORT_SYMBOL(skb_flow_dissect_tunnel_info);
247 static enum flow_dissect_ret
248 __skb_flow_dissect_mpls(const struct sk_buff *skb,
249 struct flow_dissector *flow_dissector,
250 void *target_container, void *data, int nhoff, int hlen)
252 struct flow_dissector_key_keyid *key_keyid;
253 struct mpls_label *hdr, _hdr[2];
256 if (!dissector_uses_key(flow_dissector,
257 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
258 !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
259 return FLOW_DISSECT_RET_OUT_GOOD;
261 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
264 return FLOW_DISSECT_RET_OUT_BAD;
266 entry = ntohl(hdr[0].entry);
267 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
269 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
270 struct flow_dissector_key_mpls *key_mpls;
272 key_mpls = skb_flow_dissector_target(flow_dissector,
273 FLOW_DISSECTOR_KEY_MPLS,
275 key_mpls->mpls_label = label;
276 key_mpls->mpls_ttl = (entry & MPLS_LS_TTL_MASK)
277 >> MPLS_LS_TTL_SHIFT;
278 key_mpls->mpls_tc = (entry & MPLS_LS_TC_MASK)
280 key_mpls->mpls_bos = (entry & MPLS_LS_S_MASK)
284 if (label == MPLS_LABEL_ENTROPY) {
285 key_keyid = skb_flow_dissector_target(flow_dissector,
286 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
288 key_keyid->keyid = hdr[1].entry & htonl(MPLS_LS_LABEL_MASK);
290 return FLOW_DISSECT_RET_OUT_GOOD;
293 static enum flow_dissect_ret
294 __skb_flow_dissect_arp(const struct sk_buff *skb,
295 struct flow_dissector *flow_dissector,
296 void *target_container, void *data, int nhoff, int hlen)
298 struct flow_dissector_key_arp *key_arp;
300 unsigned char ar_sha[ETH_ALEN];
301 unsigned char ar_sip[4];
302 unsigned char ar_tha[ETH_ALEN];
303 unsigned char ar_tip[4];
304 } *arp_eth, _arp_eth;
305 const struct arphdr *arp;
308 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
309 return FLOW_DISSECT_RET_OUT_GOOD;
311 arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
314 return FLOW_DISSECT_RET_OUT_BAD;
316 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
317 arp->ar_pro != htons(ETH_P_IP) ||
318 arp->ar_hln != ETH_ALEN ||
320 (arp->ar_op != htons(ARPOP_REPLY) &&
321 arp->ar_op != htons(ARPOP_REQUEST)))
322 return FLOW_DISSECT_RET_OUT_BAD;
324 arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
325 sizeof(_arp_eth), data,
328 return FLOW_DISSECT_RET_OUT_BAD;
330 key_arp = skb_flow_dissector_target(flow_dissector,
331 FLOW_DISSECTOR_KEY_ARP,
334 memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
335 memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
337 /* Only store the lower byte of the opcode;
338 * this covers ARPOP_REPLY and ARPOP_REQUEST.
340 key_arp->op = ntohs(arp->ar_op) & 0xff;
342 ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
343 ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
345 return FLOW_DISSECT_RET_OUT_GOOD;
348 static enum flow_dissect_ret
349 __skb_flow_dissect_gre(const struct sk_buff *skb,
350 struct flow_dissector_key_control *key_control,
351 struct flow_dissector *flow_dissector,
352 void *target_container, void *data,
353 __be16 *p_proto, int *p_nhoff, int *p_hlen,
356 struct flow_dissector_key_keyid *key_keyid;
357 struct gre_base_hdr *hdr, _hdr;
361 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
362 data, *p_hlen, &_hdr);
364 return FLOW_DISSECT_RET_OUT_BAD;
366 /* Only look inside GRE without routing */
367 if (hdr->flags & GRE_ROUTING)
368 return FLOW_DISSECT_RET_OUT_GOOD;
370 /* Only look inside GRE for version 0 and 1 */
371 gre_ver = ntohs(hdr->flags & GRE_VERSION);
373 return FLOW_DISSECT_RET_OUT_GOOD;
375 *p_proto = hdr->protocol;
377 /* Version1 must be PPTP, and check the flags */
378 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
379 return FLOW_DISSECT_RET_OUT_GOOD;
382 offset += sizeof(struct gre_base_hdr);
384 if (hdr->flags & GRE_CSUM)
385 offset += sizeof(((struct gre_full_hdr *) 0)->csum) +
386 sizeof(((struct gre_full_hdr *) 0)->reserved1);
388 if (hdr->flags & GRE_KEY) {
392 keyid = __skb_header_pointer(skb, *p_nhoff + offset,
394 data, *p_hlen, &_keyid);
396 return FLOW_DISSECT_RET_OUT_BAD;
398 if (dissector_uses_key(flow_dissector,
399 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
400 key_keyid = skb_flow_dissector_target(flow_dissector,
401 FLOW_DISSECTOR_KEY_GRE_KEYID,
404 key_keyid->keyid = *keyid;
406 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
408 offset += sizeof(((struct gre_full_hdr *) 0)->key);
411 if (hdr->flags & GRE_SEQ)
412 offset += sizeof(((struct pptp_gre_header *) 0)->seq);
415 if (*p_proto == htons(ETH_P_TEB)) {
416 const struct ethhdr *eth;
419 eth = __skb_header_pointer(skb, *p_nhoff + offset,
421 data, *p_hlen, &_eth);
423 return FLOW_DISSECT_RET_OUT_BAD;
424 *p_proto = eth->h_proto;
425 offset += sizeof(*eth);
427 /* Cap headers that we access via pointers at the
428 * end of the Ethernet header as our maximum alignment
429 * at that point is only 2 bytes.
432 *p_hlen = *p_nhoff + offset;
434 } else { /* version 1, must be PPTP */
435 u8 _ppp_hdr[PPP_HDRLEN];
438 if (hdr->flags & GRE_ACK)
439 offset += sizeof(((struct pptp_gre_header *) 0)->ack);
441 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
443 data, *p_hlen, _ppp_hdr);
445 return FLOW_DISSECT_RET_OUT_BAD;
447 switch (PPP_PROTOCOL(ppp_hdr)) {
449 *p_proto = htons(ETH_P_IP);
452 *p_proto = htons(ETH_P_IPV6);
455 /* Could probably catch some more like MPLS */
459 offset += PPP_HDRLEN;
463 key_control->flags |= FLOW_DIS_ENCAPSULATION;
464 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
465 return FLOW_DISSECT_RET_OUT_GOOD;
467 return FLOW_DISSECT_RET_PROTO_AGAIN;
471 * __skb_flow_dissect_batadv() - dissect batman-adv header
472 * @skb: sk_buff to with the batman-adv header
473 * @key_control: flow dissectors control key
474 * @data: raw buffer pointer to the packet, if NULL use skb->data
475 * @p_proto: pointer used to update the protocol to process next
476 * @p_nhoff: pointer used to update inner network header offset
477 * @hlen: packet header length
478 * @flags: any combination of FLOW_DISSECTOR_F_*
480 * ETH_P_BATMAN packets are tried to be dissected. Only
481 * &struct batadv_unicast packets are actually processed because they contain an
482 * inner ethernet header and are usually followed by actual network header. This
483 * allows the flow dissector to continue processing the packet.
485 * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found,
486 * FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation,
487 * otherwise FLOW_DISSECT_RET_OUT_BAD
489 static enum flow_dissect_ret
490 __skb_flow_dissect_batadv(const struct sk_buff *skb,
491 struct flow_dissector_key_control *key_control,
492 void *data, __be16 *p_proto, int *p_nhoff, int hlen,
496 struct batadv_unicast_packet batadv_unicast;
500 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen,
503 return FLOW_DISSECT_RET_OUT_BAD;
505 if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION)
506 return FLOW_DISSECT_RET_OUT_BAD;
508 if (hdr->batadv_unicast.packet_type != BATADV_UNICAST)
509 return FLOW_DISSECT_RET_OUT_BAD;
511 *p_proto = hdr->eth.h_proto;
512 *p_nhoff += sizeof(*hdr);
514 key_control->flags |= FLOW_DIS_ENCAPSULATION;
515 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
516 return FLOW_DISSECT_RET_OUT_GOOD;
518 return FLOW_DISSECT_RET_PROTO_AGAIN;
522 __skb_flow_dissect_tcp(const struct sk_buff *skb,
523 struct flow_dissector *flow_dissector,
524 void *target_container, void *data, int thoff, int hlen)
526 struct flow_dissector_key_tcp *key_tcp;
527 struct tcphdr *th, _th;
529 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
532 th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
536 if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
539 key_tcp = skb_flow_dissector_target(flow_dissector,
540 FLOW_DISSECTOR_KEY_TCP,
542 key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
546 __skb_flow_dissect_ipv4(const struct sk_buff *skb,
547 struct flow_dissector *flow_dissector,
548 void *target_container, void *data, const struct iphdr *iph)
550 struct flow_dissector_key_ip *key_ip;
552 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
555 key_ip = skb_flow_dissector_target(flow_dissector,
556 FLOW_DISSECTOR_KEY_IP,
558 key_ip->tos = iph->tos;
559 key_ip->ttl = iph->ttl;
563 __skb_flow_dissect_ipv6(const struct sk_buff *skb,
564 struct flow_dissector *flow_dissector,
565 void *target_container, void *data, const struct ipv6hdr *iph)
567 struct flow_dissector_key_ip *key_ip;
569 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
572 key_ip = skb_flow_dissector_target(flow_dissector,
573 FLOW_DISSECTOR_KEY_IP,
575 key_ip->tos = ipv6_get_dsfield(iph);
576 key_ip->ttl = iph->hop_limit;
579 /* Maximum number of protocol headers that can be parsed in
582 #define MAX_FLOW_DISSECT_HDRS 15
584 static bool skb_flow_dissect_allowed(int *num_hdrs)
588 return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
592 * __skb_flow_dissect - extract the flow_keys struct and return it
593 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
594 * @flow_dissector: list of keys to dissect
595 * @target_container: target structure to put dissected values into
596 * @data: raw buffer pointer to the packet, if NULL use skb->data
597 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
598 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
599 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
601 * The function will try to retrieve individual keys into target specified
602 * by flow_dissector from either the skbuff or a raw buffer specified by the
605 * Caller must take care of zeroing target container memory.
607 bool __skb_flow_dissect(const struct sk_buff *skb,
608 struct flow_dissector *flow_dissector,
609 void *target_container,
610 void *data, __be16 proto, int nhoff, int hlen,
613 struct flow_dissector_key_control *key_control;
614 struct flow_dissector_key_basic *key_basic;
615 struct flow_dissector_key_addrs *key_addrs;
616 struct flow_dissector_key_ports *key_ports;
617 struct flow_dissector_key_icmp *key_icmp;
618 struct flow_dissector_key_tags *key_tags;
619 struct flow_dissector_key_vlan *key_vlan;
620 enum flow_dissect_ret fdret;
621 enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
628 proto = skb_vlan_tag_present(skb) ?
629 skb->vlan_proto : skb->protocol;
630 nhoff = skb_network_offset(skb);
631 hlen = skb_headlen(skb);
632 #if IS_ENABLED(CONFIG_NET_DSA)
633 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
634 proto == htons(ETH_P_XDSA))) {
635 const struct dsa_device_ops *ops;
638 ops = skb->dev->dsa_ptr->tag_ops;
639 if (ops->flow_dissect &&
640 !ops->flow_dissect(skb, &proto, &offset)) {
648 /* It is ensured by skb_flow_dissector_init() that control key will
651 key_control = skb_flow_dissector_target(flow_dissector,
652 FLOW_DISSECTOR_KEY_CONTROL,
655 /* It is ensured by skb_flow_dissector_init() that basic key will
658 key_basic = skb_flow_dissector_target(flow_dissector,
659 FLOW_DISSECTOR_KEY_BASIC,
662 if (dissector_uses_key(flow_dissector,
663 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
664 struct ethhdr *eth = eth_hdr(skb);
665 struct flow_dissector_key_eth_addrs *key_eth_addrs;
667 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
668 FLOW_DISSECTOR_KEY_ETH_ADDRS,
670 memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs));
674 fdret = FLOW_DISSECT_RET_CONTINUE;
677 case htons(ETH_P_IP): {
678 const struct iphdr *iph;
681 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
682 if (!iph || iph->ihl < 5) {
683 fdret = FLOW_DISSECT_RET_OUT_BAD;
687 nhoff += iph->ihl * 4;
689 ip_proto = iph->protocol;
691 if (dissector_uses_key(flow_dissector,
692 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
693 key_addrs = skb_flow_dissector_target(flow_dissector,
694 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
697 memcpy(&key_addrs->v4addrs.src, &iph->saddr,
698 sizeof(key_addrs->v4addrs.src));
699 memcpy(&key_addrs->v4addrs.dst, &iph->daddr,
700 sizeof(key_addrs->v4addrs.dst));
701 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
704 if (ip_is_fragment(iph)) {
705 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
707 if (iph->frag_off & htons(IP_OFFSET)) {
708 fdret = FLOW_DISSECT_RET_OUT_GOOD;
711 key_control->flags |= FLOW_DIS_FIRST_FRAG;
713 FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
714 fdret = FLOW_DISSECT_RET_OUT_GOOD;
720 __skb_flow_dissect_ipv4(skb, flow_dissector,
721 target_container, data, iph);
723 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3) {
724 fdret = FLOW_DISSECT_RET_OUT_GOOD;
730 case htons(ETH_P_IPV6): {
731 const struct ipv6hdr *iph;
734 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
736 fdret = FLOW_DISSECT_RET_OUT_BAD;
740 ip_proto = iph->nexthdr;
741 nhoff += sizeof(struct ipv6hdr);
743 if (dissector_uses_key(flow_dissector,
744 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
745 key_addrs = skb_flow_dissector_target(flow_dissector,
746 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
749 memcpy(&key_addrs->v6addrs.src, &iph->saddr,
750 sizeof(key_addrs->v6addrs.src));
751 memcpy(&key_addrs->v6addrs.dst, &iph->daddr,
752 sizeof(key_addrs->v6addrs.dst));
753 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
756 if ((dissector_uses_key(flow_dissector,
757 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
758 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
759 ip6_flowlabel(iph)) {
760 __be32 flow_label = ip6_flowlabel(iph);
762 if (dissector_uses_key(flow_dissector,
763 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
764 key_tags = skb_flow_dissector_target(flow_dissector,
765 FLOW_DISSECTOR_KEY_FLOW_LABEL,
767 key_tags->flow_label = ntohl(flow_label);
769 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
770 fdret = FLOW_DISSECT_RET_OUT_GOOD;
775 __skb_flow_dissect_ipv6(skb, flow_dissector,
776 target_container, data, iph);
778 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
779 fdret = FLOW_DISSECT_RET_OUT_GOOD;
783 case htons(ETH_P_8021AD):
784 case htons(ETH_P_8021Q): {
785 const struct vlan_hdr *vlan = NULL;
786 struct vlan_hdr _vlan;
787 __be16 saved_vlan_tpid = proto;
789 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
790 skb && skb_vlan_tag_present(skb)) {
791 proto = skb->protocol;
793 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
796 fdret = FLOW_DISSECT_RET_OUT_BAD;
800 proto = vlan->h_vlan_encapsulated_proto;
801 nhoff += sizeof(*vlan);
804 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
805 dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
806 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
807 dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
809 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
813 if (dissector_uses_key(flow_dissector, dissector_vlan)) {
814 key_vlan = skb_flow_dissector_target(flow_dissector,
819 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
820 key_vlan->vlan_priority =
821 (skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT);
823 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
825 key_vlan->vlan_priority =
826 (ntohs(vlan->h_vlan_TCI) &
827 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
829 key_vlan->vlan_tpid = saved_vlan_tpid;
832 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
835 case htons(ETH_P_PPP_SES): {
837 struct pppoe_hdr hdr;
840 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
842 fdret = FLOW_DISSECT_RET_OUT_BAD;
847 nhoff += PPPOE_SES_HLEN;
850 proto = htons(ETH_P_IP);
851 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
853 case htons(PPP_IPV6):
854 proto = htons(ETH_P_IPV6);
855 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
858 fdret = FLOW_DISSECT_RET_OUT_BAD;
863 case htons(ETH_P_TIPC): {
864 struct tipc_basic_hdr *hdr, _hdr;
866 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
869 fdret = FLOW_DISSECT_RET_OUT_BAD;
873 if (dissector_uses_key(flow_dissector,
874 FLOW_DISSECTOR_KEY_TIPC)) {
875 key_addrs = skb_flow_dissector_target(flow_dissector,
876 FLOW_DISSECTOR_KEY_TIPC,
878 key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
879 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
881 fdret = FLOW_DISSECT_RET_OUT_GOOD;
885 case htons(ETH_P_MPLS_UC):
886 case htons(ETH_P_MPLS_MC):
887 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
888 target_container, data,
891 case htons(ETH_P_FCOE):
892 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
893 fdret = FLOW_DISSECT_RET_OUT_BAD;
897 nhoff += FCOE_HEADER_LEN;
898 fdret = FLOW_DISSECT_RET_OUT_GOOD;
901 case htons(ETH_P_ARP):
902 case htons(ETH_P_RARP):
903 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
904 target_container, data,
908 case htons(ETH_P_BATMAN):
909 fdret = __skb_flow_dissect_batadv(skb, key_control, data,
910 &proto, &nhoff, hlen, flags);
914 fdret = FLOW_DISSECT_RET_OUT_BAD;
918 /* Process result of proto processing */
920 case FLOW_DISSECT_RET_OUT_GOOD:
922 case FLOW_DISSECT_RET_PROTO_AGAIN:
923 if (skb_flow_dissect_allowed(&num_hdrs))
926 case FLOW_DISSECT_RET_CONTINUE:
927 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
929 case FLOW_DISSECT_RET_OUT_BAD:
935 fdret = FLOW_DISSECT_RET_CONTINUE;
939 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
940 target_container, data,
941 &proto, &nhoff, &hlen, flags);
945 case NEXTHDR_ROUTING:
947 u8 _opthdr[2], *opthdr;
949 if (proto != htons(ETH_P_IPV6))
952 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
953 data, hlen, &_opthdr);
955 fdret = FLOW_DISSECT_RET_OUT_BAD;
959 ip_proto = opthdr[0];
960 nhoff += (opthdr[1] + 1) << 3;
962 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
965 case NEXTHDR_FRAGMENT: {
966 struct frag_hdr _fh, *fh;
968 if (proto != htons(ETH_P_IPV6))
971 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
975 fdret = FLOW_DISSECT_RET_OUT_BAD;
979 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
981 nhoff += sizeof(_fh);
982 ip_proto = fh->nexthdr;
984 if (!(fh->frag_off & htons(IP6_OFFSET))) {
985 key_control->flags |= FLOW_DIS_FIRST_FRAG;
986 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
987 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
992 fdret = FLOW_DISSECT_RET_OUT_GOOD;
996 proto = htons(ETH_P_IP);
998 key_control->flags |= FLOW_DIS_ENCAPSULATION;
999 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1000 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1004 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1008 proto = htons(ETH_P_IPV6);
1010 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1011 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1012 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1016 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1021 proto = htons(ETH_P_MPLS_UC);
1022 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1026 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1034 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS) &&
1035 !(key_control->flags & FLOW_DIS_IS_FRAGMENT)) {
1036 key_ports = skb_flow_dissector_target(flow_dissector,
1037 FLOW_DISSECTOR_KEY_PORTS,
1039 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
1043 if (dissector_uses_key(flow_dissector,
1044 FLOW_DISSECTOR_KEY_ICMP)) {
1045 key_icmp = skb_flow_dissector_target(flow_dissector,
1046 FLOW_DISSECTOR_KEY_ICMP,
1048 key_icmp->icmp = skb_flow_get_be16(skb, nhoff, data, hlen);
1051 /* Process result of IP proto processing */
1053 case FLOW_DISSECT_RET_PROTO_AGAIN:
1054 if (skb_flow_dissect_allowed(&num_hdrs))
1057 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1058 if (skb_flow_dissect_allowed(&num_hdrs))
1059 goto ip_proto_again;
1061 case FLOW_DISSECT_RET_OUT_GOOD:
1062 case FLOW_DISSECT_RET_CONTINUE:
1064 case FLOW_DISSECT_RET_OUT_BAD:
1073 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1074 key_basic->n_proto = proto;
1075 key_basic->ip_proto = ip_proto;
1083 EXPORT_SYMBOL(__skb_flow_dissect);
1085 static siphash_key_t hashrnd __read_mostly;
1086 static __always_inline void __flow_hash_secret_init(void)
1088 net_get_random_once(&hashrnd, sizeof(hashrnd));
1091 static const void *flow_keys_hash_start(const struct flow_keys *flow)
1093 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1094 return &flow->FLOW_KEYS_HASH_START_FIELD;
1097 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1099 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1100 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
1101 sizeof(*flow) - sizeof(flow->addrs));
1103 switch (flow->control.addr_type) {
1104 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1105 diff -= sizeof(flow->addrs.v4addrs);
1107 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1108 diff -= sizeof(flow->addrs.v6addrs);
1110 case FLOW_DISSECTOR_KEY_TIPC:
1111 diff -= sizeof(flow->addrs.tipckey);
1114 return sizeof(*flow) - diff;
1117 __be32 flow_get_u32_src(const struct flow_keys *flow)
1119 switch (flow->control.addr_type) {
1120 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1121 return flow->addrs.v4addrs.src;
1122 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1123 return (__force __be32)ipv6_addr_hash(
1124 &flow->addrs.v6addrs.src);
1125 case FLOW_DISSECTOR_KEY_TIPC:
1126 return flow->addrs.tipckey.key;
1131 EXPORT_SYMBOL(flow_get_u32_src);
1133 __be32 flow_get_u32_dst(const struct flow_keys *flow)
1135 switch (flow->control.addr_type) {
1136 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1137 return flow->addrs.v4addrs.dst;
1138 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1139 return (__force __be32)ipv6_addr_hash(
1140 &flow->addrs.v6addrs.dst);
1145 EXPORT_SYMBOL(flow_get_u32_dst);
1147 static inline void __flow_hash_consistentify(struct flow_keys *keys)
1151 switch (keys->control.addr_type) {
1152 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1153 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
1154 (__force u32)keys->addrs.v4addrs.src;
1155 if ((addr_diff < 0) ||
1157 ((__force u16)keys->ports.dst <
1158 (__force u16)keys->ports.src))) {
1159 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1160 swap(keys->ports.src, keys->ports.dst);
1163 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1164 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1165 &keys->addrs.v6addrs.src,
1166 sizeof(keys->addrs.v6addrs.dst));
1167 if ((addr_diff < 0) ||
1169 ((__force u16)keys->ports.dst <
1170 (__force u16)keys->ports.src))) {
1171 for (i = 0; i < 4; i++)
1172 swap(keys->addrs.v6addrs.src.s6_addr32[i],
1173 keys->addrs.v6addrs.dst.s6_addr32[i]);
1174 swap(keys->ports.src, keys->ports.dst);
1180 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1181 const siphash_key_t *keyval)
1185 __flow_hash_consistentify(keys);
1187 hash = siphash(flow_keys_hash_start(keys),
1188 flow_keys_hash_length(keys), keyval);
1195 u32 flow_hash_from_keys(struct flow_keys *keys)
1197 __flow_hash_secret_init();
1198 return __flow_hash_from_keys(keys, &hashrnd);
1200 EXPORT_SYMBOL(flow_hash_from_keys);
1202 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1203 struct flow_keys *keys,
1204 const siphash_key_t *keyval)
1206 skb_flow_dissect_flow_keys(skb, keys,
1207 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1209 return __flow_hash_from_keys(keys, keyval);
1212 struct _flow_keys_digest_data {
1221 void make_flow_keys_digest(struct flow_keys_digest *digest,
1222 const struct flow_keys *flow)
1224 struct _flow_keys_digest_data *data =
1225 (struct _flow_keys_digest_data *)digest;
1227 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1229 memset(digest, 0, sizeof(*digest));
1231 data->n_proto = flow->basic.n_proto;
1232 data->ip_proto = flow->basic.ip_proto;
1233 data->ports = flow->ports.ports;
1234 data->src = flow->addrs.v4addrs.src;
1235 data->dst = flow->addrs.v4addrs.dst;
1237 EXPORT_SYMBOL(make_flow_keys_digest);
1239 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1241 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1243 struct flow_keys keys;
1245 __flow_hash_secret_init();
1247 memset(&keys, 0, sizeof(keys));
1248 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
1250 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1252 return __flow_hash_from_keys(&keys, &hashrnd);
1254 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1257 * __skb_get_hash: calculate a flow hash
1258 * @skb: sk_buff to calculate flow hash from
1260 * This function calculates a flow hash based on src/dst addresses
1261 * and src/dst port numbers. Sets hash in skb to non-zero hash value
1262 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
1263 * if hash is a canonical 4-tuple hash over transport ports.
1265 void __skb_get_hash(struct sk_buff *skb)
1267 struct flow_keys keys;
1270 __flow_hash_secret_init();
1272 hash = ___skb_get_hash(skb, &keys, &hashrnd);
1274 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1276 EXPORT_SYMBOL(__skb_get_hash);
1278 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1279 const siphash_key_t *perturb)
1281 struct flow_keys keys;
1283 return ___skb_get_hash(skb, &keys, perturb);
1285 EXPORT_SYMBOL(skb_get_hash_perturb);
1287 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
1288 const struct flow_keys_basic *keys, int hlen)
1290 u32 poff = keys->control.thoff;
1292 /* skip L4 headers for fragments after the first */
1293 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1294 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1297 switch (keys->basic.ip_proto) {
1299 /* access doff as u8 to avoid unaligned access */
1303 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1304 data, hlen, &_doff);
1308 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1312 case IPPROTO_UDPLITE:
1313 poff += sizeof(struct udphdr);
1315 /* For the rest, we do not really care about header
1316 * extensions at this point for now.
1319 poff += sizeof(struct icmphdr);
1321 case IPPROTO_ICMPV6:
1322 poff += sizeof(struct icmp6hdr);
1325 poff += sizeof(struct igmphdr);
1328 poff += sizeof(struct dccp_hdr);
1331 poff += sizeof(struct sctphdr);
1339 * skb_get_poff - get the offset to the payload
1340 * @skb: sk_buff to get the payload offset from
1342 * The function will get the offset to the payload as far as it could
1343 * be dissected. The main user is currently BPF, so that we can dynamically
1344 * truncate packets without needing to push actual payload to the user
1345 * space and can analyze headers only, instead.
1347 u32 skb_get_poff(const struct sk_buff *skb)
1349 struct flow_keys_basic keys;
1351 if (!skb_flow_dissect_flow_keys_basic(skb, &keys, NULL, 0, 0, 0, 0))
1354 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1357 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1359 memset(keys, 0, sizeof(*keys));
1361 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1362 sizeof(keys->addrs.v6addrs.src));
1363 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1364 sizeof(keys->addrs.v6addrs.dst));
1365 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1366 keys->ports.src = fl6->fl6_sport;
1367 keys->ports.dst = fl6->fl6_dport;
1368 keys->keyid.keyid = fl6->fl6_gre_key;
1369 keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1370 keys->basic.ip_proto = fl6->flowi6_proto;
1372 return flow_hash_from_keys(keys);
1374 EXPORT_SYMBOL(__get_hash_from_flowi6);
1376 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1378 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1379 .offset = offsetof(struct flow_keys, control),
1382 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1383 .offset = offsetof(struct flow_keys, basic),
1386 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1387 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1390 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1391 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1394 .key_id = FLOW_DISSECTOR_KEY_TIPC,
1395 .offset = offsetof(struct flow_keys, addrs.tipckey),
1398 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1399 .offset = offsetof(struct flow_keys, ports),
1402 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1403 .offset = offsetof(struct flow_keys, vlan),
1406 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1407 .offset = offsetof(struct flow_keys, tags),
1410 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1411 .offset = offsetof(struct flow_keys, keyid),
1415 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1417 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1418 .offset = offsetof(struct flow_keys, control),
1421 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1422 .offset = offsetof(struct flow_keys, basic),
1425 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1426 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1429 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1430 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1433 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1434 .offset = offsetof(struct flow_keys, ports),
1438 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
1440 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1441 .offset = offsetof(struct flow_keys, control),
1444 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1445 .offset = offsetof(struct flow_keys, basic),
1449 struct flow_dissector flow_keys_dissector __read_mostly;
1450 EXPORT_SYMBOL(flow_keys_dissector);
1452 struct flow_dissector flow_keys_basic_dissector __read_mostly;
1453 EXPORT_SYMBOL(flow_keys_basic_dissector);
1455 static int __init init_default_flow_dissectors(void)
1457 skb_flow_dissector_init(&flow_keys_dissector,
1458 flow_keys_dissector_keys,
1459 ARRAY_SIZE(flow_keys_dissector_keys));
1460 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1461 flow_keys_dissector_symmetric_keys,
1462 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1463 skb_flow_dissector_init(&flow_keys_basic_dissector,
1464 flow_keys_basic_dissector_keys,
1465 ARRAY_SIZE(flow_keys_basic_dissector_keys));
1469 core_initcall(init_default_flow_dissectors);