af_inet.c revision 191121
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1983, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 301590Srgrimes#ifndef lint 3128199Scharnierstatic const char rcsid[] = 321590Srgrimes "$FreeBSD: head/sbin/ifconfig/af_inet.c 191121 2009-04-15 20:53:34Z brooks $"; 331590Srgrimes#endif /* not lint */ 341590Srgrimes 351590Srgrimes#include <sys/types.h> 361590Srgrimes#include <sys/ioctl.h> 3728199Scharnier#include <sys/socket.h> 381590Srgrimes#include <net/if.h> 3928199Scharnier 4028199Scharnier#include <err.h> 4150477Speter#include <stdio.h> 421590Srgrimes#include <stdlib.h> 431590Srgrimes#include <string.h> 441590Srgrimes#include <unistd.h> 451590Srgrimes#include <ifaddrs.h> 4628199Scharnier 4728199Scharnier#include <netinet/in.h> 481590Srgrimes#include <net/if_var.h> /* for struct ifaddr */ 491590Srgrimes#include <netinet/in_var.h> 501590Srgrimes#include <arpa/inet.h> 51200462Sdelphij#include <netdb.h> 5228199Scharnier 531590Srgrimes#include "ifconfig.h" 541590Srgrimes 551590Srgrimesstatic struct in_aliasreq in_addreq; 561590Srgrimesstatic struct ifreq in_ridreq; 5787303Sdwmalone 581590Srgrimesstatic void 591590Srgrimesin_status(int s __unused, const struct ifaddrs *ifa) 601590Srgrimes{ 6192922Simp struct sockaddr_in *sin, null_sin; 6292922Simp 631590Srgrimes memset(&null_sin, 0, sizeof(null_sin)); 641590Srgrimes 65102944Sdwmalone sin = (struct sockaddr_in *)ifa->ifa_addr; 661590Srgrimes if (sin == NULL) 67102944Sdwmalone return; 68102944Sdwmalone 69102944Sdwmalone printf("\tinet %s ", inet_ntoa(sin->sin_addr)); 701590Srgrimes 711590Srgrimes if (ifa->ifa_flags & IFF_POINTOPOINT) { 721590Srgrimes sin = (struct sockaddr_in *)ifa->ifa_dstaddr; 731590Srgrimes if (sin == NULL) 741590Srgrimes sin = &null_sin; 7524360Simp printf("--> %s ", inet_ntoa(sin->sin_addr)); 761590Srgrimes } 771590Srgrimes 781590Srgrimes sin = (struct sockaddr_in *)ifa->ifa_netmask; 791590Srgrimes if (sin == NULL) 801590Srgrimes sin = &null_sin; 811590Srgrimes printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr)); 821590Srgrimes 831590Srgrimes if (ifa->ifa_flags & IFF_BROADCAST) { 841590Srgrimes sin = (struct sockaddr_in *)ifa->ifa_broadaddr; 8528199Scharnier if (sin != NULL && sin->sin_addr.s_addr != 0) 861590Srgrimes printf("broadcast %s", inet_ntoa(sin->sin_addr)); 871590Srgrimes } 881590Srgrimes putchar('\n'); 891590Srgrimes} 9096776Sjmallett 9199433Stjr#define SIN(x) ((struct sockaddr_in *) &(x)) 921590Srgrimesstatic struct sockaddr_in *sintab[] = { 931590Srgrimes SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr), 941590Srgrimes SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr) 951590Srgrimes}; 961590Srgrimes 971590Srgrimesstatic void 9828199Scharnierin_getaddr(const char *s, int which) 991590Srgrimes{ 1001590Srgrimes#define MIN(a,b) ((a)<(b)?(a):(b)) 1011590Srgrimes struct sockaddr_in *sin = sintab[which]; 1021590Srgrimes struct hostent *hp; 1031590Srgrimes struct netent *np; 1041590Srgrimes 1051590Srgrimes sin->sin_len = sizeof(*sin); 1061590Srgrimes if (which != MASK) 1071590Srgrimes sin->sin_family = AF_INET; 1081590Srgrimes 10928199Scharnier if (which == ADDR) { 1101590Srgrimes char *p = NULL; 1111590Srgrimes 1121590Srgrimes if((p = strrchr(s, '/')) != NULL) { 1131590Srgrimes /* address is `name/masklen' */ 1141590Srgrimes int masklen; 1151590Srgrimes int ret; 1161590Srgrimes struct sockaddr_in *min = sintab[MASK]; 11728199Scharnier *p = '\0'; 11896803Sjmallett ret = sscanf(p+1, "%u", &masklen); 1191590Srgrimes if(ret != 1 || (masklen < 0 || masklen > 32)) { 1201590Srgrimes *p = '/'; 12128199Scharnier errx(1, "%s: bad value", s); 122102944Sdwmalone } 12328199Scharnier min->sin_len = sizeof(*min); 12428199Scharnier min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) & 12528199Scharnier 0xffffffff); 12628199Scharnier } 12728199Scharnier } 1281590Srgrimes 129102944Sdwmalone if (inet_aton(s, &sin->sin_addr)) 1301590Srgrimes return; 1311590Srgrimes if ((hp = gethostbyname(s)) != 0) 1321590Srgrimes bcopy(hp->h_addr, (char *)&sin->sin_addr, 13396776Sjmallett MIN(hp->h_length, sizeof(sin->sin_addr))); 13499433Stjr else if ((np = getnetbyname(s)) != 0) 1351590Srgrimes sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 1361590Srgrimes else 1371590Srgrimes errx(1, "%s: bad value", s); 1381590Srgrimes#undef MIN 1391590Srgrimes} 140 141static void 142in_status_tunnel(int s) 143{ 144 char src[NI_MAXHOST]; 145 char dst[NI_MAXHOST]; 146 struct ifreq ifr; 147 const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr; 148 149 memset(&ifr, 0, sizeof(ifr)); 150 strncpy(ifr.ifr_name, name, IFNAMSIZ); 151 152 if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0) 153 return; 154 if (sa->sa_family != AF_INET) 155 return; 156 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0) 157 src[0] = '\0'; 158 159 if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0) 160 return; 161 if (sa->sa_family != AF_INET) 162 return; 163 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0) 164 dst[0] = '\0'; 165 166 printf("\ttunnel inet %s --> %s\n", src, dst); 167} 168 169static void 170in_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres) 171{ 172 struct in_aliasreq addreq; 173 174 memset(&addreq, 0, sizeof(addreq)); 175 strncpy(addreq.ifra_name, name, IFNAMSIZ); 176 memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len); 177 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len); 178 179 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0) 180 warn("SIOCSIFPHYADDR"); 181} 182 183static struct afswtch af_inet = { 184 .af_name = "inet", 185 .af_af = AF_INET, 186 .af_status = in_status, 187 .af_getaddr = in_getaddr, 188 .af_status_tunnel = in_status_tunnel, 189 .af_settunnel = in_set_tunnel, 190 .af_difaddr = SIOCDIFADDR, 191 .af_aifaddr = SIOCAIFADDR, 192 .af_ridreq = &in_ridreq, 193 .af_addreq = &in_addreq, 194}; 195 196static __constructor void 197inet_ctor(void) 198{ 199 af_register(&af_inet); 200} 201