ieee802_1x.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  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. 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. wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
  603. return;
  604. if (!sta->eapol_sm) {
  605. sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
  606. sta->flags & WLAN_STA_PREAUTH,
  607. sta);
  608. if (!sta->eapol_sm)
  609. return;
  610. }
  611. /* since we support version 1, we can ignore version field and proceed
  612. * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
  613. /* TODO: actually, we are not version 1 anymore.. However, Version 2
  614. * does not change frame contents, so should be ok to process frames
  615. * more or less identically. Some changes might be needed for
  616. * verification of fields. */
  617. switch (hdr->type) {
  618. case IEEE802_1X_TYPE_EAP_PACKET:
  619. handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
  620. break;
  621. case IEEE802_1X_TYPE_EAPOL_START:
  622. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  623. HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
  624. "from STA");
  625. sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
  626. pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
  627. if (pmksa) {
  628. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
  629. HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
  630. "available - ignore it since "
  631. "STA sent EAPOL-Start");
  632. wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
  633. }
  634. sta->eapol_sm->eapolStart = TRUE;
  635. sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
  636. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
  637. break;
  638. case IEEE802_1X_TYPE_EAPOL_LOGOFF:
  639. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  640. HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
  641. "from STA");
  642. sta->acct_terminate_cause =
  643. RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  644. sta->eapol_sm->eapolLogoff = TRUE;
  645. sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
  646. break;
  647. case IEEE802_1X_TYPE_EAPOL_KEY:
  648. wpa_printf(MSG_DEBUG, " EAPOL-Key");
  649. if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
  650. wpa_printf(MSG_DEBUG, " Dropped key data from "
  651. "unauthorized Supplicant");
  652. break;
  653. }
  654. break;
  655. case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
  656. wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
  657. /* TODO: implement support for this; show data */
  658. break;
  659. default:
  660. wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
  661. sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
  662. break;
  663. }
  664. eapol_auth_step(sta->eapol_sm);
  665. }
  666. void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
  667. {
  668. struct rsn_pmksa_cache_entry *pmksa;
  669. int reassoc = 1;
  670. int force_1x = 0;
  671. if ((!force_1x && !hapd->conf->ieee802_1x) ||
  672. wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
  673. return;
  674. if (sta->eapol_sm == NULL) {
  675. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  676. HOSTAPD_LEVEL_DEBUG, "start authentication");
  677. sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
  678. sta->flags & WLAN_STA_PREAUTH,
  679. sta);
  680. if (sta->eapol_sm == NULL) {
  681. hostapd_logger(hapd, sta->addr,
  682. HOSTAPD_MODULE_IEEE8021X,
  683. HOSTAPD_LEVEL_INFO,
  684. "failed to allocate state machine");
  685. return;
  686. }
  687. reassoc = 0;
  688. }
  689. sta->eapol_sm->eap_if->portEnabled = TRUE;
  690. pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
  691. if (pmksa) {
  692. int old_vlanid;
  693. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  694. HOSTAPD_LEVEL_DEBUG,
  695. "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
  696. /* Setup EAPOL state machines to already authenticated state
  697. * because of existing PMKSA information in the cache. */
  698. sta->eapol_sm->keyRun = TRUE;
  699. sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
  700. sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
  701. sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
  702. sta->eapol_sm->authSuccess = TRUE;
  703. if (sta->eapol_sm->eap)
  704. eap_sm_notify_cached(sta->eapol_sm->eap);
  705. old_vlanid = sta->vlan_id;
  706. pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
  707. if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  708. sta->vlan_id = 0;
  709. ap_sta_bind_vlan(hapd, sta, old_vlanid);
  710. } else {
  711. if (reassoc) {
  712. /*
  713. * Force EAPOL state machines to start
  714. * re-authentication without having to wait for the
  715. * Supplicant to send EAPOL-Start.
  716. */
  717. sta->eapol_sm->reAuthenticate = TRUE;
  718. }
  719. eapol_auth_step(sta->eapol_sm);
  720. }
  721. }
  722. void ieee802_1x_free_radius_class(struct radius_class_data *class)
  723. {
  724. size_t i;
  725. if (class == NULL)
  726. return;
  727. for (i = 0; i < class->count; i++)
  728. os_free(class->attr[i].data);
  729. os_free(class->attr);
  730. class->attr = NULL;
  731. class->count = 0;
  732. }
  733. int ieee802_1x_copy_radius_class(struct radius_class_data *dst,
  734. const struct radius_class_data *src)
  735. {
  736. size_t i;
  737. if (src->attr == NULL)
  738. return 0;
  739. dst->attr = os_zalloc(src->count * sizeof(struct radius_attr_data));
  740. if (dst->attr == NULL)
  741. return -1;
  742. dst->count = 0;
  743. for (i = 0; i < src->count; i++) {
  744. dst->attr[i].data = os_malloc(src->attr[i].len);
  745. if (dst->attr[i].data == NULL)
  746. break;
  747. dst->count++;
  748. os_memcpy(dst->attr[i].data, src->attr[i].data,
  749. src->attr[i].len);
  750. dst->attr[i].len = src->attr[i].len;
  751. }
  752. return 0;
  753. }
  754. void ieee802_1x_free_station(struct sta_info *sta)
  755. {
  756. struct eapol_state_machine *sm = sta->eapol_sm;
  757. eloop_cancel_timeout(ieee802_1x_eap_timeout, sta, NULL);
  758. if (sm == NULL)
  759. return;
  760. sta->eapol_sm = NULL;
  761. if (sm->last_recv_radius) {
  762. radius_msg_free(sm->last_recv_radius);
  763. os_free(sm->last_recv_radius);
  764. }
  765. os_free(sm->identity);
  766. ieee802_1x_free_radius_class(&sm->radius_class);
  767. eapol_auth_free(sm);
  768. }
  769. static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
  770. struct sta_info *sta)
  771. {
  772. u8 *eap;
  773. size_t len;
  774. struct eap_hdr *hdr;
  775. int eap_type = -1;
  776. char buf[64];
  777. struct radius_msg *msg;
  778. struct eapol_state_machine *sm = sta->eapol_sm;
  779. if (sm == NULL || sm->last_recv_radius == NULL) {
  780. if (sm)
  781. sm->eap_if->aaaEapNoReq = TRUE;
  782. return;
  783. }
  784. msg = sm->last_recv_radius;
  785. eap = radius_msg_get_eap(msg, &len);
  786. if (eap == NULL) {
  787. /* RFC 3579, Chap. 2.6.3:
  788. * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
  789. * attribute */
  790. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  791. HOSTAPD_LEVEL_WARNING, "could not extract "
  792. "EAP-Message from RADIUS message");
  793. sm->eap_if->aaaEapNoReq = TRUE;
  794. return;
  795. }
  796. if (len < sizeof(*hdr)) {
  797. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  798. HOSTAPD_LEVEL_WARNING, "too short EAP packet "
  799. "received from authentication server");
  800. os_free(eap);
  801. sm->eap_if->aaaEapNoReq = TRUE;
  802. return;
  803. }
  804. if (len > sizeof(*hdr))
  805. eap_type = eap[sizeof(*hdr)];
  806. hdr = (struct eap_hdr *) eap;
  807. switch (hdr->code) {
  808. case EAP_CODE_REQUEST:
  809. if (eap_type >= 0)
  810. sm->eap_type_authsrv = eap_type;
  811. os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
  812. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  813. eap_type);
  814. break;
  815. case EAP_CODE_RESPONSE:
  816. os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
  817. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  818. eap_type);
  819. break;
  820. case EAP_CODE_SUCCESS:
  821. os_strlcpy(buf, "EAP Success", sizeof(buf));
  822. break;
  823. case EAP_CODE_FAILURE:
  824. os_strlcpy(buf, "EAP Failure", sizeof(buf));
  825. break;
  826. default:
  827. os_strlcpy(buf, "unknown EAP code", sizeof(buf));
  828. break;
  829. }
  830. buf[sizeof(buf) - 1] = '\0';
  831. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  832. HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
  833. "id=%d len=%d) from RADIUS server: %s",
  834. hdr->code, hdr->identifier, be_to_host16(hdr->length),
  835. buf);
  836. sm->eap_if->aaaEapReq = TRUE;
  837. wpabuf_free(sm->eap_if->aaaEapReqData);
  838. sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
  839. }
  840. static void ieee802_1x_get_keys(struct hostapd_data *hapd,
  841. struct sta_info *sta, struct radius_msg *msg,
  842. struct radius_msg *req,
  843. u8 *shared_secret, size_t shared_secret_len)
  844. {
  845. struct radius_ms_mppe_keys *keys;
  846. struct eapol_state_machine *sm = sta->eapol_sm;
  847. if (sm == NULL)
  848. return;
  849. keys = radius_msg_get_ms_keys(msg, req, shared_secret,
  850. shared_secret_len);
  851. if (keys && keys->send && keys->recv) {
  852. size_t len = keys->send_len + keys->recv_len;
  853. wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
  854. keys->send, keys->send_len);
  855. wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
  856. keys->recv, keys->recv_len);
  857. os_free(sm->eap_if->aaaEapKeyData);
  858. sm->eap_if->aaaEapKeyData = os_malloc(len);
  859. if (sm->eap_if->aaaEapKeyData) {
  860. os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
  861. keys->recv_len);
  862. os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
  863. keys->send, keys->send_len);
  864. sm->eap_if->aaaEapKeyDataLen = len;
  865. sm->eap_if->aaaEapKeyAvailable = TRUE;
  866. }
  867. }
  868. if (keys) {
  869. os_free(keys->send);
  870. os_free(keys->recv);
  871. os_free(keys);
  872. }
  873. }
  874. static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
  875. struct sta_info *sta,
  876. struct radius_msg *msg)
  877. {
  878. u8 *class;
  879. size_t class_len;
  880. struct eapol_state_machine *sm = sta->eapol_sm;
  881. int count, i;
  882. struct radius_attr_data *nclass;
  883. size_t nclass_count;
  884. if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
  885. sm == NULL)
  886. return;
  887. ieee802_1x_free_radius_class(&sm->radius_class);
  888. count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
  889. if (count <= 0)
  890. return;
  891. nclass = os_zalloc(count * sizeof(struct radius_attr_data));
  892. if (nclass == NULL)
  893. return;
  894. nclass_count = 0;
  895. class = NULL;
  896. for (i = 0; i < count; i++) {
  897. do {
  898. if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
  899. &class, &class_len,
  900. class) < 0) {
  901. i = count;
  902. break;
  903. }
  904. } while (class_len < 1);
  905. nclass[nclass_count].data = os_malloc(class_len);
  906. if (nclass[nclass_count].data == NULL)
  907. break;
  908. os_memcpy(nclass[nclass_count].data, class, class_len);
  909. nclass[nclass_count].len = class_len;
  910. nclass_count++;
  911. }
  912. sm->radius_class.attr = nclass;
  913. sm->radius_class.count = nclass_count;
  914. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
  915. "attributes for " MACSTR,
  916. (unsigned long) sm->radius_class.count,
  917. MAC2STR(sta->addr));
  918. }
  919. /* Update sta->identity based on User-Name attribute in Access-Accept */
  920. static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
  921. struct sta_info *sta,
  922. struct radius_msg *msg)
  923. {
  924. u8 *buf, *identity;
  925. size_t len;
  926. struct eapol_state_machine *sm = sta->eapol_sm;
  927. if (sm == NULL)
  928. return;
  929. if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
  930. NULL) < 0)
  931. return;
  932. identity = os_malloc(len + 1);
  933. if (identity == NULL)
  934. return;
  935. os_memcpy(identity, buf, len);
  936. identity[len] = '\0';
  937. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  938. HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
  939. "User-Name from Access-Accept '%s'",
  940. sm->identity ? (char *) sm->identity : "N/A",
  941. (char *) identity);
  942. os_free(sm->identity);
  943. sm->identity = identity;
  944. sm->identity_len = len;
  945. }
  946. struct sta_id_search {
  947. u8 identifier;
  948. struct eapol_state_machine *sm;
  949. };
  950. static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
  951. struct sta_info *sta,
  952. void *ctx)
  953. {
  954. struct sta_id_search *id_search = ctx;
  955. struct eapol_state_machine *sm = sta->eapol_sm;
  956. if (sm && sm->radius_identifier >= 0 &&
  957. sm->radius_identifier == id_search->identifier) {
  958. id_search->sm = sm;
  959. return 1;
  960. }
  961. return 0;
  962. }
  963. static struct eapol_state_machine *
  964. ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
  965. {
  966. struct sta_id_search id_search;
  967. id_search.identifier = identifier;
  968. id_search.sm = NULL;
  969. ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
  970. return id_search.sm;
  971. }
  972. /* Process the RADIUS frames from Authentication Server */
  973. static RadiusRxResult
  974. ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
  975. u8 *shared_secret, size_t shared_secret_len,
  976. void *data)
  977. {
  978. struct hostapd_data *hapd = data;
  979. struct sta_info *sta;
  980. u32 session_timeout = 0, termination_action, acct_interim_interval;
  981. int session_timeout_set, old_vlanid = 0;
  982. int eap_timeout;
  983. struct eapol_state_machine *sm;
  984. int override_eapReq = 0;
  985. sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
  986. if (sm == NULL) {
  987. wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
  988. "station for this RADIUS message");
  989. return RADIUS_RX_UNKNOWN;
  990. }
  991. sta = sm->sta;
  992. /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
  993. * present when packet contains an EAP-Message attribute */
  994. if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
  995. radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
  996. 0) < 0 &&
  997. radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
  998. wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
  999. "Message-Authenticator since it does not include "
  1000. "EAP-Message");
  1001. } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
  1002. req, 1)) {
  1003. printf("Incoming RADIUS packet did not have correct "
  1004. "Message-Authenticator - dropped\n");
  1005. return RADIUS_RX_INVALID_AUTHENTICATOR;
  1006. }
  1007. if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
  1008. msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
  1009. msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
  1010. printf("Unknown RADIUS message code\n");
  1011. return RADIUS_RX_UNKNOWN;
  1012. }
  1013. sm->radius_identifier = -1;
  1014. wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
  1015. MAC2STR(sta->addr));
  1016. if (sm->last_recv_radius) {
  1017. radius_msg_free(sm->last_recv_radius);
  1018. os_free(sm->last_recv_radius);
  1019. }
  1020. sm->last_recv_radius = msg;
  1021. session_timeout_set =
  1022. !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
  1023. &session_timeout);
  1024. if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
  1025. &termination_action))
  1026. termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
  1027. if (hapd->conf->radius->acct_interim_interval == 0 &&
  1028. msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
  1029. radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
  1030. &acct_interim_interval) == 0) {
  1031. if (acct_interim_interval < 60) {
  1032. hostapd_logger(hapd, sta->addr,
  1033. HOSTAPD_MODULE_IEEE8021X,
  1034. HOSTAPD_LEVEL_INFO,
  1035. "ignored too small "
  1036. "Acct-Interim-Interval %d",
  1037. acct_interim_interval);
  1038. } else
  1039. sta->acct_interim_interval = acct_interim_interval;
  1040. }
  1041. switch (msg->hdr->code) {
  1042. case RADIUS_CODE_ACCESS_ACCEPT:
  1043. if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  1044. sta->vlan_id = 0;
  1045. else {
  1046. old_vlanid = sta->vlan_id;
  1047. sta->vlan_id = radius_msg_get_vlanid(msg);
  1048. }
  1049. if (sta->vlan_id > 0 &&
  1050. hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  1051. sta->vlan_id)) {
  1052. hostapd_logger(hapd, sta->addr,
  1053. HOSTAPD_MODULE_RADIUS,
  1054. HOSTAPD_LEVEL_INFO,
  1055. "VLAN ID %d", sta->vlan_id);
  1056. } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
  1057. sta->eapol_sm->authFail = TRUE;
  1058. hostapd_logger(hapd, sta->addr,
  1059. HOSTAPD_MODULE_IEEE8021X,
  1060. HOSTAPD_LEVEL_INFO, "authentication "
  1061. "server did not include required VLAN "
  1062. "ID in Access-Accept");
  1063. break;
  1064. }
  1065. ap_sta_bind_vlan(hapd, sta, old_vlanid);
  1066. /* RFC 3580, Ch. 3.17 */
  1067. if (session_timeout_set && termination_action ==
  1068. RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
  1069. sm->reAuthPeriod = session_timeout;
  1070. } else if (session_timeout_set)
  1071. ap_sta_session_timeout(hapd, sta, session_timeout);
  1072. sm->eap_if->aaaSuccess = TRUE;
  1073. override_eapReq = 1;
  1074. ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
  1075. shared_secret_len);
  1076. ieee802_1x_store_radius_class(hapd, sta, msg);
  1077. ieee802_1x_update_sta_identity(hapd, sta, msg);
  1078. if (sm->eap_if->eapKeyAvailable &&
  1079. wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
  1080. session_timeout_set ?
  1081. (int) session_timeout : -1, sm) == 0) {
  1082. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
  1083. HOSTAPD_LEVEL_DEBUG,
  1084. "Added PMKSA cache entry");
  1085. }
  1086. break;
  1087. case RADIUS_CODE_ACCESS_REJECT:
  1088. sm->eap_if->aaaFail = TRUE;
  1089. override_eapReq = 1;
  1090. break;
  1091. case RADIUS_CODE_ACCESS_CHALLENGE:
  1092. sm->eap_if->aaaEapReq = TRUE;
  1093. if (session_timeout_set) {
  1094. /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
  1095. eap_timeout = session_timeout;
  1096. } else
  1097. eap_timeout = 30;
  1098. hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
  1099. HOSTAPD_LEVEL_DEBUG,
  1100. "using EAP timeout of %d seconds%s",
  1101. eap_timeout,
  1102. session_timeout_set ? " (from RADIUS)" : "");
  1103. eloop_cancel_timeout(ieee802_1x_eap_timeout, sta, NULL);
  1104. eloop_register_timeout(eap_timeout, 0, ieee802_1x_eap_timeout,
  1105. sta, NULL);
  1106. sm->eap_if->eapTimeout = FALSE;
  1107. break;
  1108. }
  1109. ieee802_1x_decapsulate_radius(hapd, sta);
  1110. if (override_eapReq)
  1111. sm->eap_if->aaaEapReq = FALSE;
  1112. eapol_auth_step(sm);
  1113. return RADIUS_RX_QUEUED;
  1114. }
  1115. void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
  1116. {
  1117. struct eapol_state_machine *sm = sta->eapol_sm;
  1118. if (sm == NULL)
  1119. return;
  1120. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  1121. HOSTAPD_LEVEL_DEBUG, "aborting authentication");
  1122. if (sm->last_recv_radius) {
  1123. radius_msg_free(sm->last_recv_radius);
  1124. os_free(sm->last_recv_radius);
  1125. sm->last_recv_radius = NULL;
  1126. }
  1127. }
  1128. #ifdef HOSTAPD_DUMP_STATE
  1129. static void fprint_char(FILE *f, char c)
  1130. {
  1131. if (c >= 32 && c < 127)
  1132. fprintf(f, "%c", c);
  1133. else
  1134. fprintf(f, "<%02x>", c);
  1135. }
  1136. void ieee802_1x_dump_state(FILE *f, const char *prefix, struct sta_info *sta)
  1137. {
  1138. struct eapol_state_machine *sm = sta->eapol_sm;
  1139. if (sm == NULL)
  1140. return;
  1141. fprintf(f, "%sIEEE 802.1X:\n", prefix);
  1142. if (sm->identity) {
  1143. size_t i;
  1144. fprintf(f, "%sidentity=", prefix);
  1145. for (i = 0; i < sm->identity_len; i++)
  1146. fprint_char(f, sm->identity[i]);
  1147. fprintf(f, "\n");
  1148. }
  1149. fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
  1150. "Supplicant: %d (%s)\n", prefix,
  1151. sm->eap_type_authsrv, eap_type_text(sm->eap_type_authsrv),
  1152. sm->eap_type_supp, eap_type_text(sm->eap_type_supp));
  1153. fprintf(f, "%scached_packets=%s\n", prefix,
  1154. sm->last_recv_radius ? "[RX RADIUS]" : "");
  1155. eapol_auth_dump_state(f, prefix, sm);
  1156. }
  1157. #endif /* HOSTAPD_DUMP_STATE */
  1158. static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
  1159. {
  1160. if (hapd->conf->default_wep_key_len < 1)
  1161. return 0;
  1162. os_free(hapd->default_wep_key);
  1163. hapd->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
  1164. if (hapd->default_wep_key == NULL ||
  1165. os_get_random(hapd->default_wep_key,
  1166. hapd->conf->default_wep_key_len)) {
  1167. printf("Could not generate random WEP key.\n");
  1168. os_free(hapd->default_wep_key);
  1169. hapd->default_wep_key = NULL;
  1170. return -1;
  1171. }
  1172. wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
  1173. hapd->default_wep_key,
  1174. hapd->conf->default_wep_key_len);
  1175. return 0;
  1176. }
  1177. static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
  1178. struct sta_info *sta, void *ctx)
  1179. {
  1180. if (sta->eapol_sm) {
  1181. sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
  1182. eapol_auth_step(sta->eapol_sm);
  1183. }
  1184. return 0;
  1185. }
  1186. static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
  1187. {
  1188. struct hostapd_data *hapd = eloop_ctx;
  1189. if (hapd->default_wep_key_idx >= 3)
  1190. hapd->default_wep_key_idx =
  1191. hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
  1192. else
  1193. hapd->default_wep_key_idx++;
  1194. wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
  1195. hapd->default_wep_key_idx);
  1196. if (ieee802_1x_rekey_broadcast(hapd)) {
  1197. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
  1198. HOSTAPD_LEVEL_WARNING, "failed to generate a "
  1199. "new broadcast key");
  1200. os_free(hapd->default_wep_key);
  1201. hapd->default_wep_key = NULL;
  1202. return;
  1203. }
  1204. /* TODO: Could setup key for RX here, but change default TX keyid only
  1205. * after new broadcast key has been sent to all stations. */
  1206. if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP", NULL,
  1207. hapd->default_wep_key_idx,
  1208. hapd->default_wep_key,
  1209. hapd->conf->default_wep_key_len, 1)) {
  1210. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
  1211. HOSTAPD_LEVEL_WARNING, "failed to configure a "
  1212. "new broadcast key");
  1213. os_free(hapd->default_wep_key);
  1214. hapd->default_wep_key = NULL;
  1215. return;
  1216. }
  1217. ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
  1218. if (hapd->conf->wep_rekeying_period > 0) {
  1219. eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
  1220. ieee802_1x_rekey, hapd, NULL);
  1221. }
  1222. }
  1223. static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
  1224. const u8 *data, size_t datalen)
  1225. {
  1226. ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
  1227. }
  1228. static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
  1229. const u8 *data, size_t datalen)
  1230. {
  1231. struct hostapd_data *hapd = ctx;
  1232. struct sta_info *sta = sta_ctx;
  1233. ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
  1234. }
  1235. static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
  1236. int preauth)
  1237. {
  1238. struct hostapd_data *hapd = ctx;
  1239. struct sta_info *sta = sta_ctx;
  1240. if (preauth)
  1241. rsn_preauth_finished(hapd, sta, success);
  1242. else
  1243. ieee802_1x_finished(hapd, sta, success);
  1244. }
  1245. static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
  1246. size_t identity_len, int phase2,
  1247. struct eap_user *user)
  1248. {
  1249. struct hostapd_data *hapd = ctx;
  1250. const struct hostapd_eap_user *eap_user;
  1251. int i, count;
  1252. eap_user = hostapd_get_eap_user(hapd->conf, identity,
  1253. identity_len, phase2);
  1254. if (eap_user == NULL)
  1255. return -1;
  1256. os_memset(user, 0, sizeof(*user));
  1257. user->phase2 = phase2;
  1258. count = EAP_USER_MAX_METHODS;
  1259. if (count > EAP_MAX_METHODS)
  1260. count = EAP_MAX_METHODS;
  1261. for (i = 0; i < count; i++) {
  1262. user->methods[i].vendor = eap_user->methods[i].vendor;
  1263. user->methods[i].method = eap_user->methods[i].method;
  1264. }
  1265. if (eap_user->password) {
  1266. user->password = os_malloc(eap_user->password_len);
  1267. if (user->password == NULL)
  1268. return -1;
  1269. os_memcpy(user->password, eap_user->password,
  1270. eap_user->password_len);
  1271. user->password_len = eap_user->password_len;
  1272. }
  1273. user->force_version = eap_user->force_version;
  1274. user->ttls_auth = eap_user->ttls_auth;
  1275. return 0;
  1276. }
  1277. static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
  1278. {
  1279. struct hostapd_data *hapd = ctx;
  1280. struct sta_info *sta;
  1281. sta = ap_get_sta(hapd, addr);
  1282. if (sta == NULL || sta->eapol_sm == NULL)
  1283. return 0;
  1284. return 1;
  1285. }
  1286. static void ieee802_1x_logger(void *ctx, const u8 *addr,
  1287. eapol_logger_level level, const char *txt)
  1288. {
  1289. struct hostapd_data *hapd = ctx;
  1290. int hlevel;
  1291. switch (level) {
  1292. case EAPOL_LOGGER_WARNING:
  1293. hlevel = HOSTAPD_LEVEL_WARNING;
  1294. break;
  1295. case EAPOL_LOGGER_INFO:
  1296. hlevel = HOSTAPD_LEVEL_INFO;
  1297. break;
  1298. case EAPOL_LOGGER_DEBUG:
  1299. default:
  1300. hlevel = HOSTAPD_LEVEL_DEBUG;
  1301. break;
  1302. }
  1303. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
  1304. txt);
  1305. }
  1306. static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
  1307. int authorized)
  1308. {
  1309. struct hostapd_data *hapd = ctx;
  1310. struct sta_info *sta = sta_ctx;
  1311. ieee802_1x_set_sta_authorized(hapd, sta, authorized);
  1312. }
  1313. static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
  1314. {
  1315. struct hostapd_data *hapd = ctx;
  1316. struct sta_info *sta = sta_ctx;
  1317. ieee802_1x_abort_auth(hapd, sta);
  1318. }
  1319. static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
  1320. {
  1321. struct hostapd_data *hapd = ctx;
  1322. struct sta_info *sta = sta_ctx;
  1323. ieee802_1x_tx_key(hapd, sta);
  1324. }
  1325. int ieee802_1x_init(struct hostapd_data *hapd)
  1326. {
  1327. int i;
  1328. struct eapol_auth_config conf;
  1329. struct eapol_auth_cb cb;
  1330. os_memset(&conf, 0, sizeof(conf));
  1331. conf.hapd = hapd;
  1332. conf.eap_reauth_period = hapd->conf->eap_reauth_period;
  1333. conf.wpa = hapd->conf->wpa;
  1334. conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
  1335. conf.eap_server = hapd->conf->eap_server;
  1336. conf.ssl_ctx = hapd->ssl_ctx;
  1337. conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
  1338. conf.eap_req_id_text = hapd->conf->eap_req_id_text;
  1339. conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
  1340. conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
  1341. conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
  1342. conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
  1343. conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
  1344. conf.eap_fast_prov = hapd->conf->eap_fast_prov;
  1345. conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
  1346. conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
  1347. conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
  1348. conf.tnc = hapd->conf->tnc;
  1349. os_memset(&cb, 0, sizeof(cb));
  1350. cb.eapol_send = ieee802_1x_eapol_send;
  1351. cb.aaa_send = ieee802_1x_aaa_send;
  1352. cb.finished = _ieee802_1x_finished;
  1353. cb.get_eap_user = ieee802_1x_get_eap_user;
  1354. cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
  1355. cb.logger = ieee802_1x_logger;
  1356. cb.set_port_authorized = ieee802_1x_set_port_authorized;
  1357. cb.abort_auth = _ieee802_1x_abort_auth;
  1358. cb.tx_key = _ieee802_1x_tx_key;
  1359. hapd->eapol_auth = eapol_auth_init(&conf, &cb);
  1360. if (hapd->eapol_auth == NULL)
  1361. return -1;
  1362. if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
  1363. hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1))
  1364. return -1;
  1365. if (radius_client_register(hapd->radius, RADIUS_AUTH,
  1366. ieee802_1x_receive_auth, hapd))
  1367. return -1;
  1368. if (hapd->conf->default_wep_key_len) {
  1369. hostapd_set_privacy(hapd, 1);
  1370. for (i = 0; i < 4; i++)
  1371. hostapd_set_encryption(hapd->conf->iface, hapd,
  1372. "none", NULL, i, NULL, 0, 0);
  1373. ieee802_1x_rekey(hapd, NULL);
  1374. if (hapd->default_wep_key == NULL)
  1375. return -1;
  1376. }
  1377. return 0;
  1378. }
  1379. void ieee802_1x_deinit(struct hostapd_data *hapd)
  1380. {
  1381. eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
  1382. if (hapd->driver != NULL &&
  1383. (hapd->conf->ieee802_1x || hapd->conf->wpa))
  1384. hostapd_set_ieee8021x(hapd->conf->iface, hapd, 0);
  1385. eapol_auth_deinit(hapd->eapol_auth);
  1386. hapd->eapol_auth = NULL;
  1387. }
  1388. int ieee802_1x_reconfig(struct hostapd_data *hapd,
  1389. struct hostapd_config *oldconf,
  1390. struct hostapd_bss_config *oldbss)
  1391. {
  1392. ieee802_1x_deinit(hapd);
  1393. return ieee802_1x_init(hapd);
  1394. }
  1395. int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
  1396. u8 *buf, size_t len, int ack)
  1397. {
  1398. struct ieee80211_hdr *hdr;
  1399. struct ieee802_1x_hdr *xhdr;
  1400. struct ieee802_1x_eapol_key *key;
  1401. u8 *pos;
  1402. const unsigned char rfc1042_hdr[ETH_ALEN] =
  1403. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  1404. if (sta == NULL)
  1405. return -1;
  1406. if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
  1407. return 0;
  1408. hdr = (struct ieee80211_hdr *) buf;
  1409. pos = (u8 *) (hdr + 1);
  1410. if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
  1411. return 0;
  1412. pos += sizeof(rfc1042_hdr);
  1413. if (WPA_GET_BE16(pos) != ETH_P_PAE)
  1414. return 0;
  1415. pos += 2;
  1416. xhdr = (struct ieee802_1x_hdr *) pos;
  1417. pos += sizeof(*xhdr);
  1418. wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
  1419. "type=%d length=%d - ack=%d",
  1420. MAC2STR(sta->addr), xhdr->version, xhdr->type,
  1421. be_to_host16(xhdr->length), ack);
  1422. /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
  1423. * or Authenticator state machines, but EAPOL-Key packets are not
  1424. * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
  1425. * packets couple of times because otherwise STA keys become
  1426. * unsynchronized with AP. */
  1427. if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
  1428. pos + sizeof(*key) <= buf + len) {
  1429. key = (struct ieee802_1x_eapol_key *) pos;
  1430. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
  1431. HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
  1432. "frame (%scast index=%d)",
  1433. key->key_index & BIT(7) ? "uni" : "broad",
  1434. key->key_index & ~BIT(7));
  1435. /* TODO: re-send EAPOL-Key couple of times (with short delay
  1436. * between them?). If all attempt fail, report error and
  1437. * deauthenticate STA so that it will get new keys when
  1438. * authenticating again (e.g., after returning in range).
  1439. * Separate limit/transmit state needed both for unicast and
  1440. * broadcast keys(?) */
  1441. }
  1442. /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
  1443. * to here and change the key only if the EAPOL-Key packet was Acked.
  1444. */
  1445. return 1;
  1446. }
  1447. u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
  1448. {
  1449. if (sm == NULL || sm->identity == NULL)
  1450. return NULL;
  1451. *len = sm->identity_len;
  1452. return sm->identity;
  1453. }
  1454. u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
  1455. int idx)
  1456. {
  1457. if (sm == NULL || sm->radius_class.attr == NULL ||
  1458. idx >= (int) sm->radius_class.count)
  1459. return NULL;
  1460. *len = sm->radius_class.attr[idx].len;
  1461. return sm->radius_class.attr[idx].data;
  1462. }
  1463. const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
  1464. {
  1465. if (sm == NULL)
  1466. return NULL;
  1467. *len = sm->eap_if->eapKeyDataLen;
  1468. return sm->eap_if->eapKeyData;
  1469. }
  1470. void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
  1471. int enabled)
  1472. {
  1473. if (sm == NULL)
  1474. return;
  1475. sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
  1476. eapol_auth_step(sm);
  1477. }
  1478. void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
  1479. int valid)
  1480. {
  1481. if (sm == NULL)
  1482. return;
  1483. sm->portValid = valid ? TRUE : FALSE;
  1484. eapol_auth_step(sm);
  1485. }
  1486. void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
  1487. {
  1488. if (sm == NULL)
  1489. return;
  1490. if (pre_auth)
  1491. sm->flags |= EAPOL_SM_PREAUTH;
  1492. else
  1493. sm->flags &= ~EAPOL_SM_PREAUTH;
  1494. }
  1495. static const char * bool_txt(Boolean bool)
  1496. {
  1497. return bool ? "TRUE" : "FALSE";
  1498. }
  1499. int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
  1500. {
  1501. /* TODO */
  1502. return 0;
  1503. }
  1504. int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1505. char *buf, size_t buflen)
  1506. {
  1507. int len = 0, ret;
  1508. struct eapol_state_machine *sm = sta->eapol_sm;
  1509. if (sm == NULL)
  1510. return 0;
  1511. ret = os_snprintf(buf + len, buflen - len,
  1512. "dot1xPaePortNumber=%d\n"
  1513. "dot1xPaePortProtocolVersion=%d\n"
  1514. "dot1xPaePortCapabilities=1\n"
  1515. "dot1xPaePortInitialize=%d\n"
  1516. "dot1xPaePortReauthenticate=FALSE\n",
  1517. sta->aid,
  1518. EAPOL_VERSION,
  1519. sm->initialize);
  1520. if (ret < 0 || (size_t) ret >= buflen - len)
  1521. return len;
  1522. len += ret;
  1523. /* dot1xAuthConfigTable */
  1524. ret = os_snprintf(buf + len, buflen - len,
  1525. "dot1xAuthPaeState=%d\n"
  1526. "dot1xAuthBackendAuthState=%d\n"
  1527. "dot1xAuthAdminControlledDirections=%d\n"
  1528. "dot1xAuthOperControlledDirections=%d\n"
  1529. "dot1xAuthAuthControlledPortStatus=%d\n"
  1530. "dot1xAuthAuthControlledPortControl=%d\n"
  1531. "dot1xAuthQuietPeriod=%u\n"
  1532. "dot1xAuthServerTimeout=%u\n"
  1533. "dot1xAuthReAuthPeriod=%u\n"
  1534. "dot1xAuthReAuthEnabled=%s\n"
  1535. "dot1xAuthKeyTxEnabled=%s\n",
  1536. sm->auth_pae_state + 1,
  1537. sm->be_auth_state + 1,
  1538. sm->adminControlledDirections,
  1539. sm->operControlledDirections,
  1540. sm->authPortStatus,
  1541. sm->portControl,
  1542. sm->quietPeriod,
  1543. sm->serverTimeout,
  1544. sm->reAuthPeriod,
  1545. bool_txt(sm->reAuthEnabled),
  1546. bool_txt(sm->keyTxEnabled));
  1547. if (ret < 0 || (size_t) ret >= buflen - len)
  1548. return len;
  1549. len += ret;
  1550. /* dot1xAuthStatsTable */
  1551. ret = os_snprintf(buf + len, buflen - len,
  1552. "dot1xAuthEapolFramesRx=%u\n"
  1553. "dot1xAuthEapolFramesTx=%u\n"
  1554. "dot1xAuthEapolStartFramesRx=%u\n"
  1555. "dot1xAuthEapolLogoffFramesRx=%u\n"
  1556. "dot1xAuthEapolRespIdFramesRx=%u\n"
  1557. "dot1xAuthEapolRespFramesRx=%u\n"
  1558. "dot1xAuthEapolReqIdFramesTx=%u\n"
  1559. "dot1xAuthEapolReqFramesTx=%u\n"
  1560. "dot1xAuthInvalidEapolFramesRx=%u\n"
  1561. "dot1xAuthEapLengthErrorFramesRx=%u\n"
  1562. "dot1xAuthLastEapolFrameVersion=%u\n"
  1563. "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
  1564. sm->dot1xAuthEapolFramesRx,
  1565. sm->dot1xAuthEapolFramesTx,
  1566. sm->dot1xAuthEapolStartFramesRx,
  1567. sm->dot1xAuthEapolLogoffFramesRx,
  1568. sm->dot1xAuthEapolRespIdFramesRx,
  1569. sm->dot1xAuthEapolRespFramesRx,
  1570. sm->dot1xAuthEapolReqIdFramesTx,
  1571. sm->dot1xAuthEapolReqFramesTx,
  1572. sm->dot1xAuthInvalidEapolFramesRx,
  1573. sm->dot1xAuthEapLengthErrorFramesRx,
  1574. sm->dot1xAuthLastEapolFrameVersion,
  1575. MAC2STR(sm->addr));
  1576. if (ret < 0 || (size_t) ret >= buflen - len)
  1577. return len;
  1578. len += ret;
  1579. /* dot1xAuthDiagTable */
  1580. ret = os_snprintf(buf + len, buflen - len,
  1581. "dot1xAuthEntersConnecting=%u\n"
  1582. "dot1xAuthEapLogoffsWhileConnecting=%u\n"
  1583. "dot1xAuthEntersAuthenticating=%u\n"
  1584. "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
  1585. "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
  1586. "dot1xAuthAuthFailWhileAuthenticating=%u\n"
  1587. "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
  1588. "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
  1589. "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
  1590. "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
  1591. "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
  1592. "dot1xAuthBackendResponses=%u\n"
  1593. "dot1xAuthBackendAccessChallenges=%u\n"
  1594. "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
  1595. "dot1xAuthBackendAuthSuccesses=%u\n"
  1596. "dot1xAuthBackendAuthFails=%u\n",
  1597. sm->authEntersConnecting,
  1598. sm->authEapLogoffsWhileConnecting,
  1599. sm->authEntersAuthenticating,
  1600. sm->authAuthSuccessesWhileAuthenticating,
  1601. sm->authAuthTimeoutsWhileAuthenticating,
  1602. sm->authAuthFailWhileAuthenticating,
  1603. sm->authAuthEapStartsWhileAuthenticating,
  1604. sm->authAuthEapLogoffWhileAuthenticating,
  1605. sm->authAuthReauthsWhileAuthenticated,
  1606. sm->authAuthEapStartsWhileAuthenticated,
  1607. sm->authAuthEapLogoffWhileAuthenticated,
  1608. sm->backendResponses,
  1609. sm->backendAccessChallenges,
  1610. sm->backendOtherRequestsToSupplicant,
  1611. sm->backendAuthSuccesses,
  1612. sm->backendAuthFails);
  1613. if (ret < 0 || (size_t) ret >= buflen - len)
  1614. return len;
  1615. len += ret;
  1616. /* dot1xAuthSessionStatsTable */
  1617. ret = os_snprintf(buf + len, buflen - len,
  1618. /* TODO: dot1xAuthSessionOctetsRx */
  1619. /* TODO: dot1xAuthSessionOctetsTx */
  1620. /* TODO: dot1xAuthSessionFramesRx */
  1621. /* TODO: dot1xAuthSessionFramesTx */
  1622. "dot1xAuthSessionId=%08X-%08X\n"
  1623. "dot1xAuthSessionAuthenticMethod=%d\n"
  1624. "dot1xAuthSessionTime=%u\n"
  1625. "dot1xAuthSessionTerminateCause=999\n"
  1626. "dot1xAuthSessionUserName=%s\n",
  1627. sta->acct_session_id_hi, sta->acct_session_id_lo,
  1628. (wpa_key_mgmt_wpa_ieee8021x(
  1629. wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
  1630. 1 : 2,
  1631. (unsigned int) (time(NULL) -
  1632. sta->acct_session_start),
  1633. sm->identity);
  1634. if (ret < 0 || (size_t) ret >= buflen - len)
  1635. return len;
  1636. len += ret;
  1637. return len;
  1638. }
  1639. static void ieee802_1x_finished(struct hostapd_data *hapd,
  1640. struct sta_info *sta, int success)
  1641. {
  1642. const u8 *key;
  1643. size_t len;
  1644. /* TODO: get PMKLifetime from WPA parameters */
  1645. static const int dot11RSNAConfigPMKLifetime = 43200;
  1646. key = ieee802_1x_get_key(sta->eapol_sm, &len);
  1647. if (success && key && len >= PMK_LEN &&
  1648. wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
  1649. sta->eapol_sm) == 0) {
  1650. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
  1651. HOSTAPD_LEVEL_DEBUG,
  1652. "Added PMKSA cache entry (IEEE 802.1X)");
  1653. }
  1654. }