2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2003 Bruce M. Simpson.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include "namespace.h"
30 #include <sys/param.h>
31 #include <sys/sysctl.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
35 #include <net/if_dl.h>
36 #include <net/route.h>
42 #include "un-namespace.h"
44 #define SALIGN (sizeof(long) - 1)
45 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : \
47 #define MAX_SYSCTL_TRY 5
48 #define RTA_MASKS (RTA_GATEWAY | RTA_IFP | RTA_IFA)
51 getifmaddrs(struct ifmaddrs **pif)
64 struct ifma_msghdr *ifmam;
65 struct ifmaddrs *ifa, *ift;
66 struct rt_msghdr *rtm;
71 mib[2] = 0; /* protocol */
72 mib[3] = 0; /* wildcard address family */
73 mib[4] = NET_RT_IFMALIST;
74 mib[5] = 0; /* no flags */
76 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
78 if ((buf = malloc(needed)) == NULL)
80 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
81 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
88 } while (buf == NULL);
90 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
91 rtm = (struct rt_msghdr *)(void *)next;
92 if (rtm->rtm_version != RTM_VERSION)
94 switch (rtm->rtm_type) {
96 ifmam = (struct ifma_msghdr *)(void *)rtm;
97 if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
100 p = (char *)(ifmam + 1);
101 for (i = 0; i < RTAX_MAX; i++) {
102 if ((RTA_MASKS & ifmam->ifmam_addrs &
105 sa = (struct sockaddr *)(void *)p;
114 data = malloc(sizeof(struct ifmaddrs) * icnt + dcnt);
120 ifa = (struct ifmaddrs *)(void *)data;
121 data += sizeof(struct ifmaddrs) * icnt;
123 memset(ifa, 0, sizeof(struct ifmaddrs) * icnt);
126 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
127 rtm = (struct rt_msghdr *)(void *)next;
128 if (rtm->rtm_version != RTM_VERSION)
131 switch (rtm->rtm_type) {
133 ifmam = (struct ifma_msghdr *)(void *)rtm;
134 if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
137 p = (char *)(ifmam + 1);
138 for (i = 0; i < RTAX_MAX; i++) {
139 if ((RTA_MASKS & ifmam->ifmam_addrs &
142 sa = (struct sockaddr *)(void *)p;
147 (struct sockaddr *)(void *)data;
148 memcpy(data, p, len);
154 (struct sockaddr *)(void *)data;
155 memcpy(data, p, len);
161 (struct sockaddr *)(void *)data;
162 memcpy(data, p, len);
172 ift->ifma_next = ift + 1;
173 ift = ift->ifma_next;
182 ift->ifma_next = NULL;
192 freeifmaddrs(struct ifmaddrs *ifmp)