datapath: Use netlink ipv4 API to handle the ipv4 addr attributes.
[cascardo/ovs.git] / datapath / flow_netlink.c
index 34edcfe..6280246 100644 (file)
@@ -340,6 +340,7 @@ static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
                                     .next = ovs_tunnel_key_lens, },
        [OVS_KEY_ATTR_MPLS]      = { .len = sizeof(struct ovs_key_mpls) },
 };
+
 static bool is_all_zero(const u8 *fp, size_t size)
 {
        int i;
@@ -534,11 +535,11 @@ static int ipv4_tun_from_nlattr(const struct nlattr *attr,
                        break;
                case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
                        SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
-                                       nla_get_be32(a), is_mask);
+                                       nla_get_in_addr(a), is_mask);
                        break;
                case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
                        SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
-                                       nla_get_be32(a), is_mask);
+                                       nla_get_in_addr(a), is_mask);
                        break;
                case OVS_TUNNEL_KEY_ATTR_TOS:
                        SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
@@ -647,10 +648,10 @@ static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
            nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
                return -EMSGSIZE;
        if (output->ipv4_src &&
-           nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
+           nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
                return -EMSGSIZE;
        if (output->ipv4_dst &&
-           nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
+           nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
                return -EMSGSIZE;
        if (output->ipv4_tos &&
            nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
@@ -813,7 +814,7 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
                if (is_mask) {
                        /* Always exact match EtherType. */
                        eth_type = htons(0xffff);
-               } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
+               } else if (!eth_proto_is_802_3(eth_type)) {
                        OVS_NLERR(log, "EtherType %x is less than min %x",
                                  ntohs(eth_type), ETH_P_802_3_MIN);
                        return -EINVAL;
@@ -858,12 +859,14 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
                                  ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
                        return -EINVAL;
                }
+
                if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
                        OVS_NLERR(log,
                                  "Invalid IPv6 flow label value (value=%x, max=%x).",
                                  ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
                        return -EINVAL;
                }
+
                SW_FLOW_KEY_PUT(match, ipv6.label,
                                ipv6_key->ipv6_label, is_mask);
                SW_FLOW_KEY_PUT(match, ip.proto,
@@ -1178,6 +1181,59 @@ free_newmask:
        return err;
 }
 
+static size_t get_ufid_len(const struct nlattr *attr, bool log)
+{
+       size_t len;
+
+       if (!attr)
+               return 0;
+
+       len = nla_len(attr);
+       if (len < 1 || len > MAX_UFID_LENGTH) {
+               OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
+                         nla_len(attr), MAX_UFID_LENGTH);
+               return 0;
+       }
+
+       return len;
+}
+
+/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
+ * or false otherwise.
+ */
+bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
+                     bool log)
+{
+       sfid->ufid_len = get_ufid_len(attr, log);
+       if (sfid->ufid_len)
+               memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
+
+       return sfid->ufid_len;
+}
+
+int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
+                          const struct sw_flow_key *key, bool log)
+{
+       struct sw_flow_key *new_key;
+
+       if (ovs_nla_get_ufid(sfid, ufid, log))
+               return 0;
+
+       /* If UFID was not provided, use unmasked key. */
+       new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
+       if (!new_key)
+               return -ENOMEM;
+       memcpy(new_key, key, sizeof(*key));
+       sfid->unmasked_key = new_key;
+
+       return 0;
+}
+
+u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
+{
+       return attr ? nla_get_u32(attr) : 0;
+}
+
 /**
  * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
  * @key: Receives extracted in_port, priority, tun_key and skb_mark.
@@ -1215,12 +1271,12 @@ int ovs_nla_get_flow_metadata(const struct nlattr *attr,
        return metadata_from_nlattrs(&match, &attrs, a, false, log);
 }
 
-int ovs_nla_put_flow(const struct sw_flow_key *swkey,
-                    const struct sw_flow_key *output, struct sk_buff *skb)
+static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
+                            const struct sw_flow_key *output, bool is_mask,
+                            struct sk_buff *skb)
 {
        struct ovs_key_ethernet *eth_key;
        struct nlattr *nla, *encap;
-       bool is_mask = (swkey != output);
 
        if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
                goto nla_put_failure;
@@ -1430,6 +1486,49 @@ nla_put_failure:
        return -EMSGSIZE;
 }
 
+int ovs_nla_put_key(const struct sw_flow_key *swkey,
+                   const struct sw_flow_key *output, int attr, bool is_mask,
+                   struct sk_buff *skb)
+{
+       int err;
+       struct nlattr *nla;
+
+       nla = nla_nest_start(skb, attr);
+       if (!nla)
+               return -EMSGSIZE;
+       err = __ovs_nla_put_key(swkey, output, is_mask, skb);
+       if (err)
+               return err;
+       nla_nest_end(skb, nla);
+
+       return 0;
+}
+
+/* Called with ovs_mutex or RCU read lock. */
+int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
+{
+       if (ovs_identifier_is_ufid(&flow->id))
+               return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
+                              flow->id.ufid);
+
+       return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
+                              OVS_FLOW_ATTR_KEY, false, skb);
+}
+
+/* Called with ovs_mutex or RCU read lock. */
+int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
+{
+       return ovs_nla_put_key(&flow->key, &flow->key,
+                               OVS_FLOW_ATTR_KEY, false, skb);
+}
+
+/* Called with ovs_mutex or RCU read lock. */
+int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
+{
+       return ovs_nla_put_key(&flow->key, &flow->mask->key,
+                               OVS_FLOW_ATTR_MASK, true, skb);
+}
+
 #define MAX_ACTIONS_BUFSIZE    (32 * 1024)
 
 static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
