Skip to content

Commit ad06a56

Browse files
gvrose8192justinpettit
authored andcommitted
openvswitch: Properly set L4 keys on "later" IP fragments
When IP fragments are reassembled before being sent to conntrack, the key from the last fragment is used. Unless there are reordering issues, the last fragment received will not contain the L4 ports, so the key for the reassembled datagram won't contain them. This patch updates the key once we have a reassembled datagram. The handle_fragments() function works on L3 headers so we pull the L3/L4 flow key update code from key_extract into a new function 'key_extract_l3l4'. Then we add a another new function ovs_flow_key_update_l3l4() and export it so that it is accessible by handle_fragments() for conntrack packet reassembly. Co-authored-by: Justin Pettit <jpettit@ovn.org> Signed-off-by: Greg Rose <gvrose8192@gmail.com> Acked-by: Pravin B Shelar <pshelar@ovn.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a84d016 commit ad06a56

3 files changed

Lines changed: 95 additions & 66 deletions

File tree

‎net/openvswitch/conntrack.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,11 @@ static int handle_fragments(struct net *net, struct sw_flow_key *key,
525525
return -EPFNOSUPPORT;
526526
}
527527

528+
/* The key extracted from the fragment that completed this datagram
529+
* likely didn't have an L4 header, so regenerate it.
530+
*/
531+
ovs_flow_key_update_l3l4(skb, key);
532+
528533
key->ip.frag = OVS_FRAG_TYPE_NONE;
529534
skb_clear_hash(skb);
530535
skb->ignore_df = 1;

‎net/openvswitch/flow.c‎

Lines changed: 89 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -523,78 +523,15 @@ static int parse_nsh(struct sk_buff *skb, struct sw_flow_key *key)
523523
}
524524

