ieee80211_pae_output.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /* $OpenBSD: ieee80211_pae_output.c,v 1.20 2015/03/14 03:38:51 jsg Exp $ */
  2. /*-
  3. * Copyright (c) 2007,2008 Damien Bergamini <damien.bergamini@free.fr>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * This code implements the 4-Way Handshake and Group Key Handshake protocols
  19. * (both Supplicant and Authenticator Key Transmit state machines) defined in
  20. * IEEE Std 802.11-2007 section 8.5.
  21. */
  22. #include <sys/param.h>
  23. #include <sys/systm.h>
  24. #include <sys/mbuf.h>
  25. #include <sys/kernel.h>
  26. #include <sys/socket.h>
  27. #include <sys/sockio.h>
  28. #include <sys/endian.h>
  29. #include <sys/errno.h>
  30. #include <net/if.h>
  31. #include <net/if_dl.h>
  32. #include <net/if_media.h>
  33. #include <net/if_arp.h>
  34. #include <net/if_llc.h>
  35. #include <netinet/in.h>
  36. #include <netinet/if_ether.h>
  37. #include <netinet/ip.h>
  38. #include <net80211/ieee80211_var.h>
  39. #include <net80211/ieee80211_priv.h>
  40. int ieee80211_send_eapol_key(struct ieee80211com *, struct mbuf *,
  41. struct ieee80211_node *, const struct ieee80211_ptk *);
  42. #ifndef IEEE80211_STA_ONLY
  43. u_int8_t *ieee80211_add_gtk_kde(u_int8_t *, struct ieee80211_node *,
  44. const struct ieee80211_key *);
  45. u_int8_t *ieee80211_add_pmkid_kde(u_int8_t *, const u_int8_t *);
  46. u_int8_t *ieee80211_add_igtk_kde(u_int8_t *,
  47. const struct ieee80211_key *);
  48. #endif
  49. struct mbuf *ieee80211_get_eapol_key(int, int, u_int);
  50. /*
  51. * Send an EAPOL-Key frame to node `ni'. If MIC or encryption is required,
  52. * the PTK must be passed (otherwise it can be set to NULL.)
  53. */
  54. int
  55. ieee80211_send_eapol_key(struct ieee80211com *ic, struct mbuf *m,
  56. struct ieee80211_node *ni, const struct ieee80211_ptk *ptk)
  57. {
  58. struct ifnet *ifp = &ic->ic_if;
  59. struct ether_header *eh;
  60. struct ieee80211_eapol_key *key;
  61. u_int16_t info;
  62. int s, len, error;
  63. M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
  64. if (m == NULL)
  65. return ENOMEM;
  66. /* no need to m_pullup here (ok by construction) */
  67. eh = mtod(m, struct ether_header *);
  68. eh->ether_type = htons(ETHERTYPE_PAE);
  69. IEEE80211_ADDR_COPY(eh->ether_shost, ic->ic_myaddr);
  70. IEEE80211_ADDR_COPY(eh->ether_dhost, ni->ni_macaddr);
  71. key = (struct ieee80211_eapol_key *)&eh[1];
  72. key->version = EAPOL_VERSION;
  73. key->type = EAPOL_KEY;
  74. key->desc = (ni->ni_rsnprotos == IEEE80211_PROTO_RSN) ?
  75. EAPOL_KEY_DESC_IEEE80211 : EAPOL_KEY_DESC_WPA;
  76. info = BE_READ_2(key->info);
  77. /* use V3 descriptor if KDF is SHA256-based */
  78. if (ieee80211_is_sha256_akm(ni->ni_rsnakms))
  79. info |= EAPOL_KEY_DESC_V3;
  80. /* use V2 descriptor if pairwise or group cipher is CCMP */
  81. else if (ni->ni_rsncipher == IEEE80211_CIPHER_CCMP ||
  82. ni->ni_rsngroupcipher == IEEE80211_CIPHER_CCMP)
  83. info |= EAPOL_KEY_DESC_V2;
  84. else
  85. info |= EAPOL_KEY_DESC_V1;
  86. BE_WRITE_2(key->info, info);
  87. len = m->m_len - sizeof(struct ether_header);
  88. BE_WRITE_2(key->paylen, len - sizeof(*key));
  89. BE_WRITE_2(key->len, len - 4);
  90. #ifndef IEEE80211_STA_ONLY
  91. if (info & EAPOL_KEY_ENCRYPTED) {
  92. if (ni->ni_rsnprotos == IEEE80211_PROTO_WPA) {
  93. /* clear "Encrypted" bit for WPA */
  94. info &= ~EAPOL_KEY_ENCRYPTED;
  95. BE_WRITE_2(key->info, info);
  96. }
  97. ieee80211_eapol_key_encrypt(ic, key, ptk->kek);
  98. if ((info & EAPOL_KEY_VERSION_MASK) != EAPOL_KEY_DESC_V1) {
  99. /* AES Key Wrap adds 8 bytes + padding */
  100. m->m_pkthdr.len = m->m_len =
  101. sizeof(*eh) + 4 + BE_READ_2(key->len);
  102. }
  103. }
  104. #endif
  105. if (info & EAPOL_KEY_KEYMIC)
  106. ieee80211_eapol_key_mic(key, ptk->kck);
  107. len = m->m_pkthdr.len;
  108. s = splnet();
  109. #ifndef IEEE80211_STA_ONLY
  110. /* start a 100ms timeout if an answer is expected from supplicant */
  111. if (info & EAPOL_KEY_KEYACK)
  112. timeout_add_msec(&ni->ni_eapol_to, 100);
  113. #endif
  114. IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
  115. if (error == 0) {
  116. ifp->if_obytes += len;
  117. if ((ifp->if_flags & IFF_OACTIVE) == 0)
  118. (*ifp->if_start)(ifp);
  119. }
  120. splx(s);
  121. return error;
  122. }
  123. #ifndef IEEE80211_STA_ONLY
  124. /*
  125. * Handle EAPOL-Key timeouts (no answer from supplicant).
  126. */
  127. void
  128. ieee80211_eapol_timeout(void *arg)
  129. {
  130. struct ieee80211_node *ni = arg;
  131. struct ieee80211com *ic = ni->ni_ic;
  132. int s;
  133. DPRINTF(("no answer from station %s in state %d\n",
  134. ether_sprintf(ni->ni_macaddr), ni->ni_rsn_state));
  135. s = splnet();
  136. switch (ni->ni_rsn_state) {
  137. case RSNA_PTKSTART:
  138. case RSNA_PTKCALCNEGOTIATING:
  139. (void)ieee80211_send_4way_msg1(ic, ni);
  140. break;
  141. case RSNA_PTKINITNEGOTIATING:
  142. (void)ieee80211_send_4way_msg3(ic, ni);
  143. break;
  144. }
  145. switch (ni->ni_rsn_gstate) {
  146. case RSNA_REKEYNEGOTIATING:
  147. (void)ieee80211_send_group_msg1(ic, ni);
  148. break;
  149. }
  150. splx(s);
  151. }
  152. /*
  153. * Add a GTK KDE to an EAPOL-Key frame (see Figure 144).
  154. */
  155. u_int8_t *
  156. ieee80211_add_gtk_kde(u_int8_t *frm, struct ieee80211_node *ni,
  157. const struct ieee80211_key *k)
  158. {
  159. KASSERT(k->k_flags & IEEE80211_KEY_GROUP);
  160. *frm++ = IEEE80211_ELEMID_VENDOR;
  161. *frm++ = 6 + k->k_len;
  162. memcpy(frm, IEEE80211_OUI, 3); frm += 3;
  163. *frm++ = IEEE80211_KDE_GTK;
  164. *frm = k->k_id & 3;
  165. /*
  166. * The TxRx flag for sending a GTK is always the opposite of whether
  167. * the pairwise key is used for data encryption/integrity or not.
  168. */
  169. if (ni->ni_rsncipher == IEEE80211_CIPHER_USEGROUP)
  170. *frm |= 1 << 2; /* set the Tx bit */
  171. frm++;
  172. *frm++ = 0; /* reserved */
  173. memcpy(frm, k->k_key, k->k_len);
  174. return frm + k->k_len;
  175. }
  176. /*
  177. * Add a PMKID KDE to an EAPOL-Key frame (see Figure 146).
  178. */
  179. u_int8_t *
  180. ieee80211_add_pmkid_kde(u_int8_t *frm, const u_int8_t *pmkid)
  181. {
  182. *frm++ = IEEE80211_ELEMID_VENDOR;
  183. *frm++ = 20;
  184. memcpy(frm, IEEE80211_OUI, 3); frm += 3;
  185. *frm++ = IEEE80211_KDE_PMKID;
  186. memcpy(frm, pmkid, IEEE80211_PMKID_LEN);
  187. return frm + IEEE80211_PMKID_LEN;
  188. }
  189. /*
  190. * Add an IGTK KDE to an EAPOL-Key frame (see Figure 8-32a).
  191. */
  192. u_int8_t *
  193. ieee80211_add_igtk_kde(u_int8_t *frm, const struct ieee80211_key *k)
  194. {
  195. KASSERT(k->k_flags & IEEE80211_KEY_IGTK);
  196. *frm++ = IEEE80211_ELEMID_VENDOR;
  197. *frm++ = 4 + 24;
  198. memcpy(frm, IEEE80211_OUI, 3); frm += 3;
  199. *frm++ = IEEE80211_KDE_IGTK;
  200. LE_WRITE_2(frm, k->k_id); frm += 2;
  201. LE_WRITE_6(frm, k->k_tsc); frm += 6; /* IPN */
  202. memcpy(frm, k->k_key, 16);
  203. return frm + 16;
  204. }
  205. #endif /* IEEE80211_STA_ONLY */
  206. struct mbuf *
  207. ieee80211_get_eapol_key(int flags, int type, u_int pktlen)
  208. {
  209. struct mbuf *m;
  210. /* reserve space for 802.11 encapsulation and EAPOL-Key header */
  211. pktlen += sizeof(struct ieee80211_frame) + LLC_SNAPFRAMELEN +
  212. sizeof(struct ieee80211_eapol_key);
  213. if (pktlen > MCLBYTES)
  214. panic("EAPOL-Key frame too large: %u", pktlen);
  215. MGETHDR(m, flags, type);
  216. if (m == NULL)
  217. return NULL;
  218. if (pktlen > MHLEN) {
  219. MCLGET(m, flags);
  220. if (!(m->m_flags & M_EXT))
  221. return m_free(m);
  222. }
  223. m->m_data += sizeof(struct ieee80211_frame) + LLC_SNAPFRAMELEN;
  224. return m;
  225. }
  226. #ifndef IEEE80211_STA_ONLY
  227. /*
  228. * Send 4-Way Handshake Message 1 to the supplicant.
  229. */
  230. int
  231. ieee80211_send_4way_msg1(struct ieee80211com *ic, struct ieee80211_node *ni)
  232. {
  233. struct ieee80211_eapol_key *key;
  234. struct mbuf *m;
  235. u_int16_t info, keylen;
  236. u_int8_t *frm;
  237. ni->ni_rsn_state = RSNA_PTKSTART;
  238. if (++ni->ni_rsn_retries > 3) {
  239. IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
  240. IEEE80211_REASON_4WAY_TIMEOUT);
  241. ieee80211_node_leave(ic, ni);
  242. return 0;
  243. }
  244. m = ieee80211_get_eapol_key(M_DONTWAIT, MT_DATA,
  245. (ni->ni_rsnprotos == IEEE80211_PROTO_RSN) ? 2 + 20 : 0);
  246. if (m == NULL)
  247. return ENOMEM;
  248. key = mtod(m, struct ieee80211_eapol_key *);
  249. memset(key, 0, sizeof(*key));
  250. info = EAPOL_KEY_PAIRWISE | EAPOL_KEY_KEYACK;
  251. BE_WRITE_2(key->info, info);
  252. /* copy the authenticator's nonce (ANonce) */
  253. memcpy(key->nonce, ni->ni_nonce, EAPOL_KEY_NONCE_LEN);
  254. keylen = ieee80211_cipher_keylen(ni->ni_rsncipher);
  255. BE_WRITE_2(key->keylen, keylen);
  256. frm = (u_int8_t *)&key[1];
  257. /* NB: WPA does not have PMKID KDE */
  258. if (ni->ni_rsnprotos == IEEE80211_PROTO_RSN &&
  259. ieee80211_is_8021x_akm(ni->ni_rsnakms))
  260. frm = ieee80211_add_pmkid_kde(frm, ni->ni_pmkid);
  261. m->m_pkthdr.len = m->m_len = frm - (u_int8_t *)key;
  262. if (ic->ic_if.if_flags & IFF_DEBUG)
  263. printf("%s: sending msg %d/%d of the %s handshake to %s\n",
  264. ic->ic_if.if_xname, 1, 4, "4-way",
  265. ether_sprintf(ni->ni_macaddr));
  266. ni->ni_replaycnt++;
  267. BE_WRITE_8(key->replaycnt, ni->ni_replaycnt);
  268. return ieee80211_send_eapol_key(ic, m, ni, NULL);
  269. }
  270. #endif /* IEEE80211_STA_ONLY */
  271. /*
  272. * Send 4-Way Handshake Message 2 to the authenticator.
  273. */
  274. int
  275. ieee80211_send_4way_msg2(struct ieee80211com *ic, struct ieee80211_node *ni,
  276. const u_int8_t *replaycnt, const struct ieee80211_ptk *tptk)
  277. {
  278. struct ieee80211_eapol_key *key;
  279. struct mbuf *m;
  280. u_int16_t info;
  281. u_int8_t *frm;
  282. m = ieee80211_get_eapol_key(M_DONTWAIT, MT_DATA,
  283. (ni->ni_rsnprotos == IEEE80211_PROTO_WPA) ?
  284. 2 + IEEE80211_WPAIE_MAXLEN :
  285. 2 + IEEE80211_RSNIE_MAXLEN);
  286. if (m == NULL)
  287. return ENOMEM;
  288. key = mtod(m, struct ieee80211_eapol_key *);
  289. memset(key, 0, sizeof(*key));
  290. info = EAPOL_KEY_PAIRWISE | EAPOL_KEY_KEYMIC;
  291. BE_WRITE_2(key->info, info);
  292. /* copy key replay counter from Message 1/4 */
  293. memcpy(key->replaycnt, replaycnt, 8);
  294. /* copy the supplicant's nonce (SNonce) */
  295. memcpy(key->nonce, ic->ic_nonce, EAPOL_KEY_NONCE_LEN);
  296. frm = (u_int8_t *)&key[1];
  297. /* add the WPA/RSN IE used in the (Re)Association Request */
  298. if (ni->ni_rsnprotos == IEEE80211_PROTO_WPA) {
  299. int keylen;
  300. frm = ieee80211_add_wpa(frm, ic, ni);
  301. /* WPA sets the key length field here */
  302. keylen = ieee80211_cipher_keylen(ni->ni_rsncipher);
  303. BE_WRITE_2(key->keylen, keylen);
  304. } else /* RSN */
  305. frm = ieee80211_add_rsn(frm, ic, ni);
  306. m->m_pkthdr.len = m->m_len = frm - (u_int8_t *)key;
  307. if (ic->ic_if.if_flags & IFF_DEBUG)
  308. printf("%s: sending msg %d/%d of the %s handshake to %s\n",
  309. ic->ic_if.if_xname, 2, 4, "4-way",
  310. ether_sprintf(ni->ni_macaddr));
  311. return ieee80211_send_eapol_key(ic, m, ni, tptk);
  312. }
  313. #ifndef IEEE80211_STA_ONLY
  314. /*
  315. * Send 4-Way Handshake Message 3 to the supplicant.
  316. */
  317. int
  318. ieee80211_send_4way_msg3(struct ieee80211com *ic, struct ieee80211_node *ni)
  319. {
  320. struct ieee80211_eapol_key *key;
  321. struct ieee80211_key *k;
  322. struct mbuf *m;
  323. u_int16_t info, keylen;
  324. u_int8_t *frm;
  325. ni->ni_rsn_state = RSNA_PTKINITNEGOTIATING;
  326. if (++ni->ni_rsn_retries > 3) {
  327. IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
  328. IEEE80211_REASON_4WAY_TIMEOUT);
  329. ieee80211_node_leave(ic, ni);
  330. return 0;
  331. }
  332. if (ni->ni_rsnprotos == IEEE80211_PROTO_RSN)
  333. k = &ic->ic_nw_keys[ic->ic_def_txkey];
  334. m = ieee80211_get_eapol_key(M_DONTWAIT, MT_DATA,
  335. ((ni->ni_rsnprotos == IEEE80211_PROTO_WPA) ?
  336. 2 + IEEE80211_WPAIE_MAXLEN :
  337. 2 + IEEE80211_RSNIE_MAXLEN + 2 + 6 + k->k_len + 15) +
  338. ((ni->ni_flags & IEEE80211_NODE_MFP) ? 2 + 28 : 0));
  339. if (m == NULL)
  340. return ENOMEM;
  341. key = mtod(m, struct ieee80211_eapol_key *);
  342. memset(key, 0, sizeof(*key));
  343. info = EAPOL_KEY_PAIRWISE | EAPOL_KEY_KEYACK | EAPOL_KEY_KEYMIC;
  344. if (ni->ni_rsncipher != IEEE80211_CIPHER_USEGROUP)
  345. info |= EAPOL_KEY_INSTALL;
  346. /* use same nonce as in Message 1 */
  347. memcpy(key->nonce, ni->ni_nonce, EAPOL_KEY_NONCE_LEN);
  348. ni->ni_replaycnt++;
  349. BE_WRITE_8(key->replaycnt, ni->ni_replaycnt);
  350. keylen = ieee80211_cipher_keylen(ni->ni_rsncipher);
  351. BE_WRITE_2(key->keylen, keylen);
  352. frm = (u_int8_t *)&key[1];
  353. /* add the WPA/RSN IE included in Beacon/Probe Response */
  354. if (ni->ni_rsnprotos == IEEE80211_PROTO_RSN) {
  355. frm = ieee80211_add_rsn(frm, ic, ic->ic_bss);
  356. /* encapsulate the GTK */
  357. frm = ieee80211_add_gtk_kde(frm, ni, k);
  358. LE_WRITE_6(key->rsc, k->k_tsc);
  359. /* encapsulate the IGTK if MFP was negotiated */
  360. if (ni->ni_flags & IEEE80211_NODE_MFP) {
  361. frm = ieee80211_add_igtk_kde(frm,
  362. &ic->ic_nw_keys[ic->ic_igtk_kid]);
  363. }
  364. /* ask that the EAPOL-Key frame be encrypted */
  365. info |= EAPOL_KEY_ENCRYPTED | EAPOL_KEY_SECURE;
  366. } else /* WPA */
  367. frm = ieee80211_add_wpa(frm, ic, ic->ic_bss);
  368. /* write the key info field */
  369. BE_WRITE_2(key->info, info);
  370. m->m_pkthdr.len = m->m_len = frm - (u_int8_t *)key;
  371. if (ic->ic_if.if_flags & IFF_DEBUG)
  372. printf("%s: sending msg %d/%d of the %s handshake to %s\n",
  373. ic->ic_if.if_xname, 3, 4, "4-way",
  374. ether_sprintf(ni->ni_macaddr));
  375. return ieee80211_send_eapol_key(ic, m, ni, &ni->ni_ptk);
  376. }
  377. #endif /* IEEE80211_STA_ONLY */
  378. /*
  379. * Send 4-Way Handshake Message 4 to the authenticator.
  380. */
  381. int
  382. ieee80211_send_4way_msg4(struct ieee80211com *ic, struct ieee80211_node *ni)
  383. {
  384. struct ieee80211_eapol_key *key;
  385. struct mbuf *m;
  386. u_int16_t info;
  387. m = ieee80211_get_eapol_key(M_DONTWAIT, MT_DATA, 0);
  388. if (m == NULL)
  389. return ENOMEM;
  390. key = mtod(m, struct ieee80211_eapol_key *);
  391. memset(key, 0, sizeof(*key));
  392. info = EAPOL_KEY_PAIRWISE | EAPOL_KEY_KEYMIC;
  393. /* copy key replay counter from authenticator */
  394. BE_WRITE_8(key->replaycnt, ni->ni_replaycnt);
  395. if (ni->ni_rsnprotos == IEEE80211_PROTO_WPA) {
  396. int keylen;
  397. /* WPA sets the key length field here */
  398. keylen = ieee80211_cipher_keylen(ni->ni_rsncipher);
  399. BE_WRITE_2(key->keylen, keylen);
  400. } else
  401. info |= EAPOL_KEY_SECURE;
  402. /* write the key info field */
  403. BE_WRITE_2(key->info, info);
  404. /* empty key data field */
  405. m->m_pkthdr.len = m->m_len = sizeof(*key);
  406. if (ic->ic_if.if_flags & IFF_DEBUG)
  407. printf("%s: sending msg %d/%d of the %s handshake to %s\n",
  408. ic->ic_if.if_xname, 4, 4, "4-way",
  409. ether_sprintf(ni->ni_macaddr));
  410. return ieee80211_send_eapol_key(ic, m, ni, &ni->ni_ptk);
  411. }
  412. #ifndef IEEE80211_STA_ONLY
  413. /*
  414. * Send Group Key Handshake Message 1 to the supplicant.
  415. */
  416. int
  417. ieee80211_send_group_msg1(struct ieee80211com *ic, struct ieee80211_node *ni)
  418. {
  419. struct ieee80211_eapol_key *key;
  420. const struct ieee80211_key *k;
  421. struct mbuf *m;
  422. u_int16_t info;
  423. u_int8_t *frm;
  424. u_int8_t kid;
  425. ni->ni_rsn_gstate = RSNA_REKEYNEGOTIATING;
  426. if (++ni->ni_rsn_retries > 3) {
  427. IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
  428. IEEE80211_REASON_GROUP_TIMEOUT);
  429. ieee80211_node_leave(ic, ni);
  430. return 0;
  431. }
  432. if (ni->ni_flags & IEEE80211_NODE_REKEY)
  433. kid = (ic->ic_def_txkey == 1) ? 2 : 1;
  434. else
  435. kid = ic->ic_def_txkey;
  436. k = &ic->ic_nw_keys[kid];
  437. m = ieee80211_get_eapol_key(M_DONTWAIT, MT_DATA,
  438. ((ni->ni_rsnprotos == IEEE80211_PROTO_WPA) ?
  439. k->k_len : 2 + 6 + k->k_len) +
  440. ((ni->ni_flags & IEEE80211_NODE_MFP) ? 2 + 28 : 0) +
  441. 15);
  442. if (m == NULL)
  443. return ENOMEM;
  444. key = mtod(m, struct ieee80211_eapol_key *);
  445. memset(key, 0, sizeof(*key));
  446. info = EAPOL_KEY_KEYACK | EAPOL_KEY_KEYMIC | EAPOL_KEY_SECURE |
  447. EAPOL_KEY_ENCRYPTED;
  448. ni->ni_replaycnt++;
  449. BE_WRITE_8(key->replaycnt, ni->ni_replaycnt);
  450. frm = (u_int8_t *)&key[1];
  451. if (ni->ni_rsnprotos == IEEE80211_PROTO_WPA) {
  452. /* WPA does not have GTK KDE */
  453. BE_WRITE_2(key->keylen, k->k_len);
  454. memcpy(frm, k->k_key, k->k_len);
  455. frm += k->k_len;
  456. info |= (k->k_id & 0x3) << EAPOL_KEY_WPA_KID_SHIFT;
  457. if (ni->ni_rsncipher == IEEE80211_CIPHER_USEGROUP)
  458. info |= EAPOL_KEY_WPA_TX;
  459. } else { /* RSN */
  460. frm = ieee80211_add_gtk_kde(frm, ni, k);
  461. if (ni->ni_flags & IEEE80211_NODE_MFP) {
  462. if (ni->ni_flags & IEEE80211_NODE_REKEY)
  463. kid = (ic->ic_igtk_kid == 4) ? 5 : 4;
  464. else
  465. kid = ic->ic_igtk_kid;
  466. frm = ieee80211_add_igtk_kde(frm,
  467. &ic->ic_nw_keys[kid]);
  468. }
  469. }
  470. /* RSC = last transmit sequence number for the GTK */
  471. LE_WRITE_6(key->rsc, k->k_tsc);
  472. /* write the key info field */
  473. BE_WRITE_2(key->info, info);
  474. m->m_pkthdr.len = m->m_len = frm - (u_int8_t *)key;
  475. if (ic->ic_if.if_flags & IFF_DEBUG)
  476. printf("%s: sending msg %d/%d of the %s handshake to %s\n",
  477. ic->ic_if.if_xname, 1, 2, "group key",
  478. ether_sprintf(ni->ni_macaddr));
  479. return ieee80211_send_eapol_key(ic, m, ni, &ni->ni_ptk);
  480. }
  481. #endif /* IEEE80211_STA_ONLY */
  482. /*
  483. * Send Group Key Handshake Message 2 to the authenticator.
  484. */
  485. int
  486. ieee80211_send_group_msg2(struct ieee80211com *ic, struct ieee80211_node *ni,
  487. const struct ieee80211_key *k)
  488. {
  489. struct ieee80211_eapol_key *key;
  490. u_int16_t info;
  491. struct mbuf *m;
  492. m = ieee80211_get_eapol_key(M_DONTWAIT, MT_DATA, 0);
  493. if (m == NULL)
  494. return ENOMEM;
  495. key = mtod(m, struct ieee80211_eapol_key *);
  496. memset(key, 0, sizeof(*key));
  497. info = EAPOL_KEY_KEYMIC | EAPOL_KEY_SECURE;
  498. /* copy key replay counter from authenticator */
  499. BE_WRITE_8(key->replaycnt, ni->ni_replaycnt);
  500. if (ni->ni_rsnprotos == IEEE80211_PROTO_WPA) {
  501. /* WPA sets the key length and key id fields here */
  502. BE_WRITE_2(key->keylen, k->k_len);
  503. info |= (k->k_id & 3) << EAPOL_KEY_WPA_KID_SHIFT;
  504. }
  505. /* write the key info field */
  506. BE_WRITE_2(key->info, info);
  507. /* empty key data field */
  508. m->m_pkthdr.len = m->m_len = sizeof(*key);
  509. if (ic->ic_if.if_flags & IFF_DEBUG)
  510. printf("%s: sending msg %d/%d of the %s handshake to %s\n",
  511. ic->ic_if.if_xname, 2, 2, "group key",
  512. ether_sprintf(ni->ni_macaddr));
  513. return ieee80211_send_eapol_key(ic, m, ni, &ni->ni_ptk);
  514. }
  515. /*
  516. * EAPOL-Key Request frames are sent by the supplicant to request that the
  517. * authenticator initiates either a 4-Way Handshake or Group Key Handshake,
  518. * or to report a MIC failure in a TKIP MSDU.
  519. */
  520. int
  521. ieee80211_send_eapol_key_req(struct ieee80211com *ic,
  522. struct ieee80211_node *ni, u_int16_t info, u_int64_t tsc)
  523. {
  524. struct ieee80211_eapol_key *key;
  525. struct mbuf *m;
  526. m = ieee80211_get_eapol_key(M_DONTWAIT, MT_DATA, 0);
  527. if (m == NULL)
  528. return ENOMEM;
  529. key = mtod(m, struct ieee80211_eapol_key *);
  530. memset(key, 0, sizeof(*key));
  531. info |= EAPOL_KEY_REQUEST;
  532. BE_WRITE_2(key->info, info);
  533. /* in case of TKIP MIC failure, fill the RSC field */
  534. if (info & EAPOL_KEY_ERROR)
  535. LE_WRITE_6(key->rsc, tsc);
  536. /* use our separate key replay counter for key requests */
  537. BE_WRITE_8(key->replaycnt, ni->ni_reqreplaycnt);
  538. ni->ni_reqreplaycnt++;
  539. if (ic->ic_if.if_flags & IFF_DEBUG)
  540. printf("%s: sending EAPOL-Key request to %s\n",
  541. ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr));
  542. return ieee80211_send_eapol_key(ic, m, ni, &ni->ni_ptk);
  543. }