ieee802_1x.c 56 KB

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