-/* SPDX-License-Identifier: LGPL-2.1+ */
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <net/if.h>
#include <linux/if.h>
#include "alloc-util.h"
#include "ether-addr-util.h"
+#include "hexdecoct.h"
#include "lockfile-util.h"
#include "missing_network.h"
#include "netif-naming-scheme.h"
#include "nspawn-network.h"
#include "parse-util.h"
#include "siphash24.h"
+#include "socket-netlink.h"
#include "socket-util.h"
#include "stat-util.h"
#include "string-util.h"
return 0;
}
-/* This is almost base64char(), but not entirely, as it uses the "url and filename safe" alphabet, since we
- * don't want "/" appear in interface names (since interfaces appear in sysfs as filenames). See section #5
- * of RFC 4648. */
-static char urlsafe_base64char(int x) {
- static const char table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz"
- "0123456789-_";
- return table[x & 63];
-}
-
static int shorten_ifname(char *ifname) {
char new_ifname[IFNAMSIZ];
if (r < 0)
return r;
- u = if_nametoindex(n);
+ u = if_nametoindex(n); /* We don't need to use rtnl_resolve_ifname() here because the
+ * name we assigned is always the main name. */
if (u == 0)
return log_error_errno(errno, "Failed to resolve interface %s: %m", n);
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
uint64_t idx = 0;
- char **a, **b;
int r;
assert(machine_name);
assert(veth_name);
assert(bridge_name);
- r = parse_ifindex_or_ifname(bridge_name, &bridge_ifi);
- if (r < 0)
- return r;
+ bridge_ifi = rtnl_resolve_interface(&rtnl, bridge_name);
+ if (bridge_ifi < 0)
+ return bridge_ifi;
r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, 0);
if (r < 0)
return remove_one_link(rtnl, bridge_name);
}
-static int parse_interface(const char *name) {
- int ifi, r;
-
- r = parse_ifindex_or_ifname(name, &ifi);
- if (r < 0)
- return log_error_errno(r, "Failed to resolve interface %s: %m", name);
-
- return ifi;
-}
-
int test_network_interface_initialized(const char *name) {
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
- int ifi, r;
- char ifi_str[2 + DECIMAL_STR_MAX(int)];
+ int r;
- if (path_is_read_only_fs("/sys"))
+ if (path_is_read_only_fs("/sys") > 0)
return 0;
/* udev should be around. */
- ifi = parse_interface(name);
- if (ifi < 0)
- return ifi;
-
- sprintf(ifi_str, "n%i", ifi);
- r = sd_device_new_from_device_id(&d, ifi_str);
+ r = sd_device_new_from_ifname(&d, name);
if (r < 0)
return log_error_errno(r, "Failed to get device %s: %m", name);
int move_network_interfaces(int netns_fd, char **ifaces) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
- char **i;
int r;
if (strv_isempty(ifaces))
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
int ifi;
- ifi = parse_interface(*i);
+ ifi = rtnl_resolve_interface_or_warn(&rtnl, *i);
if (ifi < 0)
return ifi;
int setup_macvlan(const char *machine_name, pid_t pid, char **ifaces) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
unsigned idx = 0;
- char **i;
int r;
if (strv_isempty(ifaces))
struct ether_addr mac;
int ifi;
- ifi = parse_interface(*i);
+ ifi = rtnl_resolve_interface_or_warn(&rtnl, *i);
if (ifi < 0)
return ifi;
int setup_ipvlan(const char *machine_name, pid_t pid, char **ifaces) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
- char **i;
int r;
if (strv_isempty(ifaces))
_cleanup_free_ char *n = NULL, *a = NULL;
int ifi;
- ifi = parse_interface(*i);
+ ifi = rtnl_resolve_interface_or_warn(&rtnl, *i);
if (ifi < 0)
return ifi;
int remove_veth_links(const char *primary, char **pairs) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
- char **a, **b;
int r;
/* In some cases the kernel might pin the veth links between host and container even after the namespace