af_inet.c revision 1.19 1 1.19 riastrad /* $NetBSD: af_inet.c,v 1.19 2016/02/29 16:23:25 riastradh Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1983, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. Neither the name of the University nor the names of its contributors
16 1.1 thorpej * may be used to endorse or promote products derived from this software
17 1.1 thorpej * without specific prior written permission.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 thorpej * SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include <sys/cdefs.h>
33 1.1 thorpej #ifndef lint
34 1.19 riastrad __RCSID("$NetBSD: af_inet.c,v 1.19 2016/02/29 16:23:25 riastradh Exp $");
35 1.1 thorpej #endif /* not lint */
36 1.1 thorpej
37 1.1 thorpej #include <sys/param.h>
38 1.1 thorpej #include <sys/ioctl.h>
39 1.1 thorpej #include <sys/socket.h>
40 1.1 thorpej
41 1.1 thorpej #include <net/if.h>
42 1.1 thorpej #include <netinet/in.h>
43 1.1 thorpej #include <netinet/in_var.h>
44 1.1 thorpej
45 1.1 thorpej #include <arpa/inet.h>
46 1.1 thorpej
47 1.6 dyoung #include <assert.h>
48 1.1 thorpej #include <err.h>
49 1.1 thorpej #include <errno.h>
50 1.1 thorpej #include <ifaddrs.h>
51 1.1 thorpej #include <netdb.h>
52 1.1 thorpej #include <string.h>
53 1.1 thorpej #include <stdlib.h>
54 1.1 thorpej #include <stdio.h>
55 1.4 christos #include <util.h>
56 1.1 thorpej
57 1.6 dyoung #include "env.h"
58 1.1 thorpej #include "extern.h"
59 1.8 dyoung #include "af_inetany.h"
60 1.15 pooka #include "prog_ops.h"
61 1.1 thorpej
62 1.12 dyoung static void in_constructor(void) __attribute__((constructor));
63 1.12 dyoung static void in_status(prop_dictionary_t, prop_dictionary_t, bool);
64 1.12 dyoung static void in_commit_address(prop_dictionary_t, prop_dictionary_t);
65 1.18 roy static bool in_addr_flags(struct ifaddrs *, int);
66 1.18 roy static bool in_addr_tentative(struct ifaddrs *);
67 1.18 roy static bool in_addr_tentative_or_detached(struct ifaddrs *);
68 1.10 dyoung static void in_alias(const char *, prop_dictionary_t, prop_dictionary_t,
69 1.10 dyoung struct in_aliasreq *);
70 1.5 dyoung
71 1.12 dyoung static struct afswtch af = {
72 1.12 dyoung .af_name = "inet", .af_af = AF_INET, .af_status = in_status,
73 1.16 roy .af_addr_commit = in_commit_address,
74 1.18 roy .af_addr_tentative = in_addr_tentative,
75 1.18 roy .af_addr_tentative_or_detached = in_addr_tentative_or_detached
76 1.12 dyoung };
77 1.12 dyoung
78 1.11 dyoung static void
79 1.6 dyoung in_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
80 1.10 dyoung struct in_aliasreq *creq)
81 1.1 thorpej {
82 1.6 dyoung struct ifreq ifr;
83 1.12 dyoung bool alias;
84 1.12 dyoung int s;
85 1.6 dyoung unsigned short flags;
86 1.6 dyoung struct in_aliasreq in_addreq;
87 1.13 dyoung const struct sockaddr_in * const asin = &in_addreq.ifra_addr;
88 1.13 dyoung const struct sockaddr_in * const dsin = &in_addreq.ifra_dstaddr;
89 1.13 dyoung const struct sockaddr_in * const bsin = &in_addreq.ifra_broadaddr;
90 1.13 dyoung char hbuf[NI_MAXHOST];
91 1.13 dyoung const int niflag = Nflag ? 0 : NI_NUMERICHOST;
92 1.1 thorpej
93 1.1 thorpej if (lflag)
94 1.1 thorpej return;
95 1.1 thorpej
96 1.12 dyoung alias = true;
97 1.1 thorpej
98 1.1 thorpej /* Get the non-alias address for this interface. */
99 1.6 dyoung if ((s = getsock(AF_INET)) == -1) {
100 1.2 tron if (errno == EAFNOSUPPORT)
101 1.1 thorpej return;
102 1.1 thorpej err(EXIT_FAILURE, "socket");
103 1.1 thorpej }
104 1.6 dyoung memset(&ifr, 0, sizeof(ifr));
105 1.6 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
106 1.15 pooka if (prog_ioctl(s, SIOCGIFADDR, &ifr) == -1) {
107 1.10 dyoung if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
108 1.1 thorpej return;
109 1.10 dyoung warn("SIOCGIFADDR");
110 1.1 thorpej }
111 1.1 thorpej /* If creq and ifr are the same address, this is not an alias. */
112 1.10 dyoung if (memcmp(&ifr.ifr_addr, &creq->ifra_addr, sizeof(ifr.ifr_addr)) == 0)
113 1.12 dyoung alias = false;
114 1.10 dyoung in_addreq = *creq;
115 1.15 pooka if (prog_ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
116 1.1 thorpej if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
117 1.1 thorpej return;
118 1.1 thorpej } else
119 1.1 thorpej warn("SIOCGIFALIAS");
120 1.1 thorpej }
121 1.1 thorpej
122 1.13 dyoung if (getnameinfo((const struct sockaddr *)asin, asin->sin_len,
123 1.13 dyoung hbuf, sizeof(hbuf), NULL, 0, niflag))
124 1.13 dyoung strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
125 1.13 dyoung printf("\tinet %s%s", alias ? "alias " : "", hbuf);
126 1.6 dyoung
127 1.6 dyoung if (getifflags(env, oenv, &flags) == -1)
128 1.6 dyoung err(EXIT_FAILURE, "%s: getifflags", __func__);
129 1.1 thorpej
130 1.13 dyoung if (flags & IFF_POINTOPOINT) {
131 1.13 dyoung if (getnameinfo((const struct sockaddr *)dsin, dsin->sin_len,
132 1.13 dyoung hbuf, sizeof(hbuf), NULL, 0, niflag))
133 1.13 dyoung strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
134 1.13 dyoung printf(" -> %s", hbuf);
135 1.13 dyoung }
136 1.1 thorpej
137 1.10 dyoung printf(" netmask 0x%x", ntohl(in_addreq.ifra_mask.sin_addr.s_addr));
138 1.1 thorpej
139 1.1 thorpej if (flags & IFF_BROADCAST) {
140 1.13 dyoung if (getnameinfo((const struct sockaddr *)bsin, bsin->sin_len,
141 1.13 dyoung hbuf, sizeof(hbuf), NULL, 0, niflag))
142 1.13 dyoung strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
143 1.13 dyoung printf(" broadcast %s", hbuf);
144 1.1 thorpej }
145 1.16 roy
146 1.16 roy #ifdef IN_IFF_TENTATIVE
147 1.16 roy memcpy(&ifr.ifr_addr, &creq->ifra_addr, creq->ifra_addr.sin_len);
148 1.16 roy if (prog_ioctl(s, SIOCGIFAFLAG_IN, &ifr) == -1) {
149 1.16 roy if (errno != EADDRNOTAVAIL)
150 1.16 roy warn("SIOCGIFAFLAG_IN");
151 1.16 roy } else {
152 1.16 roy if (ifr.ifr_addrflags & IN_IFF_TENTATIVE)
153 1.16 roy printf(" tentative");
154 1.16 roy if (ifr.ifr_addrflags & IN_IFF_DUPLICATED)
155 1.16 roy printf(" duplicated");
156 1.16 roy if (ifr.ifr_addrflags & IN_IFF_DETACHED)
157 1.16 roy printf(" detached");
158 1.16 roy }
159 1.16 roy #endif
160 1.5 dyoung }
161 1.5 dyoung
162 1.12 dyoung static void
163 1.7 dyoung in_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
164 1.1 thorpej {
165 1.1 thorpej struct ifaddrs *ifap, *ifa;
166 1.10 dyoung struct in_aliasreq ifra;
167 1.14 dyoung bool printprefs = false;
168 1.6 dyoung const char *ifname;
169 1.6 dyoung
170 1.6 dyoung if ((ifname = getifname(env)) == NULL)
171 1.6 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
172 1.1 thorpej
173 1.1 thorpej if (getifaddrs(&ifap) != 0)
174 1.1 thorpej err(EXIT_FAILURE, "getifaddrs");
175 1.10 dyoung
176 1.14 dyoung printprefs = ifa_any_preferences(ifname, ifap, AF_INET);
177 1.14 dyoung
178 1.10 dyoung for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
179 1.6 dyoung if (strcmp(ifname, ifa->ifa_name) != 0)
180 1.1 thorpej continue;
181 1.1 thorpej if (ifa->ifa_addr->sa_family != AF_INET)
182 1.1 thorpej continue;
183 1.10 dyoung if (sizeof(ifra.ifra_addr) < ifa->ifa_addr->sa_len)
184 1.1 thorpej continue;
185 1.1 thorpej
186 1.10 dyoung memset(&ifra, 0, sizeof(ifra));
187 1.10 dyoung estrlcpy(ifra.ifra_name, ifa->ifa_name, sizeof(ifra.ifra_name));
188 1.10 dyoung memcpy(&ifra.ifra_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
189 1.10 dyoung in_alias(ifa->ifa_name, env, oenv, &ifra);
190 1.5 dyoung if (printprefs)
191 1.14 dyoung ifa_print_preference(ifa->ifa_name, ifa->ifa_addr);
192 1.5 dyoung printf("\n");
193 1.5 dyoung }
194 1.1 thorpej freeifaddrs(ifap);
195 1.1 thorpej }
196 1.8 dyoung
197 1.12 dyoung static void
198 1.8 dyoung in_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
199 1.8 dyoung {
200 1.8 dyoung struct ifreq in_ifr;
201 1.8 dyoung struct in_aliasreq in_ifra;
202 1.8 dyoung struct afparam inparam = {
203 1.8 dyoung .req = BUFPARAM(in_ifra)
204 1.8 dyoung , .dgreq = BUFPARAM(in_ifr)
205 1.8 dyoung , .name = {
206 1.8 dyoung {.buf = in_ifr.ifr_name,
207 1.8 dyoung .buflen = sizeof(in_ifr.ifr_name)}
208 1.8 dyoung , {.buf = in_ifra.ifra_name,
209 1.8 dyoung .buflen = sizeof(in_ifra.ifra_name)}
210 1.8 dyoung }
211 1.8 dyoung , .dgaddr = BUFPARAM(in_ifr.ifr_addr)
212 1.8 dyoung , .addr = BUFPARAM(in_ifra.ifra_addr)
213 1.8 dyoung , .dst = BUFPARAM(in_ifra.ifra_dstaddr)
214 1.8 dyoung , .brd = BUFPARAM(in_ifra.ifra_broadaddr)
215 1.8 dyoung , .mask = BUFPARAM(in_ifra.ifra_mask)
216 1.8 dyoung , .aifaddr = IFADDR_PARAM(SIOCAIFADDR)
217 1.8 dyoung , .difaddr = IFADDR_PARAM(SIOCDIFADDR)
218 1.8 dyoung , .gifaddr = IFADDR_PARAM(SIOCGIFADDR)
219 1.8 dyoung , .defmask = {.buf = NULL, .buflen = 0}
220 1.8 dyoung };
221 1.9 dyoung memset(&in_ifr, 0, sizeof(in_ifr));
222 1.9 dyoung memset(&in_ifra, 0, sizeof(in_ifra));
223 1.8 dyoung commit_address(env, oenv, &inparam);
224 1.8 dyoung }
225 1.12 dyoung
226 1.18 roy #ifdef SIOCGIFAFLAG_IN
227 1.16 roy static bool
228 1.18 roy in_addr_flags(struct ifaddrs *ifa, int flags)
229 1.16 roy {
230 1.16 roy int s;
231 1.16 roy struct ifreq ifr;
232 1.16 roy
233 1.16 roy memset(&ifr, 0, sizeof(ifr));
234 1.19 riastrad estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
235 1.16 roy ifr.ifr_addr = *ifa->ifa_addr;
236 1.16 roy if ((s = getsock(AF_INET)) == -1)
237 1.16 roy err(EXIT_FAILURE, "%s: getsock", __func__);
238 1.17 roy if (prog_ioctl(s, SIOCGIFAFLAG_IN, &ifr) == -1)
239 1.16 roy err(EXIT_FAILURE, "SIOCGIFAFLAG_IN");
240 1.18 roy return ifr.ifr_addrflags & flags ? true : false;
241 1.18 roy return false;
242 1.18 roy }
243 1.18 roy #endif
244 1.18 roy
245 1.18 roy static bool
246 1.18 roy in_addr_tentative(struct ifaddrs *ifa)
247 1.18 roy {
248 1.18 roy
249 1.18 roy #ifdef IN_IFF_TENTATIVE
250 1.18 roy return in_addr_flags(ifa, IN_IFF_TENTATIVE);
251 1.18 roy #else
252 1.18 roy return false;
253 1.18 roy #endif
254 1.18 roy }
255 1.18 roy
256 1.18 roy static bool
257 1.18 roy in_addr_tentative_or_detached(struct ifaddrs *ifa)
258 1.18 roy {
259 1.18 roy
260 1.18 roy #ifdef IN_IFF_TENTATIVE
261 1.18 roy return in_addr_flags(ifa, IN_IFF_TENTATIVE | IN_IFF_DETACHED);
262 1.16 roy #else
263 1.16 roy return false;
264 1.16 roy #endif
265 1.16 roy }
266 1.16 roy
267 1.12 dyoung static void
268 1.12 dyoung in_constructor(void)
269 1.12 dyoung {
270 1.12 dyoung register_family(&af);
271 1.12 dyoung }
272