1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of nss-myhostname.
6 Copyright 2008-2011 Lennart Poettering
8 nss-myhostname is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
13 nss-myhostname is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with nss-myhostname; If not, see
20 <http://www.gnu.org/licenses/>.
23 #include <sys/socket.h>
25 #include <asm/types.h>
27 #include <linux/netlink.h>
28 #include <linux/rtnetlink.h>
33 #include <arpa/inet.h>
40 static int address_compare(const void *_a, const void *_b) {
41 const struct address *a = _a, *b = _b;
43 /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */
45 if (a->scope < b->scope)
47 if (a->scope > b->scope)
50 if (a->family == AF_INET && b->family == AF_INET6)
52 if (a->family == AF_INET6 && b->family == AF_INET)
55 if (a->ifindex < b->ifindex)
57 if (a->ifindex > b->ifindex)
63 int netlink_acquire_addresses(struct address **_list, unsigned *_n_list) {
72 struct address *list = NULL;
75 fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
79 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
84 memset(&req, 0, sizeof(req));
85 req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
86 req.hdr.nlmsg_type = RTM_GETADDR;
87 req.hdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP|NLM_F_ACK;
88 req.hdr.nlmsg_seq = seq;
89 req.hdr.nlmsg_pid = 0;
91 gen = NLMSG_DATA(&req.hdr);
92 gen->rtgen_family = AF_UNSPEC;
94 if (send(fd, &req, req.hdr.nlmsg_len, 0) < 0) {
102 struct cmsghdr *cmsg;
106 uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
109 struct ifaddrmsg ifaddrmsg;
110 uint8_t payload[16*1024];
113 memset(&iov, 0, sizeof(iov));
114 iov.iov_base = &resp;
115 iov.iov_len = sizeof(resp);
117 memset(&msg, 0, sizeof(msg));
122 msg.msg_control = cred_buffer;
123 msg.msg_controllen = sizeof(cred_buffer);
126 bytes = recvmsg(fd, &msg, 0);
132 cmsg = CMSG_FIRSTHDR(&msg);
133 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
138 ucred = (struct ucred*) CMSG_DATA(cmsg);
139 if (ucred->uid != 0 || ucred->pid != 0)
142 for (p = &resp.hdr; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
143 struct ifaddrmsg *ifaddrmsg;
146 void *local = NULL, *address = NULL;
148 if (!NLMSG_OK(p, (size_t) bytes)) {
153 if (p->nlmsg_seq != seq)
156 if (p->nlmsg_type == NLMSG_DONE) {
161 if (p->nlmsg_type == NLMSG_ERROR) {
162 struct nlmsgerr *nlmsgerr;
164 nlmsgerr = NLMSG_DATA(p);
165 r = -nlmsgerr->error;
169 if (p->nlmsg_type != RTM_NEWADDR)
172 ifaddrmsg = NLMSG_DATA(p);
174 if (ifaddrmsg->ifa_family != AF_INET &&
175 ifaddrmsg->ifa_family != AF_INET6)
178 if (ifaddrmsg->ifa_scope == RT_SCOPE_HOST ||
179 ifaddrmsg->ifa_scope == RT_SCOPE_NOWHERE)
182 if (ifaddrmsg->ifa_flags & IFA_F_DEPRECATED)
185 l = NLMSG_PAYLOAD(p, sizeof(struct ifaddrmsg));
186 a = IFA_RTA(ifaddrmsg);
188 while (RTA_OK(a, l)) {
190 if (a->rta_type == IFA_ADDRESS)
191 address = RTA_DATA(a);
192 else if (a->rta_type == IFA_LOCAL)
204 list = realloc(list, (n_list+1) * sizeof(struct address));
210 list[n_list].family = ifaddrmsg->ifa_family;
211 list[n_list].scope = ifaddrmsg->ifa_scope;
212 memcpy(list[n_list].address, address, ifaddrmsg->ifa_family == AF_INET ? 4 : 16);
213 list[n_list].ifindex = ifaddrmsg->ifa_index;
225 qsort(list, n_list, sizeof(struct address), address_compare);