2003-03-29 07:26:59 +00:00
|
|
|
/* getifaddrs -- get names and addresses of all network interfaces
|
2023-01-06 21:08:04 +00:00
|
|
|
Copyright (C) 2003-2023 Free Software Foundation, Inc.
|
2003-03-29 07:26:59 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2003-03-29 07:26:59 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netpacket/packet.h>
|
2023-06-21 20:00:53 +00:00
|
|
|
#include <scratch_buffer.h>
|
2003-03-29 07:26:59 +00:00
|
|
|
#include <stdbool.h>
|
2005-06-14 00:56:49 +00:00
|
|
|
#include <stdint.h>
|
2003-03-29 07:26:59 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sysdep.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2004-06-30 07:43:11 +00:00
|
|
|
#include "netlinkaccess.h"
|
2003-03-29 07:26:59 +00:00
|
|
|
|
|
|
|
|
2008-08-13 06:57:42 +00:00
|
|
|
/* There is a problem with this type. The address length for
|
|
|
|
Infiniband sockets is much longer than the 8 bytes allocated in the
|
|
|
|
sockaddr_ll definition. Hence we use here a special
|
|
|
|
definition. */
|
|
|
|
struct sockaddr_ll_max
|
|
|
|
{
|
|
|
|
unsigned short int sll_family;
|
|
|
|
unsigned short int sll_protocol;
|
|
|
|
int sll_ifindex;
|
|
|
|
unsigned short int sll_hatype;
|
|
|
|
unsigned char sll_pkttype;
|
|
|
|
unsigned char sll_halen;
|
|
|
|
unsigned char sll_addr[24];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-03-29 07:26:59 +00:00
|
|
|
/* struct to hold the data for one ifaddrs entry, so we can allocate
|
|
|
|
everything at once. */
|
|
|
|
struct ifaddrs_storage
|
|
|
|
{
|
|
|
|
struct ifaddrs ifa;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
/* Save space for the biggest of the four used sockaddr types and
|
|
|
|
avoid a lot of casts. */
|
|
|
|
struct sockaddr sa;
|
2008-08-13 06:57:42 +00:00
|
|
|
struct sockaddr_ll_max sl;
|
2003-03-29 07:26:59 +00:00
|
|
|
struct sockaddr_in s4;
|
|
|
|
struct sockaddr_in6 s6;
|
|
|
|
} addr, netmask, broadaddr;
|
|
|
|
char name[IF_NAMESIZE + 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-06-30 07:43:11 +00:00
|
|
|
void
|
|
|
|
__netlink_free_handle (struct netlink_handle *h)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
|
|
|
struct netlink_res *ptr;
|
|
|
|
int saved_errno = errno;
|
|
|
|
|
|
|
|
ptr = h->nlm_list;
|
|
|
|
while (ptr != NULL)
|
|
|
|
{
|
|
|
|
struct netlink_res *tmpptr;
|
|
|
|
|
|
|
|
tmpptr = ptr->next;
|
|
|
|
free (ptr);
|
|
|
|
ptr = tmpptr;
|
|
|
|
}
|
|
|
|
|
2004-06-30 07:43:11 +00:00
|
|
|
__set_errno (saved_errno);
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-14 00:56:49 +00:00
|
|
|
static int
|
2004-06-30 07:43:11 +00:00
|
|
|
__netlink_sendreq (struct netlink_handle *h, int type)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
2006-05-19 07:55:24 +00:00
|
|
|
struct req
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
|
|
|
struct nlmsghdr nlh;
|
|
|
|
struct rtgenmsg g;
|
2006-05-19 07:55:24 +00:00
|
|
|
char pad[0];
|
2003-03-29 07:26:59 +00:00
|
|
|
} req;
|
|
|
|
struct sockaddr_nl nladdr;
|
|
|
|
|
|
|
|
if (h->seq == 0)
|
2019-10-24 21:01:40 +00:00
|
|
|
h->seq = time_now ();
|
2003-03-29 07:26:59 +00:00
|
|
|
|
|
|
|
req.nlh.nlmsg_len = sizeof (req);
|
|
|
|
req.nlh.nlmsg_type = type;
|
|
|
|
req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
|
|
|
|
req.nlh.nlmsg_pid = 0;
|
|
|
|
req.nlh.nlmsg_seq = h->seq;
|
|
|
|
req.g.rtgen_family = AF_UNSPEC;
|
2006-05-19 07:55:24 +00:00
|
|
|
if (sizeof (req) != offsetof (struct req, pad))
|
|
|
|
memset (req.pad, '\0', sizeof (req) - offsetof (struct req, pad));
|
2003-03-29 07:26:59 +00:00
|
|
|
|
|
|
|
memset (&nladdr, '\0', sizeof (nladdr));
|
|
|
|
nladdr.nl_family = AF_NETLINK;
|
|
|
|
|
2003-04-29 23:49:37 +00:00
|
|
|
return TEMP_FAILURE_RETRY (__sendto (h->fd, (void *) &req, sizeof (req), 0,
|
|
|
|
(struct sockaddr *) &nladdr,
|
|
|
|
sizeof (nladdr)));
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-30 07:43:11 +00:00
|
|
|
int
|
2005-06-14 00:56:49 +00:00
|
|
|
__netlink_request (struct netlink_handle *h, int type)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
|
|
|
struct netlink_res *nlm_next;
|
|
|
|
struct sockaddr_nl nladdr;
|
|
|
|
struct nlmsghdr *nlmh;
|
2005-06-14 00:56:49 +00:00
|
|
|
ssize_t read_len;
|
2003-03-29 07:26:59 +00:00
|
|
|
bool done = false;
|
2005-06-14 00:56:49 +00:00
|
|
|
|
2023-06-21 20:00:53 +00:00
|
|
|
/* Netlink requires that user buffer needs to be either 8kb or page size
|
|
|
|
(whichever is bigger), however this has been changed over time and now
|
|
|
|
8Kb is sufficient (check NLMSG_DEFAULT_SIZE on Linux
|
|
|
|
linux/include/linux/netlink.h). */
|
|
|
|
const size_t buf_size = 8192;
|
|
|
|
char *buf = malloc (buf_size);
|
|
|
|
if (buf == NULL)
|
|
|
|
goto out_fail;
|
2005-06-14 00:56:49 +00:00
|
|
|
|
2007-03-15 20:05:19 +00:00
|
|
|
struct iovec iov = { buf, buf_size };
|
2005-06-14 00:56:49 +00:00
|
|
|
|
2007-03-15 20:05:19 +00:00
|
|
|
if (__netlink_sendreq (h, type) < 0)
|
|
|
|
goto out_fail;
|
2003-03-29 07:26:59 +00:00
|
|
|
|
|
|
|
while (! done)
|
|
|
|
{
|
|
|
|
struct msghdr msg =
|
|
|
|
{
|
2016-03-18 20:34:33 +00:00
|
|
|
.msg_name = (void *) &nladdr,
|
|
|
|
.msg_namelen = sizeof (nladdr),
|
|
|
|
.msg_iov = &iov,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
.msg_control = NULL,
|
|
|
|
.msg_controllen = 0,
|
|
|
|
.msg_flags = 0
|
2003-03-29 07:26:59 +00:00
|
|
|
};
|
|
|
|
|
2003-04-29 23:49:37 +00:00
|
|
|
read_len = TEMP_FAILURE_RETRY (__recvmsg (h->fd, &msg, 0));
|
2015-11-09 11:48:41 +00:00
|
|
|
__netlink_assert_response (h->fd, read_len);
|
2003-03-29 07:26:59 +00:00
|
|
|
if (read_len < 0)
|
2005-06-14 00:56:49 +00:00
|
|
|
goto out_fail;
|
2003-03-29 07:26:59 +00:00
|
|
|
|
2005-06-14 00:56:49 +00:00
|
|
|
if (nladdr.nl_pid != 0)
|
|
|
|
continue;
|
2003-03-29 07:26:59 +00:00
|
|
|
|
2014-02-10 13:45:42 +00:00
|
|
|
if (__glibc_unlikely (msg.msg_flags & MSG_TRUNC))
|
2007-03-15 20:05:19 +00:00
|
|
|
goto out_fail;
|
2005-06-14 00:56:49 +00:00
|
|
|
|
|
|
|
size_t count = 0;
|
|
|
|
size_t remaining_len = read_len;
|
2003-03-29 07:26:59 +00:00
|
|
|
for (nlmh = (struct nlmsghdr *) buf;
|
2005-06-14 00:56:49 +00:00
|
|
|
NLMSG_OK (nlmh, remaining_len);
|
|
|
|
nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, remaining_len))
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
2005-06-14 00:56:49 +00:00
|
|
|
if ((pid_t) nlmh->nlmsg_pid != h->pid
|
2003-11-13 23:39:31 +00:00
|
|
|
|| nlmh->nlmsg_seq != h->seq)
|
2003-03-29 07:26:59 +00:00
|
|
|
continue;
|
|
|
|
|
2005-06-14 00:56:49 +00:00
|
|
|
++count;
|
2003-03-29 07:26:59 +00:00
|
|
|
if (nlmh->nlmsg_type == NLMSG_DONE)
|
|
|
|
{
|
2003-04-16 22:50:37 +00:00
|
|
|
/* We found the end, leave the loop. */
|
2003-03-29 07:26:59 +00:00
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (nlmh->nlmsg_type == NLMSG_ERROR)
|
|
|
|
{
|
|
|
|
struct nlmsgerr *nlerr = (struct nlmsgerr *) NLMSG_DATA (nlmh);
|
|
|
|
if (nlmh->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
|
|
|
|
errno = EIO;
|
|
|
|
else
|
|
|
|
errno = -nlerr->error;
|
2005-06-14 00:56:49 +00:00
|
|
|
goto out_fail;
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-14 00:56:49 +00:00
|
|
|
|
|
|
|
/* If there was nothing with the expected nlmsg_pid and nlmsg_seq,
|
|
|
|
there is no point to record it. */
|
|
|
|
if (count == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nlm_next = (struct netlink_res *) malloc (sizeof (struct netlink_res)
|
|
|
|
+ read_len);
|
|
|
|
if (nlm_next == NULL)
|
|
|
|
goto out_fail;
|
|
|
|
nlm_next->next = NULL;
|
|
|
|
nlm_next->nlh = memcpy (nlm_next + 1, buf, read_len);
|
|
|
|
nlm_next->size = read_len;
|
|
|
|
nlm_next->seq = h->seq;
|
|
|
|
if (h->nlm_list == NULL)
|
|
|
|
h->nlm_list = nlm_next;
|
|
|
|
else
|
|
|
|
h->end_ptr->next = nlm_next;
|
|
|
|
h->end_ptr = nlm_next;
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
2005-06-14 00:56:49 +00:00
|
|
|
|
2023-06-21 20:00:53 +00:00
|
|
|
free(buf);
|
2003-03-29 07:26:59 +00:00
|
|
|
return 0;
|
2005-06-14 00:56:49 +00:00
|
|
|
|
|
|
|
out_fail:
|
2023-06-21 20:00:53 +00:00
|
|
|
free(buf);
|
2005-06-14 00:56:49 +00:00
|
|
|
return -1;
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-30 07:43:11 +00:00
|
|
|
void
|
|
|
|
__netlink_close (struct netlink_handle *h)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
|
|
|
/* Don't modify errno. */
|
2020-01-29 20:38:36 +00:00
|
|
|
INTERNAL_SYSCALL_CALL (close, h->fd);
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Open a NETLINK socket. */
|
2004-06-30 07:43:11 +00:00
|
|
|
int
|
|
|
|
__netlink_open (struct netlink_handle *h)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_nl nladdr;
|
|
|
|
|
2017-04-19 05:45:04 +00:00
|
|
|
h->fd = __socket (PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
|
2003-03-29 07:26:59 +00:00
|
|
|
if (h->fd < 0)
|
2004-06-30 07:43:11 +00:00
|
|
|
goto out;
|
2003-03-29 07:26:59 +00:00
|
|
|
|
|
|
|
memset (&nladdr, '\0', sizeof (nladdr));
|
|
|
|
nladdr.nl_family = AF_NETLINK;
|
2003-04-29 23:49:37 +00:00
|
|
|
if (__bind (h->fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) < 0)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
2003-06-03 23:07:39 +00:00
|
|
|
close_and_out:
|
2004-06-30 07:43:11 +00:00
|
|
|
__netlink_close (h);
|
|
|
|
out:
|
2003-03-29 07:26:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-06-03 23:07:39 +00:00
|
|
|
/* Determine the ID the kernel assigned for this netlink connection.
|
|
|
|
It is not necessarily the PID if there is more than one socket
|
|
|
|
open. */
|
|
|
|
socklen_t addr_len = sizeof (nladdr);
|
|
|
|
if (__getsockname (h->fd, (struct sockaddr *) &nladdr, &addr_len) < 0)
|
|
|
|
goto close_and_out;
|
|
|
|
h->pid = nladdr.nl_pid;
|
2003-03-29 07:26:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* We know the number of RTM_NEWLINK entries, so we reserve the first
|
|
|
|
# of entries for this type. All RTM_NEWADDR entries have an index
|
|
|
|
pointer to the RTM_NEWLINK entry. To find the entry, create
|
|
|
|
a table to map kernel index entries to our index numbers.
|
|
|
|
Since we get at first all RTM_NEWLINK entries, it can never happen
|
|
|
|
that a RTM_NEWADDR index is not known to this map. */
|
|
|
|
static int
|
2003-04-17 01:08:57 +00:00
|
|
|
map_newlink (int index, struct ifaddrs_storage *ifas, int *map, int max)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < max; i++)
|
|
|
|
{
|
|
|
|
if (map[i] == -1)
|
|
|
|
{
|
|
|
|
map[i] = index;
|
2003-04-17 01:08:57 +00:00
|
|
|
if (i > 0)
|
|
|
|
ifas[i - 1].ifa.ifa_next = &ifas[i].ifa;
|
2003-03-29 07:26:59 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
else if (map[i] == index)
|
|
|
|
return i;
|
|
|
|
}
|
2010-04-04 03:36:59 +00:00
|
|
|
|
2013-08-29 07:08:54 +00:00
|
|
|
/* This means interfaces changed between the reading of the
|
2010-04-04 03:36:59 +00:00
|
|
|
RTM_GETLINK and RTM_GETADDR information. We have to repeat
|
|
|
|
everything. */
|
|
|
|
return -1;
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Create a linked list of `struct ifaddrs' structures, one for each
|
|
|
|
network interface on the host machine. If successful, store the
|
2009-04-29 16:42:32 +00:00
|
|
|
list in *IFAP and return 0. On errors, return -1 and set `errno'. */
|
2010-04-04 03:36:59 +00:00
|
|
|
static int
|
|
|
|
getifaddrs_internal (struct ifaddrs **ifap)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
|
|
|
struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
|
|
|
|
struct netlink_res *nlp;
|
|
|
|
struct ifaddrs_storage *ifas;
|
|
|
|
unsigned int i, newlink, newaddr, newaddr_idx;
|
|
|
|
int *map_newlink_data;
|
|
|
|
size_t ifa_data_size = 0; /* Size to allocate for all ifa_data. */
|
2005-06-14 00:56:49 +00:00
|
|
|
char *ifa_data_ptr; /* Pointer to the unused part of memory for
|
2003-03-29 07:26:59 +00:00
|
|
|
ifa_data. */
|
2003-04-16 22:50:37 +00:00
|
|
|
int result = 0;
|
2023-06-21 20:00:53 +00:00
|
|
|
struct scratch_buffer buf;
|
|
|
|
scratch_buffer_init (&buf);
|
2003-03-29 07:26:59 +00:00
|
|
|
|
2006-05-10 00:03:41 +00:00
|
|
|
*ifap = NULL;
|
2003-03-29 07:26:59 +00:00
|
|
|
|
2012-07-25 21:19:13 +00:00
|
|
|
if (__netlink_open (&nh) < 0)
|
|
|
|
return -1;
|
2003-03-29 07:26:59 +00:00
|
|
|
|
|
|
|
/* Tell the kernel that we wish to get a list of all
|
2005-06-14 00:56:49 +00:00
|
|
|
active interfaces, collect all data for every interface. */
|
|
|
|
if (__netlink_request (&nh, RTM_GETLINK) < 0)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
2003-04-16 22:50:37 +00:00
|
|
|
result = -1;
|
|
|
|
goto exit_free;
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now ask the kernel for all addresses which are assigned
|
2005-06-14 00:56:49 +00:00
|
|
|
to an interface and collect all data for every interface.
|
|
|
|
Since we store the addresses after the interfaces in the
|
|
|
|
list, we will later always find the interface before the
|
|
|
|
corresponding addresses. */
|
2003-03-29 07:26:59 +00:00
|
|
|
++nh.seq;
|
2005-06-14 00:56:49 +00:00
|
|
|
if (__netlink_request (&nh, RTM_GETADDR) < 0)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
2003-04-16 22:50:37 +00:00
|
|
|
result = -1;
|
|
|
|
goto exit_free;
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Count all RTM_NEWLINK and RTM_NEWADDR entries to allocate
|
|
|
|
enough memory. */
|
|
|
|
newlink = newaddr = 0;
|
|
|
|
for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
|
|
|
|
{
|
|
|
|
struct nlmsghdr *nlh;
|
|
|
|
size_t size = nlp->size;
|
|
|
|
|
|
|
|
if (nlp->nlh == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Walk through all entries we got from the kernel and look, which
|
2005-06-14 00:56:49 +00:00
|
|
|
message type they contain. */
|
2003-03-29 07:26:59 +00:00
|
|
|
for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
|
|
|
|
{
|
2004-06-30 07:43:11 +00:00
|
|
|
/* Check if the message is what we want. */
|
2003-03-29 07:26:59 +00:00
|
|
|
if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
|
|
|
|
continue;
|
|
|
|
|
2018-06-29 07:44:55 +00:00
|
|
|
/* If the dump got interrupted, we can't rely on the results
|
|
|
|
so try again. */
|
|
|
|
if (nlh->nlmsg_flags & NLM_F_DUMP_INTR)
|
|
|
|
{
|
|
|
|
result = -EAGAIN;
|
|
|
|
goto exit_free;
|
|
|
|
}
|
|
|
|
|
2003-03-29 07:26:59 +00:00
|
|
|
if (nlh->nlmsg_type == NLMSG_DONE)
|
|
|
|
break; /* ok */
|
|
|
|
|
|
|
|
if (nlh->nlmsg_type == RTM_NEWLINK)
|
|
|
|
{
|
|
|
|
/* A RTM_NEWLINK message can have IFLA_STATS data. We need to
|
|
|
|
know the size before creating the list to allocate enough
|
|
|
|
memory. */
|
|
|
|
struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
|
|
|
|
struct rtattr *rta = IFLA_RTA (ifim);
|
|
|
|
size_t rtasize = IFLA_PAYLOAD (nlh);
|
|
|
|
|
|
|
|
while (RTA_OK (rta, rtasize))
|
|
|
|
{
|
|
|
|
size_t rta_payload = RTA_PAYLOAD (rta);
|
|
|
|
|
|
|
|
if (rta->rta_type == IFLA_STATS)
|
|
|
|
{
|
|
|
|
ifa_data_size += rta_payload;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rta = RTA_NEXT (rta, rtasize);
|
|
|
|
}
|
|
|
|
++newlink;
|
|
|
|
}
|
|
|
|
else if (nlh->nlmsg_type == RTM_NEWADDR)
|
|
|
|
++newaddr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return if no interface is up. */
|
|
|
|
if ((newlink + newaddr) == 0)
|
2003-04-16 22:50:37 +00:00
|
|
|
goto exit_free;
|
2003-03-29 07:26:59 +00:00
|
|
|
|
|
|
|
/* Allocate memory for all entries we have and initialize next
|
|
|
|
pointer. */
|
|
|
|
ifas = (struct ifaddrs_storage *) calloc (1,
|
|
|
|
(newlink + newaddr)
|
|
|
|
* sizeof (struct ifaddrs_storage)
|
|
|
|
+ ifa_data_size);
|
|
|
|
if (ifas == NULL)
|
|
|
|
{
|
2003-04-16 22:50:37 +00:00
|
|
|
result = -1;
|
|
|
|
goto exit_free;
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
|
2003-04-17 01:08:57 +00:00
|
|
|
/* Table for mapping kernel index to entry in our list. */
|
2023-06-21 20:00:53 +00:00
|
|
|
if (!scratch_buffer_set_array_size (&buf, newlink, sizeof (int)))
|
|
|
|
{
|
|
|
|
result = -1;
|
|
|
|
goto exit_free;
|
|
|
|
}
|
|
|
|
map_newlink_data = buf.data;
|
2003-04-17 01:08:57 +00:00
|
|
|
memset (map_newlink_data, '\xff', newlink * sizeof (int));
|
|
|
|
|
2003-04-16 22:50:37 +00:00
|
|
|
ifa_data_ptr = (char *) &ifas[newlink + newaddr];
|
2003-03-29 07:26:59 +00:00
|
|
|
newaddr_idx = 0; /* Counter for newaddr index. */
|
|
|
|
|
|
|
|
/* Walk through the list of data we got from the kernel. */
|
|
|
|
for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
|
|
|
|
{
|
|
|
|
struct nlmsghdr *nlh;
|
|
|
|
size_t size = nlp->size;
|
|
|
|
|
|
|
|
if (nlp->nlh == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Walk through one message and look at the type: If it is our
|
|
|
|
message, we need RTM_NEWLINK/RTM_NEWADDR and stop if we reach
|
|
|
|
the end or we find the end marker (in this case we ignore the
|
|
|
|
following data. */
|
|
|
|
for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
|
|
|
|
{
|
|
|
|
int ifa_index = 0;
|
|
|
|
|
2003-04-16 22:50:37 +00:00
|
|
|
/* Check if the message is the one we want */
|
2003-03-29 07:26:59 +00:00
|
|
|
if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (nlh->nlmsg_type == NLMSG_DONE)
|
|
|
|
break; /* ok */
|
2003-04-16 22:50:37 +00:00
|
|
|
|
|
|
|
if (nlh->nlmsg_type == RTM_NEWLINK)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
|
|
|
/* We found a new interface. Now extract everything from the
|
|
|
|
interface data we got and need. */
|
|
|
|
struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
|
|
|
|
struct rtattr *rta = IFLA_RTA (ifim);
|
|
|
|
size_t rtasize = IFLA_PAYLOAD (nlh);
|
|
|
|
|
2003-04-16 22:50:37 +00:00
|
|
|
/* Interfaces are stored in the first "newlink" entries
|
2003-03-29 07:26:59 +00:00
|
|
|
of our list, starting in the order as we got from the
|
|
|
|
kernel. */
|
2005-06-14 00:56:49 +00:00
|
|
|
ifa_index = map_newlink (ifim->ifi_index - 1, ifas,
|
2003-03-29 07:26:59 +00:00
|
|
|
map_newlink_data, newlink);
|
2014-02-10 13:45:42 +00:00
|
|
|
if (__glibc_unlikely (ifa_index == -1))
|
2010-04-04 03:36:59 +00:00
|
|
|
{
|
|
|
|
try_again:
|
|
|
|
result = -EAGAIN;
|
|
|
|
free (ifas);
|
|
|
|
goto exit_free;
|
|
|
|
}
|
2003-03-29 07:26:59 +00:00
|
|
|
ifas[ifa_index].ifa.ifa_flags = ifim->ifi_flags;
|
|
|
|
|
|
|
|
while (RTA_OK (rta, rtasize))
|
|
|
|
{
|
|
|
|
char *rta_data = RTA_DATA (rta);
|
|
|
|
size_t rta_payload = RTA_PAYLOAD (rta);
|
|
|
|
|
|
|
|
switch (rta->rta_type)
|
|
|
|
{
|
|
|
|
case IFLA_ADDRESS:
|
2003-04-17 01:08:57 +00:00
|
|
|
if (rta_payload <= sizeof (ifas[ifa_index].addr))
|
|
|
|
{
|
|
|
|
ifas[ifa_index].addr.sl.sll_family = AF_PACKET;
|
|
|
|
memcpy (ifas[ifa_index].addr.sl.sll_addr,
|
|
|
|
(char *) rta_data, rta_payload);
|
|
|
|
ifas[ifa_index].addr.sl.sll_halen = rta_payload;
|
|
|
|
ifas[ifa_index].addr.sl.sll_ifindex
|
|
|
|
= ifim->ifi_index;
|
|
|
|
ifas[ifa_index].addr.sl.sll_hatype = ifim->ifi_type;
|
|
|
|
|
|
|
|
ifas[ifa_index].ifa.ifa_addr
|
|
|
|
= &ifas[ifa_index].addr.sa;
|
|
|
|
}
|
2003-03-29 07:26:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IFLA_BROADCAST:
|
2003-04-17 01:08:57 +00:00
|
|
|
if (rta_payload <= sizeof (ifas[ifa_index].broadaddr))
|
|
|
|
{
|
|
|
|
ifas[ifa_index].broadaddr.sl.sll_family = AF_PACKET;
|
|
|
|
memcpy (ifas[ifa_index].broadaddr.sl.sll_addr,
|
|
|
|
(char *) rta_data, rta_payload);
|
|
|
|
ifas[ifa_index].broadaddr.sl.sll_halen = rta_payload;
|
|
|
|
ifas[ifa_index].broadaddr.sl.sll_ifindex
|
|
|
|
= ifim->ifi_index;
|
|
|
|
ifas[ifa_index].broadaddr.sl.sll_hatype
|
|
|
|
= ifim->ifi_type;
|
2003-03-29 07:26:59 +00:00
|
|
|
|
2003-04-17 01:08:57 +00:00
|
|
|
ifas[ifa_index].ifa.ifa_broadaddr
|
|
|
|
= &ifas[ifa_index].broadaddr.sa;
|
|
|
|
}
|
2003-03-29 07:26:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IFLA_IFNAME: /* Name of Interface */
|
|
|
|
if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name))
|
|
|
|
{
|
|
|
|
ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
|
2003-04-17 01:08:57 +00:00
|
|
|
*(char *) __mempcpy (ifas[ifa_index].name, rta_data,
|
|
|
|
rta_payload) = '\0';
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IFLA_STATS: /* Statistics of Interface */
|
|
|
|
ifas[ifa_index].ifa.ifa_data = ifa_data_ptr;
|
|
|
|
ifa_data_ptr += rta_payload;
|
|
|
|
memcpy (ifas[ifa_index].ifa.ifa_data, rta_data,
|
|
|
|
rta_payload);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IFLA_UNSPEC:
|
|
|
|
break;
|
|
|
|
case IFLA_MTU:
|
|
|
|
break;
|
|
|
|
case IFLA_LINK:
|
|
|
|
break;
|
|
|
|
case IFLA_QDISC:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rta = RTA_NEXT (rta, rtasize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nlh->nlmsg_type == RTM_NEWADDR)
|
|
|
|
{
|
|
|
|
struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlh);
|
|
|
|
struct rtattr *rta = IFA_RTA (ifam);
|
|
|
|
size_t rtasize = IFA_PAYLOAD (nlh);
|
|
|
|
|
|
|
|
/* New Addresses are stored in the order we got them from
|
2003-04-17 01:08:57 +00:00
|
|
|
the kernel after the interfaces. Theoretically it is possible
|
2003-03-29 07:26:59 +00:00
|
|
|
that we have holes in the interface part of the list,
|
|
|
|
but we always have already the interface for this address. */
|
|
|
|
ifa_index = newlink + newaddr_idx;
|
2010-04-04 03:36:59 +00:00
|
|
|
int idx = map_newlink (ifam->ifa_index - 1, ifas,
|
|
|
|
map_newlink_data, newlink);
|
2014-02-10 13:45:42 +00:00
|
|
|
if (__glibc_unlikely (idx == -1))
|
2010-04-04 03:36:59 +00:00
|
|
|
goto try_again;
|
|
|
|
ifas[ifa_index].ifa.ifa_flags = ifas[idx].ifa.ifa_flags;
|
2003-04-17 01:08:57 +00:00
|
|
|
if (ifa_index > 0)
|
|
|
|
ifas[ifa_index - 1].ifa.ifa_next = &ifas[ifa_index].ifa;
|
2003-03-29 07:26:59 +00:00
|
|
|
++newaddr_idx;
|
|
|
|
|
|
|
|
while (RTA_OK (rta, rtasize))
|
|
|
|
{
|
|
|
|
char *rta_data = RTA_DATA (rta);
|
|
|
|
size_t rta_payload = RTA_PAYLOAD (rta);
|
|
|
|
|
|
|
|
switch (rta->rta_type)
|
|
|
|
{
|
|
|
|
case IFA_ADDRESS:
|
|
|
|
{
|
|
|
|
struct sockaddr *sa;
|
|
|
|
|
|
|
|
if (ifas[ifa_index].ifa.ifa_addr != NULL)
|
|
|
|
{
|
|
|
|
/* In a point-to-poing network IFA_ADDRESS
|
|
|
|
contains the destination address, local
|
|
|
|
address is supplied in IFA_LOCAL attribute.
|
|
|
|
destination address and broadcast address
|
|
|
|
are stored in an union, so it doesn't matter
|
|
|
|
which name we use. */
|
|
|
|
ifas[ifa_index].ifa.ifa_broadaddr
|
|
|
|
= &ifas[ifa_index].broadaddr.sa;
|
|
|
|
sa = &ifas[ifa_index].broadaddr.sa;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ifas[ifa_index].ifa.ifa_addr
|
|
|
|
= &ifas[ifa_index].addr.sa;
|
|
|
|
sa = &ifas[ifa_index].addr.sa;
|
|
|
|
}
|
|
|
|
|
|
|
|
sa->sa_family = ifam->ifa_family;
|
|
|
|
|
|
|
|
switch (ifam->ifa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
2003-04-17 01:08:57 +00:00
|
|
|
/* Size must match that of an address for IPv4. */
|
|
|
|
if (rta_payload == 4)
|
|
|
|
memcpy (&((struct sockaddr_in *) sa)->sin_addr,
|
|
|
|
rta_data, rta_payload);
|
2003-03-29 07:26:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
2003-04-17 01:08:57 +00:00
|
|
|
/* Size must match that of an address for IPv6. */
|
|
|
|
if (rta_payload == 16)
|
|
|
|
{
|
|
|
|
memcpy (&((struct sockaddr_in6 *) sa)->sin6_addr,
|
|
|
|
rta_data, rta_payload);
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL (rta_data)
|
|
|
|
|| IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
|
|
|
|
((struct sockaddr_in6 *) sa)->sin6_scope_id
|
2004-11-10 00:37:05 +00:00
|
|
|
= ifam->ifa_index;
|
2003-04-17 01:08:57 +00:00
|
|
|
}
|
2003-03-29 07:26:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-04-17 01:08:57 +00:00
|
|
|
if (rta_payload <= sizeof (ifas[ifa_index].addr))
|
|
|
|
memcpy (sa->sa_data, rta_data, rta_payload);
|
2003-03-29 07:26:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IFA_LOCAL:
|
|
|
|
if (ifas[ifa_index].ifa.ifa_addr != NULL)
|
|
|
|
{
|
|
|
|
/* If ifa_addr is set and we get IFA_LOCAL,
|
|
|
|
assume we have a point-to-point network.
|
|
|
|
Move address to correct field. */
|
|
|
|
ifas[ifa_index].broadaddr = ifas[ifa_index].addr;
|
|
|
|
ifas[ifa_index].ifa.ifa_broadaddr
|
|
|
|
= &ifas[ifa_index].broadaddr.sa;
|
|
|
|
memset (&ifas[ifa_index].addr, '\0',
|
|
|
|
sizeof (ifas[ifa_index].addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
ifas[ifa_index].ifa.ifa_addr = &ifas[ifa_index].addr.sa;
|
|
|
|
ifas[ifa_index].ifa.ifa_addr->sa_family
|
|
|
|
= ifam->ifa_family;
|
|
|
|
|
|
|
|
switch (ifam->ifa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
2003-04-17 01:08:57 +00:00
|
|
|
/* Size must match that of an address for IPv4. */
|
|
|
|
if (rta_payload == 4)
|
|
|
|
memcpy (&ifas[ifa_index].addr.s4.sin_addr,
|
2003-03-29 07:26:59 +00:00
|
|
|
rta_data, rta_payload);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
2003-04-17 01:08:57 +00:00
|
|
|
/* Size must match that of an address for IPv6. */
|
|
|
|
if (rta_payload == 16)
|
|
|
|
{
|
|
|
|
memcpy (&ifas[ifa_index].addr.s6.sin6_addr,
|
|
|
|
rta_data, rta_payload);
|
2004-11-10 00:50:40 +00:00
|
|
|
if (IN6_IS_ADDR_LINKLOCAL (rta_data)
|
|
|
|
|| IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
|
2003-04-17 01:08:57 +00:00
|
|
|
ifas[ifa_index].addr.s6.sin6_scope_id =
|
2004-11-10 00:37:05 +00:00
|
|
|
ifam->ifa_index;
|
2003-04-17 01:08:57 +00:00
|
|
|
}
|
2003-03-29 07:26:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-04-17 01:08:57 +00:00
|
|
|
if (rta_payload <= sizeof (ifas[ifa_index].addr))
|
|
|
|
memcpy (ifas[ifa_index].addr.sa.sa_data,
|
|
|
|
rta_data, rta_payload);
|
2003-03-29 07:26:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IFA_BROADCAST:
|
|
|
|
/* We get IFA_BROADCAST, so IFA_LOCAL was too much. */
|
|
|
|
if (ifas[ifa_index].ifa.ifa_broadaddr != NULL)
|
|
|
|
memset (&ifas[ifa_index].broadaddr, '\0',
|
|
|
|
sizeof (ifas[ifa_index].broadaddr));
|
|
|
|
|
|
|
|
ifas[ifa_index].ifa.ifa_broadaddr
|
|
|
|
= &ifas[ifa_index].broadaddr.sa;
|
|
|
|
ifas[ifa_index].ifa.ifa_broadaddr->sa_family
|
|
|
|
= ifam->ifa_family;
|
|
|
|
|
|
|
|
switch (ifam->ifa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
2003-04-17 01:08:57 +00:00
|
|
|
/* Size must match that of an address for IPv4. */
|
|
|
|
if (rta_payload == 4)
|
|
|
|
memcpy (&ifas[ifa_index].broadaddr.s4.sin_addr,
|
|
|
|
rta_data, rta_payload);
|
2003-03-29 07:26:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
2003-04-17 01:08:57 +00:00
|
|
|
/* Size must match that of an address for IPv6. */
|
|
|
|
if (rta_payload == 16)
|
|
|
|
{
|
|
|
|
memcpy (&ifas[ifa_index].broadaddr.s6.sin6_addr,
|
|
|
|
rta_data, rta_payload);
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL (rta_data)
|
|
|
|
|| IN6_IS_ADDR_MC_LINKLOCAL (rta_data))
|
|
|
|
ifas[ifa_index].broadaddr.s6.sin6_scope_id
|
2004-11-10 00:37:05 +00:00
|
|
|
= ifam->ifa_index;
|
2003-04-17 01:08:57 +00:00
|
|
|
}
|
2003-03-29 07:26:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-04-17 01:08:57 +00:00
|
|
|
if (rta_payload <= sizeof (ifas[ifa_index].addr))
|
|
|
|
memcpy (&ifas[ifa_index].broadaddr.sa.sa_data,
|
|
|
|
rta_data, rta_payload);
|
2003-03-29 07:26:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IFA_LABEL:
|
|
|
|
if (rta_payload + 1 <= sizeof (ifas[ifa_index].name))
|
|
|
|
{
|
|
|
|
ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name;
|
2003-04-17 01:08:57 +00:00
|
|
|
*(char *) __mempcpy (ifas[ifa_index].name, rta_data,
|
|
|
|
rta_payload) = '\0';
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
abort ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IFA_UNSPEC:
|
|
|
|
break;
|
|
|
|
case IFA_CACHEINFO:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rta = RTA_NEXT (rta, rtasize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we didn't get the interface name with the
|
|
|
|
address, use the name from the interface entry. */
|
|
|
|
if (ifas[ifa_index].ifa.ifa_name == NULL)
|
2010-04-04 03:36:59 +00:00
|
|
|
{
|
|
|
|
int idx = map_newlink (ifam->ifa_index - 1, ifas,
|
|
|
|
map_newlink_data, newlink);
|
2014-02-10 13:45:42 +00:00
|
|
|
if (__glibc_unlikely (idx == -1))
|
2010-04-04 03:36:59 +00:00
|
|
|
goto try_again;
|
|
|
|
ifas[ifa_index].ifa.ifa_name = ifas[idx].ifa.ifa_name;
|
|
|
|
}
|
2003-03-29 07:26:59 +00:00
|
|
|
|
|
|
|
/* Calculate the netmask. */
|
|
|
|
if (ifas[ifa_index].ifa.ifa_addr
|
|
|
|
&& ifas[ifa_index].ifa.ifa_addr->sa_family != AF_UNSPEC
|
|
|
|
&& ifas[ifa_index].ifa.ifa_addr->sa_family != AF_PACKET)
|
|
|
|
{
|
|
|
|
uint32_t max_prefixlen = 0;
|
|
|
|
char *cp = NULL;
|
|
|
|
|
|
|
|
ifas[ifa_index].ifa.ifa_netmask
|
|
|
|
= &ifas[ifa_index].netmask.sa;
|
|
|
|
|
|
|
|
switch (ifas[ifa_index].ifa.ifa_addr->sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
cp = (char *) &ifas[ifa_index].netmask.s4.sin_addr;
|
|
|
|
max_prefixlen = 32;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
cp = (char *) &ifas[ifa_index].netmask.s6.sin6_addr;
|
|
|
|
max_prefixlen = 128;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifas[ifa_index].ifa.ifa_netmask->sa_family
|
|
|
|
= ifas[ifa_index].ifa.ifa_addr->sa_family;
|
|
|
|
|
|
|
|
if (cp != NULL)
|
|
|
|
{
|
|
|
|
unsigned int preflen;
|
|
|
|
|
2014-09-13 08:10:29 +00:00
|
|
|
if (ifam->ifa_prefixlen > max_prefixlen)
|
2003-03-29 07:26:59 +00:00
|
|
|
preflen = max_prefixlen;
|
|
|
|
else
|
|
|
|
preflen = ifam->ifa_prefixlen;
|
|
|
|
|
2014-09-13 08:10:29 +00:00
|
|
|
for (i = 0; i < preflen / 8; i++)
|
2003-03-29 07:26:59 +00:00
|
|
|
*cp++ = 0xff;
|
2014-09-13 08:10:29 +00:00
|
|
|
if (preflen % 8)
|
|
|
|
*cp = 0xff << (8 - preflen % 8);
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-17 01:08:57 +00:00
|
|
|
assert (ifa_data_ptr <= (char *) &ifas[newlink + newaddr] + ifa_data_size);
|
|
|
|
|
|
|
|
if (newaddr_idx > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < newlink; ++i)
|
|
|
|
if (map_newlink_data[i] == -1)
|
|
|
|
{
|
|
|
|
/* We have fewer links then we anticipated. Adjust the
|
|
|
|
forward pointer to the first address entry. */
|
|
|
|
ifas[i - 1].ifa.ifa_next = &ifas[newlink].ifa;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 0 && newlink > 0)
|
|
|
|
/* No valid link, but we allocated memory. We have to
|
|
|
|
populate the first entry. */
|
|
|
|
memmove (ifas, &ifas[newlink], sizeof (struct ifaddrs_storage));
|
|
|
|
}
|
|
|
|
|
2006-05-10 00:03:41 +00:00
|
|
|
*ifap = &ifas[0].ifa;
|
2003-04-16 22:50:37 +00:00
|
|
|
|
|
|
|
exit_free:
|
2004-06-30 07:43:11 +00:00
|
|
|
__netlink_free_handle (&nh);
|
|
|
|
__netlink_close (&nh);
|
2023-06-21 20:00:53 +00:00
|
|
|
scratch_buffer_free (&buf);
|
2003-03-29 07:26:59 +00:00
|
|
|
|
2003-04-16 22:50:37 +00:00
|
|
|
return result;
|
2003-03-29 07:26:59 +00:00
|
|
|
}
|
2010-04-04 03:36:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Create a linked list of `struct ifaddrs' structures, one for each
|
|
|
|
network interface on the host machine. If successful, store the
|
|
|
|
list in *IFAP and return 0. On errors, return -1 and set `errno'. */
|
|
|
|
int
|
2014-12-02 23:11:09 +00:00
|
|
|
__getifaddrs (struct ifaddrs **ifap)
|
2010-04-04 03:36:59 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
do
|
|
|
|
res = getifaddrs_internal (ifap);
|
|
|
|
while (res == -EAGAIN);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2014-12-02 23:11:09 +00:00
|
|
|
weak_alias (__getifaddrs, getifaddrs)
|
2017-09-28 00:18:32 +00:00
|
|
|
libc_hidden_def (__getifaddrs)
|
2014-12-02 23:11:09 +00:00
|
|
|
libc_hidden_weak (getifaddrs)
|
2003-03-29 07:26:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2014-12-02 23:11:09 +00:00
|
|
|
__freeifaddrs (struct ifaddrs *ifa)
|
2003-03-29 07:26:59 +00:00
|
|
|
{
|
|
|
|
free (ifa);
|
|
|
|
}
|
2014-12-02 23:11:09 +00:00
|
|
|
weak_alias (__freeifaddrs, freeifaddrs)
|
2017-09-28 00:18:32 +00:00
|
|
|
libc_hidden_def (__freeifaddrs)
|
2014-12-02 23:11:09 +00:00
|
|
|
libc_hidden_weak (freeifaddrs)
|