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