ieee802_1x.c 59 KB

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