monitor.c 3.4 KB

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