xref: /openbsd-src/lib/libpcap/inet.c (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
1 /*	$OpenBSD: inet.c,v 1.27 2024/04/05 18:01:56 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(char *errbuf)
113 {
114 #ifdef HAVE_IFADDRS_H
115 	struct ifaddrs *ifap, *ifa, *mp;
116 	int n, minunit;
117 	char *cp;
118 	static char device[IF_NAMESIZE + 1];
119 
120 	if (getifaddrs(&ifap) != 0) {
121 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
122 		    "getifaddrs: %s", pcap_strerror(errno));
123 		return NULL;
124 	}
125 
126 	mp = NULL;
127 	minunit = 666;
128 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
129 		if ((ifa->ifa_flags & IFF_UP) == 0)
130 			continue;
131 		if (ISLOOPBACK(ifa->ifa_name, ifa->ifa_flags))
132 			continue;
133 		for (cp = ifa->ifa_name; !isdigit((unsigned char)*cp); ++cp)
134 			continue;
135 		n = atoi(cp);
136 		if (n < minunit) {
137 			minunit = n;
138 			mp = ifa;
139 		}
140 	}
141 	if (mp == NULL) {
142 		(void)strlcpy(errbuf, "no suitable device found",
143 		    PCAP_ERRBUF_SIZE);
144 		freeifaddrs(ifap);
145 		return (NULL);
146 	}
147 
148 	(void)strlcpy(device, mp->ifa_name, sizeof(device));
149 	freeifaddrs(ifap);
150 	return (device);
151 #else
152 	int fd, minunit, n;
153 	char *cp;
154 	struct ifreq *ifrp, *ifend, *ifnext, *mp;
155 	struct ifconf ifc;
156 	struct ifreq ibuf[16], ifr;
157 	static char device[sizeof(ifrp->ifr_name) + 1];
158 
159 	fd = socket(AF_INET, SOCK_DGRAM, 0);
160 	if (fd == -1) {
161 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
162 		    pcap_strerror(errno));
163 		return (NULL);
164 	}
165 	ifc.ifc_len = sizeof ibuf;
166 	ifc.ifc_buf = (caddr_t)ibuf;
167 
168 	memset((char *)ibuf, 0, sizeof(ibuf));
169 	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) == -1 ||
170 	    ifc.ifc_len < sizeof(struct ifreq)) {
171 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFCONF: %s",
172 		    pcap_strerror(errno));
173 		(void)close(fd);
174 		return (NULL);
175 	}
176 	ifrp = ibuf;
177 	ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
178 
179 	mp = NULL;
180 	minunit = 666;
181 	for (; ifrp < ifend; ifrp = ifnext) {
182 #ifdef HAVE_SOCKADDR_SA_LEN
183 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
184 		if (n < sizeof(*ifrp))
185 			ifnext = ifrp + 1;
186 		else
187 			ifnext = (struct ifreq *)((char *)ifrp + n);
188 		if (ifrp->ifr_addr.sa_family != AF_INET)
189 			continue;
190 #else
191 		ifnext = ifrp + 1;
192 #endif
193 		/*
194 		 * Need a template to preserve address info that is
195 		 * used below to locate the next entry.  (Otherwise,
196 		 * SIOCGIFFLAGS stomps over it because the requests
197 		 * are returned in a union.)
198 		 */
199 		(void)strlcpy(ifr.ifr_name, ifrp->ifr_name,
200 		    sizeof(ifr.ifr_name));
201 		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) == -1) {
202 			if (errno == ENXIO)
203 				continue;
204 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
205 			    "SIOCGIFFLAGS: %.*s: %s",
206 			    (int)sizeof(ifr.ifr_name), ifr.ifr_name,
207 			    pcap_strerror(errno));
208 			(void)close(fd);
209 			return (NULL);
210 		}
211 
212 		/* Must be up and not the loopback */
213 		if ((ifr.ifr_flags & IFF_UP) == 0 ||
214 		    ISLOOPBACK(ifr.ifr_name, ifr.ifr_flags))
215 			continue;
216 
217 		for (cp = ifrp->ifr_name; !isdigit((unsigned char)*cp); ++cp)
218 			continue;
219 		n = atoi(cp);
220 		if (n < minunit) {
221 			minunit = n;
222 			mp = ifrp;
223 		}
224 	}
225 	(void)close(fd);
226 	if (mp == NULL) {
227 		(void)strlcpy(errbuf, "no suitable device found",
228 		    PCAP_ERRBUF_SIZE);
229 		return (NULL);
230 	}
231 
232 	(void)strlcpy(device, mp->ifr_name, sizeof(device));
233 	return (device);
234 #endif
235 }
236 
237 int
238 pcap_lookupnet(const char *device, bpf_u_int32 *netp, bpf_u_int32 *maskp,
239     char *errbuf)
240 {
241 	int fd;
242 	struct sockaddr_in *sin;
243 	struct ifreq ifr;
244 
245 	fd = socket(AF_INET, SOCK_DGRAM, 0);
246 	if (fd == -1) {
247 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s",
248 		    pcap_strerror(errno));
249 		return (-1);
250 	}
251 	memset(&ifr, 0, sizeof(ifr));
252 #ifdef linux
253 	/* XXX Work around Linux kernel bug */
254 	ifr.ifr_addr.sa_family = AF_INET;
255 #endif
256 	(void)strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
257 	if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) == -1) {
258 		if (errno == EADDRNOTAVAIL) {
259 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
260 			    "%s: no IPv4 address assigned", device);
261 		} else {
262 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
263 			    "SIOCGIFADDR: %s: %s",
264 			    device, pcap_strerror(errno));
265 		}
266 		(void)close(fd);
267 		return (-1);
268 	}
269 	sin = (struct sockaddr_in *)&ifr.ifr_addr;
270 	*netp = sin->sin_addr.s_addr;
271 	if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) == -1) {
272 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
273 		    "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno));
274 		(void)close(fd);
275 		return (-1);
276 	}
277 	(void)close(fd);
278 	*maskp = sin->sin_addr.s_addr;
279 	if (*maskp == 0) {
280 		if (IN_CLASSA(*netp))
281 			*maskp = IN_CLASSA_NET;
282 		else if (IN_CLASSB(*netp))
283 			*maskp = IN_CLASSB_NET;
284 		else if (IN_CLASSC(*netp))
285 			*maskp = IN_CLASSC_NET;
286 		else {
287 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
288 			    "inet class for 0x%x unknown", *netp);
289 			return (-1);
290 		}
291 	}
292 	*netp &= *maskp;
293 	return (0);
294 }
295