1 /* Determine protocol families for which interfaces exist. Linux version.
2 Copyright (C) 2003, 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
28 #include <sys/socket.h>
30 #include <not-cancel.h>
31 #include <kernel-features.h>
33 #include "netlinkaccess.h"
35 #ifndef IFA_F_HOMEADDRESS
36 # define IFA_F_HOMEADDRESS 0
38 #ifndef IFA_F_OPTIMISTIC
39 # define IFA_F_OPTIMISTIC 0
44 make_request (int fd
, pid_t pid
, bool *seen_ipv4
, bool *seen_ipv6
,
45 struct in6addrinfo
**in6ai
, size_t *in6ailen
)
51 /* struct rtgenmsg consists of a single byte. This means there
52 are three bytes of padding included in the REQ definition.
53 We make them explicit here. */
56 struct sockaddr_nl nladdr
;
58 req
.nlh
.nlmsg_len
= sizeof (req
);
59 req
.nlh
.nlmsg_type
= RTM_GETADDR
;
60 req
.nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
61 req
.nlh
.nlmsg_pid
= 0;
62 req
.nlh
.nlmsg_seq
= time (NULL
);
63 req
.g
.rtgen_family
= AF_UNSPEC
;
65 assert (sizeof (req
) - offsetof (struct req
, pad
) == 3);
66 memset (req
.pad
, '\0', sizeof (req
.pad
));
68 memset (&nladdr
, '\0', sizeof (nladdr
));
69 nladdr
.nl_family
= AF_NETLINK
;
72 /* Help the compiler optimize out the malloc call if PAGE_SIZE
73 is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
74 const size_t buf_size
= PAGE_SIZE
;
76 const size_t buf_size
= __getpagesize ();
78 bool use_malloc
= false;
81 if (__libc_use_alloca (buf_size
))
82 buf
= alloca (buf_size
);
85 buf
= malloc (buf_size
);
92 struct iovec iov
= { buf
, buf_size
};
94 if (TEMP_FAILURE_RETRY (__sendto (fd
, (void *) &req
, sizeof (req
), 0,
95 (struct sockaddr
*) &nladdr
,
96 sizeof (nladdr
))) < 0)
105 struct in6addrinfo info
;
106 struct in6ailist
*next
;
108 size_t in6ailistlen
= 0;
114 (void *) &nladdr
, sizeof (nladdr
),
120 ssize_t read_len
= TEMP_FAILURE_RETRY (__recvmsg (fd
, &msg
, 0));
124 if (msg
.msg_flags
& MSG_TRUNC
)
127 struct nlmsghdr
*nlmh
;
128 for (nlmh
= (struct nlmsghdr
*) buf
;
129 NLMSG_OK (nlmh
, (size_t) read_len
);
130 nlmh
= (struct nlmsghdr
*) NLMSG_NEXT (nlmh
, read_len
))
132 if (nladdr
.nl_pid
!= 0 || (pid_t
) nlmh
->nlmsg_pid
!= pid
133 || nlmh
->nlmsg_seq
!= req
.nlh
.nlmsg_seq
)
136 if (nlmh
->nlmsg_type
== RTM_NEWADDR
)
138 struct ifaddrmsg
*ifam
= (struct ifaddrmsg
*) NLMSG_DATA (nlmh
);
139 struct rtattr
*rta
= IFA_RTA (ifam
);
140 size_t len
= nlmh
->nlmsg_len
- NLMSG_LENGTH (sizeof (*ifam
));
142 if (ifam
->ifa_family
!= AF_INET
143 && ifam
->ifa_family
!= AF_INET6
)
146 const void *local
= NULL
;
147 const void *address
= NULL
;
148 while (RTA_OK (rta
, len
))
150 switch (rta
->rta_type
)
153 local
= RTA_DATA (rta
);
157 address
= RTA_DATA (rta
);
161 rta
= RTA_NEXT (rta
, len
);
168 if (ifam
->ifa_family
!= AF_INET
)
170 if (*(const in_addr_t
*) address
171 != htonl (INADDR_LOOPBACK
))
176 if (!IN6_IS_ADDR_LOOPBACK (address
))
181 struct in6ailist
*newp
= alloca (sizeof (*newp
));
182 newp
->info
.flags
= (((ifam
->ifa_flags
185 ? in6ai_deprecated
: 0)
188 ? in6ai_homeaddress
: 0));
189 newp
->info
.prefixlen
= ifam
->ifa_prefixlen
;
190 newp
->info
.index
= ifam
->ifa_index
;
191 if (ifam
->ifa_family
== AF_INET
)
193 newp
->info
.addr
[0] = 0;
194 newp
->info
.addr
[1] = 0;
195 newp
->info
.addr
[2] = htonl (0xffff);
196 newp
->info
.addr
[3] = *(const in_addr_t
*) address
;
199 memcpy (newp
->info
.addr
, address
, sizeof (newp
->info
.addr
));
200 newp
->next
= in6ailist
;
204 else if (nlmh
->nlmsg_type
== NLMSG_DONE
)
205 /* We found the end, leave the loop. */
211 close_not_cancel_no_status (fd
);
213 if (*seen_ipv6
&& in6ailist
!= NULL
)
215 *in6ai
= malloc (in6ailistlen
* sizeof (**in6ai
));
219 *in6ailen
= in6ailistlen
;
223 (*in6ai
)[--in6ailistlen
] = in6ailist
->info
;
224 in6ailist
= in6ailist
->next
;
226 while (in6ailist
!= NULL
);
240 /* We don't know if we have NETLINK support compiled in in our
242 #if __ASSUME_NETLINK_SUPPORT == 0
243 /* Define in ifaddrs.h. */
244 extern int __no_netlink_support attribute_hidden
;
246 # define __no_netlink_support 0
252 __check_pf (bool *seen_ipv4
, bool *seen_ipv6
,
253 struct in6addrinfo
**in6ai
, size_t *in6ailen
)
258 if (! __no_netlink_support
)
260 int fd
= __socket (PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
262 struct sockaddr_nl nladdr
;
263 memset (&nladdr
, '\0', sizeof (nladdr
));
264 nladdr
.nl_family
= AF_NETLINK
;
266 socklen_t addr_len
= sizeof (nladdr
);
269 && __bind (fd
, (struct sockaddr
*) &nladdr
, sizeof (nladdr
)) == 0
270 && __getsockname (fd
, (struct sockaddr
*) &nladdr
, &addr_len
) == 0
271 && make_request (fd
, nladdr
.nl_pid
, seen_ipv4
, seen_ipv6
,
272 in6ai
, in6ailen
) == 0)
279 #if __ASSUME_NETLINK_SUPPORT == 0
280 /* Remember that there is no netlink support. */
281 __no_netlink_support
= 1;
283 /* We cannot determine what interfaces are available. Be
290 #if __ASSUME_NETLINK_SUPPORT == 0
291 /* No netlink. Get the interface list via getifaddrs. */
292 struct ifaddrs
*ifa
= NULL
;
293 if (getifaddrs (&ifa
) != 0)
295 /* We cannot determine what interfaces are available. Be
302 struct ifaddrs
*runp
;
303 for (runp
= ifa
; runp
!= NULL
; runp
= runp
->ifa_next
)
304 if (runp
->ifa_addr
->sa_family
== PF_INET
)
306 else if (runp
->ifa_addr
->sa_family
== PF_INET6
)
309 (void) freeifaddrs (ifa
);