wpa_auth_glue.c 12 KB

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