ieee802_1x.c 55 KB

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