af_inet.c revision 194799
1138593Ssam/*
2138593Ssam * Copyright (c) 1983, 1993
3138593Ssam *	The Regents of the University of California.  All rights reserved.
4138593Ssam *
5138593Ssam * Redistribution and use in source and binary forms, with or without
6138593Ssam * modification, are permitted provided that the following conditions
7138593Ssam * are met:
8138593Ssam * 1. Redistributions of source code must retain the above copyright
9138593Ssam *    notice, this list of conditions and the following disclaimer.
10138593Ssam * 2. Redistributions in binary form must reproduce the above copyright
11138593Ssam *    notice, this list of conditions and the following disclaimer in the
12138593Ssam *    documentation and/or other materials provided with the distribution.
13138593Ssam * 4. Neither the name of the University nor the names of its contributors
14138593Ssam *    may be used to endorse or promote products derived from this software
15138593Ssam *    without specific prior written permission.
16138593Ssam *
17138593Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18138593Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19138593Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20138593Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21138593Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22138593Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23138593Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24138593Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25138593Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26138593Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27138593Ssam * SUCH DAMAGE.
28138593Ssam */
29138593Ssam
30138593Ssam#ifndef lint
31138593Ssamstatic const char rcsid[] =
32138593Ssam  "$FreeBSD: head/sbin/ifconfig/af_inet.c 194799 2009-06-23 23:49:52Z delphij $";
33138593Ssam#endif /* not lint */
34138593Ssam
35138593Ssam#include <sys/types.h>
36138593Ssam#include <sys/ioctl.h>
37138593Ssam#include <sys/socket.h>
38138593Ssam#include <net/if.h>
39138593Ssam
40138593Ssam#include <err.h>
41138593Ssam#include <stdio.h>
42138593Ssam#include <stdlib.h>
43138593Ssam#include <string.h>
44138593Ssam#include <unistd.h>
45166956Ssam#include <ifaddrs.h>
46138593Ssam
47138593Ssam#include <netinet/in.h>
48138593Ssam#include <net/if_var.h>		/* for struct ifaddr */
49138593Ssam#include <netinet/in_var.h>
50138593Ssam#include <arpa/inet.h>
51138593Ssam#include <netdb.h>
52138593Ssam
53138593Ssam#include "ifconfig.h"
54138593Ssam
55191121Sbrooksstatic struct in_aliasreq in_addreq;
56138593Ssamstatic struct ifreq in_ridreq;
57138593Ssam
58138593Ssamstatic void
59166956Ssamin_status(int s __unused, const struct ifaddrs *ifa)
60138593Ssam{
61138593Ssam	struct sockaddr_in *sin, null_sin;
62138593Ssam
63138593Ssam	memset(&null_sin, 0, sizeof(null_sin));
64138593Ssam
65166956Ssam	sin = (struct sockaddr_in *)ifa->ifa_addr;
66138593Ssam	if (sin == NULL)
67138593Ssam		return;
68138593Ssam
69138593Ssam	printf("\tinet %s ", inet_ntoa(sin->sin_addr));
70138593Ssam
71166956Ssam	if (ifa->ifa_flags & IFF_POINTOPOINT) {
72166956Ssam		sin = (struct sockaddr_in *)ifa->ifa_dstaddr;
73166956Ssam		if (sin == NULL)
74138593Ssam			sin = &null_sin;
75138593Ssam		printf("--> %s ", inet_ntoa(sin->sin_addr));
76138593Ssam	}
77138593Ssam
78166956Ssam	sin = (struct sockaddr_in *)ifa->ifa_netmask;
79166956Ssam	if (sin == NULL)
80138593Ssam		sin = &null_sin;
81138593Ssam	printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
82138593Ssam
83166956Ssam	if (ifa->ifa_flags & IFF_BROADCAST) {
84166956Ssam		sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
85166956Ssam		if (sin != NULL && sin->sin_addr.s_addr != 0)
86138593Ssam			printf("broadcast %s", inet_ntoa(sin->sin_addr));
87138593Ssam	}
88138593Ssam	putchar('\n');
89138593Ssam}
90138593Ssam
91138593Ssam#define SIN(x) ((struct sockaddr_in *) &(x))
92138593Ssamstatic struct sockaddr_in *sintab[] = {
93138593Ssam	SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
94138593Ssam	SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)
95138593Ssam};
96138593Ssam
97138593Ssamstatic void
98138593Ssamin_getaddr(const char *s, int which)
99138593Ssam{
100138593Ssam#define	MIN(a,b)	((a)<(b)?(a):(b))
101138593Ssam	struct sockaddr_in *sin = sintab[which];
102138593Ssam	struct hostent *hp;
103138593Ssam	struct netent *np;
104138593Ssam
105138593Ssam	sin->sin_len = sizeof(*sin);
106138593Ssam	if (which != MASK)
107138593Ssam		sin->sin_family = AF_INET;
108138593Ssam
109138593Ssam	if (which == ADDR) {
110138593Ssam		char *p = NULL;
111138593Ssam
112138593Ssam		if((p = strrchr(s, '/')) != NULL) {
113138593Ssam			/* address is `name/masklen' */
114138593Ssam			int masklen;
115138593Ssam			int ret;
116138593Ssam			struct sockaddr_in *min = sintab[MASK];
117138593Ssam			*p = '\0';
118138593Ssam			ret = sscanf(p+1, "%u", &masklen);
119138593Ssam			if(ret != 1 || (masklen < 0 || masklen > 32)) {
120138593Ssam				*p = '/';
121138593Ssam				errx(1, "%s: bad value", s);
122138593Ssam			}
123138593Ssam			min->sin_len = sizeof(*min);
124138593Ssam			min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
125138593Ssam				              0xffffffff);
126138593Ssam		}
127138593Ssam	}
128138593Ssam
129138593Ssam	if (inet_aton(s, &sin->sin_addr))
130138593Ssam		return;
131138593Ssam	if ((hp = gethostbyname(s)) != 0)
132138593Ssam		bcopy(hp->h_addr, (char *)&sin->sin_addr,
133194799Sdelphij		    MIN((size_t)hp->h_length, sizeof(sin->sin_addr)));
134138593Ssam	else if ((np = getnetbyname(s)) != 0)
135138593Ssam		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
136138593Ssam	else
137138593Ssam		errx(1, "%s: bad value", s);
138138593Ssam#undef MIN
139138593Ssam}
140138593Ssam
141138593Ssamstatic void
142138593Ssamin_status_tunnel(int s)
143138593Ssam{
144138593Ssam	char src[NI_MAXHOST];
145138593Ssam	char dst[NI_MAXHOST];
146138593Ssam	struct ifreq ifr;
147138593Ssam	const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr;
148138593Ssam
149138593Ssam	memset(&ifr, 0, sizeof(ifr));
150138593Ssam	strncpy(ifr.ifr_name, name, IFNAMSIZ);
151138593Ssam
152138593Ssam	if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0)
153138593Ssam		return;
154147437Sume	if (sa->sa_family != AF_INET)
155147437Sume		return;
156138593Ssam	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0)
157138593Ssam		src[0] = '\0';
158138593Ssam
159138593Ssam	if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0)
160138593Ssam		return;
161147437Sume	if (sa->sa_family != AF_INET)
162147437Sume		return;
163138593Ssam	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0)
164138593Ssam		dst[0] = '\0';
165138593Ssam
166138593Ssam	printf("\ttunnel inet %s --> %s\n", src, dst);
167138593Ssam}
168138593Ssam
169138593Ssamstatic void
170138593Ssamin_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
171138593Ssam{
172191121Sbrooks	struct in_aliasreq addreq;
173138593Ssam
174138593Ssam	memset(&addreq, 0, sizeof(addreq));
175138593Ssam	strncpy(addreq.ifra_name, name, IFNAMSIZ);
176138593Ssam	memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
177138593Ssam	memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len);
178138593Ssam
179138593Ssam	if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
180138593Ssam		warn("SIOCSIFPHYADDR");
181138593Ssam}
182138593Ssam
183138593Ssamstatic struct afswtch af_inet = {
184138593Ssam	.af_name	= "inet",
185138593Ssam	.af_af		= AF_INET,
186138593Ssam	.af_status	= in_status,
187138593Ssam	.af_getaddr	= in_getaddr,
188138593Ssam	.af_status_tunnel = in_status_tunnel,
189138593Ssam	.af_settunnel	= in_set_tunnel,
190138593Ssam	.af_difaddr	= SIOCDIFADDR,
191138593Ssam	.af_aifaddr	= SIOCAIFADDR,
192138593Ssam	.af_ridreq	= &in_ridreq,
193138593Ssam	.af_addreq	= &in_addreq,
194138593Ssam};
195138593Ssam
196138593Ssamstatic __constructor void
197138593Ssaminet_ctor(void)
198138593Ssam{
199138593Ssam	af_register(&af_inet);
200138593Ssam}
201