ieee802_1x.c 55 KB

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