ieee802_1x.c 58 KB

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