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