ieee802_1x.c 56 KB

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