]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/ifaddrs.c
Create more sockets with SOCK_CLOEXEC [BZ #15722]
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / ifaddrs.c
CommitLineData
e0c09a43 1/* getifaddrs -- get names and addresses of all network interfaces
bfff8b1b 2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
e0c09a43
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
e0c09a43 18
c63d8f80 19#include <alloca.h>
e0c09a43
UD
20#include <assert.h>
21#include <errno.h>
22#include <ifaddrs.h>
23#include <net/if.h>
24#include <netinet/in.h>
25#include <netpacket/packet.h>
26#include <stdbool.h>
c63d8f80 27#include <stdint.h>
e0c09a43
UD
28#include <stdlib.h>
29#include <string.h>
30#include <sys/ioctl.h>
31#include <sys/socket.h>
32#include <sysdep.h>
33#include <time.h>
34#include <unistd.h>
35
f5164429 36#include "netlinkaccess.h"
e0c09a43 37
e0c09a43 38
c5671698
UD
39/* There is a problem with this type. The address length for
40 Infiniband sockets is much longer than the 8 bytes allocated in the
41 sockaddr_ll definition. Hence we use here a special
42 definition. */
43struct sockaddr_ll_max
44 {
45 unsigned short int sll_family;
46 unsigned short int sll_protocol;
47 int sll_ifindex;
48 unsigned short int sll_hatype;
49 unsigned char sll_pkttype;
50 unsigned char sll_halen;
51 unsigned char sll_addr[24];
52 };
53
54
e0c09a43
UD
55/* struct to hold the data for one ifaddrs entry, so we can allocate
56 everything at once. */
57struct ifaddrs_storage
58{
59 struct ifaddrs ifa;
60 union
61 {
62 /* Save space for the biggest of the four used sockaddr types and
63 avoid a lot of casts. */
64 struct sockaddr sa;
c5671698 65 struct sockaddr_ll_max sl;
e0c09a43
UD
66 struct sockaddr_in s4;
67 struct sockaddr_in6 s6;
68 } addr, netmask, broadaddr;
69 char name[IF_NAMESIZE + 1];
70};
71
72
f5164429
UD
73void
74__netlink_free_handle (struct netlink_handle *h)
e0c09a43
UD
75{
76 struct netlink_res *ptr;
77 int saved_errno = errno;
78
79 ptr = h->nlm_list;
80 while (ptr != NULL)
81 {
82 struct netlink_res *tmpptr;
83
e0c09a43
UD
84 tmpptr = ptr->next;
85 free (ptr);
86 ptr = tmpptr;
87 }
88
f5164429 89 __set_errno (saved_errno);
e0c09a43
UD
90}
91
92
c63d8f80 93static int
f5164429 94__netlink_sendreq (struct netlink_handle *h, int type)
e0c09a43 95{
429bb183 96 struct req
e0c09a43
UD
97 {
98 struct nlmsghdr nlh;
99 struct rtgenmsg g;
429bb183 100 char pad[0];
e0c09a43
UD
101 } req;
102 struct sockaddr_nl nladdr;
103
104 if (h->seq == 0)
105 h->seq = time (NULL);
106
107 req.nlh.nlmsg_len = sizeof (req);
108 req.nlh.nlmsg_type = type;
109 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
110 req.nlh.nlmsg_pid = 0;
111 req.nlh.nlmsg_seq = h->seq;
112 req.g.rtgen_family = AF_UNSPEC;
429bb183
UD
113 if (sizeof (req) != offsetof (struct req, pad))
114 memset (req.pad, '\0', sizeof (req) - offsetof (struct req, pad));
e0c09a43
UD
115
116 memset (&nladdr, '\0', sizeof (nladdr));
117 nladdr.nl_family = AF_NETLINK;
118
153da599
UD
119 return TEMP_FAILURE_RETRY (__sendto (h->fd, (void *) &req, sizeof (req), 0,
120 (struct sockaddr *) &nladdr,
121 sizeof (nladdr)));
e0c09a43
UD
122}
123
124
f5164429 125int
c63d8f80 126__netlink_request (struct netlink_handle *h, int type)
e0c09a43
UD
127{
128 struct netlink_res *nlm_next;
e0c09a43
UD
129 struct sockaddr_nl nladdr;
130 struct nlmsghdr *nlmh;
c63d8f80 131 ssize_t read_len;
e0c09a43 132 bool done = false;
c63d8f80 133
6cb988fa
UD
134#ifdef PAGE_SIZE
135 /* Help the compiler optimize out the malloc call if PAGE_SIZE
136 is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
137 const size_t buf_size = PAGE_SIZE;
138#else
139 const size_t buf_size = __getpagesize ();
140#endif
141 bool use_malloc = false;
142 char *buf;
11bf311e 143
6cb988fa
UD
144 if (__libc_use_alloca (buf_size))
145 buf = alloca (buf_size);
c63d8f80
UD
146 else
147 {
6cb988fa 148 buf = malloc (buf_size);
c63d8f80
UD
149 if (buf != NULL)
150 use_malloc = true;
151 else
152 goto out_fail;
153 }
154
6cb988fa 155 struct iovec iov = { buf, buf_size };
c63d8f80 156
6cb988fa
UD
157 if (__netlink_sendreq (h, type) < 0)
158 goto out_fail;
e0c09a43
UD
159
160 while (! done)
161 {
162 struct msghdr msg =
163 {
af7f7c7e
AZ
164 .msg_name = (void *) &nladdr,
165 .msg_namelen = sizeof (nladdr),
166 .msg_iov = &iov,
167 .msg_iovlen = 1,
168 .msg_control = NULL,
169 .msg_controllen = 0,
170 .msg_flags = 0
e0c09a43
UD
171 };
172
153da599 173 read_len = TEMP_FAILURE_RETRY (__recvmsg (h->fd, &msg, 0));
2eecc8af 174 __netlink_assert_response (h->fd, read_len);
e0c09a43 175 if (read_len < 0)
c63d8f80 176 goto out_fail;
e0c09a43 177
c63d8f80
UD
178 if (nladdr.nl_pid != 0)
179 continue;
e0c09a43 180
a1ffb40e 181 if (__glibc_unlikely (msg.msg_flags & MSG_TRUNC))
6cb988fa 182 goto out_fail;
c63d8f80
UD
183
184 size_t count = 0;
185 size_t remaining_len = read_len;
e0c09a43 186 for (nlmh = (struct nlmsghdr *) buf;
c63d8f80
UD
187 NLMSG_OK (nlmh, remaining_len);
188 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, remaining_len))
e0c09a43 189 {
c63d8f80 190 if ((pid_t) nlmh->nlmsg_pid != h->pid
25ce4c6b 191 || nlmh->nlmsg_seq != h->seq)
e0c09a43
UD
192 continue;
193
c63d8f80 194 ++count;
e0c09a43
UD
195 if (nlmh->nlmsg_type == NLMSG_DONE)
196 {
5bdd77cb 197 /* We found the end, leave the loop. */
e0c09a43
UD
198 done = true;
199 break;
200 }
201 if (nlmh->nlmsg_type == NLMSG_ERROR)
202 {
203 struct nlmsgerr *nlerr = (struct nlmsgerr *) NLMSG_DATA (nlmh);
204 if (nlmh->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
205 errno = EIO;
206 else
207 errno = -nlerr->error;
c63d8f80 208 goto out_fail;
e0c09a43
UD
209 }
210 }
c63d8f80
UD
211
212 /* If there was nothing with the expected nlmsg_pid and nlmsg_seq,
213 there is no point to record it. */
214 if (count == 0)
215 continue;
216
217 nlm_next = (struct netlink_res *) malloc (sizeof (struct netlink_res)
218 + read_len);
219 if (nlm_next == NULL)
220 goto out_fail;
221 nlm_next->next = NULL;
222 nlm_next->nlh = memcpy (nlm_next + 1, buf, read_len);
223 nlm_next->size = read_len;
224 nlm_next->seq = h->seq;
225 if (h->nlm_list == NULL)
226 h->nlm_list = nlm_next;
227 else
228 h->end_ptr->next = nlm_next;
229 h->end_ptr = nlm_next;
e0c09a43 230 }
c63d8f80
UD
231
232 if (use_malloc)
233 free (buf);
e0c09a43 234 return 0;
c63d8f80
UD
235
236out_fail:
237 if (use_malloc)
238 free (buf);
239 return -1;
e0c09a43
UD
240}
241
242
f5164429
UD
243void
244__netlink_close (struct netlink_handle *h)
e0c09a43
UD
245{
246 /* Don't modify errno. */
247 INTERNAL_SYSCALL_DECL (err);
248 (void) INTERNAL_SYSCALL (close, err, 1, h->fd);
249}
250
251
252/* Open a NETLINK socket. */
f5164429
UD
253int
254__netlink_open (struct netlink_handle *h)
e0c09a43
UD
255{
256 struct sockaddr_nl nladdr;
257
2f83a729 258 h->fd = __socket (PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
e0c09a43 259 if (h->fd < 0)
f5164429 260 goto out;
e0c09a43
UD
261
262 memset (&nladdr, '\0', sizeof (nladdr));
263 nladdr.nl_family = AF_NETLINK;
153da599 264 if (__bind (h->fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) < 0)
e0c09a43 265 {
332afd9e 266 close_and_out:
f5164429
UD
267 __netlink_close (h);
268 out:
e0c09a43
UD
269 return -1;
270 }
332afd9e
UD
271 /* Determine the ID the kernel assigned for this netlink connection.
272 It is not necessarily the PID if there is more than one socket
273 open. */
274 socklen_t addr_len = sizeof (nladdr);
275 if (__getsockname (h->fd, (struct sockaddr *) &nladdr, &addr_len) < 0)
276 goto close_and_out;
277 h->pid = nladdr.nl_pid;
e0c09a43
UD
278 return 0;
279}
280
281
282/* We know the number of RTM_NEWLINK entries, so we reserve the first
283 # of entries for this type. All RTM_NEWADDR entries have an index
284 pointer to the RTM_NEWLINK entry. To find the entry, create
285 a table to map kernel index entries to our index numbers.
286 Since we get at first all RTM_NEWLINK entries, it can never happen
287 that a RTM_NEWADDR index is not known to this map. */
288static int
31dfab9e
UD
289internal_function
290map_newlink (int index, struct ifaddrs_storage *ifas, int *map, int max)
e0c09a43
UD
291{
292 int i;
293
294 for (i = 0; i < max; i++)
295 {
296 if (map[i] == -1)
297 {
298 map[i] = index;
31dfab9e
UD
299 if (i > 0)
300 ifas[i - 1].ifa.ifa_next = &ifas[i].ifa;
e0c09a43
UD
301 return i;
302 }
303 else if (map[i] == index)
304 return i;
305 }
b8b14c4c 306
6f65e668 307 /* This means interfaces changed between the reading of the
b8b14c4c
UD
308 RTM_GETLINK and RTM_GETADDR information. We have to repeat
309 everything. */
310 return -1;
e0c09a43
UD
311}
312
313
314/* Create a linked list of `struct ifaddrs' structures, one for each
315 network interface on the host machine. If successful, store the
667712b8 316 list in *IFAP and return 0. On errors, return -1 and set `errno'. */
b8b14c4c
UD
317static int
318getifaddrs_internal (struct ifaddrs **ifap)
e0c09a43
UD
319{
320 struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
321 struct netlink_res *nlp;
322 struct ifaddrs_storage *ifas;
323 unsigned int i, newlink, newaddr, newaddr_idx;
324 int *map_newlink_data;
325 size_t ifa_data_size = 0; /* Size to allocate for all ifa_data. */
c63d8f80 326 char *ifa_data_ptr; /* Pointer to the unused part of memory for
e0c09a43 327 ifa_data. */
5bdd77cb 328 int result = 0;
e0c09a43 329
e7c8359e 330 *ifap = NULL;
e0c09a43 331
89b4b02f
JM
332 if (__netlink_open (&nh) < 0)
333 return -1;
e0c09a43 334
e0c09a43 335 /* Tell the kernel that we wish to get a list of all
c63d8f80
UD
336 active interfaces, collect all data for every interface. */
337 if (__netlink_request (&nh, RTM_GETLINK) < 0)
e0c09a43 338 {
5bdd77cb
UD
339 result = -1;
340 goto exit_free;
e0c09a43
UD
341 }
342
e0c09a43 343 /* Now ask the kernel for all addresses which are assigned
c63d8f80
UD
344 to an interface and collect all data for every interface.
345 Since we store the addresses after the interfaces in the
346 list, we will later always find the interface before the
347 corresponding addresses. */
e0c09a43 348 ++nh.seq;
c63d8f80 349 if (__netlink_request (&nh, RTM_GETADDR) < 0)
e0c09a43 350 {
5bdd77cb
UD
351 result = -1;
352 goto exit_free;
e0c09a43
UD
353 }
354
355 /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate
356 enough memory. */
357 newlink = newaddr = 0;
358 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
359 {
360 struct nlmsghdr *nlh;
361 size_t size = nlp->size;
362
363 if (nlp->nlh == NULL)
364 continue;
365
366 /* Walk through all entries we got from the kernel and look, which
c63d8f80 367 message type they contain. */
e0c09a43
UD
368 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
369 {
f5164429 370 /* Check if the message is what we want. */
e0c09a43
UD
371 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
372 continue;
373
374 if (nlh->nlmsg_type == NLMSG_DONE)
375 break; /* ok */
376
377 if (nlh->nlmsg_type == RTM_NEWLINK)
378 {
379 /* A RTM_NEWLINK message can have IFLA_STATS data. We need to
380 know the size before creating the list to allocate enough
381 memory. */
382 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
383 struct rtattr *rta = IFLA_RTA (ifim);
384 size_t rtasize = IFLA_PAYLOAD (nlh);
385
386 while (RTA_OK (rta, rtasize))
387 {
388 size_t rta_payload = RTA_PAYLOAD (rta);
389
390 if (rta->rta_type == IFLA_STATS)
391 {
392 ifa_data_size += rta_payload;
393 break;
394 }
395 else
396 rta = RTA_NEXT (rta, rtasize);
397 }
398 ++newlink;
399 }
400 else if (nlh->nlmsg_type == RTM_NEWADDR)
401 ++newaddr;
402 }
403 }
404
405 /* Return if no interface is up. */
406 if ((newlink + newaddr) == 0)
5bdd77cb 407 goto exit_free;
e0c09a43 408
e0c09a43
UD
409 /* Allocate memory for all entries we have and initialize next
410 pointer. */
411 ifas = (struct ifaddrs_storage *) calloc (1,
412 (newlink + newaddr)
413 * sizeof (struct ifaddrs_storage)
414 + ifa_data_size);
415 if (ifas == NULL)
416 {
5bdd77cb
UD
417 result = -1;
418 goto exit_free;
e0c09a43
UD
419 }
420
31dfab9e
UD
421 /* Table for mapping kernel index to entry in our list. */
422 map_newlink_data = alloca (newlink * sizeof (int));
423 memset (map_newlink_data, '\xff', newlink * sizeof (int));
424
5bdd77cb 425 ifa_data_ptr = (char *) &ifas[newlink + newaddr];
e0c09a43
UD
426 newaddr_idx = 0; /* Counter for newaddr index. */
427
428 /* Walk through the list of data we got from the kernel. */
429 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
430 {
431 struct nlmsghdr *nlh;
432 size_t size = nlp->size;
433
434 if (nlp->nlh == NULL)
435 continue;
436
437 /* Walk through one message and look at the type: If it is our
438 message, we need RTM_NEWLINK/RTM_NEWADDR and stop if we reach
439 the end or we find the end marker (in this case we ignore the
440 following data. */
441 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
442 {
443 int ifa_index = 0;
444
5bdd77cb 445 /* Check if the message is the one we want */
e0c09a43
UD
446 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
447 continue;
448
449 if (nlh->nlmsg_type == NLMSG_DONE)
450 break; /* ok */
5bdd77cb
UD
451
452 if (nlh->nlmsg_type == RTM_NEWLINK)
e0c09a43
UD
453 {
454 /* We found a new interface. Now extract everything from the
455 interface data we got and need. */
456 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
457 struct rtattr *rta = IFLA_RTA (ifim);
458 size_t rtasize = IFLA_PAYLOAD (nlh);
459
5bdd77cb 460 /* Interfaces are stored in the first "newlink" entries
e0c09a43
UD
461 of our list, starting in the order as we got from the
462 kernel. */
c63d8f80 463 ifa_index = map_newlink (ifim->ifi_index - 1, ifas,
e0c09a43 464 map_newlink_data, newlink);
a1ffb40e 465 if (__glibc_unlikely (ifa_index == -1))
b8b14c4c
UD
466 {
467 try_again:
468 result = -EAGAIN;
469 free (ifas);
470 goto exit_free;
471 }
e0c09a43
UD
472 ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags;
473
474 while (RTA_OK (rta, rtasize))
475 {
476 char *rta_data = RTA_DATA (rta);
477 size_t rta_payload = RTA_PAYLOAD (rta);
478
479 switch (rta->rta_type)
480 {
481 case IFLA_ADDRESS:
31dfab9e
UD
482 if (rta_payload <= sizeof (ifas[ifa_index].addr))
483 {
484 ifas[ifa_index].addr.sl.sll_family = AF_PACKET;
485 memcpy (ifas[ifa_index].addr.sl.sll_addr,
486 (char *) rta_data, rta_payload);
487 ifas[ifa_index].addr.sl.sll_halen = rta_payload;
488 ifas[ifa_index].addr.sl.sll_ifindex
489 = ifim->ifi_index;
490 ifas[ifa_index].addr.sl.sll_hatype = ifim->ifi_type;
491
492 ifas[ifa_index].ifa.ifa_addr
493 = &ifas[ifa_index].addr.sa;
494 }
e0c09a43
UD
495 break;
496
497 case IFLA_BROADCAST:
31dfab9e
UD
498 if (rta_payload <= sizeof (ifas[ifa_index].broadaddr))
499 {
500 ifas[ifa_index].broadaddr.sl.sll_family = AF_PACKET;
501 memcpy (ifas[ifa_index].broadaddr.sl.sll_addr,
502 (char *) rta_data, rta_payload);
503 ifas[ifa_index].broadaddr.sl.sll_halen = rta_payload;
504 ifas[ifa_index].broadaddr.sl.sll_ifindex
505 = ifim->ifi_index;
506 ifas[ifa_index].broadaddr.sl.sll_hatype
507 = ifim->ifi_type;
e0c09a43 508
31dfab9e
UD
509 ifas[ifa_index].ifa.ifa_broadaddr
510 = &ifas[ifa_index].broadaddr.sa;
511 }
e0c09a43
UD
512 break;
513
514 case IFLA_IFNAME: /* Name of Interface */
515 if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name))
516 {
517 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
31dfab9e
UD
518 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
519 rta_payload) = '\0';
e0c09a43
UD
520 }
521 break;
522
523 case IFLA_STATS: /* Statistics of Interface */
524 ifas[ifa_index].ifa.ifa_data = ifa_data_ptr;
525 ifa_data_ptr += rta_payload;
526 memcpy (ifas[ifa_index].ifa.ifa_data, rta_data,
527 rta_payload);
528 break;
529
530 case IFLA_UNSPEC:
531 break;
532 case IFLA_MTU:
533 break;
534 case IFLA_LINK:
535 break;
536 case IFLA_QDISC:
537 break;
538 default:
539 break;
540 }
541
542 rta = RTA_NEXT (rta, rtasize);
543 }
544 }
545 else if (nlh->nlmsg_type == RTM_NEWADDR)
546 {
547 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh);
548 struct rtattr *rta = IFA_RTA (ifam);
549 size_t rtasize = IFA_PAYLOAD (nlh);
550
551 /* New Addresses are stored in the order we got them from
31dfab9e 552 the kernel after the interfaces. Theoretically it is possible
e0c09a43
UD
553 that we have holes in the interface part of the list,
554 but we always have already the interface for this address. */
555 ifa_index = newlink + newaddr_idx;
b8b14c4c
UD
556 int idx = map_newlink (ifam->ifa_index - 1, ifas,
557 map_newlink_data, newlink);
a1ffb40e 558 if (__glibc_unlikely (idx == -1))
b8b14c4c
UD
559 goto try_again;
560 ifas[ifa_index].ifa.ifa_flags = ifas[idx].ifa.ifa_flags;
31dfab9e
UD
561 if (ifa_index > 0)
562 ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa;
e0c09a43
UD
563 ++newaddr_idx;
564
565 while (RTA_OK (rta, rtasize))
566 {
567 char *rta_data = RTA_DATA (rta);
568 size_t rta_payload = RTA_PAYLOAD (rta);
569
570 switch (rta->rta_type)
571 {
572 case IFA_ADDRESS:
573 {
574 struct sockaddr *sa;
575
576 if (ifas[ifa_index].ifa.ifa_addr != NULL)
577 {
578 /* In a point-to-poing network IFA_ADDRESS
579 contains the destination address, local
580 address is supplied in IFA_LOCAL attribute.
581 destination address and broadcast address
582 are stored in an union, so it doesn't matter
583 which name we use. */
584 ifas[ifa_index].ifa.ifa_broadaddr
585 = &ifas[ifa_index].broadaddr.sa;
586 sa = &ifas[ifa_index].broadaddr.sa;
587 }
588 else
589 {
590 ifas[ifa_index].ifa.ifa_addr
591 = &ifas[ifa_index].addr.sa;
592 sa = &ifas[ifa_index].addr.sa;
593 }
594
595 sa->sa_family = ifam->ifa_family;
596
597 switch (ifam->ifa_family)
598 {
599 case AF_INET:
31dfab9e
UD
600 /* Size must match that of an address for IPv4. */
601 if (rta_payload == 4)
602 memcpy (&((struct sockaddr_in *) sa)->sin_addr,
603 rta_data, rta_payload);
e0c09a43
UD
604 break;
605
606 case AF_INET6:
31dfab9e
UD
607 /* Size must match that of an address for IPv6. */
608 if (rta_payload == 16)
609 {
610 memcpy (&((struct sockaddr_in6 *) sa)->sin6_addr,
611 rta_data, rta_payload);
612 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
613 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
614 ((struct sockaddr_in6 *) sa)->sin6_scope_id
5d79df57 615 = ifam->ifa_index;
31dfab9e 616 }
e0c09a43
UD
617 break;
618
619 default:
31dfab9e
UD
620 if (rta_payload <= sizeof (ifas[ifa_index].addr))
621 memcpy (sa->sa_data, rta_data, rta_payload);
e0c09a43
UD
622 break;
623 }
624 }
625 break;
626
627 case IFA_LOCAL:
628 if (ifas[ifa_index].ifa.ifa_addr != NULL)
629 {
630 /* If ifa_addr is set and we get IFA_LOCAL,
631 assume we have a point-to-point network.
632 Move address to correct field. */
633 ifas[ifa_index].broadaddr = ifas[ifa_index].addr;
634 ifas[ifa_index].ifa.ifa_broadaddr
635 = &ifas[ifa_index].broadaddr.sa;
636 memset (&ifas[ifa_index].addr, '\0',
637 sizeof (ifas[ifa_index].addr));
638 }
639
640 ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa;
641 ifas[ifa_index].ifa.ifa_addr->sa_family
642 = ifam->ifa_family;
643
644 switch (ifam->ifa_family)
645 {
646 case AF_INET:
31dfab9e
UD
647 /* Size must match that of an address for IPv4. */
648 if (rta_payload == 4)
649 memcpy (&ifas[ifa_index].addr.s4.sin_addr,
e0c09a43
UD
650 rta_data, rta_payload);
651 break;
652
653 case AF_INET6:
31dfab9e
UD
654 /* Size must match that of an address for IPv6. */
655 if (rta_payload == 16)
656 {
657 memcpy (&ifas[ifa_index].addr.s6.sin6_addr,
658 rta_data, rta_payload);
b4ae56bd
UD
659 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
660 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
31dfab9e 661 ifas[ifa_index].addr.s6.sin6_scope_id =
5d79df57 662 ifam->ifa_index;
31dfab9e 663 }
e0c09a43
UD
664 break;
665
666 default:
31dfab9e
UD
667 if (rta_payload <= sizeof (ifas[ifa_index].addr))
668 memcpy (ifas[ifa_index].addr.sa.sa_data,
669 rta_data, rta_payload);
e0c09a43
UD
670 break;
671 }
672 break;
673
674 case IFA_BROADCAST:
675 /* We get IFA_BROADCAST, so IFA_LOCAL was too much. */
676 if (ifas[ifa_index].ifa.ifa_broadaddr != NULL)
677 memset (&ifas[ifa_index].broadaddr, '\0',
678 sizeof (ifas[ifa_index].broadaddr));
679
680 ifas[ifa_index].ifa.ifa_broadaddr
681 = &ifas[ifa_index].broadaddr.sa;
682 ifas[ifa_index].ifa.ifa_broadaddr->sa_family
683 = ifam->ifa_family;
684
685 switch (ifam->ifa_family)
686 {
687 case AF_INET:
31dfab9e
UD
688 /* Size must match that of an address for IPv4. */
689 if (rta_payload == 4)
690 memcpy (&ifas[ifa_index].broadaddr.s4.sin_addr,
691 rta_data, rta_payload);
e0c09a43
UD
692 break;
693
694 case AF_INET6:
31dfab9e
UD
695 /* Size must match that of an address for IPv6. */
696 if (rta_payload == 16)
697 {
698 memcpy (&ifas[ifa_index].broadaddr.s6.sin6_addr,
699 rta_data, rta_payload);
700 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
701 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
702 ifas[ifa_index].broadaddr.s6.sin6_scope_id
5d79df57 703 = ifam->ifa_index;
31dfab9e 704 }
e0c09a43
UD
705 break;
706
707 default:
31dfab9e
UD
708 if (rta_payload <= sizeof (ifas[ifa_index].addr))
709 memcpy (&ifas[ifa_index].broadaddr.sa.sa_data,
710 rta_data, rta_payload);
e0c09a43
UD
711 break;
712 }
713 break;
714
715 case IFA_LABEL:
716 if (rta_payload + 1 <= sizeof (ifas[ifa_index].name))
717 {
718 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
31dfab9e
UD
719 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
720 rta_payload) = '\0';
e0c09a43
UD
721 }
722 else
723 abort ();
724 break;
725
726 case IFA_UNSPEC:
727 break;
728 case IFA_CACHEINFO:
729 break;
730 default:
731 break;
732 }
733
734 rta = RTA_NEXT (rta, rtasize);
735 }
736
737 /* If we didn't get the interface name with the
738 address, use the name from the interface entry. */
739 if (ifas[ifa_index].ifa.ifa_name == NULL)
b8b14c4c
UD
740 {
741 int idx = map_newlink (ifam->ifa_index - 1, ifas,
742 map_newlink_data, newlink);
a1ffb40e 743 if (__glibc_unlikely (idx == -1))
b8b14c4c
UD
744 goto try_again;
745 ifas[ifa_index].ifa.ifa_name = ifas[idx].ifa.ifa_name;
746 }
e0c09a43
UD
747
748 /* Calculate the netmask. */
749 if (ifas[ifa_index].ifa.ifa_addr
750 && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_UNSPEC
751 && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_PACKET)
752 {
753 uint32_t max_prefixlen = 0;
754 char *cp = NULL;
755
756 ifas[ifa_index].ifa.ifa_netmask
757 = &ifas[ifa_index].netmask.sa;
758
759 switch (ifas[ifa_index].ifa.ifa_addr->sa_family)
760 {
761 case AF_INET:
762 cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr;
763 max_prefixlen = 32;
764 break;
765
766 case AF_INET6:
767 cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr;
768 max_prefixlen = 128;
769 break;
770 }
771
772 ifas[ifa_index].ifa.ifa_netmask->sa_family
773 = ifas[ifa_index].ifa.ifa_addr->sa_family;
774
775 if (cp != NULL)
776 {
e0c09a43
UD
777 unsigned int preflen;
778
a7b87268 779 if (ifam->ifa_prefixlen > max_prefixlen)
e0c09a43
UD
780 preflen = max_prefixlen;
781 else
782 preflen = ifam->ifa_prefixlen;
783
a7b87268 784 for (i = 0; i < preflen / 8; i++)
e0c09a43 785 *cp++ = 0xff;
a7b87268
AS
786 if (preflen % 8)
787 *cp = 0xff << (8 - preflen % 8);
e0c09a43
UD
788 }
789 }
790 }
791 }
792 }
793
31dfab9e
UD
794 assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size);
795
796 if (newaddr_idx > 0)
797 {
798 for (i = 0; i < newlink; ++i)
799 if (map_newlink_data[i] == -1)
800 {
801 /* We have fewer links then we anticipated. Adjust the
802 forward pointer to the first address entry. */
803 ifas[i - 1].ifa.ifa_next = &ifas[newlink].ifa;
804 }
805
806 if (i == 0 && newlink > 0)
807 /* No valid link, but we allocated memory. We have to
808 populate the first entry. */
809 memmove (ifas, &ifas[newlink], sizeof (struct ifaddrs_storage));
810 }
811
e7c8359e 812 *ifap = &ifas[0].ifa;
5bdd77cb
UD
813
814 exit_free:
f5164429 815 __netlink_free_handle (&nh);
f5164429 816 __netlink_close (&nh);
e0c09a43 817
5bdd77cb 818 return result;
e0c09a43 819}
b8b14c4c
UD
820
821
822/* Create a linked list of `struct ifaddrs' structures, one for each
823 network interface on the host machine. If successful, store the
824 list in *IFAP and return 0. On errors, return -1 and set `errno'. */
825int
7f994279 826__getifaddrs (struct ifaddrs **ifap)
b8b14c4c
UD
827{
828 int res;
829
830 do
831 res = getifaddrs_internal (ifap);
832 while (res == -EAGAIN);
833
834 return res;
835}
7f994279
JM
836weak_alias (__getifaddrs, getifaddrs)
837libc_hidden_weak (getifaddrs)
e0c09a43
UD
838
839
e0c09a43 840void
7f994279 841__freeifaddrs (struct ifaddrs *ifa)
e0c09a43
UD
842{
843 free (ifa);
844}
7f994279
JM
845weak_alias (__freeifaddrs, freeifaddrs)
846libc_hidden_weak (freeifaddrs)