net/mlx5e: Allow policies with reqid 0, to support IKE policy holes
authorRaed Salem <raeds@nvidia.com>
Tue, 14 Mar 2023 08:58:41 +0000 (10:58 +0200)
committerLeon Romanovsky <leon@kernel.org>
Mon, 20 Mar 2023 09:29:41 +0000 (11:29 +0200)
IKE policies hole, is special policy that exists to allow for IKE
traffic to bypass IPsec encryption even though there is already a
policies and SA(s) configured on same endpoints, these policies
does not nessecarly have the reqid configured, so need to add
an exception for such policies. These kind of policies are allowed
under the condition that at least upper protocol and/or ips
are not 0.

Signed-off-by: Raed Salem <raeds@nvidia.com>
Link: https://lore.kernel.org/r/cbcadde312c24de74c47d9b0616f86a5818cc9bf.1678714336.git.leon@kernel.org
Signed-off-by: Leon Romanovsky <leon@kernel.org>
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_fs.c

index 83012be..9cc59dc 100644 (file)
@@ -503,6 +503,8 @@ static int mlx5e_xfrm_validate_policy(struct mlx5_core_dev *mdev,
                                      struct xfrm_policy *x,
                                      struct netlink_ext_ack *extack)
 {
+       struct xfrm_selector *sel = &x->selector;
+
        if (x->type != XFRM_POLICY_TYPE_MAIN) {
                NL_SET_ERR_MSG_MOD(extack, "Cannot offload non-main policy types");
                return -EINVAL;
@@ -520,8 +522,9 @@ static int mlx5e_xfrm_validate_policy(struct mlx5_core_dev *mdev,
                return -EINVAL;
        }
 
-       if (!x->xfrm_vec[0].reqid) {
-               NL_SET_ERR_MSG_MOD(extack, "Cannot offload policy without reqid");
+       if (!x->xfrm_vec[0].reqid && sel->proto == IPPROTO_IP &&
+           addr6_all_zero(sel->saddr.a6) && addr6_all_zero(sel->daddr.a6)) {
+               NL_SET_ERR_MSG_MOD(extack, "Unsupported policy with reqid 0 without at least one of upper protocol or ip addr(s) different than 0");
                return -EINVAL;
        }
 
@@ -530,8 +533,8 @@ static int mlx5e_xfrm_validate_policy(struct mlx5_core_dev *mdev,
                return -EINVAL;
        }
 
-       if (x->selector.proto != IPPROTO_IP &&
-           (x->selector.proto != IPPROTO_UDP || x->xdo.dir != XFRM_DEV_OFFLOAD_OUT)) {
+       if (sel->proto != IPPROTO_IP &&
+           (sel->proto != IPPROTO_UDP || x->xdo.dir != XFRM_DEV_OFFLOAD_OUT)) {
                NL_SET_ERR_MSG_MOD(extack, "Device does not support upper protocol other than UDP, and only Tx direction");
                return -EINVAL;
        }
index b36e047..f3e81c3 100644 (file)
@@ -254,6 +254,13 @@ mlx5e_ipsec_pol2dev(struct mlx5e_ipsec_pol_entry *pol_entry)
 {
        return pol_entry->ipsec->mdev;
 }
+
+static inline bool addr6_all_zero(__be32 *addr6)
+{
+       static const __be32 zaddr6[4] = {};
+
+       return !memcmp(addr6, zaddr6, sizeof(*zaddr6));
+}
 #else
 static inline void mlx5e_ipsec_init(struct mlx5e_priv *priv)
 {
index 0c9640d..9f694a8 100644 (file)
@@ -621,37 +621,53 @@ static void tx_ft_put_policy(struct mlx5e_ipsec *ipsec, u32 prio)
 static void setup_fte_addr4(struct mlx5_flow_spec *spec, __be32 *saddr,
                            __be32 *daddr)
 {
+       if (!*saddr && !*daddr)
+               return;
+
        spec->match_criteria_enable |= MLX5_MATCH_OUTER_HEADERS;
 
        MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, outer_headers.ip_version);
        MLX5_SET(fte_match_param, spec->match_value, outer_headers.ip_version, 4);
 
-       memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
-                           outer_headers.src_ipv4_src_ipv6.ipv4_layout.ipv4), saddr, 4);
-       memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
-                           outer_headers.dst_ipv4_dst_ipv6.ipv4_layout.ipv4), daddr, 4);
-       MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
-                        outer_headers.src_ipv4_src_ipv6.ipv4_layout.ipv4);
-       MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
-                        outer_headers.dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
+       if (*saddr) {
+               memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
+                                   outer_headers.src_ipv4_src_ipv6.ipv4_layout.ipv4), saddr, 4);
+               MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
+                                outer_headers.src_ipv4_src_ipv6.ipv4_layout.ipv4);
+       }
+
+       if (*daddr) {
+               memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
+                                   outer_headers.dst_ipv4_dst_ipv6.ipv4_layout.ipv4), daddr, 4);
+               MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
+                                outer_headers.dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
+       }
 }
 
 static void setup_fte_addr6(struct mlx5_flow_spec *spec, __be32 *saddr,
                            __be32 *daddr)
 {
+       if (addr6_all_zero(saddr) && addr6_all_zero(daddr))
+               return;
+
        spec->match_criteria_enable |= MLX5_MATCH_OUTER_HEADERS;
 
        MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, outer_headers.ip_version);
        MLX5_SET(fte_match_param, spec->match_value, outer_headers.ip_version, 6);
 
