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