af_inet.c revision 228571
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 228571 2011-12-16 12:16:56Z glebius $"; 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 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 <net/if_var.h> /* for struct ifaddr */ 50138593Ssam#include <netinet/in_var.h> 51138593Ssam#include <arpa/inet.h> 52138593Ssam#include <netdb.h> 53138593Ssam 54138593Ssam#include "ifconfig.h" 55138593Ssam 56191121Sbrooksstatic struct in_aliasreq in_addreq; 57138593Ssamstatic struct ifreq in_ridreq; 58138593Ssam 59138593Ssamstatic void 60166956Ssamin_status(int s __unused, const struct ifaddrs *ifa) 61138593Ssam{ 62138593Ssam struct sockaddr_in *sin, null_sin; 63138593Ssam 64138593Ssam memset(&null_sin, 0, sizeof(null_sin)); 65138593Ssam 66166956Ssam sin = (struct sockaddr_in *)ifa->ifa_addr; 67138593Ssam if (sin == NULL) 68138593Ssam return; 69138593Ssam 70138593Ssam printf("\tinet %s ", inet_ntoa(sin->sin_addr)); 71138593Ssam 72166956Ssam if (ifa->ifa_flags & IFF_POINTOPOINT) { 73166956Ssam sin = (struct sockaddr_in *)ifa->ifa_dstaddr; 74166956Ssam if (sin == NULL) 75138593Ssam sin = &null_sin; 76138593Ssam printf("--> %s ", inet_ntoa(sin->sin_addr)); 77138593Ssam } 78138593Ssam 79166956Ssam sin = (struct sockaddr_in *)ifa->ifa_netmask; 80166956Ssam if (sin == NULL) 81138593Ssam sin = &null_sin; 82138593Ssam printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr)); 83138593Ssam 84166956Ssam if (ifa->ifa_flags & IFF_BROADCAST) { 85166956Ssam sin = (struct sockaddr_in *)ifa->ifa_broadaddr; 86166956Ssam if (sin != NULL && sin->sin_addr.s_addr != 0) 87228571Sglebius printf("broadcast %s ", inet_ntoa(sin->sin_addr)); 88138593Ssam } 89228571Sglebius 90228571Sglebius print_vhid(ifa, " "); 91228571Sglebius 92138593Ssam putchar('\n'); 93138593Ssam} 94138593Ssam 95138593Ssam#define SIN(x) ((struct sockaddr_in *) &(x)) 96138593Ssamstatic struct sockaddr_in *sintab[] = { 97138593Ssam SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr), 98138593Ssam SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr) 99138593Ssam}; 100138593Ssam 101138593Ssamstatic void 102138593Ssamin_getaddr(const char *s, int which) 103138593Ssam{ 104138593Ssam#define MIN(a,b) ((a)<(b)?(a):(b)) 105138593Ssam struct sockaddr_in *sin = sintab[which]; 106138593Ssam struct hostent *hp; 107138593Ssam struct netent *np; 108138593Ssam 109138593Ssam sin->sin_len = sizeof(*sin); 110227738Sglebius sin->sin_family = AF_INET; 111138593Ssam 112138593Ssam if (which == ADDR) { 113138593Ssam char *p = NULL; 114138593Ssam 115138593Ssam if((p = strrchr(s, '/')) != NULL) { 116202289Semaste const char *errstr; 117138593Ssam /* address is `name/masklen' */ 118138593Ssam int masklen; 119138593Ssam struct sockaddr_in *min = sintab[MASK]; 120138593Ssam *p = '\0'; 121202289Semaste if (!isdigit(*(p + 1))) 122202289Semaste errstr = "invalid"; 123202289Semaste else 124202289Semaste masklen = (int)strtonum(p + 1, 0, 32, &errstr); 125202289Semaste if (errstr != NULL) { 126138593Ssam *p = '/'; 127202289Semaste errx(1, "%s: bad value (width %s)", s, errstr); 128138593Ssam } 129138593Ssam min->sin_len = sizeof(*min); 130138593Ssam min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) & 131138593Ssam 0xffffffff); 132138593Ssam } 133138593Ssam } 134138593Ssam 135138593Ssam if (inet_aton(s, &sin->sin_addr)) 136138593Ssam return; 137138593Ssam if ((hp = gethostbyname(s)) != 0) 138138593Ssam bcopy(hp->h_addr, (char *)&sin->sin_addr, 139194799Sdelphij MIN((size_t)hp->h_length, sizeof(sin->sin_addr))); 140138593Ssam else if ((np = getnetbyname(s)) != 0) 141138593Ssam sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 142138593Ssam else 143138593Ssam errx(1, "%s: bad value", s); 144138593Ssam#undef MIN 145138593Ssam} 146138593Ssam 147138593Ssamstatic void 148138593Ssamin_status_tunnel(int s) 149138593Ssam{ 150138593Ssam char src[NI_MAXHOST]; 151138593Ssam char dst[NI_MAXHOST]; 152138593Ssam struct ifreq ifr; 153138593Ssam const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr; 154138593Ssam 155138593Ssam memset(&ifr, 0, sizeof(ifr)); 156138593Ssam strncpy(ifr.ifr_name, name, IFNAMSIZ); 157138593Ssam 158138593Ssam if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0) 159138593Ssam return; 160147437Sume if (sa->sa_family != AF_INET) 161147437Sume return; 162138593Ssam if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0) 163138593Ssam src[0] = '\0'; 164138593Ssam 165138593Ssam if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0) 166138593Ssam return; 167147437Sume if (sa->sa_family != AF_INET) 168147437Sume return; 169138593Ssam if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0) 170138593Ssam dst[0] = '\0'; 171138593Ssam 172138593Ssam printf("\ttunnel inet %s --> %s\n", src, dst); 173138593Ssam} 174138593Ssam 175138593Ssamstatic void 176138593Ssamin_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres) 177138593Ssam{ 178191121Sbrooks struct in_aliasreq addreq; 179138593Ssam 180138593Ssam memset(&addreq, 0, sizeof(addreq)); 181138593Ssam strncpy(addreq.ifra_name, name, IFNAMSIZ); 182138593Ssam memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len); 183138593Ssam memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len); 184138593Ssam 185138593Ssam if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0) 186138593Ssam warn("SIOCSIFPHYADDR"); 187138593Ssam} 188138593Ssam 189138593Ssamstatic struct afswtch af_inet = { 190138593Ssam .af_name = "inet", 191138593Ssam .af_af = AF_INET, 192138593Ssam .af_status = in_status, 193138593Ssam .af_getaddr = in_getaddr, 194138593Ssam .af_status_tunnel = in_status_tunnel, 195138593Ssam .af_settunnel = in_set_tunnel, 196138593Ssam .af_difaddr = SIOCDIFADDR, 197138593Ssam .af_aifaddr = SIOCAIFADDR, 198138593Ssam .af_ridreq = &in_ridreq, 199138593Ssam .af_addreq = &in_addreq, 200138593Ssam}; 201138593Ssam 202138593Ssamstatic __constructor void 203138593Ssaminet_ctor(void) 204138593Ssam{ 205224179Sbz 206224179Sbz#ifndef RESCUE 207222527Sbz if (!feature_present("inet")) 208222527Sbz return; 209224179Sbz#endif 210138593Ssam af_register(&af_inet); 211138593Ssam} 212