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