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