ieee802_1x.c 55 KB

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