ieee802_1x.c 55 KB

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