monitor.c 3.3 KB

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