ieee802_1x.c 55 KB

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