These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / net / openvswitch / flow_netlink.c
index c691b1a..d1bd4a4 100644 (file)
@@ -47,9 +47,9 @@
 #include <net/ipv6.h>
 #include <net/ndisc.h>
 #include <net/mpls.h>
+#include <net/vxlan.h>
 
 #include "flow_netlink.h"
-#include "vport-vxlan.h"
 
 struct ovs_len_tbl {
        int len;
@@ -57,6 +57,7 @@ struct ovs_len_tbl {
 };
 
 #define OVS_ATTR_NESTED -1
+#define OVS_ATTR_VARIABLE -2
 
 static void update_range(struct sw_flow_match *match,
                         size_t offset, size_t size, bool is_mask)
@@ -261,8 +262,8 @@ size_t ovs_tun_key_attr_size(void)
         * updating this function.
         */
        return    nla_total_size(8)    /* OVS_TUNNEL_KEY_ATTR_ID */
-               + nla_total_size(4)    /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
-               + nla_total_size(4)    /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
+               + nla_total_size(16)   /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
+               + nla_total_size(16)   /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
                + nla_total_size(1)    /* OVS_TUNNEL_KEY_ATTR_TOS */
                + nla_total_size(1)    /* OVS_TUNNEL_KEY_ATTR_TTL */
                + nla_total_size(0)    /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
@@ -281,7 +282,7 @@ size_t ovs_key_attr_size(void)
        /* Whenever adding new OVS_KEY_ FIELDS, we should consider
         * updating this function.
         */
-       BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
+       BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 26);
 
        return    nla_total_size(4)   /* OVS_KEY_ATTR_PRIORITY */
                + nla_total_size(0)   /* OVS_KEY_ATTR_TUNNEL */
@@ -290,6 +291,10 @@ size_t ovs_key_attr_size(void)
                + nla_total_size(4)   /* OVS_KEY_ATTR_SKB_MARK */
                + nla_total_size(4)   /* OVS_KEY_ATTR_DP_HASH */
                + nla_total_size(4)   /* OVS_KEY_ATTR_RECIRC_ID */
+               + nla_total_size(4)   /* OVS_KEY_ATTR_CT_STATE */
+               + nla_total_size(2)   /* OVS_KEY_ATTR_CT_ZONE */
+               + nla_total_size(4)   /* OVS_KEY_ATTR_CT_MARK */
+               + nla_total_size(16)  /* OVS_KEY_ATTR_CT_LABELS */
                + nla_total_size(12)  /* OVS_KEY_ATTR_ETHERNET */
                + nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
                + nla_total_size(4)   /* OVS_KEY_ATTR_VLAN */
@@ -300,6 +305,10 @@ size_t ovs_key_attr_size(void)
                + nla_total_size(28); /* OVS_KEY_ATTR_ND */
 }
 
+static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
+       [OVS_VXLAN_EXT_GBP]         = { .len = sizeof(u32) },
+};
+
 static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
        [OVS_TUNNEL_KEY_ATTR_ID]            = { .len = sizeof(u64) },
        [OVS_TUNNEL_KEY_ATTR_IPV4_SRC]      = { .len = sizeof(u32) },
@@ -311,8 +320,11 @@ static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1]
        [OVS_TUNNEL_KEY_ATTR_TP_SRC]        = { .len = sizeof(u16) },
        [OVS_TUNNEL_KEY_ATTR_TP_DST]        = { .len = sizeof(u16) },
        [OVS_TUNNEL_KEY_ATTR_OAM]           = { .len = 0 },
-       [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS]   = { .len = OVS_ATTR_NESTED },
-       [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS]    = { .len = OVS_ATTR_NESTED },
+       [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS]   = { .len = OVS_ATTR_VARIABLE },
+       [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS]    = { .len = OVS_ATTR_NESTED,
+                                               .next = ovs_vxlan_ext_key_lens },
+       [OVS_TUNNEL_KEY_ATTR_IPV6_SRC]      = { .len = sizeof(struct in6_addr) },
+       [OVS_TUNNEL_KEY_ATTR_IPV6_DST]      = { .len = sizeof(struct in6_addr) },
 };
 
 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute.  */