-       memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
-                           outer_headers.src_ipv4_src_ipv6.ipv6_layout.ipv6), saddr, 16);
-       memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
-                           outer_headers.dst_ipv4_dst_ipv6.ipv6_layout.ipv6), daddr, 16);
-       memset(MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
-                           outer_headers.src_ipv4_src_ipv6.ipv6_layout.ipv6), 0xff, 16);
-       memset(MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
-                           outer_headers.dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 0xff, 16);
+       if (!addr6_all_zero(saddr)) {
+               memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
+                                   outer_headers.src_ipv4_src_ipv6.ipv6_layout.ipv6), saddr, 16);
+               memset(MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
+                                   outer_headers.src_ipv4_src_ipv6.ipv6_layout.ipv6), 0xff, 16);
+       }
+
+       if (!addr6_all_zero(daddr)) {
+               memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
+                                   outer_headers.dst_ipv4_dst_ipv6.ipv6_layout.ipv6), daddr, 16);
+               memset(MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
+                                   outer_headers.dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 0xff, 16);
+       }
 }
 
 static void setup_fte_esp(struct mlx5_flow_spec *spec)
@@ -920,7 +936,8 @@ static int tx_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry)
                setup_fte_reg_a(spec);
                break;
        case XFRM_DEV_OFFLOAD_PACKET:
-               setup_fte_reg_c0(spec, attrs->reqid);
+               if (attrs->reqid)
+                       setup_fte_reg_c0(spec, attrs->reqid);
                err = setup_pkt_reformat(mdev, attrs, &flow_act);
                if (err)
                        goto err_pkt_reformat;
@@ -989,10 +1006,12 @@ static int tx_add_policy(struct mlx5e_ipsec_pol_entry *pol_entry)
        setup_fte_no_frags(spec);
        setup_fte_upper_proto_match(spec, &attrs->upspec);
 
-       err = setup_modify_header(mdev, attrs->reqid, XFRM_DEV_OFFLOAD_OUT,
-                                 &flow_act);
-       if (err)
-               goto err_mod_header;
+       if (attrs->reqid) {
+               err = setup_modify_header(mdev, attrs->reqid,
+                                         XFRM_DEV_OFFLOAD_OUT, &flow_act);
+               if (err)
+                       goto err_mod_header;
+       }
 
        switch (attrs->action) {
        case XFRM_POLICY_ALLOW:
@@ -1028,7 +1047,8 @@ static int tx_add_policy(struct mlx5e_ipsec_pol_entry *pol_entry)
        return 0;
 
 err_action:
-       mlx5_modify_header_dealloc(mdev, flow_act.modify_hdr);
+       if (attrs->reqid)
+               mlx5_modify_header_dealloc(mdev, flow_act.modify_hdr);
 err_mod_header:
        kvfree(spec);
 err_alloc:
@@ -1263,7 +1283,9 @@ void mlx5e_accel_ipsec_fs_del_pol(struct mlx5e_ipsec_pol_entry *pol_entry)
                return;
        }
 
-       mlx5_modify_header_dealloc(mdev, ipsec_rule->modify_hdr);
+       if (ipsec_rule->modify_hdr)
+               mlx5_modify_header_dealloc(mdev, ipsec_rule->modify_hdr);
+
        tx_ft_put_policy(pol_entry->ipsec, pol_entry->attrs.prio);
 }