ieee802_1x.c 55 KB

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