Skip to content

Commit 7fb40f8

Browse files
hadarhenziondavem330
authored andcommitted
net/mlx4_core: Add security check / enforcement for flow steering rules set for VMs
Since VFs may be mapped to VMs which aren't trusted entities, flow steering rules attached through the wrapper on behalf of VFs must be checked to make sure that their L2 specification relate to MAC address assigned to that VF, and add L2 specification if its missing. Signed-off-by: Hadar Hen Zion <hadarh@mellanox.com> Signed-off-by: Or Gerlitz <ogerlitz@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a8edc3b commit 7fb40f8

2 files changed

Lines changed: 127 additions & 0 deletions

File tree

‎drivers/net/ethernet/mellanox/mlx4/resource_tracker.c‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <linux/mlx4/cmd.h>
4343
#include <linux/mlx4/qp.h>
4444
#include <linux/if_ether.h>
45+
#include <linux/etherdevice.h>
4546

4647
#include "mlx4.h"
4748
#include "fw.h"
@@ -2776,18 +2777,133 @@ int mlx4_QP_ATTACH_wrapper(struct mlx4_dev *dev, int slave,
27762777
return err;
27772778
}
27782779

2780+
/*
2781+
* MAC validation for Flow Steering rules.
2782+
* VF can attach rules only with a mac address which is assigned to it.
2783+
*/
2784+
static int validate_eth_header_mac(int slave, struct _rule_hw *eth_header,
2785+
struct list_head *rlist)
2786+
{
2787+
struct mac_res *res, *tmp;
2788+
__be64 be_mac;
2789+
2790+
/* make sure it isn't multicast or broadcast mac*/
2791+
if (!is_multicast_ether_addr(eth_header->eth.dst_mac) &&
2792+
!is_broadcast_ether_addr(eth_header->eth.dst_mac)) {
2793+
list_for_each_entry_safe(res, tmp, rlist, list) {
2794+
be_mac = cpu_to_be64(res->mac << 16);
2795+
if (!memcmp(&be_mac, eth_header->eth.dst_mac, ETH_ALEN))
2796+
return 0;
2797+
}
2798+
pr_err("MAC %pM doesn't belong to VF %d, Steering rule rejected\n",
2799+
eth_header->eth.dst_mac, slave);
2800+
return -EINVAL;
2801+
}
2802+
return 0;
2803+
}
2804+
2805+
/*
2806+
* In case of missing eth header, append eth header with a MAC address
2807+
* assigned to the VF.
2808+
*/
2809+
static int add_eth_header(struct mlx4_dev *dev, int slave,
2810+
struct mlx4_cmd_mailbox *inbox,
2811+
struct list_head *rlist, int header_id)
2812+
{
2813+
struct mac_res *res, *tmp;
2814+
u8 port;
2815+
struct mlx4_net_trans_rule_hw_ctrl *ctrl;
2816+
struct mlx4_net_trans_rule_hw_eth *eth_header;
2817+
struct mlx4_net_trans_rule_hw_ipv4 *ip_header;
2818+
struct mlx4_net_trans_rule_hw_tcp_udp *l4_header;
2819+
__be64 be_mac = 0;
2820+
__be64 mac_msk = cpu_to_be64(MLX4_MAC_MASK << 16);
2821+
2822+
ctrl = (struct mlx4_net_trans_rule_hw_ctrl *)inbox->buf;
2823+
port = be32_to_cpu(ctrl->vf_vep_port) & 0xff;
2824+
eth_header = (struct mlx4_net_trans_rule_hw_eth *)(ctrl + 1);
2825+
2826+
/* Clear a space in the inbox for eth header */
2827+
switch (header_id) {
2828+
case MLX4_NET_TRANS_RULE_ID_IPV4:
2829+
ip_header =
2830+
(struct mlx4_net_trans_rule_hw_ipv4 *)(eth_header + 1);
2831+
memmove(ip_header, eth_header,
2832+
sizeof(*ip_header) + sizeof(*l4_header));
2833+
break;
2834+
case MLX4_NET_TRANS_RULE_ID_TCP:
2835+
case MLX4_NET_TRANS_RULE_ID_UDP:
2836+
l4_header = (struct mlx4_net_trans_rule_hw_tcp_udp *)
2837+
(eth_header + 1);
2838+
memmove(l4_header, eth_header, sizeof(*l4_header));
2839+
break;
2840+
default:
2841+
return -EINVAL;
2842+
}
2843+
list_for_each_entry_safe(res, tmp, rlist, list) {
2844+
if (port == res->port) {
2845+
be_mac = cpu_to_be64(res->mac << 16);
2846+
break;
2847+
}
2848+
}
2849+
if (!be_mac) {
2850+
pr_err("Failed adding eth header to FS rule, Can't find matching MAC for port %d .\n",
2851+
port);
2852+
return -EINVAL;
2853+
}
2854+
2855+
memset(eth_header, 0, sizeof(*eth_header));
2856+
eth_header->size = sizeof(*eth_header) >> 2;
2857+
eth_header->id = cpu_to_be16(__sw_id_hw[MLX4_NET_TRANS_RULE_ID_ETH]);
2858+
memcpy(eth_header->dst_mac, &be_mac, ETH_ALEN);
2859+
memcpy(eth_header->dst_mac_msk, &mac_msk, ETH_ALEN);
2860+
2861+
return 0;
2862+
2863+
}
2864+
27792865
int mlx4_QP_FLOW_STEERING_ATTACH_wrapper(struct mlx4_dev *dev, int slave,
27802866
struct mlx4_vhcr *vhcr,
27812867
struct mlx4_cmd_mailbox *inbox,
27822868
struct mlx4_cmd_mailbox *outbox,
27832869
struct mlx4_cmd_info *cmd)
27842870
{
2871+
2872+
struct mlx4_priv *priv = mlx4_priv(dev);
2873+
struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
2874+
struct list_head *rlist = &tracker->slave_list[slave].res_list[RES_MAC];
27852875
int err;
2876+
struct mlx4_net_trans_rule_hw_ctrl *ctrl;
2877+
struct _rule_hw *rule_header;
2878+
int header_id;
27862879

27872880
if (dev->caps.steering_mode !=
27882881
MLX4_STEERING_MODE_DEVICE_MANAGED)
27892882
return -EOPNOTSUPP;
27902883

2884+
ctrl = (struct mlx4_net_trans_rule_hw_ctrl *)inbox->buf;
2885+
rule_header = (struct _rule_hw *)(ctrl + 1);
2886+
header_id = map_hw_to_sw_id(be16_to_cpu(rule_header->id));
2887+
2888+
switch (header_id) {
2889+
case MLX4_NET_TRANS_RULE_ID_ETH:
2890+
if (validate_eth_header_mac(slave, rule_header, rlist))
2891+
return -EINVAL;
2892+
break;
2893+
case MLX4_NET_TRANS_RULE_ID_IPV4:
2894+
case MLX4_NET_TRANS_RULE_ID_TCP:
2895+
case MLX4_NET_TRANS_RULE_ID_UDP:
2896+
pr_warn("Can't attach FS rule without L2 headers, adding L2 header.\n");
2897+
if (add_eth_header(dev, slave, inbox, rlist, header_id))
2898+
return -EINVAL;
2899+
vhcr->in_modifier +=
2900+
sizeof(struct mlx4_net_trans_rule_hw_eth) >> 2;
2901+
break;
2902+
default:
2903+
pr_err("Corrupted mailbox.\n");
2904+
return -EINVAL;
2905+
}
2906+
27912907
err = mlx4_cmd_imm(dev, inbox->dma, &vhcr->out_param,
27922908
vhcr->in_modifier, 0,
27932909
MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,

‎include/linux/mlx4/device.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,17 @@ enum mlx4_net_trans_rule_id {
798798

799799
extern const u16 __sw_id_hw[];
800800

801+
static inline int map_hw_to_sw_id(u16 header_id)
802+
{
803+
804+
int i;
805+
for (i = 0; i < MLX4_NET_TRANS_RULE_NUM; i++) {
806+
if (header_id == __sw_id_hw[i])
807+
return i;
808+
}
809+
return -EINVAL;
810+
}
811+
801812
enum mlx4_net_trans_promisc_mode {
802813
MLX4_FS_PROMISC_NONE = 0,
803814
MLX4_FS_PROMISC_UPLINK,

0 commit comments

Comments
 (0)