network/netdev: use "struct hw_addr_data" to store MAC address
This commit is contained in:
@@ -49,7 +49,7 @@ NetDev.Description, config_parse_string,
|
||||
NetDev.Name, config_parse_ifname, 0, offsetof(NetDev, ifname)
|
||||
NetDev.Kind, config_parse_netdev_kind, 0, offsetof(NetDev, kind)
|
||||
NetDev.MTUBytes, config_parse_mtu, AF_UNSPEC, offsetof(NetDev, mtu)
|
||||
NetDev.MACAddress, config_parse_ether_addr, 0, offsetof(NetDev, mac)
|
||||
NetDev.MACAddress, config_parse_hw_addr, ETH_ALEN, offsetof(NetDev, hw_addr)
|
||||
VLAN.Id, config_parse_vlanid, 0, offsetof(VLan, id)
|
||||
VLAN.Protocol, config_parse_vlanprotocol, 0, offsetof(VLan, protocol)
|
||||
VLAN.GVRP, config_parse_tristate, 0, offsetof(VLan, gvrp)
|
||||
@@ -111,7 +111,7 @@ L2TPSession.PeerSessionId, config_parse_l2tp_session_id,
|
||||
L2TPSession.Layer2SpecificHeader, config_parse_l2tp_session_l2spec, 0, 0
|
||||
L2TPSession.Name, config_parse_l2tp_session_name, 0, 0
|
||||
Peer.Name, config_parse_ifname, 0, offsetof(Veth, ifname_peer)
|
||||
Peer.MACAddress, config_parse_ether_addr, 0, offsetof(Veth, mac_peer)
|
||||
Peer.MACAddress, config_parse_hw_addr, ETH_ALEN, offsetof(Veth, hw_addr_peer)
|
||||
VXCAN.Peer, config_parse_ifname, 0, offsetof(VxCan, ifname_peer)
|
||||
VXLAN.VNI, config_parse_uint32, 0, offsetof(VxLan, vni)
|
||||
VXLAN.Id, config_parse_uint32, 0, offsetof(VxLan, vni) /* deprecated */
|
||||
|
||||
@@ -229,7 +229,6 @@ static NetDev *netdev_free(NetDev *netdev) {
|
||||
|
||||
free(netdev->description);
|
||||
free(netdev->ifname);
|
||||
free(netdev->mac);
|
||||
condition_free_list(netdev->conditions);
|
||||
|
||||
/* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that
|
||||
@@ -424,8 +423,7 @@ int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) {
|
||||
|
||||
#define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48)
|
||||
|
||||
int netdev_get_mac(const char *ifname, struct ether_addr **ret) {
|
||||
_cleanup_free_ struct ether_addr *mac = NULL;
|
||||
int netdev_generate_hw_addr(const char *name, struct hw_addr_data *ret) {
|
||||
uint64_t result;
|
||||
size_t l, sz;
|
||||
uint8_t *v;
|
||||
@@ -434,10 +432,6 @@ int netdev_get_mac(const char *ifname, struct ether_addr **ret) {
|
||||
assert(ifname);
|
||||
assert(ret);
|
||||
|
||||
mac = new0(struct ether_addr, 1);
|
||||
if (!mac)
|
||||
return -ENOMEM;
|
||||
|
||||
l = strlen(ifname);
|
||||
sz = sizeof(sd_id128_t) + l;
|
||||
v = newa(uint8_t, sz);
|
||||
@@ -456,13 +450,12 @@ int netdev_get_mac(const char *ifname, struct ether_addr **ret) {
|
||||
result = siphash24(v, sz, HASH_KEY.bytes);
|
||||
|
||||
assert_cc(ETH_ALEN <= sizeof(result));
|
||||
memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
|
||||
ret->length = ETH_ALEN;
|
||||
memcpy(ret->bytes, &result, ETH_ALEN);
|
||||
|
||||
/* see eth_random_addr in the kernel */
|
||||
mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
|
||||
mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
|
||||
|
||||
*ret = TAKE_PTR(mac);
|
||||
ret->ether.ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
|
||||
ret->ether.ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -494,8 +487,8 @@ static int netdev_create(NetDev *netdev, Link *link, link_netlink_message_handle
|
||||
if (r < 0)
|
||||
return log_netdev_error_errno(netdev, r, "Could not append IFLA_IFNAME, attribute: %m");
|
||||
|
||||
if (netdev->mac) {
|
||||
r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
|
||||
if (netdev->hw_addr.length > 0) {
|
||||
r = netlink_message_append_hw_addr(m, IFLA_ADDRESS, &netdev->hw_addr);
|
||||
if (r < 0)
|
||||
return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m");
|
||||
}
|
||||
@@ -813,8 +806,8 @@ int netdev_load_one(Manager *manager, const char *filename) {
|
||||
if (!netdev->filename)
|
||||
return log_oom();
|
||||
|
||||
if (!netdev->mac && NETDEV_VTABLE(netdev)->generate_mac) {
|
||||
r = netdev_get_mac(netdev->ifname, &netdev->mac);
|
||||
if (netdev->hw_addr.length == 0 && NETDEV_VTABLE(netdev)->generate_mac) {
|
||||
r = netdev_generate_hw_addr(netdev->ifname, &netdev->hw_addr);
|
||||
if (r < 0)
|
||||
return log_netdev_error_errno(netdev, r,
|
||||
"Failed to generate predictable MAC address: %m");
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "sd-netlink.h"
|
||||
|
||||
#include "conf-parser.h"
|
||||
#include "ether-addr-util.h"
|
||||
#include "list.h"
|
||||
#include "log-link.h"
|
||||
#include "networkd-link.h"
|
||||
@@ -118,7 +119,7 @@ typedef struct NetDev {
|
||||
NetDevKind kind;
|
||||
char *description;
|
||||
char *ifname;
|
||||
struct ether_addr *mac;
|
||||
struct hw_addr_data hw_addr;
|
||||
uint32_t mtu;
|
||||
int ifindex;
|
||||
} NetDev;
|
||||
@@ -197,7 +198,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_unref);
|
||||
bool netdev_is_managed(NetDev *netdev);
|
||||
int netdev_get(Manager *manager, const char *name, NetDev **ret);
|
||||
int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *newlink);
|
||||
int netdev_get_mac(const char *ifname, struct ether_addr **ret);
|
||||
int netdev_generate_hw_addr(const char *name, struct hw_addr_data *ret);
|
||||
int netdev_join(NetDev *netdev, Link *link, link_netlink_message_handler_t cb);
|
||||
|
||||
int request_process_stacked_netdev(Request *req);
|
||||
|
||||
@@ -8,9 +8,9 @@ static int netdev_nlmon_verify(NetDev *netdev, const char *filename) {
|
||||
assert(netdev);
|
||||
assert(filename);
|
||||
|
||||
if (netdev->mac) {
|
||||
if (netdev->hw_addr.length > 0) {
|
||||
log_netdev_warning(netdev, "%s: MACAddress= is not supported. Ignoring", filename);
|
||||
netdev->mac = mfree(netdev->mac);
|
||||
netdev->hw_addr = HW_ADDR_NULL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -136,7 +136,7 @@ static int tuntap_verify(NetDev *netdev, const char *filename) {
|
||||
"Please set it in the corresponding .network file.",
|
||||
netdev_kind_to_string(netdev->kind), filename);
|
||||
|
||||
if (netdev->mac)
|
||||
if (netdev->hw_addr.length > 0)
|
||||
log_netdev_warning(netdev,
|
||||
"MACAddress= configured for %s device in %s will be ignored.\n"
|
||||
"Please set it in the corresponding .network file.",
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <linux/if_arp.h>
|
||||
#include <linux/veth.h>
|
||||
|
||||
#include "netlink-util.h"
|
||||
#include "veth.h"
|
||||
|
||||
static int netdev_veth_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
|
||||
@@ -30,8 +31,8 @@ static int netdev_veth_fill_message_create(NetDev *netdev, Link *link, sd_netlin
|
||||
return log_netdev_error_errno(netdev, r, "Failed to add netlink interface name: %m");
|
||||
}
|
||||
|
||||
if (v->mac_peer) {
|
||||
r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, v->mac_peer);
|
||||
if (v->hw_addr_peer.length > 0) {
|
||||
r = netlink_message_append_hw_addr(m, IFLA_ADDRESS, &v->hw_addr_peer);
|
||||
if (r < 0)
|
||||
return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m");
|
||||
}
|
||||
@@ -65,11 +66,11 @@ static int netdev_veth_verify(NetDev *netdev, const char *filename) {
|
||||
"Veth NetDev without peer name configured in %s. Ignoring",
|
||||
filename);
|
||||
|
||||
if (!v->mac_peer) {
|
||||
r = netdev_get_mac(v->ifname_peer, &v->mac_peer);
|
||||
if (v->hw_addr_peer.length == 0) {
|
||||
r = netdev_generate_hw_addr(v->ifname_peer, &v->hw_addr_peer);
|
||||
if (r < 0)
|
||||
return log_netdev_warning_errno(netdev, r,
|
||||
"Failed to generate predictable MAC address for %s: %m",
|
||||
"Failed to generate persistent hardware address for peer '%s': %m",
|
||||
v->ifname_peer);
|
||||
}
|
||||
|
||||
@@ -86,7 +87,6 @@ static void veth_done(NetDev *n) {
|
||||
assert(v);
|
||||
|
||||
free(v->ifname_peer);
|
||||
free(v->mac_peer);
|
||||
}
|
||||
|
||||
const NetDevVTable veth_vtable = {
|
||||
|
||||
@@ -9,7 +9,7 @@ struct Veth {
|
||||
NetDev meta;
|
||||
|
||||
char *ifname_peer;
|
||||
struct ether_addr *mac_peer;
|
||||
struct hw_addr_data hw_addr_peer;
|
||||
};
|
||||
|
||||
DEFINE_NETDEV_CAST(VETH, Veth);
|
||||
|
||||
Reference in New Issue
Block a user