ieee802_1x.c 54 KB

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