1 /* $OpenBSD: inet.c,v 1.13 2000/04/26 21:25:53 jakob 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 #ifndef lint 37 static const char rcsid[] = 38 "@(#) $Header: /home/cvs/src/lib/libpcap/inet.c,v 1.13 2000/04/26 21:25:53 jakob Exp $ (LBL)"; 39 #endif 40 41 #include <sys/param.h> 42 #include <sys/file.h> 43 #include <sys/ioctl.h> 44 #include <sys/socket.h> 45 #ifdef HAVE_SYS_SOCKIO_H 46 #include <sys/sockio.h> 47 #endif 48 #include <sys/time.h> /* concession to AIX */ 49 50 #ifdef __STDC__ 51 struct mbuf; 52 struct rtentry; 53 #endif 54 55 #include <net/if.h> 56 #include <netinet/in.h> 57 58 #include <ctype.h> 59 #include <errno.h> 60 #include <memory.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <unistd.h> 65 #ifdef HAVE_IFADDRS_H 66 #include <ifaddrs.h> 67 #endif 68 69 #include "pcap-int.h" 70 71 #ifdef HAVE_OS_PROTO_H 72 #include "os-proto.h" 73 #endif 74 75 /* Not all systems have IFF_LOOPBACK */ 76 #ifdef IFF_LOOPBACK 77 #define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK) 78 #else 79 #define ISLOOPBACK(p) ((p)->ifr_name[0] == 'l' && (p)->ifr_name[1] == 'o' && \ 80 (isdigit((p)->ifr_name[2]) || (p)->ifr_name[2] == '\0')) 81 #endif 82 83 /* 84 * Return the name of a network interface attached to the system, or NULL 85 * if none can be found. The interface must be configured up; the 86 * lowest unit number is preferred; loopback is ignored. 87 */ 88 char * 89 pcap_lookupdev(errbuf) 90 register char *errbuf; 91 { 92 #ifdef HAVE_IFADDRS_H 93 struct ifaddrs *ifap, *ifa, *mp; 94 int n, minunit; 95 char *cp; 96 static char device[IF_NAMESIZE + 1]; 97 98 if (getifaddrs(&ifap) != 0) { 99 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 100 "getifaddrs: %s", pcap_strerror(errno)); 101 return NULL; 102 } 103 104 mp = NULL; 105 minunit = 666; 106 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 107 if ((ifa->ifa_flags & IFF_UP) == 0) 108 continue; 109 #ifdef IFF_LOOPBACK 110 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) 111 continue; 112 #else 113 if (strncmp(ifa->ifa_name, "lo", 2) == 0 && 114 (ifa->ifa_name[2] == '\0' || isdigit(ifa->ifa_name[2]))) { 115 continue; 116 } 117 #endif 118 119 for (cp = ifa->ifa_name; !isdigit(*cp); ++cp) 120 continue; 121 n = atoi(cp); 122 if (n < minunit) { 123 minunit = n; 124 mp = ifa; 125 } 126 } 127 if (mp == NULL) { 128 (void)strncpy(errbuf, "no suitable device found", 129 PCAP_ERRBUF_SIZE); 130 freeifaddrs(ifap); 131 return (NULL); 132 } 133 134 (void)strncpy(device, mp->ifa_name, sizeof(device) - 1); 135 device[sizeof(device) - 1] = '\0'; 136 freeifaddrs(ifap); 137 return (device); 138 #else 139 register int fd, minunit, n; 140 register char *cp; 141 register struct ifreq *ifrp, *ifend, *ifnext, *mp; 142 struct ifconf ifc; 143 struct ifreq ibuf[16], ifr; 144 static char device[sizeof(ifrp->ifr_name) + 1]; 145 146 fd = socket(AF_INET, SOCK_DGRAM, 0); 147 if (fd < 0) { 148 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s", 149 pcap_strerror(errno)); 150 return (NULL); 151 } 152 ifc.ifc_len = sizeof ibuf; 153 ifc.ifc_buf = (caddr_t)ibuf; 154 155 memset((char *)ibuf, 0, sizeof(ibuf)); 156 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 || 157 ifc.ifc_len < sizeof(struct ifreq)) { 158 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFCONF: %s", 159 pcap_strerror(errno)); 160 (void)close(fd); 161 return (NULL); 162 } 163 ifrp = ibuf; 164 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len); 165 166 mp = NULL; 167 minunit = 666; 168 for (; ifrp < ifend; ifrp = ifnext) { 169 #ifdef HAVE_SOCKADDR_SA_LEN 170 n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name); 171 if (n < sizeof(*ifrp)) 172 ifnext = ifrp + 1; 173 else 174 ifnext = (struct ifreq *)((char *)ifrp + n); 175 if (ifrp->ifr_addr.sa_family != AF_INET) 176 continue; 177 #else 178 ifnext = ifrp + 1; 179 #endif 180 /* 181 * Need a template to preserve address info that is 182 * used below to locate the next entry. (Otherwise, 183 * SIOCGIFFLAGS stomps over it because the requests 184 * are returned in a union.) 185 */ 186 strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name)); 187 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) { 188 if (errno == ENXIO) 189 continue; 190 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 191 "SIOCGIFFLAGS: %.*s: %s", 192 (int)sizeof(ifr.ifr_name), ifr.ifr_name, 193 pcap_strerror(errno)); 194 (void)close(fd); 195 return (NULL); 196 } 197 198 /* Must be up and not the loopback */ 199 if ((ifr.ifr_flags & IFF_UP) == 0 || ISLOOPBACK(&ifr)) 200 continue; 201 202 for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp) 203 continue; 204 n = atoi(cp); 205 if (n < minunit) { 206 minunit = n; 207 mp = ifrp; 208 } 209 } 210 (void)close(fd); 211 if (mp == NULL) { 212 (void)strlcpy(errbuf, "no suitable device found", 213 PCAP_ERRBUF_SIZE); 214 return (NULL); 215 } 216 217 (void)strncpy(device, mp->ifr_name, sizeof(device) - 1); 218 device[sizeof(device) - 1] = '\0'; 219 return (device); 220 #endif 221 } 222 223 int 224 pcap_lookupnet(device, netp, maskp, errbuf) 225 register char *device; 226 register bpf_u_int32 *netp, *maskp; 227 register char *errbuf; 228 { 229 register int fd; 230 register struct sockaddr_in *sin; 231 struct ifreq ifr; 232 233 fd = socket(AF_INET, SOCK_DGRAM, 0); 234 if (fd < 0) { 235 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s", 236 pcap_strerror(errno)); 237 return (-1); 238 } 239 memset(&ifr, 0, sizeof(ifr)); 240 #ifdef linux 241 /* XXX Work around Linux kernel bug */ 242 ifr.ifr_addr.sa_family = AF_INET; 243 #endif 244 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 245 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) { 246 if (errno == EADDRNOTAVAIL) { 247 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 248 "%s: no IPv4 address assigned", device); 249 } else { 250 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 251 "SIOCGIFADDR: %s: %s", 252 device, pcap_strerror(errno)); 253 } 254 (void)close(fd); 255 return (-1); 256 } 257 sin = (struct sockaddr_in *)&ifr.ifr_addr; 258 *netp = sin->sin_addr.s_addr; 259 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) { 260 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 261 "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno)); 262 (void)close(fd); 263 return (-1); 264 } 265 (void)close(fd); 266 *maskp = sin->sin_addr.s_addr; 267 if (*maskp == 0) { 268 if (IN_CLASSA(*netp)) 269 *maskp = IN_CLASSA_NET; 270 else if (IN_CLASSB(*netp)) 271 *maskp = IN_CLASSB_NET; 272 else if (IN_CLASSC(*netp)) 273 *maskp = IN_CLASSC_NET; 274 else { 275 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 276 "inet class for 0x%x unknown", *netp); 277 return (-1); 278 } 279 } 280 *netp &= *maskp; 281 return (0); 282 } 283