ieee802_1x.c 56 KB

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