2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "namespace.h"
33 #include <sys/param.h>
34 #include <sys/sysctl.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
38 #include <net/if_dl.h>
39 #include <net/route.h>
45 #include "un-namespace.h"
47 #define SALIGN (sizeof(long) - 1)
48 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : \
50 #define MAX_SYSCTL_TRY 5
51 #define RTA_MASKS (RTA_GATEWAY | RTA_IFP | RTA_IFA)
54 getifmaddrs(struct ifmaddrs **pif)
67 struct ifma_msghdr *ifmam;
68 struct ifmaddrs *ifa, *ift;
69 struct rt_msghdr *rtm;
74 mib[2] = 0; /* protocol */
75 mib[3] = 0; /* wildcard address family */
76 mib[4] = NET_RT_IFMALIST;
77 mib[5] = 0; /* no flags */
79 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
81 if ((buf = malloc(needed)) == NULL)
83 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
84 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
91 } while (buf == NULL);
93 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
94 rtm = (struct rt_msghdr *)(void *)next;
95 if (rtm->rtm_version != RTM_VERSION)
97 switch (rtm->rtm_type) {
99 ifmam = (struct ifma_msghdr *)(void *)rtm;
100 if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
103 p = (char *)(ifmam + 1);
104 for (i = 0; i < RTAX_MAX; i++) {
105 if ((RTA_MASKS & ifmam->ifmam_addrs &
108 sa = (struct sockaddr *)(void *)p;
117 data = malloc(sizeof(struct ifmaddrs) * icnt + dcnt);
123 ifa = (struct ifmaddrs *)(void *)data;
124 data += sizeof(struct ifmaddrs) * icnt;
126 memset(ifa, 0, sizeof(struct ifmaddrs) * icnt);
129 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
130 rtm = (struct rt_msghdr *)(void *)next;
131 if (rtm->rtm_version != RTM_VERSION)
134 switch (rtm->rtm_type) {
136 ifmam = (struct ifma_msghdr *)(void *)rtm;
137 if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
140 p = (char *)(ifmam + 1);
141 for (i = 0; i < RTAX_MAX; i++) {
142 if ((RTA_MASKS & ifmam->ifmam_addrs &
145 sa = (struct sockaddr *)(void *)p;
150 (struct sockaddr *)(void *)data;
151 memcpy(data, p, len);
157 (struct sockaddr *)(void *)data;
158 memcpy(data, p, len);
164 (struct sockaddr *)(void *)data;
165 memcpy(data, p, len);
175 ift->ifma_next = ift + 1;
176 ift = ift->ifma_next;
185 ift->ifma_next = NULL;
195 freeifmaddrs(struct ifmaddrs *ifmp)