]> git.proxmox.com Git - mirror_ovs.git/blobdiff - datapath/flow_netlink.c
datapath: Add a new action check_pkt_len
[mirror_ovs.git] / datapath / flow_netlink.c
index 6e8909295a5b121036fb1a4fe654c37c97b7382f..c4704a5b3041c0d26b22bd6fd37a34ae0540c6cd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007-2015 Nicira, Inc.
+ * Copyright (c) 2007-2017 Nicira, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public
@@ -46,6 +46,8 @@
 #include <net/ndisc.h>
 #include <net/mpls.h>
 #include <net/vxlan.h>
+#include <net/tun_proto.h>
+#include <net/erspan.h>
 
 #include "datapath.h"
 #include "conntrack.h"
@@ -61,6 +63,44 @@ struct ovs_len_tbl {
 #define OVS_ATTR_NESTED -1
 #define OVS_ATTR_VARIABLE -2
 
+static bool actions_may_change_flow(const struct nlattr *actions)
+{
+       struct nlattr *nla;
+       int rem;
+
+       nla_for_each_nested(nla, actions, rem) {
+               u16 action = nla_type(nla);
+
+               switch (action) {
+               case OVS_ACTION_ATTR_OUTPUT:
+               case OVS_ACTION_ATTR_RECIRC:
+               case OVS_ACTION_ATTR_TRUNC:
+               case OVS_ACTION_ATTR_USERSPACE:
+                       break;
+
+               case OVS_ACTION_ATTR_CT:
+               case OVS_ACTION_ATTR_CT_CLEAR:
+               case OVS_ACTION_ATTR_HASH:
+               case OVS_ACTION_ATTR_POP_ETH:
+               case OVS_ACTION_ATTR_POP_MPLS:
+               case OVS_ACTION_ATTR_POP_NSH:
+               case OVS_ACTION_ATTR_POP_VLAN:
+               case OVS_ACTION_ATTR_PUSH_ETH:
+               case OVS_ACTION_ATTR_PUSH_MPLS:
+               case OVS_ACTION_ATTR_PUSH_NSH:
+               case OVS_ACTION_ATTR_PUSH_VLAN:
+               case OVS_ACTION_ATTR_SAMPLE:
+               case OVS_ACTION_ATTR_SET:
+               case OVS_ACTION_ATTR_SET_MASKED:
+               case OVS_ACTION_ATTR_METER:
+               case OVS_ACTION_ATTR_CHECK_PKT_LEN:
+               default:
+                       return true;
+               }
+       }
+       return false;
+}
+
 static void update_range(struct sw_flow_match *match,
                         size_t offset, size_t size, bool is_mask)
 {
@@ -124,14 +164,16 @@ static void update_range(struct sw_flow_match *match,
 static bool match_validate(const struct sw_flow_match *match,
                           u64 key_attrs, u64 mask_attrs, bool log)
 {
-       u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
+       u64 key_expected = 0;
        u64 mask_allowed = key_attrs;  /* At most allow all key attributes */
 
        /* The following mask attributes allowed only if they
         * pass the validation tests.
         */
        mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
+                       | (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
                        | (1ULL << OVS_KEY_ATTR_IPV6)
+                       | (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
                        | (1ULL << OVS_KEY_ATTR_TCP)
                        | (1ULL << OVS_KEY_ATTR_TCP_FLAGS)
                        | (1ULL << OVS_KEY_ATTR_UDP)
@@ -140,7 +182,8 @@ static bool match_validate(const struct sw_flow_match *match,
                        | (1ULL << OVS_KEY_ATTR_ICMPV6)
                        | (1ULL << OVS_KEY_ATTR_ARP)
                        | (1ULL << OVS_KEY_ATTR_ND)
-                       | (1ULL << OVS_KEY_ATTR_MPLS));
+                       | (1ULL << OVS_KEY_ATTR_MPLS)
+                       | (1ULL << OVS_KEY_ATTR_NSH));
 
        /* Always allowed mask fields. */
        mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
@@ -163,8 +206,10 @@ static bool match_validate(const struct sw_flow_match *match,
 
        if (match->key->eth.type == htons(ETH_P_IP)) {
                key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
-               if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
+               if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
                        mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
+                       mask_allowed |= 1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
+               }
 
                if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
                        if (match->key->ip.proto == IPPROTO_UDP) {
@@ -198,8 +243,10 @@ static bool match_validate(const struct sw_flow_match *match,
 
        if (match->key->eth.type == htons(ETH_P_IPV6)) {
                key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
-               if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
+               if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
                        mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
+                       mask_allowed |= 1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
+               }
 
                if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
                        if (match->key->ip.proto == IPPROTO_UDP) {
@@ -232,6 +279,12 @@ static bool match_validate(const struct sw_flow_match *match,
                                                htons(NDISC_NEIGHBOUR_SOLICITATION) ||
                                    match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
                                        key_expected |= 1ULL << OVS_KEY_ATTR_ND;
+                                       /* Original direction conntrack tuple
+                                        * uses the same space as the ND fields
+                                        * in the key, so both are not allowed
+                                        * at the same time.
+                                        */
+                                       mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
                                        if (match->mask && (match->mask->key.tp.src == htons(0xff)))
                                                mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
                                }
@@ -239,6 +292,14 @@ static bool match_validate(const struct sw_flow_match *match,
                }
        }
 
+       if (match->key->eth.type == htons(ETH_P_NSH)) {
+               key_expected |= 1 << OVS_KEY_ATTR_NSH;
+               if (match->mask &&
+                   match->mask->key.eth.type == htons(0xffff)) {
+                       mask_allowed |= 1 << OVS_KEY_ATTR_NSH;
+               }
+       }
+
        if ((key_attrs & key_expected) != key_expected) {
                /* Key attributes check failed. */
                OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
@@ -263,28 +324,42 @@ size_t ovs_tun_key_attr_size(void)
        /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
         * 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 */
+       return    nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
+               + 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 */
                + nla_total_size(0)    /* OVS_TUNNEL_KEY_ATTR_CSUM */
                + nla_total_size(0)    /* OVS_TUNNEL_KEY_ATTR_OAM */
                + nla_total_size(256)  /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
-               /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
+               /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS and
+                * OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS is mutually exclusive with
                 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
                 */
                + nla_total_size(2)    /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
                + nla_total_size(2);   /* OVS_TUNNEL_KEY_ATTR_TP_DST */
 }
 
+static size_t ovs_nsh_key_attr_size(void)
+{
+       /* Whenever adding new OVS_NSH_KEY_ FIELDS, we should consider
+        * updating this function.
+        */
+       return  nla_total_size(NSH_BASE_HDR_LEN) /* OVS_NSH_KEY_ATTR_BASE */
+               /* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
+                * mutually exclusive, so the bigger one can cover
+                * the small one.
+                */
+               + nla_total_size(NSH_CTX_HDRS_MAX_LEN);
+}
+
 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 != 26);
+       BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 29);
 
        return    nla_total_size(4)   /* OVS_KEY_ATTR_PRIORITY */
                + nla_total_size(0)   /* OVS_KEY_ATTR_TUNNEL */
@@ -293,10 +368,13 @@ 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(1)   /* OVS_KEY_ATTR_CT_STATE */
+               + 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_LABEL */
+               + nla_total_size(16)  /* OVS_KEY_ATTR_CT_LABELS */
+               + nla_total_size(40)  /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
+               + nla_total_size(0)   /* OVS_KEY_ATTR_NSH */
+                 + ovs_nsh_key_attr_size()
                + nla_total_size(12)  /* OVS_KEY_ATTR_ETHERNET */
                + nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
                + nla_total_size(4)   /* OVS_KEY_ATTR_VLAN */
@@ -325,6 +403,16 @@ static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1]
        [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) },
+       [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS]   = { .len = OVS_ATTR_VARIABLE },
+};
+
+static const struct ovs_len_tbl
+ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = {
+       [OVS_NSH_KEY_ATTR_BASE] = { .len = sizeof(struct ovs_nsh_key_base) },
+       [OVS_NSH_KEY_ATTR_MD1]  = { .len = sizeof(struct ovs_nsh_key_md1) },
+       [OVS_NSH_KEY_ATTR_MD2]  = { .len = OVS_ATTR_VARIABLE },
 };
 
 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute.  */
@@ -351,10 +439,16 @@ 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(u8) },
+       [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_LABEL]  = { .len = sizeof(struct ovs_key_ct_label) },
+       [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
+       [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
+               .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
+       [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
+               .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
+       [OVS_KEY_ATTR_NSH]       = { .len = OVS_ATTR_NESTED,
+                                    .next = ovs_nsh_key_attr_lens, },
 };
 
 static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
@@ -409,7 +503,7 @@ static int __parse_flow_nlattrs(const struct nlattr *attr,
                        return -EINVAL;
                }
 
-               if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
+               if (!nz || !is_all_zero(nla_data(nla), nla_len(nla))) {
                        attrs |= 1ULL << type;
                        a[type] = nla;
                }
@@ -430,9 +524,8 @@ static int parse_flow_mask_nlattrs(const struct nlattr *attr,
        return __parse_flow_nlattrs(attr, a, attrsp, log, true);
 }
 
-static int parse_flow_nlattrs(const struct nlattr *attr,
-                             const struct nlattr *a[], u64 *attrsp,
-                             bool log)
+int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
+                      u64 *attrsp, bool log)
 {
        return __parse_flow_nlattrs(attr, a, attrsp, log, false);
 }
@@ -544,15 +637,42 @@ static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
        return 0;
 }
 
-static int ipv4_tun_from_nlattr(const struct nlattr *attr,
-                               struct sw_flow_match *match, bool is_mask,
-                               bool log)
+static int erspan_tun_opt_from_nlattr(const struct nlattr *a,
+                                     struct sw_flow_match *match, bool is_mask,
+                                     bool log)
 {
-       struct nlattr *a;
-       int rem;
-       bool ttl = false;
+       unsigned long opt_key_offset;
+
+       BUILD_BUG_ON(sizeof(struct erspan_metadata) >
+                    sizeof(match->key->tun_opts));
+
+       if (nla_len(a) > sizeof(match->key->tun_opts)) {
+               OVS_NLERR(log, "ERSPAN option length err (len %d, max %zu).",
+                         nla_len(a), sizeof(match->key->tun_opts));
+               return -EINVAL;
+       }
+
+       if (!is_mask)
+               SW_FLOW_KEY_PUT(match, tun_opts_len,
+                               sizeof(struct erspan_metadata), false);
+       else
+               SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
+
+       opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
+       SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
+                                 nla_len(a), is_mask);
+       return 0;
+}
+
+static int ip_tun_from_nlattr(const struct nlattr *attr,
+                             struct sw_flow_match *match, bool is_mask,
+                             bool log)
+{
+       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);
@@ -580,10 +700,22 @@ static int ipv4_tun_from_nlattr(const struct nlattr *attr,
                case OVS_TUNNEL_KEY_ATTR_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.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.src,
+                                       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.tos,
@@ -637,29 +769,63 @@ static int ipv4_tun_from_nlattr(const struct nlattr *attr,
                        tun_flags |= TUNNEL_VXLAN_OPT;
                        opts_type = type;
                        break;
+               case OVS_TUNNEL_KEY_ATTR_PAD:
+                       break;
+               case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
+                       if (opts_type) {
+                               OVS_NLERR(log, "Multiple metadata blocks provided");
+                               return -EINVAL;
+                       }
+
+                       err = erspan_tun_opt_from_nlattr(a, match, is_mask,
+                                                        log);
+                       if (err)
+                               return err;
+
+                       tun_flags |= TUNNEL_ERSPAN_OPT;
+                       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.u.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;
                }
        }
@@ -684,21 +850,37 @@ static int vxlan_opt_to_nlattr(struct sk_buff *skb,
        return 0;
 }
 
-static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
-                               const struct ip_tunnel_key *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->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))
+           nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
+                        OVS_TUNNEL_KEY_ATTR_PAD))
                return -EMSGSIZE;
