Home | History | Annotate | Download | only in libpcap
      1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
      2 /*
      3  * Copyright (c) 1994, 1995, 1996, 1997, 1998
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by the Computer Systems
     17  *	Engineering Group at Lawrence Berkeley Laboratory.
     18  * 4. Neither the name of the University nor of the Laboratory may be used
     19  *    to endorse or promote products derived from this software without
     20  *    specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #ifdef HAVE_CONFIG_H
     36 #include "config.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/file.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/socket.h>
     43 #ifdef HAVE_SYS_SOCKIO_H
     44 #include <sys/sockio.h>
     45 #endif
     46 #include <sys/time.h>				/* concession to AIX */
     47 
     48 struct mbuf;		/* Squelch compiler warnings on some platforms for */
     49 struct rtentry;		/* declarations in <net/if.h> */
     50 #include <net/if.h>
     51 #include <netinet/in.h>
     52 
     53 #include <ctype.h>
     54 #include <errno.h>
     55 #include <memory.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 
     61 #include "pcap-int.h"
     62 
     63 #ifdef HAVE_OS_PROTO_H
     64 #include "os-proto.h"
     65 #endif
     66 
     67 /*
     68  * Get a list of all interfaces that are up and that we can open.
     69  * Returns -1 on error, 0 otherwise.
     70  * The list, as returned through "alldevsp", may be null if no interfaces
     71  * were up and could be opened.
     72  *
     73  * This is the implementation used on platforms that have SIOCGLIFCONF
     74  * but don't have "getifaddrs()".  (Solaris 8 and later; we use
     75  * SIOCGLIFCONF rather than SIOCGIFCONF in order to get IPv6 addresses.)
     76  */
     77 int
     78 pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
     79 {
     80 	pcap_if_t *devlist = NULL;
     81 	register int fd4, fd6, fd;
     82 	register struct lifreq *ifrp, *ifend;
     83 	struct lifnum ifn;
     84 	struct lifconf ifc;
     85 	char *buf = NULL;
     86 	unsigned buf_size;
     87 #ifdef HAVE_SOLARIS
     88 	char *p, *q;
     89 #endif
     90 	struct lifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
     91 	struct sockaddr *netmask, *broadaddr, *dstaddr;
     92 	int ret = 0;
     93 
     94 	/*
     95 	 * Create a socket from which to fetch the list of interfaces,
     96 	 * and from which to fetch IPv4 information.
     97 	 */
     98 	fd4 = socket(AF_INET, SOCK_DGRAM, 0);
     99 	if (fd4 < 0) {
    100 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
    101 		    "socket: %s", pcap_strerror(errno));
    102 		return (-1);
    103 	}
    104 
    105 	/*
    106 	 * Create a socket from which to fetch IPv6 information.
    107 	 */
    108 	fd6 = socket(AF_INET6, SOCK_DGRAM, 0);
    109 	if (fd6 < 0) {
    110 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
    111 		    "socket: %s", pcap_strerror(errno));
    112 		(void)close(fd4);
    113 		return (-1);
    114 	}
    115 
    116 	/*
    117 	 * How many entries will SIOCGLIFCONF return?
    118 	 */
    119 	ifn.lifn_family = AF_UNSPEC;
    120 	ifn.lifn_flags = 0;
    121 	ifn.lifn_count = 0;
    122 	if (ioctl(fd4, SIOCGLIFNUM, (char *)&ifn) < 0) {
    123 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
    124 		    "SIOCGLIFNUM: %s", pcap_strerror(errno));
    125 		(void)close(fd6);
    126 		(void)close(fd4);
    127 		return (-1);
    128 	}
    129 
    130 	/*
    131 	 * Allocate a buffer for those entries.
    132 	 */
    133 	buf_size = ifn.lifn_count * sizeof (struct lifreq);
    134 	buf = malloc(buf_size);
    135 	if (buf == NULL) {
    136 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
    137 		    "malloc: %s", pcap_strerror(errno));
    138 		(void)close(fd6);
    139 		(void)close(fd4);
    140 		return (-1);
    141 	}
    142 
    143 	/*
    144 	 * Get the entries.
    145 	 */
    146 	ifc.lifc_len = buf_size;
    147 	ifc.lifc_buf = buf;
    148 	ifc.lifc_family = AF_UNSPEC;
    149 	ifc.lifc_flags = 0;
    150 	memset(buf, 0, buf_size);
    151 	if (ioctl(fd4, SIOCGLIFCONF, (char *)&ifc) < 0) {
    152 		(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
    153 		    "SIOCGLIFCONF: %s", pcap_strerror(errno));
    154 		(void)close(fd6);
    155 		(void)close(fd4);
    156 		free(buf);
    157 		return (-1);
    158 	}
    159 
    160 	/*
    161 	 * Loop over the entries.
    162 	 */
    163 	ifrp = (struct lifreq *)buf;
    164 	ifend = (struct lifreq *)(buf + ifc.lifc_len);
    165 
    166 	for (; ifrp < ifend; ifrp++) {
    167 		/*
    168 		 * IPv6 or not?
    169 		 */
    170 		if (((struct sockaddr *)&ifrp->lifr_addr)->sa_family == AF_INET6)
    171 			fd = fd6;
    172 		else
    173 			fd = fd4;
    174 
    175 		/*
    176 		 * Skip entries that begin with "dummy".
    177 		 * XXX - what are these?  Is this Linux-specific?
    178 		 * Are there platforms on which we shouldn't do this?
    179 		 */
    180 		if (strncmp(ifrp->lifr_name, "dummy", 5) == 0)
    181 			continue;
    182 
    183 #ifdef HAVE_SOLARIS
    184 		/*
    185 		 * Skip entries that have a ":" followed by a number
    186 		 * at the end - those are Solaris virtual interfaces
    187 		 * on which you can't capture.
    188 		 */
    189 		p = strchr(ifrp->lifr_name, ':');
    190 		if (p != NULL) {
    191 			/*
    192 			 * We have a ":"; is it followed by a number?
    193 			 */
    194 			while (isdigit((unsigned char)*p))
    195 				p++;
    196 			if (*p == '\0') {
    197 				/*
    198 				 * All digits after the ":" until the end.
    199 				 */
    200 				continue;
    201 			}
    202 		}
    203 #endif
    204 
    205 		/*
    206 		 * Get the flags for this interface.
    207 		 */
    208 		strncpy(ifrflags.lifr_name, ifrp->lifr_name,
    209 		    sizeof(ifrflags.lifr_name));
    210 		if (ioctl(fd, SIOCGLIFFLAGS, (char *)&ifrflags) < 0) {
    211 			if (errno == ENXIO)
    212 				continue;
    213 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
    214 			    "SIOCGLIFFLAGS: %.*s: %s",
    215 			    (int)sizeof(ifrflags.lifr_name),
    216 			    ifrflags.lifr_name,
    217 			    pcap_strerror(errno));
    218 			ret = -1;
    219 			break;
    220 		}
    221 
    222 		/*
    223 		 * Get the netmask for this address on this interface.
    224 		 */
    225 		strncpy(ifrnetmask.lifr_name, ifrp->lifr_name,
    226 		    sizeof(ifrnetmask.lifr_name));
    227 		memcpy(&ifrnetmask.lifr_addr, &ifrp->lifr_addr,
    228 		    sizeof(ifrnetmask.lifr_addr));
    229 		if (ioctl(fd, SIOCGLIFNETMASK, (char *)&ifrnetmask) < 0) {
    230 			if (errno == EADDRNOTAVAIL) {
    231 				/*
    232 				 * Not available.
    233 				 */
    234 				netmask = NULL;
    235 			} else {
    236 				(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
    237 				    "SIOCGLIFNETMASK: %.*s: %s",
    238 				    (int)sizeof(ifrnetmask.lifr_name),
    239 				    ifrnetmask.lifr_name,
    240 				    pcap_strerror(errno));
    241 				ret = -1;
    242 				break;
    243 			}
    244 		} else
    245 			netmask = (struct sockaddr *)&ifrnetmask.lifr_addr;
    246 
    247 		/*
    248 		 * Get the broadcast address for this address on this
    249 		 * interface (if any).
    250 		 */
    251 		if (ifrflags.lifr_flags & IFF_BROADCAST) {
    252 			strncpy(ifrbroadaddr.lifr_name, ifrp->lifr_name,
    253 			    sizeof(ifrbroadaddr.lifr_name));
    254 			memcpy(&ifrbroadaddr.lifr_addr, &ifrp->lifr_addr,
    255 			    sizeof(ifrbroadaddr.lifr_addr));
    256 			if (ioctl(fd, SIOCGLIFBRDADDR,
    257 			    (char *)&ifrbroadaddr) < 0) {
    258 				if (errno == EADDRNOTAVAIL) {
    259 					/*
    260 					 * Not available.
    261 					 */
    262 					broadaddr = NULL;
    263 				} else {
    264 					(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
    265 					    "SIOCGLIFBRDADDR: %.*s: %s",
    266 					    (int)sizeof(ifrbroadaddr.lifr_name),
    267 					    ifrbroadaddr.lifr_name,
    268 					    pcap_strerror(errno));
    269 					ret = -1;
    270 					break;
    271 				}
    272 			} else
    273 				broadaddr = (struct sockaddr *)&ifrbroadaddr.lifr_broadaddr;
    274 		} else {
    275 			/*
    276 			 * Not a broadcast interface, so no broadcast
    277 			 * address.
    278 			 */
    279 			broadaddr = NULL;
    280 		}
    281 
    282 		/*
    283 		 * Get the destination address for this address on this
    284 		 * interface (if any).
    285 		 */
    286 		if (ifrflags.lifr_flags & IFF_POINTOPOINT) {
    287 			strncpy(ifrdstaddr.lifr_name, ifrp->lifr_name,
    288 			    sizeof(ifrdstaddr.lifr_name));
    289 			memcpy(&ifrdstaddr.lifr_addr, &ifrp->lifr_addr,
    290 			    sizeof(ifrdstaddr.lifr_addr));
    291 			if (ioctl(fd, SIOCGLIFDSTADDR,
    292 			    (char *)&ifrdstaddr) < 0) {
    293 				if (errno == EADDRNOTAVAIL) {
    294 					/*
    295 					 * Not available.
    296 					 */
    297 					dstaddr = NULL;
    298 				} else {
    299 					(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
    300 					    "SIOCGLIFDSTADDR: %.*s: %s",
    301 					    (int)sizeof(ifrdstaddr.lifr_name),
    302 					    ifrdstaddr.lifr_name,
    303 					    pcap_strerror(errno));
    304 					ret = -1;
    305 					break;
    306 				}
    307 			} else
    308 				dstaddr = (struct sockaddr *)&ifrdstaddr.lifr_dstaddr;
    309 		} else
    310 			dstaddr = NULL;
    311 
    312 #ifdef HAVE_SOLARIS
    313 		/*
    314 		 * If this entry has a colon followed by a number at
    315 		 * the end, it's a logical interface.  Those are just
    316 		 * the way you assign multiple IP addresses to a real
    317 		 * interface, so an entry for a logical interface should
    318 		 * be treated like the entry for the real interface;
    319 		 * we do that by stripping off the ":" and the number.
    320 		 */
    321 		p = strchr(ifrp->lifr_name, ':');
    322 		if (p != NULL) {
    323 			/*
    324 			 * We have a ":"; is it followed by a number?
    325 			 */
    326 			q = p + 1;
    327 			while (isdigit((unsigned char)*q))
    328 				q++;
    329 			if (*q == '\0') {
    330 				/*
    331 				 * All digits after the ":" until the end.
    332 				 * Strip off the ":" and everything after
    333 				 * it.
    334 				 */
    335 				*p = '\0';
    336 			}
    337 		}
    338 #endif
    339 
    340 		/*
    341 		 * Add information for this address to the list.
    342 		 */
    343 		if (add_addr_to_iflist(&devlist, ifrp->lifr_name,
    344 		    ifrflags.lifr_flags, (struct sockaddr *)&ifrp->lifr_addr,
    345 		    sizeof (struct sockaddr_storage),
    346 		    netmask, sizeof (struct sockaddr_storage),
    347 		    broadaddr, sizeof (struct sockaddr_storage),
    348 		    dstaddr, sizeof (struct sockaddr_storage), errbuf) < 0) {
    349 			ret = -1;
    350 			break;
    351 		}
    352 	}
    353 	free(buf);
    354 	(void)close(fd6);
    355 	(void)close(fd4);
    356 
    357 	if (ret == -1) {
    358 		/*
    359 		 * We had an error; free the list we've been constructing.
    360 		 */
    361 		if (devlist != NULL) {
    362 			pcap_freealldevs(devlist);
    363 			devlist = NULL;
    364 		}
    365 	}
    366 
    367 	*alldevsp = devlist;
    368 	return (ret);
    369 }
    370