monitor.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. write_pcap_captured(wt, buf, len);
  25. wlantest_process(wt, buf, len);
  26. }
  27. static void monitor_read_wired(int sock, void *eloop_ctx, void *sock_ctx)
  28. {
  29. struct wlantest *wt = eloop_ctx;
  30. u8 buf[3000];
  31. int len;
  32. len = recv(sock, buf, sizeof(buf), 0);
  33. if (len < 0) {
  34. wpa_printf(MSG_INFO, "recv(PACKET): %s", strerror(errno));
  35. return;
  36. }
  37. wlantest_process_wired(wt, buf, len);
  38. }
  39. int monitor_init(struct wlantest *wt, const char *ifname)
  40. {
  41. struct sockaddr_ll ll;
  42. os_memset(&ll, 0, sizeof(ll));
  43. ll.sll_family = AF_PACKET;
  44. ll.sll_ifindex = if_nametoindex(ifname);
  45. if (ll.sll_ifindex == 0) {
  46. wpa_printf(MSG_ERROR, "Monitor interface '%s' does not exist",
  47. ifname);
  48. return -1;
  49. }
  50. wt->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  51. if (wt->monitor_sock < 0) {
  52. wpa_printf(MSG_ERROR, "socket(PF_PACKET,SOCK_RAW): %s",
  53. strerror(errno));
  54. return -1;
  55. }
  56. if (bind(wt->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  57. wpa_printf(MSG_ERROR, "bind(PACKET): %s", strerror(errno));
  58. close(wt->monitor_sock);
  59. wt->monitor_sock = -1;
  60. return -1;
  61. }
  62. if (eloop_register_read_sock(wt->monitor_sock, monitor_read, wt, NULL))
  63. {
  64. wpa_printf(MSG_ERROR, "Could not register monitor read "
  65. "socket");
  66. close(wt->monitor_sock);
  67. wt->monitor_sock = -1;
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. int monitor_init_wired(struct wlantest *wt, const char *ifname)
  73. {
  74. struct sockaddr_ll ll;
  75. os_memset(&ll, 0, sizeof(ll));
  76. ll.sll_family = AF_PACKET;
  77. ll.sll_ifindex = if_nametoindex(ifname);
  78. if (ll.sll_ifindex == 0) {
  79. wpa_printf(MSG_ERROR, "Monitor interface '%s' does not exist",
  80. ifname);
  81. return -1;
  82. }
  83. wt->monitor_wired = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  84. if (wt->monitor_wired < 0) {
  85. wpa_printf(MSG_ERROR, "socket(PF_PACKET,SOCK_RAW): %s",
  86. strerror(errno));
  87. return -1;
  88. }
  89. if (bind(wt->monitor_wired, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  90. wpa_printf(MSG_ERROR, "bind(PACKET): %s", strerror(errno));
  91. close(wt->monitor_wired);
  92. wt->monitor_wired = -1;
  93. return -1;
  94. }
  95. if (eloop_register_read_sock(wt->monitor_wired, monitor_read_wired,
  96. wt, NULL)) {
  97. wpa_printf(MSG_ERROR, "Could not register monitor read "
  98. "socket");
  99. close(wt->monitor_wired);
  100. wt->monitor_wired = -1;
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. void monitor_deinit(struct wlantest *wt)
  106. {
  107. if (wt->monitor_sock >= 0) {
  108. eloop_unregister_read_sock(wt->monitor_sock);
  109. close(wt->monitor_sock);
  110. wt->monitor_sock = -1;
  111. }
  112. if (wt->monitor_wired >= 0) {
  113. eloop_unregister_read_sock(wt->monitor_wired);
  114. close(wt->monitor_wired);
  115. wt->monitor_wired = -1;
  116. }
  117. }