1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/skbuff.h>
4 #include <linux/export.h>
6 #include <linux/ipv6.h>
7 #include <linux/if_vlan.h>
8 #include <linux/filter.h>
10 #include <net/dst_metadata.h>
16 #include <linux/igmp.h>
17 #include <linux/icmp.h>
18 #include <linux/sctp.h>
19 #include <linux/dccp.h>
20 #include <linux/if_tunnel.h>
21 #include <linux/if_pppox.h>
22 #include <linux/ppp_defs.h>
23 #include <linux/stddef.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_hsr.h>
26 #include <linux/mpls.h>
27 #include <linux/tcp.h>
28 #include <linux/ptp_classify.h>
29 #include <net/flow_dissector.h>
30 #include <net/pkt_cls.h>
31 #include <scsi/fc/fc_fcoe.h>
32 #include <uapi/linux/batadv_packet.h>
33 #include <linux/bpf.h>
34 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
35 #include <net/netfilter/nf_conntrack_core.h>
36 #include <net/netfilter/nf_conntrack_labels.h>
38 #include <linux/bpf-netns.h>
40 static void dissector_set_key(struct flow_dissector *flow_dissector,
41 enum flow_dissector_key_id key_id)
43 flow_dissector->used_keys |= (1ULL << key_id);
46 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
47 const struct flow_dissector_key *key,
48 unsigned int key_count)
52 memset(flow_dissector, 0, sizeof(*flow_dissector));
54 for (i = 0; i < key_count; i++, key++) {
55 /* User should make sure that every key target offset is within
56 * boundaries of unsigned short.
58 BUG_ON(key->offset > USHRT_MAX);
59 BUG_ON(dissector_uses_key(flow_dissector,
62 dissector_set_key(flow_dissector, key->key_id);
63 flow_dissector->offset[key->key_id] = key->offset;
66 /* Ensure that the dissector always includes control and basic key.
67 * That way we are able to avoid handling lack of these in fast path.
69 BUG_ON(!dissector_uses_key(flow_dissector,
70 FLOW_DISSECTOR_KEY_CONTROL));
71 BUG_ON(!dissector_uses_key(flow_dissector,
72 FLOW_DISSECTOR_KEY_BASIC));
74 EXPORT_SYMBOL(skb_flow_dissector_init);
76 #ifdef CONFIG_BPF_SYSCALL
77 int flow_dissector_bpf_prog_attach_check(struct net *net,
78 struct bpf_prog *prog)
80 enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
82 if (net == &init_net) {
83 /* BPF flow dissector in the root namespace overrides
84 * any per-net-namespace one. When attaching to root,
85 * make sure we don't have any BPF program attached
86 * to the non-root namespaces.
93 if (rcu_access_pointer(ns->bpf.run_array[type]))
97 /* Make sure root flow dissector is not attached
98 * when attaching to the non-root namespace.
100 if (rcu_access_pointer(init_net.bpf.run_array[type]))
106 #endif /* CONFIG_BPF_SYSCALL */
109 * __skb_flow_get_ports - extract the upper layer ports and return them
110 * @skb: sk_buff to extract the ports from
111 * @thoff: transport header offset
112 * @ip_proto: protocol for which to get port offset
113 * @data: raw buffer pointer to the packet, if NULL use skb->data
114 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
116 * The function will try to retrieve the ports at offset thoff + poff where poff
117 * is the protocol port offset returned from proto_ports_offset
119 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
120 const void *data, int hlen)
122 int poff = proto_ports_offset(ip_proto);
126 hlen = skb_headlen(skb);
130 __be32 *ports, _ports;
132 ports = __skb_header_pointer(skb, thoff + poff,
133 sizeof(_ports), data, hlen, &_ports);
140 EXPORT_SYMBOL(__skb_flow_get_ports);
142 static bool icmp_has_id(u8 type)
148 case ICMP_TIMESTAMPREPLY:
149 case ICMPV6_ECHO_REQUEST:
150 case ICMPV6_ECHO_REPLY:
158 * skb_flow_get_icmp_tci - extract ICMP(6) Type, Code and Identifier fields
159 * @skb: sk_buff to extract from
160 * @key_icmp: struct flow_dissector_key_icmp to fill
161 * @data: raw buffer pointer to the packet
162 * @thoff: offset to extract at
163 * @hlen: packet header length
165 void skb_flow_get_icmp_tci(const struct sk_buff *skb,
166 struct flow_dissector_key_icmp *key_icmp,
167 const void *data, int thoff, int hlen)
169 struct icmphdr *ih, _ih;
171 ih = __skb_header_pointer(skb, thoff, sizeof(_ih), data, hlen, &_ih);
175 key_icmp->type = ih->type;
176 key_icmp->code = ih->code;
178 /* As we use 0 to signal that the Id field is not present,
179 * avoid confusion with packets without such field
181 if (icmp_has_id(ih->type))
182 key_icmp->id = ih->un.echo.id ? ntohs(ih->un.echo.id) : 1;
186 EXPORT_SYMBOL(skb_flow_get_icmp_tci);
188 /* If FLOW_DISSECTOR_KEY_ICMP is set, dissect an ICMP packet
189 * using skb_flow_get_icmp_tci().
191 static void __skb_flow_dissect_icmp(const struct sk_buff *skb,
192 struct flow_dissector *flow_dissector,
193 void *target_container, const void *data,
196 struct flow_dissector_key_icmp *key_icmp;
198 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ICMP))
201 key_icmp = skb_flow_dissector_target(flow_dissector,
202 FLOW_DISSECTOR_KEY_ICMP,
205 skb_flow_get_icmp_tci(skb, key_icmp, data, thoff, hlen);
208 static void __skb_flow_dissect_ah(const struct sk_buff *skb,
209 struct flow_dissector *flow_dissector,
210 void *target_container, const void *data,
213 struct flow_dissector_key_ipsec *key_ah;
214 struct ip_auth_hdr _hdr, *hdr;
216 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPSEC))
219 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
223 key_ah = skb_flow_dissector_target(flow_dissector,
224 FLOW_DISSECTOR_KEY_IPSEC,
227 key_ah->spi = hdr->spi;
230 static void __skb_flow_dissect_esp(const struct sk_buff *skb,
231 struct flow_dissector *flow_dissector,
232 void *target_container, const void *data,
235 struct flow_dissector_key_ipsec *key_esp;
236 struct ip_esp_hdr _hdr, *hdr;
238 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPSEC))
241 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
245 key_esp = skb_flow_dissector_target(flow_dissector,
246 FLOW_DISSECTOR_KEY_IPSEC,
249 key_esp->spi = hdr->spi;
252 static void __skb_flow_dissect_l2tpv3(const struct sk_buff *skb,
253 struct flow_dissector *flow_dissector,
254 void *target_container, const void *data,
257 struct flow_dissector_key_l2tpv3 *key_l2tpv3;
262 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_L2TPV3))
265 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
269 key_l2tpv3 = skb_flow_dissector_target(flow_dissector,
270 FLOW_DISSECTOR_KEY_L2TPV3,
273 key_l2tpv3->session_id = hdr->session_id;
276 void skb_flow_dissect_meta(const struct sk_buff *skb,
277 struct flow_dissector *flow_dissector,
278 void *target_container)
280 struct flow_dissector_key_meta *meta;
282 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_META))
285 meta = skb_flow_dissector_target(flow_dissector,
286 FLOW_DISSECTOR_KEY_META,
288 meta->ingress_ifindex = skb->skb_iif;
289 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
290 if (tc_skb_ext_tc_enabled()) {
291 struct tc_skb_ext *ext;
293 ext = skb_ext_find(skb, TC_SKB_EXT);
295 meta->l2_miss = ext->l2_miss;
299 EXPORT_SYMBOL(skb_flow_dissect_meta);
302 skb_flow_dissect_set_enc_control(enum flow_dissector_key_id type,
304 struct flow_dissector *flow_dissector,
305 void *target_container)
307 struct flow_dissector_key_control *ctrl;
309 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL))
312 ctrl = skb_flow_dissector_target(flow_dissector,
313 FLOW_DISSECTOR_KEY_ENC_CONTROL,
315 ctrl->addr_type = type;
316 ctrl->flags = ctrl_flags;
320 skb_flow_dissect_ct(const struct sk_buff *skb,
321 struct flow_dissector *flow_dissector,
322 void *target_container, u16 *ctinfo_map,
323 size_t mapsize, bool post_ct, u16 zone)
325 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
326 struct flow_dissector_key_ct *key;
327 enum ip_conntrack_info ctinfo;
328 struct nf_conn_labels *cl;
331 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_CT))
334 ct = nf_ct_get(skb, &ctinfo);
338 key = skb_flow_dissector_target(flow_dissector,
339 FLOW_DISSECTOR_KEY_CT,
343 key->ct_state = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
344 TCA_FLOWER_KEY_CT_FLAGS_INVALID;
349 if (ctinfo < mapsize)
350 key->ct_state = ctinfo_map[ctinfo];
351 #if IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)
352 key->ct_zone = ct->zone.id;
354 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
355 key->ct_mark = READ_ONCE(ct->mark);
358 cl = nf_ct_labels_find(ct);
360 memcpy(key->ct_labels, cl->bits, sizeof(key->ct_labels));
361 #endif /* CONFIG_NF_CONNTRACK */
363 EXPORT_SYMBOL(skb_flow_dissect_ct);
366 skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
367 struct flow_dissector *flow_dissector,
368 void *target_container)
370 struct ip_tunnel_info *info;
371 struct ip_tunnel_key *key;
374 /* A quick check to see if there might be something to do. */
375 if (!dissector_uses_key(flow_dissector,
376 FLOW_DISSECTOR_KEY_ENC_KEYID) &&
377 !dissector_uses_key(flow_dissector,
378 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) &&
379 !dissector_uses_key(flow_dissector,
380 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) &&
381 !dissector_uses_key(flow_dissector,
382 FLOW_DISSECTOR_KEY_ENC_CONTROL) &&
383 !dissector_uses_key(flow_dissector,
384 FLOW_DISSECTOR_KEY_ENC_PORTS) &&
385 !dissector_uses_key(flow_dissector,
386 FLOW_DISSECTOR_KEY_ENC_IP) &&
387 !dissector_uses_key(flow_dissector,
388 FLOW_DISSECTOR_KEY_ENC_OPTS))
391 info = skb_tunnel_info(skb);
397 if (test_bit(IP_TUNNEL_CSUM_BIT, key->tun_flags))
398 ctrl_flags |= FLOW_DIS_F_TUNNEL_CSUM;
399 if (test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, key->tun_flags))
400 ctrl_flags |= FLOW_DIS_F_TUNNEL_DONT_FRAGMENT;
401 if (test_bit(IP_TUNNEL_OAM_BIT, key->tun_flags))
402 ctrl_flags |= FLOW_DIS_F_TUNNEL_OAM;
403 if (test_bit(IP_TUNNEL_CRIT_OPT_BIT, key->tun_flags))
404 ctrl_flags |= FLOW_DIS_F_TUNNEL_CRIT_OPT;
406 switch (ip_tunnel_info_af(info)) {
408 skb_flow_dissect_set_enc_control(FLOW_DISSECTOR_KEY_IPV4_ADDRS,
409 ctrl_flags, flow_dissector,
411 if (dissector_uses_key(flow_dissector,
412 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
413 struct flow_dissector_key_ipv4_addrs *ipv4;
415 ipv4 = skb_flow_dissector_target(flow_dissector,
416 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
418 ipv4->src = key->u.ipv4.src;
419 ipv4->dst = key->u.ipv4.dst;
423 skb_flow_dissect_set_enc_control(FLOW_DISSECTOR_KEY_IPV6_ADDRS,
424 ctrl_flags, flow_dissector,
426 if (dissector_uses_key(flow_dissector,
427 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
428 struct flow_dissector_key_ipv6_addrs *ipv6;
430 ipv6 = skb_flow_dissector_target(flow_dissector,
431 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
433 ipv6->src = key->u.ipv6.src;
434 ipv6->dst = key->u.ipv6.dst;
438 skb_flow_dissect_set_enc_control(0, ctrl_flags, flow_dissector,
443 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
444 struct flow_dissector_key_keyid *keyid;
446 keyid = skb_flow_dissector_target(flow_dissector,
447 FLOW_DISSECTOR_KEY_ENC_KEYID,
449 keyid->keyid = tunnel_id_to_key32(key->tun_id);
452 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
453 struct flow_dissector_key_ports *tp;
455 tp = skb_flow_dissector_target(flow_dissector,
456 FLOW_DISSECTOR_KEY_ENC_PORTS,
458 tp->src = key->tp_src;
459 tp->dst = key->tp_dst;
462 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) {
463 struct flow_dissector_key_ip *ip;
465 ip = skb_flow_dissector_target(flow_dissector,
466 FLOW_DISSECTOR_KEY_ENC_IP,
472 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
473 struct flow_dissector_key_enc_opts *enc_opt;
474 IP_TUNNEL_DECLARE_FLAGS(flags) = { };
477 enc_opt = skb_flow_dissector_target(flow_dissector,
478 FLOW_DISSECTOR_KEY_ENC_OPTS,
481 if (!info->options_len)
484 enc_opt->len = info->options_len;
485 ip_tunnel_info_opts_get(enc_opt->data, info);
487 ip_tunnel_set_options_present(flags);
488 ip_tunnel_flags_and(flags, info->key.tun_flags, flags);
490 val = find_next_bit(flags, __IP_TUNNEL_FLAG_NUM,
491 IP_TUNNEL_GENEVE_OPT_BIT);
492 enc_opt->dst_opt_type = val < __IP_TUNNEL_FLAG_NUM ? val : 0;
495 EXPORT_SYMBOL(skb_flow_dissect_tunnel_info);
497 void skb_flow_dissect_hash(const struct sk_buff *skb,
498 struct flow_dissector *flow_dissector,
499 void *target_container)
501 struct flow_dissector_key_hash *key;
503 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_HASH))
506 key = skb_flow_dissector_target(flow_dissector,
507 FLOW_DISSECTOR_KEY_HASH,
510 key->hash = skb_get_hash_raw(skb);
512 EXPORT_SYMBOL(skb_flow_dissect_hash);
514 static enum flow_dissect_ret
515 __skb_flow_dissect_mpls(const struct sk_buff *skb,
516 struct flow_dissector *flow_dissector,
517 void *target_container, const void *data, int nhoff,
518 int hlen, int lse_index, bool *entropy_label)
520 struct mpls_label *hdr, _hdr;
521 u32 entry, label, bos;
523 if (!dissector_uses_key(flow_dissector,
524 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
525 !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
526 return FLOW_DISSECT_RET_OUT_GOOD;
528 if (lse_index >= FLOW_DIS_MPLS_MAX)
529 return FLOW_DISSECT_RET_OUT_GOOD;
531 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
534 return FLOW_DISSECT_RET_OUT_BAD;
536 entry = ntohl(hdr->entry);
537 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
538 bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
540 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
541 struct flow_dissector_key_mpls *key_mpls;
542 struct flow_dissector_mpls_lse *lse;
544 key_mpls = skb_flow_dissector_target(flow_dissector,
545 FLOW_DISSECTOR_KEY_MPLS,
547 lse = &key_mpls->ls[lse_index];
549 lse->mpls_ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
551 lse->mpls_tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
552 lse->mpls_label = label;
553 dissector_set_mpls_lse(key_mpls, lse_index);
556 if (*entropy_label &&
557 dissector_uses_key(flow_dissector,
558 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
559 struct flow_dissector_key_keyid *key_keyid;
561 key_keyid = skb_flow_dissector_target(flow_dissector,
562 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
564 key_keyid->keyid = cpu_to_be32(label);
567 *entropy_label = label == MPLS_LABEL_ENTROPY;
569 return bos ? FLOW_DISSECT_RET_OUT_GOOD : FLOW_DISSECT_RET_PROTO_AGAIN;
572 static enum flow_dissect_ret
573 __skb_flow_dissect_arp(const struct sk_buff *skb,
574 struct flow_dissector *flow_dissector,
575 void *target_container, const void *data,
578 struct flow_dissector_key_arp *key_arp;
580 unsigned char ar_sha[ETH_ALEN];
581 unsigned char ar_sip[4];
582 unsigned char ar_tha[ETH_ALEN];
583 unsigned char ar_tip[4];
584 } *arp_eth, _arp_eth;
585 const struct arphdr *arp;
588 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
589 return FLOW_DISSECT_RET_OUT_GOOD;
591 arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
594 return FLOW_DISSECT_RET_OUT_BAD;
596 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
597 arp->ar_pro != htons(ETH_P_IP) ||
598 arp->ar_hln != ETH_ALEN ||
600 (arp->ar_op != htons(ARPOP_REPLY) &&
601 arp->ar_op != htons(ARPOP_REQUEST)))
602 return FLOW_DISSECT_RET_OUT_BAD;
604 arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
605 sizeof(_arp_eth), data,
608 return FLOW_DISSECT_RET_OUT_BAD;
610 key_arp = skb_flow_dissector_target(flow_dissector,
611 FLOW_DISSECTOR_KEY_ARP,
614 memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
615 memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
617 /* Only store the lower byte of the opcode;
618 * this covers ARPOP_REPLY and ARPOP_REQUEST.
620 key_arp->op = ntohs(arp->ar_op) & 0xff;
622 ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
623 ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
625 return FLOW_DISSECT_RET_OUT_GOOD;
628 static enum flow_dissect_ret
629 __skb_flow_dissect_cfm(const struct sk_buff *skb,
630 struct flow_dissector *flow_dissector,
631 void *target_container, const void *data,
634 struct flow_dissector_key_cfm *key, *hdr, _hdr;
636 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_CFM))
637 return FLOW_DISSECT_RET_OUT_GOOD;
639 hdr = __skb_header_pointer(skb, nhoff, sizeof(*key), data, hlen, &_hdr);
641 return FLOW_DISSECT_RET_OUT_BAD;
643 key = skb_flow_dissector_target(flow_dissector, FLOW_DISSECTOR_KEY_CFM,
646 key->mdl_ver = hdr->mdl_ver;
647 key->opcode = hdr->opcode;
649 return FLOW_DISSECT_RET_OUT_GOOD;
652 static enum flow_dissect_ret
653 __skb_flow_dissect_gre(const struct sk_buff *skb,
654 struct flow_dissector_key_control *key_control,
655 struct flow_dissector *flow_dissector,
656 void *target_container, const void *data,
657 __be16 *p_proto, int *p_nhoff, int *p_hlen,
660 struct flow_dissector_key_keyid *key_keyid;
661 struct gre_base_hdr *hdr, _hdr;
665 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
666 data, *p_hlen, &_hdr);
668 return FLOW_DISSECT_RET_OUT_BAD;
670 /* Only look inside GRE without routing */
671 if (hdr->flags & GRE_ROUTING)
672 return FLOW_DISSECT_RET_OUT_GOOD;
674 /* Only look inside GRE for version 0 and 1 */
675 gre_ver = ntohs(hdr->flags & GRE_VERSION);
677 return FLOW_DISSECT_RET_OUT_GOOD;
679 *p_proto = hdr->protocol;
681 /* Version1 must be PPTP, and check the flags */
682 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
683 return FLOW_DISSECT_RET_OUT_GOOD;
686 offset += sizeof(struct gre_base_hdr);
688 if (hdr->flags & GRE_CSUM)
689 offset += sizeof_field(struct gre_full_hdr, csum) +
690 sizeof_field(struct gre_full_hdr, reserved1);
692 if (hdr->flags & GRE_KEY) {
696 keyid = __skb_header_pointer(skb, *p_nhoff + offset,
698 data, *p_hlen, &_keyid);
700 return FLOW_DISSECT_RET_OUT_BAD;
702 if (dissector_uses_key(flow_dissector,
703 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
704 key_keyid = skb_flow_dissector_target(flow_dissector,
705 FLOW_DISSECTOR_KEY_GRE_KEYID,
708 key_keyid->keyid = *keyid;
710 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
712 offset += sizeof_field(struct gre_full_hdr, key);
715 if (hdr->flags & GRE_SEQ)
716 offset += sizeof_field(struct pptp_gre_header, seq);
719 if (*p_proto == htons(ETH_P_TEB)) {
720 const struct ethhdr *eth;
723 eth = __skb_header_pointer(skb, *p_nhoff + offset,
725 data, *p_hlen, &_eth);
727 return FLOW_DISSECT_RET_OUT_BAD;
728 *p_proto = eth->h_proto;
729 offset += sizeof(*eth);
731 /* Cap headers that we access via pointers at the
732 * end of the Ethernet header as our maximum alignment
733 * at that point is only 2 bytes.
736 *p_hlen = *p_nhoff + offset;
738 } else { /* version 1, must be PPTP */
739 u8 _ppp_hdr[PPP_HDRLEN];
742 if (hdr->flags & GRE_ACK)
743 offset += sizeof_field(struct pptp_gre_header, ack);
745 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
747 data, *p_hlen, _ppp_hdr);
749 return FLOW_DISSECT_RET_OUT_BAD;
751 switch (PPP_PROTOCOL(ppp_hdr)) {
753 *p_proto = htons(ETH_P_IP);
756 *p_proto = htons(ETH_P_IPV6);
759 /* Could probably catch some more like MPLS */
763 offset += PPP_HDRLEN;
767 key_control->flags |= FLOW_DIS_ENCAPSULATION;
768 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
769 return FLOW_DISSECT_RET_OUT_GOOD;
771 return FLOW_DISSECT_RET_PROTO_AGAIN;
775 * __skb_flow_dissect_batadv() - dissect batman-adv header
776 * @skb: sk_buff to with the batman-adv header
777 * @key_control: flow dissectors control key
778 * @data: raw buffer pointer to the packet, if NULL use skb->data
779 * @p_proto: pointer used to update the protocol to process next
780 * @p_nhoff: pointer used to update inner network header offset
781 * @hlen: packet header length
782 * @flags: any combination of FLOW_DISSECTOR_F_*
784 * ETH_P_BATMAN packets are tried to be dissected. Only
785 * &struct batadv_unicast packets are actually processed because they contain an
786 * inner ethernet header and are usually followed by actual network header. This
787 * allows the flow dissector to continue processing the packet.
789 * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found,
790 * FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation,
791 * otherwise FLOW_DISSECT_RET_OUT_BAD
793 static enum flow_dissect_ret
794 __skb_flow_dissect_batadv(const struct sk_buff *skb,
795 struct flow_dissector_key_control *key_control,
796 const void *data, __be16 *p_proto, int *p_nhoff,
797 int hlen, unsigned int flags)
800 struct batadv_unicast_packet batadv_unicast;
804 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen,
807 return FLOW_DISSECT_RET_OUT_BAD;
809 if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION)
810 return FLOW_DISSECT_RET_OUT_BAD;
812 if (hdr->batadv_unicast.packet_type != BATADV_UNICAST)
813 return FLOW_DISSECT_RET_OUT_BAD;
815 *p_proto = hdr->eth.h_proto;
816 *p_nhoff += sizeof(*hdr);
818 key_control->flags |= FLOW_DIS_ENCAPSULATION;
819 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
820 return FLOW_DISSECT_RET_OUT_GOOD;
822 return FLOW_DISSECT_RET_PROTO_AGAIN;
826 __skb_flow_dissect_tcp(const struct sk_buff *skb,
827 struct flow_dissector *flow_dissector,
828 void *target_container, const void *data,
831 struct flow_dissector_key_tcp *key_tcp;
832 struct tcphdr *th, _th;
834 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
837 th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
841 if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
844 key_tcp = skb_flow_dissector_target(flow_dissector,
845 FLOW_DISSECTOR_KEY_TCP,
847 key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
851 __skb_flow_dissect_ports(const struct sk_buff *skb,
852 struct flow_dissector *flow_dissector,
853 void *target_container, const void *data,
854 int nhoff, u8 ip_proto, int hlen)
856 enum flow_dissector_key_id dissector_ports = FLOW_DISSECTOR_KEY_MAX;
857 struct flow_dissector_key_ports *key_ports;
859 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
860 dissector_ports = FLOW_DISSECTOR_KEY_PORTS;
861 else if (dissector_uses_key(flow_dissector,
862 FLOW_DISSECTOR_KEY_PORTS_RANGE))
863 dissector_ports = FLOW_DISSECTOR_KEY_PORTS_RANGE;
865 if (dissector_ports == FLOW_DISSECTOR_KEY_MAX)
868 key_ports = skb_flow_dissector_target(flow_dissector,
871 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
876 __skb_flow_dissect_ipv4(const struct sk_buff *skb,
877 struct flow_dissector *flow_dissector,
878 void *target_container, const void *data,
879 const struct iphdr *iph)
881 struct flow_dissector_key_ip *key_ip;
883 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
886 key_ip = skb_flow_dissector_target(flow_dissector,
887 FLOW_DISSECTOR_KEY_IP,
889 key_ip->tos = iph->tos;
890 key_ip->ttl = iph->ttl;
894 __skb_flow_dissect_ipv6(const struct sk_buff *skb,
895 struct flow_dissector *flow_dissector,
896 void *target_container, const void *data,
897 const struct ipv6hdr *iph)
899 struct flow_dissector_key_ip *key_ip;
901 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
904 key_ip = skb_flow_dissector_target(flow_dissector,
905 FLOW_DISSECTOR_KEY_IP,
907 key_ip->tos = ipv6_get_dsfield(iph);
908 key_ip->ttl = iph->hop_limit;
911 /* Maximum number of protocol headers that can be parsed in
914 #define MAX_FLOW_DISSECT_HDRS 15
916 static bool skb_flow_dissect_allowed(int *num_hdrs)
920 return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
923 static void __skb_flow_bpf_to_target(const struct bpf_flow_keys *flow_keys,
924 struct flow_dissector *flow_dissector,
925 void *target_container)
927 struct flow_dissector_key_ports *key_ports = NULL;
928 struct flow_dissector_key_control *key_control;
929 struct flow_dissector_key_basic *key_basic;
930 struct flow_dissector_key_addrs *key_addrs;
931 struct flow_dissector_key_tags *key_tags;
933 key_control = skb_flow_dissector_target(flow_dissector,
934 FLOW_DISSECTOR_KEY_CONTROL,
936 key_control->thoff = flow_keys->thoff;
937 if (flow_keys->is_frag)
938 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
939 if (flow_keys->is_first_frag)
940 key_control->flags |= FLOW_DIS_FIRST_FRAG;
941 if (flow_keys->is_encap)
942 key_control->flags |= FLOW_DIS_ENCAPSULATION;
944 key_basic = skb_flow_dissector_target(flow_dissector,
945 FLOW_DISSECTOR_KEY_BASIC,
947 key_basic->n_proto = flow_keys->n_proto;
948 key_basic->ip_proto = flow_keys->ip_proto;
950 if (flow_keys->addr_proto == ETH_P_IP &&
951 dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
952 key_addrs = skb_flow_dissector_target(flow_dissector,
953 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
955 key_addrs->v4addrs.src = flow_keys->ipv4_src;
956 key_addrs->v4addrs.dst = flow_keys->ipv4_dst;
957 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
958 } else if (flow_keys->addr_proto == ETH_P_IPV6 &&
959 dissector_uses_key(flow_dissector,
960 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
961 key_addrs = skb_flow_dissector_target(flow_dissector,
962 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
964 memcpy(&key_addrs->v6addrs.src, &flow_keys->ipv6_src,
965 sizeof(key_addrs->v6addrs.src));
966 memcpy(&key_addrs->v6addrs.dst, &flow_keys->ipv6_dst,
967 sizeof(key_addrs->v6addrs.dst));
968 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
971 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
972 key_ports = skb_flow_dissector_target(flow_dissector,
973 FLOW_DISSECTOR_KEY_PORTS,
975 else if (dissector_uses_key(flow_dissector,
976 FLOW_DISSECTOR_KEY_PORTS_RANGE))
977 key_ports = skb_flow_dissector_target(flow_dissector,
978 FLOW_DISSECTOR_KEY_PORTS_RANGE,
982 key_ports->src = flow_keys->sport;
983 key_ports->dst = flow_keys->dport;
986 if (dissector_uses_key(flow_dissector,
987 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
988 key_tags = skb_flow_dissector_target(flow_dissector,
989 FLOW_DISSECTOR_KEY_FLOW_LABEL,
991 key_tags->flow_label = ntohl(flow_keys->flow_label);
995 u32 bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
996 __be16 proto, int nhoff, int hlen, unsigned int flags)
998 struct bpf_flow_keys *flow_keys = ctx->flow_keys;
1001 /* Pass parameters to the BPF program */
1002 memset(flow_keys, 0, sizeof(*flow_keys));
1003 flow_keys->n_proto = proto;
1004 flow_keys->nhoff = nhoff;
1005 flow_keys->thoff = flow_keys->nhoff;
1007 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG !=
1008 (int)FLOW_DISSECTOR_F_PARSE_1ST_FRAG);
1009 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL !=
1010 (int)FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1011 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP !=
1012 (int)FLOW_DISSECTOR_F_STOP_AT_ENCAP);
1013 flow_keys->flags = flags;
1015 result = bpf_prog_run_pin_on_cpu(prog, ctx);
1017 flow_keys->nhoff = clamp_t(u16, flow_keys->nhoff, nhoff, hlen);
1018 flow_keys->thoff = clamp_t(u16, flow_keys->thoff,
1019 flow_keys->nhoff, hlen);
1024 static bool is_pppoe_ses_hdr_valid(const struct pppoe_hdr *hdr)
1026 return hdr->ver == 1 && hdr->type == 1 && hdr->code == 0;
1030 * __skb_flow_dissect - extract the flow_keys struct and return it
1031 * @net: associated network namespace, derived from @skb if NULL
1032 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
1033 * @flow_dissector: list of keys to dissect
1034 * @target_container: target structure to put dissected values into
1035 * @data: raw buffer pointer to the packet, if NULL use skb->data
1036 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
1037 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
1038 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
1039 * @flags: flags that control the dissection process, e.g.
1040 * FLOW_DISSECTOR_F_STOP_AT_ENCAP.
1042 * The function will try to retrieve individual keys into target specified
1043 * by flow_dissector from either the skbuff or a raw buffer specified by the
1046 * Caller must take care of zeroing target container memory.
1048 bool __skb_flow_dissect(const struct net *net,
1049 const struct sk_buff *skb,
1050 struct flow_dissector *flow_dissector,
1051 void *target_container, const void *data,
1052 __be16 proto, int nhoff, int hlen, unsigned int flags)
1054 struct flow_dissector_key_control *key_control;
1055 struct flow_dissector_key_basic *key_basic;
1056 struct flow_dissector_key_addrs *key_addrs;
1057 struct flow_dissector_key_tags *key_tags;
1058 struct flow_dissector_key_vlan *key_vlan;
1059 enum flow_dissect_ret fdret;
1060 enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
1061 bool mpls_el = false;
1069 proto = skb_vlan_tag_present(skb) ?
1070 skb->vlan_proto : skb->protocol;
1071 nhoff = skb_network_offset(skb);
1072 hlen = skb_headlen(skb);
1073 #if IS_ENABLED(CONFIG_NET_DSA)
1074 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
1075 proto == htons(ETH_P_XDSA))) {
1076 struct metadata_dst *md_dst = skb_metadata_dst(skb);
1077 const struct dsa_device_ops *ops;
1080 ops = skb->dev->dsa_ptr->tag_ops;
1081 /* Only DSA header taggers break flow dissection */
1082 if (ops->needed_headroom &&
1083 (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)) {
1084 if (ops->flow_dissect)
1085 ops->flow_dissect(skb, &proto, &offset);
1087 dsa_tag_generic_flow_dissect(skb,
1097 /* It is ensured by skb_flow_dissector_init() that control key will
1098 * be always present.
1100 key_control = skb_flow_dissector_target(flow_dissector,
1101 FLOW_DISSECTOR_KEY_CONTROL,
1104 /* It is ensured by skb_flow_dissector_init() that basic key will
1105 * be always present.
1107 key_basic = skb_flow_dissector_target(flow_dissector,
1108 FLOW_DISSECTOR_KEY_BASIC,
1114 net = dev_net(skb->dev);
1116 net = sock_net(skb->sk);
1120 DEBUG_NET_WARN_ON_ONCE(!net);
1122 enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
1123 struct bpf_prog_array *run_array;
1126 run_array = rcu_dereference(init_net.bpf.run_array[type]);
1128 run_array = rcu_dereference(net->bpf.run_array[type]);
1131 struct bpf_flow_keys flow_keys;
1132 struct bpf_flow_dissector ctx = {
1133 .flow_keys = &flow_keys,
1135 .data_end = data + hlen,
1137 __be16 n_proto = proto;
1138 struct bpf_prog *prog;
1143 /* we can't use 'proto' in the skb case
1144 * because it might be set to skb->vlan_proto
1145 * which has been pulled from the data
1147 n_proto = skb->protocol;
1150 prog = READ_ONCE(run_array->items[0].prog);
1151 result = bpf_flow_dissect(prog, &ctx, n_proto, nhoff,
1153 if (result == BPF_FLOW_DISSECTOR_CONTINUE)
1154 goto dissect_continue;
1155 __skb_flow_bpf_to_target(&flow_keys, flow_dissector,
1158 return result == BPF_OK;
1164 if (dissector_uses_key(flow_dissector,
1165 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1166 struct ethhdr *eth = eth_hdr(skb);
1167 struct flow_dissector_key_eth_addrs *key_eth_addrs;
1169 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
1170 FLOW_DISSECTOR_KEY_ETH_ADDRS,
1172 memcpy(key_eth_addrs, eth, sizeof(*key_eth_addrs));
1175 if (dissector_uses_key(flow_dissector,
1176 FLOW_DISSECTOR_KEY_NUM_OF_VLANS)) {
1177 struct flow_dissector_key_num_of_vlans *key_num_of_vlans;
1179 key_num_of_vlans = skb_flow_dissector_target(flow_dissector,
1180 FLOW_DISSECTOR_KEY_NUM_OF_VLANS,
1182 key_num_of_vlans->num_of_vlans = 0;
1186 fdret = FLOW_DISSECT_RET_CONTINUE;
1189 case htons(ETH_P_IP): {
1190 const struct iphdr *iph;
1193 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1194 if (!iph || iph->ihl < 5) {
1195 fdret = FLOW_DISSECT_RET_OUT_BAD;
1199 nhoff += iph->ihl * 4;
1201 ip_proto = iph->protocol;
1203 if (dissector_uses_key(flow_dissector,
1204 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
1205 key_addrs = skb_flow_dissector_target(flow_dissector,
1206 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1209 memcpy(&key_addrs->v4addrs.src, &iph->saddr,
1210 sizeof(key_addrs->v4addrs.src));
1211 memcpy(&key_addrs->v4addrs.dst, &iph->daddr,
1212 sizeof(key_addrs->v4addrs.dst));
1213 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1216 __skb_flow_dissect_ipv4(skb, flow_dissector,
1217 target_container, data, iph);
1219 if (ip_is_fragment(iph)) {
1220 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1222 if (iph->frag_off & htons(IP_OFFSET)) {
1223 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1226 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1228 FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
1229 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1237 case htons(ETH_P_IPV6): {
1238 const struct ipv6hdr *iph;
1239 struct ipv6hdr _iph;
1241 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1243 fdret = FLOW_DISSECT_RET_OUT_BAD;
1247 ip_proto = iph->nexthdr;
1248 nhoff += sizeof(struct ipv6hdr);
1250 if (dissector_uses_key(flow_dissector,
1251 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
1252 key_addrs = skb_flow_dissector_target(flow_dissector,
1253 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1256 memcpy(&key_addrs->v6addrs.src, &iph->saddr,
1257 sizeof(key_addrs->v6addrs.src));
1258 memcpy(&key_addrs->v6addrs.dst, &iph->daddr,
1259 sizeof(key_addrs->v6addrs.dst));
1260 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1263 if ((dissector_uses_key(flow_dissector,
1264 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
1265 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
1266 ip6_flowlabel(iph)) {
1267 __be32 flow_label = ip6_flowlabel(iph);
1269 if (dissector_uses_key(flow_dissector,
1270 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
1271 key_tags = skb_flow_dissector_target(flow_dissector,
1272 FLOW_DISSECTOR_KEY_FLOW_LABEL,
1274 key_tags->flow_label = ntohl(flow_label);
1276 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
1277 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1282 __skb_flow_dissect_ipv6(skb, flow_dissector,
1283 target_container, data, iph);
1287 case htons(ETH_P_8021AD):
1288 case htons(ETH_P_8021Q): {
1289 const struct vlan_hdr *vlan = NULL;
1290 struct vlan_hdr _vlan;
1291 __be16 saved_vlan_tpid = proto;
1293 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
1294 skb && skb_vlan_tag_present(skb)) {
1295 proto = skb->protocol;
1297 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
1298 data, hlen, &_vlan);
1300 fdret = FLOW_DISSECT_RET_OUT_BAD;
1304 proto = vlan->h_vlan_encapsulated_proto;
1305 nhoff += sizeof(*vlan);
1308 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_NUM_OF_VLANS) &&
1309 !(key_control->flags & FLOW_DIS_ENCAPSULATION)) {
1310 struct flow_dissector_key_num_of_vlans *key_nvs;
1312 key_nvs = skb_flow_dissector_target(flow_dissector,
1313 FLOW_DISSECTOR_KEY_NUM_OF_VLANS,
1315 key_nvs->num_of_vlans++;
1318 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
1319 dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
1320 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
1321 dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
1323 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1327 if (dissector_uses_key(flow_dissector, dissector_vlan)) {
1328 key_vlan = skb_flow_dissector_target(flow_dissector,
1333 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
1334 key_vlan->vlan_priority = skb_vlan_tag_get_prio(skb);
1336 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
1338 key_vlan->vlan_priority =
1339 (ntohs(vlan->h_vlan_TCI) &
1340 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1342 key_vlan->vlan_tpid = saved_vlan_tpid;
1343 key_vlan->vlan_eth_type = proto;
1346 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1349 case htons(ETH_P_PPP_SES): {
1351 struct pppoe_hdr hdr;
1356 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
1358 fdret = FLOW_DISSECT_RET_OUT_BAD;
1362 if (!is_pppoe_ses_hdr_valid(&hdr->hdr)) {
1363 fdret = FLOW_DISSECT_RET_OUT_BAD;
1367 /* least significant bit of the most significant octet
1368 * indicates if protocol field was compressed
1370 ppp_proto = ntohs(hdr->proto);
1371 if (ppp_proto & 0x0100) {
1372 ppp_proto = ppp_proto >> 8;
1373 nhoff += PPPOE_SES_HLEN - 1;
1375 nhoff += PPPOE_SES_HLEN;
1378 if (ppp_proto == PPP_IP) {
1379 proto = htons(ETH_P_IP);
1380 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1381 } else if (ppp_proto == PPP_IPV6) {
1382 proto = htons(ETH_P_IPV6);
1383 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1384 } else if (ppp_proto == PPP_MPLS_UC) {
1385 proto = htons(ETH_P_MPLS_UC);
1386 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1387 } else if (ppp_proto == PPP_MPLS_MC) {
1388 proto = htons(ETH_P_MPLS_MC);
1389 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1390 } else if (ppp_proto_is_valid(ppp_proto)) {
1391 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1393 fdret = FLOW_DISSECT_RET_OUT_BAD;
1397 if (dissector_uses_key(flow_dissector,
1398 FLOW_DISSECTOR_KEY_PPPOE)) {
1399 struct flow_dissector_key_pppoe *key_pppoe;
1401 key_pppoe = skb_flow_dissector_target(flow_dissector,
1402 FLOW_DISSECTOR_KEY_PPPOE,
1404 key_pppoe->session_id = hdr->hdr.sid;
1405 key_pppoe->ppp_proto = htons(ppp_proto);
1406 key_pppoe->type = htons(ETH_P_PPP_SES);
1410 case htons(ETH_P_TIPC): {
1411 struct tipc_basic_hdr *hdr, _hdr;
1413 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
1416 fdret = FLOW_DISSECT_RET_OUT_BAD;
1420 if (dissector_uses_key(flow_dissector,
1421 FLOW_DISSECTOR_KEY_TIPC)) {
1422 key_addrs = skb_flow_dissector_target(flow_dissector,
1423 FLOW_DISSECTOR_KEY_TIPC,
1425 key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
1426 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
1428 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1432 case htons(ETH_P_MPLS_UC):
1433 case htons(ETH_P_MPLS_MC):
1434 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
1435 target_container, data,
1436 nhoff, hlen, mpls_lse,
1438 nhoff += sizeof(struct mpls_label);
1441 case htons(ETH_P_FCOE):
1442 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
1443 fdret = FLOW_DISSECT_RET_OUT_BAD;
1447 nhoff += FCOE_HEADER_LEN;
1448 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1451 case htons(ETH_P_ARP):
1452 case htons(ETH_P_RARP):
1453 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
1454 target_container, data,
1458 case htons(ETH_P_BATMAN):
1459 fdret = __skb_flow_dissect_batadv(skb, key_control, data,
1460 &proto, &nhoff, hlen, flags);
1463 case htons(ETH_P_1588): {
1464 struct ptp_header *hdr, _hdr;
1466 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
1469 fdret = FLOW_DISSECT_RET_OUT_BAD;
1473 nhoff += sizeof(struct ptp_header);
1474 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1478 case htons(ETH_P_PRP):
1479 case htons(ETH_P_HSR): {
1480 struct hsr_tag *hdr, _hdr;
1482 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen,
1485 fdret = FLOW_DISSECT_RET_OUT_BAD;
1489 proto = hdr->encap_proto;
1491 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1495 case htons(ETH_P_CFM):
1496 fdret = __skb_flow_dissect_cfm(skb, flow_dissector,
1497 target_container, data,
1502 fdret = FLOW_DISSECT_RET_OUT_BAD;
1506 /* Process result of proto processing */
1508 case FLOW_DISSECT_RET_OUT_GOOD:
1510 case FLOW_DISSECT_RET_PROTO_AGAIN:
1511 if (skb_flow_dissect_allowed(&num_hdrs))
1514 case FLOW_DISSECT_RET_CONTINUE:
1515 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1517 case FLOW_DISSECT_RET_OUT_BAD:
1523 fdret = FLOW_DISSECT_RET_CONTINUE;
1527 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1528 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1532 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
1533 target_container, data,
1534 &proto, &nhoff, &hlen, flags);
1538 case NEXTHDR_ROUTING:
1539 case NEXTHDR_DEST: {
1540 u8 _opthdr[2], *opthdr;
1542 if (proto != htons(ETH_P_IPV6))
1545 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
1546 data, hlen, &_opthdr);
1548 fdret = FLOW_DISSECT_RET_OUT_BAD;
1552 ip_proto = opthdr[0];
1553 nhoff += (opthdr[1] + 1) << 3;
1555 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1558 case NEXTHDR_FRAGMENT: {
1559 struct frag_hdr _fh, *fh;
1561 if (proto != htons(ETH_P_IPV6))
1564 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
1568 fdret = FLOW_DISSECT_RET_OUT_BAD;
1572 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1574 nhoff += sizeof(_fh);
1575 ip_proto = fh->nexthdr;
1577 if (!(fh->frag_off & htons(IP6_OFFSET))) {
1578 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1579 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
1580 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1585 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1589 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1590 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1594 proto = htons(ETH_P_IP);
1596 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1597 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1598 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1602 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1606 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) {
1607 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1611 proto = htons(ETH_P_IPV6);
1613 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1614 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1615 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1619 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1624 proto = htons(ETH_P_MPLS_UC);
1625 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1629 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1634 case IPPROTO_ICMPV6:
1635 __skb_flow_dissect_icmp(skb, flow_dissector, target_container,
1639 __skb_flow_dissect_l2tpv3(skb, flow_dissector, target_container,
1643 __skb_flow_dissect_esp(skb, flow_dissector, target_container,
1647 __skb_flow_dissect_ah(skb, flow_dissector, target_container,
1654 if (!(key_control->flags & FLOW_DIS_IS_FRAGMENT))
1655 __skb_flow_dissect_ports(skb, flow_dissector, target_container,
1656 data, nhoff, ip_proto, hlen);
1658 /* Process result of IP proto processing */
1660 case FLOW_DISSECT_RET_PROTO_AGAIN:
1661 if (skb_flow_dissect_allowed(&num_hdrs))
1664 case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1665 if (skb_flow_dissect_allowed(&num_hdrs))
1666 goto ip_proto_again;
1668 case FLOW_DISSECT_RET_OUT_GOOD:
1669 case FLOW_DISSECT_RET_CONTINUE:
1671 case FLOW_DISSECT_RET_OUT_BAD:
1680 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1681 key_basic->n_proto = proto;
1682 key_basic->ip_proto = ip_proto;
1690 EXPORT_SYMBOL(__skb_flow_dissect);
1692 static siphash_aligned_key_t hashrnd;
1693 static __always_inline void __flow_hash_secret_init(void)
1695 net_get_random_once(&hashrnd, sizeof(hashrnd));
1698 static const void *flow_keys_hash_start(const struct flow_keys *flow)
1700 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1701 return &flow->FLOW_KEYS_HASH_START_FIELD;
1704 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1706 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1708 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
1710 switch (flow->control.addr_type) {
1711 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1712 diff -= sizeof(flow->addrs.v4addrs);
1714 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1715 diff -= sizeof(flow->addrs.v6addrs);
1717 case FLOW_DISSECTOR_KEY_TIPC:
1718 diff -= sizeof(flow->addrs.tipckey);
1721 return sizeof(*flow) - diff;
1724 __be32 flow_get_u32_src(const struct flow_keys *flow)
1726 switch (flow->control.addr_type) {
1727 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1728 return flow->addrs.v4addrs.src;
1729 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1730 return (__force __be32)ipv6_addr_hash(
1731 &flow->addrs.v6addrs.src);
1732 case FLOW_DISSECTOR_KEY_TIPC:
1733 return flow->addrs.tipckey.key;
1738 EXPORT_SYMBOL(flow_get_u32_src);
1740 __be32 flow_get_u32_dst(const struct flow_keys *flow)
1742 switch (flow->control.addr_type) {
1743 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1744 return flow->addrs.v4addrs.dst;
1745 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1746 return (__force __be32)ipv6_addr_hash(
1747 &flow->addrs.v6addrs.dst);
1752 EXPORT_SYMBOL(flow_get_u32_dst);
1754 /* Sort the source and destination IP and the ports,
1755 * to have consistent hash within the two directions
1757 static inline void __flow_hash_consistentify(struct flow_keys *keys)
1761 switch (keys->control.addr_type) {
1762 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1763 if ((__force u32)keys->addrs.v4addrs.dst <
1764 (__force u32)keys->addrs.v4addrs.src)
1765 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1767 if ((__force u16)keys->ports.dst <
1768 (__force u16)keys->ports.src) {
1769 swap(keys->ports.src, keys->ports.dst);
1772 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1773 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1774 &keys->addrs.v6addrs.src,
1775 sizeof(keys->addrs.v6addrs.dst));
1776 if (addr_diff < 0) {
1777 for (i = 0; i < 4; i++)
1778 swap(keys->addrs.v6addrs.src.s6_addr32[i],
1779 keys->addrs.v6addrs.dst.s6_addr32[i]);
1781 if ((__force u16)keys->ports.dst <
1782 (__force u16)keys->ports.src) {
1783 swap(keys->ports.src, keys->ports.dst);
1789 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1790 const siphash_key_t *keyval)
1794 __flow_hash_consistentify(keys);
1796 hash = siphash(flow_keys_hash_start(keys),
1797 flow_keys_hash_length(keys), keyval);
1804 u32 flow_hash_from_keys(struct flow_keys *keys)
1806 __flow_hash_secret_init();
1807 return __flow_hash_from_keys(keys, &hashrnd);
1809 EXPORT_SYMBOL(flow_hash_from_keys);
1811 u32 flow_hash_from_keys_seed(struct flow_keys *keys,
1812 const siphash_key_t *keyval)
1814 return __flow_hash_from_keys(keys, keyval);
1816 EXPORT_SYMBOL(flow_hash_from_keys_seed);
1818 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1819 struct flow_keys *keys,
1820 const siphash_key_t *keyval)
1822 skb_flow_dissect_flow_keys(skb, keys,
1823 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1825 return __flow_hash_from_keys(keys, keyval);
1828 struct _flow_keys_digest_data {
1837 void make_flow_keys_digest(struct flow_keys_digest *digest,
1838 const struct flow_keys *flow)
1840 struct _flow_keys_digest_data *data =
1841 (struct _flow_keys_digest_data *)digest;
1843 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1845 memset(digest, 0, sizeof(*digest));
1847 data->n_proto = flow->basic.n_proto;
1848 data->ip_proto = flow->basic.ip_proto;
1849 data->ports = flow->ports.ports;
1850 data->src = flow->addrs.v4addrs.src;
1851 data->dst = flow->addrs.v4addrs.dst;
1853 EXPORT_SYMBOL(make_flow_keys_digest);
1855 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1857 u32 __skb_get_hash_symmetric_net(const struct net *net, const struct sk_buff *skb)
1859 struct flow_keys keys;
1861 __flow_hash_secret_init();
1863 memset(&keys, 0, sizeof(keys));
1864 __skb_flow_dissect(net, skb, &flow_keys_dissector_symmetric,
1865 &keys, NULL, 0, 0, 0, 0);
1867 return __flow_hash_from_keys(&keys, &hashrnd);
1869 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric_net);
1872 * __skb_get_hash_net: calculate a flow hash
1873 * @net: associated network namespace, derived from @skb if NULL
1874 * @skb: sk_buff to calculate flow hash from
1876 * This function calculates a flow hash based on src/dst addresses
1877 * and src/dst port numbers. Sets hash in skb to non-zero hash value
1878 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
1879 * if hash is a canonical 4-tuple hash over transport ports.
1881 void __skb_get_hash_net(const struct net *net, struct sk_buff *skb)
1883 struct flow_keys keys;
1886 memset(&keys, 0, sizeof(keys));
1888 __skb_flow_dissect(net, skb, &flow_keys_dissector,
1889 &keys, NULL, 0, 0, 0,
1890 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1892 __flow_hash_secret_init();
1894 hash = __flow_hash_from_keys(&keys, &hashrnd);
1896 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1898 EXPORT_SYMBOL(__skb_get_hash_net);
1900 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1901 const siphash_key_t *perturb)
1903 struct flow_keys keys;
1905 return ___skb_get_hash(skb, &keys, perturb);
1907 EXPORT_SYMBOL(skb_get_hash_perturb);
1909 u32 __skb_get_poff(const struct sk_buff *skb, const void *data,
1910 const struct flow_keys_basic *keys, int hlen)
1912 u32 poff = keys->control.thoff;
1914 /* skip L4 headers for fragments after the first */
1915 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1916 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1919 switch (keys->basic.ip_proto) {
1921 /* access doff as u8 to avoid unaligned access */
1925 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1926 data, hlen, &_doff);
1930 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1934 case IPPROTO_UDPLITE:
1935 poff += sizeof(struct udphdr);
1937 /* For the rest, we do not really care about header
1938 * extensions at this point for now.
1941 poff += sizeof(struct icmphdr);
1943 case IPPROTO_ICMPV6:
1944 poff += sizeof(struct icmp6hdr);
1947 poff += sizeof(struct igmphdr);
1950 poff += sizeof(struct dccp_hdr);
1953 poff += sizeof(struct sctphdr);
1961 * skb_get_poff - get the offset to the payload
1962 * @skb: sk_buff to get the payload offset from
1964 * The function will get the offset to the payload as far as it could
1965 * be dissected. The main user is currently BPF, so that we can dynamically
1966 * truncate packets without needing to push actual payload to the user
1967 * space and can analyze headers only, instead.
1969 u32 skb_get_poff(const struct sk_buff *skb)
1971 struct flow_keys_basic keys;
1973 if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
1977 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1980 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1982 memset(keys, 0, sizeof(*keys));
1984 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1985 sizeof(keys->addrs.v6addrs.src));
1986 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1987 sizeof(keys->addrs.v6addrs.dst));
1988 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1989 keys->ports.src = fl6->fl6_sport;
1990 keys->ports.dst = fl6->fl6_dport;
1991 keys->keyid.keyid = fl6->fl6_gre_key;
1992 keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1993 keys->basic.ip_proto = fl6->flowi6_proto;
1995 return flow_hash_from_keys(keys);
1997 EXPORT_SYMBOL(__get_hash_from_flowi6);
1999 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
2001 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
2002 .offset = offsetof(struct flow_keys, control),
2005 .key_id = FLOW_DISSECTOR_KEY_BASIC,
2006 .offset = offsetof(struct flow_keys, basic),
2009 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2010 .offset = offsetof(struct flow_keys, addrs.v4addrs),
2013 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2014 .offset = offsetof(struct flow_keys, addrs.v6addrs),
2017 .key_id = FLOW_DISSECTOR_KEY_TIPC,
2018 .offset = offsetof(struct flow_keys, addrs.tipckey),
2021 .key_id = FLOW_DISSECTOR_KEY_PORTS,
2022 .offset = offsetof(struct flow_keys, ports),
2025 .key_id = FLOW_DISSECTOR_KEY_VLAN,
2026 .offset = offsetof(struct flow_keys, vlan),
2029 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
2030 .offset = offsetof(struct flow_keys, tags),
2033 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
2034 .offset = offsetof(struct flow_keys, keyid),
2038 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
2040 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
2041 .offset = offsetof(struct flow_keys, control),
2044 .key_id = FLOW_DISSECTOR_KEY_BASIC,
2045 .offset = offsetof(struct flow_keys, basic),
2048 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2049 .offset = offsetof(struct flow_keys, addrs.v4addrs),
2052 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2053 .offset = offsetof(struct flow_keys, addrs.v6addrs),
2056 .key_id = FLOW_DISSECTOR_KEY_PORTS,
2057 .offset = offsetof(struct flow_keys, ports),
2061 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
2063 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
2064 .offset = offsetof(struct flow_keys, control),
2067 .key_id = FLOW_DISSECTOR_KEY_BASIC,
2068 .offset = offsetof(struct flow_keys, basic),
2072 struct flow_dissector flow_keys_dissector __read_mostly;
2073 EXPORT_SYMBOL(flow_keys_dissector);
2075 struct flow_dissector flow_keys_basic_dissector __read_mostly;
2076 EXPORT_SYMBOL(flow_keys_basic_dissector);
2078 static int __init init_default_flow_dissectors(void)
2080 skb_flow_dissector_init(&flow_keys_dissector,
2081 flow_keys_dissector_keys,
2082 ARRAY_SIZE(flow_keys_dissector_keys));
2083 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
2084 flow_keys_dissector_symmetric_keys,
2085 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
2086 skb_flow_dissector_init(&flow_keys_basic_dissector,
2087 flow_keys_basic_dissector_keys,
2088 ARRAY_SIZE(flow_keys_basic_dissector_keys));
2091 core_initcall(init_default_flow_dissectors);