+       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;
@@ -719,7 +901,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))
@@ -727,14 +909,19 @@ static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
                else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
                         vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
                        return -EMSGSIZE;
+               else if (output->tun_flags & TUNNEL_ERSPAN_OPT &&
+                        nla_put(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
+                                swkey_tun_opts_len, tun_opts))
+                       return -EMSGSIZE;
        }
 
        return 0;
 }
 
-static int ipv4_tun_to_nlattr(struct sk_buff *skb,
-                             const struct ip_tunnel_key *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;
@@ -743,7 +930,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;
 
@@ -751,19 +939,203 @@ 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 ip_tunnel_info *egress_tun_info,
-                                 const void *egress_tun_opts)
+int ovs_nla_put_tunnel_info(struct sk_buff *skb,
+                           struct ip_tunnel_info *tun_info)
+{
+       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 encode_vlan_from_nlattrs(struct sw_flow_match *match,
+                                   const struct nlattr *a[],
+                                   bool is_mask, bool inner)
+{
+       __be16 tci = 0;
+       __be16 tpid = 0;
+
+       if (a[OVS_KEY_ATTR_VLAN])
+               tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
+
+       if (a[OVS_KEY_ATTR_ETHERTYPE])
+               tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
+
+       if (likely(!inner)) {
+               SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
+               SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
+       } else {
+               SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
+               SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
+       }
+       return 0;
+}
+
+static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
+                                     u64 key_attrs, bool inner,
+                                     const struct nlattr **a, bool log)
+{
+       __be16 tci = 0;
+
+       if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
+             (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
+              eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
+               /* Not a VLAN. */
+               return 0;
+       }
+
+       if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
+             (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
+               OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
+               return -EINVAL;
+       }
+
+       if (a[OVS_KEY_ATTR_VLAN])
+               tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
+
+       if (!(tci & htons(VLAN_TAG_PRESENT))) {
+               if (tci) {
+                       OVS_NLERR(log, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
+                                 (inner) ? "C-VLAN" : "VLAN");
+                       return -EINVAL;
+               } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
+                       /* Corner case for truncated VLAN header. */
+                       OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
+                                 (inner) ? "C-VLAN" : "VLAN");
+                       return -EINVAL;
+               }
+       }
+
+       return 1;
+}
+
+static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
+                                          u64 key_attrs, bool inner,
+                                          const struct nlattr **a, bool log)
+{
+       __be16 tci = 0;
+       __be16 tpid = 0;
+       bool encap_valid = !!(match->key->eth.vlan.tci &
+                             htons(VLAN_TAG_PRESENT));
+       bool i_encap_valid = !!(match->key->eth.cvlan.tci &
+                               htons(VLAN_TAG_PRESENT));
+
+       if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
+               /* Not a VLAN. */
+               return 0;
+       }
+
+       if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
+               OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
+                         (inner) ? "C-VLAN" : "VLAN");
+               return -EINVAL;
+       }
+
+       if (a[OVS_KEY_ATTR_VLAN])
+               tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
+
+       if (a[OVS_KEY_ATTR_ETHERTYPE])
+               tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
+
+       if (tpid != htons(0xffff)) {
+               OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
+                         (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
+               return -EINVAL;
+       }
+       if (!(tci & htons(VLAN_TAG_PRESENT))) {
+               OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
+                         (inner) ? "C-VLAN" : "VLAN");
+               return -EINVAL;
+       }
+
+       return 1;
+}
+
+static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
+                                    u64 *key_attrs, bool inner,
+                                    const struct nlattr **a, bool is_mask,
+                                    bool log)
+{
+       int err;
+       const struct nlattr *encap;
+
+       if (!is_mask)
+               err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
+                                                a, log);
+       else
+               err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
+                                                     a, log);
+       if (err <= 0)
+               return err;
+
+       err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
+       if (err)
+               return err;
+
+       *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
+       *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
+       *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
+
+       encap = a[OVS_KEY_ATTR_ENCAP];
+
+       if (!is_mask)
+               err = parse_flow_nlattrs(encap, a, key_attrs, log);
+       else
+               err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
+
+       return err;
+}
+
+static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
+                                  u64 *key_attrs, const struct nlattr **a,
+                                  bool is_mask, bool log)
+{
+       int err;
+       bool encap_valid = false;
+
+       err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
+                                       is_mask, log);
+       if (err)
+               return err;
+
+       encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_TAG_PRESENT));
+       if (encap_valid) {
+               err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
+                                               is_mask, log);
+               if (err)
+                       return err;
+       }
+
+       return 0;
+}
+
+static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
+                                      u64 *attrs, const struct nlattr **a,
+                                      bool is_mask, bool log)
 {
-       return __ipv4_tun_to_nlattr(skb, &egress_tun_info->key,
-                                   egress_tun_opts,
-                                   egress_tun_info->options_len);
+       __be16 eth_type;
+
+       eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
+       if (is_mask) {
+               /* Always exact match EtherType. */
+               eth_type = htons(0xffff);
+       } 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;
+       }
+
+       SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
+       *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
+       return 0;
 }
 
 static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
                                  u64 *attrs, const struct nlattr **a,
                                 bool is_mask, bool log)
 {
+       u8 mac_proto = MAC_PROTO_ETHERNET;
+
        if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) {
                u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
 
@@ -808,7 +1180,7 @@ static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
                *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
        }
        if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
