]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sbin/ifconfig/af_inet.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sbin / ifconfig / af_inet.c
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
31 static const char rcsid[] =
32   "$FreeBSD$";
33 #endif /* not lint */
34
35 #include <sys/types.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 <net/if_var.h>         /* for struct ifaddr */
50 #include <netinet/in_var.h>
51 #include <arpa/inet.h>
52 #include <netdb.h>
53
54 #include "ifconfig.h"
55
56 static struct in_aliasreq in_addreq;
57 static struct ifreq in_ridreq;
58
59 static void
60 in_status(int s __unused, const struct ifaddrs *ifa)
61 {
62         struct sockaddr_in *sin, null_sin;
63         
64         memset(&null_sin, 0, sizeof(null_sin));
65
66         sin = (struct sockaddr_in *)ifa->ifa_addr;
67         if (sin == NULL)
68                 return;
69
70         printf("\tinet %s ", inet_ntoa(sin->sin_addr));
71
72         if (ifa->ifa_flags & IFF_POINTOPOINT) {
73                 sin = (struct sockaddr_in *)ifa->ifa_dstaddr;
74                 if (sin == NULL)
75                         sin = &null_sin;
76                 printf("--> %s ", inet_ntoa(sin->sin_addr));
77         }
78
79         sin = (struct sockaddr_in *)ifa->ifa_netmask;
80         if (sin == NULL)
81                 sin = &null_sin;
82         printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
83
84         if (ifa->ifa_flags & IFF_BROADCAST) {
85                 sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
86                 if (sin != NULL && sin->sin_addr.s_addr != 0)
87                         printf("broadcast %s", inet_ntoa(sin->sin_addr));
88         }
89         putchar('\n');
90 }
91
92 #define SIN(x) ((struct sockaddr_in *) &(x))
93 static struct sockaddr_in *sintab[] = {
94         SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
95         SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)
96 };
97
98 static void
99 in_getaddr(const char *s, int which)
100 {
101 #define MIN(a,b)        ((a)<(b)?(a):(b))
102         struct sockaddr_in *sin = sintab[which];
103         struct hostent *hp;
104         struct netent *np;
105
106         sin->sin_len = sizeof(*sin);
107         if (which != MASK)
108                 sin->sin_family = AF_INET;
109
110         if (which == ADDR) {
111                 char *p = NULL;
112
113                 if((p = strrchr(s, '/')) != NULL) {
114                         const char *errstr;
115                         /* address is `name/masklen' */
116                         int masklen;
117                         struct sockaddr_in *min = sintab[MASK];
118                         *p = '\0';
119                         if (!isdigit(*(p + 1)))
120                                 errstr = "invalid";
121                         else
122                                 masklen = (int)strtonum(p + 1, 0, 32, &errstr);
123                         if (errstr != NULL) {
124                                 *p = '/';
125                                 errx(1, "%s: bad value (width %s)", s, errstr);
126                         }
127                         min->sin_len = sizeof(*min);
128                         min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) & 
129                                               0xffffffff);
130                 }
131         }
132
133         if (inet_aton(s, &sin->sin_addr))
134                 return;
135         if ((hp = gethostbyname(s)) != 0)
136                 bcopy(hp->h_addr, (char *)&sin->sin_addr, 
137                     MIN((size_t)hp->h_length, sizeof(sin->sin_addr)));
138         else if ((np = getnetbyname(s)) != 0)
139                 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
140         else
141                 errx(1, "%s: bad value", s);
142 #undef MIN
143 }
144
145 static void
146 in_status_tunnel(int s)
147 {
148         char src[NI_MAXHOST];
149         char dst[NI_MAXHOST];
150         struct ifreq ifr;
151         const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr;
152
153         memset(&ifr, 0, sizeof(ifr));
154         strncpy(ifr.ifr_name, name, IFNAMSIZ);
155
156         if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0)
157                 return;
158         if (sa->sa_family != AF_INET)
159                 return;
160         if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0)
161                 src[0] = '\0';
162
163         if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0)
164                 return;
165         if (sa->sa_family != AF_INET)
166                 return;
167         if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0)
168                 dst[0] = '\0';
169
170         printf("\ttunnel inet %s --> %s\n", src, dst);
171 }
172
173 static void
174 in_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
175 {
176         struct in_aliasreq addreq;
177
178         memset(&addreq, 0, sizeof(addreq));
179         strncpy(addreq.ifra_name, name, IFNAMSIZ);
180         memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
181         memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len);
182
183         if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
184                 warn("SIOCSIFPHYADDR");
185 }
186
187 static struct afswtch af_inet = {
188         .af_name        = "inet",
189         .af_af          = AF_INET,
190         .af_status      = in_status,
191         .af_getaddr     = in_getaddr,
192         .af_status_tunnel = in_status_tunnel,
193         .af_settunnel   = in_set_tunnel,
194         .af_difaddr     = SIOCDIFADDR,
195         .af_aifaddr     = SIOCAIFADDR,
196         .af_ridreq      = &in_ridreq,
197         .af_addreq      = &in_addreq,
198 };
199
200 static __constructor void
201 inet_ctor(void)
202 {
203
204 #ifndef RESCUE
205         if (!feature_present("inet"))
206                 return;
207 #endif
208         af_register(&af_inet);
209 }