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
25 #include <netinet/in.h>
26 #include <netpacket/packet.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.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"
51 /* struct to hold the data for one ifaddrs entry, so we can allocate
52 everything at once. */
53 struct ifaddrs_storage
58 /* Save space for the biggest of the four used sockaddr types and
59 avoid a lot of casts. */
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];
70 __netlink_free_handle (struct netlink_handle
*h
)
72 struct netlink_res
*ptr
;
73 int saved_errno
= errno
;
78 struct netlink_res
*tmpptr
;
85 __set_errno (saved_errno
);
90 __netlink_sendreq (struct netlink_handle
*h
, int type
)
98 struct sockaddr_nl nladdr
;
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
,
122 __netlink_request (struct netlink_handle
*h
, int type
)
124 struct netlink_res
*nlm_next
;
125 struct sockaddr_nl nladdr
;
126 struct nlmsghdr
*nlmh
;
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
;
135 const size_t buf_size
= __getpagesize ();
137 bool use_malloc
= false;
140 if (__libc_use_alloca (buf_size
))
141 buf
= alloca (buf_size
);
144 buf
= malloc (buf_size
);
151 struct iovec iov
= { buf
, buf_size
};
153 if (__netlink_sendreq (h
, type
) < 0)
160 (void *) &nladdr
, sizeof (nladdr
),
166 read_len
= TEMP_FAILURE_RETRY (__recvmsg (h
->fd
, &msg
, 0));
170 if (nladdr
.nl_pid
!= 0)
173 if (__builtin_expect (msg
.msg_flags
& MSG_TRUNC
, 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
)
187 if (nlmh
->nlmsg_type
== NLMSG_DONE
)
189 /* We found the end, leave the loop. */
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
)))
199 errno
= -nlerr
->error
;
204 /* If there was nothing with the expected nlmsg_pid and nlmsg_seq,
205 there is no point to record it. */
209 nlm_next
= (struct netlink_res
*) malloc (sizeof (struct netlink_res
)
211 if (nlm_next
== NULL
)
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
;
220 h
->end_ptr
->next
= nlm_next
;
221 h
->end_ptr
= nlm_next
;
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
);
254 memset (&nladdr
, '\0', sizeof (nladdr
));
255 nladdr
.nl_family
= AF_NETLINK
;
256 if (__bind (h
->fd
, (struct sockaddr
*) &nladdr
, sizeof (nladdr
)) < 0)
261 #if __ASSUME_NETLINK_SUPPORT == 0
262 __no_netlink_support
= 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
269 socklen_t addr_len
= sizeof (nladdr
);
270 if (__getsockname (h
->fd
, (struct sockaddr
*) &nladdr
, &addr_len
) < 0)
272 h
->pid
= nladdr
.nl_pid
;
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. */
285 map_newlink (int index
, struct ifaddrs_storage
*ifas
, int *map
, int max
)
289 for (i
= 0; i
< max
; i
++)
295 ifas
[i
- 1].ifa
.ifa_next
= &ifas
[i
].ifa
;
298 else if (map
[i
] == index
)
301 /* This should never be reached. If this will be reached, we have
302 a very big problem. */
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
325 if (! __no_netlink_support
&& __netlink_open (&nh
) < 0)
327 #if __ASSUME_NETLINK_SUPPORT != 0
332 #if __ASSUME_NETLINK_SUPPORT == 0
333 if (__no_netlink_support
)
334 return fallback_getifaddrs (ifap
);
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)
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. */
351 if (__netlink_request (&nh
, RTM_GETADDR
) < 0)
357 /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate
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
)
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
)
376 if (nlh
->nlmsg_type
== NLMSG_DONE
)
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
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
;
398 rta
= RTA_NEXT (rta
, rtasize
);
402 else if (nlh
->nlmsg_type
== RTM_NEWADDR
)
407 /* Return if no interface is up. */
408 if ((newlink
+ newaddr
) == 0)
411 /* Allocate memory for all entries we have and initialize next
413 ifas
= (struct ifaddrs_storage
*) calloc (1,
415 * sizeof (struct ifaddrs_storage
)
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
)
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
443 for (nlh
= nlp
->nlh
; NLMSG_OK (nlh
, size
); nlh
= NLMSG_NEXT (nlh
, size
))
447 /* Check if the message is the one we want */
448 if ((pid_t
) nlh
->nlmsg_pid
!= nh
.pid
|| nlh
->nlmsg_seq
!= nlp
->seq
)
451 if (nlh
->nlmsg_type
== NLMSG_DONE
)
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
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
)
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
485 ifas
[ifa_index
].addr
.sl
.sll_hatype
= ifim
->ifi_type
;
487 ifas
[ifa_index
].ifa
.ifa_addr
488 = &ifas
[ifa_index
].addr
.sa
;
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
501 ifas
[ifa_index
].broadaddr
.sl
.sll_hatype
504 ifas
[ifa_index
].ifa
.ifa_broadaddr
505 = &ifas
[ifa_index
].broadaddr
.sa
;
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
,
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
,
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
;
555 ifas
[ifa_index
- 1].ifa
.ifa_next
= &ifas
[ifa_index
].ifa
;
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
)
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
;
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
)
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
);
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
613 if (rta_payload
<= sizeof (ifas
[ifa_index
].addr
))
614 memcpy (sa
->sa_data
, rta_data
, rta_payload
);
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
637 switch (ifam
->ifa_family
)
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
);
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
=
660 if (rta_payload
<= sizeof (ifas
[ifa_index
].addr
))
661 memcpy (ifas
[ifa_index
].addr
.sa
.sa_data
,
662 rta_data
, rta_payload
);
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
678 switch (ifam
->ifa_family
)
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
);
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
701 if (rta_payload
<= sizeof (ifas
[ifa_index
].addr
))
702 memcpy (&ifas
[ifa_index
].broadaddr
.sa
.sa_data
,
703 rta_data
, rta_payload
);
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
,
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;
745 ifas
[ifa_index
].ifa
.ifa_netmask
746 = &ifas
[ifa_index
].netmask
.sa
;
748 switch (ifas
[ifa_index
].ifa
.ifa_addr
->sa_family
)
751 cp
= (char *) &ifas
[ifa_index
].netmask
.s4
.sin_addr
;
756 cp
= (char *) &ifas
[ifa_index
].netmask
.s6
.sin6_addr
;
761 ifas
[ifa_index
].ifa
.ifa_netmask
->sa_family
762 = ifas
[ifa_index
].ifa
.ifa_addr
->sa_family
;
767 unsigned int preflen
;
769 if ((max_prefixlen
> 0) &&
770 (ifam
->ifa_prefixlen
> max_prefixlen
))
771 preflen
= max_prefixlen
;
773 preflen
= ifam
->ifa_prefixlen
;
775 for (i
= 0; i
< (preflen
/ 8); i
++)
778 c
<<= (8 - (preflen
% 8));
786 assert (ifa_data_ptr
<= (char *) &ifas
[newlink
+ newaddr
] + ifa_data_size
);
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
;
807 __netlink_free_handle (&nh
);
808 __netlink_close (&nh
);
812 libc_hidden_def (getifaddrs
)
815 #if __ASSUME_NETLINK_SUPPORT != 0
817 freeifaddrs (struct ifaddrs
*ifa
)
821 libc_hidden_def (freeifaddrs
)