-               if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
+               if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
                                         is_mask, log) < 0)
                        return -EINVAL;
                *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
@@ -816,16 +1188,22 @@ static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
 
        if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
            ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
-               u8 ct_state = nla_get_u8(a[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);
+               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);
+               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) &&
@@ -835,15 +1213,273 @@ static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
                SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
                *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
        }
-       if (*attrs & (1 << OVS_KEY_ATTR_CT_LABEL) &&
-           ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABEL)) {
-               const struct ovs_key_ct_label *cl;
+       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_LABEL]);
-               SW_FLOW_KEY_MEMCPY(match, ct.label, cl->ct_label,
+               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_LABEL);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
+       }
+       if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
+               const struct ovs_key_ct_tuple_ipv4 *ct;
+
+               ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
+
+               SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
+               SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
+               SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
+               SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
+               SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
+       }
+       if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
+               const struct ovs_key_ct_tuple_ipv6 *ct;
+
+               ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
+
+               SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
+                                  sizeof(match->key->ipv6.ct_orig.src),
+                                  is_mask);
+               SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
+                                  sizeof(match->key->ipv6.ct_orig.dst),
+                                  is_mask);
+               SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
+               SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
+               SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
+       }
+
+       /* For layer 3 packets the Ethernet type is provided
+        * and treated as metadata but no MAC addresses are provided.
+        */
+       if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
+           (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
+               mac_proto = MAC_PROTO_NONE;
+
+       /* Always exact match mac_proto */
+       SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
+
+       if (mac_proto == MAC_PROTO_NONE)
+               return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
+                                                  log);
+
+       return 0;
+}
+
+int nsh_hdr_from_nlattr(const struct nlattr *attr,
+                       struct nshhdr *nh, size_t size)
+{
+       struct nlattr *a;
+       int rem;
+       u8 flags = 0;
+       u8 ttl = 0;
+       int mdlen = 0;
+
+       /* validate_nsh has check this, so we needn't do duplicate check here
+        */
+       if (size < NSH_BASE_HDR_LEN)
+               return -ENOBUFS;
+
+       nla_for_each_nested(a, attr, rem) {
+               int type = nla_type(a);
+
+               switch (type) {
+               case OVS_NSH_KEY_ATTR_BASE: {
+                       const struct ovs_nsh_key_base *base = nla_data(a);
+
+                       flags = base->flags;
+                       ttl = base->ttl;
+                       nh->np = base->np;
+                       nh->mdtype = base->mdtype;
+                       nh->path_hdr = base->path_hdr;
+                       break;
+               }
+               case OVS_NSH_KEY_ATTR_MD1:
+                       mdlen = nla_len(a);
+                       if (mdlen > size - NSH_BASE_HDR_LEN)
+                               return -ENOBUFS;
+                       memcpy(&nh->md1, nla_data(a), mdlen);
+                       break;
+
+               case OVS_NSH_KEY_ATTR_MD2:
+                       mdlen = nla_len(a);
+                       if (mdlen > size - NSH_BASE_HDR_LEN)
+                               return -ENOBUFS;
+                       memcpy(&nh->md2, nla_data(a), mdlen);
+                       break;
+
+               default:
+                       return -EINVAL;
+               }
        }
+
+       /* nsh header length  = NSH_BASE_HDR_LEN + mdlen */
+       nh->ver_flags_ttl_len = 0;
+       nsh_set_flags_ttl_len(nh, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
+
+       return 0;
+}
+
+int nsh_key_from_nlattr(const struct nlattr *attr,
+                       struct ovs_key_nsh *nsh, struct ovs_key_nsh *nsh_mask)
+{
+       struct nlattr *a;
+       int rem;
+
+       /* validate_nsh has check this, so we needn't do duplicate check here
+        */
+       nla_for_each_nested(a, attr, rem) {
+               int type = nla_type(a);
+
+               switch (type) {
+               case OVS_NSH_KEY_ATTR_BASE: {
+                       const struct ovs_nsh_key_base *base = nla_data(a);
+                       const struct ovs_nsh_key_base *base_mask = base + 1;
+
+                       nsh->base = *base;
+                       nsh_mask->base = *base_mask;
+                       break;
+               }
+               case OVS_NSH_KEY_ATTR_MD1: {
+                       const struct ovs_nsh_key_md1 *md1 = nla_data(a);
+                       const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
+
+                       memcpy(nsh->context, md1->context, sizeof(*md1));
+                       memcpy(nsh_mask->context, md1_mask->context,
+                              sizeof(*md1_mask));
+                       break;
+               }
+               case OVS_NSH_KEY_ATTR_MD2:
+                       /* Not supported yet */
+                       return -ENOTSUPP;
+               default:
+                       return -EINVAL;
+               }
+       }
+
+       return 0;
+}
+
+static int nsh_key_put_from_nlattr(const struct nlattr *attr,
+                                  struct sw_flow_match *match, bool is_mask,
+                                  bool is_push_nsh, bool log)
+{
+       struct nlattr *a;
+       int rem;
+       bool has_base = false;
+       bool has_md1 = false;
+       bool has_md2 = false;
+       u8 mdtype = 0;
+       int mdlen = 0;
+
+       if (WARN_ON(is_push_nsh && is_mask))
+               return -EINVAL;
+
+       nla_for_each_nested(a, attr, rem) {
+               int type = nla_type(a);
+               int i;
+
+               if (type > OVS_NSH_KEY_ATTR_MAX) {
+                       OVS_NLERR(log, "nsh attr %d is out of range max %d",
+                                 type, OVS_NSH_KEY_ATTR_MAX);
+                       return -EINVAL;
+               }
+
+               if (!check_attr_len(nla_len(a),
+                                   ovs_nsh_key_attr_lens[type].len)) {
+                       OVS_NLERR(
+                           log,
+                           "nsh attr %d has unexpected len %d expected %d",
+                           type,
+                           nla_len(a),
+                           ovs_nsh_key_attr_lens[type].len
+                       );
+                       return -EINVAL;
+               }
+
+               switch (type) {
+               case OVS_NSH_KEY_ATTR_BASE: {
+                       const struct ovs_nsh_key_base *base = nla_data(a);
+
+                       has_base = true;
+                       mdtype = base->mdtype;
+                       SW_FLOW_KEY_PUT(match, nsh.base.flags,
+                                       base->flags, is_mask);
+                       SW_FLOW_KEY_PUT(match, nsh.base.ttl,
+                                       base->ttl, is_mask);
+                       SW_FLOW_KEY_PUT(match, nsh.base.mdtype,
+                                       base->mdtype, is_mask);
+                       SW_FLOW_KEY_PUT(match, nsh.base.np,
+                                       base->np, is_mask);
+                       SW_FLOW_KEY_PUT(match, nsh.base.path_hdr,
+                                       base->path_hdr, is_mask);
+                       break;
+               }
+               case OVS_NSH_KEY_ATTR_MD1: {
+                       const struct ovs_nsh_key_md1 *md1 = nla_data(a);
+
+                       has_md1 = true;
+                       for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++)
+                               SW_FLOW_KEY_PUT(match, nsh.context[i],
+                                               md1->context[i], is_mask);
+                       break;
+               }
+               case OVS_NSH_KEY_ATTR_MD2:
+                       if (!is_push_nsh) /* Not supported MD type 2 yet */
+                               return -ENOTSUPP;
+
+                       has_md2 = true;
+                       mdlen = nla_len(a);
+                       if (mdlen > NSH_CTX_HDRS_MAX_LEN || mdlen <= 0) {
+                               OVS_NLERR(
+                                   log,
+                                   "Invalid MD length %d for MD type %d",
+                                   mdlen,
+                                   mdtype
+                               );
+                               return -EINVAL;
+                       }
+                       break;
+               default:
+                       OVS_NLERR(log, "Unknown nsh attribute %d",
+                                 type);
+                       return -EINVAL;
+               }
+       }
+
+       if (rem > 0) {
+               OVS_NLERR(log, "nsh attribute has %d unknown bytes.", rem);
+               return -EINVAL;
+       }
+
+       if (has_md1 && has_md2) {
+               OVS_NLERR(
+                   1,
+                   "invalid nsh attribute: md1 and md2 are exclusive."
+               );
+               return -EINVAL;
+       }
+
+       if (!is_mask) {
+               if ((has_md1 && mdtype != NSH_M_TYPE1) ||
+                   (has_md2 && mdtype != NSH_M_TYPE2)) {
+                       OVS_NLERR(1, "nsh attribute has unmatched MD type %d.",
+                                 mdtype);
+                       return -EINVAL;
+               }
+
+               if (is_push_nsh &&
+                   (!has_base || (!has_md1 && !has_md2))) {
+                       OVS_NLERR(
+                           1,
+                           "push_nsh: missing base or metadata attributes"
+                       );
+                       return -EINVAL;
+               }
+       }
+
        return 0;
 }
 
