]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/ifaddrs.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / ifaddrs.c
CommitLineData
e0c09a43 1/* getifaddrs -- get names and addresses of all network interfaces
b168057a 2 Copyright (C) 2003-2015 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 {
164 (void *) &nladdr, sizeof (nladdr),
165 &iov, 1,
166 NULL, 0,
167 0
168 };
169
153da599 170 read_len = TEMP_FAILURE_RETRY (__recvmsg (h->fd, &msg, 0));
e0c09a43 171 if (read_len < 0)
c63d8f80 172 goto out_fail;
e0c09a43 173
c63d8f80
UD
174 if (nladdr.nl_pid != 0)
175 continue;
e0c09a43 176
a1ffb40e 177 if (__glibc_unlikely (msg.msg_flags & MSG_TRUNC))
6cb988fa 178 goto out_fail;
c63d8f80
UD
179
180 size_t count = 0;
181 size_t remaining_len = read_len;
e0c09a43 182 for (nlmh = (struct nlmsghdr *) buf;
c63d8f80
UD
183 NLMSG_OK (nlmh, remaining_len);
184 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, remaining_len))
e0c09a43 185 {
c63d8f80 186 if ((pid_t) nlmh->nlmsg_pid != h->pid
25ce4c6b 187 || nlmh->nlmsg_seq != h->seq)
e0c09a43
UD
188 continue;
189
c63d8f80 190 ++count;
e0c09a43
UD
191 if (nlmh->nlmsg_type == NLMSG_DONE)
192 {
5bdd77cb 193 /* We found the end, leave the loop. */
e0c09a43
UD
194 done = true;
195 break;
196 }
197 if (nlmh->nlmsg_type == NLMSG_ERROR)
198 {
199 struct nlmsgerr *nlerr = (struct nlmsgerr *) NLMSG_DATA (nlmh);
200 if (nlmh->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
201 errno = EIO;
202 else
203 errno = -nlerr->error;
c63d8f80 204 goto out_fail;
e0c09a43
UD
205 }
206 }
c63d8f80
UD
207
208 /* If there was nothing with the expected nlmsg_pid and nlmsg_seq,
209 there is no point to record it. */
210 if (count == 0)
211 continue;
212
213 nlm_next = (struct netlink_res *) malloc (sizeof (struct netlink_res)
214 + read_len);
215 if (nlm_next == NULL)
216 goto out_fail;
217 nlm_next->next = NULL;
218 nlm_next->nlh = memcpy (nlm_next + 1, buf, read_len);
219 nlm_next->size = read_len;
220 nlm_next->seq = h->seq;
221 if (h->nlm_list == NULL)
222 h->nlm_list = nlm_next;
223 else
224 h->end_ptr->next = nlm_next;
225 h->end_ptr = nlm_next;
e0c09a43 226 }
c63d8f80
UD
227
228 if (use_malloc)
229 free (buf);
e0c09a43 230 return 0;
c63d8f80
UD
231
232out_fail:
233 if (use_malloc)
234 free (buf);
235 return -1;
e0c09a43
UD
236}
237
238
f5164429
UD
239void
240__netlink_close (struct netlink_handle *h)
e0c09a43
UD
241{
242 /* Don't modify errno. */
243 INTERNAL_SYSCALL_DECL (err);
244 (void) INTERNAL_SYSCALL (close, err, 1, h->fd);
245}
246
247
248/* Open a NETLINK socket. */
f5164429
UD
249int
250__netlink_open (struct netlink_handle *h)
e0c09a43
UD
251{
252 struct sockaddr_nl nladdr;
253
153da599 254 h->fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
e0c09a43 255 if (h->fd < 0)
f5164429 256 goto out;
e0c09a43
UD
257
258 memset (&nladdr, '\0', sizeof (nladdr));
259 nladdr.nl_family = AF_NETLINK;
153da599 260 if (__bind (h->fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) < 0)
e0c09a43 261 {
332afd9e 262 close_and_out:
f5164429
UD
263 __netlink_close (h);
264 out:
e0c09a43
UD
265 return -1;
266 }
332afd9e
UD
267 /* Determine the ID the kernel assigned for this netlink connection.
268 It is not necessarily the PID if there is more than one socket
269 open. */
270 socklen_t addr_len = sizeof (nladdr);
271 if (__getsockname (h->fd, (struct sockaddr *) &nladdr, &addr_len) < 0)
272 goto close_and_out;
273 h->pid = nladdr.nl_pid;
e0c09a43
UD
274 return 0;
275}
276
277
278/* We know the number of RTM_NEWLINK entries, so we reserve the first
279 # of entries for this type. All RTM_NEWADDR entries have an index
280 pointer to the RTM_NEWLINK entry. To find the entry, create
281 a table to map kernel index entries to our index numbers.
282 Since we get at first all RTM_NEWLINK entries, it can never happen
283 that a RTM_NEWADDR index is not known to this map. */
284static int
31dfab9e
UD
285internal_function
286map_newlink (int index, struct ifaddrs_storage *ifas, int *map, int max)
e0c09a43
UD
287{
288 int i;
289
290 for (i = 0; i < max; i++)
291 {
292 if (map[i] == -1)
293 {
294 map[i] = index;
31dfab9e
UD
295 if (i > 0)
296 ifas[i - 1].ifa.ifa_next = &ifas[i].ifa;
e0c09a43
UD
297 return i;
298 }
299 else if (map[i] == index)
300 return i;
301 }
b8b14c4c 302
6f65e668 303 /* This means interfaces changed between the reading of the
b8b14c4c
UD
304 RTM_GETLINK and RTM_GETADDR information. We have to repeat
305 everything. */
306 return -1;
e0c09a43
UD
307}
308
309
310/* Create a linked list of `struct ifaddrs' structures, one for each
311 network interface on the host machine. If successful, store the
667712b8 312 list in *IFAP and return 0. On errors, return -1 and set `errno'. */
b8b14c4c
UD
313static int
314getifaddrs_internal (struct ifaddrs **ifap)
e0c09a43
UD
315{
316 struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
317 struct netlink_res *nlp;
318 struct ifaddrs_storage *ifas;
319 unsigned int i, newlink, newaddr, newaddr_idx;
320 int *map_newlink_data;
321 size_t ifa_data_size = 0; /* Size to allocate for all ifa_data. */
c63d8f80 322 char *ifa_data_ptr; /* Pointer to the unused part of memory for
e0c09a43 323 ifa_data. */
5bdd77cb 324 int result = 0;
e0c09a43 325
e7c8359e 326 *ifap = NULL;
e0c09a43 327
89b4b02f
JM
328 if (__netlink_open (&nh) < 0)
329 return -1;
e0c09a43 330
e0c09a43 331 /* Tell the kernel that we wish to get a list of all
c63d8f80
UD
332 active interfaces, collect all data for every interface. */
333 if (__netlink_request (&nh, RTM_GETLINK) < 0)
e0c09a43 334 {
5bdd77cb
UD
335 result = -1;
336 goto exit_free;
e0c09a43
UD
337 }
338
e0c09a43 339 /* Now ask the kernel for all addresses which are assigned
c63d8f80
UD
340 to an interface and collect all data for every interface.
341 Since we store the addresses after the interfaces in the
342 list, we will later always find the interface before the
343 corresponding addresses. */
e0c09a43 344 ++nh.seq;
c63d8f80 345 if (__netlink_request (&nh, RTM_GETADDR) < 0)
e0c09a43 346 {
5bdd77cb
UD
347 result = -1;
348 goto exit_free;
e0c09a43
UD
349 }
350
351 /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate
352 enough memory. */
353 newlink = newaddr = 0;
354 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
355 {
356 struct nlmsghdr *nlh;
357 size_t size = nlp->size;
358
359 if (nlp->nlh == NULL)
360 continue;
361
362 /* Walk through all entries we got from the kernel and look, which
c63d8f80 363 message type they contain. */
e0c09a43
UD
364 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
365 {
f5164429 366 /* Check if the message is what we want. */
e0c09a43
UD
367 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
368 continue;
369
370 if (nlh->nlmsg_type == NLMSG_DONE)
371 break; /* ok */
372
373 if (nlh->nlmsg_type == RTM_NEWLINK)
374 {
375 /* A RTM_NEWLINK message can have IFLA_STATS data. We need to
376 know the size before creating the list to allocate enough
377 memory. */
378 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
379 struct rtattr *rta = IFLA_RTA (ifim);
380 size_t rtasize = IFLA_PAYLOAD (nlh);
381
382 while (RTA_OK (rta, rtasize))
383 {
384 size_t rta_payload = RTA_PAYLOAD (rta);
385
386 if (rta->rta_type == IFLA_STATS)
387 {
388 ifa_data_size += rta_payload;
389 break;
390 }
391 else
392 rta = RTA_NEXT (rta, rtasize);
393 }
394 ++newlink;
395 }
396 else if (nlh->nlmsg_type == RTM_NEWADDR)
397 ++newaddr;
398 }
399 }
400
401 /* Return if no interface is up. */
402 if ((newlink + newaddr) == 0)
5bdd77cb 403 goto exit_free;
e0c09a43 404
e0c09a43
UD
405 /* Allocate memory for all entries we have and initialize next
406 pointer. */
407 ifas = (struct ifaddrs_storage *) calloc (1,
408 (newlink + newaddr)
409 * sizeof (struct ifaddrs_storage)
410 + ifa_data_size);
411 if (ifas == NULL)
412 {
5bdd77cb
UD
413 result = -1;
414 goto exit_free;
e0c09a43
UD
415 }
416
31dfab9e
UD
417 /* Table for mapping kernel index to entry in our list. */
418 map_newlink_data = alloca (newlink * sizeof (int));
419 memset (map_newlink_data, '\xff', newlink * sizeof (int));
420
5bdd77cb 421 ifa_data_ptr = (char *) &ifas[newlink + newaddr];
e0c09a43
UD
422 newaddr_idx = 0; /* Counter for newaddr index. */
423
424 /* Walk through the list of data we got from the kernel. */
425 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
426 {
427 struct nlmsghdr *nlh;
428 size_t size = nlp->size;
429
430 if (nlp->nlh == NULL)
431 continue;
432
433 /* Walk through one message and look at the type: If it is our
434 message, we need RTM_NEWLINK/RTM_NEWADDR and stop if we reach
435 the end or we find the end marker (in this case we ignore the
436 following data. */
437 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
438 {
439 int ifa_index = 0;
440
5bdd77cb 441 /* Check if the message is the one we want */
e0c09a43
UD
442 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
443 continue;
444
445 if (nlh->nlmsg_type == NLMSG_DONE)
446 break; /* ok */
5bdd77cb
UD
447
448 if (nlh->nlmsg_type == RTM_NEWLINK)
e0c09a43
UD
449 {
450 /* We found a new interface. Now extract everything from the
451 interface data we got and need. */
452 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
453 struct rtattr *rta = IFLA_RTA (ifim);
454 size_t rtasize = IFLA_PAYLOAD (nlh);
455
5bdd77cb 456 /* Interfaces are stored in the first "newlink" entries
e0c09a43
UD
457 of our list, starting in the order as we got from the
458 kernel. */
c63d8f80 459 ifa_index = map_newlink (ifim->ifi_index - 1, ifas,
e0c09a43 460 map_newlink_data, newlink);
a1ffb40e 461 if (__glibc_unlikely (ifa_index == -1))
b8b14c4c
UD
462 {
463 try_again:
464 result = -EAGAIN;
465 free (ifas);
466 goto exit_free;
467 }
e0c09a43
UD
468 ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags;
469
470 while (RTA_OK (rta, rtasize))
471 {
472 char *rta_data = RTA_DATA (rta);
473 size_t rta_payload = RTA_PAYLOAD (rta);
474
475 switch (rta->rta_type)
476 {
477 case IFLA_ADDRESS:
31dfab9e
UD
478 if (rta_payload <= sizeof (ifas[ifa_index].addr))
479 {
480 ifas[ifa_index].addr.sl.sll_family = AF_PACKET;
481 memcpy (ifas[ifa_index].addr.sl.sll_addr,
482 (char *) rta_data, rta_payload);
483 ifas[ifa_index].addr.sl.sll_halen = rta_payload;
484 ifas[ifa_index].addr.sl.sll_ifindex
485 = ifim->ifi_index;
486 ifas[ifa_index].addr.sl.sll_hatype = ifim->ifi_type;
487
488 ifas[ifa_index].ifa.ifa_addr
489 = &ifas[ifa_index].addr.sa;
490 }
e0c09a43
UD
491 break;
492
493 case IFLA_BROADCAST:
31dfab9e
UD
494 if (rta_payload <= sizeof (ifas[ifa_index].broadaddr))
495 {
496 ifas[ifa_index].broadaddr.sl.sll_family = AF_PACKET;
497 memcpy (ifas[ifa_index].broadaddr.sl.sll_addr,
498 (char *) rta_data, rta_payload);
499 ifas[ifa_index].broadaddr.sl.sll_halen = rta_payload;
500 ifas[ifa_index].broadaddr.sl.sll_ifindex
501 = ifim->ifi_index;
502 ifas[ifa_index].broadaddr.sl.sll_hatype
503 = ifim->ifi_type;
e0c09a43 504
31dfab9e
UD
505 ifas[ifa_index].ifa.ifa_broadaddr
506 = &ifas[ifa_index].broadaddr.sa;
507 }
e0c09a43
UD
508 break;
509
510 case IFLA_IFNAME: /* Name of Interface */
511 if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name))
512 {
513 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
31dfab9e
UD
514 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
515 rta_payload) = '\0';
e0c09a43
UD
516 }
517 break;
518
519 case IFLA_STATS: /* Statistics of Interface */
520 ifas[ifa_index].ifa.ifa_data = ifa_data_ptr;
521 ifa_data_ptr += rta_payload;
522 memcpy (ifas[ifa_index].ifa.ifa_data, rta_data,
523 rta_payload);
524 break;
525
526 case IFLA_UNSPEC:
527 break;
528 case IFLA_MTU:
529 break;
530 case IFLA_LINK:
531 break;
532 case IFLA_QDISC:
533 break;
534 default:
535 break;
536 }
537
538 rta = RTA_NEXT (rta, rtasize);
539 }
540 }
541 else if (nlh->nlmsg_type == RTM_NEWADDR)
542 {
543 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh);
544 struct rtattr *rta = IFA_RTA (ifam);
545 size_t rtasize = IFA_PAYLOAD (nlh);
546
547 /* New Addresses are stored in the order we got them from
31dfab9e 548 the kernel after the interfaces. Theoretically it is possible
e0c09a43
UD
549 that we have holes in the interface part of the list,
550 but we always have already the interface for this address. */
551 ifa_index = newlink + newaddr_idx;
b8b14c4c
UD
552 int idx = map_newlink (ifam->ifa_index - 1, ifas,
553 map_newlink_data, newlink);
a1ffb40e 554 if (__glibc_unlikely (idx == -1))
b8b14c4c
UD
555 goto try_again;
556 ifas[ifa_index].ifa.ifa_flags = ifas[idx].ifa.ifa_flags;
31dfab9e
UD
557 if (ifa_index > 0)
558 ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa;
e0c09a43
UD
559 ++newaddr_idx;
560
561 while (RTA_OK (rta, rtasize))
562 {
563 char *rta_data = RTA_DATA (rta);
564 size_t rta_payload = RTA_PAYLOAD (rta);
565
566 switch (rta->rta_type)
567 {
568 case IFA_ADDRESS:
569 {
570 struct sockaddr *sa;
571
572 if (ifas[ifa_index].ifa.ifa_addr != NULL)
573 {
574 /* In a point-to-poing network IFA_ADDRESS
575 contains the destination address, local
576 address is supplied in IFA_LOCAL attribute.
577 destination address and broadcast address
578 are stored in an union, so it doesn't matter
579 which name we use. */
580 ifas[ifa_index].ifa.ifa_broadaddr
581 = &ifas[ifa_index].broadaddr.sa;
582 sa = &ifas[ifa_index].broadaddr.sa;
583 }
584 else
585 {
586 ifas[ifa_index].ifa.ifa_addr
587 = &ifas[ifa_index].addr.sa;
588 sa = &ifas[ifa_index].addr.sa;
589 }
590
591 sa->sa_family = ifam->ifa_family;
592
593 switch (ifam->ifa_family)
594 {
595 case AF_INET:
31dfab9e
UD
596 /* Size must match that of an address for IPv4. */
597 if (rta_payload == 4)
598 memcpy (&((struct sockaddr_in *) sa)->sin_addr,
599 rta_data, rta_payload);
e0c09a43
UD
600 break;
601
602 case AF_INET6:
31dfab9e
UD
603 /* Size must match that of an address for IPv6. */
604 if (rta_payload == 16)
605 {
606 memcpy (&((struct sockaddr_in6 *) sa)->sin6_addr,
607 rta_data, rta_payload);
608 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
609 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
610 ((struct sockaddr_in6 *) sa)->sin6_scope_id
5d79df57 611 = ifam->ifa_index;
31dfab9e 612 }
e0c09a43
UD
613 break;
614
615 default:
31dfab9e
UD
616 if (rta_payload <= sizeof (ifas[ifa_index].addr))
617 memcpy (sa->sa_data, rta_data, rta_payload);
e0c09a43
UD
618 break;
619 }
620 }
621 break;
622
623 case IFA_LOCAL:
624 if (ifas[ifa_index].ifa.ifa_addr != NULL)
625 {
626 /* If ifa_addr is set and we get IFA_LOCAL,
627 assume we have a point-to-point network.
628 Move address to correct field. */
629 ifas[ifa_index].broadaddr = ifas[ifa_index].addr;
630 ifas[ifa_index].ifa.ifa_broadaddr
631 = &ifas[ifa_index].broadaddr.sa;
632 memset (&ifas[ifa_index].addr, '\0',
633 sizeof (ifas[ifa_index].addr));
634 }
635
636 ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa;
637 ifas[ifa_index].ifa.ifa_addr->sa_family
638 = ifam->ifa_family;
639
640 switch (ifam->ifa_family)
641 {
642 case AF_INET:
31dfab9e
UD
643 /* Size must match that of an address for IPv4. */
644 if (rta_payload == 4)
645 memcpy (&ifas[ifa_index].addr.s4.sin_addr,
e0c09a43
UD
646 rta_data, rta_payload);
647 break;
648
649 case AF_INET6:
31dfab9e
UD
650 /* Size must match that of an address for IPv6. */
651 if (rta_payload == 16)
652 {
653 memcpy (&ifas[ifa_index].addr.s6.sin6_addr,
654 rta_data, rta_payload);
b4ae56bd
UD
655 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
656 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
31dfab9e 657 ifas[ifa_index].addr.s6.sin6_scope_id =
5d79df57 658 ifam->ifa_index;
31dfab9e 659 }
e0c09a43
UD
660 break;
661
662 default:
31dfab9e
UD
663 if (rta_payload <= sizeof (ifas[ifa_index].addr))
664 memcpy (ifas[ifa_index].addr.sa.sa_data,
665 rta_data, rta_payload);
e0c09a43
UD
666 break;
667 }
668 break;
669
670 case IFA_BROADCAST:
671 /* We get IFA_BROADCAST, so IFA_LOCAL was too much. */
672 if (ifas[ifa_index].ifa.ifa_broadaddr != NULL)
673 memset (&ifas[ifa_index].broadaddr, '\0',
674 sizeof (ifas[ifa_index].broadaddr));
675
676 ifas[ifa_index].ifa.ifa_broadaddr
677 = &ifas[ifa_index].broadaddr.sa;
678 ifas[ifa_index].ifa.ifa_broadaddr->sa_family
679 = ifam->ifa_family;
680
681 switch (ifam->ifa_family)
682 {
683 case AF_INET:
31dfab9e
UD
684 /* Size must match that of an address for IPv4. */
685 if (rta_payload == 4)
686 memcpy (&ifas[ifa_index].broadaddr.s4.sin_addr,
687 rta_data, rta_payload);
e0c09a43
UD
688 break;
689
690 case AF_INET6:
31dfab9e
UD
691 /* Size must match that of an address for IPv6. */
692 if (rta_payload == 16)
693 {
694 memcpy (&ifas[ifa_index].broadaddr.s6.sin6_addr,
695 rta_data, rta_payload);
696 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
697 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
698 ifas[ifa_index].broadaddr.s6.sin6_scope_id
5d79df57 699 = ifam->ifa_index;
31dfab9e 700 }
e0c09a43
UD
701 break;
702
703 default:
31dfab9e
UD
704 if (rta_payload <= sizeof (ifas[ifa_index].addr))
705 memcpy (&ifas[ifa_index].broadaddr.sa.sa_data,
706 rta_data, rta_payload);
e0c09a43
UD
707 break;
708 }
709 break;
710
711 case IFA_LABEL:
712 if (rta_payload + 1 <= sizeof (ifas[ifa_index].name))
713 {
714 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
31dfab9e
UD
715 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
716 rta_payload) = '\0';
e0c09a43
UD
717 }
718 else
719 abort ();
720 break;
721
722 case IFA_UNSPEC:
723 break;
724 case IFA_CACHEINFO:
725 break;
726 default:
727 break;
728 }
729
730 rta = RTA_NEXT (rta, rtasize);
731 }
732
733 /* If we didn't get the interface name with the
734 address, use the name from the interface entry. */
735 if (ifas[ifa_index].ifa.ifa_name == NULL)
b8b14c4c
UD
736 {
737 int idx = map_newlink (ifam->ifa_index - 1, ifas,
738 map_newlink_data, newlink);
a1ffb40e 739 if (__glibc_unlikely (idx == -1))
b8b14c4c
UD
740 goto try_again;
741 ifas[ifa_index].ifa.ifa_name = ifas[idx].ifa.ifa_name;
742 }
e0c09a43
UD
743
744 /* Calculate the netmask. */
745 if (ifas[ifa_index].ifa.ifa_addr
746 && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_UNSPEC
747 && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_PACKET)
748 {
749 uint32_t max_prefixlen = 0;
750 char *cp = NULL;
751
752 ifas[ifa_index].ifa.ifa_netmask
753 = &ifas[ifa_index].netmask.sa;
754
755 switch (ifas[ifa_index].ifa.ifa_addr->sa_family)
756 {
757 case AF_INET:
758 cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr;
759 max_prefixlen = 32;
760 break;
761
762 case AF_INET6:
763 cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr;
764 max_prefixlen = 128;
765 break;
766 }
767
768 ifas[ifa_index].ifa.ifa_netmask->sa_family
769 = ifas[ifa_index].ifa.ifa_addr->sa_family;
770
771 if (cp != NULL)
772 {
e0c09a43
UD
773 unsigned int preflen;
774
a7b87268 775 if (ifam->ifa_prefixlen > max_prefixlen)
e0c09a43
UD
776 preflen = max_prefixlen;
777 else
778 preflen = ifam->ifa_prefixlen;
779
a7b87268 780 for (i = 0; i < preflen / 8; i++)
e0c09a43 781 *cp++ = 0xff;
a7b87268
AS
782 if (preflen % 8)
783 *cp = 0xff << (8 - preflen % 8);
e0c09a43
UD
784 }
785 }
786 }
787 }
788 }
789
31dfab9e
UD
790 assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size);
791
792 if (newaddr_idx > 0)
793 {
794 for (i = 0; i < newlink; ++i)
795 if (map_newlink_data[i] == -1)
796 {
797 /* We have fewer links then we anticipated. Adjust the
798 forward pointer to the first address entry. */
799 ifas[i - 1].ifa.ifa_next = &ifas[newlink].ifa;
800 }
801
802 if (i == 0 && newlink > 0)
803 /* No valid link, but we allocated memory. We have to
804 populate the first entry. */
805 memmove (ifas, &ifas[newlink], sizeof (struct ifaddrs_storage));
806 }
807
e7c8359e 808 *ifap = &ifas[0].ifa;
5bdd77cb
UD
809
810 exit_free:
f5164429 811 __netlink_free_handle (&nh);
f5164429 812 __netlink_close (&nh);
e0c09a43 813
5bdd77cb 814 return result;
e0c09a43 815}
b8b14c4c
UD
816
817
818/* Create a linked list of `struct ifaddrs' structures, one for each
819 network interface on the host machine. If successful, store the
820 list in *IFAP and return 0. On errors, return -1 and set `errno'. */
821int
7f994279 822__getifaddrs (struct ifaddrs **ifap)
b8b14c4c
UD
823{
824 int res;
825
826 do
827 res = getifaddrs_internal (ifap);
828 while (res == -EAGAIN);
829
830 return res;
831}
7f994279
JM
832weak_alias (__getifaddrs, getifaddrs)
833libc_hidden_weak (getifaddrs)
e0c09a43
UD
834
835
e0c09a43 836void
7f994279 837__freeifaddrs (struct ifaddrs *ifa)
e0c09a43
UD
838{
839 free (ifa);
840}
7f994279
JM
841weak_alias (__freeifaddrs, freeifaddrs)
842libc_hidden_weak (freeifaddrs)