ieee802_1x.c 55 KB

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