@@ -339,8 +351,19 @@ static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
        [OVS_KEY_ATTR_TUNNEL]    = { .len = OVS_ATTR_NESTED,
                                     .next = ovs_tunnel_key_lens, },
        [OVS_KEY_ATTR_MPLS]      = { .len = sizeof(struct ovs_key_mpls) },
+       [OVS_KEY_ATTR_CT_STATE]  = { .len = sizeof(u32) },
+       [OVS_KEY_ATTR_CT_ZONE]   = { .len = sizeof(u16) },
+       [OVS_KEY_ATTR_CT_MARK]   = { .len = sizeof(u32) },
+       [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
 };
 
+static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
+{
+       return expected_len == attr_len ||
+              expected_len == OVS_ATTR_NESTED ||
+              expected_len == OVS_ATTR_VARIABLE;
+}
+
 static bool is_all_zero(const u8 *fp, size_t size)
 {
        int i;
@@ -380,7 +403,7 @@ static int __parse_flow_nlattrs(const struct nlattr *attr,
                }
 
                expected_len = ovs_key_lens[type].len;
-               if (nla_len(nla) != expected_len && expected_len != OVS_ATTR_NESTED) {
+               if (!check_attr_len(nla_len(nla), expected_len)) {
                        OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
                                  type, nla_len(nla), expected_len);
                        return -EINVAL;
@@ -465,29 +488,50 @@ static int genev_tun_opt_from_nlattr(const struct nlattr *a,
        return 0;
 }
 
-static const struct nla_policy vxlan_opt_policy[OVS_VXLAN_EXT_MAX + 1] = {
-       [OVS_VXLAN_EXT_GBP]     = { .type = NLA_U32 },
-};
-
-static int vxlan_tun_opt_from_nlattr(const struct nlattr *a,
+static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
                                     struct sw_flow_match *match, bool is_mask,
                                     bool log)
 {
-       struct nlattr *tb[OVS_VXLAN_EXT_MAX+1];
+       struct nlattr *a;
+       int rem;
        unsigned long opt_key_offset;
-       struct ovs_vxlan_opts opts;
-       int err;
+       struct vxlan_metadata opts;
 
        BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
 
-       err = nla_parse_nested(tb, OVS_VXLAN_EXT_MAX, a, vxlan_opt_policy);
-       if (err < 0)
-               return err;
-
        memset(&opts, 0, sizeof(opts));
+       nla_for_each_nested(a, attr, rem) {
+               int type = nla_type(a);
 
-       if (tb[OVS_VXLAN_EXT_GBP])
-               opts.gbp = nla_get_u32(tb[OVS_VXLAN_EXT_GBP]);
+               if (type > OVS_VXLAN_EXT_MAX) {
+                       OVS_NLERR(log, "VXLAN extension %d out of range max %d",
+                                 type, OVS_VXLAN_EXT_MAX);
+                       return -EINVAL;
+               }
+
+               if (!check_attr_len(nla_len(a),
+                                   ovs_vxlan_ext_key_lens[type].len)) {
+                       OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
+                                 type, nla_len(a),
+                                 ovs_vxlan_ext_key_lens[type].len);
+                       return -EINVAL;
+               }
+
+               switch (type) {
+               case OVS_VXLAN_EXT_GBP:
+                       opts.gbp = nla_get_u32(a);
+                       break;
+               default:
+                       OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
+                                 type);
+                       return -EINVAL;
+               }
+       }
+       if (rem) {
+               OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
+                         rem);
+               return -EINVAL;
+       }
 
        if (!is_mask)
                SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
@@ -500,15 +544,15 @@ static int vxlan_tun_opt_from_nlattr(const struct nlattr *a,
        return 0;
 }
 
