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