interface.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <dirent.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/file.h>
  26. #include <sys/socket.h>
  27. #include "config.h"
  28. #include "defines.h"
  29. #include "common.h"
  30. #include "interface.h"
  31. #ifdef HAVE_NETMAP
  32. #include "common/netmap.h"
  33. #endif
  34. #ifdef DEBUG
  35. extern int debug;
  36. #endif
  37. /**
  38. * Method takes a user specified device name and returns
  39. * the canonical name for that device. This allows me to
  40. * create named interface aliases on platforms like Windows
  41. * which use horrifically long interface names
  42. *
  43. * Returns NULL on error
  44. */
  45. char *
  46. get_interface(interface_list_t *list, const char *alias)
  47. {
  48. interface_list_t *ptr;
  49. assert(alias);
  50. ptr = list;
  51. while (ptr) {
  52. /* check both the alias & name fields */
  53. if (strcmp(alias, ptr->alias) == 0 ||
  54. strcmp(alias, ptr->name) == 0)
  55. return(ptr->name);
  56. ptr = ptr->next;
  57. }
  58. return(NULL);
  59. }
  60. /**
  61. * Get all available interfaces as an interface_list *
  62. */
  63. interface_list_t *
  64. get_interface_list(void)
  65. {
  66. interface_list_t *list_head, *list_ptr;
  67. char ebuf[PCAP_ERRBUF_SIZE], testnic[255];
  68. pcap_if_t *pcap_if, *pcap_if_ptr;
  69. int i = 0;
  70. DIR *dir;
  71. struct dirent *dirdata;
  72. #ifdef HAVE_NETMAP
  73. int fd = -1;
  74. nmreq_t nmr;
  75. #endif
  76. #if defined HAVE_LIBPCAP_NETMAP || defined HAVE_NETMAP
  77. u_int32_t netmap_version = -1;
  78. #endif
  79. #ifndef HAVE_WIN32
  80. /* Unix just has a warning about being root */
  81. if (geteuid() != 0)
  82. warn("May need to run as root to get access to all network interfaces.");
  83. #endif
  84. #ifdef HAVE_NETMAP
  85. netmap_version = get_netmap_version();
  86. #endif
  87. if (pcap_findalldevs(&pcap_if, ebuf) < 0)
  88. errx(-1, "Error: %s", ebuf);
  89. pcap_if_ptr = pcap_if;
  90. list_head = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  91. list_ptr = list_head;
  92. while (pcap_if_ptr != NULL) {
  93. if (i > 0) {
  94. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  95. list_ptr = list_ptr->next;
  96. }
  97. strlcpy(list_ptr->name, pcap_if_ptr->name, sizeof(list_ptr->name));
  98. dbgx(3, "Adding %s to interface list", list_ptr->name);
  99. /* description is usually null under Unix */
  100. if (pcap_if_ptr->description != NULL)
  101. strlcpy(list_ptr->description, pcap_if_ptr->description, sizeof(list_ptr->description));
  102. sprintf(list_ptr->alias, "%%%d", i++);
  103. list_ptr->flags = pcap_if_ptr->flags;
  104. #ifdef HAVE_LIBPCAP_NETMAP
  105. /*
  106. * add the syntaxes supported by netmap-libpcap
  107. *
  108. * available at http://code.google.com/p/netmap-libpcap/
  109. */
  110. if (!(pcap_if_ptr->flags & PCAP_IF_LOOPBACK)
  111. && strcmp("any", pcap_if_ptr->name)) {
  112. #endif
  113. #ifdef HAVE_NETMAP
  114. if (netmap_version != -1 && (fd = open ("/dev/netmap", O_RDWR)) < 0)
  115. continue;
  116. bzero(&nmr, sizeof(nmr));
  117. strncpy(nmr.nr_name, pcap_if_ptr->name, sizeof(nmr.nr_name));
  118. nmr.nr_version = netmap_version;
  119. if (ioctl(fd, NIOCGINFO, &nmr) == 0) {
  120. int x;
  121. #endif /* HAVE_NETMAP */
  122. #if defined HAVE_LIBPCAP_NETMAP || defined HAVE_NETMAP
  123. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  124. list_ptr = list_ptr->next;
  125. snprintf(list_ptr->name, sizeof(list_ptr->name), "vale:%s", pcap_if_ptr->name);
  126. sprintf(list_ptr->alias, "%%%d", i++);
  127. list_ptr->flags = pcap_if_ptr->flags;
  128. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  129. list_ptr = list_ptr->next;
  130. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s", pcap_if_ptr->name);
  131. sprintf(list_ptr->alias, "%%%d", i++);
  132. list_ptr->flags = pcap_if_ptr->flags;
  133. if (netmap_version >= 10) {
  134. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  135. list_ptr = list_ptr->next;
  136. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s!", pcap_if_ptr->name);
  137. sprintf(list_ptr->alias, "%%%d", i++);
  138. list_ptr->flags = pcap_if_ptr->flags;
  139. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  140. list_ptr = list_ptr->next;
  141. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s*", pcap_if_ptr->name);
  142. sprintf(list_ptr->alias, "%%%d", i++);
  143. list_ptr->flags = pcap_if_ptr->flags;
  144. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  145. list_ptr = list_ptr->next;
  146. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s^", pcap_if_ptr->name);
  147. sprintf(list_ptr->alias, "%%%d", i++);
  148. list_ptr->flags = pcap_if_ptr->flags;
  149. }
  150. #endif /* HAVE_LIBPCAP_NETMAP || HAVE_NETMAP */
  151. #ifdef HAVE_NETMAP
  152. if (netmap_version >= 10) {
  153. for (x = 0; x < nmr.nr_rx_rings; ++x) {
  154. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  155. list_ptr = list_ptr->next;
  156. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s-%d", pcap_if_ptr->name, x);
  157. sprintf(list_ptr->alias, "%%%d", i++);
  158. list_ptr->flags = pcap_if_ptr->flags;
  159. }
  160. }
  161. }
  162. close(fd);
  163. #endif /* HAVE_NETMAP */
  164. #ifdef HAVE_LIBPCAP_NETMAP
  165. }
  166. #endif /* HAVE_LIBPCAP_NETMAP */
  167. #ifdef HAVE_PF_RING_PCAP
  168. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  169. list_ptr = list_ptr->next;
  170. snprintf(list_ptr->name, sizeof(list_ptr->name), "zc:%s", pcap_if_ptr->name);
  171. sprintf(list_ptr->alias, "%%%d", i++);
  172. list_ptr->flags = pcap_if_ptr->flags;
  173. #endif
  174. pcap_if_ptr = pcap_if_ptr->next;
  175. }
  176. pcap_freealldevs(pcap_if);
  177. /* look for khial device: https://github.com/boundary/khial */
  178. if ((dir = opendir("/dev/char")) != NULL) {
  179. while ((dirdata = readdir(dir)) != NULL) {
  180. if (strncmp(dirdata->d_name, "testpackets", strlen("testpackets")) == 0) {
  181. if (i > 0) {
  182. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  183. list_ptr = list_ptr->next;
  184. }
  185. dbgx(3, "Adding %s to interface list", dirdata->d_name);
  186. snprintf(testnic, 254, "/dev/char/%s", dirdata->d_name);
  187. strlcpy(list_ptr->name, testnic, 255);
  188. snprintf(testnic, 255, "khial pseudo-nic: %s", dirdata->d_name);
  189. strlcpy(list_ptr->description, testnic, 255);
  190. strlcpy(list_ptr->alias, dirdata->d_name, 255);
  191. i += 1;
  192. }
  193. }
  194. closedir(dir);
  195. }
  196. dbg(1, "xxx get_interface_list end");
  197. return(list_head);
  198. }
  199. /**
  200. * Prints all the available interfaces found by get_interface_list()
  201. */
  202. void
  203. list_interfaces(interface_list_t *list)
  204. {
  205. interface_list_t *ptr;
  206. if (list == NULL) {
  207. printf("No network interfaces available");
  208. return;
  209. }
  210. printf("Available network interfaces:\n");
  211. #ifdef HAVE_WIN32 /* Win32 has alias/name/description */
  212. printf("Alias\tName\tDescription\n");
  213. #endif
  214. ptr = list;
  215. do {
  216. if (!(ptr->flags & PCAP_IF_LOOPBACK)) {
  217. #ifdef HAVE_WIN32
  218. printf("%s\t%s\n\t%s\n", ptr->alias, ptr->name, ptr->description);
  219. #else
  220. printf("%s\n", ptr->name);
  221. #endif
  222. }
  223. ptr = ptr->next;
  224. } while (ptr != NULL);
  225. }