ieee802_1x.c 56 KB

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