X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fnss-myhostname%2Fnetlink.c;h=e9518e32139536099e45182bf1cfbdf552337e4f;hb=5502f0d97185218b93f01f9fe979a71bae38e213;hp=b1ef912c8ac1d268f683a31b919ab270f8477eb2;hpb=7464b33d804c4a7295cc576574e8586a173b5f76;p=elogind.git diff --git a/src/nss-myhostname/netlink.c b/src/nss-myhostname/netlink.c index b1ef912c8..e9518e321 100644 --- a/src/nss-myhostname/netlink.c +++ b/src/nss-myhostname/netlink.c @@ -4,6 +4,7 @@ This file is part of systemd. Copyright 2008-2011 Lennart Poettering + Copyright 2014 Tom Gundersen systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -19,187 +20,129 @@ along with systemd; If not, see . ***/ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - +#include "sd-rtnl.h" +#include "rtnl-util.h" +#include "macro.h" #include "ifconf.h" -#define SEQ 4711 - -static int read_reply(int fd, struct address **list, unsigned *n_list) { - ssize_t bytes; - struct cmsghdr *cmsg; - struct ucred *ucred; - struct nlmsghdr *p; - uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred))]; - struct { - struct nlmsghdr hdr; - struct ifaddrmsg ifaddrmsg; - uint8_t payload[16*1024]; - } resp; - struct iovec iov = { - .iov_base = &resp, - .iov_len = sizeof(resp), - }; - struct msghdr msg = { - .msg_name = NULL, - .msg_namelen = 0, - .msg_iov = &iov, - .msg_iovlen = 1, - .msg_control = cred_buffer, - .msg_controllen = sizeof(cred_buffer), - .msg_flags = 0, - }; - - assert(fd >= 0); - assert(list); - - bytes = recvmsg(fd, &msg, 0); - if (bytes < 0) - return -errno; +static int address_compare(const void *_a, const void *_b) { + const struct address *a = _a, *b = _b; - cmsg = CMSG_FIRSTHDR(&msg); - if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) - return -EIO; + /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */ - ucred = (struct ucred*) CMSG_DATA(cmsg); - if (ucred->uid != 0 || ucred->pid != 0) - return 0; + if (a->scope < b->scope) + return -1; + if (a->scope > b->scope) + return 1; - for (p = &resp.hdr; bytes > 0; p = NLMSG_NEXT(p, bytes)) { - struct ifaddrmsg *ifaddrmsg; - struct rtattr *a; - size_t l; - void *local = NULL, *address = NULL; + if (a->family == AF_INET && b->family == AF_INET6) + return -1; + if (a->family == AF_INET6 && b->family == AF_INET) + return 1; - if (!NLMSG_OK(p, (size_t) bytes)) - return -EIO; + if (a->ifindex < b->ifindex) + return -1; + if (a->ifindex > b->ifindex) + return 1; - if (p->nlmsg_seq != SEQ) - continue; - - if (p->nlmsg_type == NLMSG_DONE) - return 1; + return 0; +} - if (p->nlmsg_type == NLMSG_ERROR) { - struct nlmsgerr *nlmsgerr; +int ifconf_acquire_addresses(struct address **_list, unsigned *_n_list) { + _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; + _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL; + _cleanup_free_ struct address *list = NULL; + size_t n_list = 0, n_allocated = 0; + sd_rtnl_message *m; + int r; + + r = sd_rtnl_open(&rtnl, 0); + if (r < 0) + return r; - nlmsgerr = NLMSG_DATA(p); - return -nlmsgerr->error; - } + r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC); + if (r < 0) + return r; - if (p->nlmsg_type != RTM_NEWADDR) - continue; + r = sd_rtnl_call(rtnl, req, 0, &reply); + if (r < 0) + return r; - ifaddrmsg = NLMSG_DATA(p); + for (m = reply; m; m = sd_rtnl_message_next(m)) { + struct address *a; + unsigned char flags; + uint16_t type; - if (ifaddrmsg->ifa_family != AF_INET && - ifaddrmsg->ifa_family != AF_INET6) - continue; + r = sd_rtnl_message_get_errno(m); + if (r < 0) + return r; - if (ifaddrmsg->ifa_scope == RT_SCOPE_HOST || - ifaddrmsg->ifa_scope == RT_SCOPE_NOWHERE) - continue; + r = sd_rtnl_message_get_type(m, &type); + if (r < 0) + return r; - if (ifaddrmsg->ifa_flags & IFA_F_DEPRECATED) + if (type != RTM_NEWADDR) continue; - l = NLMSG_PAYLOAD(p, sizeof(struct ifaddrmsg)); - a = IFA_RTA(ifaddrmsg); - - while (RTA_OK(a, l)) { + r = sd_rtnl_message_addr_get_flags(m, &flags); + if (r < 0) + return r; - if (a->rta_type == IFA_ADDRESS) - address = RTA_DATA(a); - else if (a->rta_type == IFA_LOCAL) - local = RTA_DATA(a); - - a = RTA_NEXT(a, l); - } - - if (local) - address = local; - - if (!address) + if (flags & IFA_F_DEPRECATED) continue; - *list = realloc(*list, (*n_list+1) * sizeof(struct address)); - if (!*list) + if (!GREEDY_REALLOC(list, n_allocated, n_list+1)) return -ENOMEM; - (*list)[*n_list].family = ifaddrmsg->ifa_family; - (*list)[*n_list].scope = ifaddrmsg->ifa_scope; - memcpy((*list)[*n_list].address, - address, ifaddrmsg->ifa_family == AF_INET ? 4 : 16); - (*list)[*n_list].ifindex = ifaddrmsg->ifa_index; + a = list + n_list; - (*n_list)++; - } + r = sd_rtnl_message_addr_get_scope(m, &a->scope); + if (r < 0) + return r; - return 0; -} - - -int ifconf_acquire_addresses(struct address **_list, unsigned *_n_list) { + if (a->scope == RT_SCOPE_HOST || a->scope == RT_SCOPE_NOWHERE) + continue; - struct { - struct nlmsghdr hdr; - struct rtgenmsg gen; - } req = { { - .nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)), - .nlmsg_type = RTM_GETADDR, - .nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP|NLM_F_ACK, - .nlmsg_seq = SEQ, - .nlmsg_pid = 0, - }, { - .rtgen_family = AF_UNSPEC, + r = sd_rtnl_message_addr_get_family(m, &a->family); + if (r < 0) + return r; + + switch (a->family) { + + case AF_INET: + r = sd_rtnl_message_read_in_addr(m, IFA_LOCAL, &a->address.in); + if (r < 0) { + r = sd_rtnl_message_read_in_addr(m, IFA_ADDRESS, &a->address.in); + if (r < 0) + continue; + } + break; + + case AF_INET6: + r = sd_rtnl_message_read_in6_addr(m, IFA_LOCAL, &a->address.in6); + if (r < 0) { + r = sd_rtnl_message_read_in6_addr(m, IFA_ADDRESS, &a->address.in6); + if (r < 0) + continue; + } + break; + + default: + continue; } - }; - int r, on = 1; - struct address *list = NULL; - unsigned n_list = 0; - int fd; - - fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); - if (fd < 0) - return -errno; - - if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) { - r = -errno; - goto finish; - } - if (send(fd, &req, req.hdr.nlmsg_len, 0) < 0) { - r = -errno; - goto finish; - } + r = sd_rtnl_message_addr_get_ifindex(m, &a->ifindex); + if (r < 0) + return r; - while((r = read_reply(fd, &list, &n_list)) == 0) - ; - -finish: - close(fd); - - if (r < 0) { - free(list); - return r; - } + n_list++; + }; - qsort(list, n_list, sizeof(struct address), address_compare); + if (n_list) + qsort(list, n_list, sizeof(struct address), address_compare); *_list = list; + list = NULL; *_n_list = n_list; return 0;