ieee802_1x.c 55 KB

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