ieee802_1x.c 56 KB

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