@@ -866,42 +1502,26 @@ static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
                SW_FLOW_KEY_MEMCPY(match, eth.dst,
                                eth_key->eth_dst, ETH_ALEN, is_mask);
                attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
-       }
-
-       if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
-               __be16 tci;
-
-               tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
-               if (!(tci & htons(VLAN_TAG_PRESENT))) {
-                       if (is_mask)
-                               OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
-                       else
-                               OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
 
+               if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
+                       /* VLAN attribute is always parsed before getting here since it
+                        * may occur multiple times.
+                        */
+                       OVS_NLERR(log, "VLAN attribute unexpected.");
                        return -EINVAL;
                }
 
-               SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
-               attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
-       }
-
-       if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
-               __be16 eth_type;
-
-               eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
-               if (is_mask) {
-                       /* Always exact match EtherType. */
-                       eth_type = htons(0xffff);
-               } 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;
+               if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
+                       err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
+                                                         log);
+                       if (err)
+                               return err;
+               } else if (!is_mask) {
+                       SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
                }
-
-               SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
-               attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
-       } else if (!is_mask) {
-               SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
+       } else if (!match->key->eth.type) {
+               OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
+               return -EINVAL;
        }
 
        if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
@@ -939,8 +1559,7 @@ static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
                }
 
                if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
-                       OVS_NLERR(log,
-                                 "Invalid IPv6 flow label value (value=%x, max=%x).",
+                       OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
                                  ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
                        return -EINVAL;
                }
@@ -991,6 +1610,13 @@ static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
                attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
        }
 
+       if (attrs & (1 << OVS_KEY_ATTR_NSH)) {
+               if (nsh_key_put_from_nlattr(a[OVS_KEY_ATTR_NSH], match,
+                                           is_mask, false, log) < 0)
+                       return -EINVAL;
+               attrs &= ~(1 << OVS_KEY_ATTR_NSH);
+       }
+
        if (attrs & (1ULL << OVS_KEY_ATTR_MPLS)) {
                const struct ovs_key_mpls *mpls_key;
 
@@ -1089,13 +1715,13 @@ 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[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 {
+               if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
+                       nlattr_set(nla, val, tbl[nla_type(nla)].next ? : 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;
        }
 }
 
@@ -1126,49 +1752,18 @@ int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
                      bool log)
 {
        const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
-       const struct nlattr *encap;
        struct nlattr *newmask = NULL;
        u64 key_attrs = 0;
        u64 mask_attrs = 0;
-       bool encap_valid = false;
        int err;
 
        err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
        if (err)
                return err;
 
-       if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
-           (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
-           (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
-               __be16 tci;
-
-               if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
-                     (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
-                       OVS_NLERR(log, "Invalid Vlan frame.");
-                       return -EINVAL;
-               }
-
-               key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
-               tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
-               encap = a[OVS_KEY_ATTR_ENCAP];
-               key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
-               encap_valid = true;
-
-               if (tci & htons(VLAN_TAG_PRESENT)) {
-                       err = parse_flow_nlattrs(encap, a, &key_attrs, log);
-                       if (err)
-                               return err;
-               } else if (!tci) {
-                       /* Corner case for truncated 802.1Q header. */
-                       if (nla_len(encap)) {
-                               OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
-                               return -EINVAL;
-                       }
-               } else {
-                       OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
-                       return  -EINVAL;
-               }
-       }
+       err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
+       if (err)
+               return err;
 
        err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
        if (err)
@@ -1197,7 +1792,7 @@ int ovs_nla_get_match(struct net *net, 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.u.ipv4.dst)
+                       if (match->key->tun_proto)
                                SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
                                                         0xff, true);
 
@@ -1208,47 +1803,12 @@ int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
                if (err)
                        goto free_newmask;
 
-               /* Always match on tci. */
-               SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
-
-               if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
-                       __be16 eth_type = 0;
-                       __be16 tci = 0;
-
-                       if (!encap_valid) {
-                               OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
-                               err = -EINVAL;
-                               goto free_newmask;
-                       }
+               SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
+               SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
 
-                       mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
-                       if (a[OVS_KEY_ATTR_ETHERTYPE])
-                               eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
-
-                       if (eth_type == htons(0xffff)) {
-                               mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
-                               encap = a[OVS_KEY_ATTR_ENCAP];
-                               err = parse_flow_mask_nlattrs(encap, a,
-                                                             &mask_attrs, log);
-                               if (err)
-                                       goto free_newmask;
-                       } else {
-                               OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
-                                         ntohs(eth_type));
-                               err = -EINVAL;
-                               goto free_newmask;
-                       }
-
-                       if (a[OVS_KEY_ATTR_VLAN])
-                               tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
-
-                       if (!(tci & htons(VLAN_TAG_PRESENT))) {
-                               OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
-                                         ntohs(tci));
-                               err = -EINVAL;
-                               goto free_newmask;
-                       }
-               }
+               err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
+               if (err)
+                       goto free_newmask;
 
                err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
                                           log);
@@ -1319,9 +1879,12 @@ u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
 
 /**
  * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
- * @key: Receives extracted in_port, priority, tun_key and skb_mark.
- * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
- * sequence.
+ * @net: Network namespace.
+ * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
+ * metadata.
+ * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
+ * attributes.
+ * @attrs: Bit mask for the netlink attributes included in @a.
  * @log: Boolean to allow kernel error logging.  Normally true, but when
  * probing for feature compatibility this should be passed in as false to
  * suppress unnecessary error logging.
@@ -1330,29 +1893,68 @@ u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
  * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  * get the metadata, that is, the parts of the flow key that cannot be
  * extracted from the packet itself.
+ *
+ * This must be called before the packet key fields are filled in 'key'.
  */
 