-static int ipv4_tun_from_nlattr(const struct nlattr *attr,
-                               struct sw_flow_match *match, bool is_mask,
-                               bool log)
+static int ip_tun_from_nlattr(const struct nlattr *attr,
+                             struct sw_flow_match *match, bool is_mask,
+                             bool log)
 {
-       struct nlattr *a;
-       int rem;
-       bool ttl = false;
+       bool ttl = false, ipv4 = false, ipv6 = false;
        __be16 tun_flags = 0;
        int opts_type = 0;
+       struct nlattr *a;
+       int rem;
 
        nla_for_each_nested(a, attr, rem) {
                int type = nla_type(a);
@@ -520,8 +564,8 @@ static int ipv4_tun_from_nlattr(const struct nlattr *attr,
                        return -EINVAL;
                }
 
-               if (ovs_tunnel_key_lens[type].len != nla_len(a) &&
-                   ovs_tunnel_key_lens[type].len != OVS_ATTR_NESTED) {
+               if (!check_attr_len(nla_len(a),
+                                   ovs_tunnel_key_lens[type].len)) {
                        OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
                                  type, nla_len(a), ovs_tunnel_key_lens[type].len);
                        return -EINVAL;
@@ -534,19 +578,31 @@ static int ipv4_tun_from_nlattr(const struct nlattr *attr,
                        tun_flags |= TUNNEL_KEY;
                        break;
                case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
-                       SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
+                       SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
                                        nla_get_in_addr(a), is_mask);
+                       ipv4 = true;
                        break;
                case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
-                       SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
+                       SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
                                        nla_get_in_addr(a), is_mask);
+                       ipv4 = true;
+                       break;
+               case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
+                       SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
+                                       nla_get_in6_addr(a), is_mask);
+                       ipv6 = true;
+                       break;
+               case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
+                       SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
+                                       nla_get_in6_addr(a), is_mask);
+                       ipv6 = true;
                        break;
                case OVS_TUNNEL_KEY_ATTR_TOS:
-                       SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
+                       SW_FLOW_KEY_PUT(match, tun_key.tos,
                                        nla_get_u8(a), is_mask);
                        break;
                case OVS_TUNNEL_KEY_ATTR_TTL:
-                       SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
+                       SW_FLOW_KEY_PUT(match, tun_key.ttl,
                                        nla_get_u8(a), is_mask);
                        ttl = true;
                        break;
@@ -594,28 +650,46 @@ static int ipv4_tun_from_nlattr(const struct nlattr *attr,
                        opts_type = type;
                        break;
                default:
-                       OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
+                       OVS_NLERR(log, "Unknown IP tunnel attribute %d",
                                  type);
                        return -EINVAL;
                }
        }
 
        SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
+       if (is_mask)
+               SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
+       else
+               SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
+                               false);
 
        if (rem > 0) {
-               OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
+               OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
                          rem);
                return -EINVAL;
        }
 
+       if (ipv4 && ipv6) {
+               OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
+               return -EINVAL;
+       }
+
        if (!is_mask) {
-               if (!match->key->tun_key.ipv4_dst) {
+               if (!ipv4 && !ipv6) {
+                       OVS_NLERR(log, "IP tunnel dst address not specified");
+                       return -EINVAL;
+               }
+               if (ipv4 && !match->key->tun_key.u.ipv4.dst) {
                        OVS_NLERR(log, "IPv4 tunnel dst address is zero");
                        return -EINVAL;
                }
+               if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
+                       OVS_NLERR(log, "IPv6 tunnel dst address is zero");
+                       return -EINVAL;
+               }
 
                if (!ttl) {
-                       OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
+                       OVS_NLERR(log, "IP tunnel TTL not specified.");
                        return -EINVAL;
                }
        }
