1331722Seadler/* 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: stable/11/sbin/ifconfig/af_inet.c 345397 2019-03-21 22:40:05Z asomers $"; 33138593Ssam#endif /* not lint */ 34138593Ssam 35282747Sngie#include <sys/param.h> 36138593Ssam#include <sys/ioctl.h> 37138593Ssam#include <sys/socket.h> 38138593Ssam#include <net/if.h> 39138593Ssam 40202289Semaste#include <ctype.h> 41138593Ssam#include <err.h> 42138593Ssam#include <stdio.h> 43138593Ssam#include <stdlib.h> 44138593Ssam#include <string.h> 45138593Ssam#include <unistd.h> 46166956Ssam#include <ifaddrs.h> 47138593Ssam 48138593Ssam#include <netinet/in.h> 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; 57301185Sallanjudestatic char addr_buf[NI_MAXHOST]; /*for getnameinfo()*/ 58301059Sallanjudeextern char *f_inet, *f_addr; 59138593Ssam 60138593Ssamstatic void 61166956Ssamin_status(int s __unused, const struct ifaddrs *ifa) 62138593Ssam{ 63138593Ssam struct sockaddr_in *sin, null_sin; 64301059Sallanjude int error, n_flags; 65138593Ssam 66138593Ssam memset(&null_sin, 0, sizeof(null_sin)); 67138593Ssam 68166956Ssam sin = (struct sockaddr_in *)ifa->ifa_addr; 69138593Ssam if (sin == NULL) 70138593Ssam return; 71138593Ssam 72301059Sallanjude if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0) 73301059Sallanjude n_flags = 0; 74301059Sallanjude else if (f_addr != NULL && strcmp(f_addr, "host") == 0) 75301059Sallanjude n_flags = NI_NOFQDN; 76301059Sallanjude else 77301059Sallanjude n_flags = NI_NUMERICHOST; 78138593Ssam 79301059Sallanjude error = getnameinfo((struct sockaddr *)sin, sin->sin_len, addr_buf, 80301059Sallanjude sizeof(addr_buf), NULL, 0, n_flags); 81301059Sallanjude 82301059Sallanjude if (error) 83301059Sallanjude inet_ntop(AF_INET, &sin->sin_addr, addr_buf, sizeof(addr_buf)); 84301059Sallanjude 85301059Sallanjude printf("\tinet %s", addr_buf); 86301059Sallanjude 87166956Ssam if (ifa->ifa_flags & IFF_POINTOPOINT) { 88166956Ssam sin = (struct sockaddr_in *)ifa->ifa_dstaddr; 89166956Ssam if (sin == NULL) 90138593Ssam sin = &null_sin; 91330443Seadler printf(" --> %s", inet_ntoa(sin->sin_addr)); 92138593Ssam } 93138593Ssam 94166956Ssam sin = (struct sockaddr_in *)ifa->ifa_netmask; 95166956Ssam if (sin == NULL) 96138593Ssam sin = &null_sin; 97301059Sallanjude if (f_inet != NULL && strcmp(f_inet, "cidr") == 0) { 98301059Sallanjude int cidr = 32; 99301059Sallanjude unsigned long smask; 100138593Ssam 101301059Sallanjude smask = ntohl(sin->sin_addr.s_addr); 102301059Sallanjude while ((smask & 1) == 0) { 103301059Sallanjude smask = smask >> 1; 104301059Sallanjude cidr--; 105301059Sallanjude if (cidr == 0) 106301059Sallanjude break; 107301059Sallanjude } 108345397Sasomers printf("/%d", cidr); 109301059Sallanjude } else if (f_inet != NULL && strcmp(f_inet, "dotted") == 0) 110345397Sasomers printf(" netmask %s", inet_ntoa(sin->sin_addr)); 111301059Sallanjude else 112345397Sasomers printf(" netmask 0x%lx", (unsigned long)ntohl(sin->sin_addr.s_addr)); 113301059Sallanjude 114166956Ssam if (ifa->ifa_flags & IFF_BROADCAST) { 115166956Ssam sin = (struct sockaddr_in *)ifa->ifa_broadaddr; 116166956Ssam if (sin != NULL && sin->sin_addr.s_addr != 0) 117345397Sasomers printf(" broadcast %s", inet_ntoa(sin->sin_addr)); 118138593Ssam } 119228571Sglebius 120228571Sglebius print_vhid(ifa, " "); 121228571Sglebius 122138593Ssam putchar('\n'); 123138593Ssam} 124138593Ssam 125138593Ssam#define SIN(x) ((struct sockaddr_in *) &(x)) 126138593Ssamstatic struct sockaddr_in *sintab[] = { 127138593Ssam SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr), 128138593Ssam SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr) 129138593Ssam}; 130138593Ssam 131138593Ssamstatic void 132138593Ssamin_getaddr(const char *s, int which) 133138593Ssam{ 134138593Ssam struct sockaddr_in *sin = sintab[which]; 135138593Ssam struct hostent *hp; 136138593Ssam struct netent *np; 137138593Ssam 138138593Ssam sin->sin_len = sizeof(*sin); 139227738Sglebius sin->sin_family = AF_INET; 140138593Ssam 141138593Ssam if (which == ADDR) { 142138593Ssam char *p = NULL; 143138593Ssam 144138593Ssam if((p = strrchr(s, '/')) != NULL) { 145202289Semaste const char *errstr; 146138593Ssam /* address is `name/masklen' */ 147138593Ssam int masklen; 148138593Ssam struct sockaddr_in *min = sintab[MASK]; 149138593Ssam *p = '\0'; 150202289Semaste if (!isdigit(*(p + 1))) 151202289Semaste errstr = "invalid"; 152202289Semaste else 153202289Semaste masklen = (int)strtonum(p + 1, 0, 32, &errstr); 154202289Semaste if (errstr != NULL) { 155138593Ssam *p = '/'; 156202289Semaste errx(1, "%s: bad value (width %s)", s, errstr); 157138593Ssam } 158228574Sglebius min->sin_family = AF_INET; 159138593Ssam min->sin_len = sizeof(*min); 160138593Ssam min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) & 161138593Ssam 0xffffffff); 162138593Ssam } 163138593Ssam } 164138593Ssam 165138593Ssam if (inet_aton(s, &sin->sin_addr)) 166138593Ssam return; 167298206Saraujo if ((hp = gethostbyname(s)) != NULL) 168138593Ssam bcopy(hp->h_addr, (char *)&sin->sin_addr, 169194799Sdelphij MIN((size_t)hp->h_length, sizeof(sin->sin_addr))); 170298206Saraujo else if ((np = getnetbyname(s)) != NULL) 171138593Ssam sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 172138593Ssam else 173138593Ssam errx(1, "%s: bad value", s); 174138593Ssam} 175138593Ssam 176138593Ssamstatic void 177138593Ssamin_status_tunnel(int s) 178138593Ssam{ 179138593Ssam char src[NI_MAXHOST]; 180138593Ssam char dst[NI_MAXHOST]; 181138593Ssam struct ifreq ifr; 182138593Ssam const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr; 183138593Ssam 184138593Ssam memset(&ifr, 0, sizeof(ifr)); 185299873Struckman strlcpy(ifr.ifr_name, name, IFNAMSIZ); 186138593Ssam 187138593Ssam if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0) 188138593Ssam return; 189147437Sume if (sa->sa_family != AF_INET) 190147437Sume return; 191138593Ssam if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0) 192138593Ssam src[0] = '\0'; 193138593Ssam 194138593Ssam if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0) 195138593Ssam return; 196147437Sume if (sa->sa_family != AF_INET) 197147437Sume return; 198138593Ssam if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0) 199138593Ssam dst[0] = '\0'; 200138593Ssam 201138593Ssam printf("\ttunnel inet %s --> %s\n", src, dst); 202138593Ssam} 203138593Ssam 204138593Ssamstatic void 205138593Ssamin_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres) 206138593Ssam{ 207191121Sbrooks struct in_aliasreq addreq; 208138593Ssam 209138593Ssam memset(&addreq, 0, sizeof(addreq)); 210299873Struckman strlcpy(addreq.ifra_name, name, IFNAMSIZ); 211138593Ssam memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len); 212138593Ssam memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len); 213138593Ssam 214138593Ssam if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0) 215138593Ssam warn("SIOCSIFPHYADDR"); 216138593Ssam} 217138593Ssam 218138593Ssamstatic struct afswtch af_inet = { 219138593Ssam .af_name = "inet", 220138593Ssam .af_af = AF_INET, 221138593Ssam .af_status = in_status, 222138593Ssam .af_getaddr = in_getaddr, 223138593Ssam .af_status_tunnel = in_status_tunnel, 224138593Ssam .af_settunnel = in_set_tunnel, 225138593Ssam .af_difaddr = SIOCDIFADDR, 226138593Ssam .af_aifaddr = SIOCAIFADDR, 227138593Ssam .af_ridreq = &in_ridreq, 228138593Ssam .af_addreq = &in_addreq, 229138593Ssam}; 230138593Ssam 231138593Ssamstatic __constructor void 232138593Ssaminet_ctor(void) 233138593Ssam{ 234224179Sbz 235224179Sbz#ifndef RESCUE 236222527Sbz if (!feature_present("inet")) 237222527Sbz return; 238224179Sbz#endif 239138593Ssam af_register(&af_inet); 240138593Ssam} 241