-int ovs_nla_get_flow_metadata(struct net *net, const struct nlattr *attr,
-                             struct sw_flow_key *key,
-                             bool log)
+int ovs_nla_get_flow_metadata(struct net *net,
+                             const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
+                             u64 attrs, struct sw_flow_key *key, bool log)
+{
+       struct sw_flow_match match;
+
+       memset(&match, 0, sizeof(match));
+       match.key = key;
+
+       key->ct_state = 0;
+       key->ct_zone = 0;
+       key->ct_orig_proto = 0;
+       memset(&key->ct, 0, sizeof(key->ct));
+       memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
+       memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
+
+       key->phy.in_port = DP_MAX_PORTS;
+
+       return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
+}
+
+static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
+                           bool is_mask)
+{
+       __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
+
+       if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
+           nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
+               return -EMSGSIZE;
+       return 0;
+}
+
+static int nsh_key_to_nlattr(const struct ovs_key_nsh *nsh, bool is_mask,
+                            struct sk_buff *skb)
 {
-       const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
-       struct sw_flow_match match;
-       u64 attrs = 0;
-       int err;
+       struct nlattr *start;
 
-       err = parse_flow_nlattrs(attr, a, &attrs, log);
-       if (err)
-               return -EINVAL;
+       start = nla_nest_start(skb, OVS_KEY_ATTR_NSH);
+       if (!start)
+               return -EMSGSIZE;
 
-       memset(&match, 0, sizeof(match));
-       match.key = key;
+       if (nla_put(skb, OVS_NSH_KEY_ATTR_BASE, sizeof(nsh->base), &nsh->base))
+               goto nla_put_failure;
 
-       memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
-       memset(&key->ct, 0, sizeof(key->ct));
-       key->phy.in_port = DP_MAX_PORTS;
+       if (is_mask || nsh->base.mdtype == NSH_M_TYPE1) {
+               if (nla_put(skb, OVS_NSH_KEY_ATTR_MD1,
+                           sizeof(nsh->context), nsh->context))
+                       goto nla_put_failure;
+       }
 
-       return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
+       /* Don't support MD type 2 yet */
+
+       nla_nest_end(skb, start);
+
+       return 0;
+
+nla_put_failure:
+       return -EMSGSIZE;
 }
 
 static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
@@ -1360,7 +1962,9 @@ static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
                             struct sk_buff *skb)
 {
        struct ovs_key_ethernet *eth_key;
-       struct nlattr *nla, *encap;
+       struct nlattr *nla;
+       struct nlattr *encap = NULL;
+       struct nlattr *in_encap = NULL;
 
        if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
                goto nla_put_failure;
@@ -1371,14 +1975,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.u.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;
        }
 
@@ -1398,46 +2002,60 @@ 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))
+       if (ovs_ct_put_key(swkey, output, skb))
                goto nla_put_failure;
 
-       nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
-       if (!nla)
-               goto nla_put_failure;
+       if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
+               nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
+               if (!nla)
+                       goto nla_put_failure;
 
-       eth_key = nla_data(nla);
-       ether_addr_copy(eth_key->eth_src, output->eth.src);
-       ether_addr_copy(eth_key->eth_dst, output->eth.dst);
+               eth_key = nla_data(nla);
+               ether_addr_copy(eth_key->eth_src, output->eth.src);
+               ether_addr_copy(eth_key->eth_dst, output->eth.dst);
 
-       if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
-               __be16 eth_type;
-               eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
-               if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
-                   nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
-                       goto nla_put_failure;
-               encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
-               if (!swkey->eth.tci)
-                       goto unencap;
-       } else
-               encap = NULL;
-
-       if (swkey->eth.type == htons(ETH_P_802_2)) {
-               /*
-                * Ethertype 802.2 is represented in the netlink with omitted
-                * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
-                * 0xffff in the mask attribute.  Ethertype can also
-                * be wildcarded.
-                */
-               if (is_mask && output->eth.type)
-                       if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
-                                               output->eth.type))
+               if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
+                       if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
                                goto nla_put_failure;
-               goto unencap;
+                       encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
+                       if (!swkey->eth.vlan.tci)
+                               goto unencap;
+
+                       if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
+                               if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
+                                       goto nla_put_failure;
+                               in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
+                               if (!swkey->eth.cvlan.tci)
+                                       goto unencap;
+                       }
+               }
+
+               if (swkey->eth.type == htons(ETH_P_802_2)) {
+                       /*
+                        * Ethertype 802.2 is represented in the netlink with omitted
+                        * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
+                        * 0xffff in the mask attribute.  Ethertype can also
+                        * be wildcarded.
+                        */
+                       if (is_mask && output->eth.type)
+                               if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
+                                                       output->eth.type))
+                                       goto nla_put_failure;
+                       goto unencap;
+               }
        }
 
        if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
                goto nla_put_failure;
 
+       if (eth_type_vlan(swkey->eth.type)) {
+               /* There are 3 VLAN tags, we don't know anything about the rest
+                * of the packet, so truncate here.
+                */
+               WARN_ON_ONCE(!(encap && in_encap));
+               goto unencap;
+       }
+
        if (swkey->eth.type == htons(ETH_P_IP)) {
                struct ovs_key_ipv4 *ipv4_key;
 
@@ -1467,6 +2085,9 @@ static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
                ipv6_key->ipv6_tclass = output->ip.tos;
                ipv6_key->ipv6_hlimit = output->ip.ttl;
                ipv6_key->ipv6_frag = output->ip.frag;
+       } else if (swkey->eth.type == htons(ETH_P_NSH)) {
+               if (nsh_key_to_nlattr(&output->nsh, is_mask, skb))
+                       goto nla_put_failure;
        } else if (swkey->eth.type == htons(ETH_P_ARP) ||
                   swkey->eth.type == htons(ETH_P_RARP)) {
                struct ovs_key_arp *arp_key;
@@ -1564,6 +2185,8 @@ static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
        }
 
 unencap:
+       if (in_encap)
+               nla_nest_end(skb, in_encap);
        if (encap)
                nla_nest_end(skb, encap);
 
@@ -1616,16 +2239,17 @@ int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
                                OVS_FLOW_ATTR_MASK, true, skb);
 }
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4,9,0)
+#define MAX_ACTIONS_BUFSIZE    (16 * 1024)
+#else
 #define MAX_ACTIONS_BUFSIZE    (32 * 1024)
+#endif
 
-static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
+static struct sw_flow_actions *nla_alloc_flow_actions(int size)
 {
        struct sw_flow_actions *sfa;
 
-       if (size > MAX_ACTIONS_BUFSIZE) {
-               OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
-               return ERR_PTR(-EINVAL);
-       }
+       WARN_ON_ONCE(size > MAX_ACTIONS_BUFSIZE);
 
        sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
        if (!sfa)
@@ -1676,8 +2300,7 @@ static void __ovs_nla_free_flow_actions(struct rcu_head *head)
 }
 
 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
- * The caller must hold rcu_read_lock for this to be sensible.
- */
+ * The caller must hold rcu_read_lock for this to be sensible. */
 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
 {
        call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
@@ -1689,22 +2312,25 @@ static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
 
        struct sw_flow_actions *acts;
        int new_acts_size;
-       int req_size = NLA_ALIGN(attr_len);
+       size_t req_size = NLA_ALIGN(attr_len);
        int next_offset = offsetof(struct sw_flow_actions, actions) +
                                        (*sfa)->actions_len;
 
        if (req_size <= (ksize(*sfa) - next_offset))
                goto out;
 
-       new_acts_size = ksize(*sfa) * 2;
+       new_acts_size = max(next_offset + req_size, ksize(*sfa) * 2);
 
        if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
-               if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
+               if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size) {
+                       OVS_NLERR(log, "Flow action size exceeds max %u",
+                                 MAX_ACTIONS_BUFSIZE);
                        return ERR_PTR(-EMSGSIZE);
+               }
                new_acts_size = MAX_ACTIONS_BUFSIZE;
        }
 
-       acts = nla_alloc_flow_actions(new_acts_size, log);
+       acts = nla_alloc_flow_actions(new_acts_size);
        if (IS_ERR(acts))
                return (void *)acts;
 
@@ -1744,10 +2370,8 @@ int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
        struct nlattr *a;
 
        a = __add_action(sfa, attrtype, data, len, log);
-       if (IS_ERR(a))
-               return PTR_ERR(a);
 
