1 /* $OpenBSD: inet.c,v 1.10 1998/08/27 22:36:33 mickey 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, *nibuf; 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 nibuf = realloc(ibuf, len); 105 if (nibuf == NULL) { 106 if (ibuf) 107 free(ibuf); 108 close(fd); 109 return (NULL); 110 } 111 ifc.ifc_buf = ibuf = nibuf; 112 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0) { 113 (void)close(fd); 114 free(ibuf); 115 return (NULL); 116 } 117 if (ifc.ifc_len + sizeof(ifr) < len) 118 break; 119 len *= 2; 120 } 121 122 ifrp = (struct ifreq *)ibuf; 123 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len); 124 125 mp = NULL; 126 minunit = 666; 127 for (; ifrp < ifend; ifrp = ifnext) { 128 #if BSD - 0 >= 199006 129 n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name); 130 if (n < sizeof(*ifrp)) 131 ifnext = ifrp + 1; 132 else 133 ifnext = (struct ifreq *)((char *)ifrp + n); 134 if (ifrp->ifr_addr.sa_family != AF_INET) 135 continue; 136 #else 137 ifnext = ifrp + 1; 138 #endif 139 /* 140 * Need a template to preserve address info that is 141 * used below to locate the next entry. (Otherwise, 142 * SIOCGIFFLAGS stomps over it because the requests 143 * are returned in a union.) 144 */ 145 strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name)); 146 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) { 147 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 148 "SIOCGIFFLAGS: %s", pcap_strerror(errno)); 149 (void)close(fd); 150 free(ibuf); 151 return (NULL); 152 } 153 154 /* Must be up and not the loopback */ 155 if ((ifr.ifr_flags & IFF_UP) == 0 || ISLOOPBACK(&ifr)) 156 continue; 157 158 for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp) 159 continue; 160 n = atoi(cp); 161 if (n < minunit) { 162 minunit = n; 163 mp = ifrp; 164 } 165 } 166 (void)close(fd); 167 if (mp == NULL) { 168 (void)strcpy(errbuf, "no suitable device found"); 169 free(ibuf); 170 return (NULL); 171 } 172 173 (void)strncpy(device, mp->ifr_name, sizeof(device) - 1); 174 device[sizeof(device) - 1] = '\0'; 175 free(ibuf); 176 return (device); 177 } 178 179 int 180 pcap_lookupnet(device, netp, maskp, errbuf) 181 register char *device; 182 register bpf_u_int32 *netp, *maskp; 183 register char *errbuf; 184 { 185 register int fd; 186 register struct sockaddr_in *sin; 187 struct ifreq ifr; 188 189 fd = socket(AF_INET, SOCK_DGRAM, 0); 190 if (fd < 0) { 191 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s", 192 pcap_strerror(errno)); 193 return (-1); 194 } 195 memset(&ifr, 0, sizeof(ifr)); 196 #ifdef linux 197 /* XXX Work around Linux kernel bug */ 198 ifr.ifr_addr.sa_family = AF_INET; 199 #endif 200 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 201 if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) { 202 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFADDR: %s: %s", 203 device, pcap_strerror(errno)); 204 (void)close(fd); 205 return (-1); 206 } 207 sin = (struct sockaddr_in *)&ifr.ifr_addr; 208 *netp = sin->sin_addr.s_addr; 209 if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) { 210 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFNETMASK: %s: %s", 211 device, pcap_strerror(errno)); 212 (void)close(fd); 213 return (-1); 214 } 215 (void)close(fd); 216 *maskp = sin->sin_addr.s_addr; 217 if (*maskp == 0) { 218 if (IN_CLASSA(*netp)) 219 *maskp = IN_CLASSA_NET; 220 else if (IN_CLASSB(*netp)) 221 *maskp = IN_CLASSB_NET; 222 else if (IN_CLASSC(*netp)) 223 *maskp = IN_CLASSC_NET; 224 else { 225 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 226 "inet class for 0x%x unknown", *netp); 227 return (-1); 228 } 229 } 230 *netp &= *maskp; 231 return (0); 232 } 233