ieee802_1x.c 55 KB

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