xref: /f-stack/tools/ifconfig/af_inet.c (revision d4a07e70)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char rcsid[] =
34   "$FreeBSD$";
35 #endif /* not lint */
36 
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <net/if.h>
41 
42 #include <ctype.h>
43 #include <err.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <ifaddrs.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <arpa/inet.h>
53 #include <netdb.h>
54 
55 #include "ifconfig.h"
56 
57 static struct in_aliasreq in_addreq;
58 static struct ifreq in_ridreq;
59 static char addr_buf[NI_MAXHOST];	/*for getnameinfo()*/
60 extern char *f_inet, *f_addr;
61 
62 static void
in_status(int s __unused,const struct ifaddrs * ifa)63 in_status(int s __unused, const struct ifaddrs *ifa)
64 {
65 	struct sockaddr_in *sin, null_sin;
66 #ifndef FSTACK
67 	int error, n_flags;
68 #endif
69 
70 	memset(&null_sin, 0, sizeof(null_sin));
71 
72 	sin = (struct sockaddr_in *)ifa->ifa_addr;
73 	if (sin == NULL)
74 		return;
75 
76 #ifndef FSTACK
77 	if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
78 		n_flags = 0;
79 	else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
80 		n_flags = NI_NOFQDN;
81 	else
82 		n_flags = NI_NUMERICHOST;
83 
84 	error = getnameinfo((struct sockaddr *)sin, sin->sin_len, addr_buf,
85 			    sizeof(addr_buf), NULL, 0, n_flags);
86 
87 	if (error)
88 #endif
89 		inet_ntop(AF_INET, &sin->sin_addr, addr_buf, sizeof(addr_buf));
90 
91 	printf("\tinet %s", addr_buf);
92 
93 	if (ifa->ifa_flags & IFF_POINTOPOINT) {
94 		sin = (struct sockaddr_in *)ifa->ifa_dstaddr;
95 		if (sin == NULL)
96 			sin = &null_sin;
97 		printf(" --> %s", inet_ntoa(sin->sin_addr));
98 	}
99 
100 	sin = (struct sockaddr_in *)ifa->ifa_netmask;
101 	if (sin == NULL)
102 		sin = &null_sin;
103 	if (f_inet != NULL && strcmp(f_inet, "cidr") == 0) {
104 		int cidr = 32;
105 		unsigned long smask;
106 
107 		smask = ntohl(sin->sin_addr.s_addr);
108 		while ((smask & 1) == 0) {
109 			smask = smask >> 1;
110 			cidr--;
111 			if (cidr == 0)
112 				break;
113 		}
114 		printf("/%d", cidr);
115 	} else if (f_inet != NULL && strcmp(f_inet, "dotted") == 0)
116 		printf(" netmask %s", inet_ntoa(sin->sin_addr));
117 	else
118 		printf(" netmask 0x%lx", (unsigned long)ntohl(sin->sin_addr.s_addr));
119 
120 	if (ifa->ifa_flags & IFF_BROADCAST) {
121 		sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
122 		if (sin != NULL && sin->sin_addr.s_addr != 0)
123 			printf(" broadcast %s", inet_ntoa(sin->sin_addr));
124 	}
125 
126 	print_vhid(ifa, " ");
127 
128 	putchar('\n');
129 }
130 
131 #define SIN(x) ((struct sockaddr_in *) &(x))
132 static struct sockaddr_in *sintab[] = {
133 	SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
134 	SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)
135 };
136 
137 static void
in_getaddr(const char * s,int which)138 in_getaddr(const char *s, int which)
139 {
140 	struct sockaddr_in *sin = sintab[which];
141 #ifndef FSTACK
142 	struct hostent *hp;
143 	struct netent *np;
144 #endif
145 
146 	sin->sin_len = sizeof(*sin);
147 	sin->sin_family = AF_INET;
148 
149 	if (which == ADDR) {
150 		char *p = NULL;
151 
152 		if((p = strrchr(s, '/')) != NULL) {
153 			const char *errstr;
154 			/* address is `name/masklen' */
155 			int masklen;
156 			struct sockaddr_in *min = sintab[MASK];
157 			*p = '\0';
158 			if (!isdigit(*(p + 1)))
159 				errstr = "invalid";
160 			else
161 				masklen = (int)strtonum(p + 1, 0, 32, &errstr);
162 			if (errstr != NULL) {
163 				*p = '/';
164 				errx(1, "%s: bad value (width %s)", s, errstr);
165 			}
166 			min->sin_family = AF_INET;
167 			min->sin_len = sizeof(*min);
168 			min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
169 				              0xffffffff);
170 		}
171 	}
172 
173 	if (inet_aton(s, &sin->sin_addr))
174 		return;
175 #ifdef FSTACK
176 	else
177 		errx(1, "%s: bad value", s);
178 #else
179 	if ((hp = gethostbyname(s)) != NULL)
180 		bcopy(hp->h_addr, (char *)&sin->sin_addr,
181 		    MIN((size_t)hp->h_length, sizeof(sin->sin_addr)));
182 	else if ((np = getnetbyname(s)) != NULL)
183 		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
184 	else
185 		errx(1, "%s: bad value", s);
186 #endif
187 }
188 
189 static void
in_status_tunnel(int s)190 in_status_tunnel(int s)
191 {
192 	char src[NI_MAXHOST];
193 	char dst[NI_MAXHOST];
194 	struct ifreq ifr;
195 	const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr;
196 #ifdef FSTACK
197 	const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
198 #endif
199 
200 	memset(&ifr, 0, sizeof(ifr));
201 	strlcpy(ifr.ifr_name, name, IFNAMSIZ);
202 
203 	if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0)
204 		return;
205 	if (sa->sa_family != AF_INET)
206 		return;
207 #ifndef FSTACK
208 	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0)
209 		src[0] = '\0';
210 #else
211 	if (inet_ntop(AF_INET, &sin->sin_addr, src, sizeof(src)) == NULL)
212 		return;
213 #endif
214 
215 	if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0)
216 		return;
217 	if (sa->sa_family != AF_INET)
218 		return;
219 #ifndef FSTACK
220 	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0)
221 		dst[0] = '\0';
222 #else
223 	if (inet_ntop(AF_INET, &sin->sin_addr, dst, sizeof(dst)) == NULL)
224 		return;
225 #endif
226 
227 	printf("\ttunnel inet %s --> %s\n", src, dst);
228 }
229 
230 static void
in_set_tunnel(int s,struct addrinfo * srcres,struct addrinfo * dstres)231 in_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
232 {
233 	struct in_aliasreq addreq;
234 
235 	memset(&addreq, 0, sizeof(addreq));
236 	strlcpy(addreq.ifra_name, name, IFNAMSIZ);
237 	memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
238 	memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len);
239 
240 	if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
241 		warn("SIOCSIFPHYADDR");
242 }
243 
244 static struct afswtch af_inet = {
245 	.af_name	= "inet",
246 	.af_af		= AF_INET,
247 	.af_status	= in_status,
248 	.af_getaddr	= in_getaddr,
249 	.af_status_tunnel = in_status_tunnel,
250 	.af_settunnel	= in_set_tunnel,
251 	.af_difaddr	= SIOCDIFADDR,
252 	.af_aifaddr	= SIOCAIFADDR,
253 	.af_ridreq	= &in_ridreq,
254 	.af_addreq	= &in_addreq,
255 };
256 
257 static __constructor void
inet_ctor(void)258 inet_ctor(void)
259 {
260 #ifndef FSTACK
261 #ifndef RESCUE
262 	if (!feature_present("inet"))
263 		return;
264 #endif
265 #endif
266 	af_register(&af_inet);
267 }
268