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/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 55 static struct in_aliasreq in_addreq; 56 static struct ifreq in_ridreq; 57 static char addr_buf[NI_MAXHOST]; /*for getnameinfo()*/ 58 extern char *f_inet, *f_addr; 59 60 static void 61 in_status(int s __unused, const struct ifaddrs *ifa) 62 { 63 struct sockaddr_in *sin, null_sin; 64 #ifndef FSTACK 65 int error, n_flags; 66 #endif 67 68 memset(&null_sin, 0, sizeof(null_sin)); 69 70 sin = (struct sockaddr_in *)ifa->ifa_addr; 71 if (sin == NULL) 72 return; 73 74 #ifndef FSTACK 75 if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0) 76 n_flags = 0; 77 else if (f_addr != NULL && strcmp(f_addr, "host") == 0) 78 n_flags = NI_NOFQDN; 79 else 80 n_flags = NI_NUMERICHOST; 81 82 error = getnameinfo((struct sockaddr *)sin, sin->sin_len, addr_buf, 83 sizeof(addr_buf), NULL, 0, n_flags); 84 85 if (error) 86 #endif 87 inet_ntop(AF_INET, &sin->sin_addr, addr_buf, sizeof(addr_buf)); 88 89 printf("\tinet %s", addr_buf); 90 91 if (ifa->ifa_flags & IFF_POINTOPOINT) { 92 sin = (struct sockaddr_in *)ifa->ifa_dstaddr; 93 if (sin == NULL) 94 sin = &null_sin; 95 printf(" --> %s ", inet_ntoa(sin->sin_addr)); 96 } 97 98 sin = (struct sockaddr_in *)ifa->ifa_netmask; 99 if (sin == NULL) 100 sin = &null_sin; 101 if (f_inet != NULL && strcmp(f_inet, "cidr") == 0) { 102 int cidr = 32; 103 unsigned long smask; 104 105 smask = ntohl(sin->sin_addr.s_addr); 106 while ((smask & 1) == 0) { 107 smask = smask >> 1; 108 cidr--; 109 if (cidr == 0) 110 break; 111 } 112 printf("/%d ", cidr); 113 } else if (f_inet != NULL && strcmp(f_inet, "dotted") == 0) 114 printf(" netmask %s ", inet_ntoa(sin->sin_addr)); 115 else 116 printf(" netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr)); 117 118 if (ifa->ifa_flags & IFF_BROADCAST) { 119 sin = (struct sockaddr_in *)ifa->ifa_broadaddr; 120 if (sin != NULL && sin->sin_addr.s_addr != 0) 121 printf("broadcast %s ", inet_ntoa(sin->sin_addr)); 122 } 123 124 print_vhid(ifa, " "); 125 126 putchar('\n'); 127 } 128 129 #define SIN(x) ((struct sockaddr_in *) &(x)) 130 static struct sockaddr_in *sintab[] = { 131 SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr), 132 SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr) 133 }; 134 135 static void 136 in_getaddr(const char *s, int which) 137 { 138 struct sockaddr_in *sin = sintab[which]; 139 #ifndef FSTACK 140 struct hostent *hp; 141 struct netent *np; 142 #endif 143 144 sin->sin_len = sizeof(*sin); 145 sin->sin_family = AF_INET; 146 147 if (which == ADDR) { 148 char *p = NULL; 149 150 if((p = strrchr(s, '/')) != NULL) { 151 const char *errstr; 152 /* address is `name/masklen' */ 153 int masklen; 154 struct sockaddr_in *min = sintab[MASK]; 155 *p = '\0'; 156 if (!isdigit(*(p + 1))) 157 errstr = "invalid"; 158 else 159 masklen = (int)strtonum(p + 1, 0, 32, &errstr); 160 if (errstr != NULL) { 161 *p = '/'; 162 errx(1, "%s: bad value (width %s)", s, errstr); 163 } 164 min->sin_family = AF_INET; 165 min->sin_len = sizeof(*min); 166 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) & 167 0xffffffff); 168 } 169 } 170 171 if (inet_aton(s, &sin->sin_addr)) 172 return; 173 #ifdef FSTACK 174 else 175 errx(1, "%s: bad value", s); 176 #else 177 if ((hp = gethostbyname(s)) != NULL) 178 bcopy(hp->h_addr, (char *)&sin->sin_addr, 179 MIN((size_t)hp->h_length, sizeof(sin->sin_addr))); 180 else if ((np = getnetbyname(s)) != NULL) 181 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 182 else 183 errx(1, "%s: bad value", s); 184 #endif 185 } 186 187 static void 188 in_status_tunnel(int s) 189 { 190 char src[NI_MAXHOST]; 191 char dst[NI_MAXHOST]; 192 struct ifreq ifr; 193 const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr; 194 #ifdef FSTACK 195 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa; 196 #endif 197 198 memset(&ifr, 0, sizeof(ifr)); 199 strlcpy(ifr.ifr_name, name, IFNAMSIZ); 200 201 if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0) 202 return; 203 if (sa->sa_family != AF_INET) 204 return; 205 206 #ifndef FSTACK 207 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0) 208 src[0] = '\0'; 209 #else 210 if (inet_ntop(AF_INET, &sin->sin_addr, src, sizeof(src)) == NULL) 211 return; 212 #endif 213 214 if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0) 215 return; 216 if (sa->sa_family != AF_INET) 217 return; 218 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 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 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