From 953a02d11bed5f1b01e3f37a4bb17a5464d3a2ba Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 18 Mar 2019 12:01:02 +0100 Subject: [PATCH] tree-wide: port various users over to socket_bind_to_ifindex() --- src/basic/socket-label.c | 8 +++++--- src/libsystemd-network/dhcp-network.c | 8 ++------ src/libsystemd-network/icmp6-util.c | 12 ++++-------- src/resolve/resolved-dns-stub.c | 10 ++++++---- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/basic/socket-label.c b/src/basic/socket-label.c index 4ed19cd..b5e9b37 100644 --- a/src/basic/socket-label.c +++ b/src/basic/socket-label.c @@ -68,9 +68,11 @@ int socket_address_listen( } if (IN_SET(socket_address_family(a), AF_INET, AF_INET6)) { - if (bind_to_device) - if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0) - return -errno; + if (bind_to_device) { + r = socket_bind_to_ifname(fd, bind_to_device); + if (r < 0) + return r; + } if (reuse_port) { r = setsockopt_int(fd, SOL_SOCKET, SO_REUSEPORT, true); diff --git a/src/libsystemd-network/dhcp-network.c b/src/libsystemd-network/dhcp-network.c index b62eed0..94c10ed 100644 --- a/src/libsystemd-network/dhcp-network.c +++ b/src/libsystemd-network/dhcp-network.c @@ -153,7 +153,6 @@ int dhcp_network_bind_udp_socket(int ifindex, be32_t address, uint16_t port) { .in.sin_addr.s_addr = address, }; _cleanup_close_ int s = -1; - char ifname[IF_NAMESIZE] = ""; int r; s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); @@ -169,12 +168,9 @@ int dhcp_network_bind_udp_socket(int ifindex, be32_t address, uint16_t port) { return r; if (ifindex > 0) { - if (if_indextoname(ifindex, ifname) == 0) - return -errno; - - r = setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname)); + r = socket_bind_to_ifindex(s, ifindex); if (r < 0) - return -errno; + return r; } if (address == INADDR_ANY) { diff --git a/src/libsystemd-network/icmp6-util.c b/src/libsystemd-network/icmp6-util.c index e535b12..e1f193a 100644 --- a/src/libsystemd-network/icmp6-util.c +++ b/src/libsystemd-network/icmp6-util.c @@ -31,9 +31,8 @@ static int icmp6_bind_router_message(const struct icmp6_filter *filter, const struct ipv6_mreq *mreq) { - int index = mreq->ipv6mr_interface; + int ifindex = mreq->ipv6mr_interface; _cleanup_close_ int s = -1; - char ifname[IF_NAMESIZE] = ""; int r; s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_ICMPV6); @@ -52,7 +51,7 @@ static int icmp6_bind_router_message(const struct icmp6_filter *filter, IPV6_PKTINFO socket option also applies for ICMPv6 multicast. Empirical experiments indicates otherwise and therefore an IPV6_MULTICAST_IF socket option is used here instead */ - r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, index); + r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, ifindex); if (r < 0) return r; @@ -76,12 +75,9 @@ static int icmp6_bind_router_message(const struct icmp6_filter *filter, if (r < 0) return r; - if (if_indextoname(index, ifname) == 0) - return -errno; - - r = setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname)); + r = socket_bind_to_ifindex(s, ifindex); if (r < 0) - return -errno; + return r; return TAKE_FD(s); } diff --git a/src/resolve/resolved-dns-stub.c b/src/resolve/resolved-dns-stub.c index 2bf04db..65c8094 100644 --- a/src/resolve/resolved-dns-stub.c +++ b/src/resolve/resolved-dns-stub.c @@ -421,8 +421,9 @@ static int manager_dns_stub_udp_fd(Manager *m) { return r; /* Make sure no traffic from outside the local host can leak to onto this socket */ - if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, "lo", 3) < 0) - return -errno; + r = socket_bind_to_ifindex(fd, LOOPBACK_IFINDEX); + if (r < 0) + return r; if (bind(fd, &sa.sa, sizeof(sa.in)) < 0) return -errno; @@ -514,8 +515,9 @@ static int manager_dns_stub_tcp_fd(Manager *m) { return r; /* Make sure no traffic from outside the local host can leak to onto this socket */ - if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, "lo", 3) < 0) - return -errno; + r = socket_bind_to_ifindex(fd, LOOPBACK_IFINDEX); + if (r < 0) + return r; if (bind(fd, &sa.sa, sizeof(sa.in)) < 0) return -errno; -- 2.7.4