inject.c 9.3 KB

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