wpa_auth_glue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * hostapd / WPA authenticator glue code
  3. * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "common/ieee802_11_defs.h"
  11. #include "eapol_auth/eapol_auth_sm.h"
  12. #include "eapol_auth/eapol_auth_sm_i.h"
  13. #include "eap_server/eap.h"
  14. #include "l2_packet/l2_packet.h"
  15. #include "drivers/driver.h"
  16. #include "hostapd.h"
  17. #include "ieee802_1x.h"
  18. #include "preauth_auth.h"
  19. #include "sta_info.h"
  20. #include "tkip_countermeasures.h"
  21. #include "ap_drv_ops.h"
  22. #include "ap_config.h"
  23. #include "wpa_auth.h"
  24. #include "wpa_auth_glue.h"
  25. static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
  26. struct wpa_auth_config *wconf)
  27. {
  28. os_memset(wconf, 0, sizeof(*wconf));
  29. wconf->wpa = conf->wpa;
  30. wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
  31. wconf->wpa_pairwise = conf->wpa_pairwise;
  32. wconf->wpa_group = conf->wpa_group;
  33. wconf->wpa_group_rekey = conf->wpa_group_rekey;
  34. wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
  35. wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
  36. wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
  37. wconf->rsn_pairwise = conf->rsn_pairwise;
  38. wconf->rsn_preauth = conf->rsn_preauth;
  39. wconf->eapol_version = conf->eapol_version;
  40. wconf->peerkey = conf->peerkey;
  41. wconf->wmm_enabled = conf->wmm_enabled;
  42. wconf->wmm_uapsd = conf->wmm_uapsd;
  43. wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
  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. wconf->ft_over_ds = conf->ft_over_ds;
  68. #endif /* CONFIG_IEEE80211R */
  69. }
  70. static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
  71. logger_level level, const char *txt)
  72. {
  73. #ifndef CONFIG_NO_HOSTAPD_LOGGER
  74. struct hostapd_data *hapd = ctx;
  75. int hlevel;
  76. switch (level) {
  77. case LOGGER_WARNING:
  78. hlevel = HOSTAPD_LEVEL_WARNING;
  79. break;
  80. case LOGGER_INFO:
  81. hlevel = HOSTAPD_LEVEL_INFO;
  82. break;
  83. case LOGGER_DEBUG:
  84. default:
  85. hlevel = HOSTAPD_LEVEL_DEBUG;
  86. break;
  87. }
  88. hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
  89. #endif /* CONFIG_NO_HOSTAPD_LOGGER */
  90. }
  91. static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
  92. u16 reason)
  93. {
  94. struct hostapd_data *hapd = ctx;
  95. wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
  96. "STA " MACSTR " reason %d",
  97. __func__, MAC2STR(addr), reason);
  98. ap_sta_disconnect(hapd, NULL, addr, reason);
  99. }
  100. static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
  101. {
  102. struct hostapd_data *hapd = ctx;
  103. michael_mic_failure(hapd, addr, 0);
  104. }
  105. static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
  106. wpa_eapol_variable var, int value)
  107. {
  108. struct hostapd_data *hapd = ctx;
  109. struct sta_info *sta = ap_get_sta(hapd, addr);
  110. if (sta == NULL)
  111. return;
  112. switch (var) {
  113. case WPA_EAPOL_portEnabled:
  114. ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
  115. break;
  116. case WPA_EAPOL_portValid:
  117. ieee802_1x_notify_port_valid(sta->eapol_sm, value);
  118. break;
  119. case WPA_EAPOL_authorized:
  120. ieee802_1x_set_sta_authorized(hapd, sta, value);
  121. break;
  122. case WPA_EAPOL_portControl_Auto:
  123. if (sta->eapol_sm)
  124. sta->eapol_sm->portControl = Auto;
  125. break;
  126. case WPA_EAPOL_keyRun:
  127. if (sta->eapol_sm)
  128. sta->eapol_sm->keyRun = value ? TRUE : FALSE;
  129. break;
  130. case WPA_EAPOL_keyAvailable:
  131. if (sta->eapol_sm)
  132. sta->eapol_sm->eap_if->eapKeyAvailable =
  133. value ? TRUE : FALSE;
  134. break;
  135. case WPA_EAPOL_keyDone:
  136. if (sta->eapol_sm)
  137. sta->eapol_sm->keyDone = value ? TRUE : FALSE;
  138. break;
  139. case WPA_EAPOL_inc_EapolFramesTx:
  140. if (sta->eapol_sm)
  141. sta->eapol_sm->dot1xAuthEapolFramesTx++;
  142. break;
  143. }
  144. }
  145. static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
  146. wpa_eapol_variable var)
  147. {
  148. struct hostapd_data *hapd = ctx;
  149. struct sta_info *sta = ap_get_sta(hapd, addr);
  150. if (sta == NULL || sta->eapol_sm == NULL)
  151. return -1;
  152. switch (var) {
  153. case WPA_EAPOL_keyRun:
  154. return sta->eapol_sm->keyRun;
  155. case WPA_EAPOL_keyAvailable:
  156. return sta->eapol_sm->eap_if->eapKeyAvailable;
  157. default:
  158. return -1;
  159. }
  160. }
  161. static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
  162. const u8 *prev_psk)
  163. {
  164. struct hostapd_data *hapd = ctx;
  165. struct sta_info *sta = ap_get_sta(hapd, addr);
  166. if (sta && sta->psk)
  167. return sta->psk;
  168. return hostapd_get_psk(hapd->conf, addr, prev_psk);
  169. }
  170. static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
  171. size_t *len)
  172. {
  173. struct hostapd_data *hapd = ctx;
  174. const u8 *key;
  175. size_t keylen;
  176. struct sta_info *sta;
  177. sta = ap_get_sta(hapd, addr);
  178. if (sta == NULL)
  179. return -1;
  180. key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
  181. if (key == NULL)
  182. return -1;
  183. if (keylen > *len)
  184. keylen = *len;
  185. os_memcpy(msk, key, keylen);
  186. *len = keylen;
  187. return 0;
  188. }
  189. static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
  190. const u8 *addr, int idx, u8 *key,
  191. size_t key_len)
  192. {
  193. struct hostapd_data *hapd = ctx;
  194. const char *ifname = hapd->conf->iface;
  195. if (vlan_id > 0) {
  196. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
  197. if (ifname == NULL)
  198. return -1;
  199. }
  200. return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
  201. key, key_len);
  202. }
  203. static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
  204. u8 *seq)
  205. {
  206. struct hostapd_data *hapd = ctx;
  207. return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
  208. }
  209. static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
  210. const u8 *data, size_t data_len,
  211. int encrypt)
  212. {
  213. struct hostapd_data *hapd = ctx;
  214. struct sta_info *sta;
  215. u32 flags = 0;
  216. sta = ap_get_sta(hapd, addr);
  217. if (sta)
  218. flags = hostapd_sta_flags_to_drv(sta->flags);
  219. return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
  220. encrypt, flags);
  221. }
  222. static int hostapd_wpa_auth_for_each_sta(
  223. void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
  224. void *cb_ctx)
  225. {
  226. struct hostapd_data *hapd = ctx;
  227. struct sta_info *sta;
  228. for (sta = hapd->sta_list; sta; sta = sta->next) {
  229. if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. struct wpa_auth_iface_iter_data {
  235. int (*cb)(struct wpa_authenticator *sm, void *ctx);
  236. void *cb_ctx;
  237. };
  238. static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
  239. {
  240. struct wpa_auth_iface_iter_data *data = ctx;
  241. size_t i;
  242. for (i = 0; i < iface->num_bss; i++) {
  243. if (iface->bss[i]->wpa_auth &&
  244. data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
  245. return 1;
  246. }
  247. return 0;
  248. }
  249. static int hostapd_wpa_auth_for_each_auth(
  250. void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
  251. void *cb_ctx)
  252. {
  253. struct hostapd_data *hapd = ctx;
  254. struct wpa_auth_iface_iter_data data;
  255. if (hapd->iface->for_each_interface == NULL)
  256. return -1;
  257. data.cb = cb;
  258. data.cb_ctx = cb_ctx;
  259. return hapd->iface->for_each_interface(hapd->iface->interfaces,
  260. wpa_auth_iface_iter, &data);
  261. }
  262. #ifdef CONFIG_IEEE80211R
  263. struct wpa_auth_ft_iface_iter_data {
  264. struct hostapd_data *src_hapd;
  265. const u8 *dst;
  266. const u8 *data;
  267. size_t data_len;
  268. };
  269. static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
  270. {
  271. struct wpa_auth_ft_iface_iter_data *idata = ctx;
  272. struct hostapd_data *hapd;
  273. size_t j;
  274. for (j = 0; j < iface->num_bss; j++) {
  275. hapd = iface->bss[j];
  276. if (hapd == idata->src_hapd)
  277. continue;
  278. if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
  279. wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
  280. "locally managed BSS " MACSTR "@%s -> "
  281. MACSTR "@%s",
  282. MAC2STR(idata->src_hapd->own_addr),
  283. idata->src_hapd->conf->iface,
  284. MAC2STR(hapd->own_addr), hapd->conf->iface);
  285. wpa_ft_rrb_rx(hapd->wpa_auth,
  286. idata->src_hapd->own_addr,
  287. idata->data, idata->data_len);
  288. return 1;
  289. }
  290. }
  291. return 0;
  292. }
  293. #endif /* CONFIG_IEEE80211R */
  294. static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
  295. const u8 *data, size_t data_len)
  296. {
  297. struct hostapd_data *hapd = ctx;
  298. struct l2_ethhdr *buf;
  299. int ret;
  300. #ifdef CONFIG_IEEE80211R
  301. if (proto == ETH_P_RRB && hapd->iface->for_each_interface) {
  302. int res;
  303. struct wpa_auth_ft_iface_iter_data idata;
  304. idata.src_hapd = hapd;
  305. idata.dst = dst;
  306. idata.data = data;
  307. idata.data_len = data_len;
  308. res = hapd->iface->for_each_interface(hapd->iface->interfaces,
  309. hostapd_wpa_auth_ft_iter,
  310. &idata);
  311. if (res == 1)
  312. return data_len;
  313. }
  314. #endif /* CONFIG_IEEE80211R */
  315. if (hapd->driver && hapd->driver->send_ether)
  316. return hapd->driver->send_ether(hapd->drv_priv, dst,
  317. hapd->own_addr, proto,
  318. data, data_len);
  319. if (hapd->l2 == NULL)
  320. return -1;
  321. buf = os_malloc(sizeof(*buf) + data_len);
  322. if (buf == NULL)
  323. return -1;
  324. os_memcpy(buf->h_dest, dst, ETH_ALEN);
  325. os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
  326. buf->h_proto = host_to_be16(proto);
  327. os_memcpy(buf + 1, data, data_len);
  328. ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
  329. sizeof(*buf) + data_len);
  330. os_free(buf);
  331. return ret;
  332. }
  333. #ifdef CONFIG_IEEE80211R
  334. static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
  335. const u8 *data, size_t data_len)
  336. {
  337. struct hostapd_data *hapd = ctx;
  338. int res;
  339. struct ieee80211_mgmt *m;
  340. size_t mlen;
  341. struct sta_info *sta;
  342. sta = ap_get_sta(hapd, dst);
  343. if (sta == NULL || sta->wpa_sm == NULL)
  344. return -1;
  345. m = os_zalloc(sizeof(*m) + data_len);
  346. if (m == NULL)
  347. return -1;
  348. mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
  349. m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  350. WLAN_FC_STYPE_ACTION);
  351. os_memcpy(m->da, dst, ETH_ALEN);
  352. os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
  353. os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
  354. os_memcpy(&m->u, data, data_len);
  355. res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
  356. os_free(m);
  357. return res;
  358. }
  359. static struct wpa_state_machine *
  360. hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
  361. {
  362. struct hostapd_data *hapd = ctx;
  363. struct sta_info *sta;
  364. sta = ap_sta_add(hapd, sta_addr);
  365. if (sta == NULL)
  366. return NULL;
  367. if (sta->wpa_sm) {
  368. sta->auth_alg = WLAN_AUTH_FT;
  369. return sta->wpa_sm;
  370. }
  371. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
  372. if (sta->wpa_sm == NULL) {
  373. ap_free_sta(hapd, sta);
  374. return NULL;
  375. }
  376. sta->auth_alg = WLAN_AUTH_FT;
  377. return sta->wpa_sm;
  378. }
  379. static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
  380. size_t len)
  381. {
  382. struct hostapd_data *hapd = ctx;
  383. struct l2_ethhdr *ethhdr;
  384. if (len < sizeof(*ethhdr))
  385. return;
  386. ethhdr = (struct l2_ethhdr *) buf;
  387. wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
  388. MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
  389. wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
  390. len - sizeof(*ethhdr));
  391. }
  392. #endif /* CONFIG_IEEE80211R */
  393. int hostapd_setup_wpa(struct hostapd_data *hapd)
  394. {
  395. struct wpa_auth_config _conf;
  396. struct wpa_auth_callbacks cb;
  397. const u8 *wpa_ie;
  398. size_t wpa_ie_len;
  399. hostapd_wpa_auth_conf(hapd->conf, &_conf);
  400. if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
  401. _conf.tx_status = 1;
  402. os_memset(&cb, 0, sizeof(cb));
  403. cb.ctx = hapd;
  404. cb.logger = hostapd_wpa_auth_logger;
  405. cb.disconnect = hostapd_wpa_auth_disconnect;
  406. cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
  407. cb.set_eapol = hostapd_wpa_auth_set_eapol;
  408. cb.get_eapol = hostapd_wpa_auth_get_eapol;
  409. cb.get_psk = hostapd_wpa_auth_get_psk;
  410. cb.get_msk = hostapd_wpa_auth_get_msk;
  411. cb.set_key = hostapd_wpa_auth_set_key;
  412. cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
  413. cb.send_eapol = hostapd_wpa_auth_send_eapol;
  414. cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
  415. cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
  416. cb.send_ether = hostapd_wpa_auth_send_ether;
  417. #ifdef CONFIG_IEEE80211R
  418. cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
  419. cb.add_sta = hostapd_wpa_auth_add_sta;
  420. #endif /* CONFIG_IEEE80211R */
  421. hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
  422. if (hapd->wpa_auth == NULL) {
  423. wpa_printf(MSG_ERROR, "WPA initialization failed.");
  424. return -1;
  425. }
  426. if (hostapd_set_privacy(hapd, 1)) {
  427. wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
  428. "for interface %s", hapd->conf->iface);
  429. return -1;
  430. }
  431. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  432. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
  433. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  434. "the kernel driver.");
  435. return -1;
  436. }
  437. if (rsn_preauth_iface_init(hapd)) {
  438. wpa_printf(MSG_ERROR, "Initialization of RSN "
  439. "pre-authentication failed.");
  440. return -1;
  441. }
  442. #ifdef CONFIG_IEEE80211R
  443. if (!hostapd_drv_none(hapd)) {
  444. hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
  445. hapd->conf->bridge :
  446. hapd->conf->iface, NULL, ETH_P_RRB,
  447. hostapd_rrb_receive, hapd, 1);
  448. if (hapd->l2 == NULL &&
  449. (hapd->driver == NULL ||
  450. hapd->driver->send_ether == NULL)) {
  451. wpa_printf(MSG_ERROR, "Failed to open l2_packet "
  452. "interface");
  453. return -1;
  454. }
  455. }
  456. #endif /* CONFIG_IEEE80211R */
  457. return 0;
  458. }
  459. void hostapd_reconfig_wpa(struct hostapd_data *hapd)
  460. {
  461. struct wpa_auth_config wpa_auth_conf;
  462. hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf);
  463. wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
  464. }
  465. void hostapd_deinit_wpa(struct hostapd_data *hapd)
  466. {
  467. ieee80211_tkip_countermeasures_deinit(hapd);
  468. rsn_preauth_iface_deinit(hapd);
  469. if (hapd->wpa_auth) {
  470. wpa_deinit(hapd->wpa_auth);
  471. hapd->wpa_auth = NULL;
  472. if (hostapd_set_privacy(hapd, 0)) {
  473. wpa_printf(MSG_DEBUG, "Could not disable "
  474. "PrivacyInvoked for interface %s",
  475. hapd->conf->iface);
  476. }
  477. if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
  478. wpa_printf(MSG_DEBUG, "Could not remove generic "
  479. "information element from interface %s",
  480. hapd->conf->iface);
  481. }
  482. }
  483. ieee802_1x_deinit(hapd);
  484. #ifdef CONFIG_IEEE80211R
  485. l2_packet_deinit(hapd->l2);
  486. #endif /* CONFIG_IEEE80211R */
  487. }