525525
/**
526-
* key_extract - extracts a flow key from an Ethernet frame.
526+
* key_extract_l3l4 - extracts L3/L4 header information.
527527
* @skb: sk_buff that contains the frame, with skb->data pointing to the
528-
* Ethernet header
528+
* L3 header
529529
* @key: output flow key
530530
*
531-
* The caller must ensure that skb->len >= ETH_HLEN.
532-
*
533-
* Returns 0 if successful, otherwise a negative errno value.
534-
*
535-
* Initializes @skb header fields as follows:
536-
*
537-
* - skb->mac_header: the L2 header.
538-
*
539-
* - skb->network_header: just past the L2 header, or just past the
540-
* VLAN header, to the first byte of the L2 payload.
541-
*
542-
* - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
543-
* on output, then just past the IP header, if one is present and
544-
* of a correct length, otherwise the same as skb->network_header.
545-
* For other key->eth.type values it is left untouched.
546-
*
547-
* - skb->protocol: the type of the data starting at skb->network_header.
548-
* Equals to key->eth.type.
549531
*/
550-
static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
532+
static int key_extract_l3l4(struct sk_buff *skb, struct sw_flow_key *key)
551533
{
552534
int error;
553-
struct ethhdr *eth;
554-
555-
/* Flags are always used as part of stats */
556-
key->tp.flags = 0;
557-
558-
skb_reset_mac_header(skb);
559-
560-
/* Link layer. */
561-
clear_vlan(key);
562-
if (ovs_key_mac_proto(key) == MAC_PROTO_NONE) {
563-
if (unlikely(eth_type_vlan(skb->protocol)))
564-
return -EINVAL;
565-
566-
skb_reset_network_header(skb);
567-
key->eth.type = skb->protocol;
568-
} else {
569-
eth = eth_hdr(skb);
570-
ether_addr_copy(key->eth.src, eth->h_source);
571-
ether_addr_copy(key->eth.dst, eth->h_dest);
572-
573-
__skb_pull(skb, 2 * ETH_ALEN);
574-
/* We are going to push all headers that we pull, so no need to
575-
* update skb->csum here.
576-
*/
577-
578-
if (unlikely(parse_vlan(skb, key)))
579-
return -ENOMEM;
580-
581-
key->eth.type = parse_ethertype(skb);
582-
if (unlikely(key->eth.type == htons(0)))
583-
return -ENOMEM;
584-
585-
/* Multiple tagged packets need to retain TPID to satisfy
586-
* skb_vlan_pop(), which will later shift the ethertype into
587-
* skb->protocol.
588-
*/
589-
if (key->eth.cvlan.tci & htons(VLAN_CFI_MASK))
590-
skb->protocol = key->eth.cvlan.tpid;
591-
else
592-
skb->protocol = key->eth.type;
593-
594-
skb_reset_network_header(skb);
595-
__skb_push(skb, skb->data - skb_mac_header(skb));
596-
}
597-
skb_reset_mac_len(skb);
598535

599536
/* Network layer. */
600537
if (key->eth.type == htons(ETH_P_IP)) {
@@ -788,6 +725,92 @@ static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
788725
return 0;
789726
}
790727

728+
/**
729+
* key_extract - extracts a flow key from an Ethernet frame.
730+
* @skb: sk_buff that contains the frame, with skb->data pointing to the
731+
* Ethernet header
732+
* @key: output flow key
733+
*
734+
* The caller must ensure that skb->len >= ETH_HLEN.
735+
*
736+
* Returns 0 if successful, otherwise a negative errno value.
737+
*
738+
* Initializes @skb header fields as follows:
739+
*
740+
* - skb->mac_header: the L2 header.
741+
*
742+
* - skb->network_header: just past the L2 header, or just past the
743+
* VLAN header, to the first byte of the L2 payload.
744+
*
745+
* - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
746+
* on output, then just past the IP header, if one is present and
747+
* of a correct length, otherwise the same as skb->network_header.
748+
* For other key->eth.type values it is left untouched.
749+
*
750+
* - skb->protocol: the type of the data starting at skb->network_header.
751+
* Equals to key->eth.type.
752+
*/
753+
static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
754+
{
755+
struct ethhdr *eth;
756+
757+
/* Flags are always used as part of stats */
758+
key->tp.flags = 0;
759+
760+
skb_reset_mac_header(skb);
761+
762+
/* Link layer. */
763+
clear_vlan(key);
764+
if (ovs_key_mac_proto(key) == MAC_PROTO_NONE) {
765+
if (unlikely(eth_type_vlan(skb->protocol)))
766+
return -EINVAL;
767+
768+
skb_reset_network_header(skb);
769+
key->eth.type = skb->protocol;
770+
} else {
771+
eth = eth_hdr(skb);
772+
ether_addr_copy(key->eth.src, eth->h_source);
773+
ether_addr_copy(key->eth.dst, eth->h_dest);
774+
775+
__skb_pull(skb, 2 * ETH_ALEN);
776+
/* We are going to push all headers that we pull, so no need to
777+
* update skb->csum here.
778+
*/
779+
780+
if (unlikely(parse_vlan(skb, key)))
781+
return -ENOMEM;
782+
783+
key->eth.type = parse_ethertype(skb);
784+
if (unlikely(key->eth.type == htons(0)))
785+
return -ENOMEM;
786+
787+
/* Multiple tagged packets need to retain TPID to satisfy
788+
* skb_vlan_pop(), which will later shift the ethertype into
789+
* skb->protocol.
790+
*/
791+
if (key->eth.cvlan.tci & htons(VLAN_CFI_MASK))
792+
skb->protocol = key->eth.cvlan.tpid;
793+
else
794+
skb->protocol = key->eth.type;
795+
796+
skb_reset_network_header(skb);
797+
__skb_push(skb, skb->data - skb_mac_header(skb));
798+
}
799+
800+
skb_reset_mac_len(skb);
801+
802+
/* Fill out L3/L4 key info, if any */
803+
return key_extract_l3l4(skb, key);
804+
}
805+
806+
/* In the case of conntrack fragment handling it expects L3 headers,
807+
* add a helper.
808+
*/
809+
int ovs_flow_key_update_l3l4(struct sk_buff *skb, struct sw_flow_key *key)
810+
{
811+
return key_extract_l3l4(skb, key);
812+
}
813+
791814
int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
792815
{
793816
int res;

‎net/openvswitch/flow.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ void ovs_flow_stats_clear(struct sw_flow *);
270270
u64 ovs_flow_used_time(unsigned long flow_jiffies);
271271

272272
int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key);
273+
int ovs_flow_key_update_l3l4(struct sk_buff *skb, struct sw_flow_key *key);
273274
int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
274275
struct sk_buff *skb,
275276
struct sw_flow_key *key);

0 commit comments

Comments
 (0)