inject.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * wlantest frame injection
  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 "utils/common.h"
  16. #include "wlantest.h"
  17. static int inject_frame(int s, const void *data, size_t len)
  18. {
  19. #define IEEE80211_RADIOTAP_F_FRAG 0x08
  20. unsigned char rtap_hdr[] = {
  21. 0x00, 0x00, /* radiotap version */
  22. 0x0e, 0x00, /* radiotap length */
  23. 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
  24. IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
  25. 0x00, /* padding */
  26. 0x00, 0x00, /* RX and TX flags to indicate that */
  27. 0x00, 0x00, /* this is the injected frame directly */
  28. };
  29. struct iovec iov[2] = {
  30. {
  31. .iov_base = &rtap_hdr,
  32. .iov_len = sizeof(rtap_hdr),
  33. },
  34. {
  35. .iov_base = (void *) data,
  36. .iov_len = len,
  37. }
  38. };
  39. struct msghdr msg = {
  40. .msg_name = NULL,
  41. .msg_namelen = 0,
  42. .msg_iov = iov,
  43. .msg_iovlen = 2,
  44. .msg_control = NULL,
  45. .msg_controllen = 0,
  46. .msg_flags = 0,
  47. };
  48. int ret;
  49. ret = sendmsg(s, &msg, 0);
  50. if (ret < 0)
  51. perror("sendmsg");
  52. return ret;
  53. }
  54. int wlantest_inject(struct wlantest *wt, struct wlantest_bss *bss,
  55. struct wlantest_sta *sta, u8 *frame, size_t len,
  56. enum wlantest_inject_protection prot)
  57. {
  58. int ret;
  59. wpa_hexdump(MSG_DEBUG, "Inject frame", frame, len);
  60. if (wt->monitor_sock < 0) {
  61. wpa_printf(MSG_INFO, "Cannot inject frames when monitor "
  62. "interface is not in use");
  63. return -1;
  64. }
  65. /* TODO: encrypt if needed */
  66. if (prot != WLANTEST_INJECT_UNPROTECTED)
  67. return -1;
  68. ret = inject_frame(wt->monitor_sock, frame, len);
  69. return (ret < 0) ? -1 : 0;
  70. }