wifi_monitor.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. // Module for interfacing with WIFI
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. #include <string.h>
  6. #include <stddef.h>
  7. #include "ctype.h"
  8. #include <stdint.h>
  9. #include "user_interface.h"
  10. #include "wifi_common.h"
  11. #include "sys/network_80211.h"
  12. static int recv_cb;
  13. static uint8 mon_offset;
  14. static uint8 mon_value;
  15. static uint8 mon_mask;
  16. static task_handle_t tasknumber;
  17. #define SNIFFER_BUF2_BUF_SIZE 112
  18. #define BITFIELD(byte, start, len) 8 * (byte) + (start), (len)
  19. #define BYTEFIELD(byte, len) 8 * (byte), 8 * (len)
  20. #define IS_SIGNED 1
  21. #define IS_STRING 2
  22. #define IS_HEXSTRING 3
  23. #define IE_TABLE 4
  24. #define SINGLE_IE 5
  25. #define IS_HEADER 6
  26. #define ANY_FRAME 16
  27. #define IE(id) 0, (id), SINGLE_IE
  28. static void (*on_disconnected)(void);
  29. typedef struct {
  30. const char *key;
  31. uint8 frametype;
  32. } typekey_t;
  33. typedef struct {
  34. const char *name;
  35. unsigned int start : 12;
  36. unsigned int length : 12;
  37. unsigned int opts : 3;
  38. unsigned int frametype : 5; // 16 is *any*
  39. } field_t;
  40. // must be sorted alphabetically
  41. static const field_t fields[] = {
  42. { "aggregation", BITFIELD(7, 3, 1), 0, ANY_FRAME},
  43. { "ampdu_cnt", BITFIELD(9, 0, 8), 0, ANY_FRAME},
  44. { "association_id", BYTEFIELD(40, 2), 0, FRAME_SUBTYPE_ASSOC_RESPONSE},
  45. { "association_id", BYTEFIELD(40, 2), 0, FRAME_SUBTYPE_REASSOC_RESPONSE},
  46. { "authentication_algorithm", BYTEFIELD(36, 2), 0, FRAME_SUBTYPE_AUTHENTICATION},
  47. { "authentication_transaction", BYTEFIELD(38, 2), 0, FRAME_SUBTYPE_AUTHENTICATION},
  48. { "beacon_interval", BYTEFIELD(44, 2), 0, FRAME_SUBTYPE_PROBE_RESPONSE},
  49. { "beacon_interval", BYTEFIELD(44, 2), 0, FRAME_SUBTYPE_BEACON},
  50. { "bssid", BYTEFIELD(28, 6), IS_STRING, ANY_FRAME},
  51. { "bssid_hex", BYTEFIELD(28, 6), IS_HEXSTRING, ANY_FRAME},
  52. { "bssidmatch0", BITFIELD(3, 6, 1), 0, ANY_FRAME},
  53. { "bssidmatch1", BITFIELD(3, 7, 1), 0, ANY_FRAME},
  54. { "capability", BYTEFIELD(36, 2), 0, FRAME_SUBTYPE_ASSOC_REQUEST},
  55. { "capability", BYTEFIELD(36, 2), 0, FRAME_SUBTYPE_ASSOC_RESPONSE},
  56. { "capability", BYTEFIELD(36, 2), 0, FRAME_SUBTYPE_REASSOC_REQUEST},
  57. { "capability", BYTEFIELD(36, 2), 0, FRAME_SUBTYPE_REASSOC_RESPONSE},
  58. { "capability", BYTEFIELD(46, 2), 0, FRAME_SUBTYPE_PROBE_RESPONSE},
  59. { "capability", BYTEFIELD(46, 2), 0, FRAME_SUBTYPE_BEACON},
  60. { "channel", BITFIELD(10, 0, 4), 0, ANY_FRAME},
  61. { "current_ap", BYTEFIELD(40, 6), IS_STRING, FRAME_SUBTYPE_REASSOC_REQUEST},
  62. { "cwb", BITFIELD(4, 7, 1), 0, ANY_FRAME},
  63. { "dmatch0", BITFIELD(3, 4, 1), 0, ANY_FRAME},
  64. { "dmatch1", BITFIELD(3, 5, 1), 0, ANY_FRAME},
  65. { "dstmac", BYTEFIELD(22, 6), IS_STRING, ANY_FRAME},
  66. { "dstmac_hex", BYTEFIELD(22, 6), IS_HEXSTRING, ANY_FRAME},
  67. { "duration", BYTEFIELD(14, 2), 0, ANY_FRAME},
  68. { "fec_coding", BITFIELD(7, 6, 1), 0, ANY_FRAME},
  69. { "frame", BYTEFIELD(12, 112), IS_STRING, ANY_FRAME},
  70. { "frame_hex", BYTEFIELD(12, 112), IS_HEXSTRING, ANY_FRAME},
  71. { "fromds", BITFIELD(13, 1, 1), 0, ANY_FRAME},
  72. { "header", BYTEFIELD(12, 0), IS_HEADER, ANY_FRAME},
  73. { "ht_length", BITFIELD(5, 0, 16), 0, ANY_FRAME},
  74. { "ie_20_40_bss_coexistence", IE(72), ANY_FRAME},
  75. { "ie_20_40_bss_intolerant_channel_report", IE(73), ANY_FRAME},
  76. { "ie_advertisement_protocol", IE(108), ANY_FRAME},
  77. { "ie_aid", IE(197), ANY_FRAME},
  78. { "ie_antenna", IE(64), ANY_FRAME},
  79. { "ie_ap_channel_report", IE(51), ANY_FRAME},
  80. { "ie_authenticated_mesh_peering_exchange", IE(139), ANY_FRAME},
  81. { "ie_beacon_timing", IE(120), ANY_FRAME},
  82. { "ie_bss_ac_access_delay", IE(68), ANY_FRAME},
  83. { "ie_bss_available_admission_capacity", IE(67), ANY_FRAME},
  84. { "ie_bss_average_access_delay", IE(63), ANY_FRAME},
  85. { "ie_bss_load", IE(11), ANY_FRAME},
  86. { "ie_bss_max_idle_period", IE(90), ANY_FRAME},
  87. { "ie_cf_parameter_set", IE(4), ANY_FRAME},
  88. { "ie_challenge_text", IE(16), ANY_FRAME},
  89. { "ie_channel_switch_announcement", IE(37), ANY_FRAME},
  90. { "ie_channel_switch_timing", IE(104), ANY_FRAME},
  91. { "ie_channel_switch_wrapper", IE(196), ANY_FRAME},
  92. { "ie_channel_usage", IE(97), ANY_FRAME},
  93. { "ie_collocated_interference_report", IE(96), ANY_FRAME},
  94. { "ie_congestion_notification", IE(116), ANY_FRAME},
  95. { "ie_country", IE(7), ANY_FRAME},
  96. { "ie_destination_uri", IE(141), ANY_FRAME},
  97. { "ie_diagnostic_report", IE(81), ANY_FRAME},
  98. { "ie_diagnostic_request", IE(80), ANY_FRAME},
  99. { "ie_dms_request", IE(99), ANY_FRAME},
  100. { "ie_dms_response", IE(100), ANY_FRAME},
  101. { "ie_dse_registered_location", IE(58), ANY_FRAME},
  102. { "ie_dsss_parameter_set", IE(3), ANY_FRAME},
  103. { "ie_edca_parameter_set", IE(12), ANY_FRAME},
  104. { "ie_emergency_alart_identifier", IE(112), ANY_FRAME},
  105. { "ie_erp_information", IE(42), ANY_FRAME},
  106. { "ie_event_report", IE(79), ANY_FRAME},
  107. { "ie_event_request", IE(78), ANY_FRAME},
  108. { "ie_expedited_bandwidth_request", IE(109), ANY_FRAME},
  109. { "ie_extended_bss_load", IE(193), ANY_FRAME},
  110. { "ie_extended_capabilities", IE(127), ANY_FRAME},
  111. { "ie_extended_channel_switch_announcement", IE(60), ANY_FRAME},
  112. { "ie_extended_supported_rates", IE(50), ANY_FRAME},
  113. { "ie_fast_bss_transition", IE(55), ANY_FRAME},
  114. { "ie_fh_parameter_set", IE(2), ANY_FRAME},
  115. { "ie_fms_descriptor", IE(86), ANY_FRAME},
  116. { "ie_fms_request", IE(87), ANY_FRAME},
  117. { "ie_fms_response", IE(88), ANY_FRAME},
  118. { "ie_gann", IE(125), ANY_FRAME},
  119. { "ie_he_capabilities", IE(255), ANY_FRAME},
  120. { "ie_hopping_pattern_parameters", IE(8), ANY_FRAME},
  121. { "ie_hopping_pattern_table", IE(9), ANY_FRAME},
  122. { "ie_ht_capabilities", IE(45), ANY_FRAME},
  123. { "ie_ht_operation", IE(61), ANY_FRAME},
  124. { "ie_ibss_dfs", IE(41), ANY_FRAME},
  125. { "ie_ibss_parameter_set", IE(6), ANY_FRAME},
  126. { "ie_interworking", IE(107), ANY_FRAME},
  127. { "ie_link_identifier", IE(101), ANY_FRAME},
  128. { "ie_location_parameters", IE(82), ANY_FRAME},
  129. { "ie_management_mic", IE(76), ANY_FRAME},
  130. { "ie_mccaop", IE(124), ANY_FRAME},
  131. { "ie_mccaop_advertisement", IE(123), ANY_FRAME},
  132. { "ie_mccaop_advertisement_overview", IE(174), ANY_FRAME},
  133. { "ie_mccaop_setup_reply", IE(122), ANY_FRAME},
  134. { "ie_mccaop_setup_request", IE(121), ANY_FRAME},
  135. { "ie_measurement_pilot_transmission", IE(66), ANY_FRAME},
  136. { "ie_measurement_report", IE(39), ANY_FRAME},
  137. { "ie_measurement_request", IE(38), ANY_FRAME},
  138. { "ie_mesh_awake_window", IE(119), ANY_FRAME},
  139. { "ie_mesh_channel_switch_parameters", IE(118), ANY_FRAME},
  140. { "ie_mesh_configuration", IE(113), ANY_FRAME},
  141. { "ie_mesh_id", IE(114), ANY_FRAME},
  142. { "ie_mesh_link_metric_report", IE(115), ANY_FRAME},
  143. { "ie_mesh_peering_management", IE(117), ANY_FRAME},
  144. { "ie_mic", IE(140), ANY_FRAME},
  145. { "ie_mobility_domain", IE(54), ANY_FRAME},
  146. { "ie_multiple_bssid", IE(71), ANY_FRAME},
  147. { "ie_multiple_bssid_index", IE(85), ANY_FRAME},
  148. { "ie_neighbor_report", IE(52), ANY_FRAME},
  149. { "ie_nontransmitted_bssid_capability", IE(83), ANY_FRAME},
  150. { "ie_operating_mode_notification", IE(199), ANY_FRAME},
  151. { "ie_overlapping_bss_scan_parameters", IE(74), ANY_FRAME},
  152. { "ie_perr", IE(132), ANY_FRAME},
  153. { "ie_power_capability", IE(33), ANY_FRAME},
  154. { "ie_power_constraint", IE(32), ANY_FRAME},
  155. { "ie_prep", IE(131), ANY_FRAME},
  156. { "ie_preq", IE(130), ANY_FRAME},
  157. { "ie_proxy_update", IE(137), ANY_FRAME},
  158. { "ie_proxy_update_confirmation", IE(138), ANY_FRAME},
  159. { "ie_pti_control", IE(105), ANY_FRAME},
  160. { "ie_qos_capability", IE(46), ANY_FRAME},
  161. { "ie_qos_map_set", IE(110), ANY_FRAME},
  162. { "ie_qos_traffic_capability", IE(89), ANY_FRAME},
  163. { "ie_quiet", IE(40), ANY_FRAME},
  164. { "ie_quiet_channel", IE(198), ANY_FRAME},
  165. { "ie_rann", IE(126), ANY_FRAME},
  166. { "ie_rcpi", IE(53), ANY_FRAME},
  167. { "ie_request", IE(10), ANY_FRAME},
  168. { "ie_ric_data", IE(57), ANY_FRAME},
  169. { "ie_ric_descriptor", IE(75), ANY_FRAME},
  170. { "ie_rm_enabled_capacities", IE(70), ANY_FRAME},
  171. { "ie_roaming_consortium", IE(111), ANY_FRAME},
  172. { "ie_rsn", IE(48), ANY_FRAME},
  173. { "ie_rsni", IE(65), ANY_FRAME},
  174. { "ie_schedule", IE(15), ANY_FRAME},
  175. { "ie_secondary_channel_offset", IE(62), ANY_FRAME},
  176. { "ie_ssid", IE(0), ANY_FRAME},
  177. { "ie_ssid_list", IE(84), ANY_FRAME},
  178. { "ie_supported_channels", IE(36), ANY_FRAME},
  179. { "ie_supported_operating_classes", IE(59), ANY_FRAME},
  180. { "ie_supported_rates", IE(1), ANY_FRAME},
  181. { "ie_table", BITFIELD(0, 0, 0), IE_TABLE, ANY_FRAME},
  182. { "ie_tclas", IE(14), ANY_FRAME},
  183. { "ie_tclas_processing", IE(44), ANY_FRAME},
  184. { "ie_tfs_request", IE(91), ANY_FRAME},
  185. { "ie_tfs_response", IE(92), ANY_FRAME},
  186. { "ie_tim", IE(5), ANY_FRAME},
  187. { "ie_tim_broadcast_request", IE(94), ANY_FRAME},
  188. { "ie_tim_broadcast_response", IE(95), ANY_FRAME},
  189. { "ie_time_advertisement", IE(69), ANY_FRAME},
  190. { "ie_time_zone", IE(98), ANY_FRAME},
  191. { "ie_timeout_interval", IE(56), ANY_FRAME},
  192. { "ie_tpc_report", IE(35), ANY_FRAME},
  193. { "ie_tpc_request", IE(34), ANY_FRAME},
  194. { "ie_tpu_buffer_status", IE(106), ANY_FRAME},
  195. { "ie_ts_delay", IE(43), ANY_FRAME},
  196. { "ie_tspec", IE(13), ANY_FRAME},
  197. { "ie_uapsd_coexistence", IE(142), ANY_FRAME},
  198. { "ie_vendor_specific", IE(221), ANY_FRAME},
  199. { "ie_vht_capabilities", IE(191), ANY_FRAME},
  200. { "ie_vht_operation", IE(192), ANY_FRAME},
  201. { "ie_vht_transmit_power_envelope", IE(195), ANY_FRAME},
  202. { "ie_wakeup_schedule", IE(102), ANY_FRAME},
  203. { "ie_wide_bandwidth_channel_switch", IE(194), ANY_FRAME},
  204. { "ie_wnm_sleep_mode", IE(93), ANY_FRAME},
  205. { "is_group", BITFIELD(1, 4, 1), 0, ANY_FRAME},
  206. { "legacy_length", BITFIELD(2, 0, 12), 0, ANY_FRAME},
  207. { "listen_interval", BYTEFIELD(38, 2), 0, FRAME_SUBTYPE_ASSOC_REQUEST},
  208. { "listen_interval", BYTEFIELD(38, 2), 0, FRAME_SUBTYPE_REASSOC_REQUEST},
  209. { "mcs", BITFIELD(4, 0, 7), 0, ANY_FRAME},
  210. { "moredata", BITFIELD(13, 5, 1), 0, ANY_FRAME},
  211. { "moreflag", BITFIELD(13, 2, 1), 0, ANY_FRAME},
  212. { "not_counding", BITFIELD(7, 1, 1), 0, ANY_FRAME},
  213. { "number", BYTEFIELD(34, 2), 0, ANY_FRAME},
  214. { "order", BITFIELD(13, 7, 1), 0, ANY_FRAME},
  215. { "protectedframe", BITFIELD(13, 6, 1), 0, ANY_FRAME},
  216. { "protocol", BITFIELD(12, 0, 2), 0, ANY_FRAME},
  217. { "pwrmgmt", BITFIELD(13, 4, 1), 0, ANY_FRAME},
  218. { "radio", BYTEFIELD(0, 12), IS_STRING, ANY_FRAME},
  219. { "rate", BITFIELD(1, 0, 4), 0, ANY_FRAME},
  220. { "reason", BYTEFIELD(36, 2), 0, FRAME_SUBTYPE_DEAUTHENTICATION},
  221. { "retry", BITFIELD(13, 3, 1), 0, ANY_FRAME},
  222. { "rssi", BYTEFIELD(0, 1), IS_SIGNED, ANY_FRAME},
  223. { "rxend_state", BITFIELD(8, 0, 8), 0, ANY_FRAME},
  224. { "sgi", BITFIELD(7, 7, 1), 0, ANY_FRAME},
  225. { "sig_mode", BITFIELD(1, 6, 2), 0, ANY_FRAME},
  226. { "smoothing", BITFIELD(7, 0, 1), 0, ANY_FRAME},
  227. { "srcmac", BYTEFIELD(16, 6), IS_STRING, ANY_FRAME},
  228. { "srcmac_hex", BYTEFIELD(16, 6), IS_HEXSTRING, ANY_FRAME},
  229. { "status", BYTEFIELD(38, 2), 0, FRAME_SUBTYPE_ASSOC_RESPONSE},
  230. { "status", BYTEFIELD(38, 2), 0, FRAME_SUBTYPE_REASSOC_RESPONSE},
  231. { "status", BYTEFIELD(40, 2), 0, FRAME_SUBTYPE_AUTHENTICATION},
  232. { "stbc", BITFIELD(7, 4, 2), 0, ANY_FRAME},
  233. { "subtype", BITFIELD(12, 4, 4), 0, ANY_FRAME},
  234. { "timestamp", BYTEFIELD(36, 8), IS_STRING, FRAME_SUBTYPE_PROBE_RESPONSE},
  235. { "timestamp", BYTEFIELD(36, 8), IS_STRING, FRAME_SUBTYPE_BEACON},
  236. { "tods", BITFIELD(13, 0, 1), 0, ANY_FRAME},
  237. { "type", BITFIELD(12, 2, 2), 0, ANY_FRAME}
  238. };
  239. static int8 variable_start[16] = {
  240. 4, // assoc req
  241. 6, // assoc response
  242. 10, // reassoc req
  243. 6, // reassoc resp
  244. 0, // probe req
  245. 12, // probe resp
  246. -1,
  247. -1,
  248. 12, // beacon
  249. -1, // ATIM
  250. 2, // Disassociation
  251. 6, // authentication
  252. 2, // Deauthentication
  253. 2, // action
  254. -1,
  255. -1
  256. };
  257. typedef struct {
  258. uint16 len;
  259. uint8 buf[];
  260. } packet_t;
  261. LROT_TABLE(packet_function);
  262. static void wifi_rx_cb(uint8 *buf, uint16 len) {
  263. if (len != sizeof(struct sniffer_buf2)) {
  264. return;
  265. }
  266. struct sniffer_buf2 *snb = (struct sniffer_buf2 *) buf;
  267. management_request_t *mgt = (management_request_t *) snb->buf;
  268. if (mon_offset > len) {
  269. return;
  270. }
  271. if ((buf[mon_offset] & mon_mask) != mon_value) {
  272. return;
  273. }
  274. packet_t *packet = (packet_t *) malloc(len + sizeof(packet_t));
  275. if (packet) {
  276. packet->len = len;
  277. memcpy(packet->buf, buf, len);
  278. if (!task_post_medium(tasknumber, (ETSParam) packet)) {
  279. free(packet);
  280. }
  281. }
  282. }
  283. static void monitor_task(os_param_t param, uint8_t prio)
  284. {
  285. packet_t *input = (packet_t *) param;
  286. (void) prio;
  287. lua_State *L = lua_getstate();
  288. if (recv_cb != LUA_NOREF) {
  289. lua_rawgeti(L, LUA_REGISTRYINDEX, recv_cb);
  290. packet_t *packet = (packet_t *) lua_newuserdata(L, input->len + sizeof(packet_t));
  291. packet->len = input->len;
  292. memcpy(packet->buf, input->buf, input->len);
  293. luaL_getmetatable(L, "wifi.packet");
  294. lua_setmetatable(L, -2);
  295. free(input);
  296. luaL_pcallx(L, 1, 0);
  297. } else {
  298. free(input);
  299. }
  300. }
  301. static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {
  302. /* relative string position: negative means back from end */
  303. if (pos < 0) pos += (ptrdiff_t)len + 1;
  304. return (pos >= 0) ? pos : 0;
  305. }
  306. static int packet_sub(lua_State *L, int buf_offset, int buf_length) {
  307. packet_t *packet = luaL_checkudata(L, 1, "wifi.packet");
  308. ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), buf_length);
  309. ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), buf_length);
  310. if (start < 1) start = 1;
  311. if (end > buf_length) end = buf_length;
  312. if (start <= end) {
  313. lua_pushlstring(L, packet->buf+start-1 + buf_offset, end-start+1);
  314. } else {
  315. lua_pushliteral(L, "");
  316. }
  317. return 1;
  318. }
  319. static int packet_frame_sub(lua_State *L) {
  320. return packet_sub(L, sizeof(struct RxControl), SNIFFER_BUF2_BUF_SIZE);
  321. }
  322. static int packet_radio_sub(lua_State *L) {
  323. return packet_sub(L, 0, sizeof(struct RxControl));
  324. }
  325. static void push_hex_string(lua_State *L, const uint8 *buf, int len, char *sep) {
  326. luaL_Buffer b;
  327. luaL_buffinit(L, &b);
  328. int i;
  329. for (i = 0; i < len; i++) {
  330. if (i && sep && *sep) {
  331. luaL_addstring(&b, sep);
  332. }
  333. char hex[3];
  334. uint8 c = buf[i];
  335. hex[0] = "0123456789abcdef"[c >> 4];
  336. hex[1] = "0123456789abcdef"[c & 0xf];
  337. hex[2] = 0;
  338. luaL_addstring(&b, hex);
  339. }
  340. luaL_pushresult(&b);
  341. }
  342. static void push_hex_string_colon(lua_State *L, const uint8 *buf, int len) {
  343. push_hex_string(L, buf, len, ":");
  344. }
  345. static int comparator(const void *typekey, const void *obj) {
  346. field_t *f = (field_t *) obj;
  347. const char *name = f->name;
  348. const typekey_t *tk = (const typekey_t *) typekey;
  349. const char *key = tk->key;
  350. if (!((uint32)key & 3) && !((uint32)name & 3)) {
  351. // Since all strings are 3 characters or more, can do accelerated first comparison
  352. uint32 key32 = htonl(*(uint32 *) key);
  353. uint32 name32 = htonl(*(uint32 *) name);
  354. if (key32 < name32) {
  355. return -1;
  356. }
  357. if (key32 > name32) {
  358. return 1;
  359. }
  360. }
  361. int rc = strcmp((const char *) key, name);
  362. if (rc) {
  363. return rc;
  364. }
  365. if (f->frametype == ANY_FRAME) {
  366. return 0;
  367. }
  368. return tk->frametype - f->frametype;
  369. }
  370. static bool push_field_value_string(lua_State *L, const uint8 *pkt,
  371. const uint8 *packet_end, const char *field) {
  372. const struct RxControl *rxc = (struct RxControl *) pkt;
  373. const management_request_t *mgt = (management_request_t *) (rxc + 1);
  374. typekey_t tk;
  375. tk.key = field;
  376. tk.frametype = mgt->framectrl.Subtype;
  377. field_t *f = bsearch(&tk, fields, sizeof(fields) / sizeof(fields[0]), sizeof(fields[0]), comparator);
  378. if (f) {
  379. if (f->opts == SINGLE_IE) {
  380. int varstart = variable_start[mgt->framectrl.Subtype];
  381. if (varstart >= 0) {
  382. const uint8 *var = (uint8 *) (mgt + 1) + varstart;
  383. while (var + 2 <= packet_end && var + 2 + var[1] <= packet_end) {
  384. if (*var == f->length) {
  385. lua_pushlstring(L, var + 2, var[1]);
  386. return true;
  387. }
  388. var += 2 + var[1];
  389. }
  390. }
  391. lua_pushnil(L);
  392. return true;
  393. }
  394. if (f->opts == IE_TABLE) {
  395. lua_newtable(L);
  396. int varstart = variable_start[mgt->framectrl.Subtype];
  397. if (varstart >= 0) {
  398. const uint8 *var = (uint8 *) (mgt + 1) + varstart;
  399. while (var + 2 <= packet_end && var + 2 + var[1] <= packet_end) {
  400. lua_pushlstring(L, var + 2, var[1]);
  401. lua_rawseti(L, -2, *var);
  402. var += 2 + var[1];
  403. }
  404. }
  405. return true;
  406. }
  407. if (f->opts == IS_STRING) {
  408. int use = f->length >> 3;
  409. const uint8 *start = pkt + (f->start >> 3);
  410. if (start + use > packet_end) {
  411. use = packet_end - start;
  412. }
  413. lua_pushlstring(L, start, use);
  414. return true;
  415. }
  416. if (f->opts == IS_HEXSTRING) {
  417. int use = f->length >> 3;
  418. const uint8 *start = pkt + (f->start >> 3);
  419. if (start + use > packet_end) {
  420. use = packet_end - start;
  421. }
  422. push_hex_string_colon(L, start, use);
  423. return true;
  424. }
  425. if (f->opts == IS_HEADER) {
  426. int varstart = variable_start[mgt->framectrl.Subtype];
  427. if (varstart >= 0) {
  428. lua_pushlstring(L, (const uint8 *) (mgt + 1), varstart);
  429. } else {
  430. lua_pushnil(L);
  431. }
  432. return true;
  433. }
  434. if (f->opts == 0 || f->opts == IS_SIGNED) {
  435. // bits start from the bottom of the byte.
  436. int value = 0;
  437. int bits = 0;
  438. int bitoff = f->start & 7;
  439. int byteoff = f->start >> 3;
  440. while (bits < f->length) {
  441. uint8 b = pkt[byteoff];
  442. value |= (b >> bitoff) << bits;
  443. bits += (8 - bitoff);
  444. bitoff = 0;
  445. byteoff++;
  446. }
  447. // get rid of excess bits
  448. value &= (1 << f->length) - 1;
  449. if (f->opts & IS_SIGNED) {
  450. if (value & (1 << (f->length - 1))) {
  451. value |= - (1 << f->length);
  452. }
  453. }
  454. lua_pushinteger(L, value);
  455. return true;
  456. }
  457. }
  458. return false;
  459. }
  460. static bool push_field_value_int(lua_State *L, management_request_t *mgt,
  461. const uint8 *packet_end, int field) {
  462. int varstart = variable_start[mgt->framectrl.Subtype];
  463. if (varstart >= 0) {
  464. uint8 *var = (uint8 *) (mgt + 1) + varstart;
  465. while (var + 2 <= packet_end && var + 2 + var[1] <= packet_end) {
  466. if (*var == field) {
  467. lua_pushlstring(L, var + 2, var[1]);
  468. return true;
  469. }
  470. var += var[1] + 2;
  471. }
  472. }
  473. return false;
  474. }
  475. static int packet_map_lookup(lua_State *L) {
  476. packet_t *packet = luaL_checkudata(L, 1, "wifi.packet");
  477. struct RxControl *rxc = (struct RxControl *) packet->buf;
  478. management_request_t *mgt = (management_request_t *) (rxc + 1);
  479. const uint8 *packet_end = packet->buf + packet->len;
  480. if ((void *) (mgt + 1) > (void *) packet_end) {
  481. return 0;
  482. }
  483. if (mgt->framectrl.Type != FRAME_TYPE_MANAGEMENT) {
  484. return 0;
  485. }
  486. if (lua_type(L, 2) == LUA_TNUMBER) {
  487. int field = luaL_checkinteger(L, 2);
  488. if (push_field_value_int(L, mgt, packet_end, field)) {
  489. return 1;
  490. }
  491. } else {
  492. const char *field = luaL_checkstring(L, 2);
  493. if (push_field_value_string(L, packet->buf, packet_end, field)) {
  494. return 1;
  495. }
  496. // Now search the packet function map
  497. lua_pushrotable(L, LROT_TABLEREF(packet_function));
  498. lua_getfield(L, -1, field);
  499. if (!lua_isnil(L, -1)) {
  500. return 1;
  501. }
  502. }
  503. return 0;
  504. }
  505. static int packet_byte(lua_State *L, int buf_offset, int buf_length) {
  506. packet_t *packet = luaL_checkudata(L, 1, "wifi.packet");
  507. int offset = luaL_checkinteger(L, 2);
  508. if (offset < 1 || offset > buf_length) {
  509. return 0;
  510. }
  511. lua_pushinteger(L, packet->buf[offset - 1 + buf_offset]);
  512. return 1;
  513. }
  514. static int packet_frame_byte(lua_State *L) {
  515. return packet_byte(L, sizeof(struct RxControl), SNIFFER_BUF2_BUF_SIZE);
  516. }
  517. static int packet_radio_byte(lua_State *L) {
  518. return packet_byte(L, 0, sizeof(struct RxControl));
  519. }
  520. static int packet_subhex(lua_State *L, int buf_offset, int buf_length) {
  521. packet_t *packet = luaL_checkudata(L, 1, "wifi.packet");
  522. ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), buf_length);
  523. ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), buf_length);
  524. const char *sep = luaL_optstring(L, 4, "");
  525. if (start < 1) start = 1;
  526. if (end > buf_length) end = buf_length;
  527. if (start <= end) {
  528. luaL_Buffer b;
  529. luaL_buffinit(L, &b);
  530. int i;
  531. for (i = start - 1; i < end; i++) {
  532. char hex[3];
  533. if (i >= start) {
  534. luaL_addstring(&b, sep);
  535. }
  536. uint8 c = packet->buf[i + buf_offset];
  537. hex[0] = "0123456789abcdef"[c >> 4];
  538. hex[1] = "0123456789abcdef"[c & 0xf];
  539. hex[2] = 0;
  540. luaL_addstring(&b, hex);
  541. }
  542. luaL_pushresult(&b);
  543. } else {
  544. lua_pushliteral(L, "");
  545. }
  546. return 1;
  547. }
  548. static int packet_frame_subhex(lua_State *L) {
  549. return packet_subhex(L, sizeof(struct RxControl), SNIFFER_BUF2_BUF_SIZE);
  550. }
  551. static int packet_radio_subhex(lua_State *L) {
  552. return packet_subhex(L, 0, sizeof(struct RxControl));
  553. }
  554. static void start_actually_monitoring() {
  555. wifi_set_channel(1);
  556. wifi_promiscuous_enable(1);
  557. }
  558. static int wifi_event_monitor_handle_event_cb_hook(System_Event_t *evt)
  559. {
  560. if (evt->event == EVENT_STAMODE_DISCONNECTED) {
  561. if (on_disconnected) {
  562. on_disconnected();
  563. on_disconnected = NULL;
  564. return 1; // We did handle the event
  565. }
  566. }
  567. return 0; // We did not handle the event
  568. }
  569. // This is a bit ugly as we have to use a bit of the event monitor infrastructure
  570. #ifdef WIFI_SDK_EVENT_MONITOR_ENABLE
  571. extern void wifi_event_monitor_register_hook(int (*fn)(System_Event_t*));
  572. static void eventmon_setup() {
  573. wifi_event_monitor_register_hook(wifi_event_monitor_handle_event_cb_hook);
  574. }
  575. #else
  576. static void wifi_event_monitor_handle_event_cb(System_Event_t *evt)
  577. {
  578. wifi_event_monitor_handle_event_cb_hook(evt);
  579. }
  580. static void eventmon_setup() {
  581. wifi_set_event_handler_cb(wifi_event_monitor_handle_event_cb);
  582. }
  583. #endif
  584. static void eventmon_call_on_disconnected(void (*fn)(void)) {
  585. on_disconnected = fn;
  586. }
  587. static int wifi_monitor_start(lua_State *L) {
  588. int argno = 1;
  589. if (lua_type(L, argno) == LUA_TNUMBER) {
  590. int offset = luaL_checkinteger(L, argno);
  591. argno++;
  592. if (lua_type(L, argno) == LUA_TNUMBER) {
  593. int value = luaL_checkinteger(L, argno);
  594. int mask = 0xff;
  595. argno++;
  596. if (lua_type(L, argno) == LUA_TNUMBER) {
  597. mask = luaL_checkinteger(L, argno);
  598. argno++;
  599. }
  600. mon_offset = offset - 1;
  601. mon_value = value;
  602. mon_mask = mask;
  603. } else {
  604. return luaL_error(L, "Must supply offset and value");
  605. }
  606. } else {
  607. // Management frames by default
  608. mon_offset = 12;
  609. mon_value = 0x00;
  610. mon_mask = 0x0C;
  611. }
  612. if (lua_isfunction(L, argno))
  613. {
  614. lua_pushvalue(L, argno); // copy argument (func) to the top of stack
  615. recv_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  616. uint8 connect_status = wifi_station_get_connect_status();
  617. wifi_station_set_auto_connect(0);
  618. wifi_set_opmode_current(1);
  619. wifi_promiscuous_enable(0);
  620. wifi_station_disconnect();
  621. wifi_set_promiscuous_rx_cb(wifi_rx_cb);
  622. // Now we have to wait until we get the EVENT_STAMODE_DISCONNECTED event
  623. // before we can go further.
  624. if (connect_status == STATION_IDLE) {
  625. start_actually_monitoring();
  626. } else {
  627. eventmon_call_on_disconnected(start_actually_monitoring);
  628. }
  629. return 0;
  630. }
  631. return luaL_error(L, "Missing callback");
  632. }
  633. static int wifi_monitor_channel(lua_State *L) {
  634. lua_pushinteger(L, wifi_get_channel());
  635. if (lua_type(L, 1) == LUA_TNUMBER) {
  636. int channel = luaL_checkinteger(L, 1);
  637. if (channel < 1 || channel > 15) {
  638. return luaL_error(L, "Channel number (%d) is out of range", channel);
  639. }
  640. wifi_set_channel(channel);
  641. }
  642. return 1;
  643. }
  644. static int wifi_monitor_stop(lua_State *L) {
  645. wifi_promiscuous_enable(0);
  646. wifi_set_opmode_current(1);
  647. luaL_unref(L, LUA_REGISTRYINDEX, recv_cb);
  648. recv_cb = LUA_NOREF;
  649. return 0;
  650. }
  651. LROT_BEGIN(packet_function, NULL, 0)
  652. LROT_FUNCENTRY( radio_byte, packet_radio_byte )
  653. LROT_FUNCENTRY( frame_byte, packet_frame_byte )
  654. LROT_FUNCENTRY( radio_sub, packet_radio_sub )
  655. LROT_FUNCENTRY( frame_sub, packet_frame_sub )
  656. LROT_FUNCENTRY( radio_subhex, packet_radio_subhex )
  657. LROT_FUNCENTRY( frame_subhex, packet_frame_subhex )
  658. LROT_END(packet_function, NULL, 0)
  659. LROT_BEGIN(packet, NULL, LROT_MASK_INDEX)
  660. LROT_FUNCENTRY( __index, packet_map_lookup )
  661. LROT_END(packet, NULL, LROT_MASK_INDEX)
  662. // Module function map
  663. LROT_BEGIN(wifi_monitor, NULL, 0)
  664. LROT_FUNCENTRY( start, wifi_monitor_start )
  665. LROT_FUNCENTRY( stop, wifi_monitor_stop )
  666. LROT_FUNCENTRY( channel, wifi_monitor_channel )
  667. LROT_END(wifi_monitor, NULL, 0)
  668. int wifi_monitor_init(lua_State *L)
  669. {
  670. luaL_rometatable(L, "wifi.packet", LROT_TABLEREF(packet));
  671. tasknumber = task_get_id(monitor_task);
  672. eventmon_setup();
  673. #ifdef CHECK_TABLE_IN_ORDER
  674. // verify that the table is in order
  675. typekey_t tk;
  676. tk.key = "";
  677. tk.frametype = 0;
  678. int i;
  679. for (i = 0; i < sizeof(fields) / sizeof(fields[0]); i++) {
  680. if (comparator(&tk, &fields[i]) >= 0) {
  681. dbg_printf("Wrong order: %s,%d should be after %s,%d\n", tk.key, tk.frametype, fields[i].name, fields[i].frametype);
  682. }
  683. tk.key = fields[i].name;
  684. tk.frametype = fields[i].frametype;
  685. }
  686. #endif
  687. return 0;
  688. }