ieee802_1x.c 55 KB

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