interface.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. if (list != NULL) {
  51. ptr = list;
  52. do {
  53. /* check both the alias & name fields */
  54. if (strcmp(alias, ptr->alias) == 0)
  55. return(ptr->name);
  56. if (strcmp(alias, ptr->name) == 0)
  57. return(ptr->name);
  58. ptr = ptr->next;
  59. } while (ptr != NULL);
  60. }
  61. return(NULL);
  62. }
  63. /**
  64. * Get all available interfaces as an interface_list *
  65. */
  66. interface_list_t *
  67. get_interface_list(void)
  68. {
  69. interface_list_t *list_head, *list_ptr;
  70. char ebuf[PCAP_ERRBUF_SIZE], testnic[255];
  71. pcap_if_t *pcap_if, *pcap_if_ptr;
  72. int i = 0;
  73. DIR *dir;
  74. struct dirent *dirdata;
  75. #ifdef HAVE_NETMAP
  76. int fd = -1;
  77. nmreq_t nmr;
  78. #endif
  79. #if defined HAVE_LIBPCAP_NETMAP || defined HAVE_NETMAP
  80. u_int32_t netmap_version = -1;
  81. #endif
  82. #ifndef HAVE_WIN32
  83. /* Unix just has a warning about being root */
  84. if (geteuid() != 0)
  85. warn("May need to run as root to get access to all network interfaces.");
  86. #endif
  87. #ifdef HAVE_NETMAP
  88. netmap_version = get_netmap_version();
  89. #endif
  90. if (pcap_findalldevs(&pcap_if, ebuf) < 0)
  91. errx(-1, "Error: %s", ebuf);
  92. pcap_if_ptr = pcap_if;
  93. list_head = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  94. list_ptr = list_head;
  95. while (pcap_if_ptr != NULL) {
  96. if (i > 0) {
  97. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  98. list_ptr = list_ptr->next;
  99. }
  100. strlcpy(list_ptr->name, pcap_if_ptr->name, sizeof(list_ptr->name));
  101. dbgx(3, "Adding %s to interface list", list_ptr->name);
  102. /* description is usually null under Unix */
  103. if (pcap_if_ptr->description != NULL)
  104. strlcpy(list_ptr->description, pcap_if_ptr->description, sizeof(list_ptr->description));
  105. sprintf(list_ptr->alias, "%%%d", i++);
  106. list_ptr->flags = pcap_if_ptr->flags;
  107. #ifdef HAVE_LIBPCAP_NETMAP
  108. /*
  109. * add the syntaxes supported by netmap-libpcap
  110. *
  111. * available at http://code.google.com/p/netmap-libpcap/
  112. */
  113. if (!(pcap_if_ptr->flags & PCAP_IF_LOOPBACK)
  114. && strcmp("any", pcap_if_ptr->name)) {
  115. #endif
  116. #ifdef HAVE_NETMAP
  117. if (netmap_version != -1 && (fd = open ("/dev/netmap", O_RDWR)) < 0)
  118. continue;
  119. bzero(&nmr, sizeof(nmr));
  120. strncpy(nmr.nr_name, pcap_if_ptr->name, sizeof(nmr.nr_name));
  121. nmr.nr_version = netmap_version;
  122. if (ioctl(fd, NIOCGINFO, &nmr) == 0) {
  123. int x;
  124. #endif /* HAVE_NETMAP */
  125. #if defined HAVE_LIBPCAP_NETMAP || defined HAVE_NETMAP
  126. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  127. list_ptr = list_ptr->next;
  128. snprintf(list_ptr->name, sizeof(list_ptr->name), "vale:%s", pcap_if_ptr->name);
  129. sprintf(list_ptr->alias, "%%%d", i++);
  130. list_ptr->flags = pcap_if_ptr->flags;
  131. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  132. list_ptr = list_ptr->next;
  133. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s", pcap_if_ptr->name);
  134. sprintf(list_ptr->alias, "%%%d", i++);
  135. list_ptr->flags = pcap_if_ptr->flags;
  136. if (netmap_version >= 10) {
  137. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  138. list_ptr = list_ptr->next;
  139. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s!", pcap_if_ptr->name);
  140. sprintf(list_ptr->alias, "%%%d", i++);
  141. list_ptr->flags = pcap_if_ptr->flags;
  142. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  143. list_ptr = list_ptr->next;
  144. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s*", pcap_if_ptr->name);
  145. sprintf(list_ptr->alias, "%%%d", i++);
  146. list_ptr->flags = pcap_if_ptr->flags;
  147. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  148. list_ptr = list_ptr->next;
  149. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s^", pcap_if_ptr->name);
  150. sprintf(list_ptr->alias, "%%%d", i++);
  151. list_ptr->flags = pcap_if_ptr->flags;
  152. }
  153. #endif /* HAVE_LIBPCAP_NETMAP || HAVE_NETMAP */
  154. #ifdef HAVE_NETMAP
  155. if (netmap_version >= 10) {
  156. for (x = 0; x < nmr.nr_rx_rings; ++x) {
  157. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  158. list_ptr = list_ptr->next;
  159. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s-%d", pcap_if_ptr->name, x);
  160. sprintf(list_ptr->alias, "%%%d", i++);
  161. list_ptr->flags = pcap_if_ptr->flags;
  162. }
  163. }
  164. }
  165. close(fd);
  166. #endif /* HAVE_NETMAP */
  167. #ifdef HAVE_LIBPCAP_NETMAP
  168. }
  169. #endif /* HAVE_LIBPCAP_NETMAP */
  170. #ifdef HAVE_PF_RING_PCAP
  171. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  172. list_ptr = list_ptr->next;
  173. snprintf(list_ptr->name, sizeof(list_ptr->name), "zc:%s", pcap_if_ptr->name);
  174. sprintf(list_ptr->alias, "%%%d", i++);
  175. list_ptr->flags = pcap_if_ptr->flags;
  176. #endif
  177. pcap_if_ptr = pcap_if_ptr->next;
  178. }
  179. pcap_freealldevs(pcap_if);
  180. /* look for khial device: https://github.com/boundary/khial */
  181. if ((dir = opendir("/dev/char")) != NULL) {
  182. while ((dirdata = readdir(dir)) != NULL) {
  183. if (strncmp(dirdata->d_name, "testpackets", strlen("testpackets")) == 0) {
  184. if (i > 0) {
  185. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  186. list_ptr = list_ptr->next;
  187. }
  188. dbgx(3, "Adding %s to interface list", dirdata->d_name);
  189. snprintf(testnic, 254, "/dev/char/%s", dirdata->d_name);
  190. strlcpy(list_ptr->name, testnic, 255);
  191. snprintf(testnic, 255, "khial pseudo-nic: %s", dirdata->d_name);
  192. strlcpy(list_ptr->description, testnic, 255);
  193. strlcpy(list_ptr->alias, dirdata->d_name, 255);
  194. i += 1;
  195. }
  196. }
  197. closedir(dir);
  198. }
  199. dbg(1, "xxx get_interface_list end");
  200. return(list_head);
  201. }
  202. /**
  203. * Prints all the available interfaces found by get_interface_list()
  204. */
  205. void
  206. list_interfaces(interface_list_t *list)
  207. {
  208. interface_list_t *ptr;
  209. if (list == NULL) {
  210. printf("No network interfaces available");
  211. return;
  212. }
  213. printf("Available network interfaces:\n");
  214. #ifdef HAVE_WIN32 /* Win32 has alias/name/description */
  215. printf("Alias\tName\tDescription\n");
  216. #endif
  217. ptr = list;
  218. do {
  219. if (!(ptr->flags & PCAP_IF_LOOPBACK)) {
  220. #ifdef HAVE_WIN32
  221. printf("%s\t%s\n\t%s\n", ptr->alias, ptr->name, ptr->description);
  222. #else
  223. printf("%s\n", ptr->name);
  224. #endif
  225. }
  226. ptr = ptr->next;
  227. } while (ptr != NULL);
  228. }