1 /* $OpenBSD: inet.c,v 1.11 1999/07/20 04:49:55 deraadt 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.11 1999/07/20 04:49:55 deraadt 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 66 #include "pcap-int.h" 67 68 #ifdef HAVE_OS_PROTO_H 69 #include "os-proto.h" 70 #endif 71 72 /* Not all systems have IFF_LOOPBACK */ 73 #ifdef IFF_LOOPBACK 74 #define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK) 75 #else 76 #define ISLOOPBACK(p) ((p)->ifr_name[0] == 'l' && (p)->ifr_name[1] == 'o' && \ 77 (isdigit((p)->ifr_name[2]) || (p)->ifr_name[2] == '\0')) 78 #endif 79 80 /* 81 * Return the name of a network interface attached to the system, or NULL 82 * if none can be found. The interface must be configured up; the 83 * lowest unit number is preferred; loopback is ignored. 84 */ 85 char * 86 pcap_lookupdev(errbuf) 87 register char *errbuf; 88 { 89 register int fd, minunit, n; 90 register char *cp, *ibuf = NULL, *nibuf; 91 register struct ifreq *ifrp, *ifend, *ifnext, *mp; 92 struct ifconf ifc; 93 struct ifreq ifr; 94 static char device[sizeof(ifrp->ifr_name) + 1]; 95 int len = 8192; 96 97 fd = socket(AF_INET, SOCK_DGRAM, 0); 98 if (fd < 0) { 99 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s", 100 pcap_strerror(errno)); 101 return (NULL); 102 } 103 while (1) { 104 ifc.ifc_len = len; 105 nibuf = realloc(ibuf, len); 106 if (nibuf == NULL) { 107 if (ibuf) 108 free(ibuf); 109 close(fd); 110 return (NULL); 111 } 112 ifc.ifc_buf = ibuf = nibuf; 113 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0) { 114 (void)close(fd); 115 free(ibuf); 116 return (NULL); 117 } 118 if (ifc.ifc_len + sizeof(ifr) < len) 119 break; 120 len *= 2; 121 } 122 123 ifrp = (struct ifreq *)ibuf; 124 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len); 125 126 mp = NULL; 127 minunit = 666; 128 for (; ifrp < ifend; ifrp = ifnext) { 129 #if BSD - 0 >= 199006 130 n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name); 131 if (n < sizeof(*ifrp)) 132 ifnext = ifrp + 1; 133 else 134 ifnext = (struct ifreq *)((char *)ifrp + n); 135 if (ifrp->ifr_addr.sa_family != AF_INET) 136 continue; 137 #else 138 ifnext = ifrp + 1; 139 #endif 140 /* 141 * Need a template to preserve address info that is 142 * used below to locate the next entry. (Otherwise, 143 * SIOCGIFFLAGS stomps over it because the requests 144 * are returned in a union.) 145 */ 146 strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name)); 147 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) { 148 if (errno == ENXIO) 149 continue; 150 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 151 "SIOCGIFFLAGS: %.*s: %s", 152 (int)sizeof(ifr.ifr_name), ifr.ifr_name, 153 pcap_strerror(errno)); 154 (void)close(fd); 155 free(ibuf); 156 return (NULL); 157 } 158 159 /* Must be up and not the loopback */ 160 if ((ifr.ifr_flags & IFF_UP) == 0 || ISLOOPBACK(&ifr)) 161 continue; 162 163 for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp) 164 continue; 165 n = atoi(cp); 166 if (n < minunit) { 167 minunit = n; 168 mp = ifrp; 169 } 170 } 171 (void)close(fd); 172 if (mp == NULL) { 173 (void)strlcpy(errbuf, "no suitable device found", 174 PCAP_ERRBUF_SIZE); 175 free(ibuf); 176 return (NULL); 177 } 178 179 (void)strncpy(device, mp->ifr_name, sizeof(device) - 1); 180 device[sizeof(device) - 1] = '\0'; 181 free(ibuf); 182 return (device); 183 } 184 185 int 186 pcap_lookupnet(device, netp, maskp, errbuf) 187 register char *device; 188 register bpf_u_int32 *netp, *maskp; 189 register char *errbuf; 190 { 191 register int fd; 192 register struct sockaddr_in *sin; 193 struct ifreq ifr; 194 195 fd = socket(AF_INET, SOCK_DGRAM, 0); 196 if (fd < 0) { 197 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s", 198 pcap_strerror(errno)); 199 return (-1); 200 } 201 memset(&ifr, 0, sizeof(ifr)); 202 #ifdef linux 203 /* XXX Work around Linux kernel bug */ 204 ifr.ifr_addr.sa_family = AF_INET; 205 #endif 206 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 207 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) { 208 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFADDR: %s: %s", 209 device, pcap_strerror(errno)); 210 (void)close(fd); 211 return (-1); 212 } 213 sin = (struct sockaddr_in *)&ifr.ifr_addr; 214 *netp = sin->sin_addr.s_addr; 215 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) { 216 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFNETMASK: %s: %s", 217 device, pcap_strerror(errno)); 218 (void)close(fd); 219 return (-1); 220 } 221 (void)close(fd); 222 *maskp = sin->sin_addr.s_addr; 223 if (*maskp == 0) { 224 if (IN_CLASSA(*netp)) 225 *maskp = IN_CLASSA_NET; 226 else if (IN_CLASSB(*netp)) 227 *maskp = IN_CLASSB_NET; 228 else if (IN_CLASSC(*netp)) 229 *maskp = IN_CLASSC_NET; 230 else { 231 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 232 "inet class for 0x%x unknown", *netp); 233 return (-1); 234 } 235 } 236 *netp &= *maskp; 237 return (0); 238 } 239