ieee802_1x.c 69 KB

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