@@ -1609,16 +1708,6 @@ static int validate_and_copy_sample(const struct nlattr *attr,
        return 0;
 }
 
-static int validate_tp_port(const struct sw_flow_key *flow_key,
-                           __be16 eth_type)
-{
-       if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
-           (flow_key->tp.src || flow_key->tp.dst))
-               return 0;
-
-       return -EINVAL;
-}
-
 void ovs_match_init(struct sw_flow_match *match,
                    struct sw_flow_key *key,
                    struct sw_flow_mask *mask)
@@ -1670,7 +1759,8 @@ static int validate_and_copy_set_tun(const struct nlattr *attr,
        struct sw_flow_key key;
        struct ovs_tunnel_info *tun_info;
        struct nlattr *a;
-       int err, start, opts_type;
+       int start, opts_type;
+       int err = 0;
 
        ovs_match_init(&match, &key, NULL);
        opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
@@ -1710,7 +1800,6 @@ static int validate_and_copy_set_tun(const struct nlattr *attr,
                memcpy((tun_info + 1),
                       TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
                tun_info->options = (tun_info + 1);
-
        } else {
                tun_info->options = NULL;
        }
@@ -1720,23 +1809,45 @@ static int validate_and_copy_set_tun(const struct nlattr *attr,
        return err;
 }
 
+/* Return false if there are any non-masked bits set.
+ * Mask follows data immediately, before any netlink padding.
+ */
+static bool validate_masked(u8 *data, int len)
+{
+       u8 *mask = data + len;
+
+       while (len--)
+               if (*data++ & ~*mask++)
+                       return false;
+
+       return true;
+}
+
 static int validate_set(const struct nlattr *a,
                        const struct sw_flow_key *flow_key,
                        struct sw_flow_actions **sfa,
-                       bool *set_tun, __be16 eth_type, bool log)
+                       bool *skip_copy, __be16 eth_type, bool masked, bool log)
 {
        const struct nlattr *ovs_key = nla_data(a);
        int key_type = nla_type(ovs_key);
+       size_t key_len;
 
        /* There can be only one key in a action */
        if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
                return -EINVAL;
 
+       key_len = nla_len(ovs_key);
+       if (masked)
+               key_len /= 2;
+
        if (key_type > OVS_KEY_ATTR_MAX ||
-           (ovs_key_lens[key_type].len != nla_len(ovs_key) &&
+           (ovs_key_lens[key_type].len != key_len &&
             ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
                return -EINVAL;
 
+       if (masked && !validate_masked(nla_data(ovs_key), key_len))
+               return -EINVAL;
+
        switch (key_type) {
        const struct ovs_key_ipv4 *ipv4_key;
        const struct ovs_key_ipv6 *ipv6_key;
@@ -1751,7 +1862,10 @@ static int validate_set(const struct nlattr *a,
                if (eth_p_mpls(eth_type))
                        return -EINVAL;
 
-               *set_tun = true;
+               if (masked)
+                       return -EINVAL; /* Masked tunnel set not supported. */
+
+               *skip_copy = true;
                err = validate_and_copy_set_tun(a, sfa, log);
                if (err)
                        return err;
@@ -1761,48 +1875,66 @@ static int validate_set(const struct nlattr *a,
                if (eth_type != htons(ETH_P_IP))
                        return -EINVAL;
 
-               if (!flow_key->ip.proto)
-                       return -EINVAL;
-
                ipv4_key = nla_data(ovs_key);
-               if (ipv4_key->ipv4_proto != flow_key->ip.proto)
-                       return -EINVAL;
 
-               if (ipv4_key->ipv4_frag != flow_key->ip.frag)
-                       return -EINVAL;
+               if (masked) {
+                       const struct ovs_key_ipv4 *mask = ipv4_key + 1;
+
+                       /* Non-writeable fields. */
+                       if (mask->ipv4_proto || mask->ipv4_frag)
+                               return -EINVAL;
+               } else {
+                       if (ipv4_key->ipv4_proto != flow_key->ip.proto)
+                               return -EINVAL;
 
+                       if (ipv4_key->ipv4_frag != flow_key->ip.frag)
+                               return -EINVAL;
+               }
                break;
 
        case OVS_KEY_ATTR_IPV6:
                if (eth_type != htons(ETH_P_IPV6))
                        return -EINVAL;
 
-               if (!flow_key->ip.proto)
-                       return -EINVAL;
-
                ipv6_key = nla_data(ovs_key);
-               if (ipv6_key->ipv6_proto != flow_key->ip.proto)
-                       return -EINVAL;
 
-               if (ipv6_key->ipv6_frag != flow_key->ip.frag)
-                       return -EINVAL;
+               if (masked) {
+                       const struct ovs_key_ipv6 *mask = ipv6_key + 1;
+
+                       /* Non-writeable fields. */
+                       if (mask->ipv6_proto || mask->ipv6_frag)
+                               return -EINVAL;
 
+                       /* Invalid bits in the flow label mask? */
+                       if (ntohl(mask->ipv6_label) & 0xFFF00000)
+                               return -EINVAL;
+               } else {
+                       if (ipv6_key->ipv6_proto != flow_key->ip.proto)
+                               return -EINVAL;
+
+                       if (ipv6_key->ipv6_frag != flow_key->ip.frag)
+                               return -EINVAL;
+               }
                if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
                        return -EINVAL;
 
                break;
 
        case OVS_KEY_ATTR_TCP:
-               if (flow_key->ip.proto != IPPROTO_TCP)
+               if ((eth_type != htons(ETH_P_IP) &&
+                    eth_type != htons(ETH_P_IPV6)) ||
+                   flow_key->ip.proto != IPPROTO_TCP)
                        return -EINVAL;
 
-               return validate_tp_port(flow_key, eth_type);
+               break;
 
        case OVS_KEY_ATTR_UDP:
-               if (flow_key->ip.proto != IPPROTO_UDP)
+               if ((eth_type != htons(ETH_P_IP) &&
+                    eth_type != htons(ETH_P_IPV6)) ||
+                   flow_key->ip.proto != IPPROTO_UDP)
                        return -EINVAL;
 
-               return validate_tp_port(flow_key, eth_type);
+               break;
 
        case OVS_KEY_ATTR_MPLS:
                if (!eth_p_mpls(eth_type))
@@ -1810,15 +1942,45 @@ static int validate_set(const struct nlattr *a,
                break;
 
        case OVS_KEY_ATTR_SCTP:
-               if (flow_key->ip.proto != IPPROTO_SCTP)
+               if ((eth_type != htons(ETH_P_IP) &&
+                    eth_type != htons(ETH_P_IPV6)) ||
+                   flow_key->ip.proto != IPPROTO_SCTP)
                        return -EINVAL;
 
-               return validate_tp_port(flow_key, eth_type);
+               break;
 
        default:
                return -EINVAL;
        }
 
+       /* Convert non-masked non-tunnel set actions to masked set actions. */
+       if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
+               int start, len = key_len * 2;
+               struct nlattr *at;
+
+               *skip_copy = true;
+
+               start = add_nested_action_start(sfa,
+                                               OVS_ACTION_ATTR_SET_TO_MASKED,
+                                               log);
+               if (start < 0)
+                       return start;
+
+               at = __add_action(sfa, key_type, NULL, len, log);
+               if (IS_ERR(at))
+                       return PTR_ERR(at);
+
+               memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
+               memset(nla_data(at) + key_len, 0xff, key_len);    /* Mask. */
+               /* Clear non-writeable bits from otherwise writeable fields. */
+               if (key_type == OVS_KEY_ATTR_IPV6) {
+                       struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
+
+                       mask->ipv6_label &= htonl(0x000FFFFF);
+               }
+               add_nested_action_end(*sfa, start);
+       }
+
        return 0;
 }
 
@@ -1880,6 +2042,7 @@ static int __ovs_nla_copy_actions(const struct nlattr *attr,
                        [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
                        [OVS_ACTION_ATTR_POP_VLAN] = 0,
                        [OVS_ACTION_ATTR_SET] = (u32)-1,
+                       [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
                        [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
                        [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
                };
@@ -1906,7 +2069,6 @@ static int __ovs_nla_copy_actions(const struct nlattr *attr,
                case OVS_ACTION_ATTR_OUTPUT:
                        if (nla_get_u32(a) >= DP_MAX_PORTS)
                                return -EINVAL;
-
                        break;
 
                case OVS_ACTION_ATTR_HASH: {
@@ -1943,7 +2105,6 @@ static int __ovs_nla_copy_actions(const struct nlattr *attr,
 
                        if (!eth_p_mpls(mpls->mpls_ethertype))
                                return -EINVAL;
-
                        /* Prohibit push MPLS other than to a white list
                         * for packets that have a known tag order.
                         */
@@ -1977,7 +2138,14 @@ static int __ovs_nla_copy_actions(const struct nlattr *attr,
 
                case OVS_ACTION_ATTR_SET:
                        err = validate_set(a, key, sfa,
-                                          &skip_copy, eth_type, log);
+                                          &skip_copy, 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);
                        if (err)
                                return err;
                        break;
@@ -2007,6 +2175,7 @@ static int __ovs_nla_copy_actions(const struct nlattr *attr,
        return 0;
 }
 
+/* 'key' must be the masked key. */
 int ovs_nla_copy_actions(const struct nlattr *attr,
                         const struct sw_flow_key *key,
                         struct sw_flow_actions **sfa, bool log)
@@ -2094,6 +2263,21 @@ static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
        return 0;
 }
 
+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);
+       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))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
 {
        const struct nlattr *a;
@@ -2109,6 +2293,12 @@ int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
                                return err;
                        break;
 
+               case OVS_ACTION_ATTR_SET_TO_MASKED:
+                       err = masked_set_action_to_set_action_attr(a, skb);
+                       if (err)
+                               return err;
+                       break;
+
                case OVS_ACTION_ATTR_SAMPLE:
                        err = sample_action_to_attr(a, skb);
                        if (err)