Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / ifaddrs.c
blob64b4a1c8597b60bd6d24e17c640b4b4c88c97aa4
1 /* getifaddrs -- get names and addresses of all network interfaces
2 Copyright (C) 2003-2015 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 <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <sysdep.h>
33 #include <time.h>
34 #include <unistd.h>
36 #include "netlinkaccess.h"
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. */
43 struct sockaddr_ll_max
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];
55 /* struct to hold the data for one ifaddrs entry, so we can allocate
56 everything at once. */
57 struct ifaddrs_storage
59 struct ifaddrs ifa;
60 union
62 /* Save space for the biggest of the four used sockaddr types and
63 avoid a lot of casts. */
64 struct sockaddr sa;
65 struct sockaddr_ll_max sl;
66 struct sockaddr_in s4;
67 struct sockaddr_in6 s6;
68 } addr, netmask, broadaddr;
69 char name[IF_NAMESIZE + 1];
73 void
74 __netlink_free_handle (struct netlink_handle *h)
76 struct netlink_res *ptr;
77 int saved_errno = errno;
79 ptr = h->nlm_list;
80 while (ptr != NULL)
82 struct netlink_res *tmpptr;
84 tmpptr = ptr->next;
85 free (ptr);
86 ptr = tmpptr;
89 __set_errno (saved_errno);
93 static int
94 __netlink_sendreq (struct netlink_handle *h, int type)
96 struct req
98 struct nlmsghdr nlh;
99 struct rtgenmsg g;
100 char pad[0];
101 } req;
102 struct sockaddr_nl nladdr;
104 if (h->seq == 0)
105 h->seq = time (NULL);
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;
113 if (sizeof (req) != offsetof (struct req, pad))
114 memset (req.pad, '\0', sizeof (req) - offsetof (struct req, pad));
116 memset (&nladdr, '\0', sizeof (nladdr));
117 nladdr.nl_family = AF_NETLINK;
119 return TEMP_FAILURE_RETRY (__sendto (h->fd, (void *) &req, sizeof (req), 0,
120 (struct sockaddr *) &nladdr,
121 sizeof (nladdr)));
126 __netlink_request (struct netlink_handle *h, int type)
128 struct netlink_res *nlm_next;
129 struct sockaddr_nl nladdr;
130 struct nlmsghdr *nlmh;
131 ssize_t read_len;
132 bool done = false;
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;
144 if (__libc_use_alloca (buf_size))
145 buf = alloca (buf_size);
146 else
148 buf = malloc (buf_size);
149 if (buf != NULL)
150 use_malloc = true;
151 else
152 goto out_fail;
155 struct iovec iov = { buf, buf_size };
157 if (__netlink_sendreq (h, type) < 0)
158 goto out_fail;
160 while (! done)
162 struct msghdr msg =
164 (void *) &nladdr, sizeof (nladdr),
165 &iov, 1,
166 NULL, 0,
170 read_len = TEMP_FAILURE_RETRY (__recvmsg (h->fd, &msg, 0));
171 if (read_len < 0)
172 goto out_fail;
174 if (nladdr.nl_pid != 0)
175 continue;
177 if (__glibc_unlikely (msg.msg_flags & MSG_TRUNC))
178 goto out_fail;
180 size_t count = 0;
181 size_t remaining_len = read_len;
182 for (nlmh = (struct nlmsghdr *) buf;
183 NLMSG_OK (nlmh, remaining_len);
184 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, remaining_len))
186 if ((pid_t) nlmh->nlmsg_pid != h->pid
187 || nlmh->nlmsg_seq != h->seq)
188 continue;
190 ++count;
191 if (nlmh->nlmsg_type == NLMSG_DONE)
193 /* We found the end, leave the loop. */
194 done = true;
195 break;
197 if (nlmh->nlmsg_type == NLMSG_ERROR)
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;
204 goto out_fail;
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;
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;
228 if (use_malloc)
229 free (buf);
230 return 0;
232 out_fail:
233 if (use_malloc)
234 free (buf);
235 return -1;
239 void
240 __netlink_close (struct netlink_handle *h)
242 /* Don't modify errno. */
243 INTERNAL_SYSCALL_DECL (err);
244 (void) INTERNAL_SYSCALL (close, err, 1, h->fd);
248 /* Open a NETLINK socket. */
250 __netlink_open (struct netlink_handle *h)
252 struct sockaddr_nl nladdr;
254 h->fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
255 if (h->fd < 0)
256 goto out;
258 memset (&nladdr, '\0', sizeof (nladdr));
259 nladdr.nl_family = AF_NETLINK;
260 if (__bind (h->fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) < 0)
262 close_and_out:
263 __netlink_close (h);
264 out:
265 return -1;
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;
274 return 0;
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. */
284 static int
285 internal_function
286 map_newlink (int index, struct ifaddrs_storage *ifas, int *map, int max)
288 int i;
290 for (i = 0; i < max; i++)
292 if (map[i] == -1)
294 map[i] = index;
295 if (i > 0)
296 ifas[i - 1].ifa.ifa_next = &ifas[i].ifa;
297 return i;
299 else if (map[i] == index)
300 return i;
303 /* This means interfaces changed between the reading of the
304 RTM_GETLINK and RTM_GETADDR information. We have to repeat
305 everything. */
306 return -1;
310 /* Create a linked list of `struct ifaddrs' structures, one for each
311 network interface on the host machine. If successful, store the
312 list in *IFAP and return 0. On errors, return -1 and set `errno'. */
313 static int
314 getifaddrs_internal (struct ifaddrs **ifap)
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. */
322 char *ifa_data_ptr; /* Pointer to the unused part of memory for
323 ifa_data. */
324 int result = 0;
326 *ifap = NULL;
328 if (__netlink_open (&nh) < 0)
329 return -1;
331 /* Tell the kernel that we wish to get a list of all
332 active interfaces, collect all data for every interface. */
333 if (__netlink_request (&nh, RTM_GETLINK) < 0)
335 result = -1;
336 goto exit_free;
339 /* Now ask the kernel for all addresses which are assigned
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. */
344 ++nh.seq;
345 if (__netlink_request (&nh, RTM_GETADDR) < 0)
347 result = -1;
348 goto exit_free;
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)
356 struct nlmsghdr *nlh;
357 size_t size = nlp->size;
359 if (nlp->nlh == NULL)
360 continue;
362 /* Walk through all entries we got from the kernel and look, which
363 message type they contain. */
364 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
366 /* Check if the message is what we want. */
367 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
368 continue;
370 if (nlh->nlmsg_type == NLMSG_DONE)
371 break; /* ok */
373 if (nlh->nlmsg_type == RTM_NEWLINK)
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);
382 while (RTA_OK (rta, rtasize))
384 size_t rta_payload = RTA_PAYLOAD (rta);
386 if (rta->rta_type == IFLA_STATS)
388 ifa_data_size += rta_payload;
389 break;
391 else
392 rta = RTA_NEXT (rta, rtasize);
394 ++newlink;
396 else if (nlh->nlmsg_type == RTM_NEWADDR)
397 ++newaddr;
401 /* Return if no interface is up. */
402 if ((newlink + newaddr) == 0)
403 goto exit_free;
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)
413 result = -1;
414 goto exit_free;
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));
421 ifa_data_ptr = (char *) &ifas[newlink + newaddr];
422 newaddr_idx = 0; /* Counter for newaddr index. */
424 /* Walk through the list of data we got from the kernel. */
425 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
427 struct nlmsghdr *nlh;
428 size_t size = nlp->size;
430 if (nlp->nlh == NULL)
431 continue;
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))
439 int ifa_index = 0;
441 /* Check if the message is the one we want */
442 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
443 continue;
445 if (nlh->nlmsg_type == NLMSG_DONE)
446 break; /* ok */
448 if (nlh->nlmsg_type == RTM_NEWLINK)
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);
456 /* Interfaces are stored in the first "newlink" entries
457 of our list, starting in the order as we got from the
458 kernel. */
459 ifa_index = map_newlink (ifim->ifi_index - 1, ifas,
460 map_newlink_data, newlink);
461 if (__glibc_unlikely (ifa_index == -1))
463 try_again:
464 result = -EAGAIN;
465 free (ifas);
466 goto exit_free;
468 ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags;
470 while (RTA_OK (rta, rtasize))
472 char *rta_data = RTA_DATA (rta);
473 size_t rta_payload = RTA_PAYLOAD (rta);
475 switch (rta->rta_type)
477 case IFLA_ADDRESS:
478 if (rta_payload <= sizeof (ifas[ifa_index].addr))
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;
488 ifas[ifa_index].ifa.ifa_addr
489 = &ifas[ifa_index].addr.sa;
491 break;
493 case IFLA_BROADCAST:
494 if (rta_payload <= sizeof (ifas[ifa_index].broadaddr))
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;
505 ifas[ifa_index].ifa.ifa_broadaddr
506 = &ifas[ifa_index].broadaddr.sa;
508 break;
510 case IFLA_IFNAME: /* Name of Interface */
511 if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name))
513 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
514 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
515 rta_payload) = '\0';
517 break;
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;
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;
538 rta = RTA_NEXT (rta, rtasize);
541 else if (nlh->nlmsg_type == RTM_NEWADDR)
543 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh);
544 struct rtattr *rta = IFA_RTA (ifam);
545 size_t rtasize = IFA_PAYLOAD (nlh);
547 /* New Addresses are stored in the order we got them from
548 the kernel after the interfaces. Theoretically it is possible
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;
552 int idx = map_newlink (ifam->ifa_index - 1, ifas,
553 map_newlink_data, newlink);
554 if (__glibc_unlikely (idx == -1))
555 goto try_again;
556 ifas[ifa_index].ifa.ifa_flags = ifas[idx].ifa.ifa_flags;
557 if (ifa_index > 0)
558 ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa;
559 ++newaddr_idx;
561 while (RTA_OK (rta, rtasize))
563 char *rta_data = RTA_DATA (rta);
564 size_t rta_payload = RTA_PAYLOAD (rta);
566 switch (rta->rta_type)
568 case IFA_ADDRESS:
570 struct sockaddr *sa;
572 if (ifas[ifa_index].ifa.ifa_addr != NULL)
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;
584 else
586 ifas[ifa_index].ifa.ifa_addr
587 = &ifas[ifa_index].addr.sa;
588 sa = &ifas[ifa_index].addr.sa;
591 sa->sa_family = ifam->ifa_family;
593 switch (ifam->ifa_family)
595 case AF_INET:
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);
600 break;
602 case AF_INET6:
603 /* Size must match that of an address for IPv6. */
604 if (rta_payload == 16)
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
611 = ifam->ifa_index;
613 break;
615 default:
616 if (rta_payload <= sizeof (ifas[ifa_index].addr))
617 memcpy (sa->sa_data, rta_data, rta_payload);
618 break;
621 break;
623 case IFA_LOCAL:
624 if (ifas[ifa_index].ifa.ifa_addr != NULL)
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));
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;
640 switch (ifam->ifa_family)
642 case AF_INET:
643 /* Size must match that of an address for IPv4. */
644 if (rta_payload == 4)
645 memcpy (&ifas[ifa_index].addr.s4.sin_addr,
646 rta_data, rta_payload);
647 break;
649 case AF_INET6:
650 /* Size must match that of an address for IPv6. */
651 if (rta_payload == 16)
653 memcpy (&ifas[ifa_index].addr.s6.sin6_addr,
654 rta_data, rta_payload);
655 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
656 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
657 ifas[ifa_index].addr.s6.sin6_scope_id =
658 ifam->ifa_index;
660 break;
662 default:
663 if (rta_payload <= sizeof (ifas[ifa_index].addr))
664 memcpy (ifas[ifa_index].addr.sa.sa_data,
665 rta_data, rta_payload);
666 break;
668 break;
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));
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;
681 switch (ifam->ifa_family)
683 case AF_INET:
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);
688 break;
690 case AF_INET6:
691 /* Size must match that of an address for IPv6. */
692 if (rta_payload == 16)
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
699 = ifam->ifa_index;
701 break;
703 default:
704 if (rta_payload <= sizeof (ifas[ifa_index].addr))
705 memcpy (&ifas[ifa_index].broadaddr.sa.sa_data,
706 rta_data, rta_payload);
707 break;
709 break;
711 case IFA_LABEL:
712 if (rta_payload + 1 <= sizeof (ifas[ifa_index].name))
714 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
715 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
716 rta_payload) = '\0';
718 else
719 abort ();
720 break;
722 case IFA_UNSPEC:
723 break;
724 case IFA_CACHEINFO:
725 break;
726 default:
727 break;
730 rta = RTA_NEXT (rta, rtasize);
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)
737 int idx = map_newlink (ifam->ifa_index - 1, ifas,
738 map_newlink_data, newlink);
739 if (__glibc_unlikely (idx == -1))
740 goto try_again;
741 ifas[ifa_index].ifa.ifa_name = ifas[idx].ifa.ifa_name;
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)
749 uint32_t max_prefixlen = 0;
750 char *cp = NULL;
752 ifas[ifa_index].ifa.ifa_netmask
753 = &ifas[ifa_index].netmask.sa;
755 switch (ifas[ifa_index].ifa.ifa_addr->sa_family)
757 case AF_INET:
758 cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr;
759 max_prefixlen = 32;
760 break;
762 case AF_INET6:
763 cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr;
764 max_prefixlen = 128;
765 break;
768 ifas[ifa_index].ifa.ifa_netmask->sa_family
769 = ifas[ifa_index].ifa.ifa_addr->sa_family;
771 if (cp != NULL)
773 unsigned int preflen;
775 if (ifam->ifa_prefixlen > max_prefixlen)
776 preflen = max_prefixlen;
777 else
778 preflen = ifam->ifa_prefixlen;
780 for (i = 0; i < preflen / 8; i++)
781 *cp++ = 0xff;
782 if (preflen % 8)
783 *cp = 0xff << (8 - preflen % 8);
790 assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size);
792 if (newaddr_idx > 0)
794 for (i = 0; i < newlink; ++i)
795 if (map_newlink_data[i] == -1)
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;
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));
808 *ifap = &ifas[0].ifa;
810 exit_free:
811 __netlink_free_handle (&nh);
812 __netlink_close (&nh);
814 return result;
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'. */
822 __getifaddrs (struct ifaddrs **ifap)
824 int res;
827 res = getifaddrs_internal (ifap);
828 while (res == -EAGAIN);
830 return res;
832 weak_alias (__getifaddrs, getifaddrs)
833 libc_hidden_weak (getifaddrs)
836 void
837 __freeifaddrs (struct ifaddrs *ifa)
839 free (ifa);
841 weak_alias (__freeifaddrs, freeifaddrs)
842 libc_hidden_weak (freeifaddrs)