@@ -626,7 +700,7 @@ static int ipv4_tun_from_nlattr(const struct nlattr *attr,
 static int vxlan_opt_to_nlattr(struct sk_buff *skb,
                               const void *tun_opts, int swkey_tun_opts_len)
 {
-       const struct ovs_vxlan_opts *opts = tun_opts;
+       const struct vxlan_metadata *opts = tun_opts;
        struct nlattr *nla;
 
        nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
@@ -640,25 +714,40 @@ static int vxlan_opt_to_nlattr(struct sk_buff *skb,
        return 0;
 }
 
-static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
-                               const struct ovs_key_ipv4_tunnel *output,
-                               const void *tun_opts, int swkey_tun_opts_len)
+static int __ip_tun_to_nlattr(struct sk_buff *skb,
+                             const struct ip_tunnel_key *output,
+                             const void *tun_opts, int swkey_tun_opts_len,
+                             unsigned short tun_proto)
 {
        if (output->tun_flags & TUNNEL_KEY &&
            nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
                return -EMSGSIZE;
-       if (output->ipv4_src &&
-           nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
-                           output->ipv4_src))
-               return -EMSGSIZE;
-       if (output->ipv4_dst &&
-           nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
-                           output->ipv4_dst))
-               return -EMSGSIZE;
-       if (output->ipv4_tos &&
-           nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
+       switch (tun_proto) {
+       case AF_INET:
+               if (output->u.ipv4.src &&
+                   nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
+                                   output->u.ipv4.src))
+                       return -EMSGSIZE;
+               if (output->u.ipv4.dst &&
+                   nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
+                                   output->u.ipv4.dst))
+                       return -EMSGSIZE;
+               break;
+       case AF_INET6:
+               if (!ipv6_addr_any(&output->u.ipv6.src) &&
+                   nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
+                                    &output->u.ipv6.src))
+                       return -EMSGSIZE;
+               if (!ipv6_addr_any(&output->u.ipv6.dst) &&
+                   nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
+                                    &output->u.ipv6.dst))
+                       return -EMSGSIZE;
+               break;
+       }
+       if (output->tos &&
+           nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
                return -EMSGSIZE;
-       if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
+       if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
                return -EMSGSIZE;
        if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
            nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
@@ -675,7 +764,7 @@ static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
        if ((output->tun_flags & TUNNEL_OAM) &&
            nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
                return -EMSGSIZE;
-       if (tun_opts) {
+       if (swkey_tun_opts_len) {
                if (output->tun_flags & TUNNEL_GENEVE_OPT &&
                    nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
                            swkey_tun_opts_len, tun_opts))
@@ -688,9 +777,10 @@ static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
        return 0;
 }
 
-static int ipv4_tun_to_nlattr(struct sk_buff *skb,
-                             const struct ovs_key_ipv4_tunnel *output,
-                             const void *tun_opts, int swkey_tun_opts_len)
+static int ip_tun_to_nlattr(struct sk_buff *skb,
+                           const struct ip_tunnel_key *output,
+                           const void *tun_opts, int swkey_tun_opts_len,
+                           unsigned short tun_proto)
 {
        struct nlattr *nla;
        int err;
@@ -699,7 +789,8 @@ static int ipv4_tun_to_nlattr(struct sk_buff *skb,
        if (!nla)
                return -EMSGSIZE;
 
-       err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
+       err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
+                                tun_proto);
        if (err)
                return err;
 
@@ -707,17 +798,18 @@ static int ipv4_tun_to_nlattr(struct sk_buff *skb,
        return 0;
 }
 
-int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
-                                 const struct ovs_tunnel_info *egress_tun_info)
+int ovs_nla_put_tunnel_info(struct sk_buff *skb,
+                           struct ip_tunnel_info *tun_info)
 {
-       return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel,
-                                   egress_tun_info->options,
-                                   egress_tun_info->options_len);
+       return __ip_tun_to_nlattr(skb, &tun_info->key,
+                                 ip_tunnel_info_opts(tun_info),
+                                 tun_info->options_len,
+                                 ip_tunnel_info_af(tun_info));
 }
 
-static int metadata_from_nlattrs(struct sw_flow_match *match,  u64 *attrs,
-                                const struct nlattr **a, bool is_mask,
-                                bool log)
+static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
+                                u64 *attrs, const struct nlattr **a,
+                                bool is_mask, bool log)
 {
        if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
                u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
@@ -763,21 +855,58 @@ static int metadata_from_nlattrs(struct sw_flow_match *match,  u64 *attrs,
                *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
        }
        if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
-               if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
-                                        is_mask, log) < 0)
+               if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
+                                      is_mask, log) < 0)
                        return -EINVAL;
                *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
        }
