drv_callbacks.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /*
  2. * hostapd / Callback functions for driver wrappers
  3. * Copyright (c) 2002-2013, 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 "radius/radius.h"
  11. #include "drivers/driver.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/ieee802_11_common.h"
  14. #include "common/wpa_ctrl.h"
  15. #include "crypto/random.h"
  16. #include "p2p/p2p.h"
  17. #include "wps/wps.h"
  18. #include "wnm_ap.h"
  19. #include "hostapd.h"
  20. #include "ieee802_11.h"
  21. #include "sta_info.h"
  22. #include "accounting.h"
  23. #include "tkip_countermeasures.h"
  24. #include "ieee802_1x.h"
  25. #include "wpa_auth.h"
  26. #include "wps_hostapd.h"
  27. #include "ap_drv_ops.h"
  28. #include "ap_config.h"
  29. #include "hw_features.h"
  30. #include "dfs.h"
  31. int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
  32. const u8 *req_ies, size_t req_ies_len, int reassoc)
  33. {
  34. struct sta_info *sta;
  35. int new_assoc, res;
  36. struct ieee802_11_elems elems;
  37. const u8 *ie;
  38. size_t ielen;
  39. #ifdef CONFIG_IEEE80211R
  40. u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
  41. u8 *p = buf;
  42. #endif /* CONFIG_IEEE80211R */
  43. u16 reason = WLAN_REASON_UNSPECIFIED;
  44. u16 status = WLAN_STATUS_SUCCESS;
  45. const u8 *p2p_dev_addr = NULL;
  46. if (addr == NULL) {
  47. /*
  48. * This could potentially happen with unexpected event from the
  49. * driver wrapper. This was seen at least in one case where the
  50. * driver ended up being set to station mode while hostapd was
  51. * running, so better make sure we stop processing such an
  52. * event here.
  53. */
  54. wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
  55. "no address");
  56. return -1;
  57. }
  58. random_add_randomness(addr, ETH_ALEN);
  59. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  60. HOSTAPD_LEVEL_INFO, "associated");
  61. ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
  62. if (elems.wps_ie) {
  63. ie = elems.wps_ie - 2;
  64. ielen = elems.wps_ie_len + 2;
  65. wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
  66. } else if (elems.rsn_ie) {
  67. ie = elems.rsn_ie - 2;
  68. ielen = elems.rsn_ie_len + 2;
  69. wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
  70. } else if (elems.wpa_ie) {
  71. ie = elems.wpa_ie - 2;
  72. ielen = elems.wpa_ie_len + 2;
  73. wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
  74. } else {
  75. ie = NULL;
  76. ielen = 0;
  77. wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
  78. "(Re)AssocReq");
  79. }
  80. sta = ap_get_sta(hapd, addr);
  81. if (sta) {
  82. ap_sta_no_session_timeout(hapd, sta);
  83. accounting_sta_stop(hapd, sta);
  84. /*
  85. * Make sure that the previously registered inactivity timer
  86. * will not remove the STA immediately.
  87. */
  88. sta->timeout_next = STA_NULLFUNC;
  89. } else {
  90. sta = ap_sta_add(hapd, addr);
  91. if (sta == NULL) {
  92. hostapd_drv_sta_disassoc(hapd, addr,
  93. WLAN_REASON_DISASSOC_AP_BUSY);
  94. return -1;
  95. }
  96. }
  97. sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
  98. #ifdef CONFIG_P2P
  99. if (elems.p2p) {
  100. wpabuf_free(sta->p2p_ie);
  101. sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
  102. P2P_IE_VENDOR_TYPE);
  103. if (sta->p2p_ie)
  104. p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
  105. }
  106. #endif /* CONFIG_P2P */
  107. #ifdef CONFIG_INTERWORKING
  108. if (elems.ext_capab && elems.ext_capab_len > 4) {
  109. if (elems.ext_capab[4] & 0x01)
  110. sta->qos_map_enabled = 1;
  111. }
  112. #endif /* CONFIG_INTERWORKING */
  113. #ifdef CONFIG_HS20
  114. wpabuf_free(sta->hs20_ie);
  115. if (elems.hs20 && elems.hs20_len > 4) {
  116. sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
  117. elems.hs20_len - 4);
  118. } else
  119. sta->hs20_ie = NULL;
  120. #endif /* CONFIG_HS20 */
  121. if (hapd->conf->wpa) {
  122. if (ie == NULL || ielen == 0) {
  123. #ifdef CONFIG_WPS
  124. if (hapd->conf->wps_state) {
  125. wpa_printf(MSG_DEBUG, "STA did not include "
  126. "WPA/RSN IE in (Re)Association "
  127. "Request - possible WPS use");
  128. sta->flags |= WLAN_STA_MAYBE_WPS;
  129. goto skip_wpa_check;
  130. }
  131. #endif /* CONFIG_WPS */
  132. wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
  133. return -1;
  134. }
  135. #ifdef CONFIG_WPS
  136. if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
  137. os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
  138. struct wpabuf *wps;
  139. sta->flags |= WLAN_STA_WPS;
  140. wps = ieee802_11_vendor_ie_concat(ie, ielen,
  141. WPS_IE_VENDOR_TYPE);
  142. if (wps) {
  143. if (wps_is_20(wps)) {
  144. wpa_printf(MSG_DEBUG, "WPS: STA "
  145. "supports WPS 2.0");
  146. sta->flags |= WLAN_STA_WPS2;
  147. }
  148. wpabuf_free(wps);
  149. }
  150. goto skip_wpa_check;
  151. }
  152. #endif /* CONFIG_WPS */
  153. if (sta->wpa_sm == NULL)
  154. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  155. sta->addr,
  156. p2p_dev_addr);
  157. if (sta->wpa_sm == NULL) {
  158. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  159. "machine");
  160. return -1;
  161. }
  162. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  163. ie, ielen,
  164. elems.mdie, elems.mdie_len);
  165. if (res != WPA_IE_OK) {
  166. wpa_printf(MSG_DEBUG, "WPA/RSN information element "
  167. "rejected? (res %u)", res);
  168. wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
  169. if (res == WPA_INVALID_GROUP) {
  170. reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
  171. status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  172. } else if (res == WPA_INVALID_PAIRWISE) {
  173. reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
  174. status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
  175. } else if (res == WPA_INVALID_AKMP) {
  176. reason = WLAN_REASON_AKMP_NOT_VALID;
  177. status = WLAN_STATUS_AKMP_NOT_VALID;
  178. }
  179. #ifdef CONFIG_IEEE80211W
  180. else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
  181. reason = WLAN_REASON_INVALID_IE;
  182. status = WLAN_STATUS_INVALID_IE;
  183. } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
  184. reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
  185. status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
  186. }
  187. #endif /* CONFIG_IEEE80211W */
  188. else {
  189. reason = WLAN_REASON_INVALID_IE;
  190. status = WLAN_STATUS_INVALID_IE;
  191. }
  192. goto fail;
  193. }
  194. #ifdef CONFIG_IEEE80211W
  195. if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
  196. sta->sa_query_count > 0)
  197. ap_check_sa_query_timeout(hapd, sta);
  198. if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
  199. (sta->auth_alg != WLAN_AUTH_FT)) {
  200. /*
  201. * STA has already been associated with MFP and SA
  202. * Query timeout has not been reached. Reject the
  203. * association attempt temporarily and start SA Query,
  204. * if one is not pending.
  205. */
  206. if (sta->sa_query_count == 0)
  207. ap_sta_start_sa_query(hapd, sta);
  208. #ifdef CONFIG_IEEE80211R
  209. status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
  210. p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
  211. hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
  212. p - buf);
  213. #endif /* CONFIG_IEEE80211R */
  214. return 0;
  215. }
  216. if (wpa_auth_uses_mfp(sta->wpa_sm))
  217. sta->flags |= WLAN_STA_MFP;
  218. else
  219. sta->flags &= ~WLAN_STA_MFP;
  220. #endif /* CONFIG_IEEE80211W */
  221. #ifdef CONFIG_IEEE80211R
  222. if (sta->auth_alg == WLAN_AUTH_FT) {
  223. status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
  224. req_ies_len);
  225. if (status != WLAN_STATUS_SUCCESS) {
  226. if (status == WLAN_STATUS_INVALID_PMKID)
  227. reason = WLAN_REASON_INVALID_IE;
  228. if (status == WLAN_STATUS_INVALID_MDIE)
  229. reason = WLAN_REASON_INVALID_IE;
  230. if (status == WLAN_STATUS_INVALID_FTIE)
  231. reason = WLAN_REASON_INVALID_IE;
  232. goto fail;
  233. }
  234. }
  235. #endif /* CONFIG_IEEE80211R */
  236. } else if (hapd->conf->wps_state) {
  237. #ifdef CONFIG_WPS
  238. struct wpabuf *wps;
  239. if (req_ies)
  240. wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
  241. WPS_IE_VENDOR_TYPE);
  242. else
  243. wps = NULL;
  244. #ifdef CONFIG_WPS_STRICT
  245. if (wps && wps_validate_assoc_req(wps) < 0) {
  246. reason = WLAN_REASON_INVALID_IE;
  247. status = WLAN_STATUS_INVALID_IE;
  248. wpabuf_free(wps);
  249. goto fail;
  250. }
  251. #endif /* CONFIG_WPS_STRICT */
  252. if (wps) {
  253. sta->flags |= WLAN_STA_WPS;
  254. if (wps_is_20(wps)) {
  255. wpa_printf(MSG_DEBUG, "WPS: STA supports "
  256. "WPS 2.0");
  257. sta->flags |= WLAN_STA_WPS2;
  258. }
  259. } else
  260. sta->flags |= WLAN_STA_MAYBE_WPS;
  261. wpabuf_free(wps);
  262. #endif /* CONFIG_WPS */
  263. }
  264. #ifdef CONFIG_WPS
  265. skip_wpa_check:
  266. #endif /* CONFIG_WPS */
  267. #ifdef CONFIG_IEEE80211R
  268. p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
  269. sta->auth_alg, req_ies, req_ies_len);
  270. hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
  271. #else /* CONFIG_IEEE80211R */
  272. /* Keep compiler silent about unused variables */
  273. if (status) {
  274. }
  275. #endif /* CONFIG_IEEE80211R */
  276. new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
  277. sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
  278. sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
  279. if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
  280. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
  281. else
  282. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  283. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  284. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  285. #ifdef CONFIG_P2P
  286. if (req_ies) {
  287. p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
  288. req_ies, req_ies_len);
  289. }
  290. #endif /* CONFIG_P2P */
  291. return 0;
  292. fail:
  293. #ifdef CONFIG_IEEE80211R
  294. hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
  295. #endif /* CONFIG_IEEE80211R */
  296. hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
  297. ap_free_sta(hapd, sta);
  298. return -1;
  299. }
  300. void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
  301. {
  302. struct sta_info *sta;
  303. if (addr == NULL) {
  304. /*
  305. * This could potentially happen with unexpected event from the
  306. * driver wrapper. This was seen at least in one case where the
  307. * driver ended up reporting a station mode event while hostapd
  308. * was running, so better make sure we stop processing such an
  309. * event here.
  310. */
  311. wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
  312. "with no address");
  313. return;
  314. }
  315. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  316. HOSTAPD_LEVEL_INFO, "disassociated");
  317. sta = ap_get_sta(hapd, addr);
  318. if (sta == NULL) {
  319. wpa_printf(MSG_DEBUG, "Disassociation notification for "
  320. "unknown STA " MACSTR, MAC2STR(addr));
  321. return;
  322. }
  323. ap_sta_set_authorized(hapd, sta, 0);
  324. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  325. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  326. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  327. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  328. ap_free_sta(hapd, sta);
  329. }
  330. void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
  331. {
  332. struct sta_info *sta = ap_get_sta(hapd, addr);
  333. if (!sta || !hapd->conf->disassoc_low_ack)
  334. return;
  335. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  336. HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
  337. "missing ACKs");
  338. hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
  339. if (sta)
  340. ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
  341. }
  342. void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
  343. int offset, int width, int cf1, int cf2)
  344. {
  345. #ifdef NEED_AP_MLME
  346. int channel, chwidth, seg0_idx = 0, seg1_idx = 0;
  347. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  348. HOSTAPD_LEVEL_INFO, "driver had channel switch: "
  349. "freq=%d, ht=%d, offset=%d, width=%d, cf1=%d, cf2=%d",
  350. freq, ht, offset, width, cf1, cf2);
  351. hapd->iface->freq = freq;
  352. channel = hostapd_hw_get_channel(hapd, freq);
  353. if (!channel) {
  354. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  355. HOSTAPD_LEVEL_WARNING, "driver switched to "
  356. "bad channel!");
  357. return;
  358. }
  359. switch (width) {
  360. case CHAN_WIDTH_80:
  361. chwidth = VHT_CHANWIDTH_80MHZ;
  362. break;
  363. case CHAN_WIDTH_80P80:
  364. chwidth = VHT_CHANWIDTH_80P80MHZ;
  365. break;
  366. case CHAN_WIDTH_160:
  367. chwidth = VHT_CHANWIDTH_160MHZ;
  368. break;
  369. case CHAN_WIDTH_20_NOHT:
  370. case CHAN_WIDTH_20:
  371. case CHAN_WIDTH_40:
  372. default:
  373. chwidth = VHT_CHANWIDTH_USE_HT;
  374. break;
  375. }
  376. switch (hapd->iface->current_mode->mode) {
  377. case HOSTAPD_MODE_IEEE80211A:
  378. if (cf1 > 5000)
  379. seg0_idx = (cf1 - 5000) / 5;
  380. if (cf2 > 5000)
  381. seg1_idx = (cf2 - 5000) / 5;
  382. break;
  383. default:
  384. seg0_idx = hostapd_hw_get_channel(hapd, cf1);
  385. seg1_idx = hostapd_hw_get_channel(hapd, cf2);
  386. break;
  387. }
  388. hapd->iconf->channel = channel;
  389. hapd->iconf->ieee80211n = ht;
  390. hapd->iconf->secondary_channel = offset;
  391. hapd->iconf->vht_oper_chwidth = chwidth;
  392. hapd->iconf->vht_oper_centr_freq_seg0_idx = seg0_idx;
  393. hapd->iconf->vht_oper_centr_freq_seg1_idx = seg1_idx;
  394. if (hapd->iface->csa_in_progress &&
  395. freq == hapd->iface->cs_freq_params.freq) {
  396. hostapd_cleanup_cs_params(hapd);
  397. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED "freq=%d",
  398. freq);
  399. }
  400. #endif /* NEED_AP_MLME */
  401. }
  402. void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
  403. const u8 *addr, int reason_code)
  404. {
  405. switch (reason_code) {
  406. case MAX_CLIENT_REACHED:
  407. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
  408. MAC2STR(addr));
  409. break;
  410. case BLOCKED_CLIENT:
  411. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
  412. MAC2STR(addr));
  413. break;
  414. }
  415. }
  416. int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
  417. const u8 *bssid, const u8 *ie, size_t ie_len,
  418. int ssi_signal)
  419. {
  420. size_t i;
  421. int ret = 0;
  422. if (sa == NULL || ie == NULL)
  423. return -1;
  424. random_add_randomness(sa, ETH_ALEN);
  425. for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
  426. if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
  427. sa, da, bssid, ie, ie_len,
  428. ssi_signal) > 0) {
  429. ret = 1;
  430. break;
  431. }
  432. }
  433. return ret;
  434. }
  435. #ifdef HOSTAPD
  436. #ifdef CONFIG_IEEE80211R
  437. static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
  438. const u8 *bssid,
  439. u16 auth_transaction, u16 status,
  440. const u8 *ies, size_t ies_len)
  441. {
  442. struct hostapd_data *hapd = ctx;
  443. struct sta_info *sta;
  444. sta = ap_get_sta(hapd, dst);
  445. if (sta == NULL)
  446. return;
  447. hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
  448. HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
  449. sta->flags |= WLAN_STA_AUTH;
  450. hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
  451. }
  452. #endif /* CONFIG_IEEE80211R */
  453. static void hostapd_notif_auth(struct hostapd_data *hapd,
  454. struct auth_info *rx_auth)
  455. {
  456. struct sta_info *sta;
  457. u16 status = WLAN_STATUS_SUCCESS;
  458. u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
  459. size_t resp_ies_len = 0;
  460. sta = ap_get_sta(hapd, rx_auth->peer);
  461. if (!sta) {
  462. sta = ap_sta_add(hapd, rx_auth->peer);
  463. if (sta == NULL) {
  464. status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  465. goto fail;
  466. }
  467. }
  468. sta->flags &= ~WLAN_STA_PREAUTH;
  469. ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
  470. #ifdef CONFIG_IEEE80211R
  471. if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
  472. sta->auth_alg = WLAN_AUTH_FT;
  473. if (sta->wpa_sm == NULL)
  474. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  475. sta->addr, NULL);
  476. if (sta->wpa_sm == NULL) {
  477. wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
  478. "state machine");
  479. status = WLAN_STATUS_UNSPECIFIED_FAILURE;
  480. goto fail;
  481. }
  482. wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
  483. rx_auth->auth_transaction, rx_auth->ies,
  484. rx_auth->ies_len,
  485. hostapd_notify_auth_ft_finish, hapd);
  486. return;
  487. }
  488. #endif /* CONFIG_IEEE80211R */
  489. fail:
  490. hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
  491. status, resp_ies, resp_ies_len);
  492. }
  493. static void hostapd_action_rx(struct hostapd_data *hapd,
  494. struct rx_mgmt *drv_mgmt)
  495. {
  496. struct ieee80211_mgmt *mgmt;
  497. struct sta_info *sta;
  498. size_t plen __maybe_unused;
  499. u16 fc;
  500. if (drv_mgmt->frame_len < 24 + 1)
  501. return;
  502. plen = drv_mgmt->frame_len - 24 - 1;
  503. mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
  504. fc = le_to_host16(mgmt->frame_control);
  505. if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
  506. return; /* handled by the driver */
  507. wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
  508. mgmt->u.action.category, (int) plen);
  509. sta = ap_get_sta(hapd, mgmt->sa);
  510. if (sta == NULL) {
  511. wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
  512. return;
  513. }
  514. #ifdef CONFIG_IEEE80211R
  515. if (mgmt->u.action.category == WLAN_ACTION_FT) {
  516. const u8 *payload = drv_mgmt->frame + 24 + 1;
  517. wpa_ft_action_rx(sta->wpa_sm, payload, plen);
  518. }
  519. #endif /* CONFIG_IEEE80211R */
  520. #ifdef CONFIG_IEEE80211W
  521. if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY && plen >= 4) {
  522. ieee802_11_sa_query_action(
  523. hapd, mgmt->sa,
  524. mgmt->u.action.u.sa_query_resp.action,
  525. mgmt->u.action.u.sa_query_resp.trans_id);
  526. }
  527. #endif /* CONFIG_IEEE80211W */
  528. #ifdef CONFIG_WNM
  529. if (mgmt->u.action.category == WLAN_ACTION_WNM) {
  530. ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
  531. }
  532. #endif /* CONFIG_WNM */
  533. }
  534. #ifdef NEED_AP_MLME
  535. #define HAPD_BROADCAST ((struct hostapd_data *) -1)
  536. static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
  537. const u8 *bssid)
  538. {
  539. size_t i;
  540. if (bssid == NULL)
  541. return NULL;
  542. if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
  543. bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
  544. return HAPD_BROADCAST;
  545. for (i = 0; i < iface->num_bss; i++) {
  546. if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
  547. return iface->bss[i];
  548. }
  549. return NULL;
  550. }
  551. static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
  552. const u8 *bssid, const u8 *addr,
  553. int wds)
  554. {
  555. hapd = get_hapd_bssid(hapd->iface, bssid);
  556. if (hapd == NULL || hapd == HAPD_BROADCAST)
  557. return;
  558. ieee802_11_rx_from_unknown(hapd, addr, wds);
  559. }
  560. static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
  561. {
  562. struct hostapd_iface *iface = hapd->iface;
  563. const struct ieee80211_hdr *hdr;
  564. const u8 *bssid;
  565. struct hostapd_frame_info fi;
  566. int ret;
  567. hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
  568. bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
  569. if (bssid == NULL)
  570. return 0;
  571. hapd = get_hapd_bssid(iface, bssid);
  572. if (hapd == NULL) {
  573. u16 fc;
  574. fc = le_to_host16(hdr->frame_control);
  575. /*
  576. * Drop frames to unknown BSSIDs except for Beacon frames which
  577. * could be used to update neighbor information.
  578. */
  579. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  580. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  581. hapd = iface->bss[0];
  582. else
  583. return 0;
  584. }
  585. os_memset(&fi, 0, sizeof(fi));
  586. fi.datarate = rx_mgmt->datarate;
  587. fi.ssi_signal = rx_mgmt->ssi_signal;
  588. if (hapd == HAPD_BROADCAST) {
  589. size_t i;
  590. ret = 0;
  591. for (i = 0; i < iface->num_bss; i++) {
  592. if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
  593. rx_mgmt->frame_len, &fi) > 0)
  594. ret = 1;
  595. }
  596. } else
  597. ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
  598. &fi);
  599. random_add_randomness(&fi, sizeof(fi));
  600. return ret;
  601. }
  602. static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
  603. size_t len, u16 stype, int ok)
  604. {
  605. struct ieee80211_hdr *hdr;
  606. hdr = (struct ieee80211_hdr *) buf;
  607. hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
  608. if (hapd == NULL || hapd == HAPD_BROADCAST)
  609. return;
  610. ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
  611. }
  612. #endif /* NEED_AP_MLME */
  613. static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
  614. {
  615. struct sta_info *sta = ap_get_sta(hapd, addr);
  616. if (sta)
  617. return 0;
  618. wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
  619. " - adding a new STA", MAC2STR(addr));
  620. sta = ap_sta_add(hapd, addr);
  621. if (sta) {
  622. hostapd_new_assoc_sta(hapd, sta, 0);
  623. } else {
  624. wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
  625. MAC2STR(addr));
  626. return -1;
  627. }
  628. return 0;
  629. }
  630. static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
  631. const u8 *data, size_t data_len)
  632. {
  633. struct hostapd_iface *iface = hapd->iface;
  634. struct sta_info *sta;
  635. size_t j;
  636. for (j = 0; j < iface->num_bss; j++) {
  637. if ((sta = ap_get_sta(iface->bss[j], src))) {
  638. if (sta->flags & WLAN_STA_ASSOC) {
  639. hapd = iface->bss[j];
  640. break;
  641. }
  642. }
  643. }
  644. ieee802_1x_receive(hapd, src, data, data_len);
  645. }
  646. static struct hostapd_channel_data * hostapd_get_mode_channel(
  647. struct hostapd_iface *iface, unsigned int freq)
  648. {
  649. int i;
  650. struct hostapd_channel_data *chan;
  651. for (i = 0; i < iface->current_mode->num_channels; i++) {
  652. chan = &iface->current_mode->channels[i];
  653. if (!chan)
  654. return NULL;
  655. if ((unsigned int) chan->freq == freq)
  656. return chan;
  657. }
  658. return NULL;
  659. }
  660. static void hostapd_update_nf(struct hostapd_iface *iface,
  661. struct hostapd_channel_data *chan,
  662. struct freq_survey *survey)
  663. {
  664. if (!iface->chans_surveyed) {
  665. chan->min_nf = survey->nf;
  666. iface->lowest_nf = survey->nf;
  667. } else {
  668. if (dl_list_empty(&chan->survey_list))
  669. chan->min_nf = survey->nf;
  670. else if (survey->nf < chan->min_nf)
  671. chan->min_nf = survey->nf;
  672. if (survey->nf < iface->lowest_nf)
  673. iface->lowest_nf = survey->nf;
  674. }
  675. }
  676. static void hostapd_event_get_survey(struct hostapd_data *hapd,
  677. struct survey_results *survey_results)
  678. {
  679. struct hostapd_iface *iface = hapd->iface;
  680. struct freq_survey *survey, *tmp;
  681. struct hostapd_channel_data *chan;
  682. if (dl_list_empty(&survey_results->survey_list)) {
  683. wpa_printf(MSG_DEBUG, "No survey data received");
  684. return;
  685. }
  686. dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
  687. struct freq_survey, list) {
  688. chan = hostapd_get_mode_channel(iface, survey->freq);
  689. if (!chan)
  690. continue;
  691. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  692. continue;
  693. dl_list_del(&survey->list);
  694. dl_list_add_tail(&chan->survey_list, &survey->list);
  695. hostapd_update_nf(iface, chan, survey);
  696. iface->chans_surveyed++;
  697. }
  698. }
  699. #ifdef NEED_AP_MLME
  700. static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
  701. struct dfs_event *radar)
  702. {
  703. wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
  704. hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
  705. radar->chan_offset, radar->chan_width,
  706. radar->cf1, radar->cf2);
  707. }
  708. static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
  709. struct dfs_event *radar)
  710. {
  711. wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
  712. hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
  713. radar->chan_offset, radar->chan_width,
  714. radar->cf1, radar->cf2);
  715. }
  716. static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
  717. struct dfs_event *radar)
  718. {
  719. wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
  720. hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
  721. radar->chan_offset, radar->chan_width,
  722. radar->cf1, radar->cf2);
  723. }
  724. static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
  725. struct dfs_event *radar)
  726. {
  727. wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
  728. hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
  729. radar->chan_offset, radar->chan_width,
  730. radar->cf1, radar->cf2);
  731. }
  732. #endif /* NEED_AP_MLME */
  733. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  734. union wpa_event_data *data)
  735. {
  736. struct hostapd_data *hapd = ctx;
  737. #ifndef CONFIG_NO_STDOUT_DEBUG
  738. int level = MSG_DEBUG;
  739. if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
  740. data->rx_mgmt.frame_len >= 24) {
  741. const struct ieee80211_hdr *hdr;
  742. u16 fc;
  743. hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
  744. fc = le_to_host16(hdr->frame_control);
  745. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  746. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  747. level = MSG_EXCESSIVE;
  748. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  749. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
  750. level = MSG_EXCESSIVE;
  751. }
  752. wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
  753. event_to_string(event), event);
  754. #endif /* CONFIG_NO_STDOUT_DEBUG */
  755. switch (event) {
  756. case EVENT_MICHAEL_MIC_FAILURE:
  757. michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
  758. break;
  759. case EVENT_SCAN_RESULTS:
  760. if (hapd->iface->scan_cb)
  761. hapd->iface->scan_cb(hapd->iface);
  762. break;
  763. #ifdef CONFIG_IEEE80211R
  764. case EVENT_FT_RRB_RX:
  765. wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
  766. data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
  767. break;
  768. #endif /* CONFIG_IEEE80211R */
  769. case EVENT_WPS_BUTTON_PUSHED:
  770. hostapd_wps_button_pushed(hapd, NULL);
  771. break;
  772. #ifdef NEED_AP_MLME
  773. case EVENT_TX_STATUS:
  774. switch (data->tx_status.type) {
  775. case WLAN_FC_TYPE_MGMT:
  776. hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
  777. data->tx_status.data_len,
  778. data->tx_status.stype,
  779. data->tx_status.ack);
  780. break;
  781. case WLAN_FC_TYPE_DATA:
  782. hostapd_tx_status(hapd, data->tx_status.dst,
  783. data->tx_status.data,
  784. data->tx_status.data_len,
  785. data->tx_status.ack);
  786. break;
  787. }
  788. break;
  789. case EVENT_EAPOL_TX_STATUS:
  790. hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
  791. data->eapol_tx_status.data,
  792. data->eapol_tx_status.data_len,
  793. data->eapol_tx_status.ack);
  794. break;
  795. case EVENT_DRIVER_CLIENT_POLL_OK:
  796. hostapd_client_poll_ok(hapd, data->client_poll.addr);
  797. break;
  798. case EVENT_RX_FROM_UNKNOWN:
  799. hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
  800. data->rx_from_unknown.addr,
  801. data->rx_from_unknown.wds);
  802. break;
  803. #endif /* NEED_AP_MLME */
  804. case EVENT_RX_MGMT:
  805. #ifdef NEED_AP_MLME
  806. if (hostapd_mgmt_rx(hapd, &data->rx_mgmt) > 0)
  807. break;
  808. #endif /* NEED_AP_MLME */
  809. hostapd_action_rx(hapd, &data->rx_mgmt);
  810. break;
  811. case EVENT_RX_PROBE_REQ:
  812. if (data->rx_probe_req.sa == NULL ||
  813. data->rx_probe_req.ie == NULL)
  814. break;
  815. hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
  816. data->rx_probe_req.da,
  817. data->rx_probe_req.bssid,
  818. data->rx_probe_req.ie,
  819. data->rx_probe_req.ie_len,
  820. data->rx_probe_req.ssi_signal);
  821. break;
  822. case EVENT_NEW_STA:
  823. hostapd_event_new_sta(hapd, data->new_sta.addr);
  824. break;
  825. case EVENT_EAPOL_RX:
  826. hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
  827. data->eapol_rx.data,
  828. data->eapol_rx.data_len);
  829. break;
  830. case EVENT_ASSOC:
  831. hostapd_notif_assoc(hapd, data->assoc_info.addr,
  832. data->assoc_info.req_ies,
  833. data->assoc_info.req_ies_len,
  834. data->assoc_info.reassoc);
  835. break;
  836. case EVENT_DISASSOC:
  837. if (data)
  838. hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
  839. break;
  840. case EVENT_DEAUTH:
  841. if (data)
  842. hostapd_notif_disassoc(hapd, data->deauth_info.addr);
  843. break;
  844. case EVENT_STATION_LOW_ACK:
  845. if (!data)
  846. break;
  847. hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
  848. break;
  849. case EVENT_AUTH:
  850. hostapd_notif_auth(hapd, &data->auth);
  851. break;
  852. case EVENT_CH_SWITCH:
  853. if (!data)
  854. break;
  855. hostapd_event_ch_switch(hapd, data->ch_switch.freq,
  856. data->ch_switch.ht_enabled,
  857. data->ch_switch.ch_offset,
  858. data->ch_switch.ch_width,
  859. data->ch_switch.cf1,
  860. data->ch_switch.cf2);
  861. break;
  862. case EVENT_CONNECT_FAILED_REASON:
  863. if (!data)
  864. break;
  865. hostapd_event_connect_failed_reason(
  866. hapd, data->connect_failed_reason.addr,
  867. data->connect_failed_reason.code);
  868. break;
  869. case EVENT_SURVEY:
  870. hostapd_event_get_survey(hapd, &data->survey_results);
  871. break;
  872. #ifdef NEED_AP_MLME
  873. case EVENT_DFS_RADAR_DETECTED:
  874. if (!data)
  875. break;
  876. hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
  877. break;
  878. case EVENT_DFS_CAC_FINISHED:
  879. if (!data)
  880. break;
  881. hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
  882. break;
  883. case EVENT_DFS_CAC_ABORTED:
  884. if (!data)
  885. break;
  886. hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
  887. break;
  888. case EVENT_DFS_NOP_FINISHED:
  889. if (!data)
  890. break;
  891. hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
  892. break;
  893. case EVENT_CHANNEL_LIST_CHANGED:
  894. /* channel list changed (regulatory?), update channel list */
  895. /* TODO: check this. hostapd_get_hw_features() initializes
  896. * too much stuff. */
  897. /* hostapd_get_hw_features(hapd->iface); */
  898. hostapd_channel_list_updated(
  899. hapd->iface, data->channel_list_changed.initiator);
  900. break;
  901. #endif /* NEED_AP_MLME */
  902. default:
  903. wpa_printf(MSG_DEBUG, "Unknown event %d", event);
  904. break;
  905. }
  906. }
  907. #endif /* HOSTAPD */