drv_callbacks.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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->iface->csa_in_progress &&
  441. freq == hapd->iface->cs_freq_params.freq) {
  442. hostapd_cleanup_cs_params(hapd);
  443. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED "freq=%d",
  444. freq);
  445. }
  446. #endif /* NEED_AP_MLME */
  447. }
  448. void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
  449. const u8 *addr, int reason_code)
  450. {
  451. switch (reason_code) {
  452. case MAX_CLIENT_REACHED:
  453. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
  454. MAC2STR(addr));
  455. break;
  456. case BLOCKED_CLIENT:
  457. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
  458. MAC2STR(addr));
  459. break;
  460. }
  461. }
  462. int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
  463. const u8 *bssid, const u8 *ie, size_t ie_len,
  464. int ssi_signal)
  465. {
  466. size_t i;
  467. int ret = 0;
  468. if (sa == NULL || ie == NULL)
  469. return -1;
  470. random_add_randomness(sa, ETH_ALEN);
  471. for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
  472. if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
  473. sa, da, bssid, ie, ie_len,
  474. ssi_signal) > 0) {
  475. ret = 1;
  476. break;
  477. }
  478. }
  479. return ret;
  480. }
  481. #ifdef HOSTAPD
  482. #ifdef CONFIG_IEEE80211R
  483. static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
  484. const u8 *bssid,
  485. u16 auth_transaction, u16 status,
  486. const u8 *ies, size_t ies_len)
  487. {
  488. struct hostapd_data *hapd = ctx;
  489. struct sta_info *sta;
  490. sta = ap_get_sta(hapd, dst);
  491. if (sta == NULL)
  492. return;
  493. hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
  494. HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
  495. sta->flags |= WLAN_STA_AUTH;
  496. hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
  497. }
  498. #endif /* CONFIG_IEEE80211R */
  499. static void hostapd_notif_auth(struct hostapd_data *hapd,
  500. struct auth_info *rx_auth)
  501. {
  502. struct sta_info *sta;
  503. u16 status = WLAN_STATUS_SUCCESS;
  504. u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
  505. size_t resp_ies_len = 0;
  506. sta = ap_get_sta(hapd, rx_auth->peer);
  507. if (!sta) {
  508. sta = ap_sta_add(hapd, rx_auth->peer);
  509. if (sta == NULL) {
  510. status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
  511. goto fail;
  512. }
  513. }
  514. sta->flags &= ~WLAN_STA_PREAUTH;
  515. ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
  516. #ifdef CONFIG_IEEE80211R
  517. if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
  518. sta->auth_alg = WLAN_AUTH_FT;
  519. if (sta->wpa_sm == NULL)
  520. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  521. sta->addr, NULL);
  522. if (sta->wpa_sm == NULL) {
  523. wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
  524. "state machine");
  525. status = WLAN_STATUS_UNSPECIFIED_FAILURE;
  526. goto fail;
  527. }
  528. wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
  529. rx_auth->auth_transaction, rx_auth->ies,
  530. rx_auth->ies_len,
  531. hostapd_notify_auth_ft_finish, hapd);
  532. return;
  533. }
  534. #endif /* CONFIG_IEEE80211R */
  535. fail:
  536. hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
  537. status, resp_ies, resp_ies_len);
  538. }
  539. static void hostapd_action_rx(struct hostapd_data *hapd,
  540. struct rx_mgmt *drv_mgmt)
  541. {
  542. struct ieee80211_mgmt *mgmt;
  543. struct sta_info *sta;
  544. size_t plen __maybe_unused;
  545. u16 fc;
  546. if (drv_mgmt->frame_len < 24 + 1)
  547. return;
  548. plen = drv_mgmt->frame_len - 24 - 1;
  549. mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
  550. fc = le_to_host16(mgmt->frame_control);
  551. if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
  552. return; /* handled by the driver */
  553. wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
  554. mgmt->u.action.category, (int) plen);
  555. sta = ap_get_sta(hapd, mgmt->sa);
  556. if (sta == NULL) {
  557. wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
  558. return;
  559. }
  560. #ifdef CONFIG_IEEE80211R
  561. if (mgmt->u.action.category == WLAN_ACTION_FT) {
  562. const u8 *payload = drv_mgmt->frame + 24 + 1;
  563. wpa_ft_action_rx(sta->wpa_sm, payload, plen);
  564. }
  565. #endif /* CONFIG_IEEE80211R */
  566. #ifdef CONFIG_IEEE80211W
  567. if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY && plen >= 4) {
  568. ieee802_11_sa_query_action(
  569. hapd, mgmt->sa,
  570. mgmt->u.action.u.sa_query_resp.action,
  571. mgmt->u.action.u.sa_query_resp.trans_id);
  572. }
  573. #endif /* CONFIG_IEEE80211W */
  574. #ifdef CONFIG_WNM
  575. if (mgmt->u.action.category == WLAN_ACTION_WNM) {
  576. ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
  577. }
  578. #endif /* CONFIG_WNM */
  579. }
  580. #ifdef NEED_AP_MLME
  581. #define HAPD_BROADCAST ((struct hostapd_data *) -1)
  582. static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
  583. const u8 *bssid)
  584. {
  585. size_t i;
  586. if (bssid == NULL)
  587. return NULL;
  588. if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
  589. bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
  590. return HAPD_BROADCAST;
  591. for (i = 0; i < iface->num_bss; i++) {
  592. if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
  593. return iface->bss[i];
  594. }
  595. return NULL;
  596. }
  597. static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
  598. const u8 *bssid, const u8 *addr,
  599. int wds)
  600. {
  601. hapd = get_hapd_bssid(hapd->iface, bssid);
  602. if (hapd == NULL || hapd == HAPD_BROADCAST)
  603. return;
  604. ieee802_11_rx_from_unknown(hapd, addr, wds);
  605. }
  606. static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
  607. {
  608. struct hostapd_iface *iface = hapd->iface;
  609. const struct ieee80211_hdr *hdr;
  610. const u8 *bssid;
  611. struct hostapd_frame_info fi;
  612. int ret;
  613. #ifdef CONFIG_TESTING_OPTIONS
  614. if (hapd->ext_mgmt_frame_handling) {
  615. size_t hex_len = 2 * rx_mgmt->frame_len + 1;
  616. char *hex = os_malloc(hex_len);
  617. if (hex) {
  618. wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
  619. rx_mgmt->frame_len);
  620. wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
  621. os_free(hex);
  622. }
  623. return 1;
  624. }
  625. #endif /* CONFIG_TESTING_OPTIONS */
  626. hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
  627. bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
  628. if (bssid == NULL)
  629. return 0;
  630. hapd = get_hapd_bssid(iface, bssid);
  631. if (hapd == NULL) {
  632. u16 fc;
  633. fc = le_to_host16(hdr->frame_control);
  634. /*
  635. * Drop frames to unknown BSSIDs except for Beacon frames which
  636. * could be used to update neighbor information.
  637. */
  638. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  639. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  640. hapd = iface->bss[0];
  641. else
  642. return 0;
  643. }
  644. os_memset(&fi, 0, sizeof(fi));
  645. fi.datarate = rx_mgmt->datarate;
  646. fi.ssi_signal = rx_mgmt->ssi_signal;
  647. if (hapd == HAPD_BROADCAST) {
  648. size_t i;
  649. ret = 0;
  650. for (i = 0; i < iface->num_bss; i++) {
  651. /* if bss is set, driver will call this function for
  652. * each bss individually. */
  653. if (rx_mgmt->drv_priv &&
  654. (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
  655. continue;
  656. if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
  657. rx_mgmt->frame_len, &fi) > 0)
  658. ret = 1;
  659. }
  660. } else
  661. ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
  662. &fi);
  663. random_add_randomness(&fi, sizeof(fi));
  664. return ret;
  665. }
  666. static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
  667. size_t len, u16 stype, int ok)
  668. {
  669. struct ieee80211_hdr *hdr;
  670. hdr = (struct ieee80211_hdr *) buf;
  671. hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
  672. if (hapd == NULL || hapd == HAPD_BROADCAST)
  673. return;
  674. ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
  675. }
  676. #endif /* NEED_AP_MLME */
  677. static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
  678. {
  679. struct sta_info *sta = ap_get_sta(hapd, addr);
  680. if (sta)
  681. return 0;
  682. wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
  683. " - adding a new STA", MAC2STR(addr));
  684. sta = ap_sta_add(hapd, addr);
  685. if (sta) {
  686. hostapd_new_assoc_sta(hapd, sta, 0);
  687. } else {
  688. wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
  689. MAC2STR(addr));
  690. return -1;
  691. }
  692. return 0;
  693. }
  694. static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
  695. const u8 *data, size_t data_len)
  696. {
  697. struct hostapd_iface *iface = hapd->iface;
  698. struct sta_info *sta;
  699. size_t j;
  700. for (j = 0; j < iface->num_bss; j++) {
  701. if ((sta = ap_get_sta(iface->bss[j], src))) {
  702. if (sta->flags & WLAN_STA_ASSOC) {
  703. hapd = iface->bss[j];
  704. break;
  705. }
  706. }
  707. }
  708. ieee802_1x_receive(hapd, src, data, data_len);
  709. }
  710. static struct hostapd_channel_data * hostapd_get_mode_channel(
  711. struct hostapd_iface *iface, unsigned int freq)
  712. {
  713. int i;
  714. struct hostapd_channel_data *chan;
  715. for (i = 0; i < iface->current_mode->num_channels; i++) {
  716. chan = &iface->current_mode->channels[i];
  717. if (!chan)
  718. return NULL;
  719. if ((unsigned int) chan->freq == freq)
  720. return chan;
  721. }
  722. return NULL;
  723. }
  724. static void hostapd_update_nf(struct hostapd_iface *iface,
  725. struct hostapd_channel_data *chan,
  726. struct freq_survey *survey)
  727. {
  728. if (!iface->chans_surveyed) {
  729. chan->min_nf = survey->nf;
  730. iface->lowest_nf = survey->nf;
  731. } else {
  732. if (dl_list_empty(&chan->survey_list))
  733. chan->min_nf = survey->nf;
  734. else if (survey->nf < chan->min_nf)
  735. chan->min_nf = survey->nf;
  736. if (survey->nf < iface->lowest_nf)
  737. iface->lowest_nf = survey->nf;
  738. }
  739. }
  740. static void hostapd_event_get_survey(struct hostapd_data *hapd,
  741. struct survey_results *survey_results)
  742. {
  743. struct hostapd_iface *iface = hapd->iface;
  744. struct freq_survey *survey, *tmp;
  745. struct hostapd_channel_data *chan;
  746. if (dl_list_empty(&survey_results->survey_list)) {
  747. wpa_printf(MSG_DEBUG, "No survey data received");
  748. return;
  749. }
  750. dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
  751. struct freq_survey, list) {
  752. chan = hostapd_get_mode_channel(iface, survey->freq);
  753. if (!chan)
  754. continue;
  755. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  756. continue;
  757. dl_list_del(&survey->list);
  758. dl_list_add_tail(&chan->survey_list, &survey->list);
  759. hostapd_update_nf(iface, chan, survey);
  760. iface->chans_surveyed++;
  761. }
  762. }
  763. #ifdef NEED_AP_MLME
  764. static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
  765. struct dfs_event *radar)
  766. {
  767. wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
  768. hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
  769. radar->chan_offset, radar->chan_width,
  770. radar->cf1, radar->cf2);
  771. }
  772. static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
  773. struct dfs_event *radar)
  774. {
  775. wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
  776. hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
  777. radar->chan_offset, radar->chan_width,
  778. radar->cf1, radar->cf2);
  779. }
  780. static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
  781. struct dfs_event *radar)
  782. {
  783. wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
  784. hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
  785. radar->chan_offset, radar->chan_width,
  786. radar->cf1, radar->cf2);
  787. }
  788. static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
  789. struct dfs_event *radar)
  790. {
  791. wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
  792. hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
  793. radar->chan_offset, radar->chan_width,
  794. radar->cf1, radar->cf2);
  795. }
  796. #endif /* NEED_AP_MLME */
  797. void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
  798. union wpa_event_data *data)
  799. {
  800. struct hostapd_data *hapd = ctx;
  801. #ifndef CONFIG_NO_STDOUT_DEBUG
  802. int level = MSG_DEBUG;
  803. if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
  804. data->rx_mgmt.frame_len >= 24) {
  805. const struct ieee80211_hdr *hdr;
  806. u16 fc;
  807. hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
  808. fc = le_to_host16(hdr->frame_control);
  809. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  810. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
  811. level = MSG_EXCESSIVE;
  812. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  813. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
  814. level = MSG_EXCESSIVE;
  815. }
  816. wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
  817. event_to_string(event), event);
  818. #endif /* CONFIG_NO_STDOUT_DEBUG */
  819. switch (event) {
  820. case EVENT_MICHAEL_MIC_FAILURE:
  821. michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
  822. break;
  823. case EVENT_SCAN_RESULTS:
  824. if (hapd->iface->scan_cb)
  825. hapd->iface->scan_cb(hapd->iface);
  826. break;
  827. #ifdef CONFIG_IEEE80211R
  828. case EVENT_FT_RRB_RX:
  829. wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
  830. data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
  831. break;
  832. #endif /* CONFIG_IEEE80211R */
  833. case EVENT_WPS_BUTTON_PUSHED:
  834. hostapd_wps_button_pushed(hapd, NULL);
  835. break;
  836. #ifdef NEED_AP_MLME
  837. case EVENT_TX_STATUS:
  838. switch (data->tx_status.type) {
  839. case WLAN_FC_TYPE_MGMT:
  840. hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
  841. data->tx_status.data_len,
  842. data->tx_status.stype,
  843. data->tx_status.ack);
  844. break;
  845. case WLAN_FC_TYPE_DATA:
  846. hostapd_tx_status(hapd, data->tx_status.dst,
  847. data->tx_status.data,
  848. data->tx_status.data_len,
  849. data->tx_status.ack);
  850. break;
  851. }
  852. break;
  853. case EVENT_EAPOL_TX_STATUS:
  854. hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
  855. data->eapol_tx_status.data,
  856. data->eapol_tx_status.data_len,
  857. data->eapol_tx_status.ack);
  858. break;
  859. case EVENT_DRIVER_CLIENT_POLL_OK:
  860. hostapd_client_poll_ok(hapd, data->client_poll.addr);
  861. break;
  862. case EVENT_RX_FROM_UNKNOWN:
  863. hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
  864. data->rx_from_unknown.addr,
  865. data->rx_from_unknown.wds);
  866. break;
  867. #endif /* NEED_AP_MLME */
  868. case EVENT_RX_MGMT:
  869. if (!data->rx_mgmt.frame)
  870. break;
  871. #ifdef NEED_AP_MLME
  872. if (hostapd_mgmt_rx(hapd, &data->rx_mgmt) > 0)
  873. break;
  874. #endif /* NEED_AP_MLME */
  875. hostapd_action_rx(hapd, &data->rx_mgmt);
  876. break;
  877. case EVENT_RX_PROBE_REQ:
  878. if (data->rx_probe_req.sa == NULL ||
  879. data->rx_probe_req.ie == NULL)
  880. break;
  881. hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
  882. data->rx_probe_req.da,
  883. data->rx_probe_req.bssid,
  884. data->rx_probe_req.ie,
  885. data->rx_probe_req.ie_len,
  886. data->rx_probe_req.ssi_signal);
  887. break;
  888. case EVENT_NEW_STA:
  889. hostapd_event_new_sta(hapd, data->new_sta.addr);
  890. break;
  891. case EVENT_EAPOL_RX:
  892. hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
  893. data->eapol_rx.data,
  894. data->eapol_rx.data_len);
  895. break;
  896. case EVENT_ASSOC:
  897. hostapd_notif_assoc(hapd, data->assoc_info.addr,
  898. data->assoc_info.req_ies,
  899. data->assoc_info.req_ies_len,
  900. data->assoc_info.reassoc);
  901. break;
  902. case EVENT_DISASSOC:
  903. if (data)
  904. hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
  905. break;
  906. case EVENT_DEAUTH:
  907. if (data)
  908. hostapd_notif_disassoc(hapd, data->deauth_info.addr);
  909. break;
  910. case EVENT_STATION_LOW_ACK:
  911. if (!data)
  912. break;
  913. hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
  914. break;
  915. case EVENT_AUTH:
  916. hostapd_notif_auth(hapd, &data->auth);
  917. break;
  918. case EVENT_CH_SWITCH:
  919. if (!data)
  920. break;
  921. hostapd_event_ch_switch(hapd, data->ch_switch.freq,
  922. data->ch_switch.ht_enabled,
  923. data->ch_switch.ch_offset,
  924. data->ch_switch.ch_width,
  925. data->ch_switch.cf1,
  926. data->ch_switch.cf2);
  927. break;
  928. case EVENT_CONNECT_FAILED_REASON:
  929. if (!data)
  930. break;
  931. hostapd_event_connect_failed_reason(
  932. hapd, data->connect_failed_reason.addr,
  933. data->connect_failed_reason.code);
  934. break;
  935. case EVENT_SURVEY:
  936. hostapd_event_get_survey(hapd, &data->survey_results);
  937. break;
  938. #ifdef NEED_AP_MLME
  939. case EVENT_DFS_RADAR_DETECTED:
  940. if (!data)
  941. break;
  942. hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
  943. break;
  944. case EVENT_DFS_CAC_FINISHED:
  945. if (!data)
  946. break;
  947. hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
  948. break;
  949. case EVENT_DFS_CAC_ABORTED:
  950. if (!data)
  951. break;
  952. hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
  953. break;
  954. case EVENT_DFS_NOP_FINISHED:
  955. if (!data)
  956. break;
  957. hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
  958. break;
  959. case EVENT_CHANNEL_LIST_CHANGED:
  960. /* channel list changed (regulatory?), update channel list */
  961. /* TODO: check this. hostapd_get_hw_features() initializes
  962. * too much stuff. */
  963. /* hostapd_get_hw_features(hapd->iface); */
  964. hostapd_channel_list_updated(
  965. hapd->iface, data->channel_list_changed.initiator);
  966. break;
  967. #endif /* NEED_AP_MLME */
  968. default:
  969. wpa_printf(MSG_DEBUG, "Unknown event %d", event);
  970. break;
  971. }
  972. }
  973. #endif /* HOSTAPD */