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 63 in_status(int s __unused, const struct ifaddrs *ifa) 64 { 65 struct sockaddr_in *sin, null_sin; 66 int error, n_flags; 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 if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0) 75 n_flags = 0; 76 else if (f_addr != NULL && strcmp(f_addr, "host") == 0) 77 n_flags = NI_NOFQDN; 78 else 79 n_flags = NI_NUMERICHOST; 80 81 error = getnameinfo((struct sockaddr *)sin, sin->sin_len, addr_buf, 82 sizeof(addr_buf), NULL, 0, n_flags); 83 84 if (error) 85 inet_ntop(AF_INET, &sin->sin_addr, addr_buf, sizeof(addr_buf)); 86 87 printf("\tinet %s", addr_buf); 88 89 if (ifa->ifa_flags & IFF_POINTOPOINT) { 90 sin = (struct sockaddr_in *)ifa->ifa_dstaddr; 91 if (sin == NULL) 92 sin = &null_sin; 93 printf(" --> %s", inet_ntoa(sin->sin_addr)); 94 } 95 96 sin = (struct sockaddr_in *)ifa->ifa_netmask; 97 if (sin == NULL) 98 sin = &null_sin; 99 if (f_inet != NULL && strcmp(f_inet, "cidr") == 0) { 100 int cidr = 32; 101 unsigned long smask; 102 103 smask = ntohl(sin->sin_addr.s_addr); 104 while ((smask & 1) == 0) { 105 smask = smask >> 1; 106 cidr--; 107 if (cidr == 0) 108 break; 109 } 110 printf("/%d", cidr); 111 } else if (f_inet != NULL && strcmp(f_inet, "dotted") == 0) 112 printf(" netmask %s", inet_ntoa(sin->sin_addr)); 113 else 114 printf(" netmask 0x%lx", (unsigned long)ntohl(sin->sin_addr.s_addr)); 115 116 if (ifa->ifa_flags & IFF_BROADCAST) { 117 sin = (struct sockaddr_in *)ifa->ifa_broadaddr; 118 if (sin != NULL && sin->sin_addr.s_addr != 0) 119 printf(" broadcast %s", inet_ntoa(sin->sin_addr)); 120 } 121 122 print_vhid(ifa, " "); 123 124 putchar('\n'); 125 } 126 127 #define SIN(x) ((struct sockaddr_in *) &(x)) 128 static struct sockaddr_in *sintab[] = { 129 SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr), 130 SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr) 131 }; 132 133 static void 134 in_getaddr(const char *s, int which) 135 { 136 struct sockaddr_in *sin = sintab[which]; 137 struct hostent *hp; 138 struct netent *np; 139 140 sin->sin_len = sizeof(*sin); 141 sin->sin_family = AF_INET; 142 143 if (which == ADDR) { 144 char *p = NULL; 145 146 if((p = strrchr(s, '/')) != NULL) { 147 const char *errstr; 148 /* address is `name/masklen' */ 149 int masklen; 150 struct sockaddr_in *min = sintab[MASK]; 151 *p = '\0'; 152 if (!isdigit(*(p + 1))) 153 errstr = "invalid"; 154 else 155 masklen = (int)strtonum(p + 1, 0, 32, &errstr); 156 if (errstr != NULL) { 157 *p = '/'; 158 errx(1, "%s: bad value (width %s)", s, errstr); 159 } 160 min->sin_family = AF_INET; 161 min->sin_len = sizeof(*min); 162 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) & 163 0xffffffff); 164 } 165 } 166 167 if (inet_aton(s, &sin->sin_addr)) 168 return; 169 if ((hp = gethostbyname(s)) != NULL) 170 bcopy(hp->h_addr, (char *)&sin->sin_addr, 171 MIN((size_t)hp->h_length, sizeof(sin->sin_addr))); 172 else if ((np = getnetbyname(s)) != NULL) 173 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 174 else 175 errx(1, "%s: bad value", s); 176 } 177 178 static void 179 in_postproc(int s, const struct afswtch *afp, int newaddr, int ifflags) 180 { 181 if (sintab[ADDR]->sin_len != 0 && sintab[MASK]->sin_len == 0 && 182 newaddr && (ifflags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) { 183 warnx("WARNING: setting interface address without mask " 184 "is deprecated,\ndefault mask may not be correct."); 185 } 186 } 187 188 static void 189 in_status_tunnel(int s) 190 { 191 char src[NI_MAXHOST]; 192 char dst[NI_MAXHOST]; 193 struct ifreq ifr; 194 const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr; 195 196 memset(&ifr, 0, sizeof(ifr)); 197 strlcpy(ifr.ifr_name, name, IFNAMSIZ); 198 199 if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0) 200 return; 201 if (sa->sa_family != AF_INET) 202 return; 203 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0) 204 src[0] = '\0'; 205 206 if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0) 207 return; 208 if (sa->sa_family != AF_INET) 209 return; 210 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0) 211 dst[0] = '\0'; 212 213 printf("\ttunnel inet %s --> %s\n", src, dst); 214 } 215 216 static void 217 in_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres) 218 { 219 struct in_aliasreq addreq; 220 221 memset(&addreq, 0, sizeof(addreq)); 222 strlcpy(addreq.ifra_name, name, IFNAMSIZ); 223 memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len); 224 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len); 225 226 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0) 227 warn("SIOCSIFPHYADDR"); 228 } 229 230 static struct afswtch af_inet = { 231 .af_name = "inet", 232 .af_af = AF_INET, 233 .af_status = in_status, 234 .af_getaddr = in_getaddr, 235 .af_postproc = in_postproc, 236 .af_status_tunnel = in_status_tunnel, 237 .af_settunnel = in_set_tunnel, 238 .af_difaddr = SIOCDIFADDR, 239 .af_aifaddr = SIOCAIFADDR, 240 .af_ridreq = &in_ridreq, 241 .af_addreq = &in_addreq, 242 }; 243 244 static __constructor void 245 inet_ctor(void) 246 { 247 248 #ifndef RESCUE 249 if (!feature_present("inet")) 250 return; 251 #endif 252 af_register(&af_inet); 253 } 254