[BZ #2329]
[glibc.git] / sysdeps / unix / sysv / linux / ifaddrs.c
blobf743f702f0445e9b3076827aee2bb9f6ef1e9a21
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.
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.
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.
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. */
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>
37 #include "netlinkaccess.h"
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;
45 # define getifaddrs fallback_getifaddrs
46 # include "sysdeps/gnu/ifaddrs.c"
47 # undef getifaddrs
48 #endif
51 /* struct to hold the data for one ifaddrs entry, so we can allocate
52 everything at once. */
53 struct ifaddrs_storage
55 struct ifaddrs ifa;
56 union
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];
69 void
70 __netlink_free_handle (struct netlink_handle *h)
72 struct netlink_res *ptr;
73 int saved_errno = errno;
75 ptr = h->nlm_list;
76 while (ptr != NULL)
78 struct netlink_res *tmpptr;
80 tmpptr = ptr->next;
81 free (ptr);
82 ptr = tmpptr;
85 __set_errno (saved_errno);
89 static int
90 __netlink_sendreq (struct netlink_handle *h, int type)
92 struct
94 struct nlmsghdr nlh;
95 struct rtgenmsg g;
96 } req;
97 struct sockaddr_nl nladdr;
99 if (h->seq == 0)
100 h->seq = time (NULL);
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;
109 memset (&nladdr, '\0', sizeof (nladdr));
110 nladdr.nl_family = AF_NETLINK;
112 return TEMP_FAILURE_RETRY (__sendto (h->fd, (void *) &req, sizeof (req), 0,
113 (struct sockaddr *) &nladdr,
114 sizeof (nladdr)));
119 __netlink_request (struct netlink_handle *h, int type)
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;
131 if (__netlink_sendreq (h, type) < 0)
132 return -1;
134 size_t this_buf_size = buf_size;
135 if (__libc_use_alloca (this_buf_size))
136 buf = alloca (this_buf_size);
137 else
139 buf = malloc (this_buf_size);
140 if (buf != NULL)
141 use_malloc = true;
142 else
143 goto out_fail;
146 struct iovec iov = { buf, this_buf_size };
148 if (h->nlm_list != NULL)
149 new_nlm_list = &h->end_ptr->next;
150 else
151 new_nlm_list = &h->nlm_list;
153 while (! done)
155 struct msghdr msg =
157 (void *) &nladdr, sizeof (nladdr),
158 &iov, 1,
159 NULL, 0,
163 read_len = TEMP_FAILURE_RETRY (__recvmsg (h->fd, &msg, 0));
164 if (read_len < 0)
165 goto out_fail;
167 if (nladdr.nl_pid != 0)
168 continue;
170 if (__builtin_expect (msg.msg_flags & MSG_TRUNC, 0))
172 if (this_buf_size >= SIZE_MAX / 2)
173 goto out_fail;
175 nlm_next = *new_nlm_list;
176 while (nlm_next != NULL)
178 struct netlink_res *tmpptr;
180 tmpptr = nlm_next->next;
181 free (nlm_next);
182 nlm_next = tmpptr;
184 *new_nlm_list = NULL;
186 if (__libc_use_alloca (2 * this_buf_size))
187 buf = extend_alloca (buf, this_buf_size, 2 * this_buf_size);
188 else
190 this_buf_size *= 2;
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;
197 use_malloc = true;
199 buf_size = this_buf_size;
201 iov.iov_base = buf;
202 iov.iov_len = this_buf_size;
204 /* Increase sequence number, so that we can distinguish
205 between old and new request messages. */
206 h->seq++;
208 if (__netlink_sendreq (h, type) < 0)
209 goto out_fail;
211 continue;
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))
220 if ((pid_t) nlmh->nlmsg_pid != h->pid
221 || nlmh->nlmsg_seq != h->seq)
222 continue;
224 ++count;
225 if (nlmh->nlmsg_type == NLMSG_DONE)
227 /* We found the end, leave the loop. */
228 done = true;
229 break;
231 if (nlmh->nlmsg_type == NLMSG_ERROR)
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;
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;
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;
262 if (use_malloc)
263 free (buf);
264 return 0;
266 out_fail:
267 if (use_malloc)
268 free (buf);
269 return -1;
273 void
274 __netlink_close (struct netlink_handle *h)
276 /* Don't modify errno. */
277 INTERNAL_SYSCALL_DECL (err);
278 (void) INTERNAL_SYSCALL (close, err, 1, h->fd);
282 /* Open a NETLINK socket. */
284 __netlink_open (struct netlink_handle *h)
286 struct sockaddr_nl nladdr;
288 h->fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
289 if (h->fd < 0)
290 goto out;
292 memset (&nladdr, '\0', sizeof (nladdr));
293 nladdr.nl_family = AF_NETLINK;
294 if (__bind (h->fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) < 0)
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;
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;
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)
325 int i;
327 for (i = 0; i < max; i++)
329 if (map[i] == -1)
331 map[i] = index;
332 if (i > 0)
333 ifas[i - 1].ifa.ifa_next = &ifas[i].ifa;
334 return i;
336 else if (map[i] == index)
337 return i;
339 /* This should never be reached. If this will be reached, we have
340 a very big problem. */
341 abort ();
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'. */
349 getifaddrs (struct ifaddrs **ifap)
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;
361 if (ifap)
362 *ifap = NULL;
364 if (! __no_netlink_support && __netlink_open (&nh) < 0)
366 #if __ASSUME_NETLINK_SUPPORT != 0
367 return -1;
368 #endif
371 #if __ASSUME_NETLINK_SUPPORT == 0
372 if (__no_netlink_support)
373 return fallback_getifaddrs (ifap);
374 #endif
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)
380 result = -1;
381 goto exit_free;
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)
392 result = -1;
393 goto exit_free;
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)
401 struct nlmsghdr *nlh;
402 size_t size = nlp->size;
404 if (nlp->nlh == NULL)
405 continue;
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))
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;
415 if (nlh->nlmsg_type == NLMSG_DONE)
416 break; /* ok */
418 if (nlh->nlmsg_type == RTM_NEWLINK)
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);
427 while (RTA_OK (rta, rtasize))
429 size_t rta_payload = RTA_PAYLOAD (rta);
431 if (rta->rta_type == IFLA_STATS)
433 ifa_data_size += rta_payload;
434 break;
436 else
437 rta = RTA_NEXT (rta, rtasize);
439 ++newlink;
441 else if (nlh->nlmsg_type == RTM_NEWADDR)
442 ++newaddr;
446 /* Return if no interface is up. */
447 if ((newlink + newaddr) == 0)
448 goto exit_free;
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)
458 result = -1;
459 goto exit_free;
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));
466 ifa_data_ptr = (char *) &ifas[newlink + newaddr];
467 newaddr_idx = 0; /* Counter for newaddr index. */
469 /* Walk through the list of data we got from the kernel. */
470 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
472 struct nlmsghdr *nlh;
473 size_t size = nlp->size;
475 if (nlp->nlh == NULL)
476 continue;
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))
484 int ifa_index = 0;
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;
490 if (nlh->nlmsg_type == NLMSG_DONE)
491 break; /* ok */
493 if (nlh->nlmsg_type == RTM_NEWLINK)
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);
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;
508 while (RTA_OK (rta, rtasize))
510 char *rta_data = RTA_DATA (rta);
511 size_t rta_payload = RTA_PAYLOAD (rta);
513 switch (rta->rta_type)
515 case IFLA_ADDRESS:
516 if (rta_payload <= sizeof (ifas[ifa_index].addr))
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;
526 ifas[ifa_index].ifa.ifa_addr
527 = &ifas[ifa_index].addr.sa;
529 break;
531 case IFLA_BROADCAST:
532 if (rta_payload <= sizeof (ifas[ifa_index].broadaddr))
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;
543 ifas[ifa_index].ifa.ifa_broadaddr
544 = &ifas[ifa_index].broadaddr.sa;
546 break;
548 case IFLA_IFNAME: /* Name of Interface */
549 if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name))
551 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
552 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
553 rta_payload) = '\0';
555 break;
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;
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;
576 rta = RTA_NEXT (rta, rtasize);
579 else if (nlh->nlmsg_type == RTM_NEWADDR)
581 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh);
582 struct rtattr *rta = IFA_RTA (ifam);
583 size_t rtasize = IFA_PAYLOAD (nlh);
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;
597 while (RTA_OK (rta, rtasize))
599 char *rta_data = RTA_DATA (rta);
600 size_t rta_payload = RTA_PAYLOAD (rta);
602 switch (rta->rta_type)
604 case IFA_ADDRESS:
606 struct sockaddr *sa;
608 if (ifas[ifa_index].ifa.ifa_addr != NULL)
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;
620 else
622 ifas[ifa_index].ifa.ifa_addr
623 = &ifas[ifa_index].addr.sa;
624 sa = &ifas[ifa_index].addr.sa;
627 sa->sa_family = ifam->ifa_family;
629 switch (ifam->ifa_family)
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;
638 case AF_INET6:
639 /* Size must match that of an address for IPv6. */
640 if (rta_payload == 16)
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;
649 break;
651 default:
652 if (rta_payload <= sizeof (ifas[ifa_index].addr))
653 memcpy (sa->sa_data, rta_data, rta_payload);
654 break;
657 break;
659 case IFA_LOCAL:
660 if (ifas[ifa_index].ifa.ifa_addr != NULL)
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));
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;
676 switch (ifam->ifa_family)
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;
685 case AF_INET6:
686 /* Size must match that of an address for IPv6. */
687 if (rta_payload == 16)
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;
696 break;
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;
704 break;
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));
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;
717 switch (ifam->ifa_family)
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;
726 case AF_INET6:
727 /* Size must match that of an address for IPv6. */
728 if (rta_payload == 16)
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;
737 break;
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;
745 break;
747 case IFA_LABEL:
748 if (rta_payload + 1 <= sizeof (ifas[ifa_index].name))
750 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
751 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
752 rta_payload) = '\0';
754 else
755 abort ();
756 break;
758 case IFA_UNSPEC:
759 break;
760 case IFA_CACHEINFO:
761 break;
762 default:
763 break;
766 rta = RTA_NEXT (rta, rtasize);
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;
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)
781 uint32_t max_prefixlen = 0;
782 char *cp = NULL;
784 ifas[ifa_index].ifa.ifa_netmask
785 = &ifas[ifa_index].netmask.sa;
787 switch (ifas[ifa_index].ifa.ifa_addr->sa_family)
789 case AF_INET:
790 cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr;
791 max_prefixlen = 32;
792 break;
794 case AF_INET6:
795 cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr;
796 max_prefixlen = 128;
797 break;
800 ifas[ifa_index].ifa.ifa_netmask->sa_family
801 = ifas[ifa_index].ifa.ifa_addr->sa_family;
803 if (cp != NULL)
805 char c;
806 unsigned int preflen;
808 if ((max_prefixlen > 0) &&
809 (ifam->ifa_prefixlen > max_prefixlen))
810 preflen = max_prefixlen;
811 else
812 preflen = ifam->ifa_prefixlen;
814 for (i = 0; i < (preflen / 8); i++)
815 *cp++ = 0xff;
816 c = 0xff;
817 c <<= (8 - (preflen % 8));
818 *cp = c;
825 assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size);
827 if (newaddr_idx > 0)
829 for (i = 0; i < newlink; ++i)
830 if (map_newlink_data[i] == -1)
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;
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));
843 if (ifap != NULL)
844 *ifap = &ifas[0].ifa;
846 exit_free:
847 __netlink_free_handle (&nh);
848 __netlink_close (&nh);
850 return result;
852 libc_hidden_def (getifaddrs)
855 #if __ASSUME_NETLINK_SUPPORT != 0
856 void
857 freeifaddrs (struct ifaddrs *ifa)
859 free (ifa);
861 libc_hidden_def (freeifaddrs)
862 #endif