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