ieee802_1x.c 56 KB

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