inject.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "common/defs.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "crypto/aes_wrap.h"
  19. #include "wlantest.h"
  20. static int inject_frame(int s, const void *data, size_t len)
  21. {
  22. #define IEEE80211_RADIOTAP_F_FRAG 0x08
  23. unsigned char rtap_hdr[] = {
  24. 0x00, 0x00, /* radiotap version */
  25. 0x0e, 0x00, /* radiotap length */
  26. 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
  27. IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
  28. 0x00, /* padding */
  29. 0x00, 0x00, /* RX and TX flags to indicate that */
  30. 0x00, 0x00, /* this is the injected frame directly */
  31. };
  32. struct iovec iov[2] = {
  33. {
  34. .iov_base = &rtap_hdr,
  35. .iov_len = sizeof(rtap_hdr),
  36. },
  37. {
  38. .iov_base = (void *) data,
  39. .iov_len = len,
  40. }
  41. };
  42. struct msghdr msg = {
  43. .msg_name = NULL,
  44. .msg_namelen = 0,
  45. .msg_iov = iov,
  46. .msg_iovlen = 2,
  47. .msg_control = NULL,
  48. .msg_controllen = 0,
  49. .msg_flags = 0,
  50. };
  51. int ret;
  52. ret = sendmsg(s, &msg, 0);
  53. if (ret < 0)
  54. perror("sendmsg");
  55. return ret;
  56. }
  57. static int is_robust_mgmt(u8 *frame, size_t len)
  58. {
  59. struct ieee80211_mgmt *mgmt;
  60. u16 fc, stype;
  61. if (len < 24)
  62. return 0;
  63. mgmt = (struct ieee80211_mgmt *) frame;
  64. fc = le_to_host16(mgmt->frame_control);
  65. if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT)
  66. return 0;
  67. stype = WLAN_FC_GET_STYPE(fc);
  68. if (stype == WLAN_FC_STYPE_DEAUTH || stype == WLAN_FC_STYPE_DISASSOC)
  69. return 1;
  70. if (stype == WLAN_FC_STYPE_ACTION) {
  71. if (len < 25)
  72. return 0;
  73. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. static int wlantest_inject_bip(struct wlantest *wt, struct wlantest_bss *bss,
  79. u8 *frame, size_t len, int incorrect_key)
  80. {
  81. u8 *prot, *pos, *buf;
  82. u8 mic[16];
  83. u8 dummy[16];
  84. int ret;
  85. u16 fc;
  86. struct ieee80211_hdr *hdr;
  87. size_t plen;
  88. if (!bss->igtk_set[bss->igtk_idx])
  89. return -1;
  90. plen = len + 18;
  91. prot = os_malloc(plen);
  92. if (prot == NULL)
  93. return -1;
  94. os_memcpy(prot, frame, len);
  95. pos = prot + len;
  96. *pos++ = WLAN_EID_MMIE;
  97. *pos++ = 16;
  98. WPA_PUT_LE16(pos, bss->igtk_idx);
  99. pos += 2;
  100. inc_byte_array(bss->ipn[bss->igtk_idx], 6);
  101. os_memcpy(pos, bss->ipn[bss->igtk_idx], 6);
  102. pos += 6;
  103. os_memset(pos, 0, 8); /* MIC */
  104. buf = os_malloc(plen + 20 - 24);
  105. if (buf == NULL) {
  106. os_free(prot);
  107. return -1;
  108. }
  109. /* BIP AAD: FC(masked) A1 A2 A3 */
  110. hdr = (struct ieee80211_hdr *) frame;
  111. fc = le_to_host16(hdr->frame_control);
  112. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  113. WPA_PUT_LE16(buf, fc);
  114. os_memcpy(buf + 2, hdr->addr1, 3 * ETH_ALEN);
  115. os_memcpy(buf + 20, prot + 24, plen - 24);
  116. wpa_hexdump(MSG_MSGDUMP, "BIP: AAD|Body(masked)", buf, plen + 20 - 24);
  117. /* MIC = L(AES-128-CMAC(AAD || Frame Body(masked)), 0, 64) */
  118. os_memset(dummy, 0x11, sizeof(dummy));
  119. if (omac1_aes_128(incorrect_key ? dummy : bss->igtk[bss->igtk_idx],
  120. buf, plen + 20 - 24, mic) < 0) {
  121. os_free(prot);
  122. os_free(buf);
  123. return -1;
  124. }
  125. os_free(buf);
  126. os_memcpy(pos, mic, 8);
  127. wpa_hexdump(MSG_DEBUG, "BIP MMIE MIC", pos, 8);
  128. ret = inject_frame(wt->monitor_sock, prot, plen);
  129. os_free(prot);
  130. return (ret < 0) ? -1 : 0;
  131. }
  132. static int wlantest_inject_prot_bc(struct wlantest *wt,
  133. struct wlantest_bss *bss,
  134. u8 *frame, size_t len, int incorrect_key)
  135. {
  136. u8 *crypt;
  137. size_t crypt_len;
  138. int ret;
  139. u8 dummy[64];
  140. u8 *pn;
  141. struct ieee80211_hdr *hdr;
  142. u16 fc;
  143. int hdrlen;
  144. hdr = (struct ieee80211_hdr *) frame;
  145. hdrlen = 24;
  146. fc = le_to_host16(hdr->frame_control);
  147. if (!bss->gtk_len[bss->gtk_idx])
  148. return -1;
  149. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  150. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  151. hdrlen += ETH_ALEN;
  152. pn = bss->rsc[bss->gtk_idx];
  153. inc_byte_array(pn, 6);
  154. os_memset(dummy, 0x11, sizeof(dummy));
  155. if (bss->group_cipher == WPA_CIPHER_TKIP)
  156. crypt = tkip_encrypt(incorrect_key ? dummy :
  157. bss->gtk[bss->gtk_idx],
  158. frame, len, hdrlen, NULL, pn,
  159. bss->gtk_idx, &crypt_len);
  160. else
  161. crypt = ccmp_encrypt(incorrect_key ? dummy :
  162. bss->gtk[bss->gtk_idx],
  163. frame, len, hdrlen, NULL, pn,
  164. bss->gtk_idx, &crypt_len);
  165. if (crypt == NULL)
  166. return -1;
  167. ret = inject_frame(wt->monitor_sock, crypt, crypt_len);
  168. os_free(crypt);
  169. return (ret < 0) ? -1 : 0;
  170. }
  171. static int wlantest_inject_prot(struct wlantest *wt, struct wlantest_bss *bss,
  172. struct wlantest_sta *sta, u8 *frame,
  173. size_t len, int incorrect_key)
  174. {
  175. u8 *crypt;
  176. size_t crypt_len;
  177. int ret;
  178. u8 dummy[64];
  179. u8 *pn;
  180. struct ieee80211_hdr *hdr;
  181. u16 fc;
  182. int tid = 0;
  183. u8 *qos = NULL;
  184. int hdrlen;
  185. struct wlantest_tdls *tdls = NULL;
  186. const u8 *tk = NULL;
  187. hdr = (struct ieee80211_hdr *) frame;
  188. hdrlen = 24;
  189. fc = le_to_host16(hdr->frame_control);
  190. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
  191. (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == 0) {
  192. struct wlantest_sta *sta2;
  193. bss = bss_get(wt, hdr->addr3);
  194. if (bss == NULL) {
  195. wpa_printf(MSG_DEBUG, "No BSS found for TDLS "
  196. "injection");
  197. return -1;
  198. }
  199. sta = sta_find(bss, hdr->addr2);
  200. sta2 = sta_find(bss, hdr->addr1);
  201. if (sta == NULL || sta2 == NULL) {
  202. wpa_printf(MSG_DEBUG, "No stations found for TDLS "
  203. "injection");
  204. return -1;
  205. }
  206. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list)
  207. {
  208. if ((tdls->init == sta && tdls->resp == sta2) ||
  209. (tdls->init == sta2 && tdls->resp == sta)) {
  210. if (!tdls->link_up)
  211. wpa_printf(MSG_DEBUG, "TDLS: Link not "
  212. "up, but injecting Data "
  213. "frame on direct link");
  214. tk = tdls->tpk.tk;
  215. break;
  216. }
  217. }
  218. }
  219. if (tk == NULL && sta == NULL) {
  220. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
  221. return wlantest_inject_bip(wt, bss, frame, len,
  222. incorrect_key);
  223. return wlantest_inject_prot_bc(wt, bss, frame, len,
  224. incorrect_key);
  225. }
  226. if (tk == NULL && !sta->ptk_set) {
  227. wpa_printf(MSG_DEBUG, "No key known for injection");
  228. return -1;
  229. }
  230. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
  231. tid = 16;
  232. else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
  233. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  234. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  235. hdrlen += ETH_ALEN;
  236. if (WLAN_FC_GET_STYPE(fc) & 0x08) {
  237. qos = frame + hdrlen;
  238. hdrlen += 2;
  239. tid = qos[0] & 0x0f;
  240. }
  241. }
  242. if (tk) {
  243. if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0)
  244. pn = tdls->rsc_init[tid];
  245. else
  246. pn = tdls->rsc_resp[tid];
  247. } else if (os_memcmp(hdr->addr2, bss->bssid, ETH_ALEN) == 0)
  248. pn = sta->rsc_fromds[tid];
  249. else
  250. pn = sta->rsc_tods[tid];
  251. inc_byte_array(pn, 6);
  252. os_memset(dummy, 0x11, sizeof(dummy));
  253. if (tk)
  254. crypt = ccmp_encrypt(incorrect_key ? dummy : tk,
  255. frame, len, hdrlen, qos, pn, 0,
  256. &crypt_len);
  257. else if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
  258. crypt = tkip_encrypt(incorrect_key ? dummy : sta->ptk.tk1,
  259. frame, len, hdrlen, qos, pn, 0,
  260. &crypt_len);
  261. else
  262. crypt = ccmp_encrypt(incorrect_key ? dummy : sta->ptk.tk1,
  263. frame, len, hdrlen, qos, pn, 0,
  264. &crypt_len);
  265. if (crypt == NULL) {
  266. wpa_printf(MSG_DEBUG, "Frame encryption failed");
  267. return -1;
  268. }
  269. wpa_hexdump(MSG_DEBUG, "Inject frame (encrypted)", crypt, crypt_len);
  270. ret = inject_frame(wt->monitor_sock, crypt, crypt_len);
  271. os_free(crypt);
  272. wpa_printf(MSG_DEBUG, "inject_frame for protected frame: %d", ret);
  273. return (ret < 0) ? -1 : 0;
  274. }
  275. int wlantest_inject(struct wlantest *wt, struct wlantest_bss *bss,
  276. struct wlantest_sta *sta, u8 *frame, size_t len,
  277. enum wlantest_inject_protection prot)
  278. {
  279. int ret;
  280. struct ieee80211_hdr *hdr;
  281. u16 fc;
  282. int protectable, protect = 0;
  283. wpa_hexdump(MSG_DEBUG, "Inject frame", frame, len);
  284. if (wt->monitor_sock < 0) {
  285. wpa_printf(MSG_INFO, "Cannot inject frames when monitor "
  286. "interface is not in use");
  287. return -1;
  288. }
  289. hdr = (struct ieee80211_hdr *) frame;
  290. fc = le_to_host16(hdr->frame_control);
  291. protectable = WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA ||
  292. is_robust_mgmt(frame, len);
  293. if (prot == WLANTEST_INJECT_PROTECTED ||
  294. prot == WLANTEST_INJECT_INCORRECT_KEY) {
  295. if (!sta &&
  296. ((WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  297. !bss->igtk_set[bss->igtk_idx]) ||
  298. (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
  299. !bss->gtk_len[bss->gtk_idx]))) {
  300. wpa_printf(MSG_INFO, "No GTK/IGTK known for "
  301. MACSTR " to protect the injected "
  302. "frame", MAC2STR(bss->bssid));
  303. return -1;
  304. }
  305. if (sta && !sta->ptk_set) {
  306. wpa_printf(MSG_INFO, "No PTK known for the STA " MACSTR
  307. " to encrypt the injected frame",
  308. MAC2STR(sta->addr));
  309. return -1;
  310. }
  311. protect = 1;
  312. } else if (protectable && prot != WLANTEST_INJECT_UNPROTECTED) {
  313. if (sta && sta->ptk_set)
  314. protect = 1;
  315. else if (!sta) {
  316. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
  317. bss->gtk_len[bss->gtk_idx])
  318. protect = 1;
  319. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  320. bss->igtk_set[bss->igtk_idx])
  321. protect = 1;
  322. }
  323. }
  324. if (protect)
  325. return wlantest_inject_prot(
  326. wt, bss, sta, frame, len,
  327. prot == WLANTEST_INJECT_INCORRECT_KEY);
  328. ret = inject_frame(wt->monitor_sock, frame, len);
  329. wpa_printf(MSG_DEBUG, "inject_frame for unprotected frame: %d", ret);
  330. return (ret < 0) ? -1 : 0;
  331. }