@@ -334,14 +334,17 @@ static int parse_vlan_tag(struct sk_buff *skb, struct vlan_head *key_vh)
334334 return 1 ;
335335}
336336
337- static int parse_vlan ( struct sk_buff * skb , struct sw_flow_key * key )
337+ static void clear_vlan ( struct sw_flow_key * key )
338338{
339- int res ;
340-
341339 key -> eth .vlan .tci = 0 ;
342340 key -> eth .vlan .tpid = 0 ;
343341 key -> eth .cvlan .tci = 0 ;
344342 key -> eth .cvlan .tpid = 0 ;
343+ }
344+
345+ static int parse_vlan (struct sk_buff * skb , struct sw_flow_key * key )
346+ {
347+ int res ;
345348
346349 if (skb_vlan_tag_present (skb )) {
347350 key -> eth .vlan .tci = htons (skb -> vlan_tci );
@@ -483,17 +486,20 @@ static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
483486 *
484487 * Returns 0 if successful, otherwise a negative errno value.
485488 *
486- * Initializes @skb header pointers as follows:
489+ * Initializes @skb header fields as follows:
487490 *
488- * - skb->mac_header: the Ethernet header.
491+ * - skb->mac_header: the L2 header.
489492 *
490- * - skb->network_header: just past the Ethernet header, or just past the
491- * VLAN header, to the first byte of the Ethernet payload.
493+ * - skb->network_header: just past the L2 header, or just past the
494+ * VLAN header, to the first byte of the L2 payload.
492495 *
493496 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
494497 * on output, then just past the IP header, if one is present and
495498 * of a correct length, otherwise the same as skb->network_header.
496499 * For other key->eth.type values it is left untouched.
500+ *
501+ * - skb->protocol: the type of the data starting at skb->network_header.
502+ * Equals to key->eth.type.
497503 */
498504static int key_extract (struct sk_buff * skb , struct sw_flow_key * key )
499505{
@@ -505,28 +511,35 @@ static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
505511
506512 skb_reset_mac_header (skb );
507513
508- /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
509- * header in the linear data area.
510- */
511- eth = eth_hdr (skb );
512- ether_addr_copy (key -> eth .src , eth -> h_source );
513- ether_addr_copy (key -> eth .dst , eth -> h_dest );
514+ /* Link layer. */
515+ clear_vlan (key );
516+ if (key -> mac_proto == MAC_PROTO_NONE ) {
517+ if (unlikely (eth_type_vlan (skb -> protocol )))
518+ return - EINVAL ;
514519
515- __skb_pull (skb , 2 * ETH_ALEN );
516- /* We are going to push all headers that we pull, so no need to
517- * update skb->csum here.
518- */
520+ skb_reset_network_header (skb );
521+ } else {
522+ eth = eth_hdr (skb );
523+ ether_addr_copy (key -> eth .src , eth -> h_source );
524+ ether_addr_copy (key -> eth .dst , eth -> h_dest );
519525
520- if (unlikely (parse_vlan (skb , key )))
521- return - ENOMEM ;
526+ __skb_pull (skb , 2 * ETH_ALEN );
527+ /* We are going to push all headers that we pull, so no need to
528+ * update skb->csum here.
529+ */
522530
523- key -> eth .type = parse_ethertype (skb );
524- if (unlikely (key -> eth .type == htons (0 )))
525- return - ENOMEM ;
531+ if (unlikely (parse_vlan (skb , key )))
532+ return - ENOMEM ;
533+
534+ skb -> protocol = parse_ethertype (skb );
535+ if (unlikely (skb -> protocol == htons (0 )))
536+ return - ENOMEM ;
526537
527- skb_reset_network_header (skb );
538+ skb_reset_network_header (skb );
539+ __skb_push (skb , skb -> data - skb_mac_header (skb ));
540+ }
528541 skb_reset_mac_len (skb );
529- __skb_push ( skb , skb -> data - skb_mac_header ( skb )) ;
542+ key -> eth . type = skb -> protocol ;
530543
531544 /* Network layer. */
532545 if (key -> eth .type == htons (ETH_P_IP )) {
@@ -721,9 +734,25 @@ int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
721734 return key_extract (skb , key );
722735}
723736
737+ static int key_extract_mac_proto (struct sk_buff * skb )
738+ {
739+ switch (skb -> dev -> type ) {
740+ case ARPHRD_ETHER :
741+ return MAC_PROTO_ETHERNET ;
742+ case ARPHRD_NONE :
743+ if (skb -> protocol == htons (ETH_P_TEB ))
744+ return MAC_PROTO_ETHERNET ;
745+ return MAC_PROTO_NONE ;
746+ }
747+ WARN_ON_ONCE (1 );
748+ return - EINVAL ;
749+ }
750+
724751int ovs_flow_key_extract (const struct ip_tunnel_info * tun_info ,
725752 struct sk_buff * skb , struct sw_flow_key * key )
726753{
754+ int res ;
755+
727756 /* Extract metadata from packet. */
728757 if (tun_info ) {
729758 key -> tun_proto = ip_tunnel_info_af (tun_info );
@@ -751,7 +780,10 @@ int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
751780 key -> phy .skb_mark = skb -> mark ;
752781 ovs_ct_fill_key (skb , key );
753782 key -> ovs_flow_hash = 0 ;
754- key -> mac_proto = MAC_PROTO_ETHERNET ;
783+ res = key_extract_mac_proto (skb );
784+ if (res < 0 )
785+ return res ;
786+ key -> mac_proto = res ;
755787 key -> recirc_id = 0 ;
756788
757789 return key_extract (skb , key );
@@ -768,5 +800,29 @@ int ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr,
768800 if (err )
769801 return err ;
770802
803+ if (ovs_key_mac_proto (key ) == MAC_PROTO_NONE ) {
804+ /* key_extract assumes that skb->protocol is set-up for
805+ * layer 3 packets which is the case for other callers,
806+ * in particular packets recieved from the network stack.
807+ * Here the correct value can be set from the metadata
808+ * extracted above.
809+ */
810+ skb -> protocol = key -> eth .type ;
811+ } else {
812+ struct ethhdr * eth ;
813+
814+ skb_reset_mac_header (skb );
815+ eth = eth_hdr (skb );
816+
817+ /* Normally, setting the skb 'protocol' field would be
818+ * handled by a call to eth_type_trans(), but it assumes
819+ * there's a sending device, which we may not have.
820+ */
821+ if (eth_proto_is_802_3 (eth -> h_proto ))
822+ skb -> protocol = eth -> h_proto ;
823+ else
824+ skb -> protocol = htons (ETH_P_802_2 );
825+ }
826+
771827 return key_extract (skb , key );
772828}
0 commit comments