chiark / gitweb /
nss-myhostname: integrate documentation
[elogind.git] / src / nss-myhostname / netlink.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2008-2011 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   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.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <asm/types.h>
25 #include <inttypes.h>
26 #include <linux/netlink.h>
27 #include <linux/rtnetlink.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <arpa/inet.h>
33 #include <unistd.h>
34 #include <inttypes.h>
35 #include <stdlib.h>
36
37 #include "ifconf.h"
38
39 int ifconf_acquire_addresses(struct address **_list, unsigned *_n_list) {
40
41         struct {
42                 struct nlmsghdr hdr;
43                 struct rtgenmsg gen;
44         } req;
45         struct rtgenmsg *gen;
46         int fd, r, on = 1;
47         uint32_t seq = 4711;
48         struct address *list = NULL;
49         unsigned n_list = 0;
50
51         fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
52         if (fd < 0)
53                 return -errno;
54
55         if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
56                 r = -errno;
57                 goto finish;
58         }
59
60         memset(&req, 0, sizeof(req));
61         req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
62         req.hdr.nlmsg_type = RTM_GETADDR;
63         req.hdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP|NLM_F_ACK;
64         req.hdr.nlmsg_seq = seq;
65         req.hdr.nlmsg_pid = 0;
66
67         gen = NLMSG_DATA(&req.hdr);
68         gen->rtgen_family = AF_UNSPEC;
69
70         if (send(fd, &req, req.hdr.nlmsg_len, 0) < 0) {
71                 r = -errno;
72                 goto finish;
73         }
74
75         for (;;) {
76                 ssize_t bytes;
77                 struct msghdr msg;
78                 struct cmsghdr *cmsg;
79                 struct ucred *ucred;
80                 struct iovec iov;
81                 struct nlmsghdr *p;
82                 uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
83                 struct {
84                         struct nlmsghdr hdr;
85                         struct ifaddrmsg ifaddrmsg;
86                         uint8_t payload[16*1024];
87                 } resp;
88
89                 memset(&iov, 0, sizeof(iov));
90                 iov.iov_base = &resp;
91                 iov.iov_len = sizeof(resp);
92
93                 memset(&msg, 0, sizeof(msg));
94                 msg.msg_name = NULL;
95                 msg.msg_namelen = 0;
96                 msg.msg_iov = &iov;
97                 msg.msg_iovlen = 1;
98                 msg.msg_control = cred_buffer;
99                 msg.msg_controllen = sizeof(cred_buffer);
100                 msg.msg_flags = 0;
101
102                 bytes = recvmsg(fd, &msg, 0);
103                 if (bytes < 0) {
104                         r = -errno;
105                         goto finish;
106                 }
107
108                 cmsg = CMSG_FIRSTHDR(&msg);
109                 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
110                         r = -EIO;
111                         goto finish;
112                 }
113
114                 ucred = (struct ucred*) CMSG_DATA(cmsg);
115                 if (ucred->uid != 0 || ucred->pid != 0)
116                         continue;
117
118                 for (p = &resp.hdr; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
119                         struct ifaddrmsg *ifaddrmsg;
120                         struct rtattr *a;
121                         size_t l;
122                         void *local = NULL, *address = NULL;
123
124                         if (!NLMSG_OK(p, (size_t) bytes)) {
125                                 r = -EIO;
126                                 goto finish;
127                         }
128
129                         if (p->nlmsg_seq != seq)
130                                 continue;
131
132                         if (p->nlmsg_type == NLMSG_DONE) {
133                                 r = 0;
134                                 goto finish;
135                         }
136
137                         if (p->nlmsg_type == NLMSG_ERROR) {
138                                 struct nlmsgerr *nlmsgerr;
139
140                                 nlmsgerr = NLMSG_DATA(p);
141                                 r = -nlmsgerr->error;
142                                 goto finish;
143                         }
144
145                         if (p->nlmsg_type != RTM_NEWADDR)
146                                 continue;
147
148                         ifaddrmsg = NLMSG_DATA(p);
149
150                         if (ifaddrmsg->ifa_family != AF_INET &&
151                             ifaddrmsg->ifa_family != AF_INET6)
152                                 continue;
153
154                         if (ifaddrmsg->ifa_scope == RT_SCOPE_HOST ||
155                             ifaddrmsg->ifa_scope == RT_SCOPE_NOWHERE)
156                                 continue;
157
158                         if (ifaddrmsg->ifa_flags & IFA_F_DEPRECATED)
159                                 continue;
160
161                         l = NLMSG_PAYLOAD(p, sizeof(struct ifaddrmsg));
162                         a = IFA_RTA(ifaddrmsg);
163
164                         while (RTA_OK(a, l)) {
165
166                                 if (a->rta_type == IFA_ADDRESS)
167                                         address = RTA_DATA(a);
168                                 else if (a->rta_type == IFA_LOCAL)
169                                         local = RTA_DATA(a);
170
171                                 a = RTA_NEXT(a, l);
172                         }
173
174                         if (local)
175                                 address = local;
176
177                         if (!address)
178                                 continue;
179
180                         list = realloc(list, (n_list+1) * sizeof(struct address));
181                         if (!list) {
182                                 r = -ENOMEM;
183                                 goto finish;
184                         }
185
186                         list[n_list].family = ifaddrmsg->ifa_family;
187                         list[n_list].scope = ifaddrmsg->ifa_scope;
188                         memcpy(list[n_list].address, address, ifaddrmsg->ifa_family == AF_INET ? 4 : 16);
189                         list[n_list].ifindex = ifaddrmsg->ifa_index;
190
191                         n_list++;
192                 }
193         }
194
195 finish:
196         close(fd);
197
198         if (r < 0)
199                 free(list);
200         else {
201                 qsort(list, n_list, sizeof(struct address), address_compare);
202
203                 *_list = list;
204                 *_n_list = n_list;
205         }
206
207         return r;
208 }