diff --git a/src/libsystemd-network/dhcp-server-internal.h b/src/libsystemd-network/dhcp-server-internal.h index b5293c3ed6..c935f068f1 100644 --- a/src/libsystemd-network/dhcp-server-internal.h +++ b/src/libsystemd-network/dhcp-server-internal.h @@ -48,6 +48,7 @@ struct sd_dhcp_server { int fd_raw; int ifindex; + char *ifname; be32_t address; be32_t netmask; be32_t subnet; diff --git a/src/libsystemd-network/lldp-internal.h b/src/libsystemd-network/lldp-internal.h index daedbb088f..7ecedc61ec 100644 --- a/src/libsystemd-network/lldp-internal.h +++ b/src/libsystemd-network/lldp-internal.h @@ -12,6 +12,7 @@ struct sd_lldp { unsigned n_ref; int ifindex; + char *ifname; int fd; sd_event *event; diff --git a/src/libsystemd-network/meson.build b/src/libsystemd-network/meson.build index 6bc4a3f54b..ca3393eff3 100644 --- a/src/libsystemd-network/meson.build +++ b/src/libsystemd-network/meson.build @@ -16,6 +16,8 @@ sources = files(''' sd-ipv4acd.c arp-util.h arp-util.c + network-common.c + network-common.h network-internal.c network-internal.h sd-ndisc.c diff --git a/src/libsystemd-network/ndisc-internal.h b/src/libsystemd-network/ndisc-internal.h index 70b254867b..ca90d5c539 100644 --- a/src/libsystemd-network/ndisc-internal.h +++ b/src/libsystemd-network/ndisc-internal.h @@ -5,11 +5,11 @@ Copyright © 2014 Intel Corporation. All rights reserved. ***/ +#include "sd-ndisc.h" + #include "log.h" #include "time-util.h" -#include "sd-ndisc.h" - #define NDISC_ROUTER_SOLICITATION_INTERVAL (4U * USEC_PER_SEC) #define NDISC_MAX_ROUTER_SOLICITATION_INTERVAL (3600U * USEC_PER_SEC) #define NDISC_MAX_ROUTER_SOLICITATIONS 3U @@ -18,6 +18,7 @@ struct sd_ndisc { unsigned n_ref; int ifindex; + char *ifname; int fd; sd_event *event; diff --git a/src/libsystemd-network/network-common.c b/src/libsystemd-network/network-common.c new file mode 100644 index 0000000000..9bc0da96cd --- /dev/null +++ b/src/libsystemd-network/network-common.c @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "format-util.h" +#include "network-common.h" +#include "string-util.h" + +const char *get_ifname(int ifindex, char **ifname) { + char buf[IF_NAMESIZE + 1]; + + assert(ifname); + + /* This sets ifname only when it is not set yet. */ + + if (*ifname) + return *ifname; + + if (ifindex <= 0) + return NULL; + + if (!format_ifname(ifindex, buf)) + return NULL; + + return *ifname = strdup(buf); +} diff --git a/src/libsystemd-network/network-common.h b/src/libsystemd-network/network-common.h new file mode 100644 index 0000000000..76a6c4a989 --- /dev/null +++ b/src/libsystemd-network/network-common.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +const char *get_ifname(int ifindex, char **ifname); diff --git a/src/libsystemd-network/radv-internal.h b/src/libsystemd-network/radv-internal.h index 3dbeffe0c6..7668224e80 100644 --- a/src/libsystemd-network/radv-internal.h +++ b/src/libsystemd-network/radv-internal.h @@ -41,6 +41,7 @@ struct sd_radv { RAdvState state; int ifindex; + char *ifname; sd_event *event; int event_priority; diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index dd9cbf6ea5..bf740d01ce 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -24,6 +24,7 @@ #include "hostname-util.h" #include "io-util.h" #include "memory-util.h" +#include "network-common.h" #include "random-util.h" #include "set.h" #include "sort-util.h" @@ -76,6 +77,7 @@ struct sd_dhcp_client { int event_priority; sd_event_source *timeout_resend; int ifindex; + char *ifname; int fd; uint16_t port; union sockaddr_union link; @@ -282,6 +284,23 @@ int sd_dhcp_client_set_ifindex(sd_dhcp_client *client, int ifindex) { return 0; } +int sd_dhcp_client_set_ifname(sd_dhcp_client *client, const char *ifname) { + assert_return(client, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&client->ifname, ifname); +} + +const char *sd_dhcp_client_get_ifname(sd_dhcp_client *client) { + if (!client) + return NULL; + + return get_ifname(client->ifindex, &client->ifname); +} + int sd_dhcp_client_set_mac( sd_dhcp_client *client, const uint8_t *addr, @@ -2205,6 +2224,7 @@ static sd_dhcp_client *dhcp_client_free(sd_dhcp_client *client) { client->user_class = strv_free(client->user_class); ordered_hashmap_free(client->extra_options); ordered_hashmap_free(client->vendor_options); + free(client->ifname); return mfree(client); } diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index d0e249b104..94b0b572e8 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -15,6 +15,7 @@ #include "fd-util.h" #include "in-addr-util.h" #include "io-util.h" +#include "network-common.h" #include "ordered-set.h" #include "siphash24.h" #include "string-util.h" @@ -158,6 +159,8 @@ static sd_dhcp_server *dhcp_server_free(sd_dhcp_server *server) { ordered_set_free(server->vendor_options); free(server->bound_leases); + + free(server->ifname); return mfree(server); } @@ -169,29 +172,47 @@ int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) { assert_return(ret, -EINVAL); assert_return(ifindex > 0, -EINVAL); - server = new0(sd_dhcp_server, 1); + server = new(sd_dhcp_server, 1); if (!server) return -ENOMEM; - server->n_ref = 1; - server->fd_raw = -1; - server->fd = -1; - server->address = htobe32(INADDR_ANY); - server->netmask = htobe32(INADDR_ANY); - server->ifindex = ifindex; + *server = (sd_dhcp_server) { + .n_ref = 1, + .fd_raw = -1, + .fd = -1, + .address = htobe32(INADDR_ANY), + .netmask = htobe32(INADDR_ANY), + .ifindex = ifindex, + .default_lease_time = DIV_ROUND_UP(DHCP_DEFAULT_LEASE_TIME_USEC, USEC_PER_SEC), + .max_lease_time = DIV_ROUND_UP(DHCP_MAX_LEASE_TIME_USEC, USEC_PER_SEC), + }; server->leases_by_client_id = hashmap_new(&dhcp_lease_hash_ops); if (!server->leases_by_client_id) return -ENOMEM; - server->default_lease_time = DIV_ROUND_UP(DHCP_DEFAULT_LEASE_TIME_USEC, USEC_PER_SEC); - server->max_lease_time = DIV_ROUND_UP(DHCP_MAX_LEASE_TIME_USEC, USEC_PER_SEC); - *ret = TAKE_PTR(server); return 0; } +int sd_dhcp_server_set_ifname(sd_dhcp_server *server, const char *ifname) { + assert_return(server, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&server->ifname, ifname); +} + +const char *sd_dhcp_server_get_ifname(sd_dhcp_server *server) { + if (!server) + return NULL; + + return get_ifname(server->ifindex, &server->ifname); +} + int sd_dhcp_server_attach_event(sd_dhcp_server *server, sd_event *event, int64_t priority) { int r; diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c index 410bfda10e..f92f517029 100644 --- a/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/libsystemd-network/sd-dhcp6-client.c @@ -21,6 +21,7 @@ #include "hexdecoct.h" #include "hostname-util.h" #include "in-addr-util.h" +#include "network-common.h" #include "random-util.h" #include "socket-util.h" #include "string-table.h" @@ -47,6 +48,7 @@ struct sd_dhcp6_client { sd_event *event; int event_priority; int ifindex; + char *ifname; DHCP6Address hint_pd_prefix; struct in6_addr local_address; uint8_t mac_addr[MAX_MAC_ADDR_LEN]; @@ -165,6 +167,23 @@ int sd_dhcp6_client_set_ifindex(sd_dhcp6_client *client, int ifindex) { return 0; } +int sd_dhcp6_client_set_ifname(sd_dhcp6_client *client, const char *ifname) { + assert_return(client, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&client->ifname, ifname); +} + +const char *sd_dhcp6_client_get_ifname(sd_dhcp6_client *client) { + if (!client) + return NULL; + + return get_ifname(client->ifindex, &client->ifname); +} + int sd_dhcp6_client_set_local_address( sd_dhcp6_client *client, const struct in6_addr *local_address) { @@ -1787,6 +1806,7 @@ static sd_dhcp6_client *dhcp6_client_free(sd_dhcp6_client *client) { ordered_hashmap_free(client->extra_options); strv_free(client->user_class); strv_free(client->vendor_class); + free(client->ifname); return mfree(client); } diff --git a/src/libsystemd-network/sd-ipv4acd.c b/src/libsystemd-network/sd-ipv4acd.c index defd23d85a..2a85afd58b 100644 --- a/src/libsystemd-network/sd-ipv4acd.c +++ b/src/libsystemd-network/sd-ipv4acd.c @@ -16,9 +16,9 @@ #include "ether-addr-util.h" #include "event-util.h" #include "fd-util.h" -#include "format-util.h" #include "in-addr-util.h" #include "log-link.h" +#include "network-common.h" #include "random-util.h" #include "siphash24.h" #include "string-table.h" @@ -56,7 +56,7 @@ struct sd_ipv4acd { int ifindex; int fd; - char ifname[IF_NAMESIZE + 1]; + char *ifname; unsigned n_iteration; unsigned n_conflict; @@ -125,7 +125,7 @@ static sd_ipv4acd *ipv4acd_free(sd_ipv4acd *acd) { ipv4acd_reset(acd); sd_ipv4acd_detach_event(acd); - + free(acd->ifname); return mfree(acd); } @@ -398,16 +398,10 @@ fail: } int sd_ipv4acd_set_ifindex(sd_ipv4acd *acd, int ifindex) { - char ifname[IF_NAMESIZE + 1]; - assert_return(acd, -EINVAL); assert_return(ifindex > 0, -EINVAL); assert_return(acd->state == IPV4ACD_STATE_INIT, -EBUSY); - if (!format_ifname(ifindex, ifname)) - return -ENODEV; - - strcpy(acd->ifname, ifname); acd->ifindex = ifindex; return 0; @@ -420,11 +414,21 @@ int sd_ipv4acd_get_ifindex(sd_ipv4acd *acd) { return acd->ifindex; } +int sd_ipv4acd_set_ifname(sd_ipv4acd *acd, const char *ifname) { + assert_return(acd, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&acd->ifname, ifname); +} + const char *sd_ipv4acd_get_ifname(sd_ipv4acd *acd) { if (!acd) return NULL; - return empty_to_null(acd->ifname); + return get_ifname(acd->ifindex, &acd->ifname); } int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr) { diff --git a/src/libsystemd-network/sd-ipv4ll.c b/src/libsystemd-network/sd-ipv4ll.c index a83c9b06de..25fa4ab90b 100644 --- a/src/libsystemd-network/sd-ipv4ll.c +++ b/src/libsystemd-network/sd-ipv4ll.c @@ -112,6 +112,13 @@ int sd_ipv4ll_get_ifindex(sd_ipv4ll *ll) { return sd_ipv4acd_get_ifindex(ll->acd); } +int sd_ipv4ll_set_ifname(sd_ipv4ll *ll, const char *ifname) { + assert_return(ll, -EINVAL); + assert_return(ifname, -EINVAL); + + return sd_ipv4acd_set_ifname(ll->acd, ifname); +} + const char *sd_ipv4ll_get_ifname(sd_ipv4ll *ll) { if (!ll) return NULL; diff --git a/src/libsystemd-network/sd-lldp.c b/src/libsystemd-network/sd-lldp.c index d3848ebf91..1298fea9ff 100644 --- a/src/libsystemd-network/sd-lldp.c +++ b/src/libsystemd-network/sd-lldp.c @@ -14,6 +14,7 @@ #include "lldp-neighbor.h" #include "lldp-network.h" #include "memory-util.h" +#include "network-common.h" #include "socket-util.h" #include "sort-util.h" #include "string-table.h" @@ -343,6 +344,23 @@ _public_ int sd_lldp_set_ifindex(sd_lldp *lldp, int ifindex) { return 0; } +int sd_lldp_set_ifname(sd_lldp *lldp, const char *ifname) { + assert_return(lldp, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&lldp->ifname, ifname); +} + +const char *sd_lldp_get_ifname(sd_lldp *lldp) { + if (!lldp) + return NULL; + + return get_ifname(lldp->ifindex, &lldp->ifname); +} + static sd_lldp* lldp_free(sd_lldp *lldp) { assert(lldp); @@ -354,6 +372,7 @@ static sd_lldp* lldp_free(sd_lldp *lldp) { hashmap_free(lldp->neighbor_by_id); prioq_free(lldp->neighbor_by_expiry); + free(lldp->ifname); return mfree(lldp); } diff --git a/src/libsystemd-network/sd-ndisc.c b/src/libsystemd-network/sd-ndisc.c index 8f04001755..8a5f341615 100644 --- a/src/libsystemd-network/sd-ndisc.c +++ b/src/libsystemd-network/sd-ndisc.c @@ -16,6 +16,7 @@ #include "memory-util.h" #include "ndisc-internal.h" #include "ndisc-router.h" +#include "network-common.h" #include "random-util.h" #include "socket-util.h" #include "string-table.h" @@ -65,6 +66,23 @@ _public_ int sd_ndisc_set_ifindex(sd_ndisc *nd, int ifindex) { return 0; } +int sd_ndisc_set_ifname(sd_ndisc *nd, const char *ifname) { + assert_return(nd, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&nd->ifname, ifname); +} + +const char *sd_ndisc_get_ifname(sd_ndisc *nd) { + if (!nd) + return NULL; + + return get_ifname(nd->ifindex, &nd->ifname); +} + _public_ int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr) { assert_return(nd, -EINVAL); @@ -129,6 +147,7 @@ static sd_ndisc *ndisc_free(sd_ndisc *nd) { ndisc_reset(nd); sd_ndisc_detach_event(nd); + free(nd->ifname); return mfree(nd); } diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index 164b24c684..531ab7bcd2 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -19,6 +19,7 @@ #include "io-util.h" #include "macro.h" #include "memory-util.h" +#include "network-common.h" #include "radv-internal.h" #include "random-util.h" #include "socket-util.h" @@ -122,6 +123,7 @@ static sd_radv *radv_free(sd_radv *ra) { sd_radv_detach_event(ra); ra->fd = safe_close(ra->fd); + free(ra->ifname); return mfree(ra); } @@ -432,6 +434,23 @@ _public_ int sd_radv_set_ifindex(sd_radv *ra, int ifindex) { return 0; } +int sd_radv_set_ifname(sd_radv *ra, const char *ifname) { + assert_return(ra, -EINVAL); + assert_return(ifname, -EINVAL); + + if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE)) + return -EINVAL; + + return free_and_strdup(&ra->ifname, ifname); +} + +const char *sd_radv_get_ifname(sd_radv *ra) { + if (!ra) + return NULL; + + return get_ifname(ra->ifindex, &ra->ifname); +} + _public_ int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr) { assert_return(ra, -EINVAL); diff --git a/src/systemd/sd-dhcp-client.h b/src/systemd/sd-dhcp-client.h index 822286919e..c784cbcb9a 100644 --- a/src/systemd/sd-dhcp-client.h +++ b/src/systemd/sd-dhcp-client.h @@ -125,6 +125,10 @@ int sd_dhcp_client_set_request_broadcast( int sd_dhcp_client_set_ifindex( sd_dhcp_client *client, int interface_index); +int sd_dhcp_client_set_ifname( + sd_dhcp_client *client, + const char *interface_name); +const char *sd_dhcp_client_get_ifname(sd_dhcp_client *client); int sd_dhcp_client_set_mac( sd_dhcp_client *client, const uint8_t *addr, diff --git a/src/systemd/sd-dhcp-server.h b/src/systemd/sd-dhcp-server.h index aa49a7434c..e3097ebb31 100644 --- a/src/systemd/sd-dhcp-server.h +++ b/src/systemd/sd-dhcp-server.h @@ -37,6 +37,9 @@ enum { int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex); +int sd_dhcp_server_set_ifname(sd_dhcp_server *server, const char *ifname); +const char *sd_dhcp_server_get_ifname(sd_dhcp_server *server); + sd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server); sd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server); diff --git a/src/systemd/sd-dhcp6-client.h b/src/systemd/sd-dhcp6-client.h index 84e3170130..e02d67632a 100644 --- a/src/systemd/sd-dhcp6-client.h +++ b/src/systemd/sd-dhcp6-client.h @@ -91,6 +91,10 @@ int sd_dhcp6_client_set_callback( int sd_dhcp6_client_set_ifindex( sd_dhcp6_client *client, int interface_index); +int sd_dhcp6_client_set_ifname( + sd_dhcp6_client *client, + const char *interface_name); +const char * sd_dhcp6_client_get_ifname(sd_dhcp6_client *client); int sd_dhcp6_client_set_local_address( sd_dhcp6_client *client, const struct in6_addr *local_address); diff --git a/src/systemd/sd-ipv4acd.h b/src/systemd/sd-ipv4acd.h index 2809d8748b..1e89a81b31 100644 --- a/src/systemd/sd-ipv4acd.h +++ b/src/systemd/sd-ipv4acd.h @@ -44,6 +44,7 @@ int sd_ipv4acd_set_callback(sd_ipv4acd *acd, sd_ipv4acd_callback_t cb, void *use int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr); int sd_ipv4acd_set_ifindex(sd_ipv4acd *acd, int interface_index); int sd_ipv4acd_get_ifindex(sd_ipv4acd *acd); +int sd_ipv4acd_set_ifname(sd_ipv4acd *acd, const char *interface_name); const char *sd_ipv4acd_get_ifname(sd_ipv4acd *acd); int sd_ipv4acd_set_address(sd_ipv4acd *acd, const struct in_addr *address); int sd_ipv4acd_is_running(sd_ipv4acd *acd); diff --git a/src/systemd/sd-ipv4ll.h b/src/systemd/sd-ipv4ll.h index aa4d174e4b..bf5596ab61 100644 --- a/src/systemd/sd-ipv4ll.h +++ b/src/systemd/sd-ipv4ll.h @@ -44,6 +44,7 @@ int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_callback_t cb, void *userdat int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr); int sd_ipv4ll_set_ifindex(sd_ipv4ll *ll, int interface_index); int sd_ipv4ll_get_ifindex(sd_ipv4ll *ll); +int sd_ipv4ll_set_ifname(sd_ipv4ll *ll, const char *interface_name); const char *sd_ipv4ll_get_ifname(sd_ipv4ll *ll); int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address); int sd_ipv4ll_set_address_seed(sd_ipv4ll *ll, uint64_t seed); diff --git a/src/systemd/sd-lldp.h b/src/systemd/sd-lldp.h index d1d520afbd..c867a88590 100644 --- a/src/systemd/sd-lldp.h +++ b/src/systemd/sd-lldp.h @@ -147,6 +147,8 @@ sd_event *sd_lldp_get_event(sd_lldp *lldp); int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_callback_t cb, void *userdata); int sd_lldp_set_ifindex(sd_lldp *lldp, int ifindex); +int sd_lldp_set_ifname(sd_lldp *lldp, const char *ifname); +const char *sd_lldp_get_ifname(sd_lldp *lldp); /* Controls how much and what to store in the neighbors database */ int sd_lldp_set_neighbors_max(sd_lldp *lldp, uint64_t n); diff --git a/src/systemd/sd-ndisc.h b/src/systemd/sd-ndisc.h index 49b127c018..6088def1b6 100644 --- a/src/systemd/sd-ndisc.h +++ b/src/systemd/sd-ndisc.h @@ -78,6 +78,8 @@ sd_event *sd_ndisc_get_event(sd_ndisc *nd); int sd_ndisc_set_callback(sd_ndisc *nd, sd_ndisc_callback_t cb, void *userdata); int sd_ndisc_set_ifindex(sd_ndisc *nd, int interface_index); +int sd_ndisc_set_ifname(sd_ndisc *nd, const char *interface_name); +const char *sd_ndisc_get_ifname(sd_ndisc *nd); int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr); int sd_ndisc_get_mtu(sd_ndisc *nd, uint32_t *ret); diff --git a/src/systemd/sd-radv.h b/src/systemd/sd-radv.h index 3f6c149d5e..be5cf1b94e 100644 --- a/src/systemd/sd-radv.h +++ b/src/systemd/sd-radv.h @@ -53,6 +53,8 @@ int sd_radv_stop(sd_radv *ra); int sd_radv_is_running(sd_radv *ra); int sd_radv_set_ifindex(sd_radv *ra, int interface_index); +int sd_radv_set_ifname(sd_radv *ra, const char *interface_name); +const char *sd_radv_get_ifname(sd_radv *ra); int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr); int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu); int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit);