ieee802_1x.c 62 KB

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