Skip to content

Commit aaa5ca5

Browse files
committed
network/netdev: make MACAddress= take 'none' to suppress generating persistent hardware address
This is mostly equivalent to .link file's MACAddressPolicy=none.
1 parent 5dcc5b1 commit aaa5ca5

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

man/systemd.netdev.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,23 @@
254254
<varlistentry>
255255
<term><varname>MACAddress=</varname></term>
256256
<listitem>
257-
<para>The MAC address to use for the device. For <literal>tun</literal> or <literal>tap</literal>
258-
devices, setting <varname>MACAddress=</varname> in the [NetDev] section is not
259-
supported. Please specify it in the [Link] section of the corresponding
257+
<para>Specifies the MAC address to use for the device, or takes the special value
258+
<literal>none</literal>. When <literal>none</literal>, <command>systemd-networkd</command>
259+
does not request the MAC address for the device, and the kernel will assign a random MAC
260+
address. For <literal>tun</literal>, <literal>tap</literal>, or <literal>l2tp</literal>
261+
devices, the <varname>MACAddress=</varname> setting in the [NetDev] section is not
262+
supported and will be ignored. Please specify it in the [Link] section of the corresponding
260263
<citerefentry><refentrytitle>systemd.network</refentrytitle><manvolnum>5</manvolnum></citerefentry>
261264
file. If this option is not set, <literal>bridge</literal> and <literal>vlan</literal> devices
262265
inherit the MAC address of the first slave device or the physical interface, respectively. For other
263266
kind of netdevs, if this option is not set, then the MAC address is generated based on the interface
264267
name and the
265268
<citerefentry><refentrytitle>machine-id</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
266269
</para>
270+
<para>Note, even if <literal>none</literal> is specified, <command>systemd-udevd</command>
271+
will assign the persistent MAC address for the device, as <filename>99-default.link</filename>
272+
has <varname>MACAddressPolicy=persistent</varname>. So, it is also necessary to create a
273+
custom .link file for the device, if the MAC address assignment is not desired.</para>
267274
</listitem>
268275
</varlistentry>
269276
</variablelist>

src/network/netdev/netdev-gperf.gperf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ NetDev.Description, config_parse_string,
4949
NetDev.Name, config_parse_ifname, 0, offsetof(NetDev, ifname)
5050
NetDev.Kind, config_parse_netdev_kind, 0, offsetof(NetDev, kind)
5151
NetDev.MTUBytes, config_parse_mtu, AF_UNSPEC, offsetof(NetDev, mtu)
52-
NetDev.MACAddress, config_parse_hw_addr, ETH_ALEN, offsetof(NetDev, hw_addr)
52+
NetDev.MACAddress, config_parse_netdev_hw_addr, ETH_ALEN, offsetof(NetDev, hw_addr)
5353
VLAN.Id, config_parse_vlanid, 0, offsetof(VLan, id)
5454
VLAN.Protocol, config_parse_vlanprotocol, 0, offsetof(VLan, protocol)
5555
VLAN.GVRP, config_parse_tristate, 0, offsetof(VLan, gvrp)
@@ -111,7 +111,7 @@ L2TPSession.PeerSessionId, config_parse_l2tp_session_id,
111111
L2TPSession.Layer2SpecificHeader, config_parse_l2tp_session_l2spec, 0, 0
112112
L2TPSession.Name, config_parse_l2tp_session_name, 0, 0
113113
Peer.Name, config_parse_ifname, 0, offsetof(Veth, ifname_peer)
114-
Peer.MACAddress, config_parse_hw_addr, ETH_ALEN, offsetof(Veth, hw_addr_peer)
114+
Peer.MACAddress, config_parse_netdev_hw_addr, ETH_ALEN, offsetof(Veth, hw_addr_peer)
115115
VXCAN.Peer, config_parse_ifname, 0, offsetof(VxCan, ifname_peer)
116116
VXLAN.VNI, config_parse_uint32, 0, offsetof(VxLan, vni)
117117
VXLAN.Id, config_parse_uint32, 0, offsetof(VxLan, vni) /* deprecated */

src/network/netdev/netdev.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ int netdev_generate_hw_addr(NetDev *netdev, const char *name, struct hw_addr_dat
400400
assert(name);
401401
assert(hw_addr);
402402

403+
if (hw_addr_equal(hw_addr, &HW_ADDR_NONE))
404+
return 0;
405+
403406
if (hw_addr->length == 0) {
404407
uint64_t result;
405408

@@ -467,7 +470,7 @@ static int netdev_create(NetDev *netdev, Link *link, link_netlink_message_handle
467470
if (r < 0)
468471
return log_netdev_error_errno(netdev, r, "Could not append IFLA_IFNAME, attribute: %m");
469472

470-
if (netdev->hw_addr.length > 0) {
473+
if (netdev->hw_addr.length > 0 && !hw_addr_equal(&netdev->hw_addr, &HW_ADDR_NULL)) {
471474
r = netlink_message_append_hw_addr(m, IFLA_ADDRESS, &netdev->hw_addr);
472475
if (r < 0)
473476
return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m");
@@ -887,3 +890,28 @@ int config_parse_netdev_kind(
887890

888891
return 0;
889892
}
893+
894+
int config_parse_netdev_hw_addr(
895+
const char *unit,
896+
const char *filename,
897+
unsigned line,
898+
const char *section,
899+
unsigned section_line,
900+
const char *lvalue,
901+
int ltype,
902+
const char *rvalue,
903+
void *data,
904+
void *userdata) {
905+
906+
struct hw_addr_data *hw_addr = data;
907+
908+
assert(rvalue);
909+
assert(data);
910+
911+
if (streq(rvalue, "none")) {
912+
*hw_addr = HW_ADDR_NONE;
913+
return 0;
914+
}
915+
916+
return config_parse_hw_addr(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
917+
}

src/network/netdev/netdev.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include "networkd-link.h"
1111
#include "time-util.h"
1212

13+
/* Special hardware address value to suppress generating persistent hardware address for the netdev. */
14+
#define HW_ADDR_NONE ((struct hw_addr_data) { .length = 1, })
15+
1316
#define NETDEV_COMMON_SECTIONS "Match\0NetDev\0"
1417
/* This is the list of known sections. We need to ignore them in the initial parsing phase. */
1518
#define NETDEV_OTHER_SECTIONS \
@@ -215,6 +218,7 @@ static inline NetDevCreateType netdev_get_create_type(NetDev *netdev) {
215218
}
216219

217220
CONFIG_PARSER_PROTOTYPE(config_parse_netdev_kind);
221+
CONFIG_PARSER_PROTOTYPE(config_parse_netdev_hw_addr);
218222

219223
/* gperf */
220224
const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *key, GPERF_LEN_TYPE length);

src/network/netdev/veth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static int netdev_veth_fill_message_create(NetDev *netdev, Link *link, sd_netlin
3131
return log_netdev_error_errno(netdev, r, "Failed to add netlink interface name: %m");
3232
}
3333

34-
if (v->hw_addr_peer.length > 0) {
34+
if (v->hw_addr_peer.length > 0 && !hw_addr_equal(&v->hw_addr_peer, &HW_ADDR_NULL)) {
3535
r = netlink_message_append_hw_addr(m, IFLA_ADDRESS, &v->hw_addr_peer);
3636
if (r < 0)
3737
return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m");

0 commit comments

Comments
 (0)