datapath: fix a use after free
[cascardo/ovs.git] / datapath / flow.c
index 2a839ff..2086a1a 100644 (file)
@@ -16,8 +16,6 @@
  * 02110-1301, USA
  */
 
-#include "flow.h"
-#include "datapath.h"
 #include <linux/uaccess.h>
 #include <linux/netdevice.h>
 #include <linux/etherdevice.h>
 #include <net/ipv6.h>
 #include <net/ndisc.h>
 
+#include "datapath.h"
+#include "flow.h"
+#include "flow_netlink.h"
+
+#include "mpls.h"
 #include "vlan.h"
 
 u64 ovs_flow_used_time(unsigned long flow_jiffies)
@@ -63,7 +66,7 @@ u64 ovs_flow_used_time(unsigned long flow_jiffies)
 #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
 
 void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
-                          struct sk_buff *skb)
+                          const struct sk_buff *skb)
 {
        struct flow_stats *stats;
        int node = numa_node_id();
@@ -90,7 +93,7 @@ void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
                         * allocated stats as we have already locked them.
                         */
                        if (likely(flow->stats_last_writer != NUMA_NO_NODE)
-                           && likely(!rcu_dereference(flow->stats[node]))) {
+                           && likely(!rcu_access_pointer(flow->stats[node]))) {
                                /* Try to allocate node-specific stats. */
                                struct flow_stats *new_stats;
 
@@ -424,10 +427,9 @@ invalid:
 }
 
 /**
- * ovs_flow_extract - extracts a flow key from an Ethernet frame.
+ * key_extract - extracts a flow key from an Ethernet frame.
  * @skb: sk_buff that contains the frame, with skb->data pointing to the
  * Ethernet header
- * @in_port: port number on which @skb was received.
  * @key: output flow key
  *
  * The caller must ensure that skb->len >= ETH_HLEN.
@@ -446,22 +448,11 @@ invalid:
  *      of a correct length, otherwise the same as skb->network_header.
  *      For other key->eth.type values it is left untouched.
  */
-int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
+static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
 {
        int error;
        struct ethhdr *eth;
 
-       key->phy.priority = skb->priority;
-       if (OVS_CB(skb)->tun_key)
-               memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
-       else
-               memset(&key->tun_key, 0, sizeof(key->tun_key));
-
-       key->phy.in_port = in_port;
-       key->phy.skb_mark = skb->mark;
-       key->ovs_flow_hash = 0;
-       key->recirc_id = 0;
-
        /* Flags are always used as part of stats. */
        key->tp.flags = 0;
 
@@ -490,6 +481,7 @@ int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
                return -ENOMEM;
 
        skb_reset_network_header(skb);
+       skb_reset_mac_len(skb);
        __skb_push(skb, skb->data - skb_mac_header(skb));
 
        /* Network layer. */
@@ -569,14 +561,15 @@ int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
        } else if (key->eth.type == htons(ETH_P_ARP) ||
                   key->eth.type == htons(ETH_P_RARP)) {
                struct arp_eth_header *arp;
+               bool arp_available = arphdr_ok(skb);
 
                arp = (struct arp_eth_header *)skb_network_header(skb);
 
-               if (arphdr_ok(skb)
-                               && arp->ar_hrd == htons(ARPHRD_ETHER)
-                               && arp->ar_pro == htons(ETH_P_IP)
-                               && arp->ar_hln == ETH_ALEN
-                               && arp->ar_pln == 4) {
+               if (arp_available &&
+                   arp->ar_hrd == htons(ARPHRD_ETHER) &&
+                   arp->ar_pro == htons(ETH_P_IP) &&
+                   arp->ar_hln == ETH_ALEN &&
+                   arp->ar_pln == 4) {
 
                        /* We only match on the lower 8 bits of the opcode. */
                        if (ntohs(arp->ar_op) <= 0xff)
@@ -592,6 +585,33 @@ int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
                        memset(&key->ip, 0, sizeof(key->ip));
                        memset(&key->ipv4, 0, sizeof(key->ipv4));
                }
+       } else if (eth_p_mpls(key->eth.type)) {
+               size_t stack_len = MPLS_HLEN;
+
+               /* In the presence of an MPLS label stack the end of the L2
+                * header and the beginning of the L3 header differ.
+                *
+                * Advance network_header to the beginning of the L3
+                * header. mac_len corresponds to the end of the L2 header.
+                */
+               while (1) {
+                       __be32 lse;
+
+                       error = check_header(skb, skb->mac_len + stack_len);
+                       if (unlikely(error))
+                               return 0;
+
+                       memcpy(&lse, skb_network_header(skb), MPLS_HLEN);
+
+                       if (stack_len == MPLS_HLEN)
+                               memcpy(&key->mpls.top_lse, &lse, MPLS_HLEN);
+
+                       skb_set_network_header(skb, skb->mac_len + stack_len);
+                       if (lse & htonl(MPLS_BOS_MASK))
+                               break;
+
+                       stack_len += MPLS_HLEN;
+               }
        } else if (key->eth.type == htons(ETH_P_IPV6)) {
                int nh_len;             /* IPv6 Header + Extensions */
 
@@ -652,3 +672,54 @@ int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
 
        return 0;
 }
+
+int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
+{
+       return key_extract(skb, key);
+}
+
+int ovs_flow_key_extract(const struct ovs_tunnel_info *tun_info,
+                        struct sk_buff *skb,
+                        struct sw_flow_key *key)
+{
+       /* Extract metadata from packet. */
+       if (tun_info) {
+               memcpy(&key->tun_key, &tun_info->tunnel, sizeof(key->tun_key));
+
+               BUILD_BUG_ON(((1 << (sizeof(tun_info->options_len) * 8)) - 1) >
+                            sizeof(key->tun_opts));
+
+               if (tun_info->options) {
+                       memcpy(GENEVE_OPTS(key, tun_info->options_len),
+                               tun_info->options, tun_info->options_len);
+                       key->tun_opts_len = tun_info->options_len;
+               } else {
+                       key->tun_opts_len = 0;
+               }
+       } else {
+               key->tun_opts_len = 0;
+               memset(&key->tun_key, 0, sizeof(key->tun_key));
+       }
+
+       key->phy.priority = skb->priority;
+       key->phy.in_port = OVS_CB(skb)->input_vport->port_no;
+       key->phy.skb_mark = skb->mark;
+       key->ovs_flow_hash = 0;
+       key->recirc_id = 0;
+
+       return key_extract(skb, key);
+}
+
+int ovs_flow_key_extract_userspace(const struct nlattr *attr,
+                                  struct sk_buff *skb,
+                                  struct sw_flow_key *key, bool log)
+{
+       int err;
+
+       /* Extract metadata from netlink attributes. */
+       err = ovs_nla_get_flow_metadata(attr, key, log);
+       if (err)
+               return err;
+
+       return key_extract(skb, key);
+}