summaryrefslogtreecommitdiffstats
path: root/src/network
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2024-09-09 17:57:25 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2024-09-10 12:30:13 +0200
commit5dc20e1acdfc729c732b23fcec44cf07f4a5212e (patch)
treea816d46b69f4e1b5c2c3842ab7b32b452646d6fb /src/network
parentnetwork/netdev: split out netdev_attach() and netdev_attach_name() from netde... (diff)
downloadsystemd-5dc20e1acdfc729c732b23fcec44cf07f4a5212e.tar.xz
systemd-5dc20e1acdfc729c732b23fcec44cf07f4a5212e.zip
network/netdev: split out netdev_set_ifindex_internal() and _impl()
No functional change, just refactoring and preparation for later commits.
Diffstat (limited to 'src/network')
-rw-r--r--src/network/netdev/netdev.c59
-rw-r--r--src/network/netdev/netdev.h2
2 files changed, 40 insertions, 21 deletions
diff --git a/src/network/netdev/netdev.c b/src/network/netdev/netdev.c
index 9949ade363..d6c78c318d 100644
--- a/src/network/netdev/netdev.c
+++ b/src/network/netdev/netdev.c
@@ -363,7 +363,7 @@ void netdev_enter_failed(NetDev *netdev) {
netdev->state = NETDEV_STATE_FAILED;
}
-static int netdev_enter_ready(NetDev *netdev) {
+int netdev_enter_ready(NetDev *netdev) {
assert(netdev);
assert(netdev->ifname);
@@ -402,6 +402,42 @@ static int netdev_create_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev
return 1;
}
+int netdev_set_ifindex_internal(NetDev *netdev, int ifindex) {
+ assert(netdev);
+ assert(ifindex > 0);
+
+ if (netdev->ifindex == ifindex)
+ return 0; /* Already set. */
+
+ if (netdev->ifindex > 0 && netdev->ifindex != ifindex)
+ return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EEXIST),
+ "Could not set ifindex to %i, already set to %i.",
+ ifindex, netdev->ifindex);
+
+ netdev->ifindex = ifindex;
+ log_netdev_debug(netdev, "Gained index %i.", ifindex);
+ return 1; /* set new ifindex. */
+}
+
+static int netdev_set_ifindex_impl(NetDev *netdev, const char *name, int ifindex) {
+ int r;
+
+ assert(netdev);
+ assert(name);
+ assert(ifindex > 0);
+
+ if (!streq(netdev->ifname, name))
+ return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
+ "Received netlink message with unexpected interface name %s (ifindex=%i).",
+ name, ifindex);
+
+ r = netdev_set_ifindex_internal(netdev, ifindex);
+ if (r <= 0)
+ return r;
+
+ return netdev_enter_ready(netdev);
+}
+
int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) {
uint16_t type;
const char *kind;
@@ -432,23 +468,10 @@ int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) {
if (ifindex <= 0)
return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Got invalid ifindex: %d", ifindex);
- if (netdev->ifindex > 0) {
- if (netdev->ifindex != ifindex)
- return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EEXIST),
- "Could not set ifindex to %d, already set to %d",
- ifindex, netdev->ifindex);
-
- /* ifindex already set to the same for this netdev */
- return 0;
- }
-
r = sd_netlink_message_read_string(message, IFLA_IFNAME, &received_name);
if (r < 0)
return log_netdev_warning_errno(netdev, r, "Could not get IFNAME: %m");
- if (!streq(netdev->ifname, received_name))
- return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Received newlink with wrong IFNAME %s", received_name);
-
if (!NETDEV_VTABLE(netdev)->skip_netdev_kind_check) {
r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
@@ -477,13 +500,7 @@ int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) {
received_kind, kind);
}
- netdev->ifindex = ifindex;
-
- log_netdev_debug(netdev, "netdev has index %d", netdev->ifindex);
-
- netdev_enter_ready(netdev);
-
- return 0;
+ return netdev_set_ifindex_impl(netdev, received_name, ifindex);
}
#define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48)
diff --git a/src/network/netdev/netdev.h b/src/network/netdev/netdev.h
index c173ed3fd5..0b942caa4b 100644
--- a/src/network/netdev/netdev.h
+++ b/src/network/netdev/netdev.h
@@ -199,11 +199,13 @@ extern const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX];
int netdev_attach_name(NetDev *netdev, const char *name);
NetDev* netdev_detach_name(NetDev *netdev, const char *name);
void netdev_detach(NetDev *netdev);
+int netdev_set_ifindex_internal(NetDev *netdev, int ifindex);
int netdev_load(Manager *manager, bool reload);
int netdev_load_one(Manager *manager, const char *filename);
void netdev_drop(NetDev *netdev);
void netdev_enter_failed(NetDev *netdev);
+int netdev_enter_ready(NetDev *netdev);
NetDev *netdev_unref(NetDev *netdev);
NetDev *netdev_ref(NetDev *netdev);