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 * 3. 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 * $FreeBSD: src/sbin/ifconfig/af_inet.c,v 1.2 2005/06/16 19:37:09 ume Exp $ 30 */ 31 32 #include <sys/types.h> 33 #include <sys/ioctl.h> 34 #include <sys/socket.h> 35 #include <net/if.h> 36 #include <net/if_var.h> /* for struct ifaddr */ 37 #include <net/route.h> /* for RTX_IFA */ 38 #include <netinet/in.h> 39 #include <netinet/in_var.h> 40 #include <arpa/inet.h> 41 #include <netdb.h> 42 43 #include <err.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 #include "ifconfig.h" 50 51 static struct ifaliasreq in_addreq; 52 static struct ifreq in_ridreq; 53 54 static void 55 in_status(int s __unused, const struct rt_addrinfo * info) 56 { 57 struct sockaddr_in *sin, null_sin; 58 59 memset(&null_sin, 0, sizeof(null_sin)); 60 61 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA]; 62 if (sin == NULL) 63 return; 64 65 printf("\tinet %s ", inet_ntoa(sin->sin_addr)); 66 67 if (flags & IFF_POINTOPOINT) { 68 /* note RTAX_BRD overlap with IFF_BROADCAST */ 69 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD]; 70 if (!sin) 71 sin = &null_sin; 72 printf("--> %s ", inet_ntoa(sin->sin_addr)); 73 } 74 75 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK]; 76 if (!sin) 77 sin = &null_sin; 78 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr)); 79 80 if (flags & IFF_BROADCAST) { 81 /* note RTAX_BRD overlap with IFF_POINTOPOINT */ 82 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD]; 83 if (sin && sin->sin_addr.s_addr != 0) 84 printf("broadcast %s", inet_ntoa(sin->sin_addr)); 85 } 86 putchar('\n'); 87 } 88 89 #define SIN(x) ((struct sockaddr_in *) &(x)) 90 static struct sockaddr_in *sintab[] = { 91 SIN(in_ridreq.ifr_addr), SIN(in_addreq.ifra_addr), 92 SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr) 93 }; 94 95 static void 96 in_getaddr(const char *s, int which) 97 { 98 struct sockaddr_in *sin = sintab[which]; 99 struct hostent *hp; 100 struct netent *np; 101 102 sin->sin_len = sizeof(*sin); 103 if (which != MASK) 104 sin->sin_family = AF_INET; 105 106 if (which == ADDR) { 107 char *p = NULL; 108 109 if((p = strrchr(s, '/')) != NULL) { 110 /* address is `name/masklen' */ 111 int masklen; 112 int ret; 113 struct sockaddr_in *min = sintab[MASK]; 114 *p = '\0'; 115 ret = sscanf(p+1, "%u", &masklen); 116 if(ret != 1 || (masklen < 0 || masklen > 32)) { 117 *p = '/'; 118 errx(1, "%s: bad value", s); 119 } 120 min->sin_len = sizeof(*min); 121 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) & 122 0xffffffff); 123 } 124 } 125 126 if (inet_aton(s, &sin->sin_addr)) 127 return; 128 if ((hp = gethostbyname(s)) != NULL) 129 bcopy(hp->h_addr, (char *)&sin->sin_addr, 130 MIN((size_t)hp->h_length, sizeof(sin->sin_addr))); 131 else if ((np = getnetbyname(s)) != NULL) 132 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 133 else 134 errx(1, "%s: bad value", s); 135 } 136 137 static void 138 in_status_tunnel(int s) 139 { 140 char src[NI_MAXHOST]; 141 char dst[NI_MAXHOST]; 142 struct ifreq ifr; 143 const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr; 144 145 memset(&ifr, 0, sizeof(ifr)); 146 strlcpy(ifr.ifr_name, name, IFNAMSIZ); 147 148 if (ioctl(s, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0) 149 return; 150 if (sa->sa_family != AF_INET) 151 return; 152 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, NI_NUMERICHOST) != 0) 153 src[0] = '\0'; 154 155 if (ioctl(s, SIOCGIFPDSTADDR, (caddr_t)&ifr) < 0) 156 return; 157 if (sa->sa_family != AF_INET) 158 return; 159 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, NI_NUMERICHOST) != 0) 160 dst[0] = '\0'; 161 162 printf("\ttunnel inet %s --> %s\n", src, dst); 163 } 164 165 static void 166 in_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres) 167 { 168 struct ifaliasreq addreq; 169 170 memset(&addreq, 0, sizeof(addreq)); 171 strlcpy(addreq.ifra_name, name, IFNAMSIZ); 172 memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len); 173 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len); 174 175 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0) 176 warn("SIOCSIFPHYADDR"); 177 } 178 179 static struct afswtch af_inet = { 180 .af_name = "inet", 181 .af_af = AF_INET, 182 .af_status = in_status, 183 .af_getaddr = in_getaddr, 184 .af_status_tunnel = in_status_tunnel, 185 .af_settunnel = in_set_tunnel, 186 .af_difaddr = SIOCDIFADDR, 187 .af_aifaddr = SIOCAIFADDR, 188 .af_ridreq = &in_ridreq, 189 .af_addreq = &in_addreq, 190 }; 191 192 static __constructor(101) void 193 inet_ctor(void) 194 { 195 af_register(&af_inet); 196 } 197