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