* sysdeps/unix/sysv/linux/ifaddrs.c (__netlink_request): Fix
[glibc/pb-stable.git] / sysdeps / unix / sysv / linux / ifaddrs.c
blobe43f39f9e3ed4c7d92397849d9f1855844431bc7
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 netlink_res **new_nlm_list;
126 static volatile size_t buf_size = 4096;
127 char *buf;
128 struct sockaddr_nl nladdr;
129 struct nlmsghdr *nlmh;
130 ssize_t read_len;
131 bool done = false;
132 bool use_malloc = false;
134 if (__netlink_sendreq (h, type) < 0)
135 return -1;
137 size_t this_buf_size = buf_size;
138 if (__libc_use_alloca (this_buf_size))
139 buf = alloca (this_buf_size);
140 else
142 buf = malloc (this_buf_size);
143 if (buf != NULL)
144 use_malloc = true;
145 else
146 goto out_fail;
149 struct iovec iov = { buf, this_buf_size };
151 if (h->nlm_list != NULL)
152 new_nlm_list = &h->end_ptr->next;
153 else
154 new_nlm_list = &h->nlm_list;
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))
175 if (this_buf_size >= SIZE_MAX / 2)
176 goto out_fail;
178 nlm_next = *new_nlm_list;
179 while (nlm_next != NULL)
181 struct netlink_res *tmpptr;
183 tmpptr = nlm_next->next;
184 free (nlm_next);
185 nlm_next = tmpptr;
187 *new_nlm_list = NULL;
189 if (__libc_use_alloca (2 * this_buf_size))
190 buf = extend_alloca (buf, this_buf_size, 2 * this_buf_size);
191 else
193 this_buf_size *= 2;
195 char *new_buf = realloc (use_malloc ? buf : NULL, this_buf_size);
196 if (new_buf == NULL)
197 goto out_fail;
198 buf = new_buf;
200 use_malloc = true;
202 buf_size = this_buf_size;
204 iov.iov_base = buf;
205 iov.iov_len = this_buf_size;
207 /* Increase sequence number, so that we can distinguish
208 between old and new request messages. */
209 h->seq++;
211 if (__netlink_sendreq (h, type) < 0)
212 goto out_fail;
214 continue;
217 size_t count = 0;
218 size_t remaining_len = read_len;
219 for (nlmh = (struct nlmsghdr *) buf;
220 NLMSG_OK (nlmh, remaining_len);
221 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, remaining_len))
223 if ((pid_t) nlmh->nlmsg_pid != h->pid
224 || nlmh->nlmsg_seq != h->seq)
225 continue;
227 ++count;
228 if (nlmh->nlmsg_type == NLMSG_DONE)
230 /* We found the end, leave the loop. */
231 done = true;
232 break;
234 if (nlmh->nlmsg_type == NLMSG_ERROR)
236 struct nlmsgerr *nlerr = (struct nlmsgerr *) NLMSG_DATA (nlmh);
237 if (nlmh->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
238 errno = EIO;
239 else
240 errno = -nlerr->error;
241 goto out_fail;
245 /* If there was nothing with the expected nlmsg_pid and nlmsg_seq,
246 there is no point to record it. */
247 if (count == 0)
248 continue;
250 nlm_next = (struct netlink_res *) malloc (sizeof (struct netlink_res)
251 + read_len);
252 if (nlm_next == NULL)
253 goto out_fail;
254 nlm_next->next = NULL;
255 nlm_next->nlh = memcpy (nlm_next + 1, buf, read_len);
256 nlm_next->size = read_len;
257 nlm_next->seq = h->seq;
258 if (h->nlm_list == NULL)
259 h->nlm_list = nlm_next;
260 else
261 h->end_ptr->next = nlm_next;
262 h->end_ptr = nlm_next;
265 if (use_malloc)
266 free (buf);
267 return 0;
269 out_fail:
270 if (use_malloc)
271 free (buf);
272 return -1;
276 void
277 __netlink_close (struct netlink_handle *h)
279 /* Don't modify errno. */
280 INTERNAL_SYSCALL_DECL (err);
281 (void) INTERNAL_SYSCALL (close, err, 1, h->fd);
285 /* Open a NETLINK socket. */
287 __netlink_open (struct netlink_handle *h)
289 struct sockaddr_nl nladdr;
291 h->fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
292 if (h->fd < 0)
293 goto out;
295 memset (&nladdr, '\0', sizeof (nladdr));
296 nladdr.nl_family = AF_NETLINK;
297 if (__bind (h->fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) < 0)
299 close_and_out:
300 __netlink_close (h);
301 out:
302 #if __ASSUME_NETLINK_SUPPORT == 0
303 __no_netlink_support = 1;
304 #endif
305 return -1;
307 /* Determine the ID the kernel assigned for this netlink connection.
308 It is not necessarily the PID if there is more than one socket
309 open. */
310 socklen_t addr_len = sizeof (nladdr);
311 if (__getsockname (h->fd, (struct sockaddr *) &nladdr, &addr_len) < 0)
312 goto close_and_out;
313 h->pid = nladdr.nl_pid;
314 return 0;
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 index, 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] = index;
335 if (i > 0)
336 ifas[i - 1].ifa.ifa_next = &ifas[i].ifa;
337 return i;
339 else if (map[i] == index)
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 *ifap = NULL;
366 if (! __no_netlink_support && __netlink_open (&nh) < 0)
368 #if __ASSUME_NETLINK_SUPPORT != 0
369 return -1;
370 #endif
373 #if __ASSUME_NETLINK_SUPPORT == 0
374 if (__no_netlink_support)
375 return fallback_getifaddrs (ifap);
376 #endif
378 /* Tell the kernel that we wish to get a list of all
379 active interfaces, collect all data for every interface. */
380 if (__netlink_request (&nh, RTM_GETLINK) < 0)
382 result = -1;
383 goto exit_free;
386 /* Now ask the kernel for all addresses which are assigned
387 to an interface and collect all data for every interface.
388 Since we store the addresses after the interfaces in the
389 list, we will later always find the interface before the
390 corresponding addresses. */
391 ++nh.seq;
392 if (__netlink_request (&nh, RTM_GETADDR) < 0)
394 result = -1;
395 goto exit_free;
398 /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate
399 enough memory. */
400 newlink = newaddr = 0;
401 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
403 struct nlmsghdr *nlh;
404 size_t size = nlp->size;
406 if (nlp->nlh == NULL)
407 continue;
409 /* Walk through all entries we got from the kernel and look, which
410 message type they contain. */
411 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
413 /* Check if the message is what we want. */
414 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
415 continue;
417 if (nlh->nlmsg_type == NLMSG_DONE)
418 break; /* ok */
420 if (nlh->nlmsg_type == RTM_NEWLINK)
422 /* A RTM_NEWLINK message can have IFLA_STATS data. We need to
423 know the size before creating the list to allocate enough
424 memory. */
425 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
426 struct rtattr *rta = IFLA_RTA (ifim);
427 size_t rtasize = IFLA_PAYLOAD (nlh);
429 while (RTA_OK (rta, rtasize))
431 size_t rta_payload = RTA_PAYLOAD (rta);
433 if (rta->rta_type == IFLA_STATS)
435 ifa_data_size += rta_payload;
436 break;
438 else
439 rta = RTA_NEXT (rta, rtasize);
441 ++newlink;
443 else if (nlh->nlmsg_type == RTM_NEWADDR)
444 ++newaddr;
448 /* Return if no interface is up. */
449 if ((newlink + newaddr) == 0)
450 goto exit_free;
452 /* Allocate memory for all entries we have and initialize next
453 pointer. */
454 ifas = (struct ifaddrs_storage *) calloc (1,
455 (newlink + newaddr)
456 * sizeof (struct ifaddrs_storage)
457 + ifa_data_size);
458 if (ifas == NULL)
460 result = -1;
461 goto exit_free;
464 /* Table for mapping kernel index to entry in our list. */
465 map_newlink_data = alloca (newlink * sizeof (int));
466 memset (map_newlink_data, '\xff', newlink * sizeof (int));
468 ifa_data_ptr = (char *) &ifas[newlink + newaddr];
469 newaddr_idx = 0; /* Counter for newaddr index. */
471 /* Walk through the list of data we got from the kernel. */
472 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
474 struct nlmsghdr *nlh;
475 size_t size = nlp->size;
477 if (nlp->nlh == NULL)
478 continue;
480 /* Walk through one message and look at the type: If it is our
481 message, we need RTM_NEWLINK/RTM_NEWADDR and stop if we reach
482 the end or we find the end marker (in this case we ignore the
483 following data. */
484 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
486 int ifa_index = 0;
488 /* Check if the message is the one we want */
489 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
490 continue;
492 if (nlh->nlmsg_type == NLMSG_DONE)
493 break; /* ok */
495 if (nlh->nlmsg_type == RTM_NEWLINK)
497 /* We found a new interface. Now extract everything from the
498 interface data we got and need. */
499 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
500 struct rtattr *rta = IFLA_RTA (ifim);
501 size_t rtasize = IFLA_PAYLOAD (nlh);
503 /* Interfaces are stored in the first "newlink" entries
504 of our list, starting in the order as we got from the
505 kernel. */
506 ifa_index = map_newlink (ifim->ifi_index - 1, ifas,
507 map_newlink_data, newlink);
508 ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags;
510 while (RTA_OK (rta, rtasize))
512 char *rta_data = RTA_DATA (rta);
513 size_t rta_payload = RTA_PAYLOAD (rta);
515 switch (rta->rta_type)
517 case IFLA_ADDRESS:
518 if (rta_payload <= sizeof (ifas[ifa_index].addr))
520 ifas[ifa_index].addr.sl.sll_family = AF_PACKET;
521 memcpy (ifas[ifa_index].addr.sl.sll_addr,
522 (char *) rta_data, rta_payload);
523 ifas[ifa_index].addr.sl.sll_halen = rta_payload;
524 ifas[ifa_index].addr.sl.sll_ifindex
525 = ifim->ifi_index;
526 ifas[ifa_index].addr.sl.sll_hatype = ifim->ifi_type;
528 ifas[ifa_index].ifa.ifa_addr
529 = &ifas[ifa_index].addr.sa;
531 break;
533 case IFLA_BROADCAST:
534 if (rta_payload <= sizeof (ifas[ifa_index].broadaddr))
536 ifas[ifa_index].broadaddr.sl.sll_family = AF_PACKET;
537 memcpy (ifas[ifa_index].broadaddr.sl.sll_addr,
538 (char *) rta_data, rta_payload);
539 ifas[ifa_index].broadaddr.sl.sll_halen = rta_payload;
540 ifas[ifa_index].broadaddr.sl.sll_ifindex
541 = ifim->ifi_index;
542 ifas[ifa_index].broadaddr.sl.sll_hatype
543 = ifim->ifi_type;
545 ifas[ifa_index].ifa.ifa_broadaddr
546 = &ifas[ifa_index].broadaddr.sa;
548 break;
550 case IFLA_IFNAME: /* Name of Interface */
551 if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name))
553 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
554 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
555 rta_payload) = '\0';
557 break;
559 case IFLA_STATS: /* Statistics of Interface */
560 ifas[ifa_index].ifa.ifa_data = ifa_data_ptr;
561 ifa_data_ptr += rta_payload;
562 memcpy (ifas[ifa_index].ifa.ifa_data, rta_data,
563 rta_payload);
564 break;
566 case IFLA_UNSPEC:
567 break;
568 case IFLA_MTU:
569 break;
570 case IFLA_LINK:
571 break;
572 case IFLA_QDISC:
573 break;
574 default:
575 break;
578 rta = RTA_NEXT (rta, rtasize);
581 else if (nlh->nlmsg_type == RTM_NEWADDR)
583 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh);
584 struct rtattr *rta = IFA_RTA (ifam);
585 size_t rtasize = IFA_PAYLOAD (nlh);
587 /* New Addresses are stored in the order we got them from
588 the kernel after the interfaces. Theoretically it is possible
589 that we have holes in the interface part of the list,
590 but we always have already the interface for this address. */
591 ifa_index = newlink + newaddr_idx;
592 ifas[ifa_index].ifa.ifa_flags
593 = ifas[map_newlink (ifam->ifa_index - 1, ifas,
594 map_newlink_data, newlink)].ifa.ifa_flags;
595 if (ifa_index > 0)
596 ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa;
597 ++newaddr_idx;
599 while (RTA_OK (rta, rtasize))
601 char *rta_data = RTA_DATA (rta);
602 size_t rta_payload = RTA_PAYLOAD (rta);
604 switch (rta->rta_type)
606 case IFA_ADDRESS:
608 struct sockaddr *sa;
610 if (ifas[ifa_index].ifa.ifa_addr != NULL)
612 /* In a point-to-poing network IFA_ADDRESS
613 contains the destination address, local
614 address is supplied in IFA_LOCAL attribute.
615 destination address and broadcast address
616 are stored in an union, so it doesn't matter
617 which name we use. */
618 ifas[ifa_index].ifa.ifa_broadaddr
619 = &ifas[ifa_index].broadaddr.sa;
620 sa = &ifas[ifa_index].broadaddr.sa;
622 else
624 ifas[ifa_index].ifa.ifa_addr
625 = &ifas[ifa_index].addr.sa;
626 sa = &ifas[ifa_index].addr.sa;
629 sa->sa_family = ifam->ifa_family;
631 switch (ifam->ifa_family)
633 case AF_INET:
634 /* Size must match that of an address for IPv4. */
635 if (rta_payload == 4)
636 memcpy (&((struct sockaddr_in *) sa)->sin_addr,
637 rta_data, rta_payload);
638 break;
640 case AF_INET6:
641 /* Size must match that of an address for IPv6. */
642 if (rta_payload == 16)
644 memcpy (&((struct sockaddr_in6 *) sa)->sin6_addr,
645 rta_data, rta_payload);
646 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
647 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
648 ((struct sockaddr_in6 *) sa)->sin6_scope_id
649 = ifam->ifa_index;
651 break;
653 default:
654 if (rta_payload <= sizeof (ifas[ifa_index].addr))
655 memcpy (sa->sa_data, rta_data, rta_payload);
656 break;
659 break;
661 case IFA_LOCAL:
662 if (ifas[ifa_index].ifa.ifa_addr != NULL)
664 /* If ifa_addr is set and we get IFA_LOCAL,
665 assume we have a point-to-point network.
666 Move address to correct field. */
667 ifas[ifa_index].broadaddr = ifas[ifa_index].addr;
668 ifas[ifa_index].ifa.ifa_broadaddr
669 = &ifas[ifa_index].broadaddr.sa;
670 memset (&ifas[ifa_index].addr, '\0',
671 sizeof (ifas[ifa_index].addr));
674 ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa;
675 ifas[ifa_index].ifa.ifa_addr->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].addr.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].addr.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].addr.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].addr.sa.sa_data,
703 rta_data, rta_payload);
704 break;
706 break;
708 case IFA_BROADCAST:
709 /* We get IFA_BROADCAST, so IFA_LOCAL was too much. */
710 if (ifas[ifa_index].ifa.ifa_broadaddr != NULL)
711 memset (&ifas[ifa_index].broadaddr, '\0',
712 sizeof (ifas[ifa_index].broadaddr));
714 ifas[ifa_index].ifa.ifa_broadaddr
715 = &ifas[ifa_index].broadaddr.sa;
716 ifas[ifa_index].ifa.ifa_broadaddr->sa_family
717 = ifam->ifa_family;
719 switch (ifam->ifa_family)
721 case AF_INET:
722 /* Size must match that of an address for IPv4. */
723 if (rta_payload == 4)
724 memcpy (&ifas[ifa_index].broadaddr.s4.sin_addr,
725 rta_data, rta_payload);
726 break;
728 case AF_INET6:
729 /* Size must match that of an address for IPv6. */
730 if (rta_payload == 16)
732 memcpy (&ifas[ifa_index].broadaddr.s6.sin6_addr,
733 rta_data, rta_payload);
734 if (IN6_IS_ADDR_LINKLOCAL (rta_data)
735 || IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
736 ifas[ifa_index].broadaddr.s6.sin6_scope_id
737 = ifam->ifa_index;
739 break;
741 default:
742 if (rta_payload <= sizeof (ifas[ifa_index].addr))
743 memcpy (&ifas[ifa_index].broadaddr.sa.sa_data,
744 rta_data, rta_payload);
745 break;
747 break;
749 case IFA_LABEL:
750 if (rta_payload + 1 <= sizeof (ifas[ifa_index].name))
752 ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
753 *(char *) __mempcpy (ifas[ifa_index].name, rta_data,
754 rta_payload) = '\0';
756 else
757 abort ();
758 break;
760 case IFA_UNSPEC:
761 break;
762 case IFA_CACHEINFO:
763 break;
764 default:
765 break;
768 rta = RTA_NEXT (rta, rtasize);
771 /* If we didn't get the interface name with the
772 address, use the name from the interface entry. */
773 if (ifas[ifa_index].ifa.ifa_name == NULL)
774 ifas[ifa_index].ifa.ifa_name
775 = ifas[map_newlink (ifam->ifa_index - 1, ifas,
776 map_newlink_data, newlink)].ifa.ifa_name;
778 /* Calculate the netmask. */
779 if (ifas[ifa_index].ifa.ifa_addr
780 && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_UNSPEC
781 && ifas[ifa_index].ifa.ifa_addr->sa_family != AF_PACKET)
783 uint32_t max_prefixlen = 0;
784 char *cp = NULL;
786 ifas[ifa_index].ifa.ifa_netmask
787 = &ifas[ifa_index].netmask.sa;
789 switch (ifas[ifa_index].ifa.ifa_addr->sa_family)
791 case AF_INET:
792 cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr;
793 max_prefixlen = 32;
794 break;
796 case AF_INET6:
797 cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr;
798 max_prefixlen = 128;
799 break;
802 ifas[ifa_index].ifa.ifa_netmask->sa_family
803 = ifas[ifa_index].ifa.ifa_addr->sa_family;
805 if (cp != NULL)
807 char c;
808 unsigned int preflen;
810 if ((max_prefixlen > 0) &&
811 (ifam->ifa_prefixlen > max_prefixlen))
812 preflen = max_prefixlen;
813 else
814 preflen = ifam->ifa_prefixlen;
816 for (i = 0; i < (preflen / 8); i++)
817 *cp++ = 0xff;
818 c = 0xff;
819 c <<= (8 - (preflen % 8));
820 *cp = c;
827 assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size);
829 if (newaddr_idx > 0)
831 for (i = 0; i < newlink; ++i)
832 if (map_newlink_data[i] == -1)
834 /* We have fewer links then we anticipated. Adjust the
835 forward pointer to the first address entry. */
836 ifas[i - 1].ifa.ifa_next = &ifas[newlink].ifa;
839 if (i == 0 && newlink > 0)
840 /* No valid link, but we allocated memory. We have to
841 populate the first entry. */
842 memmove (ifas, &ifas[newlink], sizeof (struct ifaddrs_storage));
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)
856 #if __ASSUME_NETLINK_SUPPORT != 0
857 void
858 freeifaddrs (struct ifaddrs *ifa)
860 free (ifa);
862 libc_hidden_def (freeifaddrs)
863 #endif