inject.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * wlantest frame injection
  3. * Copyright (c) 2010-2015, 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. wpa_printf(MSG_ERROR, "sendmsg: %s", strerror(errno));
  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;
  76. u8 dummy[32];
  77. int ret;
  78. size_t plen;
  79. if (!bss->igtk_len[bss->igtk_idx])
  80. return -1;
  81. os_memset(dummy, 0x11, sizeof(dummy));
  82. inc_byte_array(bss->ipn[bss->igtk_idx], 6);
  83. prot = bip_protect(incorrect_key ? dummy : bss->igtk[bss->igtk_idx],
  84. bss->igtk_len[bss->igtk_idx],
  85. frame, len, bss->ipn[bss->igtk_idx],
  86. bss->igtk_idx, &plen);
  87. if (prot == NULL)
  88. return -1;
  89. ret = inject_frame(wt->monitor_sock, prot, plen);
  90. os_free(prot);
  91. return (ret < 0) ? -1 : 0;
  92. }
  93. static int wlantest_inject_prot_bc(struct wlantest *wt,
  94. struct wlantest_bss *bss,
  95. u8 *frame, size_t len, int incorrect_key)
  96. {
  97. u8 *crypt;
  98. size_t crypt_len;
  99. int ret;
  100. u8 dummy[64];
  101. u8 *pn;
  102. struct ieee80211_hdr *hdr;
  103. u16 fc;
  104. int hdrlen;
  105. hdr = (struct ieee80211_hdr *) frame;
  106. hdrlen = 24;
  107. fc = le_to_host16(hdr->frame_control);
  108. if (!bss->gtk_len[bss->gtk_idx])
  109. return -1;
  110. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  111. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  112. hdrlen += ETH_ALEN;
  113. pn = bss->rsc[bss->gtk_idx];
  114. inc_byte_array(pn, 6);
  115. os_memset(dummy, 0x11, sizeof(dummy));
  116. if (bss->group_cipher == WPA_CIPHER_TKIP)
  117. crypt = tkip_encrypt(incorrect_key ? dummy :
  118. bss->gtk[bss->gtk_idx],
  119. frame, len, hdrlen, NULL, pn,
  120. bss->gtk_idx, &crypt_len);
  121. else
  122. crypt = ccmp_encrypt(incorrect_key ? dummy :
  123. bss->gtk[bss->gtk_idx],
  124. frame, len, hdrlen, NULL, pn,
  125. bss->gtk_idx, &crypt_len);
  126. if (crypt == NULL)
  127. return -1;
  128. ret = inject_frame(wt->monitor_sock, crypt, crypt_len);
  129. os_free(crypt);
  130. return (ret < 0) ? -1 : 0;
  131. }
  132. static int wlantest_inject_prot(struct wlantest *wt, struct wlantest_bss *bss,
  133. struct wlantest_sta *sta, u8 *frame,
  134. 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 tid = 0;
  144. u8 *qos = NULL;
  145. int hdrlen;
  146. struct wlantest_tdls *tdls = NULL;
  147. const u8 *tk = NULL;
  148. hdr = (struct ieee80211_hdr *) frame;
  149. hdrlen = 24;
  150. fc = le_to_host16(hdr->frame_control);
  151. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
  152. (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == 0) {
  153. struct wlantest_sta *sta2;
  154. bss = bss_get(wt, hdr->addr3);
  155. if (bss == NULL) {
  156. wpa_printf(MSG_DEBUG, "No BSS found for TDLS "
  157. "injection");
  158. return -1;
  159. }
  160. sta = sta_find(bss, hdr->addr2);
  161. sta2 = sta_find(bss, hdr->addr1);
  162. if (sta == NULL || sta2 == NULL) {
  163. wpa_printf(MSG_DEBUG, "No stations found for TDLS "
  164. "injection");
  165. return -1;
  166. }
  167. dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list)
  168. {
  169. if ((tdls->init == sta && tdls->resp == sta2) ||
  170. (tdls->init == sta2 && tdls->resp == sta)) {
  171. if (!tdls->link_up)
  172. wpa_printf(MSG_DEBUG, "TDLS: Link not "
  173. "up, but injecting Data "
  174. "frame on direct link");
  175. tk = tdls->tpk.tk;
  176. break;
  177. }
  178. }
  179. }
  180. if (tk == NULL && sta == NULL) {
  181. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
  182. return wlantest_inject_bip(wt, bss, frame, len,
  183. incorrect_key);
  184. return wlantest_inject_prot_bc(wt, bss, frame, len,
  185. incorrect_key);
  186. }
  187. if (tk == NULL && !sta->ptk_set) {
  188. wpa_printf(MSG_DEBUG, "No key known for injection");
  189. return -1;
  190. }
  191. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
  192. tid = 16;
  193. else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
  194. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  195. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  196. hdrlen += ETH_ALEN;
  197. if (WLAN_FC_GET_STYPE(fc) & 0x08) {
  198. qos = frame + hdrlen;
  199. hdrlen += 2;
  200. tid = qos[0] & 0x0f;
  201. }
  202. }
  203. if (tk) {
  204. if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0)
  205. pn = tdls->rsc_init[tid];
  206. else
  207. pn = tdls->rsc_resp[tid];
  208. } else if (os_memcmp(hdr->addr2, bss->bssid, ETH_ALEN) == 0)
  209. pn = sta->rsc_fromds[tid];
  210. else
  211. pn = sta->rsc_tods[tid];
  212. inc_byte_array(pn, 6);
  213. os_memset(dummy, 0x11, sizeof(dummy));
  214. if (tk)
  215. crypt = ccmp_encrypt(incorrect_key ? dummy : tk,
  216. frame, len, hdrlen, qos, pn, 0,
  217. &crypt_len);
  218. else if (sta->pairwise_cipher == WPA_CIPHER_TKIP)
  219. crypt = tkip_encrypt(incorrect_key ? dummy : sta->ptk.tk,
  220. frame, len, hdrlen, qos, pn, 0,
  221. &crypt_len);
  222. else
  223. crypt = ccmp_encrypt(incorrect_key ? dummy : sta->ptk.tk,
  224. frame, len, hdrlen, qos, pn, 0,
  225. &crypt_len);
  226. if (crypt == NULL) {
  227. wpa_printf(MSG_DEBUG, "Frame encryption failed");
  228. return -1;
  229. }
  230. wpa_hexdump(MSG_DEBUG, "Inject frame (encrypted)", crypt, crypt_len);
  231. ret = inject_frame(wt->monitor_sock, crypt, crypt_len);
  232. os_free(crypt);
  233. wpa_printf(MSG_DEBUG, "inject_frame for protected frame: %d", ret);
  234. return (ret < 0) ? -1 : 0;
  235. }
  236. int wlantest_inject(struct wlantest *wt, struct wlantest_bss *bss,
  237. struct wlantest_sta *sta, u8 *frame, size_t len,
  238. enum wlantest_inject_protection prot)
  239. {
  240. int ret;
  241. struct ieee80211_hdr *hdr;
  242. u16 fc;
  243. int protectable, protect = 0;
  244. wpa_hexdump(MSG_DEBUG, "Inject frame", frame, len);
  245. if (wt->monitor_sock < 0) {
  246. wpa_printf(MSG_INFO, "Cannot inject frames when monitor "
  247. "interface is not in use");
  248. return -1;
  249. }
  250. if (prot != WLANTEST_INJECT_UNPROTECTED && bss == NULL) {
  251. wpa_printf(MSG_INFO, "No BSS information to inject "
  252. "protected frames");
  253. return -1;
  254. }
  255. hdr = (struct ieee80211_hdr *) frame;
  256. fc = le_to_host16(hdr->frame_control);
  257. protectable = WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA ||
  258. is_robust_mgmt(frame, len);
  259. if ((prot == WLANTEST_INJECT_PROTECTED ||
  260. prot == WLANTEST_INJECT_INCORRECT_KEY) && bss) {
  261. if (!sta &&
  262. ((WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  263. !bss->igtk_len[bss->igtk_idx]) ||
  264. (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
  265. !bss->gtk_len[bss->gtk_idx]))) {
  266. wpa_printf(MSG_INFO, "No GTK/IGTK known for "
  267. MACSTR " to protect the injected "
  268. "frame", MAC2STR(bss->bssid));
  269. return -1;
  270. }
  271. if (sta && !sta->ptk_set) {
  272. wpa_printf(MSG_INFO, "No PTK known for the STA " MACSTR
  273. " to encrypt the injected frame",
  274. MAC2STR(sta->addr));
  275. return -1;
  276. }
  277. protect = 1;
  278. } else if (protectable && prot != WLANTEST_INJECT_UNPROTECTED && bss) {
  279. if (sta && sta->ptk_set)
  280. protect = 1;
  281. else if (!sta) {
  282. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
  283. bss->gtk_len[bss->gtk_idx])
  284. protect = 1;
  285. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  286. bss->igtk_len[bss->igtk_idx])
  287. protect = 1;
  288. }
  289. }
  290. if (protect && bss)
  291. return wlantest_inject_prot(
  292. wt, bss, sta, frame, len,
  293. prot == WLANTEST_INJECT_INCORRECT_KEY);
  294. ret = inject_frame(wt->monitor_sock, frame, len);
  295. wpa_printf(MSG_DEBUG, "inject_frame for unprotected frame: %d", ret);
  296. return (ret < 0) ? -1 : 0;
  297. }