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