1 /* $OpenBSD: inet.c,v 1.21 2015/10/24 05:26:00 mmcc Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995, 1996, 1997, 1998 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the Computer Systems 18 * Engineering Group at Lawrence Berkeley Laboratory. 19 * 4. Neither the name of the University nor of the Laboratory may be used 20 * to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 37 #include <sys/file.h> 38 #include <sys/ioctl.h> 39 #include <sys/socket.h> 40 #ifdef HAVE_SYS_SOCKIO_H 41 #include <sys/sockio.h> 42 #endif 43 #include <sys/time.h> /* concession to AIX */ 44 45 struct mbuf; 46 struct rtentry; 47 48 #include <net/if.h> 49 #include <netinet/in.h> 50 51 #include <ctype.h> 52 #include <errno.h> 53 #include <memory.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <unistd.h> 58 #ifdef HAVE_IFADDRS_H 59 #include <ifaddrs.h> 60 #endif 61 62 #include "pcap-int.h" 63 64 #ifdef HAVE_OS_PROTO_H 65 #include "os-proto.h" 66 #endif 67 68 /* 69 * Free a list of interfaces. 70 */ 71 void 72 pcap_freealldevs(pcap_if_t *alldevs) 73 { 74 pcap_if_t *curdev, *nextdev; 75 pcap_addr_t *curaddr, *nextaddr; 76 77 for (curdev = alldevs; curdev != NULL; curdev = nextdev) { 78 nextdev = curdev->next; 79 80 /* 81 * Free all addresses. 82 */ 83 for (curaddr = curdev->addresses; curaddr != NULL; 84 curaddr = nextaddr) { 85 nextaddr = curaddr->next; 86 if (curaddr->addr) 87 free(curaddr->addr); 88 if (curaddr->netmask) 89 free(curaddr->netmask); 90 if (curaddr->broadaddr) 91 free(curaddr->broadaddr); 92 if (curaddr->dstaddr) 93 free(curaddr->dstaddr); 94 free(curaddr); 95 } 96 97 /* 98 * Free the name string. 99 */ 100 free(curdev->name); 101 102 /* 103 * Free the description string, if any. 104 */ 105 if (curdev->description != NULL) 106 free(curdev->description); 107 108 /* 109 * Free the interface. 110 */ 111 free(curdev); 112 } 113 } 114 115 /* 116 * Return the name of a network interface attached to the system, or NULL 117 * if none can be found. The interface must be configured up; the 118 * lowest unit number is preferred; loopback is ignored. 119 */ 120 char * 121 pcap_lookupdev(errbuf) 122 register char *errbuf; 123 { 124 #ifdef HAVE_IFADDRS_H 125 struct ifaddrs *ifap, *ifa, *mp; 126 int n, minunit; 127 char *cp; 128 static char device[IF_NAMESIZE + 1]; 129 130 if (getifaddrs(&ifap) != 0) { 131 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 132 "getifaddrs: %s", pcap_strerror(errno)); 133 return NULL; 134 } 135 136 mp = NULL; 137 minunit = 666; 138 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 139 if ((ifa->ifa_flags & IFF_UP) == 0) 140 continue; 141 if (ISLOOPBACK(ifa->ifa_name, ifa->ifa_flags)) 142 continue; 143 for (cp = ifa->ifa_name; !isdigit((unsigned char)*cp); ++cp) 144 continue; 145 n = atoi(cp); 146 if (n < minunit) { 147 minunit = n; 148 mp = ifa; 149 } 150 } 151 if (mp == NULL) { 152 (void)strlcpy(errbuf, "no suitable device found", 153 PCAP_ERRBUF_SIZE); 154 freeifaddrs(ifap); 155 return (NULL); 156 } 157 158 (void)strlcpy(device, mp->ifa_name, sizeof(device)); 159 freeifaddrs(ifap); 160 return (device); 161 #else 162 register int fd, minunit, n; 163 register char *cp; 164 register struct ifreq *ifrp, *ifend, *ifnext, *mp; 165 struct ifconf ifc; 166 struct ifreq ibuf[16], ifr; 167 static char device[sizeof(ifrp->ifr_name) + 1]; 168 169 fd = socket(AF_INET, SOCK_DGRAM, 0); 170 if (fd < 0) { 171 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s", 172 pcap_strerror(errno)); 173 return (NULL); 174 } 175 ifc.ifc_len = sizeof ibuf; 176 ifc.ifc_buf = (caddr_t)ibuf; 177 178 memset((char *)ibuf, 0, sizeof(ibuf)); 179 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 || 180 ifc.ifc_len < sizeof(struct ifreq)) { 181 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFCONF: %s", 182 pcap_strerror(errno)); 183 (void)close(fd); 184 return (NULL); 185 } 186 ifrp = ibuf; 187 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len); 188 189 mp = NULL; 190 minunit = 666; 191 for (; ifrp < ifend; ifrp = ifnext) { 192 #ifdef HAVE_SOCKADDR_SA_LEN 193 n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name); 194 if (n < sizeof(*ifrp)) 195 ifnext = ifrp + 1; 196 else 197 ifnext = (struct ifreq *)((char *)ifrp + n); 198 if (ifrp->ifr_addr.sa_family != AF_INET) 199 continue; 200 #else 201 ifnext = ifrp + 1; 202 #endif 203 /* 204 * Need a template to preserve address info that is 205 * used below to locate the next entry. (Otherwise, 206 * SIOCGIFFLAGS stomps over it because the requests 207 * are returned in a union.) 208 */ 209 (void)strlcpy(ifr.ifr_name, ifrp->ifr_name, 210 sizeof(ifr.ifr_name)); 211 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) { 212 if (errno == ENXIO) 213 continue; 214 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 215 "SIOCGIFFLAGS: %.*s: %s", 216 (int)sizeof(ifr.ifr_name), ifr.ifr_name, 217 pcap_strerror(errno)); 218 (void)close(fd); 219 return (NULL); 220 } 221 222 /* Must be up and not the loopback */ 223 if ((ifr.ifr_flags & IFF_UP) == 0 || 224 ISLOOPBACK(ifr.ifr_name, ifr.ifr_flags)) 225 continue; 226 227 for (cp = ifrp->ifr_name; !isdigit((unsigned char)*cp); ++cp) 228 continue; 229 n = atoi(cp); 230 if (n < minunit) { 231 minunit = n; 232 mp = ifrp; 233 } 234 } 235 (void)close(fd); 236 if (mp == NULL) { 237 (void)strlcpy(errbuf, "no suitable device found", 238 PCAP_ERRBUF_SIZE); 239 return (NULL); 240 } 241 242 (void)strlcpy(device, mp->ifr_name, sizeof(device)); 243 return (device); 244 #endif 245 } 246 247 int 248 pcap_lookupnet(const char *device, bpf_u_int32 *netp, bpf_u_int32 *maskp, 249 char *errbuf) 250 { 251 int fd; 252 struct sockaddr_in *sin; 253 struct ifreq ifr; 254 255 fd = socket(AF_INET, SOCK_DGRAM, 0); 256 if (fd < 0) { 257 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s", 258 pcap_strerror(errno)); 259 return (-1); 260 } 261 memset(&ifr, 0, sizeof(ifr)); 262 #ifdef linux 263 /* XXX Work around Linux kernel bug */ 264 ifr.ifr_addr.sa_family = AF_INET; 265 #endif 266 (void)strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 267 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) { 268 if (errno == EADDRNOTAVAIL) { 269 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 270 "%s: no IPv4 address assigned", device); 271 } else { 272 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 273 "SIOCGIFADDR: %s: %s", 274 device, pcap_strerror(errno)); 275 } 276 (void)close(fd); 277 return (-1); 278 } 279 sin = (struct sockaddr_in *)&ifr.ifr_addr; 280 *netp = sin->sin_addr.s_addr; 281 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) { 282 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 283 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno)); 284 (void)close(fd); 285 return (-1); 286 } 287 (void)close(fd); 288 *maskp = sin->sin_addr.s_addr; 289 if (*maskp == 0) { 290 if (IN_CLASSA(*netp)) 291 *maskp = IN_CLASSA_NET; 292 else if (IN_CLASSB(*netp)) 293 *maskp = IN_CLASSB_NET; 294 else if (IN_CLASSC(*netp)) 295 *maskp = IN_CLASSC_NET; 296 else { 297 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 298 "inet class for 0x%x unknown", *netp); 299 return (-1); 300 } 301 } 302 *netp &= *maskp; 303 return (0); 304 } 305