wpa_auth_glue.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * hostapd / WPA authenticator glue code
  3. * Copyright (c) 2002-2009, 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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "eapol_auth/eapol_auth_sm.h"
  18. #include "eapol_auth/eapol_auth_sm_i.h"
  19. #include "eap_server/eap.h"
  20. #include "l2_packet/l2_packet.h"
  21. #include "drivers/driver.h"
  22. #include "hostapd.h"
  23. #include "ieee802_1x.h"
  24. #include "preauth_auth.h"
  25. #include "sta_info.h"
  26. #include "tkip_countermeasures.h"
  27. #include "ap_drv_ops.h"
  28. #include "ap_config.h"
  29. #include "wpa_auth.h"
  30. static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
  31. struct wpa_auth_config *wconf)
  32. {
  33. wconf->wpa = conf->wpa;
  34. wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
  35. wconf->wpa_pairwise = conf->wpa_pairwise;
  36. wconf->wpa_group = conf->wpa_group;
  37. wconf->wpa_group_rekey = conf->wpa_group_rekey;
  38. wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
  39. wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
  40. wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
  41. wconf->rsn_pairwise = conf->rsn_pairwise;
  42. wconf->rsn_preauth = conf->rsn_preauth;
  43. wconf->eapol_version = conf->eapol_version;
  44. wconf->peerkey = conf->peerkey;
  45. wconf->wmm_enabled = conf->wmm_enabled;
  46. wconf->okc = conf->okc;
  47. #ifdef CONFIG_IEEE80211W
  48. wconf->ieee80211w = conf->ieee80211w;
  49. #endif /* CONFIG_IEEE80211W */
  50. #ifdef CONFIG_IEEE80211R
  51. wconf->ssid_len = conf->ssid.ssid_len;
  52. if (wconf->ssid_len > SSID_LEN)
  53. wconf->ssid_len = SSID_LEN;
  54. os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
  55. os_memcpy(wconf->mobility_domain, conf->mobility_domain,
  56. MOBILITY_DOMAIN_ID_LEN);
  57. if (conf->nas_identifier &&
  58. os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
  59. wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
  60. os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
  61. wconf->r0_key_holder_len);
  62. }
  63. os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
  64. wconf->r0_key_lifetime = conf->r0_key_lifetime;
  65. wconf->reassociation_deadline = conf->reassociation_deadline;
  66. wconf->r0kh_list = conf->r0kh_list;
  67. wconf->r1kh_list = conf->r1kh_list;
  68. wconf->pmk_r1_push = conf->pmk_r1_push;
  69. #endif /* CONFIG_IEEE80211R */
  70. }
  71. static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
  72. logger_level level, const char *txt)
  73. {
  74. #ifndef CONFIG_NO_HOSTAPD_LOGGER
  75. struct hostapd_data *hapd = ctx;
  76. int hlevel;
  77. switch (level) {
  78. case LOGGER_WARNING:
  79. hlevel = HOSTAPD_LEVEL_WARNING;
  80. break;
  81. case LOGGER_INFO:
  82. hlevel = HOSTAPD_LEVEL_INFO;
  83. break;
  84. case LOGGER_DEBUG:
  85. default:
  86. hlevel = HOSTAPD_LEVEL_DEBUG;
  87. break;
  88. }
  89. hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
  90. #endif /* CONFIG_NO_HOSTAPD_LOGGER */
  91. }
  92. static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
  93. u16 reason)
  94. {
  95. struct hostapd_data *hapd = ctx;
  96. wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
  97. "STA " MACSTR " reason %d",
  98. __func__, MAC2STR(addr), reason);
  99. ap_sta_disconnect(hapd, NULL, addr, reason);
  100. }
  101. static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
  102. {
  103. struct hostapd_data *hapd = ctx;
  104. michael_mic_failure(hapd, addr, 0);
  105. }
  106. static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
  107. wpa_eapol_variable var, int value)
  108. {
  109. struct hostapd_data *hapd = ctx;
  110. struct sta_info *sta = ap_get_sta(hapd, addr);
  111. if (sta == NULL)
  112. return;
  113. switch (var) {
  114. case WPA_EAPOL_portEnabled:
  115. ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
  116. break;
  117. case WPA_EAPOL_portValid:
  118. ieee802_1x_notify_port_valid(sta->eapol_sm, value);
  119. break;
  120. case WPA_EAPOL_authorized:
  121. ieee802_1x_set_sta_authorized(hapd, sta, value);
  122. break;
  123. case WPA_EAPOL_portControl_Auto:
  124. if (sta->eapol_sm)
  125. sta->eapol_sm->portControl = Auto;
  126. break;
  127. case WPA_EAPOL_keyRun:
  128. if (sta->eapol_sm)
  129. sta->eapol_sm->keyRun = value ? TRUE : FALSE;
  130. break;
  131. case WPA_EAPOL_keyAvailable:
  132. if (sta->eapol_sm)
  133. sta->eapol_sm->eap_if->eapKeyAvailable =
  134. value ? TRUE : FALSE;
  135. break;
  136. case WPA_EAPOL_keyDone:
  137. if (sta->eapol_sm)
  138. sta->eapol_sm->keyDone = value ? TRUE : FALSE;
  139. break;
  140. case WPA_EAPOL_inc_EapolFramesTx:
  141. if (sta->eapol_sm)
  142. sta->eapol_sm->dot1xAuthEapolFramesTx++;
  143. break;
  144. }
  145. }
  146. static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
  147. wpa_eapol_variable var)
  148. {
  149. struct hostapd_data *hapd = ctx;
  150. struct sta_info *sta = ap_get_sta(hapd, addr);
  151. if (sta == NULL || sta->eapol_sm == NULL)
  152. return -1;
  153. switch (var) {
  154. case WPA_EAPOL_keyRun:
  155. return sta->eapol_sm->keyRun;
  156. case WPA_EAPOL_keyAvailable:
  157. return sta->eapol_sm->eap_if->eapKeyAvailable;
  158. default:
  159. return -1;
  160. }
  161. }
  162. static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
  163. const u8 *prev_psk)
  164. {
  165. struct hostapd_data *hapd = ctx;
  166. return hostapd_get_psk(hapd->conf, addr, prev_psk);
  167. }
  168. static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
  169. size_t *len)
  170. {
  171. struct hostapd_data *hapd = ctx;
  172. const u8 *key;
  173. size_t keylen;
  174. struct sta_info *sta;
  175. sta = ap_get_sta(hapd, addr);
  176. if (sta == NULL)
  177. return -1;
  178. key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
  179. if (key == NULL)
  180. return -1;
  181. if (keylen > *len)
  182. keylen = *len;
  183. os_memcpy(msk, key, keylen);
  184. *len = keylen;
  185. return 0;
  186. }
  187. static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
  188. const u8 *addr, int idx, u8 *key,
  189. size_t key_len)
  190. {
  191. struct hostapd_data *hapd = ctx;
  192. const char *ifname = hapd->conf->iface;
  193. if (vlan_id > 0) {
  194. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
  195. if (ifname == NULL)
  196. return -1;
  197. }
  198. return hapd->drv.set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
  199. key, key_len);
  200. }
  201. static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
  202. u8 *seq)
  203. {
  204. struct hostapd_data *hapd = ctx;
  205. return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
  206. }
  207. static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
  208. const u8 *data, size_t data_len,
  209. int encrypt)
  210. {
  211. struct hostapd_data *hapd = ctx;
  212. return hapd->drv.send_eapol(hapd, addr, data, data_len, encrypt);
  213. }
  214. static int hostapd_wpa_auth_for_each_sta(
  215. void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
  216. void *cb_ctx)
  217. {
  218. struct hostapd_data *hapd = ctx;
  219. struct sta_info *sta;
  220. for (sta = hapd->sta_list; sta; sta = sta->next) {
  221. if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
  222. return 1;
  223. }
  224. return 0;
  225. }
  226. struct wpa_auth_iface_iter_data {
  227. int (*cb)(struct wpa_authenticator *sm, void *ctx);
  228. void *cb_ctx;
  229. };
  230. static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
  231. {
  232. struct wpa_auth_iface_iter_data *data = ctx;
  233. size_t i;
  234. for (i = 0; i < iface->num_bss; i++) {
  235. if (data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. static int hostapd_wpa_auth_for_each_auth(
  241. void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
  242. void *cb_ctx)
  243. {
  244. struct hostapd_data *hapd = ctx;
  245. struct wpa_auth_iface_iter_data data;
  246. if (hapd->iface->for_each_interface == NULL)
  247. return -1;
  248. data.cb = cb;
  249. data.cb_ctx = cb_ctx;
  250. return hapd->iface->for_each_interface(hapd->iface->interfaces,
  251. wpa_auth_iface_iter, &data);
  252. }
  253. static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
  254. const u8 *data, size_t data_len)
  255. {
  256. struct hostapd_data *hapd = ctx;
  257. if (hapd->driver && hapd->driver->send_ether)
  258. return hapd->driver->send_ether(hapd->drv_priv, dst,
  259. hapd->own_addr, proto,
  260. data, data_len);
  261. if (hapd->l2 == NULL)
  262. return -1;
  263. return l2_packet_send(hapd->l2, dst, proto, data, data_len);
  264. }
  265. #ifdef CONFIG_IEEE80211R
  266. static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
  267. const u8 *data, size_t data_len)
  268. {
  269. struct hostapd_data *hapd = ctx;
  270. int res;
  271. struct ieee80211_mgmt *m;
  272. size_t mlen;
  273. struct sta_info *sta;
  274. sta = ap_get_sta(hapd, dst);
  275. if (sta == NULL || sta->wpa_sm == NULL)
  276. return -1;
  277. m = os_zalloc(sizeof(*m) + data_len);
  278. if (m == NULL)
  279. return -1;
  280. mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
  281. m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  282. WLAN_FC_STYPE_ACTION);
  283. os_memcpy(m->da, dst, ETH_ALEN);
  284. os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
  285. os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
  286. os_memcpy(&m->u, data, data_len);
  287. res = hapd->drv.send_mgmt_frame(hapd, (u8 *) m, mlen);
  288. os_free(m);
  289. return res;
  290. }
  291. static struct wpa_state_machine *
  292. hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
  293. {
  294. struct hostapd_data *hapd = ctx;
  295. struct sta_info *sta;
  296. sta = ap_sta_add(hapd, sta_addr);
  297. if (sta == NULL)
  298. return NULL;
  299. if (sta->wpa_sm)
  300. return sta->wpa_sm;
  301. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
  302. if (sta->wpa_sm == NULL) {
  303. ap_free_sta(hapd, sta);
  304. return NULL;
  305. }
  306. sta->auth_alg = WLAN_AUTH_FT;
  307. return sta->wpa_sm;
  308. }
  309. static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
  310. size_t len)
  311. {
  312. struct hostapd_data *hapd = ctx;
  313. wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
  314. }
  315. #endif /* CONFIG_IEEE80211R */
  316. int hostapd_setup_wpa(struct hostapd_data *hapd)
  317. {
  318. struct wpa_auth_config _conf;
  319. struct wpa_auth_callbacks cb;
  320. const u8 *wpa_ie;
  321. size_t wpa_ie_len;
  322. hostapd_wpa_auth_conf(hapd->conf, &_conf);
  323. os_memset(&cb, 0, sizeof(cb));
  324. cb.ctx = hapd;
  325. cb.logger = hostapd_wpa_auth_logger;
  326. cb.disconnect = hostapd_wpa_auth_disconnect;
  327. cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
  328. cb.set_eapol = hostapd_wpa_auth_set_eapol;
  329. cb.get_eapol = hostapd_wpa_auth_get_eapol;
  330. cb.get_psk = hostapd_wpa_auth_get_psk;
  331. cb.get_msk = hostapd_wpa_auth_get_msk;
  332. cb.set_key = hostapd_wpa_auth_set_key;
  333. cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
  334. cb.send_eapol = hostapd_wpa_auth_send_eapol;
  335. cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
  336. cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
  337. cb.send_ether = hostapd_wpa_auth_send_ether;
  338. #ifdef CONFIG_IEEE80211R
  339. cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
  340. cb.add_sta = hostapd_wpa_auth_add_sta;
  341. #endif /* CONFIG_IEEE80211R */
  342. hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
  343. if (hapd->wpa_auth == NULL) {
  344. wpa_printf(MSG_ERROR, "WPA initialization failed.");
  345. return -1;
  346. }
  347. if (hostapd_set_privacy(hapd, 1)) {
  348. wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
  349. "for interface %s", hapd->conf->iface);
  350. return -1;
  351. }
  352. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  353. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
  354. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  355. "the kernel driver.");
  356. return -1;
  357. }
  358. if (rsn_preauth_iface_init(hapd)) {
  359. wpa_printf(MSG_ERROR, "Initialization of RSN "
  360. "pre-authentication failed.");
  361. return -1;
  362. }
  363. #ifdef CONFIG_IEEE80211R
  364. if (!hostapd_drv_none(hapd)) {
  365. hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
  366. hostapd_rrb_receive, hapd, 0);
  367. if (hapd->l2 == NULL &&
  368. (hapd->driver == NULL ||
  369. hapd->driver->send_ether == NULL)) {
  370. wpa_printf(MSG_ERROR, "Failed to open l2_packet "
  371. "interface");
  372. return -1;
  373. }
  374. }
  375. #endif /* CONFIG_IEEE80211R */
  376. return 0;
  377. }
  378. void hostapd_reconfig_wpa(struct hostapd_data *hapd)
  379. {
  380. struct wpa_auth_config wpa_auth_conf;
  381. hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf);
  382. wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
  383. }
  384. void hostapd_deinit_wpa(struct hostapd_data *hapd)
  385. {
  386. rsn_preauth_iface_deinit(hapd);
  387. if (hapd->wpa_auth) {
  388. wpa_deinit(hapd->wpa_auth);
  389. hapd->wpa_auth = NULL;
  390. if (hostapd_set_privacy(hapd, 0)) {
  391. wpa_printf(MSG_DEBUG, "Could not disable "
  392. "PrivacyInvoked for interface %s",
  393. hapd->conf->iface);
  394. }
  395. if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
  396. wpa_printf(MSG_DEBUG, "Could not remove generic "
  397. "information element from interface %s",
  398. hapd->conf->iface);
  399. }
  400. }
  401. ieee802_1x_deinit(hapd);
  402. #ifdef CONFIG_IEEE80211R
  403. l2_packet_deinit(hapd->l2);
  404. #endif /* CONFIG_IEEE80211R */
  405. }