-       return 0;
+       return PTR_ERR_OR_ZERO(a);
 }
 
 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
@@ -1774,18 +2398,20 @@ static inline void add_nested_action_end(struct sw_flow_actions *sfa,
 
 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,
+                                 struct sw_flow_actions **sfa,
                                  __be16 eth_type, __be16 vlan_tci, bool log);
 
 static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
-                                   const struct sw_flow_key *key, int depth,
+                                   const struct sw_flow_key *key,
                                    struct sw_flow_actions **sfa,
-                                   __be16 eth_type, __be16 vlan_tci, bool log)
+                                   __be16 eth_type, __be16 vlan_tci,
+                                   bool log, bool last)
 {
        const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
        const struct nlattr *probability, *actions;
        const struct nlattr *a;
-       int rem, start, err, st_acts;
+       int rem, start, err;
+       struct sample_arg arg;
 
        memset(attrs, 0, sizeof(attrs));
        nla_for_each_nested(a, attr, rem) {
@@ -1809,20 +2435,66 @@ static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
        start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
        if (start < 0)
                return start;
-       err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
-                                nla_data(probability), sizeof(u32), log);
+
+       /* When both skb and flow may be changed, put the sample
+        * into a deferred fifo. On the other hand, if only skb
+        * may be modified, the actions can be executed in place.
+        *
+        * Do this analysis at the flow installation time.
+        * Set 'clone_action->exec' to true if the actions can be
+        * executed without being deferred.
+        *
+        * If the sample is the last action, it can always be excuted
+        * rather than deferred.
+        */
+       arg.exec = last || !actions_may_change_flow(actions);
+       arg.probability = nla_get_u32(probability);
+
+       err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
+                                log);
+       if (err)
+               return err;
+
+       err = __ovs_nla_copy_actions(net, actions, key, sfa,
+                                    eth_type, vlan_tci, log);
+
+       if (err)
+               return err;
+
+       add_nested_action_end(*sfa, start);
+
+       return 0;
+}
+
+static int validate_and_copy_clone(struct net *net,
+                                  const struct nlattr *attr,
+                                  const struct sw_flow_key *key,
+                                  struct sw_flow_actions **sfa,
+                                  __be16 eth_type, __be16 vlan_tci,
+                                  bool log, bool last)
+{
+       int start, err;
+       u32 exec;
+
+       if (nla_len(attr) && nla_len(attr) < NLA_HDRLEN)
+               return -EINVAL;
+
+       start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CLONE, log);
+       if (start < 0)
+               return start;
+
+       exec = last || !actions_may_change_flow(attr);
+
+       err = ovs_nla_add_action(sfa, OVS_CLONE_ATTR_EXEC, &exec,
+                                sizeof(exec), 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(net, actions, key, depth + 1, sfa,
+       err = __ovs_nla_copy_actions(net, attr, key, sfa,
                                     eth_type, vlan_tci, log);
        if (err)
                return err;
 
-       add_nested_action_end(*sfa, st_acts);
        add_nested_action_end(*sfa, start);
 
        return 0;
@@ -1830,13 +2502,15 @@ static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
 
 void ovs_match_init(struct sw_flow_match *match,
                    struct sw_flow_key *key,
+                   bool reset_key,
                    struct sw_flow_mask *mask)
 {
        memset(match, 0, sizeof(*match));
        match->key = key;
        match->mask = mask;
 
-       memset(key, 0, sizeof(*key));
+       if (reset_key)
+               memset(key, 0, sizeof(*key));
 
        if (mask) {
                memset(&mask->key, 0, sizeof(mask->key));
@@ -1865,7 +2539,7 @@ static int validate_geneve_opts(struct sw_flow_key *key)
 
                option = (struct geneve_opt *)((u8 *)option + len);
                opts_len -= len;
-       };
+       }
 
        key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
 
@@ -1882,9 +2556,11 @@ static int validate_and_copy_set_tun(const struct nlattr *attr,
        struct ovs_tunnel_info *ovs_tun;
        struct nlattr *a;
        int err = 0, start, opts_type;
+       __be16 dst_opt_type;
 
-       ovs_match_init(&match, &key, NULL);
-       opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
+       dst_opt_type = 0;
+       ovs_match_init(&match, &key, true, NULL);
+       opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
        if (opts_type < 0)
                return opts_type;
 
@@ -1894,20 +2570,32 @@ static int validate_and_copy_set_tun(const struct nlattr *attr,
                        err = validate_geneve_opts(&key);
                        if (err < 0)
                                return err;
+                       dst_opt_type = TUNNEL_GENEVE_OPT;
                        break;
                case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
+                       dst_opt_type = TUNNEL_VXLAN_OPT;
+                       break;
+               case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
+                       dst_opt_type = TUNNEL_ERSPAN_OPT;
                        break;
                }
-       };
+       }
 
        start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
        if (start < 0)
                return start;
 
-       tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
+       tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
+                                    GFP_KERNEL);
+
        if (!tun_dst)
                return -ENOMEM;
 
+       err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
+       if (err) {
+               dst_release((struct dst_entry *)tun_dst);
+               return err;
+       }
        a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
                         sizeof(*ovs_tun), log);
        if (IS_ERR(a)) {
@@ -1920,6 +2608,8 @@ static int validate_and_copy_set_tun(const struct nlattr *attr,
 
        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
@@ -1928,12 +2618,25 @@ static int validate_and_copy_set_tun(const struct nlattr *attr,
         */
        ip_tunnel_info_opts_set(tun_info,
                                TUN_METADATA_OPTS(&key, key.tun_opts_len),
-                               key.tun_opts_len);
+                               key.tun_opts_len, dst_opt_type);
        add_nested_action_end(*sfa, start);
 
        return err;
 }
 
+static bool validate_nsh(const struct nlattr *attr, bool is_mask,
+                        bool is_push_nsh, bool log)
+{
+       struct sw_flow_match match;
+       struct sw_flow_key key;
+       int ret = 0;
+
+       ovs_match_init(&match, &key, true, NULL);
+       ret = nsh_key_put_from_nlattr(attr, &match, is_mask,
+                                     is_push_nsh, log);
+       return !ret;
+}
+
 /* Return false if there are any non-masked bits set.
  * Mask follows data immediately, before any netlink padding.
  */
@@ -1950,8 +2653,8 @@ static bool validate_masked(u8 *data, int len)
 
 static int validate_set(const struct nlattr *a,
                        const struct sw_flow_key *flow_key,
-                       struct sw_flow_actions **sfa,
-                       bool *skip_copy, __be16 eth_type, bool masked, bool log)
+                       struct sw_flow_actions **sfa, bool *skip_copy,
+                       u8 mac_proto, __be16 eth_type, bool masked, bool log)
 {
        const struct nlattr *ovs_key = nla_data(a);
        int key_type = nla_type(ovs_key);
@@ -1980,14 +2683,19 @@ 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_LABEL:
+       case OVS_KEY_ATTR_CT_LABELS:
+               break;
+
        case OVS_KEY_ATTR_ETHERNET:
+               if (mac_proto != MAC_PROTO_ETHERNET)
+                       return -EINVAL;
                break;
 
        case OVS_KEY_ATTR_TUNNEL:
+#ifndef USE_UPSTREAM_TUNNEL
                if (eth_p_mpls(eth_type))
                        return -EINVAL;
-
+#endif
                if (masked)
                        return -EINVAL; /* Masked tunnel set not supported. */
 
@@ -2075,6 +2783,13 @@ static int validate_set(const struct nlattr *a,
 
                break;
 
+       case OVS_KEY_ATTR_NSH:
+               if (eth_type != htons(ETH_P_NSH))
+                       return -EINVAL;
+               if (!validate_nsh(nla_data(a), masked, false, log))
+                       return -EINVAL;
+               break;
+
        default:
                return -EINVAL;
        }
@@ -2120,8 +2835,8 @@ static int validate_userspace(const struct nlattr *attr)
        struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
        int error;
 
-       error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
-                                attr, userspace_policy);
+       error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr,
+                                userspace_policy, NULL);
        if (error)
                return error;
 
