libdl: first execute all destructors, then munmap library
[uclibc-ng.git] / libc / inet / ifaddrs.c
blob0c93106512728e17ed3aa73ebd7639cb13def133
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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <alloca.h>
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>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <time.h>
34 #include <unistd.h>
36 #include "netlinkaccess.h"
39 #ifndef __libc_use_alloca
40 # define __libc_use_alloca(x) (x < __MAX_ALLOCA_CUTOFF)
41 #endif
44 #if __ASSUME_NETLINK_SUPPORT
45 #ifdef __UCLIBC_SUPPORT_AI_ADDRCONFIG__
46 /* struct to hold the data for one ifaddrs entry, so we can allocate
47 everything at once. */
48 struct ifaddrs_storage
50 struct ifaddrs ifa;
51 union
53 /* Save space for the biggest of the four used sockaddr types and
54 avoid a lot of casts. */
55 struct sockaddr sa;
56 struct sockaddr_ll sl;
57 struct sockaddr_in s4;
58 #ifdef __UCLIBC_HAS_IPV6__
59 struct sockaddr_in6 s6;
60 #endif
61 } addr, netmask, broadaddr;
62 char name[IF_NAMESIZE + 1];
64 #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
67 void
68 __netlink_free_handle (struct netlink_handle *h)
70 struct netlink_res *ptr;
72 ptr = h->nlm_list;
73 while (ptr != NULL)
75 struct netlink_res *tmpptr;
77 tmpptr = ptr->next;
78 free (ptr); /* doesn't affect errno */
79 ptr = tmpptr;
84 static int
85 __netlink_sendreq (struct netlink_handle *h, int type)
87 struct
89 struct nlmsghdr nlh;
90 struct rtgenmsg g;
91 } req;
92 struct sockaddr_nl nladdr;
94 if (h->seq == 0)
95 h->seq = time (NULL);
97 req.nlh.nlmsg_len = sizeof (req);
98 req.nlh.nlmsg_type = type;
99 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
100 req.nlh.nlmsg_pid = 0;
101 req.nlh.nlmsg_seq = h->seq;
102 req.g.rtgen_family = AF_UNSPEC;
104 memset (&nladdr, '\0', sizeof (nladdr));
105 nladdr.nl_family = AF_NETLINK;
107 return TEMP_FAILURE_RETRY (sendto (h->fd, (void *) &req, sizeof (req), 0,
108 (struct sockaddr *) &nladdr,
109 sizeof (nladdr)));
114 __netlink_request (struct netlink_handle *h, int type)
116 struct netlink_res *nlm_next;
117 struct netlink_res **new_nlm_list;
118 static volatile size_t buf_size = 0;
119 size_t this_buf_size;
120 char *buf;
121 struct sockaddr_nl nladdr;
122 struct nlmsghdr *nlmh;
123 ssize_t read_len;
124 bool done = false;
125 bool use_malloc = false;
127 if (__netlink_sendreq (h, type) < 0)
128 return -1;
130 if (buf_size)
131 this_buf_size = buf_size;
132 else {
133 #ifdef PAGE_SIZE
134 this_buf_size = PAGE_SIZE;
135 #else
136 this_buf_size = __pagesize;
137 #endif
139 if (__libc_use_alloca (this_buf_size))
140 buf = alloca (this_buf_size);
141 else
143 buf = malloc (this_buf_size);
144 if (buf != NULL)
145 use_malloc = true;
146 else
147 goto out_fail;
150 struct iovec iov = { buf, this_buf_size };
152 if (h->nlm_list != NULL)
153 new_nlm_list = &h->end_ptr->next;
154 else
155 new_nlm_list = &h->nlm_list;
157 while (! done)
159 struct msghdr msg =
161 (void *) &nladdr, sizeof (nladdr),
162 &iov, 1,
163 NULL, 0,
167 read_len = TEMP_FAILURE_RETRY (recvmsg (h->fd, &msg, 0));
168 if (read_len < 0)
169 goto out_fail;
171 if (nladdr.nl_pid != 0)
172 continue;
174 if (__builtin_expect (msg.msg_flags & MSG_TRUNC, 0))
176 if (this_buf_size >= SIZE_MAX / 2)
177 goto out_fail;
179 nlm_next = *new_nlm_list;
180 while (nlm_next != NULL)
182 struct netlink_res *tmpptr;
184 tmpptr = nlm_next->next;
185 free (nlm_next);
186 nlm_next = tmpptr;
188 *new_nlm_list = NULL;
190 if (__libc_use_alloca (2 * this_buf_size))
191 buf = extend_alloca (buf, this_buf_size, 2 * this_buf_size);
192 else
194 this_buf_size *= 2;
196 char *new_buf = realloc (use_malloc ? buf : NULL, this_buf_size);
197 if (new_buf == NULL)
198 goto out_fail;
199 new_buf = buf;
201 use_malloc = true;
203 buf_size = this_buf_size;
205 iov.iov_base = buf;
206 iov.iov_len = this_buf_size;
208 /* Increase sequence number, so that we can distinguish
209 between old and new request messages. */
210 h->seq++;
212 if (__netlink_sendreq (h, type) < 0)
213 goto out_fail;
215 continue;
218 size_t count = 0;
219 size_t remaining_len = read_len;
220 for (nlmh = (struct nlmsghdr *) buf;
221 NLMSG_OK (nlmh, remaining_len);
222 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, remaining_len))
224 if ((pid_t) nlmh->nlmsg_pid != h->pid
225 || nlmh->nlmsg_seq != h->seq)
226 continue;
228 ++count;
229 if (nlmh->nlmsg_type == NLMSG_DONE)
231 /* We found the end, leave the loop. */
232 done = true;
233 break;
235 if (nlmh->nlmsg_type == NLMSG_ERROR)
237 struct nlmsgerr *nlerr = (struct nlmsgerr *) NLMSG_DATA (nlmh);
238 if (nlmh->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
239 errno = EIO;
240 else
241 errno = -nlerr->error;
242 goto out_fail;
246 /* If there was nothing with the expected nlmsg_pid and nlmsg_seq,
247 there is no point to record it. */
248 if (count == 0)
249 continue;
251 nlm_next = (struct netlink_res *) malloc (sizeof (struct netlink_res)
252 + read_len);
253 if (nlm_next == NULL)
254 goto out_fail;
255 nlm_next->next = NULL;
256 nlm_next->nlh = memcpy (nlm_next + 1, buf, read_len);
257 nlm_next->size = read_len;
258 nlm_next->seq = h->seq;
259 if (h->nlm_list == NULL)
260 h->nlm_list = nlm_next;
261 else
262 h->end_ptr->next = nlm_next;
263 h->end_ptr = nlm_next;
266 if (use_malloc)
267 free (buf);
268 return 0;
270 out_fail:
271 if (use_malloc)
272 free (buf);
273 return -1;
277 void
278 __netlink_close (struct netlink_handle *h)
280 /* Don't modify errno. */
281 int serrno = errno;
282 close(h->fd);
283 __set_errno(serrno);
287 /* Open a NETLINK socket. */
289 __netlink_open (struct netlink_handle *h)
291 struct sockaddr_nl nladdr;
293 h->fd = socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
294 if (h->fd < 0)
295 goto out;
297 memset (&nladdr, '\0', sizeof (nladdr));
298 nladdr.nl_family = AF_NETLINK;
299 if (bind (h->fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) < 0)
301 close_and_out:
302 __netlink_close (h);
303 out:
304 return -1;
306 /* Determine the ID the kernel assigned for this netlink connection.
307 It is not necessarily the PID if there is more than one socket
308 open. */
309 socklen_t addr_len = sizeof (nladdr);
310 if (getsockname (h->fd, (struct sockaddr *) &nladdr, &addr_len) < 0)
311 goto close_and_out;
312 h->pid = nladdr.nl_pid;
313 return 0;
317 #ifdef __UCLIBC_SUPPORT_AI_ADDRCONFIG__
318 /* We know the number of RTM_NEWLINK entries, so we reserve the first
319 # of entries for this type. All RTM_NEWADDR entries have an index
320 pointer to the RTM_NEWLINK entry. To find the entry, create
321 a table to map kernel index entries to our index numbers.
322 Since we get at first all RTM_NEWLINK entries, it can never happen
323 that a RTM_NEWADDR index is not known to this map. */
324 static int
325 internal_function
326 map_newlink (int idx, struct ifaddrs_storage *ifas, int *map, int max)
328 int i;
330 for (i = 0; i < max; i++)
332 if (map[i] == -1)
334 map[i] = idx;
335 if (i > 0)
336 ifas[i - 1].ifa.ifa_next = &ifas[i].ifa;
337 return i;
339 else if (map[i] == idx)
340 return i;
342 /* This should never be reached. If this will be reached, we have
343 a very big problem. */
344 abort ();
348 /* Create a linked list of `struct ifaddrs' structures, one for each
349 network interface on the host machine. If successful, store the
350 list in *IFAP and return 0. On errors, return -1 and set `errno'. */
352 getifaddrs (struct ifaddrs **ifap)
354 struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
355 struct netlink_res *nlp;
356 struct ifaddrs_storage *ifas;
357 unsigned int i, newlink, newaddr, newaddr_idx;
358 int *map_newlink_data;
359 size_t ifa_data_size = 0; /* Size to allocate for all ifa_data. */
360 char *ifa_data_ptr; /* Pointer to the unused part of memory for
361 ifa_data. */
362 int result = 0;
364 if (ifap)
365 *ifap = NULL;
367 if (__netlink_open (&nh) < 0)
369 return -1;
372 /* Tell the kernel that we wish to get a list of all
373 active interfaces, collect all data for every interface. */
374 if (__netlink_request (&nh, RTM_GETLINK) < 0)
376 result = -1;
377 goto exit_free;
380 /* Now ask the kernel for all addresses which are assigned
381 to an interface and collect all data for every interface.
382 Since we store the addresses after the interfaces in the
383 list, we will later always find the interface before the
384 corresponding addresses. */
385 ++nh.seq;
386 if (__netlink_request (&nh, RTM_GETADDR) < 0)
388 result = -1;
389 goto exit_free;
392 /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate
393 enough memory. */
394 newlink = newaddr = 0;
395 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
397 struct nlmsghdr *nlh;
398 size_t size = nlp->size;
400 if (nlp->nlh == NULL)
401 continue;
403 /* Walk through all entries we got from the kernel and look, which
404 message type they contain. */
405 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
407 /* Check if the message is what we want. */
408 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
409 continue;
411 if (nlh->nlmsg_type == NLMSG_DONE)
412 break; /* ok */
414 if (nlh->nlmsg_type == RTM_NEWLINK)
416 /* A RTM_NEWLINK message can have IFLA_STATS data. We need to
417 know the size before creating the list to allocate enough
418 memory. */
419 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
420 struct rtattr *rta = IFLA_RTA (ifim);
421 size_t rtasize = IFLA_PAYLOAD (nlh);
423 while (RTA_OK (rta, rtasize))
425 size_t rta_payload = RTA_PAYLOAD (rta);
427 if (rta->rta_type == IFLA_STATS)
429 ifa_data_size += rta_payload;
430 break;
432 else
433 rta = RTA_NEXT (rta, rtasize);
435 ++newlink;
437 else if (nlh->nlmsg_type == RTM_NEWADDR)
438 ++newaddr;
442 /* Return if no interface is up. */
443 if ((newlink + newaddr) == 0)
444 goto exit_free;
446 /* Allocate memory for all entries we have and initialize next
447 pointer. */
448 ifas = calloc (1, (newlink + newaddr) * sizeof (ifas[0]) + ifa_data_size);
449 if (ifas == NULL)
451 result = -1;
452 goto exit_free;
455 /* Table for mapping kernel index to entry in our list. */
456 map_newlink_data = alloca (newlink * sizeof (int));
457 memset (map_newlink_data, '\xff', newlink * sizeof (int));
459 ifa_data_ptr = (char *) &ifas[newlink + newaddr];
460 newaddr_idx = 0; /* Counter for newaddr index. */
462 /* Walk through the list of data we got from the kernel. */
463 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
465 struct nlmsghdr *nlh;
466 size_t size = nlp->size;
468 if (nlp->nlh == NULL)
469 continue;
471 /* Walk through one message and look at the type: If it is our
472 message, we need RTM_NEWLINK/RTM_NEWADDR and stop if we reach
473 the end or we find the end marker (in this case we ignore the
474 following data. */
475 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
477 int ifa_index = 0;
479 /* Check if the message is the one we want */
480 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
481 continue;
483 if (nlh->nlmsg_type == NLMSG_DONE)
484 break; /* ok */
486 if (nlh->nlmsg_type == RTM_NEWLINK)
488 /* We found a new interface. Now extract everything from the
489 interface data we got and need. */
490 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
491 struct rtattr *rta = IFLA_RTA (ifim);
492 size_t rtasize = IFLA_PAYLOAD (nlh);
494 /* Interfaces are stored in the first "newlink" entries
495 of our list, starting in the order as we got from the
496 kernel. */
497 ifa_index = map_newlink (ifim->ifi_index - 1, ifas,
498 map_newlink_data, newlink);
499 ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags;
501 while (RTA_OK (rta, rtasize))
503 char *rta_data = RTA_DATA (rta);
504 size_t rta_payload = RTA_PAYLOAD (rta);
506 switch (rta->rta_type)
508 case IFLA_ADDRESS:
509 if (rta_payload <= sizeof (ifas[ifa_index].addr))
511 ifas[ifa_index].addr.sl.sll_family = AF_PACKET;
512 memcpy (ifas[ifa_index].addr.sl.sll_addr,
513 (char *) rta_data, rta_payload);
514 ifas[ifa_index].addr.sl.sll_halen = rta_payload;
515 ifas[ifa_index].addr.sl.sll_ifindex
516 = ifim->ifi_index;
517 ifas[ifa_index].addr.sl.sll_hatype = ifim->ifi_type;
519 ifas[ifa_index].ifa.ifa_addr
520 = &ifas[ifa_index].addr.sa;
522 break;
524 case IFLA_BROADCAST:
525 if (rta_payload <= sizeof (ifas[ifa_index].broadaddr))
527 ifas[ifa_index].broadaddr.sl.sll_family = AF_PACKET;
528 memcpy (ifas[ifa_index].broadaddr.sl.sll_addr,
529 (char *) rta_data, rta_payload);
530 ifas[ifa_index].broadaddr.sl.sll_halen = rta_payload;
531 ifas[ifa_index].broadaddr.sl.sll_ifindex
532 = ifim->ifi_index;
533 ifas[ifa_index].broadaddr.sl.sll_hatype
534 = ifim->ifi_type;
536 ifas[ifa_index].ifa.ifa_broadaddr
537 = &ifas[ifa_index].broadaddr.sa;
539 break;
541 case IFLA_IFNAME: /* Name of Interface */
542 if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name))
544 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
545 *(char *) mempcpy (ifas[ifa_index].name, rta_data,
546 rta_payload) = '\0';
548 break;
550 case IFLA_STATS: /* Statistics of Interface */
551 ifas[ifa_index].ifa.ifa_data = ifa_data_ptr;
552 ifa_data_ptr += rta_payload;
553 memcpy (ifas[ifa_index].ifa.ifa_data, rta_data,
554 rta_payload);
555 break;
557 case IFLA_UNSPEC:
558 break;
559 case IFLA_MTU:
560 break;
561 case IFLA_LINK:
562 break;
563 case IFLA_QDISC:
564 break;
565 default:
566 break;
569 rta = RTA_NEXT (rta, rtasize);
572 else if (nlh->nlmsg_type == RTM_NEWADDR)
574 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh);
575 struct rtattr *rta = IFA_RTA (ifam);
576 size_t rtasize = IFA_PAYLOAD (nlh);
578 /* New Addresses are stored in the order we got them from
579 the kernel after the interfaces. Theoretically it is possible
580 that we have holes in the interface part of the list,
581 but we always have already the interface for this address. */
582 ifa_index = newlink + newaddr_idx;
583 ifas[ifa_index].ifa.ifa_flags
584 = ifas[map_newlink (ifam->ifa_index - 1, ifas,
585 map_newlink_data, newlink)].ifa.ifa_flags;
586 if (ifa_index > 0)
587 ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa;
588 ++newaddr_idx;
590 while (RTA_OK (rta, rtasize))
592 char *rta_data = RTA_DATA (rta);
593 size_t rta_payload = RTA_PAYLOAD (rta);
595 switch (rta->rta_type)
597 case IFA_ADDRESS:
599 struct sockaddr *sa;
601 if (ifas[ifa_index].ifa.ifa_addr != NULL)
603 /* In a point-to-poing network IFA_ADDRESS
604 contains the destination address, local
605 address is supplied in IFA_LOCAL attribute.
606 destination address and broadcast address
607 are stored in an union, so it doesn't matter
608 which name we use. */
609 ifas[ifa_index].ifa.ifa_broadaddr
610 = &ifas[ifa_index].broadaddr.sa;
611 sa = &ifas[ifa_index].broadaddr.sa;
613 else
615 ifas[ifa_index].ifa.ifa_addr
616 = &ifas[ifa_index].addr.sa;
617 sa = &ifas[ifa_index].addr.sa;
620 sa->sa_family = ifam->ifa_family;
622 switch (ifam->ifa_family)
624 case AF_INET:
625 /* Size must match that of an address for IPv4. */
626 if (rta_payload == 4)
627 memcpy (&((struct sockaddr_in *) sa)->sin_addr,
628 rta_data, rta_payload);
629 break;
631 #ifdef __UCLIBC_HAS_IPV6__
632 case AF_INET6:
633 /* Size must match that of an address for IPv6. */
634 if (rta_payload == 16)
636 memcpy (&((struct sockaddr_in6 *) sa)->sin6_addr,
637 rta_data, rta_payload);
638 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
639 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
640 ((struct sockaddr_in6 *) sa)->sin6_scope_id
641 = ifam->ifa_index;
643 break;
644 #endif
646 default:
647 if (rta_payload <= sizeof (ifas[ifa_index].addr))
648 memcpy (sa->sa_data, rta_data, rta_payload);
649 break;
652 break;
654 case IFA_LOCAL:
655 if (ifas[ifa_index].ifa.ifa_addr != NULL)
657 /* If ifa_addr is set and we get IFA_LOCAL,
658 assume we have a point-to-point network.
659 Move address to correct field. */
660 ifas[ifa_index].broadaddr = ifas[ifa_index].addr;
661 ifas[ifa_index].ifa.ifa_broadaddr
662 = &ifas[ifa_index].broadaddr.sa;
663 memset (&ifas[ifa_index].addr, '\0',
664 sizeof (ifas[ifa_index].addr));
667 ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa;
668 ifas[ifa_index].ifa.ifa_addr->sa_family
669 = ifam->ifa_family;
671 switch (ifam->ifa_family)
673 case AF_INET:
674 /* Size must match that of an address for IPv4. */
675 if (rta_payload == 4)
676 memcpy (&ifas[ifa_index].addr.s4.sin_addr,
677 rta_data, rta_payload);
678 break;
680 #ifdef __UCLIBC_HAS_IPV6__
681 case AF_INET6:
682 /* Size must match that of an address for IPv6. */
683 if (rta_payload == 16)
685 memcpy (&ifas[ifa_index].addr.s6.sin6_addr,
686 rta_data, rta_payload);
687 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
688 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
689 ifas[ifa_index].addr.s6.sin6_scope_id =
690 ifam->ifa_index;
692 break;
693 #endif
695 default:
696 if (rta_payload <= sizeof (ifas[ifa_index].addr))
697 memcpy (ifas[ifa_index].addr.sa.sa_data,
698 rta_data, rta_payload);
699 break;
701 break;
703 case IFA_BROADCAST:
704 /* We get IFA_BROADCAST, so IFA_LOCAL was too much. */
705 if (ifas[ifa_index].ifa.ifa_broadaddr != NULL)
706 memset (&ifas[ifa_index].broadaddr, '\0',
707 sizeof (ifas[ifa_index].broadaddr));
709 ifas[ifa_index].ifa.ifa_broadaddr
710 = &ifas[ifa_index].broadaddr.sa;
711 ifas[ifa_index].ifa.ifa_broadaddr->sa_family
712 = ifam->ifa_family;
714 switch (ifam->ifa_family)
716 case AF_INET:
717 /* Size must match that of an address for IPv4. */
718 if (rta_payload == 4)
719 memcpy (&ifas[ifa_index].broadaddr.s4.sin_addr,
720 rta_data, rta_payload);
721 break;
723 #ifdef __UCLIBC_HAS_IPV6__
724 case AF_INET6:
725 /* Size must match that of an address for IPv6. */
726 if (rta_payload == 16)
728 memcpy (&ifas[ifa_index].broadaddr.s6.sin6_addr,
729 rta_data, rta_payload);
730 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
731 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
732 ifas[ifa_index].broadaddr.s6.sin6_scope_id
733 = ifam->ifa_index;
735 break;
736 #endif
738 default:
739 if (rta_payload <= sizeof (ifas[ifa_index].addr))
740 memcpy (&ifas[ifa_index].broadaddr.sa.sa_data,
741 rta_data, rta_payload);
742 break;
744 break;
746 case IFA_LABEL:
747 if (rta_payload + 1 <= sizeof (ifas[ifa_index].name))
749 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
750 *(char *) mempcpy (ifas[ifa_index].name, rta_data,
751 rta_payload) = '\0';
753 else
754 abort ();
755 break;
757 case IFA_UNSPEC:
758 break;
759 case IFA_CACHEINFO:
760 break;
761 default:
762 break;
765 rta = RTA_NEXT (rta, rtasize);
768 /* If we didn't get the interface name with the
769 address, use the name from the interface entry. */
770 if (ifas[ifa_index].ifa.ifa_name == NULL)
771 ifas[ifa_index].ifa.ifa_name
772 = ifas[map_newlink (ifam->ifa_index - 1, ifas,
773 map_newlink_data, newlink)].ifa.ifa_name;
775 /* Calculate the netmask. */
776 if (ifas[ifa_index].ifa.ifa_addr
777 && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_UNSPEC
778 && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_PACKET)
780 uint32_t max_prefixlen = 0;
781 char *cp = NULL;
783 ifas[ifa_index].ifa.ifa_netmask
784 = &ifas[ifa_index].netmask.sa;
786 switch (ifas[ifa_index].ifa.ifa_addr->sa_family)
788 case AF_INET:
789 cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr;
790 max_prefixlen = 32;
791 break;
793 #ifdef __UCLIBC_HAS_IPV6__
794 case AF_INET6:
795 cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr;
796 max_prefixlen = 128;
797 break;
798 #endif
801 ifas[ifa_index].ifa.ifa_netmask->sa_family
802 = ifas[ifa_index].ifa.ifa_addr->sa_family;
804 if (cp != NULL)
806 char c;
807 unsigned int preflen;
809 if ((max_prefixlen > 0) &&
810 (ifam->ifa_prefixlen > max_prefixlen))
811 preflen = max_prefixlen;
812 else
813 preflen = ifam->ifa_prefixlen;
815 for (i = 0; i < (preflen / 8); i++)
816 *cp++ = 0xff;
817 c = 0xff;
818 c <<= (8 - (preflen % 8));
819 *cp = c;
826 assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size);
828 if (newaddr_idx > 0)
830 for (i = 0; i < newlink; ++i)
831 if (map_newlink_data[i] == -1)
833 /* We have fewer links then we anticipated. Adjust the
834 forward pointer to the first address entry. */
835 ifas[i - 1].ifa.ifa_next = &ifas[newlink].ifa;
838 if (i == 0 && newlink > 0)
839 /* No valid link, but we allocated memory. We have to
840 populate the first entry. */
841 memmove (ifas, &ifas[newlink], sizeof (struct ifaddrs_storage));
844 if (ifap != NULL)
845 *ifap = &ifas[0].ifa;
847 exit_free:
848 __netlink_free_handle (&nh);
849 __netlink_close (&nh);
851 return result;
853 libc_hidden_def(getifaddrs)
855 void
856 freeifaddrs (struct ifaddrs *ifa)
858 free (ifa);
860 libc_hidden_def(freeifaddrs)
862 #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
864 #endif /* __ASSUME_NETLINK_SUPPORT */