ieee802_1x.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  1. /*
  2. * hostapd / IEEE 802.1X-2004 Authenticator
  3. * Copyright (c) 2002-2012, 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 "utils/eloop.h"
  11. #include "crypto/md5.h"
  12. #include "crypto/crypto.h"
  13. #include "crypto/random.h"
  14. #include "common/ieee802_11_defs.h"
  15. #include "radius/radius.h"
  16. #include "radius/radius_client.h"
  17. #include "eap_server/eap.h"
  18. #include "eap_common/eap_wsc_common.h"
  19. #include "eapol_auth/eapol_auth_sm.h"
  20. #include "eapol_auth/eapol_auth_sm_i.h"
  21. #include "p2p/p2p.h"
  22. #include "hostapd.h"
  23. #include "accounting.h"
  24. #include "sta_info.h"
  25. #include "wpa_auth.h"
  26. #include "preauth_auth.h"
  27. #include "pmksa_cache_auth.h"
  28. #include "ap_config.h"
  29. #include "ap_drv_ops.h"
  30. #include "ieee802_1x.h"
  31. static void ieee802_1x_finished(struct hostapd_data *hapd,
  32. struct sta_info *sta, int success);
  33. static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
  34. u8 type, const u8 *data, size_t datalen)
  35. {
  36. u8 *buf;
  37. struct ieee802_1x_hdr *xhdr;
  38. size_t len;
  39. int encrypt = 0;
  40. len = sizeof(*xhdr) + datalen;
  41. buf = os_zalloc(len);
  42. if (buf == NULL) {
  43. wpa_printf(MSG_ERROR, "malloc() failed for "
  44. "ieee802_1x_send(len=%lu)",
  45. (unsigned long) len);
  46. return;
  47. }
  48. xhdr = (struct ieee802_1x_hdr *) buf;
  49. xhdr->version = hapd->conf->eapol_version;
  50. xhdr->type = type;
  51. xhdr->length = host_to_be16(datalen);
  52. if (datalen > 0 && data != NULL)
  53. os_memcpy(xhdr + 1, data, datalen);
  54. if (wpa_auth_pairwise_set(sta->wpa_sm))
  55. encrypt = 1;
  56. if (sta->flags & WLAN_STA_PREAUTH) {
  57. rsn_preauth_send(hapd, sta, buf, len);
  58. } else {
  59. hostapd_drv_hapd_send_eapol(hapd, sta->addr, buf, len,
  60. encrypt, sta->flags);
  61. }
  62. os_free(buf);
  63. }
  64. void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
  65. struct sta_info *sta, int authorized)
  66. {
  67. int res;
  68. if (sta->flags & WLAN_STA_PREAUTH)
  69. return;
  70. if (authorized) {
  71. ap_sta_set_authorized(hapd, sta, 1);
  72. res = hostapd_set_authorized(hapd, sta, 1);
  73. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  74. HOSTAPD_LEVEL_DEBUG, "authorizing port");
  75. } else {
  76. ap_sta_set_authorized(hapd, sta, 0);
  77. res = hostapd_set_authorized(hapd, sta, 0);
  78. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  79. HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
  80. }
  81. if (res && errno != ENOENT) {
  82. printf("Could not set station " MACSTR " flags for kernel "
  83. "driver (errno=%d).\n", MAC2STR(sta->addr), errno);
  84. }
  85. if (authorized) {
  86. os_get_time(&sta->connected_time);
  87. accounting_sta_start(hapd, sta);
  88. }
  89. }
  90. static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
  91. struct sta_info *sta,
  92. int idx, int broadcast,
  93. u8 *key_data, size_t key_len)
  94. {
  95. u8 *buf, *ekey;
  96. struct ieee802_1x_hdr *hdr;
  97. struct ieee802_1x_eapol_key *key;
  98. size_t len, ekey_len;
  99. struct eapol_state_machine *sm = sta->eapol_sm;
  100. if (sm == NULL)
  101. return;
  102. len = sizeof(*key) + key_len;
  103. buf = os_zalloc(sizeof(*hdr) + len);
  104. if (buf == NULL)
  105. return;
  106. hdr = (struct ieee802_1x_hdr *) buf;
  107. key = (struct ieee802_1x_eapol_key *) (hdr + 1);
  108. key->type = EAPOL_KEY_TYPE_RC4;
  109. WPA_PUT_BE16(key->key_length, key_len);
  110. wpa_get_ntp_timestamp(key->replay_counter);
  111. if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
  112. wpa_printf(MSG_ERROR, "Could not get random numbers");
  113. os_free(buf);
  114. return;
  115. }
  116. key->key_index = idx | (broadcast ? 0 : BIT(7));
  117. if (hapd->conf->eapol_key_index_workaround) {
  118. /* According to some information, WinXP Supplicant seems to
  119. * interpret bit7 as an indication whether the key is to be
  120. * activated, so make it possible to enable workaround that
  121. * sets this bit for all keys. */
  122. key->key_index |= BIT(7);
  123. }
  124. /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
  125. * MSK[32..63] is used to sign the message. */
  126. if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
  127. wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
  128. "and signing EAPOL-Key");
  129. os_free(buf);
  130. return;
  131. }
  132. os_memcpy((u8 *) (key + 1), key_data, key_len);
  133. ekey_len = sizeof(key->key_iv) + 32;
  134. ekey = os_malloc(ekey_len);
  135. if (ekey == NULL) {
  136. wpa_printf(MSG_ERROR, "Could not encrypt key");
  137. os_free(buf);
  138. return;
  139. }
  140. os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
  141. os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
  142. rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
  143. os_free(ekey);
  144. /* This header is needed here for HMAC-MD5, but it will be regenerated
  145. * in ieee802_1x_send() */
  146. hdr->version = hapd->conf->eapol_version;
  147. hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
  148. hdr->length = host_to_be16(len);
  149. hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
  150. key->key_signature);
  151. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
  152. " (%s index=%d)", MAC2STR(sm->addr),
  153. broadcast ? "broadcast" : "unicast", idx);
  154. ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
  155. if (sta->eapol_sm)
  156. sta->eapol_sm->dot1xAuthEapolFramesTx++;
  157. os_free(buf);
  158. }
  159. #ifndef CONFIG_NO_VLAN
  160. static struct hostapd_wep_keys *
  161. ieee802_1x_group_alloc(struct hostapd_data *hapd, const char *ifname)
  162. {
  163. struct hostapd_wep_keys *key;
  164. key = os_zalloc(sizeof(*key));
  165. if (key == NULL)
  166. return NULL;
  167. key->default_len = hapd->conf->default_wep_key_len;
  168. if (key->idx >= hapd->conf->broadcast_key_idx_max ||
  169. key->idx < hapd->conf->broadcast_key_idx_min)
  170. key->idx = hapd->conf->broadcast_key_idx_min;
  171. else
  172. key->idx++;
  173. if (!key->key[key->idx])
  174. key->key[key->idx] = os_malloc(key->default_len);
  175. if (key->key[key->idx] == NULL ||
  176. random_get_bytes(key->key[key->idx], key->default_len)) {
  177. printf("Could not generate random WEP key (dynamic VLAN).\n");
  178. os_free(key->key[key->idx]);
  179. key->key[key->idx] = NULL;
  180. os_free(key);
  181. return NULL;
  182. }
  183. key->len[key->idx] = key->default_len;
  184. wpa_printf(MSG_DEBUG, "%s: Default WEP idx %d for dynamic VLAN\n",
  185. ifname, key->idx);
  186. wpa_hexdump_key(MSG_DEBUG, "Default WEP key (dynamic VLAN)",
  187. key->key[key->idx], key->len[key->idx]);
  188. if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_WEP,
  189. broadcast_ether_addr, key->idx, 1,
  190. NULL, 0, key->key[key->idx],
  191. key->len[key->idx]))
  192. printf("Could not set dynamic VLAN WEP encryption key.\n");
  193. hostapd_set_drv_ieee8021x(hapd, ifname, 1);
  194. return key;
  195. }
  196. static struct hostapd_wep_keys *
  197. ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
  198. size_t vlan_id)
  199. {
  200. const char *ifname;
  201. if (vlan_id == 0)
  202. return &ssid->wep;
  203. if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
  204. ssid->dyn_vlan_keys[vlan_id])
  205. return ssid->dyn_vlan_keys[vlan_id];
  206. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
  207. "state machine for VLAN ID %lu",
  208. (unsigned long) vlan_id);
  209. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
  210. if (ifname == NULL) {
  211. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
  212. "cannot create group key state machine",
  213. (unsigned long) vlan_id);
  214. return NULL;
  215. }
  216. if (ssid->dyn_vlan_keys == NULL) {
  217. int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
  218. ssid->dyn_vlan_keys = os_zalloc(size);
  219. if (ssid->dyn_vlan_keys == NULL)
  220. return NULL;
  221. ssid->max_dyn_vlan_keys = vlan_id;
  222. }
  223. if (ssid->max_dyn_vlan_keys < vlan_id) {
  224. struct hostapd_wep_keys **na;
  225. int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
  226. na = os_realloc(ssid->dyn_vlan_keys, size);
  227. if (na == NULL)
  228. return NULL;
  229. ssid->dyn_vlan_keys = na;
  230. os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
  231. (vlan_id - ssid->max_dyn_vlan_keys) *
  232. sizeof(ssid->dyn_vlan_keys[0]));
  233. ssid->max_dyn_vlan_keys = vlan_id;
  234. }
  235. ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
  236. return ssid->dyn_vlan_keys[vlan_id];
  237. }
  238. #endif /* CONFIG_NO_VLAN */
  239. void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
  240. {
  241. struct eapol_authenticator *eapol = hapd->eapol_auth;
  242. struct eapol_state_machine *sm = sta->eapol_sm;
  243. #ifndef CONFIG_NO_VLAN
  244. struct hostapd_wep_keys *key = NULL;
  245. int vlan_id;
  246. #endif /* CONFIG_NO_VLAN */
  247. if (sm == NULL || !sm->eap_if->eapKeyData)
  248. return;
  249. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
  250. MAC2STR(sta->addr));
  251. #ifndef CONFIG_NO_VLAN
  252. vlan_id = sta->vlan_id;
  253. if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
  254. vlan_id = 0;
  255. if (vlan_id) {
  256. key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
  257. if (key && key->key[key->idx])
  258. ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
  259. key->key[key->idx],
  260. key->len[key->idx]);
  261. } else
  262. #endif /* CONFIG_NO_VLAN */
  263. if (eapol->default_wep_key) {
  264. ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
  265. eapol->default_wep_key,
  266. hapd->conf->default_wep_key_len);
  267. }
  268. if (hapd->conf->individual_wep_key_len > 0) {
  269. u8 *ikey;
  270. ikey = os_malloc(hapd->conf->individual_wep_key_len);
  271. if (ikey == NULL ||
  272. random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
  273. {
  274. wpa_printf(MSG_ERROR, "Could not generate random "
  275. "individual WEP key.");
  276. os_free(ikey);
  277. return;
  278. }
  279. wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
  280. ikey, hapd->conf->individual_wep_key_len);
  281. ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
  282. hapd->conf->individual_wep_key_len);
  283. /* TODO: set encryption in TX callback, i.e., only after STA
  284. * has ACKed EAPOL-Key frame */
  285. if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
  286. sta->addr, 0, 1, NULL, 0, ikey,
  287. hapd->conf->individual_wep_key_len)) {
  288. wpa_printf(MSG_ERROR, "Could not set individual WEP "
  289. "encryption.");
  290. }
  291. os_free(ikey);
  292. }
  293. }
  294. const char *radius_mode_txt(struct hostapd_data *hapd)
  295. {
  296. switch (hapd->iface->conf->hw_mode) {
  297. case HOSTAPD_MODE_IEEE80211AD:
  298. return "802.11ad";
  299. case HOSTAPD_MODE_IEEE80211A:
  300. return "802.11a";
  301. case HOSTAPD_MODE_IEEE80211G:
  302. return "802.11g";
  303. case HOSTAPD_MODE_IEEE80211B:
  304. default:
  305. return "802.11b";
  306. }
  307. }
  308. int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
  309. {
  310. int i;
  311. u8 rate = 0;
  312. for (i = 0; i < sta->supported_rates_len; i++)
  313. if ((sta->supported_rates[i] & 0x7f) > rate)
  314. rate = sta->supported_rates[i] & 0x7f;
  315. return rate;
  316. }
  317. #ifndef CONFIG_NO_RADIUS
  318. static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
  319. struct eapol_state_machine *sm,
  320. const u8 *eap, size_t len)
  321. {
  322. const u8 *identity;
  323. size_t identity_len;
  324. if (len <= sizeof(struct eap_hdr) ||
  325. eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
  326. return;
  327. identity = eap_get_identity(sm->eap, &identity_len);
  328. if (identity == NULL)
  329. return;
  330. /* Save station identity for future RADIUS packets */
  331. os_free(sm->identity);
  332. sm->identity = os_malloc(identity_len + 1);
  333. if (sm->identity == NULL) {
  334. sm->identity_len = 0;
  335. return;
  336. }
  337. os_memcpy(sm->identity, identity, identity_len);
  338. sm->identity_len = identity_len;
  339. sm->identity[identity_len] = '\0';
  340. hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
  341. HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
  342. sm->dot1xAuthEapolRespIdFramesRx++;
  343. }
  344. static int add_common_radius_sta_attr(struct hostapd_data *hapd,
  345. struct hostapd_radius_attr *req_attr,
  346. struct sta_info *sta,
  347. struct radius_msg *msg)
  348. {
  349. char buf[128];
  350. if (!hostapd_config_get_radius_attr(req_attr,
  351. RADIUS_ATTR_NAS_PORT) &&
  352. !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
  353. wpa_printf(MSG_ERROR, "Could not add NAS-Port");
  354. return -1;
  355. }
  356. os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
  357. MAC2STR(sta->addr));
  358. buf[sizeof(buf) - 1] = '\0';
  359. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
  360. (u8 *) buf, os_strlen(buf))) {
  361. wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
  362. return -1;
  363. }
  364. if (sta->flags & WLAN_STA_PREAUTH) {
  365. os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
  366. sizeof(buf));
  367. } else {
  368. os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
  369. radius_sta_rate(hapd, sta) / 2,
  370. (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
  371. radius_mode_txt(hapd));
  372. buf[sizeof(buf) - 1] = '\0';
  373. }
  374. if (!hostapd_config_get_radius_attr(req_attr,
  375. RADIUS_ATTR_CONNECT_INFO) &&
  376. !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
  377. (u8 *) buf, os_strlen(buf))) {
  378. wpa_printf(MSG_ERROR, "Could not add Connect-Info");
  379. return -1;
  380. }
  381. return 0;
  382. }
  383. int add_common_radius_attr(struct hostapd_data *hapd,
  384. struct hostapd_radius_attr *req_attr,
  385. struct sta_info *sta,
  386. struct radius_msg *msg)
  387. {
  388. char buf[128];
  389. struct hostapd_radius_attr *attr;
  390. if (!hostapd_config_get_radius_attr(req_attr,
  391. RADIUS_ATTR_NAS_IP_ADDRESS) &&
  392. hapd->conf->own_ip_addr.af == AF_INET &&
  393. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
  394. (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
  395. wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
  396. return -1;
  397. }
  398. #ifdef CONFIG_IPV6
  399. if (!hostapd_config_get_radius_attr(req_attr,
  400. RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
  401. hapd->conf->own_ip_addr.af == AF_INET6 &&
  402. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
  403. (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
  404. wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
  405. return -1;
  406. }
  407. #endif /* CONFIG_IPV6 */
  408. if (!hostapd_config_get_radius_attr(req_attr,
  409. RADIUS_ATTR_NAS_IDENTIFIER) &&
  410. hapd->conf->nas_identifier &&
  411. !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
  412. (u8 *) hapd->conf->nas_identifier,
  413. os_strlen(hapd->conf->nas_identifier))) {
  414. wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
  415. return -1;
  416. }
  417. os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
  418. MAC2STR(hapd->own_addr),
  419. wpa_ssid_txt(hapd->conf->ssid.ssid,
  420. hapd->conf->ssid.ssid_len));
  421. buf[sizeof(buf) - 1] = '\0';
  422. if (!hostapd_config_get_radius_attr(req_attr,
  423. RADIUS_ATTR_CALLED_STATION_ID) &&
  424. !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
  425. (u8 *) buf, os_strlen(buf))) {
  426. wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
  427. return -1;
  428. }
  429. if (!hostapd_config_get_radius_attr(req_attr,
  430. RADIUS_ATTR_NAS_PORT_TYPE) &&
  431. !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
  432. RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
  433. wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
  434. return -1;
  435. }
  436. if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
  437. return -1;
  438. for (attr = req_attr; attr; attr = attr->next) {
  439. if (!radius_msg_add_attr(msg, attr->type,
  440. wpabuf_head(attr->val),
  441. wpabuf_len(attr->val))) {
  442. wpa_printf(MSG_ERROR, "Could not add RADIUS "
  443. "attribute");
  444. return -1;
  445. }
  446. }
  447. return 0;
  448. }
  449. static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
  450. struct sta_info *sta,
  451. const u8 *eap, size_t len)
  452. {
  453. struct radius_msg *msg;
  454. struct eapol_state_machine *sm = sta->eapol_sm;
  455. if (sm == NULL)
  456. return;
  457. ieee802_1x_learn_identity(hapd, sm, eap, len);
  458. wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
  459. "packet");
  460. sm->radius_identifier = radius_client_get_id(hapd->radius);
  461. msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
  462. sm->radius_identifier);
  463. if (msg == NULL) {
  464. printf("Could not create net RADIUS packet\n");
  465. return;
  466. }
  467. radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
  468. if (sm->identity &&
  469. !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
  470. sm->identity, sm->identity_len)) {
  471. printf("Could not add User-Name\n");
  472. goto fail;
  473. }
  474. if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
  475. msg) < 0)
  476. goto fail;
  477. /* TODO: should probably check MTU from driver config; 2304 is max for
  478. * IEEE 802.11, but use 1400 to avoid problems with too large packets
  479. */
  480. if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
  481. RADIUS_ATTR_FRAMED_MTU) &&
  482. !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
  483. printf("Could not add Framed-MTU\n");
  484. goto fail;
  485. }
  486. if (eap && !radius_msg_add_eap(msg, eap, len)) {
  487. printf("Could not add EAP-Message\n");
  488. goto fail;
  489. }
  490. /* State attribute must be copied if and only if this packet is
  491. * Access-Request reply to the previous Access-Challenge */
  492. if (sm->last_recv_radius &&
  493. radius_msg_get_hdr(sm->last_recv_radius)->code ==
  494. RADIUS_CODE_ACCESS_CHALLENGE) {
  495. int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
  496. RADIUS_ATTR_STATE);
  497. if (res < 0) {
  498. printf("Could not copy State attribute from previous "
  499. "Access-Challenge\n");
  500. goto fail;
  501. }
  502. if (res > 0) {
  503. wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
  504. }
  505. }
  506. if (hapd->conf->radius_request_cui) {
  507. const u8 *cui;
  508. size_t cui_len;
  509. /* Add previously learned CUI or nul CUI to request CUI */
  510. if (sm->radius_cui) {
  511. cui = wpabuf_head(sm->radius_cui);
  512. cui_len = wpabuf_len(sm->radius_cui);
  513. } else {
  514. cui = (const u8 *) "\0";
  515. cui_len = 1;
  516. }
  517. if (!radius_msg_add_attr(msg,
  518. RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
  519. cui, cui_len)) {
  520. wpa_printf(MSG_ERROR, "Could not add CUI");
  521. goto fail;
  522. }
  523. }
  524. if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
  525. goto fail;
  526. return;
  527. fail:
  528. radius_msg_free(msg);
  529. }
  530. #endif /* CONFIG_NO_RADIUS */
  531. static void handle_eap_response(struct hostapd_data *hapd,
  532. struct sta_info *sta, struct eap_hdr *eap,
  533. size_t len)
  534. {
  535. u8 type, *data;
  536. struct eapol_state_machine *sm = sta->eapol_sm;
  537. if (sm == NULL)
  538. return;
  539. data = (u8 *) (eap + 1);
  540. if (len < sizeof(*eap) + 1) {
  541. printf("handle_eap_response: too short response data\n");
  542. return;
  543. }
  544. sm->eap_type_supp = type = data[0];
  545. hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
  546. HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
  547. "id=%d len=%d) from STA: EAP Response-%s (%d)",
  548. eap->code, eap->identifier, be_to_host16(eap->length),
  549. eap_server_get_name(0, type), type);
  550. sm->dot1xAuthEapolRespFramesRx++;
  551. wpabuf_free(sm->eap_if->eapRespData);
  552. sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
  553. sm->eapolEap = TRUE;
  554. }
  555. /* Process incoming EAP packet from Supplicant */
  556. static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
  557. u8 *buf, size_t len)
  558. {
  559. struct eap_hdr *eap;
  560. u16 eap_len;
  561. if (len < sizeof(*eap)) {
  562. printf(" too short EAP packet\n");
  563. return;
  564. }
  565. eap = (struct eap_hdr *) buf;
  566. eap_len = be_to_host16(eap->length);
  567. wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
  568. eap->code, eap->identifier, eap_len);
  569. if (eap_len < sizeof(*eap)) {
  570. wpa_printf(MSG_DEBUG, " Invalid EAP length");
  571. return;
  572. } else if (eap_len > len) {
  573. wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
  574. "packet");
  575. return;
  576. } else if (eap_len < len) {
  577. wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
  578. "packet", (unsigned long) len - eap_len);
  579. }
  580. switch (eap->code) {
  581. case EAP_CODE_REQUEST:
  582. wpa_printf(MSG_DEBUG, " (request)");
  583. return;
  584. case EAP_CODE_RESPONSE:
  585. wpa_printf(MSG_DEBUG, " (response)");
  586. handle_eap_response(hapd, sta, eap, eap_len);
  587. break;
  588. case EAP_CODE_SUCCESS:
  589. wpa_printf(MSG_DEBUG, " (success)");
  590. return;
  591. case EAP_CODE_FAILURE:
  592. wpa_printf(MSG_DEBUG, " (failure)");
  593. return;
  594. default:
  595. wpa_printf(MSG_DEBUG, " (unknown code)");
  596. return;
  597. }
  598. }
  599. static struct eapol_state_machine *
  600. ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
  601. {
  602. int flags = 0;
  603. if (sta->flags & WLAN_STA_PREAUTH)
  604. flags |= EAPOL_SM_PREAUTH;
  605. if (sta->wpa_sm) {
  606. flags |= EAPOL_SM_USES_WPA;
  607. if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
  608. flags |= EAPOL_SM_FROM_PMKSA_CACHE;
  609. }
  610. return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
  611. sta->wps_ie, sta->p2p_ie, sta,
  612. sta->identity, sta->radius_cui);
  613. }
  614. /**
  615. * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
  616. * @hapd: hostapd BSS data
  617. * @sa: Source address (sender of the EAPOL frame)
  618. * @buf: EAPOL frame
  619. * @len: Length of buf in octets
  620. *
  621. * This function is called for each incoming EAPOL frame from the interface
  622. */
  623. void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
  624. size_t len)
  625. {
  626. struct sta_info *sta;
  627. struct ieee802_1x_hdr *hdr;
  628. struct ieee802_1x_eapol_key *key;
  629. u16 datalen;
  630. struct rsn_pmksa_cache_entry *pmksa;
  631. int key_mgmt;
  632. if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
  633. !hapd->conf->wps_state)
  634. return;
  635. wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
  636. (unsigned long) len, MAC2STR(sa));
  637. sta = ap_get_sta(hapd, sa);
  638. if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
  639. !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
  640. wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
  641. "associated/Pre-authenticating STA");
  642. return;
  643. }
  644. if (len < sizeof(*hdr)) {
  645. printf(" too short IEEE 802.1X packet\n");
  646. return;
  647. }
  648. hdr = (struct ieee802_1x_hdr *) buf;
  649. datalen = be_to_host16(hdr->length);
  650. wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
  651. hdr->version, hdr->type, datalen);
  652. if (len - sizeof(*hdr) < datalen) {
  653. printf(" frame too short for this IEEE 802.1X packet\n");
  654. if (sta->eapol_sm)
  655. sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
  656. return;
  657. }
  658. if (len - sizeof(*hdr) > datalen) {
  659. wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
  660. "IEEE 802.1X packet",
  661. (unsigned long) len - sizeof(*hdr) - datalen);
  662. }
  663. if (sta->eapol_sm) {
  664. sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
  665. sta->eapol_sm->dot1xAuthEapolFramesRx++;
  666. }
  667. key = (struct ieee802_1x_eapol_key *) (hdr + 1);
  668. if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
  669. hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
  670. (key->type == EAPOL_KEY_TYPE_WPA ||
  671. key->type == EAPOL_KEY_TYPE_RSN)) {
  672. wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
  673. sizeof(*hdr) + datalen);
  674. return;
  675. }
  676. if (!hapd->conf->ieee802_1x &&
  677. !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
  678. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
  679. "802.1X not enabled and WPS not used");
  680. return;
  681. }
  682. key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
  683. if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
  684. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
  685. "STA is using PSK");
  686. return;
  687. }
  688. if (!sta->eapol_sm) {
  689. sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
  690. if (!sta->eapol_sm)
  691. return;
  692. #ifdef CONFIG_WPS
  693. if (!hapd->conf->ieee802_1x) {
  694. u32 wflags = sta->flags & (WLAN_STA_WPS |
  695. WLAN_STA_WPS2 |
  696. WLAN_STA_MAYBE_WPS);
  697. if (wflags == WLAN_STA_MAYBE_WPS ||
  698. wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
  699. /*
  700. * Delay EAPOL frame transmission until a
  701. * possible WPS STA initiates the handshake
  702. * with EAPOL-Start. Only allow the wait to be
  703. * skipped if the STA is known to support WPS
  704. * 2.0.
  705. */
  706. wpa_printf(MSG_DEBUG, "WPS: Do not start "
  707. "EAPOL until EAPOL-Start is "
  708. "received");
  709. sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
  710. }
  711. }
  712. #endif /* CONFIG_WPS */
  713. sta->eapol_sm->eap_if->portEnabled = TRUE;
  714. }
  715. /* since we support version 1, we can ignore version field and proceed
  716. * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
  717. /* TODO: actually, we are not version 1 anymore.. However, Version 2
  718. * does not change frame contents, so should be ok to process frames
  719. * more or less identically. Some changes might be needed for
  720. * verification of fields. */
  721. switch (hdr->type) {
  722. case IEEE802_1X_TYPE_EAP_PACKET:
  723. handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
  724. break;
  725. case IEEE802_1X_TYPE_EAPOL_START:
  726. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  727. HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
  728. "from STA");
  729. sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
  730. pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
  731. if (pmksa) {
  732. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
  733. HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
  734. "available - ignore it since "
  735. "STA sent EAPOL-Start");
  736. wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
  737. }
  738. sta->eapol_sm->eapolStart = TRUE;
  739. sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
  740. eap_server_clear_identity(sta->eapol_sm->eap);
  741. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
  742. break;
  743. case IEEE802_1X_TYPE_EAPOL_LOGOFF:
  744. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  745. HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
  746. "from STA");
  747. sta->acct_terminate_cause =
  748. RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  749. accounting_sta_stop(hapd, sta);
  750. sta->eapol_sm->eapolLogoff = TRUE;
  751. sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
  752. eap_server_clear_identity(sta->eapol_sm->eap);
  753. break;
  754. case IEEE802_1X_TYPE_EAPOL_KEY:
  755. wpa_printf(MSG_DEBUG, " EAPOL-Key");
  756. if (!ap_sta_is_authorized(sta)) {
  757. wpa_printf(MSG_DEBUG, " Dropped key data from "
  758. "unauthorized Supplicant");
  759. break;
  760. }
  761. break;
  762. case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
  763. wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
  764. /* TODO: implement support for this; show data */
  765. break;
  766. default:
  767. wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
  768. sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
  769. break;
  770. }
  771. eapol_auth_step(sta->eapol_sm);
  772. }
  773. /**
  774. * ieee802_1x_new_station - Start IEEE 802.1X authentication
  775. * @hapd: hostapd BSS data
  776. * @sta: The station
  777. *
  778. * This function is called to start IEEE 802.1X authentication when a new
  779. * station completes IEEE 802.11 association.
  780. */
  781. void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
  782. {
  783. struct rsn_pmksa_cache_entry *pmksa;
  784. int reassoc = 1;
  785. int force_1x = 0;
  786. int key_mgmt;
  787. #ifdef CONFIG_WPS
  788. if (hapd->conf->wps_state && hapd->conf->wpa &&
  789. (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
  790. /*
  791. * Need to enable IEEE 802.1X/EAPOL state machines for possible
  792. * WPS handshake even if IEEE 802.1X/EAPOL is not used for
  793. * authentication in this BSS.
  794. */
  795. force_1x = 1;
  796. }
  797. #endif /* CONFIG_WPS */
  798. if (!force_1x && !hapd->conf->ieee802_1x) {
  799. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
  800. "802.1X not enabled or forced for WPS");
  801. /*
  802. * Clear any possible EAPOL authenticator state to support
  803. * reassociation change from WPS to PSK.
  804. */
  805. ieee802_1x_free_station(sta);
  806. return;
  807. }
  808. key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
  809. if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
  810. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
  811. /*
  812. * Clear any possible EAPOL authenticator state to support
  813. * reassociation change from WPA-EAP to PSK.
  814. */
  815. ieee802_1x_free_station(sta);
  816. return;
  817. }
  818. if (sta->eapol_sm == NULL) {
  819. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  820. HOSTAPD_LEVEL_DEBUG, "start authentication");
  821. sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
  822. if (sta->eapol_sm == NULL) {
  823. hostapd_logger(hapd, sta->addr,
  824. HOSTAPD_MODULE_IEEE8021X,
  825. HOSTAPD_LEVEL_INFO,
  826. "failed to allocate state machine");
  827. return;
  828. }
  829. reassoc = 0;
  830. }
  831. #ifdef CONFIG_WPS
  832. sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
  833. if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS2)) {
  834. /*
  835. * Delay EAPOL frame transmission until a possible WPS STA
  836. * initiates the handshake with EAPOL-Start. Only allow the
  837. * wait to be skipped if the STA is known to support WPS 2.0.
  838. */
  839. wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
  840. "EAPOL-Start is received");
  841. sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
  842. }
  843. #endif /* CONFIG_WPS */
  844. sta->eapol_sm->eap_if->portEnabled = TRUE;
  845. #ifdef CONFIG_IEEE80211R
  846. if (sta->auth_alg == WLAN_AUTH_FT) {
  847. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  848. HOSTAPD_LEVEL_DEBUG,
  849. "PMK from FT - skip IEEE 802.1X/EAP");
  850. /* Setup EAPOL state machines to already authenticated state
  851. * because of existing FT information from R0KH. */
  852. sta->eapol_sm->keyRun = TRUE;
  853. sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
  854. sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
  855. sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
  856. sta->eapol_sm->authSuccess = TRUE;
  857. sta->eapol_sm->authFail = FALSE;
  858. if (sta->eapol_sm->eap)
  859. eap_sm_notify_cached(sta->eapol_sm->eap);
  860. /* TODO: get vlan_id from R0KH using RRB message */
  861. return;
  862. }
  863. #endif /* CONFIG_IEEE80211R */
  864. pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
  865. if (pmksa) {
  866. int old_vlanid;
  867. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  868. HOSTAPD_LEVEL_DEBUG,
  869. "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
  870. /* Setup EAPOL state machines to already authenticated state
  871. * because of existing PMKSA information in the cache. */
  872. sta->eapol_sm->keyRun = TRUE;
  873. sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
  874. sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
  875. sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
  876. sta->eapol_sm->authSuccess = TRUE;
  877. sta->eapol_sm->authFail = FALSE;
  878. if (sta->eapol_sm->eap)
  879. eap_sm_notify_cached(sta->eapol_sm->eap);
  880. old_vlanid = sta->vlan_id;
  881. pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
  882. if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  883. sta->vlan_id = 0;
  884. ap_sta_bind_vlan(hapd, sta, old_vlanid);
  885. } else {
  886. if (reassoc) {
  887. /*
  888. * Force EAPOL state machines to start
  889. * re-authentication without having to wait for the
  890. * Supplicant to send EAPOL-Start.
  891. */
  892. sta->eapol_sm->reAuthenticate = TRUE;
  893. }
  894. eapol_auth_step(sta->eapol_sm);
  895. }
  896. }
  897. void ieee802_1x_free_station(struct sta_info *sta)
  898. {
  899. struct eapol_state_machine *sm = sta->eapol_sm;
  900. if (sm == NULL)
  901. return;
  902. sta->eapol_sm = NULL;
  903. #ifndef CONFIG_NO_RADIUS
  904. radius_msg_free(sm->last_recv_radius);
  905. radius_free_class(&sm->radius_class);
  906. wpabuf_free(sm->radius_cui);
  907. #endif /* CONFIG_NO_RADIUS */
  908. os_free(sm->identity);
  909. eapol_auth_free(sm);
  910. }
  911. #ifndef CONFIG_NO_RADIUS
  912. static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
  913. struct sta_info *sta)
  914. {
  915. struct wpabuf *eap;
  916. const struct eap_hdr *hdr;
  917. int eap_type = -1;
  918. char buf[64];
  919. struct radius_msg *msg;
  920. struct eapol_state_machine *sm = sta->eapol_sm;
  921. if (sm == NULL || sm->last_recv_radius == NULL) {
  922. if (sm)
  923. sm->eap_if->aaaEapNoReq = TRUE;
  924. return;
  925. }
  926. msg = sm->last_recv_radius;
  927. eap = radius_msg_get_eap(msg);
  928. if (eap == NULL) {
  929. /* RFC 3579, Chap. 2.6.3:
  930. * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
  931. * attribute */
  932. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  933. HOSTAPD_LEVEL_WARNING, "could not extract "
  934. "EAP-Message from RADIUS message");
  935. sm->eap_if->aaaEapNoReq = TRUE;
  936. return;
  937. }
  938. if (wpabuf_len(eap) < sizeof(*hdr)) {
  939. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  940. HOSTAPD_LEVEL_WARNING, "too short EAP packet "
  941. "received from authentication server");
  942. wpabuf_free(eap);
  943. sm->eap_if->aaaEapNoReq = TRUE;
  944. return;
  945. }
  946. if (wpabuf_len(eap) > sizeof(*hdr))
  947. eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
  948. hdr = wpabuf_head(eap);
  949. switch (hdr->code) {
  950. case EAP_CODE_REQUEST:
  951. if (eap_type >= 0)
  952. sm->eap_type_authsrv = eap_type;
  953. os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
  954. eap_type >= 0 ? eap_server_get_name(0, eap_type) :
  955. "??",
  956. eap_type);
  957. break;
  958. case EAP_CODE_RESPONSE:
  959. os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
  960. eap_type >= 0 ? eap_server_get_name(0, eap_type) :
  961. "??",
  962. eap_type);
  963. break;
  964. case EAP_CODE_SUCCESS:
  965. os_strlcpy(buf, "EAP Success", sizeof(buf));
  966. break;
  967. case EAP_CODE_FAILURE:
  968. os_strlcpy(buf, "EAP Failure", sizeof(buf));
  969. break;
  970. default:
  971. os_strlcpy(buf, "unknown EAP code", sizeof(buf));
  972. break;
  973. }
  974. buf[sizeof(buf) - 1] = '\0';
  975. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  976. HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
  977. "id=%d len=%d) from RADIUS server: %s",
  978. hdr->code, hdr->identifier, be_to_host16(hdr->length),
  979. buf);
  980. sm->eap_if->aaaEapReq = TRUE;
  981. wpabuf_free(sm->eap_if->aaaEapReqData);
  982. sm->eap_if->aaaEapReqData = eap;
  983. }
  984. static void ieee802_1x_get_keys(struct hostapd_data *hapd,
  985. struct sta_info *sta, struct radius_msg *msg,
  986. struct radius_msg *req,
  987. const u8 *shared_secret,
  988. size_t shared_secret_len)
  989. {
  990. struct radius_ms_mppe_keys *keys;
  991. struct eapol_state_machine *sm = sta->eapol_sm;
  992. if (sm == NULL)
  993. return;
  994. keys = radius_msg_get_ms_keys(msg, req, shared_secret,
  995. shared_secret_len);
  996. if (keys && keys->send && keys->recv) {
  997. size_t len = keys->send_len + keys->recv_len;
  998. wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
  999. keys->send, keys->send_len);
  1000. wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
  1001. keys->recv, keys->recv_len);
  1002. os_free(sm->eap_if->aaaEapKeyData);
  1003. sm->eap_if->aaaEapKeyData = os_malloc(len);
  1004. if (sm->eap_if->aaaEapKeyData) {
  1005. os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
  1006. keys->recv_len);
  1007. os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
  1008. keys->send, keys->send_len);
  1009. sm->eap_if->aaaEapKeyDataLen = len;
  1010. sm->eap_if->aaaEapKeyAvailable = TRUE;
  1011. }
  1012. }
  1013. if (keys) {
  1014. os_free(keys->send);
  1015. os_free(keys->recv);
  1016. os_free(keys);
  1017. }
  1018. }
  1019. static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
  1020. struct sta_info *sta,
  1021. struct radius_msg *msg)
  1022. {
  1023. u8 *class;
  1024. size_t class_len;
  1025. struct eapol_state_machine *sm = sta->eapol_sm;
  1026. int count, i;
  1027. struct radius_attr_data *nclass;
  1028. size_t nclass_count;
  1029. if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
  1030. sm == NULL)
  1031. return;
  1032. radius_free_class(&sm->radius_class);
  1033. count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
  1034. if (count <= 0)
  1035. return;
  1036. nclass = os_calloc(count, sizeof(struct radius_attr_data));
  1037. if (nclass == NULL)
  1038. return;
  1039. nclass_count = 0;
  1040. class = NULL;
  1041. for (i = 0; i < count; i++) {
  1042. do {
  1043. if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
  1044. &class, &class_len,
  1045. class) < 0) {
  1046. i = count;
  1047. break;
  1048. }
  1049. } while (class_len < 1);
  1050. nclass[nclass_count].data = os_malloc(class_len);
  1051. if (nclass[nclass_count].data == NULL)
  1052. break;
  1053. os_memcpy(nclass[nclass_count].data, class, class_len);
  1054. nclass[nclass_count].len = class_len;
  1055. nclass_count++;
  1056. }
  1057. sm->radius_class.attr = nclass;
  1058. sm->radius_class.count = nclass_count;
  1059. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
  1060. "attributes for " MACSTR,
  1061. (unsigned long) sm->radius_class.count,
  1062. MAC2STR(sta->addr));
  1063. }
  1064. /* Update sta->identity based on User-Name attribute in Access-Accept */
  1065. static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
  1066. struct sta_info *sta,
  1067. struct radius_msg *msg)
  1068. {
  1069. u8 *buf, *identity;
  1070. size_t len;
  1071. struct eapol_state_machine *sm = sta->eapol_sm;
  1072. if (sm == NULL)
  1073. return;
  1074. if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
  1075. NULL) < 0)
  1076. return;
  1077. identity = os_malloc(len + 1);
  1078. if (identity == NULL)
  1079. return;
  1080. os_memcpy(identity, buf, len);
  1081. identity[len] = '\0';
  1082. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  1083. HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
  1084. "User-Name from Access-Accept '%s'",
  1085. sm->identity ? (char *) sm->identity : "N/A",
  1086. (char *) identity);
  1087. os_free(sm->identity);
  1088. sm->identity = identity;
  1089. sm->identity_len = len;
  1090. }
  1091. /* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
  1092. static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
  1093. struct sta_info *sta,
  1094. struct radius_msg *msg)
  1095. {
  1096. struct eapol_state_machine *sm = sta->eapol_sm;
  1097. struct wpabuf *cui;
  1098. u8 *buf;
  1099. size_t len;
  1100. if (sm == NULL)
  1101. return;
  1102. if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
  1103. &buf, &len, NULL) < 0)
  1104. return;
  1105. cui = wpabuf_alloc_copy(buf, len);
  1106. if (cui == NULL)
  1107. return;
  1108. wpabuf_free(sm->radius_cui);
  1109. sm->radius_cui = cui;
  1110. }
  1111. struct sta_id_search {
  1112. u8 identifier;
  1113. struct eapol_state_machine *sm;
  1114. };
  1115. static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
  1116. struct sta_info *sta,
  1117. void *ctx)
  1118. {
  1119. struct sta_id_search *id_search = ctx;
  1120. struct eapol_state_machine *sm = sta->eapol_sm;
  1121. if (sm && sm->radius_identifier >= 0 &&
  1122. sm->radius_identifier == id_search->identifier) {
  1123. id_search->sm = sm;
  1124. return 1;
  1125. }
  1126. return 0;
  1127. }
  1128. static struct eapol_state_machine *
  1129. ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
  1130. {
  1131. struct sta_id_search id_search;
  1132. id_search.identifier = identifier;
  1133. id_search.sm = NULL;
  1134. ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
  1135. return id_search.sm;
  1136. }
  1137. /**
  1138. * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
  1139. * @msg: RADIUS response message
  1140. * @req: RADIUS request message
  1141. * @shared_secret: RADIUS shared secret
  1142. * @shared_secret_len: Length of shared_secret in octets
  1143. * @data: Context data (struct hostapd_data *)
  1144. * Returns: Processing status
  1145. */
  1146. static RadiusRxResult
  1147. ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
  1148. const u8 *shared_secret, size_t shared_secret_len,
  1149. void *data)
  1150. {
  1151. struct hostapd_data *hapd = data;
  1152. struct sta_info *sta;
  1153. u32 session_timeout = 0, termination_action, acct_interim_interval;
  1154. int session_timeout_set, old_vlanid = 0;
  1155. struct eapol_state_machine *sm;
  1156. int override_eapReq = 0;
  1157. struct radius_hdr *hdr = radius_msg_get_hdr(msg);
  1158. sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
  1159. if (sm == NULL) {
  1160. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
  1161. "station for this RADIUS message");
  1162. return RADIUS_RX_UNKNOWN;
  1163. }
  1164. sta = sm->sta;
  1165. /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
  1166. * present when packet contains an EAP-Message attribute */
  1167. if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
  1168. radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
  1169. 0) < 0 &&
  1170. radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
  1171. wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
  1172. "Message-Authenticator since it does not include "
  1173. "EAP-Message");
  1174. } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
  1175. req, 1)) {
  1176. printf("Incoming RADIUS packet did not have correct "
  1177. "Message-Authenticator - dropped\n");
  1178. return RADIUS_RX_INVALID_AUTHENTICATOR;
  1179. }
  1180. if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
  1181. hdr->code != RADIUS_CODE_ACCESS_REJECT &&
  1182. hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
  1183. printf("Unknown RADIUS message code\n");
  1184. return RADIUS_RX_UNKNOWN;
  1185. }
  1186. sm->radius_identifier = -1;
  1187. wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
  1188. MAC2STR(sta->addr));
  1189. radius_msg_free(sm->last_recv_radius);
  1190. sm->last_recv_radius = msg;
  1191. session_timeout_set =
  1192. !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
  1193. &session_timeout);
  1194. if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
  1195. &termination_action))
  1196. termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
  1197. if (hapd->conf->acct_interim_interval == 0 &&
  1198. hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
  1199. radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
  1200. &acct_interim_interval) == 0) {
  1201. if (acct_interim_interval < 60) {
  1202. hostapd_logger(hapd, sta->addr,
  1203. HOSTAPD_MODULE_IEEE8021X,
  1204. HOSTAPD_LEVEL_INFO,
  1205. "ignored too small "
  1206. "Acct-Interim-Interval %d",
  1207. acct_interim_interval);
  1208. } else
  1209. sta->acct_interim_interval = acct_interim_interval;
  1210. }
  1211. switch (hdr->code) {
  1212. case RADIUS_CODE_ACCESS_ACCEPT:
  1213. if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  1214. sta->vlan_id = 0;
  1215. #ifndef CONFIG_NO_VLAN
  1216. else {
  1217. old_vlanid = sta->vlan_id;
  1218. sta->vlan_id = radius_msg_get_vlanid(msg);
  1219. }
  1220. if (sta->vlan_id > 0 &&
  1221. hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  1222. sta->vlan_id)) {
  1223. hostapd_logger(hapd, sta->addr,
  1224. HOSTAPD_MODULE_RADIUS,
  1225. HOSTAPD_LEVEL_INFO,
  1226. "VLAN ID %d", sta->vlan_id);
  1227. } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
  1228. sta->eapol_sm->authFail = TRUE;
  1229. hostapd_logger(hapd, sta->addr,
  1230. HOSTAPD_MODULE_IEEE8021X,
  1231. HOSTAPD_LEVEL_INFO, "authentication "
  1232. "server did not include required VLAN "
  1233. "ID in Access-Accept");
  1234. break;
  1235. }
  1236. #endif /* CONFIG_NO_VLAN */
  1237. if (ap_sta_bind_vlan(hapd, sta, old_vlanid) < 0)
  1238. break;
  1239. /* RFC 3580, Ch. 3.17 */
  1240. if (session_timeout_set && termination_action ==
  1241. RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
  1242. sm->reAuthPeriod = session_timeout;
  1243. } else if (session_timeout_set)
  1244. ap_sta_session_timeout(hapd, sta, session_timeout);
  1245. sm->eap_if->aaaSuccess = TRUE;
  1246. override_eapReq = 1;
  1247. ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
  1248. shared_secret_len);
  1249. ieee802_1x_store_radius_class(hapd, sta, msg);
  1250. ieee802_1x_update_sta_identity(hapd, sta, msg);
  1251. ieee802_1x_update_sta_cui(hapd, sta, msg);
  1252. if (sm->eap_if->eapKeyAvailable &&
  1253. wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
  1254. session_timeout_set ?
  1255. (int) session_timeout : -1, sm) == 0) {
  1256. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
  1257. HOSTAPD_LEVEL_DEBUG,
  1258. "Added PMKSA cache entry");
  1259. }
  1260. break;
  1261. case RADIUS_CODE_ACCESS_REJECT:
  1262. sm->eap_if->aaaFail = TRUE;
  1263. override_eapReq = 1;
  1264. break;
  1265. case RADIUS_CODE_ACCESS_CHALLENGE:
  1266. sm->eap_if->aaaEapReq = TRUE;
  1267. if (session_timeout_set) {
  1268. /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
  1269. sm->eap_if->aaaMethodTimeout = session_timeout;
  1270. hostapd_logger(hapd, sm->addr,
  1271. HOSTAPD_MODULE_IEEE8021X,
  1272. HOSTAPD_LEVEL_DEBUG,
  1273. "using EAP timeout of %d seconds (from "
  1274. "RADIUS)",
  1275. sm->eap_if->aaaMethodTimeout);
  1276. } else {
  1277. /*
  1278. * Use dynamic retransmission behavior per EAP
  1279. * specification.
  1280. */
  1281. sm->eap_if->aaaMethodTimeout = 0;
  1282. }
  1283. break;
  1284. }
  1285. ieee802_1x_decapsulate_radius(hapd, sta);
  1286. if (override_eapReq)
  1287. sm->eap_if->aaaEapReq = FALSE;
  1288. eapol_auth_step(sm);
  1289. return RADIUS_RX_QUEUED;
  1290. }
  1291. #endif /* CONFIG_NO_RADIUS */
  1292. void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
  1293. {
  1294. struct eapol_state_machine *sm = sta->eapol_sm;
  1295. if (sm == NULL)
  1296. return;
  1297. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  1298. HOSTAPD_LEVEL_DEBUG, "aborting authentication");
  1299. #ifndef CONFIG_NO_RADIUS
  1300. radius_msg_free(sm->last_recv_radius);
  1301. sm->last_recv_radius = NULL;
  1302. #endif /* CONFIG_NO_RADIUS */
  1303. if (sm->eap_if->eapTimeout) {
  1304. /*
  1305. * Disconnect the STA since it did not reply to the last EAP
  1306. * request and we cannot continue EAP processing (EAP-Failure
  1307. * could only be sent if the EAP peer actually replied).
  1308. */
  1309. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
  1310. MAC2STR(sta->addr));
  1311. sm->eap_if->portEnabled = FALSE;
  1312. ap_sta_disconnect(hapd, sta, sta->addr,
  1313. WLAN_REASON_PREV_AUTH_NOT_VALID);
  1314. }
  1315. }
  1316. static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
  1317. {
  1318. struct eapol_authenticator *eapol = hapd->eapol_auth;
  1319. if (hapd->conf->default_wep_key_len < 1)
  1320. return 0;
  1321. os_free(eapol->default_wep_key);
  1322. eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
  1323. if (eapol->default_wep_key == NULL ||
  1324. random_get_bytes(eapol->default_wep_key,
  1325. hapd->conf->default_wep_key_len)) {
  1326. printf("Could not generate random WEP key.\n");
  1327. os_free(eapol->default_wep_key);
  1328. eapol->default_wep_key = NULL;
  1329. return -1;
  1330. }
  1331. wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
  1332. eapol->default_wep_key,
  1333. hapd->conf->default_wep_key_len);
  1334. return 0;
  1335. }
  1336. static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
  1337. struct sta_info *sta, void *ctx)
  1338. {
  1339. if (sta->eapol_sm) {
  1340. sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
  1341. eapol_auth_step(sta->eapol_sm);
  1342. }
  1343. return 0;
  1344. }
  1345. static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
  1346. {
  1347. struct hostapd_data *hapd = eloop_ctx;
  1348. struct eapol_authenticator *eapol = hapd->eapol_auth;
  1349. if (eapol->default_wep_key_idx >= 3)
  1350. eapol->default_wep_key_idx =
  1351. hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
  1352. else
  1353. eapol->default_wep_key_idx++;
  1354. wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
  1355. eapol->default_wep_key_idx);
  1356. if (ieee802_1x_rekey_broadcast(hapd)) {
  1357. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
  1358. HOSTAPD_LEVEL_WARNING, "failed to generate a "
  1359. "new broadcast key");
  1360. os_free(eapol->default_wep_key);
  1361. eapol->default_wep_key = NULL;
  1362. return;
  1363. }
  1364. /* TODO: Could setup key for RX here, but change default TX keyid only
  1365. * after new broadcast key has been sent to all stations. */
  1366. if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
  1367. broadcast_ether_addr,
  1368. eapol->default_wep_key_idx, 1, NULL, 0,
  1369. eapol->default_wep_key,
  1370. hapd->conf->default_wep_key_len)) {
  1371. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
  1372. HOSTAPD_LEVEL_WARNING, "failed to configure a "
  1373. "new broadcast key");
  1374. os_free(eapol->default_wep_key);
  1375. eapol->default_wep_key = NULL;
  1376. return;
  1377. }
  1378. ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
  1379. if (hapd->conf->wep_rekeying_period > 0) {
  1380. eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
  1381. ieee802_1x_rekey, hapd, NULL);
  1382. }
  1383. }
  1384. static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
  1385. const u8 *data, size_t datalen)
  1386. {
  1387. #ifdef CONFIG_WPS
  1388. struct sta_info *sta = sta_ctx;
  1389. if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
  1390. WLAN_STA_MAYBE_WPS) {
  1391. const u8 *identity;
  1392. size_t identity_len;
  1393. struct eapol_state_machine *sm = sta->eapol_sm;
  1394. identity = eap_get_identity(sm->eap, &identity_len);
  1395. if (identity &&
  1396. ((identity_len == WSC_ID_ENROLLEE_LEN &&
  1397. os_memcmp(identity, WSC_ID_ENROLLEE,
  1398. WSC_ID_ENROLLEE_LEN) == 0) ||
  1399. (identity_len == WSC_ID_REGISTRAR_LEN &&
  1400. os_memcmp(identity, WSC_ID_REGISTRAR,
  1401. WSC_ID_REGISTRAR_LEN) == 0))) {
  1402. wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
  1403. "WLAN_STA_WPS");
  1404. sta->flags |= WLAN_STA_WPS;
  1405. }
  1406. }
  1407. #endif /* CONFIG_WPS */
  1408. ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
  1409. }
  1410. static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
  1411. const u8 *data, size_t datalen)
  1412. {
  1413. #ifndef CONFIG_NO_RADIUS
  1414. struct hostapd_data *hapd = ctx;
  1415. struct sta_info *sta = sta_ctx;
  1416. ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
  1417. #endif /* CONFIG_NO_RADIUS */
  1418. }
  1419. static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
  1420. int preauth)
  1421. {
  1422. struct hostapd_data *hapd = ctx;
  1423. struct sta_info *sta = sta_ctx;
  1424. if (preauth)
  1425. rsn_preauth_finished(hapd, sta, success);
  1426. else
  1427. ieee802_1x_finished(hapd, sta, success);
  1428. }
  1429. static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
  1430. size_t identity_len, int phase2,
  1431. struct eap_user *user)
  1432. {
  1433. struct hostapd_data *hapd = ctx;
  1434. const struct hostapd_eap_user *eap_user;
  1435. int i;
  1436. eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
  1437. if (eap_user == NULL)
  1438. return -1;
  1439. os_memset(user, 0, sizeof(*user));
  1440. user->phase2 = phase2;
  1441. for (i = 0; i < EAP_MAX_METHODS; i++) {
  1442. user->methods[i].vendor = eap_user->methods[i].vendor;
  1443. user->methods[i].method = eap_user->methods[i].method;
  1444. }
  1445. if (eap_user->password) {
  1446. user->password = os_malloc(eap_user->password_len);
  1447. if (user->password == NULL)
  1448. return -1;
  1449. os_memcpy(user->password, eap_user->password,
  1450. eap_user->password_len);
  1451. user->password_len = eap_user->password_len;
  1452. user->password_hash = eap_user->password_hash;
  1453. }
  1454. user->force_version = eap_user->force_version;
  1455. user->ttls_auth = eap_user->ttls_auth;
  1456. return 0;
  1457. }
  1458. static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
  1459. {
  1460. struct hostapd_data *hapd = ctx;
  1461. struct sta_info *sta;
  1462. sta = ap_get_sta(hapd, addr);
  1463. if (sta == NULL || sta->eapol_sm == NULL)
  1464. return 0;
  1465. return 1;
  1466. }
  1467. static void ieee802_1x_logger(void *ctx, const u8 *addr,
  1468. eapol_logger_level level, const char *txt)
  1469. {
  1470. #ifndef CONFIG_NO_HOSTAPD_LOGGER
  1471. struct hostapd_data *hapd = ctx;
  1472. int hlevel;
  1473. switch (level) {
  1474. case EAPOL_LOGGER_WARNING:
  1475. hlevel = HOSTAPD_LEVEL_WARNING;
  1476. break;
  1477. case EAPOL_LOGGER_INFO:
  1478. hlevel = HOSTAPD_LEVEL_INFO;
  1479. break;
  1480. case EAPOL_LOGGER_DEBUG:
  1481. default:
  1482. hlevel = HOSTAPD_LEVEL_DEBUG;
  1483. break;
  1484. }
  1485. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
  1486. txt);
  1487. #endif /* CONFIG_NO_HOSTAPD_LOGGER */
  1488. }
  1489. static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
  1490. int authorized)
  1491. {
  1492. struct hostapd_data *hapd = ctx;
  1493. struct sta_info *sta = sta_ctx;
  1494. ieee802_1x_set_sta_authorized(hapd, sta, authorized);
  1495. }
  1496. static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
  1497. {
  1498. struct hostapd_data *hapd = ctx;
  1499. struct sta_info *sta = sta_ctx;
  1500. ieee802_1x_abort_auth(hapd, sta);
  1501. }
  1502. static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
  1503. {
  1504. struct hostapd_data *hapd = ctx;
  1505. struct sta_info *sta = sta_ctx;
  1506. ieee802_1x_tx_key(hapd, sta);
  1507. }
  1508. static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
  1509. enum eapol_event type)
  1510. {
  1511. /* struct hostapd_data *hapd = ctx; */
  1512. struct sta_info *sta = sta_ctx;
  1513. switch (type) {
  1514. case EAPOL_AUTH_SM_CHANGE:
  1515. wpa_auth_sm_notify(sta->wpa_sm);
  1516. break;
  1517. case EAPOL_AUTH_REAUTHENTICATE:
  1518. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
  1519. break;
  1520. }
  1521. }
  1522. int ieee802_1x_init(struct hostapd_data *hapd)
  1523. {
  1524. int i;
  1525. struct eapol_auth_config conf;
  1526. struct eapol_auth_cb cb;
  1527. os_memset(&conf, 0, sizeof(conf));
  1528. conf.ctx = hapd;
  1529. conf.eap_reauth_period = hapd->conf->eap_reauth_period;
  1530. conf.wpa = hapd->conf->wpa;
  1531. conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
  1532. conf.eap_server = hapd->conf->eap_server;
  1533. conf.ssl_ctx = hapd->ssl_ctx;
  1534. conf.msg_ctx = hapd->msg_ctx;
  1535. conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
  1536. conf.eap_req_id_text = hapd->conf->eap_req_id_text;
  1537. conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
  1538. conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
  1539. conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
  1540. conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
  1541. conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
  1542. conf.eap_fast_prov = hapd->conf->eap_fast_prov;
  1543. conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
  1544. conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
  1545. conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
  1546. conf.tnc = hapd->conf->tnc;
  1547. conf.wps = hapd->wps;
  1548. conf.fragment_size = hapd->conf->fragment_size;
  1549. conf.pwd_group = hapd->conf->pwd_group;
  1550. conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
  1551. os_memset(&cb, 0, sizeof(cb));
  1552. cb.eapol_send = ieee802_1x_eapol_send;
  1553. cb.aaa_send = ieee802_1x_aaa_send;
  1554. cb.finished = _ieee802_1x_finished;
  1555. cb.get_eap_user = ieee802_1x_get_eap_user;
  1556. cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
  1557. cb.logger = ieee802_1x_logger;
  1558. cb.set_port_authorized = ieee802_1x_set_port_authorized;
  1559. cb.abort_auth = _ieee802_1x_abort_auth;
  1560. cb.tx_key = _ieee802_1x_tx_key;
  1561. cb.eapol_event = ieee802_1x_eapol_event;
  1562. hapd->eapol_auth = eapol_auth_init(&conf, &cb);
  1563. if (hapd->eapol_auth == NULL)
  1564. return -1;
  1565. if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
  1566. hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
  1567. return -1;
  1568. #ifndef CONFIG_NO_RADIUS
  1569. if (radius_client_register(hapd->radius, RADIUS_AUTH,
  1570. ieee802_1x_receive_auth, hapd))
  1571. return -1;
  1572. #endif /* CONFIG_NO_RADIUS */
  1573. if (hapd->conf->default_wep_key_len) {
  1574. for (i = 0; i < 4; i++)
  1575. hostapd_drv_set_key(hapd->conf->iface, hapd,
  1576. WPA_ALG_NONE, NULL, i, 0, NULL, 0,
  1577. NULL, 0);
  1578. ieee802_1x_rekey(hapd, NULL);
  1579. if (hapd->eapol_auth->default_wep_key == NULL)
  1580. return -1;
  1581. }
  1582. return 0;
  1583. }
  1584. void ieee802_1x_deinit(struct hostapd_data *hapd)
  1585. {
  1586. eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
  1587. if (hapd->driver != NULL &&
  1588. (hapd->conf->ieee802_1x || hapd->conf->wpa))
  1589. hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
  1590. eapol_auth_deinit(hapd->eapol_auth);
  1591. hapd->eapol_auth = NULL;
  1592. }
  1593. int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
  1594. const u8 *buf, size_t len, int ack)
  1595. {
  1596. struct ieee80211_hdr *hdr;
  1597. u8 *pos;
  1598. const unsigned char rfc1042_hdr[ETH_ALEN] =
  1599. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  1600. if (sta == NULL)
  1601. return -1;
  1602. if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
  1603. return 0;
  1604. hdr = (struct ieee80211_hdr *) buf;
  1605. pos = (u8 *) (hdr + 1);
  1606. if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
  1607. return 0;
  1608. pos += sizeof(rfc1042_hdr);
  1609. if (WPA_GET_BE16(pos) != ETH_P_PAE)
  1610. return 0;
  1611. pos += 2;
  1612. return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
  1613. ack);
  1614. }
  1615. int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
  1616. const u8 *buf, int len, int ack)
  1617. {
  1618. const struct ieee802_1x_hdr *xhdr =
  1619. (const struct ieee802_1x_hdr *) buf;
  1620. const u8 *pos = buf + sizeof(*xhdr);
  1621. struct ieee802_1x_eapol_key *key;
  1622. if (len < (int) sizeof(*xhdr))
  1623. return 0;
  1624. wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
  1625. "type=%d length=%d - ack=%d",
  1626. MAC2STR(sta->addr), xhdr->version, xhdr->type,
  1627. be_to_host16(xhdr->length), ack);
  1628. if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
  1629. return 0;
  1630. if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
  1631. const struct wpa_eapol_key *wpa;
  1632. wpa = (const struct wpa_eapol_key *) pos;
  1633. if (wpa->type == EAPOL_KEY_TYPE_RSN ||
  1634. wpa->type == EAPOL_KEY_TYPE_WPA)
  1635. wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
  1636. sta->wpa_sm, ack);
  1637. }
  1638. /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
  1639. * or Authenticator state machines, but EAPOL-Key packets are not
  1640. * retransmitted in case of failure. Try to re-send failed EAPOL-Key
  1641. * packets couple of times because otherwise STA keys become
  1642. * unsynchronized with AP. */
  1643. if (!ack && pos + sizeof(*key) <= buf + len) {
  1644. key = (struct ieee802_1x_eapol_key *) pos;
  1645. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  1646. HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
  1647. "frame (%scast index=%d)",
  1648. key->key_index & BIT(7) ? "uni" : "broad",
  1649. key->key_index & ~BIT(7));
  1650. /* TODO: re-send EAPOL-Key couple of times (with short delay
  1651. * between them?). If all attempt fail, report error and
  1652. * deauthenticate STA so that it will get new keys when
  1653. * authenticating again (e.g., after returning in range).
  1654. * Separate limit/transmit state needed both for unicast and
  1655. * broadcast keys(?) */
  1656. }
  1657. /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
  1658. * to here and change the key only if the EAPOL-Key packet was Acked.
  1659. */
  1660. return 1;
  1661. }
  1662. u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
  1663. {
  1664. if (sm == NULL || sm->identity == NULL)
  1665. return NULL;
  1666. *len = sm->identity_len;
  1667. return sm->identity;
  1668. }
  1669. u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
  1670. int idx)
  1671. {
  1672. if (sm == NULL || sm->radius_class.attr == NULL ||
  1673. idx >= (int) sm->radius_class.count)
  1674. return NULL;
  1675. *len = sm->radius_class.attr[idx].len;
  1676. return sm->radius_class.attr[idx].data;
  1677. }
  1678. struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
  1679. {
  1680. if (sm == NULL)
  1681. return NULL;
  1682. return sm->radius_cui;
  1683. }
  1684. const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
  1685. {
  1686. *len = 0;
  1687. if (sm == NULL)
  1688. return NULL;
  1689. *len = sm->eap_if->eapKeyDataLen;
  1690. return sm->eap_if->eapKeyData;
  1691. }
  1692. void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
  1693. int enabled)
  1694. {
  1695. if (sm == NULL)
  1696. return;
  1697. sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
  1698. eapol_auth_step(sm);
  1699. }
  1700. void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
  1701. int valid)
  1702. {
  1703. if (sm == NULL)
  1704. return;
  1705. sm->portValid = valid ? TRUE : FALSE;
  1706. eapol_auth_step(sm);
  1707. }
  1708. void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
  1709. {
  1710. if (sm == NULL)
  1711. return;
  1712. if (pre_auth)
  1713. sm->flags |= EAPOL_SM_PREAUTH;
  1714. else
  1715. sm->flags &= ~EAPOL_SM_PREAUTH;
  1716. }
  1717. static const char * bool_txt(Boolean bool)
  1718. {
  1719. return bool ? "TRUE" : "FALSE";
  1720. }
  1721. int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  1722. {
  1723. /* TODO */
  1724. return 0;
  1725. }
  1726. int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1727. char *buf, size_t buflen)
  1728. {
  1729. int len = 0, ret;
  1730. struct eapol_state_machine *sm = sta->eapol_sm;
  1731. struct os_time t;
  1732. if (sm == NULL)
  1733. return 0;
  1734. ret = os_snprintf(buf + len, buflen - len,
  1735. "dot1xPaePortNumber=%d\n"
  1736. "dot1xPaePortProtocolVersion=%d\n"
  1737. "dot1xPaePortCapabilities=1\n"
  1738. "dot1xPaePortInitialize=%d\n"
  1739. "dot1xPaePortReauthenticate=FALSE\n",
  1740. sta->aid,
  1741. EAPOL_VERSION,
  1742. sm->initialize);
  1743. if (ret < 0 || (size_t) ret >= buflen - len)
  1744. return len;
  1745. len += ret;
  1746. /* dot1xAuthConfigTable */
  1747. ret = os_snprintf(buf + len, buflen - len,
  1748. "dot1xAuthPaeState=%d\n"
  1749. "dot1xAuthBackendAuthState=%d\n"
  1750. "dot1xAuthAdminControlledDirections=%d\n"
  1751. "dot1xAuthOperControlledDirections=%d\n"
  1752. "dot1xAuthAuthControlledPortStatus=%d\n"
  1753. "dot1xAuthAuthControlledPortControl=%d\n"
  1754. "dot1xAuthQuietPeriod=%u\n"
  1755. "dot1xAuthServerTimeout=%u\n"
  1756. "dot1xAuthReAuthPeriod=%u\n"
  1757. "dot1xAuthReAuthEnabled=%s\n"
  1758. "dot1xAuthKeyTxEnabled=%s\n",
  1759. sm->auth_pae_state + 1,
  1760. sm->be_auth_state + 1,
  1761. sm->adminControlledDirections,
  1762. sm->operControlledDirections,
  1763. sm->authPortStatus,
  1764. sm->portControl,
  1765. sm->quietPeriod,
  1766. sm->serverTimeout,
  1767. sm->reAuthPeriod,
  1768. bool_txt(sm->reAuthEnabled),
  1769. bool_txt(sm->keyTxEnabled));
  1770. if (ret < 0 || (size_t) ret >= buflen - len)
  1771. return len;
  1772. len += ret;
  1773. /* dot1xAuthStatsTable */
  1774. ret = os_snprintf(buf + len, buflen - len,
  1775. "dot1xAuthEapolFramesRx=%u\n"
  1776. "dot1xAuthEapolFramesTx=%u\n"
  1777. "dot1xAuthEapolStartFramesRx=%u\n"
  1778. "dot1xAuthEapolLogoffFramesRx=%u\n"
  1779. "dot1xAuthEapolRespIdFramesRx=%u\n"
  1780. "dot1xAuthEapolRespFramesRx=%u\n"
  1781. "dot1xAuthEapolReqIdFramesTx=%u\n"
  1782. "dot1xAuthEapolReqFramesTx=%u\n"
  1783. "dot1xAuthInvalidEapolFramesRx=%u\n"
  1784. "dot1xAuthEapLengthErrorFramesRx=%u\n"
  1785. "dot1xAuthLastEapolFrameVersion=%u\n"
  1786. "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
  1787. sm->dot1xAuthEapolFramesRx,
  1788. sm->dot1xAuthEapolFramesTx,
  1789. sm->dot1xAuthEapolStartFramesRx,
  1790. sm->dot1xAuthEapolLogoffFramesRx,
  1791. sm->dot1xAuthEapolRespIdFramesRx,
  1792. sm->dot1xAuthEapolRespFramesRx,
  1793. sm->dot1xAuthEapolReqIdFramesTx,
  1794. sm->dot1xAuthEapolReqFramesTx,
  1795. sm->dot1xAuthInvalidEapolFramesRx,
  1796. sm->dot1xAuthEapLengthErrorFramesRx,
  1797. sm->dot1xAuthLastEapolFrameVersion,
  1798. MAC2STR(sm->addr));
  1799. if (ret < 0 || (size_t) ret >= buflen - len)
  1800. return len;
  1801. len += ret;
  1802. /* dot1xAuthDiagTable */
  1803. ret = os_snprintf(buf + len, buflen - len,
  1804. "dot1xAuthEntersConnecting=%u\n"
  1805. "dot1xAuthEapLogoffsWhileConnecting=%u\n"
  1806. "dot1xAuthEntersAuthenticating=%u\n"
  1807. "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
  1808. "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
  1809. "dot1xAuthAuthFailWhileAuthenticating=%u\n"
  1810. "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
  1811. "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
  1812. "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
  1813. "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
  1814. "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
  1815. "dot1xAuthBackendResponses=%u\n"
  1816. "dot1xAuthBackendAccessChallenges=%u\n"
  1817. "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
  1818. "dot1xAuthBackendAuthSuccesses=%u\n"
  1819. "dot1xAuthBackendAuthFails=%u\n",
  1820. sm->authEntersConnecting,
  1821. sm->authEapLogoffsWhileConnecting,
  1822. sm->authEntersAuthenticating,
  1823. sm->authAuthSuccessesWhileAuthenticating,
  1824. sm->authAuthTimeoutsWhileAuthenticating,
  1825. sm->authAuthFailWhileAuthenticating,
  1826. sm->authAuthEapStartsWhileAuthenticating,
  1827. sm->authAuthEapLogoffWhileAuthenticating,
  1828. sm->authAuthReauthsWhileAuthenticated,
  1829. sm->authAuthEapStartsWhileAuthenticated,
  1830. sm->authAuthEapLogoffWhileAuthenticated,
  1831. sm->backendResponses,
  1832. sm->backendAccessChallenges,
  1833. sm->backendOtherRequestsToSupplicant,
  1834. sm->backendAuthSuccesses,
  1835. sm->backendAuthFails);
  1836. if (ret < 0 || (size_t) ret >= buflen - len)
  1837. return len;
  1838. len += ret;
  1839. /* dot1xAuthSessionStatsTable */
  1840. os_get_time(&t);
  1841. ret = os_snprintf(buf + len, buflen - len,
  1842. /* TODO: dot1xAuthSessionOctetsRx */
  1843. /* TODO: dot1xAuthSessionOctetsTx */
  1844. /* TODO: dot1xAuthSessionFramesRx */
  1845. /* TODO: dot1xAuthSessionFramesTx */
  1846. "dot1xAuthSessionId=%08X-%08X\n"
  1847. "dot1xAuthSessionAuthenticMethod=%d\n"
  1848. "dot1xAuthSessionTime=%u\n"
  1849. "dot1xAuthSessionTerminateCause=999\n"
  1850. "dot1xAuthSessionUserName=%s\n",
  1851. sta->acct_session_id_hi, sta->acct_session_id_lo,
  1852. (wpa_key_mgmt_wpa_ieee8021x(
  1853. wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
  1854. 1 : 2,
  1855. (unsigned int) (t.sec - sta->acct_session_start),
  1856. sm->identity);
  1857. if (ret < 0 || (size_t) ret >= buflen - len)
  1858. return len;
  1859. len += ret;
  1860. return len;
  1861. }
  1862. static void ieee802_1x_finished(struct hostapd_data *hapd,
  1863. struct sta_info *sta, int success)
  1864. {
  1865. const u8 *key;
  1866. size_t len;
  1867. /* TODO: get PMKLifetime from WPA parameters */
  1868. static const int dot11RSNAConfigPMKLifetime = 43200;
  1869. key = ieee802_1x_get_key(sta->eapol_sm, &len);
  1870. if (success && key && len >= PMK_LEN &&
  1871. wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
  1872. sta->eapol_sm) == 0) {
  1873. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
  1874. HOSTAPD_LEVEL_DEBUG,
  1875. "Added PMKSA cache entry (IEEE 802.1X)");
  1876. }
  1877. if (!success) {
  1878. /*
  1879. * Many devices require deauthentication after WPS provisioning
  1880. * and some may not be be able to do that themselves, so
  1881. * disconnect the client here. In addition, this may also
  1882. * benefit IEEE 802.1X/EAPOL authentication cases, too since
  1883. * the EAPOL PAE state machine would remain in HELD state for
  1884. * considerable amount of time and some EAP methods, like
  1885. * EAP-FAST with anonymous provisioning, may require another
  1886. * EAPOL authentication to be started to complete connection.
  1887. */
  1888. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "IEEE 802.1X: Force "
  1889. "disconnection after EAP-Failure");
  1890. /* Add a small sleep to increase likelihood of previously
  1891. * requested EAP-Failure TX getting out before this should the
  1892. * driver reorder operations.
  1893. */
  1894. os_sleep(0, 10000);
  1895. ap_sta_disconnect(hapd, sta, sta->addr,
  1896. WLAN_REASON_IEEE_802_1X_AUTH_FAILED);
  1897. }
  1898. }