+
+       if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
+           ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
+               u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
+
+               if (ct_state & ~CT_SUPPORTED_MASK) {
+                       OVS_NLERR(log, "ct_state flags %08x unsupported",
+                                 ct_state);
+                       return -EINVAL;
+               }
+
+               SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
+       }
+       if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
+           ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
+               u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
+
+               SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
+       }
+       if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
+           ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
+               u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
+
+               SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
+       }
+       if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
+           ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
+               const struct ovs_key_ct_labels *cl;
+
+               cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
+               SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
+                                  sizeof(*cl), is_mask);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
+       }
        return 0;
 }
 
-static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
-                               const struct nlattr **a, bool is_mask,
-                               bool log)
+static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
+                               u64 attrs, const struct nlattr **a,
+                               bool is_mask, bool log)
 {
        int err;
 
-       err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
+       err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
        if (err)
                return err;
 
@@ -816,7 +945,7 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
                if (is_mask) {
                        /* Always exact match EtherType. */
                        eth_type = htons(0xffff);
-               } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
+               } else if (!eth_proto_is_802_3(eth_type)) {
                        OVS_NLERR(log, "EtherType %x is less than min %x",
                                  ntohs(eth_type), ETH_P_802_3_MIN);
                        return -EINVAL;
@@ -1012,10 +1141,16 @@ static void nlattr_set(struct nlattr *attr, u8 val,
 
        /* The nlattr stream should already have been validated */
        nla_for_each_nested(nla, attr, rem) {
-               if (tbl && tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
-                       nlattr_set(nla, val, tbl[nla_type(nla)].next);
-               else
+               if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
+                       if (tbl[nla_type(nla)].next)
+                               tbl = tbl[nla_type(nla)].next;
+                       nlattr_set(nla, val, tbl);
+               } else {
                        memset(nla_data(nla), val, nla_len(nla));
+               }
+
+               if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
+                       *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
        }
 }
 
@@ -1029,6 +1164,7 @@ static void mask_set_nlattr(struct nlattr *attr, u8 val)
  * mask. In case the 'mask' is NULL, the flow is treated as exact match
  * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  * does not include any don't care bit.
+ * @net: Used to determine per-namespace field support.
  * @match: receives the extracted flow match information.
  * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  * sequence. The fields should of the packet that triggered the creation
@@ -1039,7 +1175,7 @@ static void mask_set_nlattr(struct nlattr *attr, u8 val)
  * probing for feature compatibility this should be passed in as false to
  * suppress unnecessary error logging.
  */
-int ovs_nla_get_match(struct sw_flow_match *match,
+int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
                      const struct nlattr *nla_key,
                      const struct nlattr *nla_mask,
                      bool log)
@@ -1089,7 +1225,7 @@ int ovs_nla_get_match(struct sw_flow_match *match,
                }
        }
 
-       err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
+       err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
        if (err)
                return err;
 
@@ -1116,7 +1252,7 @@ int ovs_nla_get_match(struct sw_flow_match *match,
                        /* The userspace does not send tunnel attributes that
                         * are 0, but we should not wildcard them nonetheless.
                         */
-                       if (match->key->tun_key.ipv4_dst)
+                       if (match->key->tun_proto)
                                SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
                                                         0xff, true);
 
