ieee802_1x.c 72 KB

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