X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcore%2Floopback-setup.c;h=63b15c12009e345ee9343151841ca2a2dd1c8f22;hb=c43b2132f37264600cc26e07c8d85dfdd6c969f0;hp=46c1fc843a56ac8615588cd67efe2a20b349355f;hpb=08e1fb68d78b4adf26cce8387fc428b9e370bcf4;p=elogind.git diff --git a/src/core/loopback-setup.c b/src/core/loopback-setup.c index 46c1fc843..63b15c120 100644 --- a/src/core/loopback-setup.c +++ b/src/core/loopback-setup.c @@ -19,256 +19,73 @@ along with systemd; If not, see . ***/ -#include -#include #include -#include -#include -#include #include -#include -#include -#include -#include "util.h" -#include "macro.h" +#include "sd-rtnl.h" +#include "rtnl-util.h" +#include "missing.h" #include "loopback-setup.h" -#include "socket-util.h" -#define NLMSG_TAIL(nmsg) \ - ((struct rtattr *) (((uint8_t*) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) - -static int add_rtattr(struct nlmsghdr *n, size_t max_length, int type, const void *data, size_t data_length) { - size_t length; - struct rtattr *rta; - - length = RTA_LENGTH(data_length); - - if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length) > max_length) - return -E2BIG; - - rta = NLMSG_TAIL(n); - rta->rta_type = type; - rta->rta_len = length; - memcpy(RTA_DATA(rta), data, data_length); - n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length); - - return 0; -} - -static ssize_t sendto_loop(int fd, const void *buf, size_t buf_len, int flags, const struct sockaddr *sa, socklen_t sa_len) { - - for (;;) { - ssize_t l; - - if ((l = sendto(fd, buf, buf_len, flags, sa, sa_len)) >= 0) - return l; - - if (errno != EINTR) - return -errno; - } -} - -static ssize_t recvfrom_loop(int fd, void *buf, size_t buf_len, int flags, struct sockaddr *sa, socklen_t *sa_len) { - - for (;;) { - ssize_t l; - - if ((l = recvfrom(fd, buf, buf_len, flags, sa, sa_len)) >= 0) - return l; - - if (errno != EINTR) - return -errno; - } -} - -static int add_adresses(int fd, int if_loopback, unsigned *requests) { - union { - struct sockaddr sa; - struct sockaddr_nl nl; - } sa; - union { - struct nlmsghdr header; - uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + - NLMSG_ALIGN(sizeof(struct ifaddrmsg)) + - RTA_LENGTH(sizeof(struct in6_addr))]; - } request; - - struct ifaddrmsg *ifaddrmsg; - uint32_t ipv4_address = htonl(INADDR_LOOPBACK); +static int start_loopback(sd_rtnl *rtnl) { + _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; int r; - zero(request); - - request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); - request.header.nlmsg_type = RTM_NEWADDR; - request.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK; - request.header.nlmsg_seq = *requests + 1; - - ifaddrmsg = NLMSG_DATA(&request.header); - ifaddrmsg->ifa_family = AF_INET; - ifaddrmsg->ifa_prefixlen = 8; - ifaddrmsg->ifa_flags = IFA_F_PERMANENT; - ifaddrmsg->ifa_scope = RT_SCOPE_HOST; - ifaddrmsg->ifa_index = if_loopback; - - if ((r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &ipv4_address, sizeof(ipv4_address))) < 0) + r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX); + if (r < 0) return r; - zero(sa); - sa.nl.nl_family = AF_NETLINK; - - if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0) - return -errno; - (*requests)++; - - if (!socket_ipv6_is_supported()) - return 0; - - request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); - request.header.nlmsg_seq = *requests + 1; - - ifaddrmsg->ifa_family = AF_INET6; - ifaddrmsg->ifa_prefixlen = 128; - - if ((r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &in6addr_loopback, sizeof(in6addr_loopback))) < 0) + r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); + if (r < 0) return r; - if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0) - return -errno; - (*requests)++; - - return 0; -} - -static int start_interface(int fd, int if_loopback, unsigned *requests) { - union { - struct sockaddr sa; - struct sockaddr_nl nl; - } sa; - union { - struct nlmsghdr header; - uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + - NLMSG_ALIGN(sizeof(struct ifinfomsg))]; - } request; - - struct ifinfomsg *ifinfomsg; - - zero(request); - - request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); - request.header.nlmsg_type = RTM_NEWLINK; - request.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK; - request.header.nlmsg_seq = *requests + 1; - - ifinfomsg = NLMSG_DATA(&request.header); - ifinfomsg->ifi_family = AF_UNSPEC; - ifinfomsg->ifi_index = if_loopback; - ifinfomsg->ifi_flags = IFF_UP; - ifinfomsg->ifi_change = IFF_UP; - - zero(sa); - sa.nl.nl_family = AF_NETLINK; - - if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0) - return -errno; - - (*requests)++; + r = sd_rtnl_call(rtnl, req, 0, NULL); + if (r < 0) + return r; return 0; } -static int read_response(int fd, unsigned requests_max) { - union { - struct sockaddr sa; - struct sockaddr_nl nl; - } sa; - union { - struct nlmsghdr header; - uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + - NLMSG_ALIGN(sizeof(struct nlmsgerr))]; - } response; - - ssize_t l; - socklen_t sa_len = sizeof(sa); - struct nlmsgerr *nlmsgerr; - - if ((l = recvfrom_loop(fd, &response, sizeof(response), 0, &sa.sa, &sa_len)) < 0) - return -errno; - - if (sa_len != sizeof(sa.nl) || - sa.nl.nl_family != AF_NETLINK) - return -EIO; - - if (sa.nl.nl_pid != 0) - return 0; - - if ((size_t) l < sizeof(struct nlmsghdr)) - return -EIO; - - if (response.header.nlmsg_type != NLMSG_ERROR || - (pid_t) response.header.nlmsg_pid != getpid() || - response.header.nlmsg_seq >= requests_max) - return 0; +static bool check_loopback(sd_rtnl *rtnl) { + _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; + unsigned flags; + int r; - if ((size_t) l < NLMSG_LENGTH(sizeof(struct nlmsgerr)) || - response.header.nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) - return -EIO; + r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX); + if (r < 0) + return false; - nlmsgerr = NLMSG_DATA(&response.header); + r = sd_rtnl_call(rtnl, req, 0, &reply); + if (r < 0) + return false; - if (nlmsgerr->error < 0 && nlmsgerr->error != -EEXIST) { - log_warning("Netlink failure for request %i: %s", response.header.nlmsg_seq, strerror(-nlmsgerr->error)); - return nlmsgerr->error; - } + r = sd_rtnl_message_link_get_flags(reply, &flags); + if (r < 0) + return false; - return response.header.nlmsg_seq; + return flags & IFF_UP; } int loopback_setup(void) { - int r, if_loopback; - union { - struct sockaddr sa; - struct sockaddr_nl nl; - struct sockaddr_storage storage; - } sa; - unsigned requests = 0, i; - int fd; - - errno = 0; - if ((if_loopback = (int) if_nametoindex("lo")) <= 0) - return errno ? -errno : -ENODEV; - - if ((fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) - return -errno; - - zero(sa); - sa.nl.nl_family = AF_NETLINK; + _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL; + int r; - if (bind(fd, &sa.sa, sizeof(sa)) < 0) { - r = -errno; - goto finish; - } + r = sd_rtnl_open(&rtnl, 0); + if (r < 0) + return r; - if ((r = add_adresses(fd, if_loopback, &requests)) < 0) - goto finish; + r = start_loopback(rtnl); + if (r < 0) { - if ((r = start_interface(fd, if_loopback, &requests)) < 0) - goto finish; + /* If we lack the permissions to configure the + * loopback device, but we find it to be already + * configured, let's exit cleanly, in order to + * supported unprivileged containers. */ + if (r == -EPERM && check_loopback(rtnl)) + return 0; - for (i = 0; i < requests; i++) { - if ((r = read_response(fd, requests)) < 0) - goto finish; + return log_warning_errno(r, "Failed to configure loopback device: %m"); } - r = 0; - -finish: - if (r < 0) - log_warning("Failed to configure loopback device: %s", strerror(-r)); - - if (fd >= 0) - close_nointr_nofail(fd); - - return r; + return 0; }