@@ -1169,7 +1305,8 @@ int ovs_nla_get_match(struct sw_flow_match *match,
                        }
                }
 
-               err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
+               err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
+                                          log);
                if (err)
                        goto free_newmask;
        }
@@ -1250,7 +1387,7 @@ u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
  * extracted from the packet itself.
  */
 
-int ovs_nla_get_flow_metadata(const struct nlattr *attr,
+int ovs_nla_get_flow_metadata(struct net *net, const struct nlattr *attr,
                              struct sw_flow_key *key,
                              bool log)
 {
@@ -1266,9 +1403,10 @@ int ovs_nla_get_flow_metadata(const struct nlattr *attr,
        memset(&match, 0, sizeof(match));
        match.key = key;
 
+       memset(&key->ct, 0, sizeof(key->ct));
        key->phy.in_port = DP_MAX_PORTS;
 
-       return metadata_from_nlattrs(&match, &attrs, a, false, log);
+       return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
 }
 
 static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
@@ -1287,14 +1425,14 @@ static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
        if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
                goto nla_put_failure;
 
-       if ((swkey->tun_key.ipv4_dst || is_mask)) {
+       if ((swkey->tun_proto || is_mask)) {
                const void *opts = NULL;
 
                if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
                        opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
 
-               if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
-                                      swkey->tun_opts_len))
+               if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
+                                    swkey->tun_opts_len, swkey->tun_proto))
                        goto nla_put_failure;
        }
 
@@ -1314,6 +1452,9 @@ static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
        if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
                goto nla_put_failure;
 
+       if (ovs_ct_put_key(output, skb))
+               goto nla_put_failure;
+
        nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
        if (!nla)
                goto nla_put_failure;
@@ -1548,11 +1689,51 @@ static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
        return sfa;
 }
 
+static void ovs_nla_free_set_action(const struct nlattr *a)
+{
+       const struct nlattr *ovs_key = nla_data(a);
+       struct ovs_tunnel_info *ovs_tun;
+
+       switch (nla_type(ovs_key)) {
+       case OVS_KEY_ATTR_TUNNEL_INFO:
+               ovs_tun = nla_data(ovs_key);
+               dst_release((struct dst_entry *)ovs_tun->tun_dst);
+               break;
+       }
+}
+
+void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
+{
+       const struct nlattr *a;
+       int rem;
+
+       if (!sf_acts)
+               return;
+
+       nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
+               switch (nla_type(a)) {
+               case OVS_ACTION_ATTR_SET:
+                       ovs_nla_free_set_action(a);
+                       break;
+               case OVS_ACTION_ATTR_CT:
+                       ovs_ct_free_action(a);
+                       break;
+               }
+       }
+
+       kfree(sf_acts);
+}
+
+static void __ovs_nla_free_flow_actions(struct rcu_head *head)
+{
+       ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
+}
+
 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  * The caller must hold rcu_read_lock for this to be sensible. */
-void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
+void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
 {
-       kfree_rcu(sf_acts, rcu);
+       call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
 }
 
 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
@@ -1582,6 +1763,7 @@ static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
 
        memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
        acts->actions_len = (*sfa)->actions_len;
+       acts->orig_len = (*sfa)->orig_len;
        kfree(*sfa);
        *sfa = acts;
 
@@ -1609,8 +1791,8 @@ static struct nlattr *__add_action(struct sw_flow_actions **sfa,
        return a;
 }
 
