Skip to content

Commit d6fe532

Browse files
borkmannAlexei Starovoitov
authored and
Alexei Starovoitov
committed
netkit: Fix setting mac address in l2 mode
When running Cilium connectivity test suite with netkit in L2 mode, we found that it is expected to be able to specify a custom MAC address for the devices, in particular, cilium-cni obtains the specified MAC address by querying the endpoint and sets the MAC address of the interface inside the Pod. Thus, fix the missing support in netkit for L2 mode. Fixes: 35dfaad ("netkit, bpf: Add bpf programmable net device") Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Nikolay Aleksandrov <[email protected]> Acked-by: Stanislav Fomichev <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent dd6a403 commit d6fe532

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

drivers/net/netkit.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ static void netkit_set_multicast(struct net_device *dev)
155155
/* Nothing to do, we receive whatever gets pushed to us! */
156156
}
157157

158+
static int netkit_set_macaddr(struct net_device *dev, void *sa)
159+
{
160+
struct netkit *nk = netkit_priv(dev);
161+
162+
if (nk->mode != NETKIT_L2)
163+
return -EOPNOTSUPP;
164+
165+
return eth_mac_addr(dev, sa);
166+
}
167+
158168
static void netkit_set_headroom(struct net_device *dev, int headroom)
159169
{
160170
struct netkit *nk = netkit_priv(dev), *nk2;
@@ -198,6 +208,7 @@ static const struct net_device_ops netkit_netdev_ops = {
198208
.ndo_start_xmit = netkit_xmit,
199209
.ndo_set_rx_mode = netkit_set_multicast,
200210
.ndo_set_rx_headroom = netkit_set_headroom,
211+
.ndo_set_mac_address = netkit_set_macaddr,
201212
.ndo_get_iflink = netkit_get_iflink,
202213
.ndo_get_peer_dev = netkit_peer_dev,
203214
.ndo_get_stats64 = netkit_get_stats,
@@ -300,9 +311,11 @@ static int netkit_validate(struct nlattr *tb[], struct nlattr *data[],
300311

301312
if (!attr)
302313
return 0;
303-
NL_SET_ERR_MSG_ATTR(extack, attr,
304-
"Setting Ethernet address is not supported");
305-
return -EOPNOTSUPP;
314+
if (nla_len(attr) != ETH_ALEN)
315+
return -EINVAL;
316+
if (!is_valid_ether_addr(nla_data(attr)))
317+
return -EADDRNOTAVAIL;
318+
return 0;
306319
}
307320

308321
static struct rtnl_link_ops netkit_link_ops;
@@ -365,6 +378,9 @@ static int netkit_new_link(struct net *src_net, struct net_device *dev,
365378
strscpy(ifname, "nk%d", IFNAMSIZ);
366379
ifname_assign_type = NET_NAME_ENUM;
367380
}
381+
if (mode != NETKIT_L2 &&
382+
(tb[IFLA_ADDRESS] || tbp[IFLA_ADDRESS]))
383+
return -EOPNOTSUPP;
368384

369385
net = rtnl_link_get_net(src_net, tbp);
370386
if (IS_ERR(net))
@@ -379,7 +395,7 @@ static int netkit_new_link(struct net *src_net, struct net_device *dev,
379395

380396
netif_inherit_tso_max(peer, dev);
381397

382-
if (mode == NETKIT_L2)
398+
if (mode == NETKIT_L2 && !(ifmp && tbp[IFLA_ADDRESS]))
383399
eth_hw_addr_random(peer);
384400
if (ifmp && dev->ifindex)
385401
peer->ifindex = ifmp->ifi_index;
@@ -402,7 +418,7 @@ static int netkit_new_link(struct net *src_net, struct net_device *dev,
402418
if (err < 0)
403419
goto err_configure_peer;
404420

405-
if (mode == NETKIT_L2)
421+
if (mode == NETKIT_L2 && !tb[IFLA_ADDRESS])
406422
eth_hw_addr_random(dev);
407423
if (tb[IFLA_IFNAME])
408424
nla_strscpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);

0 commit comments

Comments
 (0)