Updated to fedora-glibc-20070317T2130
[glibc.git] / sysdeps / unix / sysv / linux / ifaddrs.c
blob02e693553833df3f02273f888d83e3b16ab599e7
1 /* getifaddrs -- get names and addresses of all network interfaces
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 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 req
94 struct nlmsghdr nlh;
95 struct rtgenmsg g;
96 char pad[0];
97 } req;
98 struct sockaddr_nl nladdr;
100 if (h->seq == 0)
101 h->seq = time (NULL);
103 req.nlh.nlmsg_len = sizeof (req);
104 req.nlh.nlmsg_type = type;
105 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
106 req.nlh.nlmsg_pid = 0;
107 req.nlh.nlmsg_seq = h->seq;
108 req.g.rtgen_family = AF_UNSPEC;
109 if (sizeof (req) != offsetof (struct req, pad))
110 memset (req.pad, '\0', sizeof (req) - offsetof (struct req, pad));
112 memset (&nladdr, '\0', sizeof (nladdr));
113 nladdr.nl_family = AF_NETLINK;
115 return TEMP_FAILURE_RETRY (__sendto (h->fd, (void *) &req, sizeof (req), 0,
116 (struct sockaddr *) &nladdr,
117 sizeof (nladdr)));
122 __netlink_request (struct netlink_handle *h, int type)
124 struct netlink_res *nlm_next;
125 struct sockaddr_nl nladdr;
126 struct nlmsghdr *nlmh;
127 ssize_t read_len;
128 bool done = false;
130 #ifdef PAGE_SIZE
131 /* Help the compiler optimize out the malloc call if PAGE_SIZE
132 is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
133 const size_t buf_size = PAGE_SIZE;
134 #else
135 const size_t buf_size = __getpagesize ();
136 #endif
137 bool use_malloc = false;
138 char *buf;
140 if (__libc_use_alloca (buf_size))
141 buf = alloca (buf_size);
142 else
144 buf = malloc (buf_size);
145 if (buf != NULL)
146 use_malloc = true;
147 else
148 goto out_fail;
151 struct iovec iov = { buf, buf_size };
153 if (__netlink_sendreq (h, type) < 0)
154 goto out_fail;
156 while (! done)
158 struct msghdr msg =
160 (void *) &nladdr, sizeof (nladdr),
161 &iov, 1,
162 NULL, 0,
166 read_len = TEMP_FAILURE_RETRY (__recvmsg (h->fd, &msg, 0));
167 if (read_len < 0)
168 goto out_fail;
170 if (nladdr.nl_pid != 0)
171 continue;
173 if (__builtin_expect (msg.msg_flags & MSG_TRUNC, 0))
174 goto out_fail;
176 size_t count = 0;
177 size_t remaining_len = read_len;
178 for (nlmh = (struct nlmsghdr *) buf;
179 NLMSG_OK (nlmh, remaining_len);
180 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, remaining_len))
182 if ((pid_t) nlmh->nlmsg_pid != h->pid
183 || nlmh->nlmsg_seq != h->seq)
184 continue;
186 ++count;
187 if (nlmh->nlmsg_type == NLMSG_DONE)
189 /* We found the end, leave the loop. */
190 done = true;
191 break;
193 if (nlmh->nlmsg_type == NLMSG_ERROR)
195 struct nlmsgerr *nlerr = (struct nlmsgerr *) NLMSG_DATA (nlmh);
196 if (nlmh->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
197 errno = EIO;
198 else
199 errno = -nlerr->error;
200 goto out_fail;
204 /* If there was nothing with the expected nlmsg_pid and nlmsg_seq,
205 there is no point to record it. */
206 if (count == 0)
207 continue;
209 nlm_next = (struct netlink_res *) malloc (sizeof (struct netlink_res)
210 + read_len);
211 if (nlm_next == NULL)
212 goto out_fail;
213 nlm_next->next = NULL;
214 nlm_next->nlh = memcpy (nlm_next + 1, buf, read_len);
215 nlm_next->size = read_len;
216 nlm_next->seq = h->seq;
217 if (h->nlm_list == NULL)
218 h->nlm_list = nlm_next;
219 else
220 h->end_ptr->next = nlm_next;
221 h->end_ptr = nlm_next;
224 if (use_malloc)
225 free (buf);
226 return 0;
228 out_fail:
229 if (use_malloc)
230 free (buf);
231 return -1;
235 void
236 __netlink_close (struct netlink_handle *h)
238 /* Don't modify errno. */
239 INTERNAL_SYSCALL_DECL (err);
240 (void) INTERNAL_SYSCALL (close, err, 1, h->fd);
244 /* Open a NETLINK socket. */
246 __netlink_open (struct netlink_handle *h)
248 struct sockaddr_nl nladdr;
250 h->fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
251 if (h->fd < 0)
252 goto out;
254 memset (&nladdr, '\0', sizeof (nladdr));
255 nladdr.nl_family = AF_NETLINK;
256 if (__bind (h->fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) < 0)
258 close_and_out:
259 __netlink_close (h);
260 out:
261 #if __ASSUME_NETLINK_SUPPORT == 0
262 __no_netlink_support = 1;
263 #endif
264 return -1;
266 /* Determine the ID the kernel assigned for this netlink connection.
267 It is not necessarily the PID if there is more than one socket
268 open. */
269 socklen_t addr_len = sizeof (nladdr);
270 if (__getsockname (h->fd, (struct sockaddr *) &nladdr, &addr_len) < 0)
271 goto close_and_out;
272 h->pid = nladdr.nl_pid;
273 return 0;
277 /* We know the number of RTM_NEWLINK entries, so we reserve the first
278 # of entries for this type. All RTM_NEWADDR entries have an index
279 pointer to the RTM_NEWLINK entry. To find the entry, create
280 a table to map kernel index entries to our index numbers.
281 Since we get at first all RTM_NEWLINK entries, it can never happen
282 that a RTM_NEWADDR index is not known to this map. */
283 static int
284 internal_function
285 map_newlink (int index, struct ifaddrs_storage *ifas, int *map, int max)
287 int i;
289 for (i = 0; i < max; i++)
291 if (map[i] == -1)
293 map[i] = index;
294 if (i > 0)
295 ifas[i - 1].ifa.ifa_next = &ifas[i].ifa;
296 return i;
298 else if (map[i] == index)
299 return i;
301 /* This should never be reached. If this will be reached, we have
302 a very big problem. */
303 abort ();
307 /* Create a linked list of `struct ifaddrs' structures, one for each
308 network interface on the host machine. If successful, store the
309 list in *IFAP and return 0. On errors, return -1 and set `errno'. */
311 getifaddrs (struct ifaddrs **ifap)
313 struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
314 struct netlink_res *nlp;
315 struct ifaddrs_storage *ifas;
316 unsigned int i, newlink, newaddr, newaddr_idx;
317 int *map_newlink_data;
318 size_t ifa_data_size = 0; /* Size to allocate for all ifa_data. */
319 char *ifa_data_ptr; /* Pointer to the unused part of memory for
320 ifa_data. */
321 int result = 0;
323 *ifap = NULL;
325 if (! __no_netlink_support && __netlink_open (&nh) < 0)
327 #if __ASSUME_NETLINK_SUPPORT != 0
328 return -1;
329 #endif
332 #if __ASSUME_NETLINK_SUPPORT == 0
333 if (__no_netlink_support)
334 return fallback_getifaddrs (ifap);
335 #endif
337 /* Tell the kernel that we wish to get a list of all
338 active interfaces, collect all data for every interface. */
339 if (__netlink_request (&nh, RTM_GETLINK) < 0)
341 result = -1;
342 goto exit_free;
345 /* Now ask the kernel for all addresses which are assigned
346 to an interface and collect all data for every interface.
347 Since we store the addresses after the interfaces in the
348 list, we will later always find the interface before the
349 corresponding addresses. */
350 ++nh.seq;
351 if (__netlink_request (&nh, RTM_GETADDR) < 0)
353 result = -1;
354 goto exit_free;
357 /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate
358 enough memory. */
359 newlink = newaddr = 0;
360 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
362 struct nlmsghdr *nlh;
363 size_t size = nlp->size;
365 if (nlp->nlh == NULL)
366 continue;
368 /* Walk through all entries we got from the kernel and look, which
369 message type they contain. */
370 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
372 /* Check if the message is what we want. */
373 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
374 continue;
376 if (nlh->nlmsg_type == NLMSG_DONE)
377 break; /* ok */
379 if (nlh->nlmsg_type == RTM_NEWLINK)
381 /* A RTM_NEWLINK message can have IFLA_STATS data. We need to
382 know the size before creating the list to allocate enough
383 memory. */
384 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
385 struct rtattr *rta = IFLA_RTA (ifim);
386 size_t rtasize = IFLA_PAYLOAD (nlh);
388 while (RTA_OK (rta, rtasize))
390 size_t rta_payload = RTA_PAYLOAD (rta);
392 if (rta->rta_type == IFLA_STATS)
394 ifa_data_size += rta_payload;
395 break;
397 else
398 rta = RTA_NEXT (rta, rtasize);
400 ++newlink;
402 else if (nlh->nlmsg_type == RTM_NEWADDR)
403 ++newaddr;
407 /* Return if no interface is up. */
408 if ((newlink + newaddr) == 0)
409 goto exit_free;
411 /* Allocate memory for all entries we have and initialize next
412 pointer. */
413 ifas = (struct ifaddrs_storage *) calloc (1,
414 (newlink + newaddr)
415 * sizeof (struct ifaddrs_storage)
416 + ifa_data_size);
417 if (ifas == NULL)
419 result = -1;
420 goto exit_free;
423 /* Table for mapping kernel index to entry in our list. */
424 map_newlink_data = alloca (newlink * sizeof (int));
425 memset (map_newlink_data, '\xff', newlink * sizeof (int));
427 ifa_data_ptr = (char *) &ifas[newlink + newaddr];
428 newaddr_idx = 0; /* Counter for newaddr index. */
430 /* Walk through the list of data we got from the kernel. */
431 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
433 struct nlmsghdr *nlh;
434 size_t size = nlp->size;
436 if (nlp->nlh == NULL)
437 continue;
439 /* Walk through one message and look at the type: If it is our
440 message, we need RTM_NEWLINK/RTM_NEWADDR and stop if we reach
441 the end or we find the end marker (in this case we ignore the
442 following data. */
443 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
445 int ifa_index = 0;
447 /* Check if the message is the one we want */
448 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
449 continue;
451 if (nlh->nlmsg_type == NLMSG_DONE)
452 break; /* ok */
454 if (nlh->nlmsg_type == RTM_NEWLINK)
456 /* We found a new interface. Now extract everything from the
457 interface data we got and need. */
458 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
459 struct rtattr *rta = IFLA_RTA (ifim);
460 size_t rtasize = IFLA_PAYLOAD (nlh);
462 /* Interfaces are stored in the first "newlink" entries
463 of our list, starting in the order as we got from the
464 kernel. */
465 ifa_index = map_newlink (ifim->ifi_index - 1, ifas,
466 map_newlink_data, newlink);
467 ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags;
469 while (RTA_OK (rta, rtasize))
471 char *rta_data = RTA_DATA (rta);
472 size_t rta_payload = RTA_PAYLOAD (rta);
474 switch (rta->rta_type)
476 case IFLA_ADDRESS:
477 if (rta_payload <= sizeof (ifas[ifa_index].addr))
479 ifas[ifa_index].addr.sl.sll_family = AF_PACKET;
480 memcpy (ifas[ifa_index].addr.sl.sll_addr,
481 (char *) rta_data, rta_payload);
482 ifas[ifa_index].addr.sl.sll_halen = rta_payload;
483 ifas[ifa_index].addr.sl.sll_ifindex
484 = ifim->ifi_index;
485 ifas[ifa_index].addr.sl.sll_hatype = ifim->ifi_type;
487 ifas[ifa_index].ifa.ifa_addr
488 = &ifas[ifa_index].addr.sa;
490 break;
492 case IFLA_BROADCAST:
493 if (rta_payload <= sizeof (ifas[ifa_index].broadaddr))
495 ifas[ifa_index].broadaddr.sl.sll_family = AF_PACKET;
496 memcpy (ifas[ifa_index].broadaddr.sl.sll_addr,
497 (char *) rta_data, rta_payload);
498 ifas[ifa_index].broadaddr.sl.sll_halen = rta_payload;
499 ifas[ifa_index].broadaddr.sl.sll_ifindex
500 = ifim->ifi_index;
501 ifas[ifa_index].broadaddr.sl.sll_hatype
502 = ifim->ifi_type;
504 ifas[ifa_index].ifa.ifa_broadaddr
505 = &ifas[ifa_index].broadaddr.sa;
507 break;
509 case IFLA_IFNAME: /* Name of Interface */
510 if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name))
512 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
513 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
514 rta_payload) = '\0';
516 break;
518 case IFLA_STATS: /* Statistics of Interface */
519 ifas[ifa_index].ifa.ifa_data = ifa_data_ptr;
520 ifa_data_ptr += rta_payload;
521 memcpy (ifas[ifa_index].ifa.ifa_data, rta_data,
522 rta_payload);
523 break;
525 case IFLA_UNSPEC:
526 break;
527 case IFLA_MTU:
528 break;
529 case IFLA_LINK:
530 break;
531 case IFLA_QDISC:
532 break;
533 default:
534 break;
537 rta = RTA_NEXT (rta, rtasize);
540 else if (nlh->nlmsg_type == RTM_NEWADDR)
542 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh);
543 struct rtattr *rta = IFA_RTA (ifam);
544 size_t rtasize = IFA_PAYLOAD (nlh);
546 /* New Addresses are stored in the order we got them from
547 the kernel after the interfaces. Theoretically it is possible
548 that we have holes in the interface part of the list,
549 but we always have already the interface for this address. */
550 ifa_index = newlink + newaddr_idx;
551 ifas[ifa_index].ifa.ifa_flags
552 = ifas[map_newlink (ifam->ifa_index - 1, ifas,
553 map_newlink_data, newlink)].ifa.ifa_flags;
554 if (ifa_index > 0)
555 ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa;
556 ++newaddr_idx;
558 while (RTA_OK (rta, rtasize))
560 char *rta_data = RTA_DATA (rta);
561 size_t rta_payload = RTA_PAYLOAD (rta);
563 switch (rta->rta_type)
565 case IFA_ADDRESS:
567 struct sockaddr *sa;
569 if (ifas[ifa_index].ifa.ifa_addr != NULL)
571 /* In a point-to-poing network IFA_ADDRESS
572 contains the destination address, local
573 address is supplied in IFA_LOCAL attribute.
574 destination address and broadcast address
575 are stored in an union, so it doesn't matter
576 which name we use. */
577 ifas[ifa_index].ifa.ifa_broadaddr
578 = &ifas[ifa_index].broadaddr.sa;
579 sa = &ifas[ifa_index].broadaddr.sa;
581 else
583 ifas[ifa_index].ifa.ifa_addr
584 = &ifas[ifa_index].addr.sa;
585 sa = &ifas[ifa_index].addr.sa;
588 sa->sa_family = ifam->ifa_family;
590 switch (ifam->ifa_family)
592 case AF_INET:
593 /* Size must match that of an address for IPv4. */
594 if (rta_payload == 4)
595 memcpy (&((struct sockaddr_in *) sa)->sin_addr,
596 rta_data, rta_payload);
597 break;
599 case AF_INET6:
600 /* Size must match that of an address for IPv6. */
601 if (rta_payload == 16)
603 memcpy (&((struct sockaddr_in6 *) sa)->sin6_addr,
604 rta_data, rta_payload);
605 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
606 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
607 ((struct sockaddr_in6 *) sa)->sin6_scope_id
608 = ifam->ifa_index;
610 break;
612 default:
613 if (rta_payload <= sizeof (ifas[ifa_index].addr))
614 memcpy (sa->sa_data, rta_data, rta_payload);
615 break;
618 break;
620 case IFA_LOCAL:
621 if (ifas[ifa_index].ifa.ifa_addr != NULL)
623 /* If ifa_addr is set and we get IFA_LOCAL,
624 assume we have a point-to-point network.
625 Move address to correct field. */
626 ifas[ifa_index].broadaddr = ifas[ifa_index].addr;
627 ifas[ifa_index].ifa.ifa_broadaddr
628 = &ifas[ifa_index].broadaddr.sa;
629 memset (&ifas[ifa_index].addr, '\0',
630 sizeof (ifas[ifa_index].addr));
633 ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa;
634 ifas[ifa_index].ifa.ifa_addr->sa_family
635 = ifam->ifa_family;
637 switch (ifam->ifa_family)
639 case AF_INET:
640 /* Size must match that of an address for IPv4. */
641 if (rta_payload == 4)
642 memcpy (&ifas[ifa_index].addr.s4.sin_addr,
643 rta_data, rta_payload);
644 break;
646 case AF_INET6:
647 /* Size must match that of an address for IPv6. */
648 if (rta_payload == 16)
650 memcpy (&ifas[ifa_index].addr.s6.sin6_addr,
651 rta_data, rta_payload);
652 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
653 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
654 ifas[ifa_index].addr.s6.sin6_scope_id =
655 ifam->ifa_index;
657 break;
659 default:
660 if (rta_payload <= sizeof (ifas[ifa_index].addr))
661 memcpy (ifas[ifa_index].addr.sa.sa_data,
662 rta_data, rta_payload);
663 break;
665 break;
667 case IFA_BROADCAST:
668 /* We get IFA_BROADCAST, so IFA_LOCAL was too much. */
669 if (ifas[ifa_index].ifa.ifa_broadaddr != NULL)
670 memset (&ifas[ifa_index].broadaddr, '\0',
671 sizeof (ifas[ifa_index].broadaddr));
673 ifas[ifa_index].ifa.ifa_broadaddr
674 = &ifas[ifa_index].broadaddr.sa;
675 ifas[ifa_index].ifa.ifa_broadaddr->sa_family
676 = ifam->ifa_family;
678 switch (ifam->ifa_family)
680 case AF_INET:
681 /* Size must match that of an address for IPv4. */
682 if (rta_payload == 4)
683 memcpy (&ifas[ifa_index].broadaddr.s4.sin_addr,
684 rta_data, rta_payload);
685 break;
687 case AF_INET6:
688 /* Size must match that of an address for IPv6. */
689 if (rta_payload == 16)
691 memcpy (&ifas[ifa_index].broadaddr.s6.sin6_addr,
692 rta_data, rta_payload);
693 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
694 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
695 ifas[ifa_index].broadaddr.s6.sin6_scope_id
696 = ifam->ifa_index;
698 break;
700 default:
701 if (rta_payload <= sizeof (ifas[ifa_index].addr))
702 memcpy (&ifas[ifa_index].broadaddr.sa.sa_data,
703 rta_data, rta_payload);
704 break;
706 break;
708 case IFA_LABEL:
709 if (rta_payload + 1 <= sizeof (ifas[ifa_index].name))
711 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
712 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
713 rta_payload) = '\0';
715 else
716 abort ();
717 break;
719 case IFA_UNSPEC:
720 break;
721 case IFA_CACHEINFO:
722 break;
723 default:
724 break;
727 rta = RTA_NEXT (rta, rtasize);
730 /* If we didn't get the interface name with the
731 address, use the name from the interface entry. */
732 if (ifas[ifa_index].ifa.ifa_name == NULL)
733 ifas[ifa_index].ifa.ifa_name
734 = ifas[map_newlink (ifam->ifa_index - 1, ifas,
735 map_newlink_data, newlink)].ifa.ifa_name;
737 /* Calculate the netmask. */
738 if (ifas[ifa_index].ifa.ifa_addr
739 && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_UNSPEC
740 && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_PACKET)
742 uint32_t max_prefixlen = 0;
743 char *cp = NULL;
745 ifas[ifa_index].ifa.ifa_netmask
746 = &ifas[ifa_index].netmask.sa;
748 switch (ifas[ifa_index].ifa.ifa_addr->sa_family)
750 case AF_INET:
751 cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr;
752 max_prefixlen = 32;
753 break;
755 case AF_INET6:
756 cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr;
757 max_prefixlen = 128;
758 break;
761 ifas[ifa_index].ifa.ifa_netmask->sa_family
762 = ifas[ifa_index].ifa.ifa_addr->sa_family;
764 if (cp != NULL)
766 char c;
767 unsigned int preflen;
769 if ((max_prefixlen > 0) &&
770 (ifam->ifa_prefixlen > max_prefixlen))
771 preflen = max_prefixlen;
772 else
773 preflen = ifam->ifa_prefixlen;
775 for (i = 0; i < (preflen / 8); i++)
776 *cp++ = 0xff;
777 c = 0xff;
778 c <<= (8 - (preflen % 8));
779 *cp = c;
786 assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size);
788 if (newaddr_idx > 0)
790 for (i = 0; i < newlink; ++i)
791 if (map_newlink_data[i] == -1)
793 /* We have fewer links then we anticipated. Adjust the
794 forward pointer to the first address entry. */
795 ifas[i - 1].ifa.ifa_next = &ifas[newlink].ifa;
798 if (i == 0 && newlink > 0)
799 /* No valid link, but we allocated memory. We have to
800 populate the first entry. */
801 memmove (ifas, &ifas[newlink], sizeof (struct ifaddrs_storage));
804 *ifap = &ifas[0].ifa;
806 exit_free:
807 __netlink_free_handle (&nh);
808 __netlink_close (&nh);
810 return result;
812 libc_hidden_def (getifaddrs)
815 #if __ASSUME_NETLINK_SUPPORT != 0
816 void
817 freeifaddrs (struct ifaddrs *ifa)
819 free (ifa);
821 libc_hidden_def (freeifaddrs)
822 #endif