-static int add_action(struct sw_flow_actions **sfa, int attrtype,
-                     void *data, int len, bool log)
+int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
+                      int len, bool log)
 {
        struct nlattr *a;
 
@@ -1625,7 +1807,7 @@ static inline int add_nested_action_start(struct sw_flow_actions **sfa,
        int used = (*sfa)->actions_len;
        int err;
 
-       err = add_action(sfa, attrtype, NULL, 0, log);
+       err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
        if (err)
                return err;
 
@@ -1641,12 +1823,12 @@ static inline void add_nested_action_end(struct sw_flow_actions *sfa,
        a->nla_len = sfa->actions_len - st_offset;
 }
 
-static int __ovs_nla_copy_actions(const struct nlattr *attr,
+static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                                  const struct sw_flow_key *key,
                                  int depth, struct sw_flow_actions **sfa,
                                  __be16 eth_type, __be16 vlan_tci, bool log);
 
-static int validate_and_copy_sample(const struct nlattr *attr,
+static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
                                    const struct sw_flow_key *key, int depth,
                                    struct sw_flow_actions **sfa,
                                    __be16 eth_type, __be16 vlan_tci, bool log)
@@ -1678,15 +1860,15 @@ static int validate_and_copy_sample(const struct nlattr *attr,
        start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
        if (start < 0)
                return start;
-       err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
-                        nla_data(probability), sizeof(u32), log);
+       err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
+                                nla_data(probability), sizeof(u32), log);
        if (err)
                return err;
        st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
        if (st_acts < 0)
                return st_acts;
 
-       err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
+       err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
                                     eth_type, vlan_tci, log);
        if (err)
                return err;
