ieee802_1x.c 76 KB

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