@@ -2132,6 +2847,87 @@ static int validate_userspace(const struct nlattr *attr)
        return 0;
 }
 
+static const struct nla_policy cpl_policy[OVS_CHECK_PKT_LEN_ATTR_MAX + 1] = {
+       [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] = {.type = NLA_U16 },
+       [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] = {.type = NLA_NESTED },
+       [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL] = {.type = NLA_NESTED },
+};
+
+static int validate_and_copy_check_pkt_len(struct net *net,
+                                          const struct nlattr *attr,
+                                          const struct sw_flow_key *key,
+                                          struct sw_flow_actions **sfa,
+                                          __be16 eth_type, __be16 vlan_tci,
+                                          bool log, bool last)
+{
+       const struct nlattr *acts_if_greater, *acts_if_lesser_eq;
+       struct nlattr *a[OVS_CHECK_PKT_LEN_ATTR_MAX + 1];
+       struct check_pkt_len_arg arg;
+       int nested_acts_start;
+       int start, err;
+
+       err = nla_parse_nested(a, OVS_CHECK_PKT_LEN_ATTR_MAX, attr,
+                              cpl_policy, NULL);
+       if (err)
+               return err;
+
+       if (!a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] ||
+           !nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]))
+               return -EINVAL;
+
+       acts_if_lesser_eq = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL];
+       acts_if_greater = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER];
+
+       /* Both the nested action should be present. */
+       if (!acts_if_greater || !acts_if_lesser_eq)
+               return -EINVAL;
+
+       /* validation done, copy the nested actions. */
+       start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CHECK_PKT_LEN,
+                                       log);
+       if (start < 0)
+               return start;
+
+       arg.pkt_len = nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]);
+       arg.exec_for_lesser_equal =
+               last || !actions_may_change_flow(acts_if_lesser_eq);
+       arg.exec_for_greater =
+               last || !actions_may_change_flow(acts_if_greater);
+
+       err = ovs_nla_add_action(sfa, OVS_CHECK_PKT_LEN_ATTR_ARG, &arg,
+                                sizeof(arg), log);
+       if (err)
+               return err;
+
+       nested_acts_start = add_nested_action_start(sfa,
+               OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL, log);
+       if (nested_acts_start < 0)
+               return nested_acts_start;
+
+       err = __ovs_nla_copy_actions(net, acts_if_lesser_eq, key, sfa,
+                                    eth_type, vlan_tci, log);
+
+       if (err)
+               return err;
+
+       add_nested_action_end(*sfa, nested_acts_start);
+
+       nested_acts_start = add_nested_action_start(sfa,
+               OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER, log);
+       if (nested_acts_start < 0)
+               return nested_acts_start;
+
+       err = __ovs_nla_copy_actions(net, acts_if_greater, key, sfa,
+                                    eth_type, vlan_tci, log);
+
+       if (err)
+               return err;
+
+       add_nested_action_end(*sfa, nested_acts_start);
+       add_nested_action_end(*sfa, start);
+       return 0;
+}
+
 static int copy_action(const struct nlattr *from,
                       struct sw_flow_actions **sfa, bool log)
 {
@@ -2148,15 +2944,13 @@ static int copy_action(const struct nlattr *from,
 
 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,
+                                 struct sw_flow_actions **sfa,
                                  __be16 eth_type, __be16 vlan_tci, bool log)
 {
+       u8 mac_proto = ovs_key_mac_proto(key);
        const struct nlattr *a;
        int rem, err;
 
-       if (depth >= SAMPLE_ACTION_DEPTH)
-               return -EOVERFLOW;
-
        nla_for_each_nested(a, attr, rem) {
                /* Expected argument lengths, (u32)-1 for variable length. */
                static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
@@ -2172,6 +2966,15 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                        [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
                        [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
                        [OVS_ACTION_ATTR_CT] = (u32)-1,
+                       [OVS_ACTION_ATTR_CT_CLEAR] = 0,
+                       [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
+                       [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
+                       [OVS_ACTION_ATTR_POP_ETH] = 0,
+                       [OVS_ACTION_ATTR_PUSH_NSH] = (u32)-1,
+                       [OVS_ACTION_ATTR_POP_NSH] = 0,
+                       [OVS_ACTION_ATTR_METER] = sizeof(u32),
+                       [OVS_ACTION_ATTR_CLONE] = (u32)-1,
+                       [OVS_ACTION_ATTR_CHECK_PKT_LEN] = (u32)-1,
                };
                const struct ovs_action_push_vlan *vlan;
                int type = nla_type(a);
@@ -2198,6 +3001,14 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                                return -EINVAL;
                        break;
 
+               case OVS_ACTION_ATTR_TRUNC: {
+                       const struct ovs_action_trunc *trunc = nla_data(a);
+
+                       if (trunc->max_len < ETH_HLEN)
+                               return -EINVAL;
+                       break;
+               }
+
                case OVS_ACTION_ATTR_HASH: {
                        const struct ovs_action_hash *act_hash = nla_data(a);
 
@@ -2212,12 +3023,16 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                }
 
                case OVS_ACTION_ATTR_POP_VLAN:
+                       if (mac_proto != MAC_PROTO_ETHERNET)
+                               return -EINVAL;
                        vlan_tci = htons(0);
                        break;
 
                case OVS_ACTION_ATTR_PUSH_VLAN:
+                       if (mac_proto != MAC_PROTO_ETHERNET)
+                               return -EINVAL;
                        vlan = nla_data(a);
-                       if (vlan->vlan_tpid != htons(ETH_P_8021Q))
+                       if (!eth_type_vlan(vlan->vlan_tpid))
                                return -EINVAL;
                        if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
                                return -EINVAL;
@@ -2265,25 +3080,31 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
 
                case OVS_ACTION_ATTR_SET:
                        err = validate_set(a, key, sfa,
-                                          &skip_copy, eth_type, false, log);
+                                          &skip_copy, mac_proto, eth_type,
+                                          false, log);
                        if (err)
                                return err;
                        break;
 
                case OVS_ACTION_ATTR_SET_MASKED:
                        err = validate_set(a, key, sfa,
-                                          &skip_copy, eth_type, true, log);
+                                          &skip_copy, mac_proto, eth_type,
+                                          true, log);
                        if (err)
                                return err;
                        break;
 
-               case OVS_ACTION_ATTR_SAMPLE:
-                       err = validate_and_copy_sample(net, a, key, depth, sfa,
-                                                      eth_type, vlan_tci, log);
+               case OVS_ACTION_ATTR_SAMPLE: {
+                       bool last = nla_is_last(a, rem);
+
+                       err = validate_and_copy_sample(net, a, key, sfa,
+                                                      eth_type, vlan_tci,
+                                                      log, last);
                        if (err)
                                return err;
                        skip_copy = true;
                        break;
+               }
 
                case OVS_ACTION_ATTR_CT:
                        err = ovs_ct_copy_action(net, a, key, sfa, log);
@@ -2292,6 +3113,82 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                        skip_copy = true;
                        break;
 
+               case OVS_ACTION_ATTR_CT_CLEAR:
+                       break;
+
+               case OVS_ACTION_ATTR_PUSH_ETH:
+                       /* Disallow pushing an Ethernet header if one
+                        * is already present */
+                       if (mac_proto != MAC_PROTO_NONE)
+                               return -EINVAL;
+                       mac_proto = MAC_PROTO_ETHERNET;
+                       break;
+
+               case OVS_ACTION_ATTR_POP_ETH:
+                       if (mac_proto != MAC_PROTO_ETHERNET)
+                               return -EINVAL;
+                       if (vlan_tci & htons(VLAN_TAG_PRESENT))
+                               return -EINVAL;
+                       mac_proto = MAC_PROTO_NONE;
+                       break;
+
+               case OVS_ACTION_ATTR_PUSH_NSH:
+                       if (mac_proto != MAC_PROTO_ETHERNET) {
+                               u8 next_proto;
+
+                               next_proto = tun_p_from_eth_p(eth_type);
+                               if (!next_proto)
+                                       return -EINVAL;
+                       }
+                       mac_proto = MAC_PROTO_NONE;
+                       if (!validate_nsh(nla_data(a), false, true, true))
+                               return -EINVAL;
+                       break;
+
+               case OVS_ACTION_ATTR_POP_NSH: {
+                       __be16 inner_proto;
+
+                       if (eth_type != htons(ETH_P_NSH))
+                               return -EINVAL;
+                       inner_proto = tun_p_to_eth_p(key->nsh.base.np);
+                       if (!inner_proto)
+                               return -EINVAL;
+                       if (key->nsh.base.np == TUN_P_ETHERNET)
+                               mac_proto = MAC_PROTO_ETHERNET;
+                       else
+                               mac_proto = MAC_PROTO_NONE;
+                       break;
+               }
+
+               case OVS_ACTION_ATTR_METER:
+                       /* Non-existent meters are simply ignored.  */
+                       break;
+
+               case OVS_ACTION_ATTR_CLONE: {
+                       bool last = nla_is_last(a, rem);
+
+                       err = validate_and_copy_clone(net, a, key, sfa,
+                                                     eth_type, vlan_tci,
+                                                     log, last);
+                       if (err)
+                               return err;
+                       skip_copy = true;
+                       break;
+               }
+
+               case OVS_ACTION_ATTR_CHECK_PKT_LEN: {
+                        bool last = nla_is_last(a, rem);
+
+                        err = validate_and_copy_check_pkt_len(net, a, key, sfa,
+                                                              eth_type,
+                                                              vlan_tci, log,
+                                                              last);
+                        if (err)
+                                return err;
+                        skip_copy = true;
+                        break;
+                }
+
                default:
                        OVS_NLERR(log, "Unknown Action type %d", type);
                        return -EINVAL;
@@ -2316,52 +3213,146 @@ int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
 {
        int err;
 
-       *sfa = nla_alloc_flow_actions(nla_len(attr), log);
+       *sfa = nla_alloc_flow_actions(min(nla_len(attr), MAX_ACTIONS_BUFSIZE));
        if (IS_ERR(*sfa))
                return PTR_ERR(*sfa);
 
        (*sfa)->orig_len = nla_len(attr);
-       err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
-                                    key->eth.tci, log);
+       err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
+                                    key->eth.vlan.tci, log);
        if (err)
                ovs_nla_free_flow_actions(*sfa);
 
        return err;
 }
 
-static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
+static int sample_action_to_attr(const struct nlattr *attr,
+                                struct sk_buff *skb)
 {
-       const struct nlattr *a;
-       struct nlattr *start;
-       int err = 0, rem;
+       struct nlattr *start, *ac_start = NULL, *sample_arg;
+       int err = 0, rem = nla_len(attr);
+       const struct sample_arg *arg;
+       struct nlattr *actions;
 
        start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
        if (!start)
                return -EMSGSIZE;
 
-       nla_for_each_nested(a, attr, rem) {
-               int type = nla_type(a);
-               struct nlattr *st_sample;
+       sample_arg = nla_data(attr);
+       arg = nla_data(sample_arg);
+       actions = nla_next(sample_arg, &rem);
 
-               switch (type) {
-               case OVS_SAMPLE_ATTR_PROBABILITY:
-                       if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
-                                   sizeof(u32), nla_data(a)))
-                               return -EMSGSIZE;
-                       break;
-               case OVS_SAMPLE_ATTR_ACTIONS:
-                       st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
-                       if (!st_sample)
-                               return -EMSGSIZE;
-                       err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
-                       if (err)
-                               return err;
-                       nla_nest_end(skb, st_sample);
-                       break;
-               }
+       if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
+               err = -EMSGSIZE;
+               goto out;
+       }
+
+       ac_start = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
+       if (!ac_start) {
+               err = -EMSGSIZE;
+               goto out;
+       }
+
+       err = ovs_nla_put_actions(actions, rem, skb);
+
+out:
+       if (err) {
+               nla_nest_cancel(skb, ac_start);
+               nla_nest_cancel(skb, start);
+       } else {
+               nla_nest_end(skb, ac_start);
+               nla_nest_end(skb, start);
+       }
+
+       return err;
+}
+
+static int clone_action_to_attr(const struct nlattr *attr,
+                               struct sk_buff *skb)
+{
+       struct nlattr *start;
+       int err = 0, rem = nla_len(attr);
+
+       start = nla_nest_start(skb, OVS_ACTION_ATTR_CLONE);
+       if (!start)
+               return -EMSGSIZE;
+
+       err = ovs_nla_put_actions(nla_data(attr), rem, skb);
+
+       if (err)
+               nla_nest_cancel(skb, start);
+       else
+               nla_nest_end(skb, start);
+
+       return err;
+}
+
+static int check_pkt_len_action_to_attr(const struct nlattr *attr,
+                                       struct sk_buff *skb)
+{
+       struct nlattr *start, *ac_start = NULL;
+       const struct check_pkt_len_arg *arg;
+       const struct nlattr *a, *cpl_arg;
+       int err = 0, rem = nla_len(attr);
+
+       start = nla_nest_start(skb, OVS_ACTION_ATTR_CHECK_PKT_LEN);
+       if (!start)
+               return -EMSGSIZE;
+
+       /* The first nested attribute in 'attr' is always
+        * 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
+        */
+       cpl_arg = nla_data(attr);
+       arg = nla_data(cpl_arg);
+
+       if (nla_put_u16(skb, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN, arg->pkt_len)) {
+               err = -EMSGSIZE;
+               goto out;
+       }
+
+       /* Second nested attribute in 'attr' is always
+        * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
+        */
+       a = nla_next(cpl_arg, &rem);
+       ac_start =  nla_nest_start(skb,
+               OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL);
+       if (!ac_start) {
+               err = -EMSGSIZE;
+               goto out;
+       }
+
+       err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
+       if (err) {
+               nla_nest_cancel(skb, ac_start);
+               goto out;
+       } else {
+               nla_nest_end(skb, ac_start);
+       }
+
+       /* Third nested attribute in 'attr' is always
+        * OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER.
+        */
+       a = nla_next(a, &rem);
+       ac_start =  nla_nest_start(skb,
+                                  OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER);
+       if (!ac_start) {
+               err = -EMSGSIZE;
+               goto out;
+       }
+
+       err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
+       if (err) {
+               nla_nest_cancel(skb, ac_start);
+               goto out;
+       } else {
+               nla_nest_end(skb, ac_start);
        }
 
        nla_nest_end(skb, start);
+       return 0;
+
+out:
+       nla_nest_cancel(skb, start);
        return err;
 }
 
