interface.c 8.5 KB

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