fad-gifc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. #ifdef HAVE_CONFIG_H
  35. #include <config.h>
  36. #endif
  37. #include <sys/param.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. struct mbuf; /* Squelch compiler warnings on some platforms for */
  45. struct rtentry; /* declarations in <net/if.h> */
  46. #include <net/if.h>
  47. #include <netinet/in.h>
  48. #include <ctype.h>
  49. #include <errno.h>
  50. #include <memory.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include <unistd.h>
  55. #ifdef HAVE_LIMITS_H
  56. #include <limits.h>
  57. #else
  58. #define INT_MAX 2147483647
  59. #endif
  60. #include "pcap-int.h"
  61. #ifdef HAVE_OS_PROTO_H
  62. #include "os-proto.h"
  63. #endif
  64. /*
  65. * This is fun.
  66. *
  67. * In older BSD systems, socket addresses were fixed-length, and
  68. * "sizeof (struct sockaddr)" gave the size of the structure.
  69. * All addresses fit within a "struct sockaddr".
  70. *
  71. * In newer BSD systems, the socket address is variable-length, and
  72. * there's an "sa_len" field giving the length of the structure;
  73. * this allows socket addresses to be longer than 2 bytes of family
  74. * and 14 bytes of data.
  75. *
  76. * Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
  77. * variant of the old BSD scheme (with "struct sockaddr_storage" rather
  78. * than "struct sockaddr"), and some use the new BSD scheme.
  79. *
  80. * Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
  81. * macro that determines the size based on the address family. Other
  82. * versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
  83. * but not in the final version).
  84. *
  85. * We assume that a UNIX that doesn't have "getifaddrs()" and doesn't have
  86. * SIOCGLIFCONF, but has SIOCGIFCONF, uses "struct sockaddr" for the
  87. * address in an entry returned by SIOCGIFCONF.
  88. */
  89. #ifndef SA_LEN
  90. #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
  91. #define SA_LEN(addr) ((addr)->sa_len)
  92. #else /* HAVE_STRUCT_SOCKADDR_SA_LEN */
  93. #define SA_LEN(addr) (sizeof (struct sockaddr))
  94. #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
  95. #endif /* SA_LEN */
  96. /*
  97. * This is also fun.
  98. *
  99. * There is no ioctl that returns the amount of space required for all
  100. * the data that SIOCGIFCONF could return, and if a buffer is supplied
  101. * that's not large enough for all the data SIOCGIFCONF could return,
  102. * on at least some platforms it just returns the data that'd fit with
  103. * no indication that there wasn't enough room for all the data, much
  104. * less an indication of how much more room is required.
  105. *
  106. * The only way to ensure that we got all the data is to pass a buffer
  107. * large enough that the amount of space in the buffer *not* filled in
  108. * is greater than the largest possible entry.
  109. *
  110. * We assume that's "sizeof(ifreq.ifr_name)" plus 255, under the assumption
  111. * that no address is more than 255 bytes (on systems where the "sa_len"
  112. * field in a "struct sockaddr" is 1 byte, e.g. newer BSDs, that's the
  113. * case, and addresses are unlikely to be bigger than that in any case).
  114. */
  115. #define MAX_SA_LEN 255
  116. /*
  117. * Get a list of all interfaces that are up and that we can open.
  118. * Returns -1 on error, 0 otherwise.
  119. * The list, as returned through "alldevsp", may be null if no interfaces
  120. * were up and could be opened.
  121. *
  122. * This is the implementation used on platforms that have SIOCGIFCONF but
  123. * don't have any other mechanism for getting a list of interfaces.
  124. *
  125. * XXX - or platforms that have other, better mechanisms but for which
  126. * we don't yet have code to use that mechanism; I think there's a better
  127. * way on Linux, for example, but if that better way is "getifaddrs()",
  128. * we already have that.
  129. */
  130. int
  131. pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf,
  132. int (*check_usable)(const char *), get_if_flags_func get_flags_func)
  133. {
  134. register int fd;
  135. register struct ifreq *ifrp, *ifend, *ifnext;
  136. size_t n;
  137. struct ifconf ifc;
  138. char *buf = NULL;
  139. unsigned buf_size;
  140. #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
  141. char *p, *q;
  142. #endif
  143. struct ifreq ifrflags, ifrnetmask, ifrbroadaddr, ifrdstaddr;
  144. struct sockaddr *netmask, *broadaddr, *dstaddr;
  145. size_t netmask_size, broadaddr_size, dstaddr_size;
  146. int ret = 0;
  147. /*
  148. * Create a socket from which to fetch the list of interfaces.
  149. */
  150. fd = socket(AF_INET, SOCK_DGRAM, 0);
  151. if (fd < 0) {
  152. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  153. errno, "socket");
  154. return (-1);
  155. }
  156. /*
  157. * Start with an 8K buffer, and keep growing the buffer until
  158. * we have more than "sizeof(ifrp->ifr_name) + MAX_SA_LEN"
  159. * bytes left over in the buffer or we fail to get the
  160. * interface list for some reason other than EINVAL (which is
  161. * presumed here to mean "buffer is too small").
  162. */
  163. buf_size = 8192;
  164. for (;;) {
  165. /*
  166. * Don't let the buffer size get bigger than INT_MAX.
  167. */
  168. if (buf_size > INT_MAX) {
  169. (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  170. "interface information requires more than %u bytes",
  171. INT_MAX);
  172. (void)close(fd);
  173. return (-1);
  174. }
  175. buf = malloc(buf_size);
  176. if (buf == NULL) {
  177. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  178. errno, "malloc");
  179. (void)close(fd);
  180. return (-1);
  181. }
  182. ifc.ifc_len = buf_size;
  183. ifc.ifc_buf = buf;
  184. memset(buf, 0, buf_size);
  185. if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0
  186. && errno != EINVAL) {
  187. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  188. errno, "SIOCGIFCONF");
  189. (void)close(fd);
  190. free(buf);
  191. return (-1);
  192. }
  193. if (ifc.ifc_len < (int)buf_size &&
  194. (buf_size - ifc.ifc_len) > sizeof(ifrp->ifr_name) + MAX_SA_LEN)
  195. break;
  196. free(buf);
  197. buf_size *= 2;
  198. }
  199. ifrp = (struct ifreq *)buf;
  200. ifend = (struct ifreq *)(buf + ifc.ifc_len);
  201. for (; ifrp < ifend; ifrp = ifnext) {
  202. /*
  203. * XXX - what if this isn't an IPv4 address? Can
  204. * we still get the netmask, etc. with ioctls on
  205. * an IPv4 socket?
  206. *
  207. * The answer is probably platform-dependent, and
  208. * if the answer is "no" on more than one platform,
  209. * the way you work around it is probably platform-
  210. * dependent as well.
  211. */
  212. n = SA_LEN(&ifrp->ifr_addr) + sizeof(ifrp->ifr_name);
  213. if (n < sizeof(*ifrp))
  214. ifnext = ifrp + 1;
  215. else
  216. ifnext = (struct ifreq *)((char *)ifrp + n);
  217. /*
  218. * XXX - The 32-bit compatibility layer for Linux on IA-64
  219. * is slightly broken. It correctly converts the structures
  220. * to and from kernel land from 64 bit to 32 bit but
  221. * doesn't update ifc.ifc_len, leaving it larger than the
  222. * amount really used. This means we read off the end
  223. * of the buffer and encounter an interface with an
  224. * "empty" name. Since this is highly unlikely to ever
  225. * occur in a valid case we can just finish looking for
  226. * interfaces if we see an empty name.
  227. */
  228. if (!(*ifrp->ifr_name))
  229. break;
  230. /*
  231. * Skip entries that begin with "dummy".
  232. * XXX - what are these? Is this Linux-specific?
  233. * Are there platforms on which we shouldn't do this?
  234. */
  235. if (strncmp(ifrp->ifr_name, "dummy", 5) == 0)
  236. continue;
  237. /*
  238. * Can we capture on this device?
  239. */
  240. if (!(*check_usable)(ifrp->ifr_name)) {
  241. /*
  242. * No.
  243. */
  244. continue;
  245. }
  246. /*
  247. * Get the flags for this interface.
  248. */
  249. strncpy(ifrflags.ifr_name, ifrp->ifr_name,
  250. sizeof(ifrflags.ifr_name));
  251. if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
  252. if (errno == ENXIO)
  253. continue;
  254. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  255. errno, "SIOCGIFFLAGS: %.*s",
  256. (int)sizeof(ifrflags.ifr_name),
  257. ifrflags.ifr_name);
  258. ret = -1;
  259. break;
  260. }
  261. /*
  262. * Get the netmask for this address on this interface.
  263. */
  264. strncpy(ifrnetmask.ifr_name, ifrp->ifr_name,
  265. sizeof(ifrnetmask.ifr_name));
  266. memcpy(&ifrnetmask.ifr_addr, &ifrp->ifr_addr,
  267. sizeof(ifrnetmask.ifr_addr));
  268. if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifrnetmask) < 0) {
  269. if (errno == EADDRNOTAVAIL) {
  270. /*
  271. * Not available.
  272. */
  273. netmask = NULL;
  274. netmask_size = 0;
  275. } else {
  276. pcap_fmt_errmsg_for_errno(errbuf,
  277. PCAP_ERRBUF_SIZE, errno,
  278. "SIOCGIFNETMASK: %.*s",
  279. (int)sizeof(ifrnetmask.ifr_name),
  280. ifrnetmask.ifr_name);
  281. ret = -1;
  282. break;
  283. }
  284. } else {
  285. netmask = &ifrnetmask.ifr_addr;
  286. netmask_size = SA_LEN(netmask);
  287. }
  288. /*
  289. * Get the broadcast address for this address on this
  290. * interface (if any).
  291. */
  292. if (ifrflags.ifr_flags & IFF_BROADCAST) {
  293. strncpy(ifrbroadaddr.ifr_name, ifrp->ifr_name,
  294. sizeof(ifrbroadaddr.ifr_name));
  295. memcpy(&ifrbroadaddr.ifr_addr, &ifrp->ifr_addr,
  296. sizeof(ifrbroadaddr.ifr_addr));
  297. if (ioctl(fd, SIOCGIFBRDADDR,
  298. (char *)&ifrbroadaddr) < 0) {
  299. if (errno == EADDRNOTAVAIL) {
  300. /*
  301. * Not available.
  302. */
  303. broadaddr = NULL;
  304. broadaddr_size = 0;
  305. } else {
  306. pcap_fmt_errmsg_for_errno(errbuf,
  307. PCAP_ERRBUF_SIZE, errno,
  308. "SIOCGIFBRDADDR: %.*s",
  309. (int)sizeof(ifrbroadaddr.ifr_name),
  310. ifrbroadaddr.ifr_name);
  311. ret = -1;
  312. break;
  313. }
  314. } else {
  315. broadaddr = &ifrbroadaddr.ifr_broadaddr;
  316. broadaddr_size = SA_LEN(broadaddr);
  317. }
  318. } else {
  319. /*
  320. * Not a broadcast interface, so no broadcast
  321. * address.
  322. */
  323. broadaddr = NULL;
  324. broadaddr_size = 0;
  325. }
  326. /*
  327. * Get the destination address for this address on this
  328. * interface (if any).
  329. */
  330. if (ifrflags.ifr_flags & IFF_POINTOPOINT) {
  331. strncpy(ifrdstaddr.ifr_name, ifrp->ifr_name,
  332. sizeof(ifrdstaddr.ifr_name));
  333. memcpy(&ifrdstaddr.ifr_addr, &ifrp->ifr_addr,
  334. sizeof(ifrdstaddr.ifr_addr));
  335. if (ioctl(fd, SIOCGIFDSTADDR,
  336. (char *)&ifrdstaddr) < 0) {
  337. if (errno == EADDRNOTAVAIL) {
  338. /*
  339. * Not available.
  340. */
  341. dstaddr = NULL;
  342. dstaddr_size = 0;
  343. } else {
  344. pcap_fmt_errmsg_for_errno(errbuf,
  345. PCAP_ERRBUF_SIZE, errno,
  346. "SIOCGIFDSTADDR: %.*s",
  347. (int)sizeof(ifrdstaddr.ifr_name),
  348. ifrdstaddr.ifr_name);
  349. ret = -1;
  350. break;
  351. }
  352. } else {
  353. dstaddr = &ifrdstaddr.ifr_dstaddr;
  354. dstaddr_size = SA_LEN(dstaddr);
  355. }
  356. } else {
  357. /*
  358. * Not a point-to-point interface, so no destination
  359. * address.
  360. */
  361. dstaddr = NULL;
  362. dstaddr_size = 0;
  363. }
  364. #if defined (HAVE_SOLARIS) || defined (HAVE_HPUX10_20_OR_LATER)
  365. /*
  366. * If this entry has a colon followed by a number at
  367. * the end, it's a logical interface. Those are just
  368. * the way you assign multiple IP addresses to a real
  369. * interface, so an entry for a logical interface should
  370. * be treated like the entry for the real interface;
  371. * we do that by stripping off the ":" and the number.
  372. */
  373. p = strchr(ifrp->ifr_name, ':');
  374. if (p != NULL) {
  375. /*
  376. * We have a ":"; is it followed by a number?
  377. */
  378. q = p + 1;
  379. while (isdigit((unsigned char)*q))
  380. q++;
  381. if (*q == '\0') {
  382. /*
  383. * All digits after the ":" until the end.
  384. * Strip off the ":" and everything after
  385. * it.
  386. */
  387. *p = '\0';
  388. }
  389. }
  390. #endif
  391. /*
  392. * Add information for this address to the list.
  393. */
  394. if (add_addr_to_if(devlistp, ifrp->ifr_name,
  395. ifrflags.ifr_flags, get_flags_func,
  396. &ifrp->ifr_addr, SA_LEN(&ifrp->ifr_addr),
  397. netmask, netmask_size, broadaddr, broadaddr_size,
  398. dstaddr, dstaddr_size, errbuf) < 0) {
  399. ret = -1;
  400. break;
  401. }
  402. }
  403. free(buf);
  404. (void)close(fd);
  405. return (ret);
  406. }