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/>. */
24 #include <netinet/in.h>
25 #include <netpacket/packet.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.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
43 struct sockaddr_ll_max
45 unsigned short int sll_family
;
46 unsigned short int sll_protocol
;
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
62 /* Save space for the biggest of the four used sockaddr types and
63 avoid a lot of casts. */
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];
74 __netlink_free_handle (struct netlink_handle
*h
)
76 struct netlink_res
*ptr
;
77 int saved_errno
= errno
;
82 struct netlink_res
*tmpptr
;
89 __set_errno (saved_errno
);
94 __netlink_sendreq (struct netlink_handle
*h
, int type
)
102 struct sockaddr_nl nladdr
;
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
,
126 __netlink_request (struct netlink_handle
*h
, int type
)
128 struct netlink_res
*nlm_next
;
129 struct sockaddr_nl nladdr
;
130 struct nlmsghdr
*nlmh
;
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
;
139 const size_t buf_size
= __getpagesize ();
141 bool use_malloc
= false;
144 if (__libc_use_alloca (buf_size
))
145 buf
= alloca (buf_size
);
148 buf
= malloc (buf_size
);
155 struct iovec iov
= { buf
, buf_size
};
157 if (__netlink_sendreq (h
, type
) < 0)
164 (void *) &nladdr
, sizeof (nladdr
),
170 read_len
= TEMP_FAILURE_RETRY (__recvmsg (h
->fd
, &msg
, 0));
174 if (nladdr
.nl_pid
!= 0)
177 if (__glibc_unlikely (msg
.msg_flags
& MSG_TRUNC
))
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
)
191 if (nlmh
->nlmsg_type
== NLMSG_DONE
)
193 /* We found the end, leave the loop. */
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
)))
203 errno
= -nlerr
->error
;
208 /* If there was nothing with the expected nlmsg_pid and nlmsg_seq,
209 there is no point to record it. */
213 nlm_next
= (struct netlink_res
*) malloc (sizeof (struct netlink_res
)
215 if (nlm_next
== NULL
)
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
;
224 h
->end_ptr
->next
= nlm_next
;
225 h
->end_ptr
= nlm_next
;
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
);
258 memset (&nladdr
, '\0', sizeof (nladdr
));
259 nladdr
.nl_family
= AF_NETLINK
;
260 if (__bind (h
->fd
, (struct sockaddr
*) &nladdr
, sizeof (nladdr
)) < 0)
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
270 socklen_t addr_len
= sizeof (nladdr
);
271 if (__getsockname (h
->fd
, (struct sockaddr
*) &nladdr
, &addr_len
) < 0)
273 h
->pid
= nladdr
.nl_pid
;
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. */
286 map_newlink (int index
, struct ifaddrs_storage
*ifas
, int *map
, int max
)
290 for (i
= 0; i
< max
; i
++)
296 ifas
[i
- 1].ifa
.ifa_next
= &ifas
[i
].ifa
;
299 else if (map
[i
] == index
)
303 /* This means interfaces changed between the reading of the
304 RTM_GETLINK and RTM_GETADDR information. We have to repeat
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'. */
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
328 if (__netlink_open (&nh
) < 0)
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)
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. */
345 if (__netlink_request (&nh
, RTM_GETADDR
) < 0)
351 /* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate
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
)
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
)
370 if (nlh
->nlmsg_type
== NLMSG_DONE
)
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
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
;
392 rta
= RTA_NEXT (rta
, rtasize
);
396 else if (nlh
->nlmsg_type
== RTM_NEWADDR
)
401 /* Return if no interface is up. */
402 if ((newlink
+ newaddr
) == 0)
405 /* Allocate memory for all entries we have and initialize next
407 ifas
= (struct ifaddrs_storage
*) calloc (1,
409 * sizeof (struct ifaddrs_storage
)
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
)
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
437 for (nlh
= nlp
->nlh
; NLMSG_OK (nlh
, size
); nlh
= NLMSG_NEXT (nlh
, size
))
441 /* Check if the message is the one we want */
442 if ((pid_t
) nlh
->nlmsg_pid
!= nh
.pid
|| nlh
->nlmsg_seq
!= nlp
->seq
)
445 if (nlh
->nlmsg_type
== NLMSG_DONE
)
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
459 ifa_index
= map_newlink (ifim
->ifi_index
- 1, ifas
,
460 map_newlink_data
, newlink
);
461 if (__glibc_unlikely (ifa_index
== -1))
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
)
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
486 ifas
[ifa_index
].addr
.sl
.sll_hatype
= ifim
->ifi_type
;
488 ifas
[ifa_index
].ifa
.ifa_addr
489 = &ifas
[ifa_index
].addr
.sa
;
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
502 ifas
[ifa_index
].broadaddr
.sl
.sll_hatype
505 ifas
[ifa_index
].ifa
.ifa_broadaddr
506 = &ifas
[ifa_index
].broadaddr
.sa
;
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
,
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
,
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))
556 ifas
[ifa_index
].ifa
.ifa_flags
= ifas
[idx
].ifa
.ifa_flags
;
558 ifas
[ifa_index
- 1].ifa
.ifa_next
= &ifas
[ifa_index
].ifa
;
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
)
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
;
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
)
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
);
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
616 if (rta_payload
<= sizeof (ifas
[ifa_index
].addr
))
617 memcpy (sa
->sa_data
, rta_data
, rta_payload
);
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
640 switch (ifam
->ifa_family
)
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
);
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
=
663 if (rta_payload
<= sizeof (ifas
[ifa_index
].addr
))
664 memcpy (ifas
[ifa_index
].addr
.sa
.sa_data
,
665 rta_data
, rta_payload
);
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
681 switch (ifam
->ifa_family
)
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
);
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
704 if (rta_payload
<= sizeof (ifas
[ifa_index
].addr
))
705 memcpy (&ifas
[ifa_index
].broadaddr
.sa
.sa_data
,
706 rta_data
, rta_payload
);
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
,
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))
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;
752 ifas
[ifa_index
].ifa
.ifa_netmask
753 = &ifas
[ifa_index
].netmask
.sa
;
755 switch (ifas
[ifa_index
].ifa
.ifa_addr
->sa_family
)
758 cp
= (char *) &ifas
[ifa_index
].netmask
.s4
.sin_addr
;
763 cp
= (char *) &ifas
[ifa_index
].netmask
.s6
.sin6_addr
;
768 ifas
[ifa_index
].ifa
.ifa_netmask
->sa_family
769 = ifas
[ifa_index
].ifa
.ifa_addr
->sa_family
;
773 unsigned int preflen
;
775 if (ifam
->ifa_prefixlen
> max_prefixlen
)
776 preflen
= max_prefixlen
;
778 preflen
= ifam
->ifa_prefixlen
;
780 for (i
= 0; i
< preflen
/ 8; i
++)
783 *cp
= 0xff << (8 - preflen
% 8);
790 assert (ifa_data_ptr
<= (char *) &ifas
[newlink
+ newaddr
] + ifa_data_size
);
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
;
811 __netlink_free_handle (&nh
);
812 __netlink_close (&nh
);
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
)
827 res
= getifaddrs_internal (ifap
);
828 while (res
== -EAGAIN
);
832 weak_alias (__getifaddrs
, getifaddrs
)
833 libc_hidden_weak (getifaddrs
)
837 __freeifaddrs (struct ifaddrs
*ifa
)
841 weak_alias (__freeifaddrs
, freeifaddrs
)
842 libc_hidden_weak (freeifaddrs
)