ieee802_1x.c 54 KB

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