ieee80211_hdr.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "tcpedit.h"
  22. #include "common.h"
  23. #include "tcpr.h"
  24. #include "dlt_utils.h"
  25. #include "tcpedit_stub.h"
  26. #include "ieee80211.h"
  27. #include "ieee80211_hdr.h"
  28. /*
  29. * Does the given 802.11 header have data?
  30. * returns 1 for true & 0 for false
  31. */
  32. int
  33. ieee80211_is_data(tcpeditdlt_t *ctx, const void *packet, const int pktlen)
  34. {
  35. uint16_t *frame_control, fc;
  36. struct tcpr_802_2snap_hdr *snap;
  37. int hdrlen = 0;
  38. assert(ctx);
  39. assert(packet);
  40. /* Ack, Auth, NULL packets often are very small (10-30 bytes) */
  41. if (pktlen <= (int)sizeof(ieee80211_hdr_t)) {
  42. dbgx(1, "**** packet " COUNTER_SPEC " is too small (%d)", ctx->tcpedit->runtime.packetnum, pktlen);
  43. return 0;
  44. }
  45. /*
  46. * Fields: Version|Type|Subtype|Flags
  47. * Bytes: 2|2|4|8
  48. * Types: 00 = Management, 01 = Control, 10 = Data
  49. * Data Subtypes (in binary):
  50. * 0000 - Data
  51. * 0001 - Data + Ack
  52. * 0010 - Data + Poll
  53. * 0011 - Data + Ack + Poll
  54. * 01?? - Data + Null (no data)
  55. * 1000 - QoS (w/ data)
  56. * 1100 - QoS (no data)
  57. * 1??? - Reserved (beacon, etc)
  58. * FIXME:
  59. * So right now, we only look for pure data frames, since I'm not sure what to do with ACK/Poll
  60. */
  61. frame_control = (uint16_t *)packet;
  62. fc = ntohs(*frame_control);
  63. /* reserved == no data */
  64. if ((fc & ieee80211_FC_SUBTYPE_MASK) == ieee80211_FC_SUBTYPE_NULL) {
  65. dbg(2, "packet is NULL");
  66. return 1;
  67. }
  68. /* check for data */
  69. if ((fc & ieee80211_FC_TYPE_MASK) == ieee80211_FC_TYPE_DATA) {
  70. dbg(2, "packet has data bit set");
  71. return 1;
  72. }
  73. /* QoS is set by the high bit, all the lower bits are QoS sub-types
  74. QoS seems to add 2 bytes of data at the end of the 802.11 hdr */
  75. if ((fc & ieee80211_FC_SUBTYPE_MASK) >= ieee80211_FC_SUBTYPE_QOS) {
  76. hdrlen += 2;
  77. }
  78. /* frame must also have a 802.2 SNAP header */
  79. if (ieee80211_USE_4(fc)) {
  80. hdrlen += sizeof(ieee80211_addr4_hdr_t);
  81. } else {
  82. hdrlen += sizeof(ieee80211_hdr_t);
  83. }
  84. if (pktlen < hdrlen + (int)sizeof(struct tcpr_802_2snap_hdr)) {
  85. return 0; /* not long enough for SNAP */
  86. }
  87. snap = (struct tcpr_802_2snap_hdr *)&((u_char *)packet)[hdrlen];
  88. /* verify the header is 802.2SNAP (8 bytes) not 802.2 (3 bytes) */
  89. if (snap->snap_dsap == 0xAA && snap->snap_ssap == 0xAA) {
  90. dbg(2, "packet is 802.2SNAP which I think always has data");
  91. return 1;
  92. }
  93. warnx("Packet " COUNTER_SPEC " is unknown reason for non-data", ctx->tcpedit->runtime.packetnum);
  94. return 0;
  95. }
  96. /*
  97. * returns 1 if WEP is enabled, 0 if not
  98. */
  99. int
  100. ieee80211_is_encrypted(tcpeditdlt_t *ctx, const void *packet, const int pktlen)
  101. {
  102. uint16_t *frame_control, fc;
  103. assert(ctx);
  104. assert(packet);
  105. if (pktlen < (int)sizeof(ieee80211_hdr_t))
  106. return 0;
  107. frame_control = (uint16_t *)packet;
  108. fc = ntohs(*frame_control);
  109. if ((fc & ieee80211_FC_WEP_MASK) == ieee80211_FC_WEP_MASK) {
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. /*
  115. * 802.11 headers are variable length and the clients (non-AP's) have their
  116. * src & dst MAC addresses in different places in the header based on the
  117. * flags set in the first two bytes of the header (frame control)
  118. */
  119. u_char *
  120. ieee80211_get_src(const void *header)
  121. {
  122. uint16_t *frame_control, fc;
  123. assert(header);
  124. frame_control = (uint16_t *)header;
  125. fc = ntohs(*frame_control);
  126. if (ieee80211_USE_4(fc)) {
  127. ieee80211_addr4_hdr_t *addr4 = (ieee80211_addr4_hdr_t *)header;
  128. return addr4->addr4;
  129. } else {
  130. ieee80211_hdr_t *addr3 = (ieee80211_hdr_t *)header;
  131. switch (fc & (ieee80211_FC_TO_DS_MASK + ieee80211_FC_FROM_DS_MASK)) {
  132. case ieee80211_FC_TO_DS_MASK:
  133. return addr3->addr2;
  134. case ieee80211_FC_FROM_DS_MASK:
  135. return addr3->addr3;
  136. case 0:
  137. return addr3->addr2;
  138. default:
  139. err(-1, "Whoops... we shouldn't of gotten here.");
  140. }
  141. }
  142. return NULL;
  143. }
  144. u_char *
  145. ieee80211_get_dst(const void *header)
  146. {
  147. uint16_t *frame_control, fc;
  148. assert(header);
  149. frame_control = (uint16_t *)header;
  150. fc = ntohs(*frame_control);
  151. if (ieee80211_USE_4(fc)) {
  152. ieee80211_addr4_hdr_t *addr4 = (ieee80211_addr4_hdr_t *)header;
  153. return addr4->addr3;
  154. } else {
  155. ieee80211_hdr_t *addr3 = (ieee80211_hdr_t *)header;
  156. switch (fc & (ieee80211_FC_TO_DS_MASK + ieee80211_FC_FROM_DS_MASK)) {
  157. case ieee80211_FC_TO_DS_MASK:
  158. return addr3->addr3;
  159. case ieee80211_FC_FROM_DS_MASK:
  160. return addr3->addr1;
  161. case 0:
  162. return addr3->addr3;
  163. default:
  164. err(-1, "Whoops... we shouldn't of gotten here.");
  165. }
  166. }
  167. return NULL;
  168. }