@@ -1746,12 +1928,14 @@ static int validate_and_copy_set_tun(const struct nlattr *attr,
 {
        struct sw_flow_match match;
        struct sw_flow_key key;
-       struct ovs_tunnel_info *tun_info;
+       struct metadata_dst *tun_dst;
+       struct ip_tunnel_info *tun_info;
+       struct ovs_tunnel_info *ovs_tun;
        struct nlattr *a;
        int err = 0, start, opts_type;
 
        ovs_match_init(&match, &key, NULL);
-       opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
+       opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
        if (opts_type < 0)
                return opts_type;
 
@@ -1771,27 +1955,33 @@ static int validate_and_copy_set_tun(const struct nlattr *attr,
        if (start < 0)
                return start;
 
+       tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
+       if (!tun_dst)
+               return -ENOMEM;
+
        a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
-                        sizeof(*tun_info) + key.tun_opts_len, log);
-       if (IS_ERR(a))
+                        sizeof(*ovs_tun), log);
+       if (IS_ERR(a)) {
+               dst_release((struct dst_entry *)tun_dst);
                return PTR_ERR(a);
+       }
 
-       tun_info = nla_data(a);
-       tun_info->tunnel = key.tun_key;
-       tun_info->options_len = key.tun_opts_len;
+       ovs_tun = nla_data(a);
+       ovs_tun->tun_dst = tun_dst;
 
-       if (tun_info->options_len) {
-               /* We need to store the options in the action itself since
-                * everything else will go away after flow setup. We can append
-                * it to tun_info and then point there.
-                */
-               memcpy((tun_info + 1),
-                      TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
-               tun_info->options = (tun_info + 1);
-       } else {
-               tun_info->options = NULL;
-       }
+       tun_info = &tun_dst->u.tun_info;
+       tun_info->mode = IP_TUNNEL_INFO_TX;
+       if (key.tun_proto == AF_INET6)
+               tun_info->mode |= IP_TUNNEL_INFO_IPV6;
+       tun_info->key = key.tun_key;
 
+       /* We need to store the options in the action itself since
+        * everything else will go away after flow setup. We can append
+        * it to tun_info and then point there.
+        */
+       ip_tunnel_info_opts_set(tun_info,
+                               TUN_METADATA_OPTS(&key, key.tun_opts_len),
+                               key.tun_opts_len);
        add_nested_action_end(*sfa, start);
 
        return err;
@@ -1829,8 +2019,7 @@ static int validate_set(const struct nlattr *a,
                key_len /= 2;
 
        if (key_type > OVS_KEY_ATTR_MAX ||
-           (ovs_key_lens[key_type].len != key_len &&
-            ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
+           !check_attr_len(key_len, ovs_key_lens[key_type].len))
                return -EINVAL;
 
        if (masked && !validate_masked(nla_data(ovs_key), key_len))
@@ -1843,6 +2032,8 @@ static int validate_set(const struct nlattr *a,
 
        case OVS_KEY_ATTR_PRIORITY:
        case OVS_KEY_ATTR_SKB_MARK:
+       case OVS_KEY_ATTR_CT_MARK:
+       case OVS_KEY_ATTR_CT_LABELS:
        case OVS_KEY_ATTR_ETHERNET:
                break;
 
@@ -2008,7 +2199,7 @@ static int copy_action(const struct nlattr *from,
        return 0;
 }
 
-static int __ovs_nla_copy_actions(const struct nlattr *attr,
+static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                                  const struct sw_flow_key *key,
                                  int depth, struct sw_flow_actions **sfa,
                                  __be16 eth_type, __be16 vlan_tci, bool log)
@@ -2032,7 +2223,8 @@ static int __ovs_nla_copy_actions(const struct nlattr *attr,
                        [OVS_ACTION_ATTR_SET] = (u32)-1,
                        [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
                        [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
-                       [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
+                       [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
+                       [OVS_ACTION_ATTR_CT] = (u32)-1,
                };
                const struct ovs_action_push_vlan *vlan;
                int type = nla_type(a);
@@ -2139,13 +2331,20 @@ static int __ovs_nla_copy_actions(const struct nlattr *attr,
                        break;
 
                case OVS_ACTION_ATTR_SAMPLE:
-                       err = validate_and_copy_sample(a, key, depth, sfa,
+                       err = validate_and_copy_sample(net, a, key, depth, sfa,
                                                       eth_type, vlan_tci, log);
                        if (err)
                                return err;
                        skip_copy = true;
                        break;
 
+               case OVS_ACTION_ATTR_CT:
+                       err = ovs_ct_copy_action(net, a, key, sfa, log);
+                       if (err)
+                               return err;
+                       skip_copy = true;
+                       break;
+
                default:
                        OVS_NLERR(log, "Unknown Action type %d", type);
                        return -EINVAL;
@@ -2164,7 +2363,7 @@ static int __ovs_nla_copy_actions(const struct nlattr *attr,
 }
 
 /* 'key' must be the masked key. */
-int ovs_nla_copy_actions(const struct nlattr *attr,
+int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                         const struct sw_flow_key *key,
                         struct sw_flow_actions **sfa, bool log)
 {
@@ -2174,10 +2373,11 @@ int ovs_nla_copy_actions(const struct nlattr *attr,
        if (IS_ERR(*sfa))
                return PTR_ERR(*sfa);
 
-       err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
+       (*sfa)->orig_len = nla_len(attr);
+       err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
                                     key->eth.tci, log);
        if (err)
-               kfree(*sfa);
+               ovs_nla_free_flow_actions(*sfa);
 
        return err;
 }
@@ -2227,16 +2427,17 @@ static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
 
        switch (key_type) {
        case OVS_KEY_ATTR_TUNNEL_INFO: {
-               struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
+               struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
+               struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
 
                start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
                if (!start)
                        return -EMSGSIZE;
 
-               err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
-                                        tun_info->options_len ?
-                                               tun_info->options : NULL,
-                                        tun_info->options_len);
+               err =  ip_tun_to_nlattr(skb, &tun_info->key,
+                                       ip_tunnel_info_opts(tun_info),
+                                       tun_info->options_len,
+                                       ip_tunnel_info_af(tun_info));
                if (err)
                        return err;
                nla_nest_end(skb, start);
@@ -2298,6 +2499,13 @@ int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
                        if (err)
                                return err;
                        break;
+
+               case OVS_ACTION_ATTR_CT:
+                       err = ovs_ct_action_to_attr(nla_data(a), skb);
+                       if (err)
+                               return err;
+                       break;
+
                default:
                        if (nla_put(skb, type, nla_len(a), nla_data(a)))
                                return -EMSGSIZE;