ieee802_1x.c 55 KB

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