ieee80211.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* $Id: ieee80211.c 1921 2007-10-25 18:18:50Z aturner $ */
  2. /*
  3. * Copyright (c) 2006-2007 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "dlt_plugins-int.h"
  34. #include "dlt_utils.h"
  35. #include "ieee80211.h"
  36. #include "ieee80211_hdr.h"
  37. #include "tcpedit.h"
  38. #include "common.h"
  39. #include "tcpr.h"
  40. /*
  41. * Notes about the ieee80211 plugin:
  42. * 802.11 is a little different from most other L2 protocols:
  43. * - Not all frames are data frames (control, data, management)
  44. */
  45. static char dlt_name[] = "ieee80211";
  46. _U_ static char dlt_prefix[] = "ieee802_11";
  47. static u_int16_t dlt_value = DLT_IEEE802_11;
  48. /*
  49. * Function to register ourselves. This function is always called, regardless
  50. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  51. * or anything like that (use the dlt_ieee80211_init() function below for that).
  52. * Tasks:
  53. * - Create a new plugin struct
  54. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  55. * actually in the header.
  56. * - Add the plugin to the context's plugin chain
  57. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  58. */
  59. int
  60. dlt_ieee80211_register(tcpeditdlt_t *ctx)
  61. {
  62. tcpeditdlt_plugin_t *plugin;
  63. assert(ctx);
  64. /* create a new plugin structure */
  65. plugin = tcpedit_dlt_newplugin();
  66. /* we're a decoder only plugin */
  67. plugin->provides += PLUGIN_MASK_PROTO + PLUGIN_MASK_SRCADDR + PLUGIN_MASK_DSTADDR;
  68. plugin->requires += 0;
  69. /* what is our DLT value? */
  70. plugin->dlt = dlt_value;
  71. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  72. plugin->name = safe_strdup(dlt_name);
  73. /*
  74. * Point to our functions, note, you need a function for EVERY method.
  75. * Even if it is only an empty stub returning success.
  76. */
  77. plugin->plugin_init = dlt_ieee80211_init;
  78. plugin->plugin_cleanup = dlt_ieee80211_cleanup;
  79. plugin->plugin_parse_opts = dlt_ieee80211_parse_opts;
  80. plugin->plugin_decode = dlt_ieee80211_decode;
  81. plugin->plugin_encode = dlt_ieee80211_encode;
  82. plugin->plugin_proto = dlt_ieee80211_proto;
  83. plugin->plugin_l2addr_type = dlt_ieee80211_l2addr_type;
  84. plugin->plugin_l2len = dlt_ieee80211_l2len;
  85. plugin->plugin_get_layer3 = dlt_ieee80211_get_layer3;
  86. plugin->plugin_merge_layer3 = dlt_ieee80211_merge_layer3;
  87. plugin->plugin_get_mac = dlt_ieee80211_get_mac;
  88. /* add it to the available plugin list */
  89. return tcpedit_dlt_addplugin(ctx, plugin);
  90. }
  91. /*
  92. * Initializer function. This function is called only once, if and only iif
  93. * this plugin will be utilized. Remember, if you need to keep track of any state,
  94. * store it in your plugin->config, not a global!
  95. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  96. */
  97. int
  98. dlt_ieee80211_init(tcpeditdlt_t *ctx)
  99. {
  100. tcpeditdlt_plugin_t *plugin;
  101. ieee80211_config_t *config;
  102. assert(ctx);
  103. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  104. tcpedit_seterr(ctx->tcpedit, "Unable to initalize unregistered plugin %s", dlt_name);
  105. return TCPEDIT_ERROR;
  106. }
  107. /* allocate memory for our deocde extra data */
  108. if (sizeof(ieee80211_extra_t) > 0)
  109. ctx->decoded_extra = safe_malloc(sizeof(ieee80211_extra_t));
  110. /* allocate memory for our config data */
  111. if (sizeof(ieee80211_config_t) > 0)
  112. plugin->config = safe_malloc(sizeof(ieee80211_config_t));
  113. config = (ieee80211_config_t *)plugin->config;
  114. /* FIXME: set default config values here */
  115. return TCPEDIT_OK; /* success */
  116. }
  117. /*
  118. * Since this is used in a library, we should manually clean up after ourselves
  119. * Unless you allocated some memory in dlt_ieee80211_init(), this is just an stub.
  120. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  121. */
  122. int
  123. dlt_ieee80211_cleanup(tcpeditdlt_t *ctx)
  124. {
  125. tcpeditdlt_plugin_t *plugin;
  126. assert(ctx);
  127. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  128. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  129. return TCPEDIT_ERROR;
  130. }
  131. if (ctx->decoded_extra != NULL) {
  132. safe_free(ctx->decoded_extra);
  133. ctx->decoded_extra = NULL;
  134. }
  135. if (plugin->config != NULL) {
  136. safe_free(plugin->config);
  137. plugin->config = NULL;
  138. }
  139. return TCPEDIT_OK; /* success */
  140. }
  141. /*
  142. * This is where you should define all your AutoGen AutoOpts option parsing.
  143. * Any user specified option should have it's bit turned on in the 'provides'
  144. * bit mask.
  145. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  146. */
  147. int
  148. dlt_ieee80211_parse_opts(tcpeditdlt_t *ctx)
  149. {
  150. assert(ctx);
  151. /* we have none */
  152. return TCPEDIT_OK; /* success */
  153. }
  154. /*
  155. * Function to decode the layer 2 header in the packet.
  156. * You need to fill out:
  157. * - ctx->l2len
  158. * - ctx->srcaddr
  159. * - ctx->dstaddr
  160. * - ctx->proto
  161. * - ctx->decoded_extra
  162. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  163. */
  164. int
  165. dlt_ieee80211_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  166. {
  167. assert(ctx);
  168. assert(packet);
  169. assert(pktlen > dlt_ieee80211_l2len(ctx, packet, pktlen));
  170. if (!ieee80211_is_data(ctx, packet, pktlen)) {
  171. tcpedit_seterr(ctx->tcpedit, "Packet " COUNTER_SPEC " is not a normal 802.11 data frame",
  172. ctx->tcpedit->runtime.packetnum);
  173. return TCPEDIT_SOFT_ERROR;
  174. }
  175. if (ieee80211_is_encrypted(ctx, packet, pktlen)) {
  176. tcpedit_seterr(ctx->tcpedit, "Packet " COUNTER_SPEC " is encrypted. Unable to decode frame.",
  177. ctx->tcpedit->runtime.packetnum);
  178. return TCPEDIT_SOFT_ERROR;
  179. }
  180. memcpy(&(ctx->srcaddr), ieee80211_get_src((ieee80211_hdr_t *)packet), ETHER_ADDR_LEN);
  181. memcpy(&(ctx->dstaddr), ieee80211_get_dst((ieee80211_hdr_t *)packet), ETHER_ADDR_LEN);
  182. ctx->proto = dlt_ieee80211_proto(ctx, packet, pktlen);
  183. return TCPEDIT_OK; /* success */
  184. }
  185. /*
  186. * Function to encode the layer 2 header back into the packet.
  187. * Returns: total packet len or TCPEDIT_ERROR
  188. */
  189. int
  190. dlt_ieee80211_encode(tcpeditdlt_t *ctx, u_char **packet_ex, int pktlen, _U_ tcpr_dir_t dir)
  191. {
  192. u_char *packet;
  193. assert(ctx);
  194. assert(packet_ex);
  195. assert(pktlen);
  196. packet = *packet_ex;
  197. assert(packet);
  198. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_IEEE802_11 plugin does not support packet encoding");
  199. return TCPEDIT_ERROR;
  200. }
  201. /*
  202. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  203. */
  204. int
  205. dlt_ieee80211_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  206. {
  207. int protocol, l2len;
  208. assert(ctx);
  209. assert(packet);
  210. l2len = dlt_ieee80211_l2len(ctx, packet, pktlen);
  211. assert(pktlen >= l2len);
  212. protocol = (u_int16_t)packet[l2len - 2];
  213. return protocol;
  214. }
  215. /*
  216. * Function returns a pointer to the layer 3 protocol header or NULL on error
  217. */
  218. u_char *
  219. dlt_ieee80211_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  220. {
  221. int l2len;
  222. assert(ctx);
  223. assert(packet);
  224. l2len = dlt_ieee80211_l2len(ctx, packet, pktlen);
  225. assert(pktlen >= l2len);
  226. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  227. }
  228. /*
  229. * function merges the packet (containing L2 and old L3) with the l3data buffer
  230. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  231. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  232. * like SPARC
  233. */
  234. u_char *
  235. dlt_ieee80211_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  236. {
  237. int l2len;
  238. assert(ctx);
  239. assert(packet);
  240. assert(l3data);
  241. l2len = dlt_ieee80211_l2len(ctx, packet, pktlen);
  242. assert(pktlen >= l2len);
  243. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  244. }
  245. /*
  246. * return the length of the L2 header of the current packet
  247. * based on: http://www.tcpdump.org/lists/workers/2004/07/msg00121.html
  248. * Returns >= 0 or TCPEDIT_SOFT_ERROR on error
  249. *
  250. */
  251. int
  252. dlt_ieee80211_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  253. {
  254. u_int16_t *frame_control, fc;
  255. struct tcpr_802_2snap_hdr *hdr;
  256. int hdrlen = 0;
  257. assert(ctx);
  258. assert(packet);
  259. assert(pktlen);
  260. frame_control = (u_int16_t *)packet;
  261. fc = ntohs(*frame_control);
  262. if (ieee80211_USE_4(fc)) {
  263. hdrlen = sizeof(ieee80211_addr4_hdr_t);
  264. } else {
  265. hdrlen = sizeof(ieee80211_hdr_t);
  266. }
  267. /*
  268. * FIXME: 802.11e? has a QoS feature which apparently extends the header by another
  269. * 2 bytes, but I don't know how to test for that yet.
  270. */
  271. if (pktlen < hdrlen + (int)sizeof(struct tcpr_802_2snap_hdr)) {
  272. return TCPEDIT_SOFT_ERROR;
  273. }
  274. hdr = (struct tcpr_802_2snap_hdr *)&packet[hdrlen];
  275. /* verify the header is 802.2SNAP (8 bytes) not 802.2 (3 bytes) */
  276. if (hdr->snap_dsap == 0xAA && hdr->snap_ssap == 0xAA) {
  277. hdrlen += (int)sizeof(struct tcpr_802_2snap_hdr);
  278. } else {
  279. hdrlen += (int)sizeof(struct tcpr_802_2_hdr);
  280. }
  281. return hdrlen;
  282. }
  283. /*
  284. * return a static pointer to the source/destination MAC address
  285. * return NULL on error/address doesn't exist
  286. */
  287. u_char *
  288. dlt_ieee80211_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  289. {
  290. assert(ctx);
  291. assert(packet);
  292. assert(pktlen);
  293. char *macaddr;
  294. switch(mac) {
  295. case SRC_MAC:
  296. macaddr = ieee80211_get_src(packet);
  297. memcpy(ctx->srcmac, macaddr, ETHER_ADDR_LEN);
  298. return(ctx->srcmac);
  299. break;
  300. case DST_MAC:
  301. macaddr = ieee80211_get_dst(packet);
  302. memcpy(ctx->dstmac, macaddr, ETHER_ADDR_LEN);
  303. return(ctx->dstmac);
  304. break;
  305. default:
  306. errx(1, "Invalid tcpeditdlt_mac_type_t: %d", mac);
  307. }
  308. return(NULL);
  309. }
  310. tcpeditdlt_l2addr_type_t
  311. dlt_ieee80211_l2addr_type(void)
  312. {
  313. return ETHERNET;
  314. }