ieee802_1x.c 55 KB

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