inject.c 8.4 KB

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