ieee802_1x.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  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. return;
  562. wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
  563. (unsigned long) len, MAC2STR(sa));
  564. sta = ap_get_sta(hapd, sa);
  565. if (!sta) {
  566. printf(" no station information available\n");
  567. return;
  568. }
  569. if (len < sizeof(*hdr)) {
  570. printf(" too short IEEE 802.1X packet\n");
  571. return;
  572. }
  573. hdr = (struct ieee802_1x_hdr *) buf;
  574. datalen = be_to_host16(hdr->length);
  575. wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
  576. hdr->version, hdr->type, datalen);
  577. if (len - sizeof(*hdr) < datalen) {
  578. printf(" frame too short for this IEEE 802.1X packet\n");
  579. if (sta->eapol_sm)
  580. sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
  581. return;
  582. }
  583. if (len - sizeof(*hdr) > datalen) {
  584. wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
  585. "IEEE 802.1X packet",
  586. (unsigned long) len - sizeof(*hdr) - datalen);
  587. }
  588. if (sta->eapol_sm) {
  589. sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
  590. sta->eapol_sm->dot1xAuthEapolFramesRx++;
  591. }
  592. key = (struct ieee802_1x_eapol_key *) (hdr + 1);
  593. if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
  594. hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
  595. (key->type == EAPOL_KEY_TYPE_WPA ||
  596. key->type == EAPOL_KEY_TYPE_RSN)) {
  597. wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
  598. sizeof(*hdr) + datalen);
  599. return;
  600. }
  601. if (!hapd->conf->ieee802_1x ||
  602. wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
  603. return;
  604. if (!sta->eapol_sm) {
  605. sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
  606. sta->flags & WLAN_STA_PREAUTH,
  607. sta);
  608. if (!sta->eapol_sm)
  609. return;
  610. }
  611. /* since we support version 1, we can ignore version field and proceed
  612. * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
  613. /* TODO: actually, we are not version 1 anymore.. However, Version 2
  614. * does not change frame contents, so should be ok to process frames
  615. * more or less identically. Some changes might be needed for
  616. * verification of fields. */
  617. switch (hdr->type) {
  618. case IEEE802_1X_TYPE_EAP_PACKET:
  619. handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
  620. break;
  621. case IEEE802_1X_TYPE_EAPOL_START:
  622. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  623. HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
  624. "from STA");
  625. sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
  626. pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
  627. if (pmksa) {
  628. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
  629. HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
  630. "available - ignore it since "
  631. "STA sent EAPOL-Start");
  632. wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
  633. }
  634. sta->eapol_sm->eapolStart = TRUE;
  635. sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
  636. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
  637. break;
  638. case IEEE802_1X_TYPE_EAPOL_LOGOFF:
  639. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  640. HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
  641. "from STA");
  642. sta->acct_terminate_cause =
  643. RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  644. accounting_sta_stop(hapd, sta);
  645. accounting_sta_get_id(hapd, sta);
  646. sta->eapol_sm->eapolLogoff = TRUE;
  647. sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
  648. break;
  649. case IEEE802_1X_TYPE_EAPOL_KEY:
  650. wpa_printf(MSG_DEBUG, " EAPOL-Key");
  651. if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
  652. wpa_printf(MSG_DEBUG, " Dropped key data from "
  653. "unauthorized Supplicant");
  654. break;
  655. }
  656. break;
  657. case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
  658. wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
  659. /* TODO: implement support for this; show data */
  660. break;
  661. default:
  662. wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
  663. sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
  664. break;
  665. }
  666. eapol_auth_step(sta->eapol_sm);
  667. }
  668. void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
  669. {
  670. struct rsn_pmksa_cache_entry *pmksa;
  671. int reassoc = 1;
  672. int force_1x = 0;
  673. if ((!force_1x && !hapd->conf->ieee802_1x) ||
  674. wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
  675. return;
  676. if (sta->eapol_sm == NULL) {
  677. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  678. HOSTAPD_LEVEL_DEBUG, "start authentication");
  679. sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
  680. sta->flags & WLAN_STA_PREAUTH,
  681. sta);
  682. if (sta->eapol_sm == NULL) {
  683. hostapd_logger(hapd, sta->addr,
  684. HOSTAPD_MODULE_IEEE8021X,
  685. HOSTAPD_LEVEL_INFO,
  686. "failed to allocate state machine");
  687. return;
  688. }
  689. reassoc = 0;
  690. }
  691. sta->eapol_sm->eap_if->portEnabled = TRUE;
  692. pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
  693. if (pmksa) {
  694. int old_vlanid;
  695. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  696. HOSTAPD_LEVEL_DEBUG,
  697. "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
  698. /* Setup EAPOL state machines to already authenticated state
  699. * because of existing PMKSA information in the cache. */
  700. sta->eapol_sm->keyRun = TRUE;
  701. sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
  702. sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
  703. sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
  704. sta->eapol_sm->authSuccess = TRUE;
  705. if (sta->eapol_sm->eap)
  706. eap_sm_notify_cached(sta->eapol_sm->eap);
  707. old_vlanid = sta->vlan_id;
  708. pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
  709. if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  710. sta->vlan_id = 0;
  711. ap_sta_bind_vlan(hapd, sta, old_vlanid);
  712. } else {
  713. if (reassoc) {
  714. /*
  715. * Force EAPOL state machines to start
  716. * re-authentication without having to wait for the
  717. * Supplicant to send EAPOL-Start.
  718. */
  719. sta->eapol_sm->reAuthenticate = TRUE;
  720. }
  721. eapol_auth_step(sta->eapol_sm);
  722. }
  723. }
  724. void ieee802_1x_free_radius_class(struct radius_class_data *class)
  725. {
  726. size_t i;
  727. if (class == NULL)
  728. return;
  729. for (i = 0; i < class->count; i++)
  730. os_free(class->attr[i].data);
  731. os_free(class->attr);
  732. class->attr = NULL;
  733. class->count = 0;
  734. }
  735. int ieee802_1x_copy_radius_class(struct radius_class_data *dst,
  736. const struct radius_class_data *src)
  737. {
  738. size_t i;
  739. if (src->attr == NULL)
  740. return 0;
  741. dst->attr = os_zalloc(src->count * sizeof(struct radius_attr_data));
  742. if (dst->attr == NULL)
  743. return -1;
  744. dst->count = 0;
  745. for (i = 0; i < src->count; i++) {
  746. dst->attr[i].data = os_malloc(src->attr[i].len);
  747. if (dst->attr[i].data == NULL)
  748. break;
  749. dst->count++;
  750. os_memcpy(dst->attr[i].data, src->attr[i].data,
  751. src->attr[i].len);
  752. dst->attr[i].len = src->attr[i].len;
  753. }
  754. return 0;
  755. }
  756. void ieee802_1x_free_station(struct sta_info *sta)
  757. {
  758. struct eapol_state_machine *sm = sta->eapol_sm;
  759. eloop_cancel_timeout(ieee802_1x_eap_timeout, sta, NULL);
  760. if (sm == NULL)
  761. return;
  762. sta->eapol_sm = NULL;
  763. if (sm->last_recv_radius) {
  764. radius_msg_free(sm->last_recv_radius);
  765. os_free(sm->last_recv_radius);
  766. }
  767. os_free(sm->identity);
  768. ieee802_1x_free_radius_class(&sm->radius_class);
  769. eapol_auth_free(sm);
  770. }
  771. static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
  772. struct sta_info *sta)
  773. {
  774. u8 *eap;
  775. size_t len;
  776. struct eap_hdr *hdr;
  777. int eap_type = -1;
  778. char buf[64];
  779. struct radius_msg *msg;
  780. struct eapol_state_machine *sm = sta->eapol_sm;
  781. if (sm == NULL || sm->last_recv_radius == NULL) {
  782. if (sm)
  783. sm->eap_if->aaaEapNoReq = TRUE;
  784. return;
  785. }
  786. msg = sm->last_recv_radius;
  787. eap = radius_msg_get_eap(msg, &len);
  788. if (eap == NULL) {
  789. /* RFC 3579, Chap. 2.6.3:
  790. * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
  791. * attribute */
  792. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  793. HOSTAPD_LEVEL_WARNING, "could not extract "
  794. "EAP-Message from RADIUS message");
  795. sm->eap_if->aaaEapNoReq = TRUE;
  796. return;
  797. }
  798. if (len < sizeof(*hdr)) {
  799. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  800. HOSTAPD_LEVEL_WARNING, "too short EAP packet "
  801. "received from authentication server");
  802. os_free(eap);
  803. sm->eap_if->aaaEapNoReq = TRUE;
  804. return;
  805. }
  806. if (len > sizeof(*hdr))
  807. eap_type = eap[sizeof(*hdr)];
  808. hdr = (struct eap_hdr *) eap;
  809. switch (hdr->code) {
  810. case EAP_CODE_REQUEST:
  811. if (eap_type >= 0)
  812. sm->eap_type_authsrv = eap_type;
  813. os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
  814. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  815. eap_type);
  816. break;
  817. case EAP_CODE_RESPONSE:
  818. os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
  819. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  820. eap_type);
  821. break;
  822. case EAP_CODE_SUCCESS:
  823. os_strlcpy(buf, "EAP Success", sizeof(buf));
  824. break;
  825. case EAP_CODE_FAILURE:
  826. os_strlcpy(buf, "EAP Failure", sizeof(buf));
  827. break;
  828. default:
  829. os_strlcpy(buf, "unknown EAP code", sizeof(buf));
  830. break;
  831. }
  832. buf[sizeof(buf) - 1] = '\0';
  833. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  834. HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
  835. "id=%d len=%d) from RADIUS server: %s",
  836. hdr->code, hdr->identifier, be_to_host16(hdr->length),
  837. buf);
  838. sm->eap_if->aaaEapReq = TRUE;
  839. wpabuf_free(sm->eap_if->aaaEapReqData);
  840. sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
  841. }
  842. static void ieee802_1x_get_keys(struct hostapd_data *hapd,
  843. struct sta_info *sta, struct radius_msg *msg,
  844. struct radius_msg *req,
  845. u8 *shared_secret, size_t shared_secret_len)
  846. {
  847. struct radius_ms_mppe_keys *keys;
  848. struct eapol_state_machine *sm = sta->eapol_sm;
  849. if (sm == NULL)
  850. return;
  851. keys = radius_msg_get_ms_keys(msg, req, shared_secret,
  852. shared_secret_len);
  853. if (keys && keys->send && keys->recv) {
  854. size_t len = keys->send_len + keys->recv_len;
  855. wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
  856. keys->send, keys->send_len);
  857. wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
  858. keys->recv, keys->recv_len);
  859. os_free(sm->eap_if->aaaEapKeyData);
  860. sm->eap_if->aaaEapKeyData = os_malloc(len);
  861. if (sm->eap_if->aaaEapKeyData) {
  862. os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
  863. keys->recv_len);
  864. os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
  865. keys->send, keys->send_len);
  866. sm->eap_if->aaaEapKeyDataLen = len;
  867. sm->eap_if->aaaEapKeyAvailable = TRUE;
  868. }
  869. }
  870. if (keys) {
  871. os_free(keys->send);
  872. os_free(keys->recv);
  873. os_free(keys);
  874. }
  875. }
  876. static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
  877. struct sta_info *sta,
  878. struct radius_msg *msg)
  879. {
  880. u8 *class;
  881. size_t class_len;
  882. struct eapol_state_machine *sm = sta->eapol_sm;
  883. int count, i;
  884. struct radius_attr_data *nclass;
  885. size_t nclass_count;
  886. if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
  887. sm == NULL)
  888. return;
  889. ieee802_1x_free_radius_class(&sm->radius_class);
  890. count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
  891. if (count <= 0)
  892. return;
  893. nclass = os_zalloc(count * sizeof(struct radius_attr_data));
  894. if (nclass == NULL)
  895. return;
  896. nclass_count = 0;
  897. class = NULL;
  898. for (i = 0; i < count; i++) {
  899. do {
  900. if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
  901. &class, &class_len,
  902. class) < 0) {
  903. i = count;
  904. break;
  905. }
  906. } while (class_len < 1);
  907. nclass[nclass_count].data = os_malloc(class_len);
  908. if (nclass[nclass_count].data == NULL)
  909. break;
  910. os_memcpy(nclass[nclass_count].data, class, class_len);
  911. nclass[nclass_count].len = class_len;
  912. nclass_count++;
  913. }
  914. sm->radius_class.attr = nclass;
  915. sm->radius_class.count = nclass_count;
  916. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
  917. "attributes for " MACSTR,
  918. (unsigned long) sm->radius_class.count,
  919. MAC2STR(sta->addr));
  920. }
  921. /* Update sta->identity based on User-Name attribute in Access-Accept */
  922. static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
  923. struct sta_info *sta,
  924. struct radius_msg *msg)
  925. {
  926. u8 *buf, *identity;
  927. size_t len;
  928. struct eapol_state_machine *sm = sta->eapol_sm;
  929. if (sm == NULL)
  930. return;
  931. if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
  932. NULL) < 0)
  933. return;
  934. identity = os_malloc(len + 1);
  935. if (identity == NULL)
  936. return;
  937. os_memcpy(identity, buf, len);
  938. identity[len] = '\0';
  939. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  940. HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
  941. "User-Name from Access-Accept '%s'",
  942. sm->identity ? (char *) sm->identity : "N/A",
  943. (char *) identity);
  944. os_free(sm->identity);
  945. sm->identity = identity;
  946. sm->identity_len = len;
  947. }
  948. struct sta_id_search {
  949. u8 identifier;
  950. struct eapol_state_machine *sm;
  951. };
  952. static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
  953. struct sta_info *sta,
  954. void *ctx)
  955. {
  956. struct sta_id_search *id_search = ctx;
  957. struct eapol_state_machine *sm = sta->eapol_sm;
  958. if (sm && sm->radius_identifier >= 0 &&
  959. sm->radius_identifier == id_search->identifier) {
  960. id_search->sm = sm;
  961. return 1;
  962. }
  963. return 0;
  964. }
  965. static struct eapol_state_machine *
  966. ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
  967. {
  968. struct sta_id_search id_search;
  969. id_search.identifier = identifier;
  970. id_search.sm = NULL;
  971. ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
  972. return id_search.sm;
  973. }
  974. /* Process the RADIUS frames from Authentication Server */
  975. static RadiusRxResult
  976. ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
  977. u8 *shared_secret, size_t shared_secret_len,
  978. void *data)
  979. {
  980. struct hostapd_data *hapd = data;
  981. struct sta_info *sta;
  982. u32 session_timeout = 0, termination_action, acct_interim_interval;
  983. int session_timeout_set, old_vlanid = 0;
  984. int eap_timeout;
  985. struct eapol_state_machine *sm;
  986. int override_eapReq = 0;
  987. sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
  988. if (sm == NULL) {
  989. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
  990. "station for this RADIUS message");
  991. return RADIUS_RX_UNKNOWN;
  992. }
  993. sta = sm->sta;
  994. /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
  995. * present when packet contains an EAP-Message attribute */
  996. if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
  997. radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
  998. 0) < 0 &&
  999. radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
  1000. wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
  1001. "Message-Authenticator since it does not include "
  1002. "EAP-Message");
  1003. } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
  1004. req, 1)) {
  1005. printf("Incoming RADIUS packet did not have correct "
  1006. "Message-Authenticator - dropped\n");
  1007. return RADIUS_RX_INVALID_AUTHENTICATOR;
  1008. }
  1009. if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
  1010. msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
  1011. msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
  1012. printf("Unknown RADIUS message code\n");
  1013. return RADIUS_RX_UNKNOWN;
  1014. }
  1015. sm->radius_identifier = -1;
  1016. wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
  1017. MAC2STR(sta->addr));
  1018. if (sm->last_recv_radius) {
  1019. radius_msg_free(sm->last_recv_radius);
  1020. os_free(sm->last_recv_radius);
  1021. }
  1022. sm->last_recv_radius = msg;
  1023. session_timeout_set =
  1024. !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
  1025. &session_timeout);
  1026. if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
  1027. &termination_action))
  1028. termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
  1029. if (hapd->conf->radius->acct_interim_interval == 0 &&
  1030. msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
  1031. radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
  1032. &acct_interim_interval) == 0) {
  1033. if (acct_interim_interval < 60) {
  1034. hostapd_logger(hapd, sta->addr,
  1035. HOSTAPD_MODULE_IEEE8021X,
  1036. HOSTAPD_LEVEL_INFO,
  1037. "ignored too small "
  1038. "Acct-Interim-Interval %d",
  1039. acct_interim_interval);
  1040. } else
  1041. sta->acct_interim_interval = acct_interim_interval;
  1042. }
  1043. switch (msg->hdr->code) {
  1044. case RADIUS_CODE_ACCESS_ACCEPT:
  1045. if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  1046. sta->vlan_id = 0;
  1047. else {
  1048. old_vlanid = sta->vlan_id;
  1049. sta->vlan_id = radius_msg_get_vlanid(msg);
  1050. }
  1051. if (sta->vlan_id > 0 &&
  1052. hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  1053. sta->vlan_id)) {
  1054. hostapd_logger(hapd, sta->addr,
  1055. HOSTAPD_MODULE_RADIUS,
  1056. HOSTAPD_LEVEL_INFO,
  1057. "VLAN ID %d", sta->vlan_id);
  1058. } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
  1059. sta->eapol_sm->authFail = TRUE;
  1060. hostapd_logger(hapd, sta->addr,
  1061. HOSTAPD_MODULE_IEEE8021X,
  1062. HOSTAPD_LEVEL_INFO, "authentication "
  1063. "server did not include required VLAN "
  1064. "ID in Access-Accept");
  1065. break;
  1066. }
  1067. ap_sta_bind_vlan(hapd, sta, old_vlanid);
  1068. /* RFC 3580, Ch. 3.17 */
  1069. if (session_timeout_set && termination_action ==
  1070. RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
  1071. sm->reAuthPeriod = session_timeout;
  1072. } else if (session_timeout_set)
  1073. ap_sta_session_timeout(hapd, sta, session_timeout);
  1074. sm->eap_if->aaaSuccess = TRUE;
  1075. override_eapReq = 1;
  1076. ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
  1077. shared_secret_len);
  1078. ieee802_1x_store_radius_class(hapd, sta, msg);
  1079. ieee802_1x_update_sta_identity(hapd, sta, msg);
  1080. if (sm->eap_if->eapKeyAvailable &&
  1081. wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
  1082. session_timeout_set ?
  1083. (int) session_timeout : -1, sm) == 0) {
  1084. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
  1085. HOSTAPD_LEVEL_DEBUG,
  1086. "Added PMKSA cache entry");
  1087. }
  1088. break;
  1089. case RADIUS_CODE_ACCESS_REJECT:
  1090. sm->eap_if->aaaFail = TRUE;
  1091. override_eapReq = 1;
  1092. break;
  1093. case RADIUS_CODE_ACCESS_CHALLENGE:
  1094. sm->eap_if->aaaEapReq = TRUE;
  1095. if (session_timeout_set) {
  1096. /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
  1097. eap_timeout = session_timeout;
  1098. } else
  1099. eap_timeout = 30;
  1100. hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
  1101. HOSTAPD_LEVEL_DEBUG,
  1102. "using EAP timeout of %d seconds%s",
  1103. eap_timeout,
  1104. session_timeout_set ? " (from RADIUS)" : "");
  1105. eloop_cancel_timeout(ieee802_1x_eap_timeout, sta, NULL);
  1106. eloop_register_timeout(eap_timeout, 0, ieee802_1x_eap_timeout,
  1107. sta, NULL);
  1108. sm->eap_if->eapTimeout = FALSE;
  1109. break;
  1110. }
  1111. ieee802_1x_decapsulate_radius(hapd, sta);
  1112. if (override_eapReq)
  1113. sm->eap_if->aaaEapReq = FALSE;
  1114. eapol_auth_step(sm);
  1115. return RADIUS_RX_QUEUED;
  1116. }
  1117. void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
  1118. {
  1119. struct eapol_state_machine *sm = sta->eapol_sm;
  1120. if (sm == NULL)
  1121. return;
  1122. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  1123. HOSTAPD_LEVEL_DEBUG, "aborting authentication");
  1124. if (sm->last_recv_radius) {
  1125. radius_msg_free(sm->last_recv_radius);
  1126. os_free(sm->last_recv_radius);
  1127. sm->last_recv_radius = NULL;
  1128. }
  1129. }
  1130. #ifdef HOSTAPD_DUMP_STATE
  1131. static void fprint_char(FILE *f, char c)
  1132. {
  1133. if (c >= 32 && c < 127)
  1134. fprintf(f, "%c", c);
  1135. else
  1136. fprintf(f, "<%02x>", c);
  1137. }
  1138. void ieee802_1x_dump_state(FILE *f, const char *prefix, struct sta_info *sta)
  1139. {
  1140. struct eapol_state_machine *sm = sta->eapol_sm;
  1141. if (sm == NULL)
  1142. return;
  1143. fprintf(f, "%sIEEE 802.1X:\n", prefix);
  1144. if (sm->identity) {
  1145. size_t i;
  1146. fprintf(f, "%sidentity=", prefix);
  1147. for (i = 0; i < sm->identity_len; i++)
  1148. fprint_char(f, sm->identity[i]);
  1149. fprintf(f, "\n");
  1150. }
  1151. fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
  1152. "Supplicant: %d (%s)\n", prefix,
  1153. sm->eap_type_authsrv, eap_type_text(sm->eap_type_authsrv),
  1154. sm->eap_type_supp, eap_type_text(sm->eap_type_supp));
  1155. fprintf(f, "%scached_packets=%s\n", prefix,
  1156. sm->last_recv_radius ? "[RX RADIUS]" : "");
  1157. eapol_auth_dump_state(f, prefix, sm);
  1158. }
  1159. #endif /* HOSTAPD_DUMP_STATE */
  1160. static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
  1161. {
  1162. if (hapd->conf->default_wep_key_len < 1)
  1163. return 0;
  1164. os_free(hapd->default_wep_key);
  1165. hapd->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
  1166. if (hapd->default_wep_key == NULL ||
  1167. os_get_random(hapd->default_wep_key,
  1168. hapd->conf->default_wep_key_len)) {
  1169. printf("Could not generate random WEP key.\n");
  1170. os_free(hapd->default_wep_key);
  1171. hapd->default_wep_key = NULL;
  1172. return -1;
  1173. }
  1174. wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
  1175. hapd->default_wep_key,
  1176. hapd->conf->default_wep_key_len);
  1177. return 0;
  1178. }
  1179. static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
  1180. struct sta_info *sta, void *ctx)
  1181. {
  1182. if (sta->eapol_sm) {
  1183. sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
  1184. eapol_auth_step(sta->eapol_sm);
  1185. }
  1186. return 0;
  1187. }
  1188. static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
  1189. {
  1190. struct hostapd_data *hapd = eloop_ctx;
  1191. if (hapd->default_wep_key_idx >= 3)
  1192. hapd->default_wep_key_idx =
  1193. hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
  1194. else
  1195. hapd->default_wep_key_idx++;
  1196. wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
  1197. hapd->default_wep_key_idx);
  1198. if (ieee802_1x_rekey_broadcast(hapd)) {
  1199. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
  1200. HOSTAPD_LEVEL_WARNING, "failed to generate a "
  1201. "new broadcast key");
  1202. os_free(hapd->default_wep_key);
  1203. hapd->default_wep_key = NULL;
  1204. return;
  1205. }
  1206. /* TODO: Could setup key for RX here, but change default TX keyid only
  1207. * after new broadcast key has been sent to all stations. */
  1208. if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP", NULL,
  1209. hapd->default_wep_key_idx,
  1210. hapd->default_wep_key,
  1211. hapd->conf->default_wep_key_len, 1)) {
  1212. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
  1213. HOSTAPD_LEVEL_WARNING, "failed to configure a "
  1214. "new broadcast key");
  1215. os_free(hapd->default_wep_key);
  1216. hapd->default_wep_key = NULL;
  1217. return;
  1218. }
  1219. ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
  1220. if (hapd->conf->wep_rekeying_period > 0) {
  1221. eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
  1222. ieee802_1x_rekey, hapd, NULL);
  1223. }
  1224. }
  1225. static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
  1226. const u8 *data, size_t datalen)
  1227. {
  1228. ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
  1229. }
  1230. static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
  1231. const u8 *data, size_t datalen)
  1232. {
  1233. struct hostapd_data *hapd = ctx;
  1234. struct sta_info *sta = sta_ctx;
  1235. ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
  1236. }
  1237. static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
  1238. int preauth)
  1239. {
  1240. struct hostapd_data *hapd = ctx;
  1241. struct sta_info *sta = sta_ctx;
  1242. if (preauth)
  1243. rsn_preauth_finished(hapd, sta, success);
  1244. else
  1245. ieee802_1x_finished(hapd, sta, success);
  1246. }
  1247. static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
  1248. size_t identity_len, int phase2,
  1249. struct eap_user *user)
  1250. {
  1251. struct hostapd_data *hapd = ctx;
  1252. const struct hostapd_eap_user *eap_user;
  1253. int i, count;
  1254. eap_user = hostapd_get_eap_user(hapd->conf, identity,
  1255. identity_len, phase2);
  1256. if (eap_user == NULL)
  1257. return -1;
  1258. os_memset(user, 0, sizeof(*user));
  1259. user->phase2 = phase2;
  1260. count = EAP_USER_MAX_METHODS;
  1261. if (count > EAP_MAX_METHODS)
  1262. count = EAP_MAX_METHODS;
  1263. for (i = 0; i < count; i++) {
  1264. user->methods[i].vendor = eap_user->methods[i].vendor;
  1265. user->methods[i].method = eap_user->methods[i].method;
  1266. }
  1267. if (eap_user->password) {
  1268. user->password = os_malloc(eap_user->password_len);
  1269. if (user->password == NULL)
  1270. return -1;
  1271. os_memcpy(user->password, eap_user->password,
  1272. eap_user->password_len);
  1273. user->password_len = eap_user->password_len;
  1274. }
  1275. user->force_version = eap_user->force_version;
  1276. user->ttls_auth = eap_user->ttls_auth;
  1277. return 0;
  1278. }
  1279. static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
  1280. {
  1281. struct hostapd_data *hapd = ctx;
  1282. struct sta_info *sta;
  1283. sta = ap_get_sta(hapd, addr);
  1284. if (sta == NULL || sta->eapol_sm == NULL)
  1285. return 0;
  1286. return 1;
  1287. }
  1288. static void ieee802_1x_logger(void *ctx, const u8 *addr,
  1289. eapol_logger_level level, const char *txt)
  1290. {
  1291. struct hostapd_data *hapd = ctx;
  1292. int hlevel;
  1293. switch (level) {
  1294. case EAPOL_LOGGER_WARNING:
  1295. hlevel = HOSTAPD_LEVEL_WARNING;
  1296. break;
  1297. case EAPOL_LOGGER_INFO:
  1298. hlevel = HOSTAPD_LEVEL_INFO;
  1299. break;
  1300. case EAPOL_LOGGER_DEBUG:
  1301. default:
  1302. hlevel = HOSTAPD_LEVEL_DEBUG;
  1303. break;
  1304. }
  1305. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
  1306. txt);
  1307. }
  1308. static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
  1309. int authorized)
  1310. {
  1311. struct hostapd_data *hapd = ctx;
  1312. struct sta_info *sta = sta_ctx;
  1313. ieee802_1x_set_sta_authorized(hapd, sta, authorized);
  1314. }
  1315. static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
  1316. {
  1317. struct hostapd_data *hapd = ctx;
  1318. struct sta_info *sta = sta_ctx;
  1319. ieee802_1x_abort_auth(hapd, sta);
  1320. }
  1321. static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
  1322. {
  1323. struct hostapd_data *hapd = ctx;
  1324. struct sta_info *sta = sta_ctx;
  1325. ieee802_1x_tx_key(hapd, sta);
  1326. }
  1327. int ieee802_1x_init(struct hostapd_data *hapd)
  1328. {
  1329. int i;
  1330. struct eapol_auth_config conf;
  1331. struct eapol_auth_cb cb;
  1332. os_memset(&conf, 0, sizeof(conf));
  1333. conf.hapd = hapd;
  1334. conf.eap_reauth_period = hapd->conf->eap_reauth_period;
  1335. conf.wpa = hapd->conf->wpa;
  1336. conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
  1337. conf.eap_server = hapd->conf->eap_server;
  1338. conf.ssl_ctx = hapd->ssl_ctx;
  1339. conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
  1340. conf.eap_req_id_text = hapd->conf->eap_req_id_text;
  1341. conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
  1342. conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
  1343. conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
  1344. conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
  1345. conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
  1346. conf.eap_fast_prov = hapd->conf->eap_fast_prov;
  1347. conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
  1348. conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
  1349. conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
  1350. conf.tnc = hapd->conf->tnc;
  1351. os_memset(&cb, 0, sizeof(cb));
  1352. cb.eapol_send = ieee802_1x_eapol_send;
  1353. cb.aaa_send = ieee802_1x_aaa_send;
  1354. cb.finished = _ieee802_1x_finished;
  1355. cb.get_eap_user = ieee802_1x_get_eap_user;
  1356. cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
  1357. cb.logger = ieee802_1x_logger;
  1358. cb.set_port_authorized = ieee802_1x_set_port_authorized;
  1359. cb.abort_auth = _ieee802_1x_abort_auth;
  1360. cb.tx_key = _ieee802_1x_tx_key;
  1361. hapd->eapol_auth = eapol_auth_init(&conf, &cb);
  1362. if (hapd->eapol_auth == NULL)
  1363. return -1;
  1364. if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
  1365. hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1))
  1366. return -1;
  1367. if (radius_client_register(hapd->radius, RADIUS_AUTH,
  1368. ieee802_1x_receive_auth, hapd))
  1369. return -1;
  1370. if (hapd->conf->default_wep_key_len) {
  1371. hostapd_set_privacy(hapd, 1);
  1372. for (i = 0; i < 4; i++)
  1373. hostapd_set_encryption(hapd->conf->iface, hapd,
  1374. "none", NULL, i, NULL, 0, 0);
  1375. ieee802_1x_rekey(hapd, NULL);
  1376. if (hapd->default_wep_key == NULL)
  1377. return -1;
  1378. }
  1379. return 0;
  1380. }
  1381. void ieee802_1x_deinit(struct hostapd_data *hapd)
  1382. {
  1383. eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
  1384. if (hapd->driver != NULL &&
  1385. (hapd->conf->ieee802_1x || hapd->conf->wpa))
  1386. hostapd_set_ieee8021x(hapd->conf->iface, hapd, 0);
  1387. eapol_auth_deinit(hapd->eapol_auth);
  1388. hapd->eapol_auth = NULL;
  1389. }
  1390. int ieee802_1x_reconfig(struct hostapd_data *hapd,
  1391. struct hostapd_config *oldconf,
  1392. struct hostapd_bss_config *oldbss)
  1393. {
  1394. ieee802_1x_deinit(hapd);
  1395. return ieee802_1x_init(hapd);
  1396. }
  1397. int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
  1398. u8 *buf, size_t len, int ack)
  1399. {
  1400. struct ieee80211_hdr *hdr;
  1401. struct ieee802_1x_hdr *xhdr;
  1402. struct ieee802_1x_eapol_key *key;
  1403. u8 *pos;
  1404. const unsigned char rfc1042_hdr[ETH_ALEN] =
  1405. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  1406. if (sta == NULL)
  1407. return -1;
  1408. if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
  1409. return 0;
  1410. hdr = (struct ieee80211_hdr *) buf;
  1411. pos = (u8 *) (hdr + 1);
  1412. if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
  1413. return 0;
  1414. pos += sizeof(rfc1042_hdr);
  1415. if (WPA_GET_BE16(pos) != ETH_P_PAE)
  1416. return 0;
  1417. pos += 2;
  1418. xhdr = (struct ieee802_1x_hdr *) pos;
  1419. pos += sizeof(*xhdr);
  1420. wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
  1421. "type=%d length=%d - ack=%d",
  1422. MAC2STR(sta->addr), xhdr->version, xhdr->type,
  1423. be_to_host16(xhdr->length), ack);
  1424. /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
  1425. * or Authenticator state machines, but EAPOL-Key packets are not
  1426. * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
  1427. * packets couple of times because otherwise STA keys become
  1428. * unsynchronized with AP. */
  1429. if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
  1430. pos + sizeof(*key) <= buf + len) {
  1431. key = (struct ieee802_1x_eapol_key *) pos;
  1432. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  1433. HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
  1434. "frame (%scast index=%d)",
  1435. key->key_index & BIT(7) ? "uni" : "broad",
  1436. key->key_index & ~BIT(7));
  1437. /* TODO: re-send EAPOL-Key couple of times (with short delay
  1438. * between them?). If all attempt fail, report error and
  1439. * deauthenticate STA so that it will get new keys when
  1440. * authenticating again (e.g., after returning in range).
  1441. * Separate limit/transmit state needed both for unicast and
  1442. * broadcast keys(?) */
  1443. }
  1444. /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
  1445. * to here and change the key only if the EAPOL-Key packet was Acked.
  1446. */
  1447. return 1;
  1448. }
  1449. u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
  1450. {
  1451. if (sm == NULL || sm->identity == NULL)
  1452. return NULL;
  1453. *len = sm->identity_len;
  1454. return sm->identity;
  1455. }
  1456. u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
  1457. int idx)
  1458. {
  1459. if (sm == NULL || sm->radius_class.attr == NULL ||
  1460. idx >= (int) sm->radius_class.count)
  1461. return NULL;
  1462. *len = sm->radius_class.attr[idx].len;
  1463. return sm->radius_class.attr[idx].data;
  1464. }
  1465. const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
  1466. {
  1467. if (sm == NULL)
  1468. return NULL;
  1469. *len = sm->eap_if->eapKeyDataLen;
  1470. return sm->eap_if->eapKeyData;
  1471. }
  1472. void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
  1473. int enabled)
  1474. {
  1475. if (sm == NULL)
  1476. return;
  1477. sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
  1478. eapol_auth_step(sm);
  1479. }
  1480. void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
  1481. int valid)
  1482. {
  1483. if (sm == NULL)
  1484. return;
  1485. sm->portValid = valid ? TRUE : FALSE;
  1486. eapol_auth_step(sm);
  1487. }
  1488. void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
  1489. {
  1490. if (sm == NULL)
  1491. return;
  1492. if (pre_auth)
  1493. sm->flags |= EAPOL_SM_PREAUTH;
  1494. else
  1495. sm->flags &= ~EAPOL_SM_PREAUTH;
  1496. }
  1497. static const char * bool_txt(Boolean bool)
  1498. {
  1499. return bool ? "TRUE" : "FALSE";
  1500. }
  1501. int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  1502. {
  1503. /* TODO */
  1504. return 0;
  1505. }
  1506. int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1507. char *buf, size_t buflen)
  1508. {
  1509. int len = 0, ret;
  1510. struct eapol_state_machine *sm = sta->eapol_sm;
  1511. if (sm == NULL)
  1512. return 0;
  1513. ret = os_snprintf(buf + len, buflen - len,
  1514. "dot1xPaePortNumber=%d\n"
  1515. "dot1xPaePortProtocolVersion=%d\n"
  1516. "dot1xPaePortCapabilities=1\n"
  1517. "dot1xPaePortInitialize=%d\n"
  1518. "dot1xPaePortReauthenticate=FALSE\n",
  1519. sta->aid,
  1520. EAPOL_VERSION,
  1521. sm->initialize);
  1522. if (ret < 0 || (size_t) ret >= buflen - len)
  1523. return len;
  1524. len += ret;
  1525. /* dot1xAuthConfigTable */
  1526. ret = os_snprintf(buf + len, buflen - len,
  1527. "dot1xAuthPaeState=%d\n"
  1528. "dot1xAuthBackendAuthState=%d\n"
  1529. "dot1xAuthAdminControlledDirections=%d\n"
  1530. "dot1xAuthOperControlledDirections=%d\n"
  1531. "dot1xAuthAuthControlledPortStatus=%d\n"
  1532. "dot1xAuthAuthControlledPortControl=%d\n"
  1533. "dot1xAuthQuietPeriod=%u\n"
  1534. "dot1xAuthServerTimeout=%u\n"
  1535. "dot1xAuthReAuthPeriod=%u\n"
  1536. "dot1xAuthReAuthEnabled=%s\n"
  1537. "dot1xAuthKeyTxEnabled=%s\n",
  1538. sm->auth_pae_state + 1,
  1539. sm->be_auth_state + 1,
  1540. sm->adminControlledDirections,
  1541. sm->operControlledDirections,
  1542. sm->authPortStatus,
  1543. sm->portControl,
  1544. sm->quietPeriod,
  1545. sm->serverTimeout,
  1546. sm->reAuthPeriod,
  1547. bool_txt(sm->reAuthEnabled),
  1548. bool_txt(sm->keyTxEnabled));
  1549. if (ret < 0 || (size_t) ret >= buflen - len)
  1550. return len;
  1551. len += ret;
  1552. /* dot1xAuthStatsTable */
  1553. ret = os_snprintf(buf + len, buflen - len,
  1554. "dot1xAuthEapolFramesRx=%u\n"
  1555. "dot1xAuthEapolFramesTx=%u\n"
  1556. "dot1xAuthEapolStartFramesRx=%u\n"
  1557. "dot1xAuthEapolLogoffFramesRx=%u\n"
  1558. "dot1xAuthEapolRespIdFramesRx=%u\n"
  1559. "dot1xAuthEapolRespFramesRx=%u\n"
  1560. "dot1xAuthEapolReqIdFramesTx=%u\n"
  1561. "dot1xAuthEapolReqFramesTx=%u\n"
  1562. "dot1xAuthInvalidEapolFramesRx=%u\n"
  1563. "dot1xAuthEapLengthErrorFramesRx=%u\n"
  1564. "dot1xAuthLastEapolFrameVersion=%u\n"
  1565. "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
  1566. sm->dot1xAuthEapolFramesRx,
  1567. sm->dot1xAuthEapolFramesTx,
  1568. sm->dot1xAuthEapolStartFramesRx,
  1569. sm->dot1xAuthEapolLogoffFramesRx,
  1570. sm->dot1xAuthEapolRespIdFramesRx,
  1571. sm->dot1xAuthEapolRespFramesRx,
  1572. sm->dot1xAuthEapolReqIdFramesTx,
  1573. sm->dot1xAuthEapolReqFramesTx,
  1574. sm->dot1xAuthInvalidEapolFramesRx,
  1575. sm->dot1xAuthEapLengthErrorFramesRx,
  1576. sm->dot1xAuthLastEapolFrameVersion,
  1577. MAC2STR(sm->addr));
  1578. if (ret < 0 || (size_t) ret >= buflen - len)
  1579. return len;
  1580. len += ret;
  1581. /* dot1xAuthDiagTable */
  1582. ret = os_snprintf(buf + len, buflen - len,
  1583. "dot1xAuthEntersConnecting=%u\n"
  1584. "dot1xAuthEapLogoffsWhileConnecting=%u\n"
  1585. "dot1xAuthEntersAuthenticating=%u\n"
  1586. "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
  1587. "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
  1588. "dot1xAuthAuthFailWhileAuthenticating=%u\n"
  1589. "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
  1590. "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
  1591. "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
  1592. "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
  1593. "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
  1594. "dot1xAuthBackendResponses=%u\n"
  1595. "dot1xAuthBackendAccessChallenges=%u\n"
  1596. "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
  1597. "dot1xAuthBackendAuthSuccesses=%u\n"
  1598. "dot1xAuthBackendAuthFails=%u\n",
  1599. sm->authEntersConnecting,
  1600. sm->authEapLogoffsWhileConnecting,
  1601. sm->authEntersAuthenticating,
  1602. sm->authAuthSuccessesWhileAuthenticating,
  1603. sm->authAuthTimeoutsWhileAuthenticating,
  1604. sm->authAuthFailWhileAuthenticating,
  1605. sm->authAuthEapStartsWhileAuthenticating,
  1606. sm->authAuthEapLogoffWhileAuthenticating,
  1607. sm->authAuthReauthsWhileAuthenticated,
  1608. sm->authAuthEapStartsWhileAuthenticated,
  1609. sm->authAuthEapLogoffWhileAuthenticated,
  1610. sm->backendResponses,
  1611. sm->backendAccessChallenges,
  1612. sm->backendOtherRequestsToSupplicant,
  1613. sm->backendAuthSuccesses,
  1614. sm->backendAuthFails);
  1615. if (ret < 0 || (size_t) ret >= buflen - len)
  1616. return len;
  1617. len += ret;
  1618. /* dot1xAuthSessionStatsTable */
  1619. ret = os_snprintf(buf + len, buflen - len,
  1620. /* TODO: dot1xAuthSessionOctetsRx */
  1621. /* TODO: dot1xAuthSessionOctetsTx */
  1622. /* TODO: dot1xAuthSessionFramesRx */
  1623. /* TODO: dot1xAuthSessionFramesTx */
  1624. "dot1xAuthSessionId=%08X-%08X\n"
  1625. "dot1xAuthSessionAuthenticMethod=%d\n"
  1626. "dot1xAuthSessionTime=%u\n"
  1627. "dot1xAuthSessionTerminateCause=999\n"
  1628. "dot1xAuthSessionUserName=%s\n",
  1629. sta->acct_session_id_hi, sta->acct_session_id_lo,
  1630. (wpa_key_mgmt_wpa_ieee8021x(
  1631. wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
  1632. 1 : 2,
  1633. (unsigned int) (time(NULL) -
  1634. sta->acct_session_start),
  1635. sm->identity);
  1636. if (ret < 0 || (size_t) ret >= buflen - len)
  1637. return len;
  1638. len += ret;
  1639. return len;
  1640. }
  1641. static void ieee802_1x_finished(struct hostapd_data *hapd,
  1642. struct sta_info *sta, int success)
  1643. {
  1644. const u8 *key;
  1645. size_t len;
  1646. /* TODO: get PMKLifetime from WPA parameters */
  1647. static const int dot11RSNAConfigPMKLifetime = 43200;
  1648. key = ieee802_1x_get_key(sta->eapol_sm, &len);
  1649. if (success && key && len >= PMK_LEN &&
  1650. wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
  1651. sta->eapol_sm) == 0) {
  1652. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
  1653. HOSTAPD_LEVEL_DEBUG,
  1654. "Added PMKSA cache entry (IEEE 802.1X)");
  1655. }
  1656. }