monitor.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Linux packet socket monitor
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include <net/if.h>
  10. #include <netpacket/packet.h>
  11. #include "utils/common.h"
  12. #include "utils/eloop.h"
  13. #include "wlantest.h"
  14. static void monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
  15. {
  16. struct wlantest *wt = eloop_ctx;
  17. u8 buf[3000];
  18. int len;
  19. len = recv(sock, buf, sizeof(buf), 0);
  20. if (len < 0) {
  21. wpa_printf(MSG_INFO, "recv(PACKET): %s", strerror(errno));
  22. return;
  23. }
  24. clear_notes(wt);
  25. os_free(wt->decrypted);
  26. wt->decrypted = NULL;
  27. write_pcap_captured(wt, buf, len);
  28. wlantest_process(wt, buf, len);
  29. write_pcapng_captured(wt, buf, len);
  30. }
  31. static void monitor_read_wired(int sock, void *eloop_ctx, void *sock_ctx)
  32. {
  33. struct wlantest *wt = eloop_ctx;
  34. u8 buf[3000];
  35. int len;
  36. len = recv(sock, buf, sizeof(buf), 0);
  37. if (len < 0) {
  38. wpa_printf(MSG_INFO, "recv(PACKET): %s", strerror(errno));
  39. return;
  40. }
  41. wlantest_process_wired(wt, buf, len);
  42. }
  43. int monitor_init(struct wlantest *wt, const char *ifname)
  44. {
  45. struct sockaddr_ll ll;
  46. os_memset(&ll, 0, sizeof(ll));
  47. ll.sll_family = AF_PACKET;
  48. ll.sll_ifindex = if_nametoindex(ifname);
  49. if (ll.sll_ifindex == 0) {
  50. wpa_printf(MSG_ERROR, "Monitor interface '%s' does not exist",
  51. ifname);
  52. return -1;
  53. }
  54. wt->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  55. if (wt->monitor_sock < 0) {
  56. wpa_printf(MSG_ERROR, "socket(PF_PACKET,SOCK_RAW): %s",
  57. strerror(errno));
  58. return -1;
  59. }
  60. if (bind(wt->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  61. wpa_printf(MSG_ERROR, "bind(PACKET): %s", strerror(errno));
  62. close(wt->monitor_sock);
  63. wt->monitor_sock = -1;
  64. return -1;
  65. }
  66. if (eloop_register_read_sock(wt->monitor_sock, monitor_read, wt, NULL))
  67. {
  68. wpa_printf(MSG_ERROR, "Could not register monitor read "
  69. "socket");
  70. close(wt->monitor_sock);
  71. wt->monitor_sock = -1;
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. int monitor_init_wired(struct wlantest *wt, const char *ifname)
  77. {
  78. struct sockaddr_ll ll;
  79. os_memset(&ll, 0, sizeof(ll));
  80. ll.sll_family = AF_PACKET;
  81. ll.sll_ifindex = if_nametoindex(ifname);
  82. if (ll.sll_ifindex == 0) {
  83. wpa_printf(MSG_ERROR, "Monitor interface '%s' does not exist",
  84. ifname);
  85. return -1;
  86. }
  87. wt->monitor_wired = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  88. if (wt->monitor_wired < 0) {
  89. wpa_printf(MSG_ERROR, "socket(PF_PACKET,SOCK_RAW): %s",
  90. strerror(errno));
  91. return -1;
  92. }
  93. if (bind(wt->monitor_wired, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  94. wpa_printf(MSG_ERROR, "bind(PACKET): %s", strerror(errno));
  95. close(wt->monitor_wired);
  96. wt->monitor_wired = -1;
  97. return -1;
  98. }
  99. if (eloop_register_read_sock(wt->monitor_wired, monitor_read_wired,
  100. wt, NULL)) {
  101. wpa_printf(MSG_ERROR, "Could not register monitor read "
  102. "socket");
  103. close(wt->monitor_wired);
  104. wt->monitor_wired = -1;
  105. return -1;
  106. }
  107. return 0;
  108. }
  109. void monitor_deinit(struct wlantest *wt)
  110. {
  111. if (wt->monitor_sock >= 0) {
  112. eloop_unregister_read_sock(wt->monitor_sock);
  113. close(wt->monitor_sock);
  114. wt->monitor_sock = -1;
  115. }
  116. if (wt->monitor_wired >= 0) {
  117. eloop_unregister_read_sock(wt->monitor_wired);
  118. close(wt->monitor_wired);
  119. wt->monitor_wired = -1;
  120. }
  121. }