ieee802_1x.c 53 KB

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