wpa_ie.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * wpa_supplicant - WPA/RSN IE and KDE processing
  3. * Copyright (c) 2003-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 "includes.h"
  9. #include "common.h"
  10. #include "wpa.h"
  11. #include "pmksa_cache.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "wpa_i.h"
  14. #include "wpa_ie.h"
  15. /**
  16. * wpa_parse_wpa_ie - Parse WPA/RSN IE
  17. * @wpa_ie: Pointer to WPA or RSN IE
  18. * @wpa_ie_len: Length of the WPA/RSN IE
  19. * @data: Pointer to data area for parsing results
  20. * Returns: 0 on success, -1 on failure
  21. *
  22. * Parse the contents of WPA or RSN IE and write the parsed data into data.
  23. */
  24. int wpa_parse_wpa_ie(const u8 *wpa_ie, size_t wpa_ie_len,
  25. struct wpa_ie_data *data)
  26. {
  27. if (wpa_ie_len >= 1 && wpa_ie[0] == WLAN_EID_RSN)
  28. return wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, data);
  29. if (wpa_ie_len >= 6 && wpa_ie[0] == WLAN_EID_VENDOR_SPECIFIC &&
  30. wpa_ie[1] >= 4 && WPA_GET_BE32(&wpa_ie[2]) == OSEN_IE_VENDOR_TYPE)
  31. return wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, data);
  32. else
  33. return wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, data);
  34. }
  35. static int wpa_gen_wpa_ie_wpa(u8 *wpa_ie, size_t wpa_ie_len,
  36. int pairwise_cipher, int group_cipher,
  37. int key_mgmt)
  38. {
  39. u8 *pos;
  40. struct wpa_ie_hdr *hdr;
  41. u32 suite;
  42. if (wpa_ie_len < sizeof(*hdr) + WPA_SELECTOR_LEN +
  43. 2 + WPA_SELECTOR_LEN + 2 + WPA_SELECTOR_LEN)
  44. return -1;
  45. hdr = (struct wpa_ie_hdr *) wpa_ie;
  46. hdr->elem_id = WLAN_EID_VENDOR_SPECIFIC;
  47. RSN_SELECTOR_PUT(hdr->oui, WPA_OUI_TYPE);
  48. WPA_PUT_LE16(hdr->version, WPA_VERSION);
  49. pos = (u8 *) (hdr + 1);
  50. suite = wpa_cipher_to_suite(WPA_PROTO_WPA, group_cipher);
  51. if (suite == 0) {
  52. wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
  53. group_cipher);
  54. return -1;
  55. }
  56. RSN_SELECTOR_PUT(pos, suite);
  57. pos += WPA_SELECTOR_LEN;
  58. *pos++ = 1;
  59. *pos++ = 0;
  60. suite = wpa_cipher_to_suite(WPA_PROTO_WPA, pairwise_cipher);
  61. if (suite == 0 ||
  62. (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
  63. pairwise_cipher != WPA_CIPHER_NONE)) {
  64. wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
  65. pairwise_cipher);
  66. return -1;
  67. }
  68. RSN_SELECTOR_PUT(pos, suite);
  69. pos += WPA_SELECTOR_LEN;
  70. *pos++ = 1;
  71. *pos++ = 0;
  72. if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
  73. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
  74. } else if (key_mgmt == WPA_KEY_MGMT_PSK) {
  75. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
  76. } else if (key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  77. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_NONE);
  78. } else if (key_mgmt == WPA_KEY_MGMT_CCKM) {
  79. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_CCKM);
  80. } else {
  81. wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
  82. key_mgmt);
  83. return -1;
  84. }
  85. pos += WPA_SELECTOR_LEN;
  86. /* WPA Capabilities; use defaults, so no need to include it */
  87. hdr->len = (pos - wpa_ie) - 2;
  88. WPA_ASSERT((size_t) (pos - wpa_ie) <= wpa_ie_len);
  89. return pos - wpa_ie;
  90. }
  91. static int wpa_gen_wpa_ie_rsn(u8 *rsn_ie, size_t rsn_ie_len,
  92. int pairwise_cipher, int group_cipher,
  93. int key_mgmt, int mgmt_group_cipher,
  94. struct wpa_sm *sm)
  95. {
  96. u8 *pos;
  97. struct rsn_ie_hdr *hdr;
  98. u16 capab;
  99. u32 suite;
  100. if (rsn_ie_len < sizeof(*hdr) + RSN_SELECTOR_LEN +
  101. 2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN + 2 +
  102. (sm->cur_pmksa ? 2 + PMKID_LEN : 0)) {
  103. wpa_printf(MSG_DEBUG, "RSN: Too short IE buffer (%lu bytes)",
  104. (unsigned long) rsn_ie_len);
  105. return -1;
  106. }
  107. hdr = (struct rsn_ie_hdr *) rsn_ie;
  108. hdr->elem_id = WLAN_EID_RSN;
  109. WPA_PUT_LE16(hdr->version, RSN_VERSION);
  110. pos = (u8 *) (hdr + 1);
  111. suite = wpa_cipher_to_suite(WPA_PROTO_RSN, group_cipher);
  112. if (suite == 0) {
  113. wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
  114. group_cipher);
  115. return -1;
  116. }
  117. RSN_SELECTOR_PUT(pos, suite);
  118. pos += RSN_SELECTOR_LEN;
  119. *pos++ = 1;
  120. *pos++ = 0;
  121. suite = wpa_cipher_to_suite(WPA_PROTO_RSN, pairwise_cipher);
  122. if (suite == 0 ||
  123. (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
  124. pairwise_cipher != WPA_CIPHER_NONE)) {
  125. wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
  126. pairwise_cipher);
  127. return -1;
  128. }
  129. RSN_SELECTOR_PUT(pos, suite);
  130. pos += RSN_SELECTOR_LEN;
  131. *pos++ = 1;
  132. *pos++ = 0;
  133. if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
  134. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X);
  135. } else if (key_mgmt == WPA_KEY_MGMT_PSK) {
  136. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X);
  137. } else if (key_mgmt == WPA_KEY_MGMT_CCKM) {
  138. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_CCKM);
  139. #ifdef CONFIG_IEEE80211R
  140. } else if (key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
  141. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
  142. } else if (key_mgmt == WPA_KEY_MGMT_FT_PSK) {
  143. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
  144. #endif /* CONFIG_IEEE80211R */
  145. #ifdef CONFIG_IEEE80211W
  146. } else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SHA256) {
  147. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA256);
  148. } else if (key_mgmt == WPA_KEY_MGMT_PSK_SHA256) {
  149. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_SHA256);
  150. #endif /* CONFIG_IEEE80211W */
  151. #ifdef CONFIG_SAE
  152. } else if (key_mgmt == WPA_KEY_MGMT_SAE) {
  153. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE);
  154. } else if (key_mgmt == WPA_KEY_MGMT_FT_SAE) {
  155. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
  156. #endif /* CONFIG_SAE */
  157. } else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  158. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192);
  159. } else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  160. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B);
  161. } else {
  162. wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
  163. key_mgmt);
  164. return -1;
  165. }
  166. pos += RSN_SELECTOR_LEN;
  167. /* RSN Capabilities */
  168. capab = 0;
  169. #ifdef CONFIG_IEEE80211W
  170. if (sm->mfp)
  171. capab |= WPA_CAPABILITY_MFPC;
  172. if (sm->mfp == 2)
  173. capab |= WPA_CAPABILITY_MFPR;
  174. #endif /* CONFIG_IEEE80211W */
  175. WPA_PUT_LE16(pos, capab);
  176. pos += 2;
  177. if (sm->cur_pmksa) {
  178. /* PMKID Count (2 octets, little endian) */
  179. *pos++ = 1;
  180. *pos++ = 0;
  181. /* PMKID */
  182. os_memcpy(pos, sm->cur_pmksa->pmkid, PMKID_LEN);
  183. pos += PMKID_LEN;
  184. }
  185. #ifdef CONFIG_IEEE80211W
  186. if (wpa_cipher_valid_mgmt_group(mgmt_group_cipher)) {
  187. if (!sm->cur_pmksa) {
  188. /* PMKID Count */
  189. WPA_PUT_LE16(pos, 0);
  190. pos += 2;
  191. }
  192. /* Management Group Cipher Suite */
  193. RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
  194. mgmt_group_cipher));
  195. pos += RSN_SELECTOR_LEN;
  196. }
  197. #endif /* CONFIG_IEEE80211W */
  198. hdr->len = (pos - rsn_ie) - 2;
  199. WPA_ASSERT((size_t) (pos - rsn_ie) <= rsn_ie_len);
  200. return pos - rsn_ie;
  201. }
  202. #ifdef CONFIG_HS20
  203. static int wpa_gen_wpa_ie_osen(u8 *wpa_ie, size_t wpa_ie_len,
  204. int pairwise_cipher, int group_cipher,
  205. int key_mgmt)
  206. {
  207. u8 *pos, *len;
  208. u32 suite;
  209. if (wpa_ie_len < 2 + 4 + RSN_SELECTOR_LEN +
  210. 2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN)
  211. return -1;
  212. pos = wpa_ie;
  213. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  214. len = pos++; /* to be filled */
  215. WPA_PUT_BE24(pos, OUI_WFA);
  216. pos += 3;
  217. *pos++ = HS20_OSEN_OUI_TYPE;
  218. /* Group Data Cipher Suite */
  219. suite = wpa_cipher_to_suite(WPA_PROTO_RSN, group_cipher);
  220. if (suite == 0) {
  221. wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
  222. group_cipher);
  223. return -1;
  224. }
  225. RSN_SELECTOR_PUT(pos, suite);
  226. pos += RSN_SELECTOR_LEN;
  227. /* Pairwise Cipher Suite Count and List */
  228. WPA_PUT_LE16(pos, 1);
  229. pos += 2;
  230. suite = wpa_cipher_to_suite(WPA_PROTO_RSN, pairwise_cipher);
  231. if (suite == 0 ||
  232. (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
  233. pairwise_cipher != WPA_CIPHER_NONE)) {
  234. wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
  235. pairwise_cipher);
  236. return -1;
  237. }
  238. RSN_SELECTOR_PUT(pos, suite);
  239. pos += RSN_SELECTOR_LEN;
  240. /* AKM Suite Count and List */
  241. WPA_PUT_LE16(pos, 1);
  242. pos += 2;
  243. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_OSEN);
  244. pos += RSN_SELECTOR_LEN;
  245. *len = pos - len - 1;
  246. WPA_ASSERT((size_t) (pos - wpa_ie) <= wpa_ie_len);
  247. return pos - wpa_ie;
  248. }
  249. #endif /* CONFIG_HS20 */
  250. /**
  251. * wpa_gen_wpa_ie - Generate WPA/RSN IE based on current security policy
  252. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  253. * @wpa_ie: Pointer to memory area for the generated WPA/RSN IE
  254. * @wpa_ie_len: Maximum length of the generated WPA/RSN IE
  255. * Returns: Length of the generated WPA/RSN IE or -1 on failure
  256. */
  257. int wpa_gen_wpa_ie(struct wpa_sm *sm, u8 *wpa_ie, size_t wpa_ie_len)
  258. {
  259. if (sm->proto == WPA_PROTO_RSN)
  260. return wpa_gen_wpa_ie_rsn(wpa_ie, wpa_ie_len,
  261. sm->pairwise_cipher,
  262. sm->group_cipher,
  263. sm->key_mgmt, sm->mgmt_group_cipher,
  264. sm);
  265. #ifdef CONFIG_HS20
  266. else if (sm->proto == WPA_PROTO_OSEN)
  267. return wpa_gen_wpa_ie_osen(wpa_ie, wpa_ie_len,
  268. sm->pairwise_cipher,
  269. sm->group_cipher,
  270. sm->key_mgmt);
  271. #endif /* CONFIG_HS20 */
  272. else
  273. return wpa_gen_wpa_ie_wpa(wpa_ie, wpa_ie_len,
  274. sm->pairwise_cipher,
  275. sm->group_cipher,
  276. sm->key_mgmt);
  277. }
  278. /**
  279. * wpa_parse_vendor_specific - Parse Vendor Specific IEs
  280. * @pos: Pointer to the IE header
  281. * @end: Pointer to the end of the Key Data buffer
  282. * @ie: Pointer to parsed IE data
  283. * Returns: 0 on success, 1 if end mark is found, -1 on failure
  284. */
  285. static int wpa_parse_vendor_specific(const u8 *pos, const u8 *end,
  286. struct wpa_eapol_ie_parse *ie)
  287. {
  288. unsigned int oui;
  289. if (pos[1] < 4) {
  290. wpa_printf(MSG_MSGDUMP, "Too short vendor specific IE ignored (len=%u)",
  291. pos[1]);
  292. return 1;
  293. }
  294. oui = WPA_GET_BE24(&pos[2]);
  295. if (oui == OUI_MICROSOFT && pos[5] == WMM_OUI_TYPE && pos[1] > 4) {
  296. if (pos[6] == WMM_OUI_SUBTYPE_INFORMATION_ELEMENT) {
  297. ie->wmm = &pos[2];
  298. ie->wmm_len = pos[1];
  299. wpa_hexdump(MSG_DEBUG, "WPA: WMM IE",
  300. ie->wmm, ie->wmm_len);
  301. } else if (pos[6] == WMM_OUI_SUBTYPE_PARAMETER_ELEMENT) {
  302. ie->wmm = &pos[2];
  303. ie->wmm_len = pos[1];
  304. wpa_hexdump(MSG_DEBUG, "WPA: WMM Parameter Element",
  305. ie->wmm, ie->wmm_len);
  306. }
  307. }
  308. return 0;
  309. }
  310. /**
  311. * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
  312. * @pos: Pointer to the IE header
  313. * @end: Pointer to the end of the Key Data buffer
  314. * @ie: Pointer to parsed IE data
  315. * Returns: 0 on success, 1 if end mark is found, -1 on failure
  316. */
  317. static int wpa_parse_generic(const u8 *pos, const u8 *end,
  318. struct wpa_eapol_ie_parse *ie)
  319. {
  320. if (pos[1] == 0)
  321. return 1;
  322. if (pos[1] >= 6 &&
  323. RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
  324. pos[2 + WPA_SELECTOR_LEN] == 1 &&
  325. pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
  326. ie->wpa_ie = pos;
  327. ie->wpa_ie_len = pos[1] + 2;
  328. wpa_hexdump(MSG_DEBUG, "WPA: WPA IE in EAPOL-Key",
  329. ie->wpa_ie, ie->wpa_ie_len);
  330. return 0;
  331. }
  332. if (pos + 1 + RSN_SELECTOR_LEN < end &&
  333. pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
  334. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
  335. ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
  336. wpa_hexdump(MSG_DEBUG, "WPA: PMKID in EAPOL-Key",
  337. pos, pos[1] + 2);
  338. return 0;
  339. }
  340. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  341. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
  342. ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
  343. ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
  344. wpa_hexdump_key(MSG_DEBUG, "WPA: GTK in EAPOL-Key",
  345. pos, pos[1] + 2);
  346. return 0;
  347. }
  348. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  349. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
  350. ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
  351. ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
  352. wpa_hexdump(MSG_DEBUG, "WPA: MAC Address in EAPOL-Key",
  353. pos, pos[1] + 2);
  354. return 0;
  355. }
  356. #ifdef CONFIG_PEERKEY
  357. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  358. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_SMK) {
  359. ie->smk = pos + 2 + RSN_SELECTOR_LEN;
  360. ie->smk_len = pos[1] - RSN_SELECTOR_LEN;
  361. wpa_hexdump_key(MSG_DEBUG, "WPA: SMK in EAPOL-Key",
  362. pos, pos[1] + 2);
  363. return 0;
  364. }
  365. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  366. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_NONCE) {
  367. ie->nonce = pos + 2 + RSN_SELECTOR_LEN;
  368. ie->nonce_len = pos[1] - RSN_SELECTOR_LEN;
  369. wpa_hexdump(MSG_DEBUG, "WPA: Nonce in EAPOL-Key",
  370. pos, pos[1] + 2);
  371. return 0;
  372. }
  373. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  374. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_LIFETIME) {
  375. ie->lifetime = pos + 2 + RSN_SELECTOR_LEN;
  376. ie->lifetime_len = pos[1] - RSN_SELECTOR_LEN;
  377. wpa_hexdump(MSG_DEBUG, "WPA: Lifetime in EAPOL-Key",
  378. pos, pos[1] + 2);
  379. return 0;
  380. }
  381. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  382. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_ERROR) {
  383. ie->error = pos + 2 + RSN_SELECTOR_LEN;
  384. ie->error_len = pos[1] - RSN_SELECTOR_LEN;
  385. wpa_hexdump(MSG_DEBUG, "WPA: Error in EAPOL-Key",
  386. pos, pos[1] + 2);
  387. return 0;
  388. }
  389. #endif /* CONFIG_PEERKEY */
  390. #ifdef CONFIG_IEEE80211W
  391. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  392. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
  393. ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
  394. ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
  395. wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK in EAPOL-Key",
  396. pos, pos[1] + 2);
  397. return 0;
  398. }
  399. #endif /* CONFIG_IEEE80211W */
  400. #ifdef CONFIG_P2P
  401. if (pos[1] >= RSN_SELECTOR_LEN + 1 &&
  402. RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_REQ) {
  403. ie->ip_addr_req = pos + 2 + RSN_SELECTOR_LEN;
  404. wpa_hexdump(MSG_DEBUG, "WPA: IP Address Request in EAPOL-Key",
  405. ie->ip_addr_req, pos[1] - RSN_SELECTOR_LEN);
  406. return 0;
  407. }
  408. if (pos[1] >= RSN_SELECTOR_LEN + 3 * 4 &&
  409. RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_ALLOC) {
  410. ie->ip_addr_alloc = pos + 2 + RSN_SELECTOR_LEN;
  411. wpa_hexdump(MSG_DEBUG,
  412. "WPA: IP Address Allocation in EAPOL-Key",
  413. ie->ip_addr_alloc, pos[1] - RSN_SELECTOR_LEN);
  414. return 0;
  415. }
  416. #endif /* CONFIG_P2P */
  417. return 0;
  418. }
  419. /**
  420. * wpa_supplicant_parse_ies - Parse EAPOL-Key Key Data IEs
  421. * @buf: Pointer to the Key Data buffer
  422. * @len: Key Data Length
  423. * @ie: Pointer to parsed IE data
  424. * Returns: 0 on success, -1 on failure
  425. */
  426. int wpa_supplicant_parse_ies(const u8 *buf, size_t len,
  427. struct wpa_eapol_ie_parse *ie)
  428. {
  429. const u8 *pos, *end;
  430. int ret = 0;
  431. os_memset(ie, 0, sizeof(*ie));
  432. for (pos = buf, end = pos + len; pos + 1 < end; pos += 2 + pos[1]) {
  433. if (pos[0] == 0xdd &&
  434. ((pos == buf + len - 1) || pos[1] == 0)) {
  435. /* Ignore padding */
  436. break;
  437. }
  438. if (pos + 2 + pos[1] > end) {
  439. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
  440. "underflow (ie=%d len=%d pos=%d)",
  441. pos[0], pos[1], (int) (pos - buf));
  442. wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data",
  443. buf, len);
  444. ret = -1;
  445. break;
  446. }
  447. if (*pos == WLAN_EID_RSN) {
  448. ie->rsn_ie = pos;
  449. ie->rsn_ie_len = pos[1] + 2;
  450. wpa_hexdump(MSG_DEBUG, "WPA: RSN IE in EAPOL-Key",
  451. ie->rsn_ie, ie->rsn_ie_len);
  452. } else if (*pos == WLAN_EID_MOBILITY_DOMAIN &&
  453. pos[1] >= sizeof(struct rsn_mdie)) {
  454. ie->mdie = pos;
  455. ie->mdie_len = pos[1] + 2;
  456. wpa_hexdump(MSG_DEBUG, "WPA: MDIE in EAPOL-Key",
  457. ie->mdie, ie->mdie_len);
  458. } else if (*pos == WLAN_EID_FAST_BSS_TRANSITION &&
  459. pos[1] >= sizeof(struct rsn_ftie)) {
  460. ie->ftie = pos;
  461. ie->ftie_len = pos[1] + 2;
  462. wpa_hexdump(MSG_DEBUG, "WPA: FTIE in EAPOL-Key",
  463. ie->ftie, ie->ftie_len);
  464. } else if (*pos == WLAN_EID_TIMEOUT_INTERVAL && pos[1] >= 5) {
  465. if (pos[2] == WLAN_TIMEOUT_REASSOC_DEADLINE) {
  466. ie->reassoc_deadline = pos;
  467. wpa_hexdump(MSG_DEBUG, "WPA: Reassoc Deadline "
  468. "in EAPOL-Key",
  469. ie->reassoc_deadline, pos[1] + 2);
  470. } else if (pos[2] == WLAN_TIMEOUT_KEY_LIFETIME) {
  471. ie->key_lifetime = pos;
  472. wpa_hexdump(MSG_DEBUG, "WPA: KeyLifetime "
  473. "in EAPOL-Key",
  474. ie->key_lifetime, pos[1] + 2);
  475. } else {
  476. wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized "
  477. "EAPOL-Key Key Data IE",
  478. pos, 2 + pos[1]);
  479. }
  480. } else if (*pos == WLAN_EID_LINK_ID) {
  481. if (pos[1] >= 18) {
  482. ie->lnkid = pos;
  483. ie->lnkid_len = pos[1] + 2;
  484. }
  485. } else if (*pos == WLAN_EID_EXT_CAPAB) {
  486. ie->ext_capab = pos;
  487. ie->ext_capab_len = pos[1] + 2;
  488. } else if (*pos == WLAN_EID_SUPP_RATES) {
  489. ie->supp_rates = pos;
  490. ie->supp_rates_len = pos[1] + 2;
  491. } else if (*pos == WLAN_EID_EXT_SUPP_RATES) {
  492. ie->ext_supp_rates = pos;
  493. ie->ext_supp_rates_len = pos[1] + 2;
  494. } else if (*pos == WLAN_EID_HT_CAP &&
  495. pos[1] >= sizeof(struct ieee80211_ht_capabilities)) {
  496. ie->ht_capabilities = pos + 2;
  497. } else if (*pos == WLAN_EID_VHT_AID) {
  498. if (pos[1] >= 2)
  499. ie->aid = WPA_GET_LE16(pos + 2) & 0x3fff;
  500. } else if (*pos == WLAN_EID_VHT_CAP &&
  501. pos[1] >= sizeof(struct ieee80211_vht_capabilities))
  502. {
  503. ie->vht_capabilities = pos + 2;
  504. } else if (*pos == WLAN_EID_QOS && pos[1] >= 1) {
  505. ie->qosinfo = pos[2];
  506. } else if (*pos == WLAN_EID_SUPPORTED_CHANNELS) {
  507. ie->supp_channels = pos + 2;
  508. ie->supp_channels_len = pos[1];
  509. } else if (*pos == WLAN_EID_SUPPORTED_OPERATING_CLASSES) {
  510. /*
  511. * The value of the Length field of the Supported
  512. * Operating Classes element is between 2 and 253.
  513. * Silently skip invalid elements to avoid interop
  514. * issues when trying to use the value.
  515. */
  516. if (pos[1] >= 2 && pos[1] <= 253) {
  517. ie->supp_oper_classes = pos + 2;
  518. ie->supp_oper_classes_len = pos[1];
  519. }
  520. } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
  521. ret = wpa_parse_generic(pos, end, ie);
  522. if (ret < 0)
  523. break;
  524. if (ret > 0) {
  525. ret = 0;
  526. break;
  527. }
  528. ret = wpa_parse_vendor_specific(pos, end, ie);
  529. if (ret < 0)
  530. break;
  531. if (ret > 0) {
  532. ret = 0;
  533. break;
  534. }
  535. } else {
  536. wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
  537. "Key Data IE", pos, 2 + pos[1]);
  538. }
  539. }
  540. return ret;
  541. }