ieee802_1x.c 54 KB

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