drv_callbacks.c 29 KB

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