@@ -2381,10 +3372,10 @@ static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
                if (!start)
                        return -EMSGSIZE;
 
-               err = ipv4_tun_to_nlattr(skb, &tun_info->key,
-                                        tun_info->options_len ?
-                                            ip_tunnel_info_opts(tun_info) : 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);
@@ -2403,14 +3394,20 @@ static int masked_set_action_to_set_action_attr(const struct nlattr *a,
                                                struct sk_buff *skb)
 {
        const struct nlattr *ovs_key = nla_data(a);
+       struct nlattr *nla;
        size_t key_len = nla_len(ovs_key) / 2;
 
        /* Revert the conversion we did from a non-masked set action to
         * masked set action.
         */
-       if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a) - key_len, ovs_key))
+       nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
+       if (!nla)
                return -EMSGSIZE;
 
+       if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
+               return -EMSGSIZE;
+
+       nla_nest_end(skb, nla);
        return 0;
 }
 
@@ -2447,6 +3444,18 @@ int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
                                return err;
                        break;
 
+               case OVS_ACTION_ATTR_CLONE:
+                       err = clone_action_to_attr(a, skb);
+                       if (err)
+                               return err;
+                       break;
+
+               case OVS_ACTION_ATTR_CHECK_PKT_LEN:
+                        err = check_pkt_len_action_to_attr(a, skb);
+                        if (err)
+                                return err;
+                        break;
+
                default:
                        if (nla_put(skb, type, nla_len(a), nla_data(a)))
                                return -EMSGSIZE;