sta_info.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. /*
  2. * hostapd / Station table
  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 "common/ieee802_11_defs.h"
  12. #include "common/wpa_ctrl.h"
  13. #include "common/sae.h"
  14. #include "radius/radius.h"
  15. #include "radius/radius_client.h"
  16. #include "p2p/p2p.h"
  17. #include "fst/fst.h"
  18. #include "hostapd.h"
  19. #include "accounting.h"
  20. #include "ieee802_1x.h"
  21. #include "ieee802_11.h"
  22. #include "ieee802_11_auth.h"
  23. #include "wpa_auth.h"
  24. #include "preauth_auth.h"
  25. #include "ap_config.h"
  26. #include "beacon.h"
  27. #include "ap_mlme.h"
  28. #include "vlan_init.h"
  29. #include "p2p_hostapd.h"
  30. #include "ap_drv_ops.h"
  31. #include "gas_serv.h"
  32. #include "wnm_ap.h"
  33. #include "ndisc_snoop.h"
  34. #include "sta_info.h"
  35. #include "vlan.h"
  36. static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
  37. struct sta_info *sta);
  38. static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
  39. static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
  40. static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
  41. static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
  42. #ifdef CONFIG_IEEE80211W
  43. static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
  44. #endif /* CONFIG_IEEE80211W */
  45. static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
  46. int ap_for_each_sta(struct hostapd_data *hapd,
  47. int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
  48. void *ctx),
  49. void *ctx)
  50. {
  51. struct sta_info *sta;
  52. for (sta = hapd->sta_list; sta; sta = sta->next) {
  53. if (cb(hapd, sta, ctx))
  54. return 1;
  55. }
  56. return 0;
  57. }
  58. struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
  59. {
  60. struct sta_info *s;
  61. s = hapd->sta_hash[STA_HASH(sta)];
  62. while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
  63. s = s->hnext;
  64. return s;
  65. }
  66. #ifdef CONFIG_P2P
  67. struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
  68. {
  69. struct sta_info *sta;
  70. for (sta = hapd->sta_list; sta; sta = sta->next) {
  71. const u8 *p2p_dev_addr;
  72. if (sta->p2p_ie == NULL)
  73. continue;
  74. p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
  75. if (p2p_dev_addr == NULL)
  76. continue;
  77. if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
  78. return sta;
  79. }
  80. return NULL;
  81. }
  82. #endif /* CONFIG_P2P */
  83. static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
  84. {
  85. struct sta_info *tmp;
  86. if (hapd->sta_list == sta) {
  87. hapd->sta_list = sta->next;
  88. return;
  89. }
  90. tmp = hapd->sta_list;
  91. while (tmp != NULL && tmp->next != sta)
  92. tmp = tmp->next;
  93. if (tmp == NULL) {
  94. wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
  95. "list.", MAC2STR(sta->addr));
  96. } else
  97. tmp->next = sta->next;
  98. }
  99. void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
  100. {
  101. sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
  102. hapd->sta_hash[STA_HASH(sta->addr)] = sta;
  103. }
  104. static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
  105. {
  106. struct sta_info *s;
  107. s = hapd->sta_hash[STA_HASH(sta->addr)];
  108. if (s == NULL) return;
  109. if (os_memcmp(s->addr, sta->addr, 6) == 0) {
  110. hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
  111. return;
  112. }
  113. while (s->hnext != NULL &&
  114. os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
  115. s = s->hnext;
  116. if (s->hnext != NULL)
  117. s->hnext = s->hnext->hnext;
  118. else
  119. wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
  120. " from hash table", MAC2STR(sta->addr));
  121. }
  122. void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
  123. {
  124. sta_ip6addr_del(hapd, sta);
  125. }
  126. void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
  127. {
  128. int set_beacon = 0;
  129. accounting_sta_stop(hapd, sta);
  130. /* just in case */
  131. ap_sta_set_authorized(hapd, sta, 0);
  132. if (sta->flags & WLAN_STA_WDS)
  133. hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
  134. if (sta->ipaddr)
  135. hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
  136. ap_sta_ip6addr_del(hapd, sta);
  137. if (!hapd->iface->driver_ap_teardown &&
  138. !(sta->flags & WLAN_STA_PREAUTH))
  139. hostapd_drv_sta_remove(hapd, sta->addr);
  140. ap_sta_hash_del(hapd, sta);
  141. ap_sta_list_del(hapd, sta);
  142. if (sta->aid > 0)
  143. hapd->sta_aid[(sta->aid - 1) / 32] &=
  144. ~BIT((sta->aid - 1) % 32);
  145. hapd->num_sta--;
  146. if (sta->nonerp_set) {
  147. sta->nonerp_set = 0;
  148. hapd->iface->num_sta_non_erp--;
  149. if (hapd->iface->num_sta_non_erp == 0)
  150. set_beacon++;
  151. }
  152. if (sta->no_short_slot_time_set) {
  153. sta->no_short_slot_time_set = 0;
  154. hapd->iface->num_sta_no_short_slot_time--;
  155. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  156. && hapd->iface->num_sta_no_short_slot_time == 0)
  157. set_beacon++;
  158. }
  159. if (sta->no_short_preamble_set) {
  160. sta->no_short_preamble_set = 0;
  161. hapd->iface->num_sta_no_short_preamble--;
  162. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  163. && hapd->iface->num_sta_no_short_preamble == 0)
  164. set_beacon++;
  165. }
  166. if (sta->no_ht_gf_set) {
  167. sta->no_ht_gf_set = 0;
  168. hapd->iface->num_sta_ht_no_gf--;
  169. }
  170. if (sta->no_ht_set) {
  171. sta->no_ht_set = 0;
  172. hapd->iface->num_sta_no_ht--;
  173. }
  174. if (sta->ht_20mhz_set) {
  175. sta->ht_20mhz_set = 0;
  176. hapd->iface->num_sta_ht_20mhz--;
  177. }
  178. #ifdef CONFIG_IEEE80211N
  179. ht40_intolerant_remove(hapd->iface, sta);
  180. #endif /* CONFIG_IEEE80211N */
  181. #ifdef CONFIG_P2P
  182. if (sta->no_p2p_set) {
  183. sta->no_p2p_set = 0;
  184. hapd->num_sta_no_p2p--;
  185. if (hapd->num_sta_no_p2p == 0)
  186. hostapd_p2p_non_p2p_sta_disconnected(hapd);
  187. }
  188. #endif /* CONFIG_P2P */
  189. #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
  190. if (hostapd_ht_operation_update(hapd->iface) > 0)
  191. set_beacon++;
  192. #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
  193. #ifdef CONFIG_MESH
  194. if (hapd->mesh_sta_free_cb)
  195. hapd->mesh_sta_free_cb(hapd, sta);
  196. #endif /* CONFIG_MESH */
  197. if (set_beacon)
  198. ieee802_11_set_beacons(hapd->iface);
  199. wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
  200. __func__, MAC2STR(sta->addr));
  201. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  202. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  203. eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
  204. ap_sta_clear_disconnect_timeouts(hapd, sta);
  205. sae_clear_retransmit_timer(hapd, sta);
  206. ieee802_1x_free_station(hapd, sta);
  207. wpa_auth_sta_deinit(sta->wpa_sm);
  208. rsn_preauth_free_station(hapd, sta);
  209. #ifndef CONFIG_NO_RADIUS
  210. if (hapd->radius)
  211. radius_client_flush_auth(hapd->radius, sta->addr);
  212. #endif /* CONFIG_NO_RADIUS */
  213. #ifndef CONFIG_NO_VLAN
  214. /*
  215. * sta->wpa_sm->group needs to be released before so that
  216. * vlan_remove_dynamic() can check that no stations are left on the
  217. * AP_VLAN netdev.
  218. */
  219. if (sta->vlan_id)
  220. vlan_remove_dynamic(hapd, sta->vlan_id);
  221. if (sta->vlan_id_bound) {
  222. /*
  223. * Need to remove the STA entry before potentially removing the
  224. * VLAN.
  225. */
  226. if (hapd->iface->driver_ap_teardown &&
  227. !(sta->flags & WLAN_STA_PREAUTH))
  228. hostapd_drv_sta_remove(hapd, sta->addr);
  229. vlan_remove_dynamic(hapd, sta->vlan_id_bound);
  230. }
  231. #endif /* CONFIG_NO_VLAN */
  232. os_free(sta->challenge);
  233. #ifdef CONFIG_IEEE80211W
  234. os_free(sta->sa_query_trans_id);
  235. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  236. #endif /* CONFIG_IEEE80211W */
  237. #ifdef CONFIG_P2P
  238. p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
  239. #endif /* CONFIG_P2P */
  240. #ifdef CONFIG_INTERWORKING
  241. if (sta->gas_dialog) {
  242. int i;
  243. for (i = 0; i < GAS_DIALOG_MAX; i++)
  244. gas_serv_dialog_clear(&sta->gas_dialog[i]);
  245. os_free(sta->gas_dialog);
  246. }
  247. #endif /* CONFIG_INTERWORKING */
  248. wpabuf_free(sta->wps_ie);
  249. wpabuf_free(sta->p2p_ie);
  250. wpabuf_free(sta->hs20_ie);
  251. #ifdef CONFIG_FST
  252. wpabuf_free(sta->mb_ies);
  253. #endif /* CONFIG_FST */
  254. os_free(sta->ht_capabilities);
  255. os_free(sta->vht_capabilities);
  256. hostapd_free_psk_list(sta->psk);
  257. os_free(sta->identity);
  258. os_free(sta->radius_cui);
  259. os_free(sta->remediation_url);
  260. wpabuf_free(sta->hs20_deauth_req);
  261. os_free(sta->hs20_session_info_url);
  262. #ifdef CONFIG_SAE
  263. sae_clear_data(sta->sae);
  264. os_free(sta->sae);
  265. #endif /* CONFIG_SAE */
  266. os_free(sta);
  267. }
  268. void hostapd_free_stas(struct hostapd_data *hapd)
  269. {
  270. struct sta_info *sta, *prev;
  271. sta = hapd->sta_list;
  272. while (sta) {
  273. prev = sta;
  274. if (sta->flags & WLAN_STA_AUTH) {
  275. mlme_deauthenticate_indication(
  276. hapd, sta, WLAN_REASON_UNSPECIFIED);
  277. }
  278. sta = sta->next;
  279. wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
  280. MAC2STR(prev->addr));
  281. ap_free_sta(hapd, prev);
  282. }
  283. }
  284. /**
  285. * ap_handle_timer - Per STA timer handler
  286. * @eloop_ctx: struct hostapd_data *
  287. * @timeout_ctx: struct sta_info *
  288. *
  289. * This function is called to check station activity and to remove inactive
  290. * stations.
  291. */
  292. void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
  293. {
  294. struct hostapd_data *hapd = eloop_ctx;
  295. struct sta_info *sta = timeout_ctx;
  296. unsigned long next_time = 0;
  297. int reason;
  298. wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
  299. hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
  300. sta->timeout_next);
  301. if (sta->timeout_next == STA_REMOVE) {
  302. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  303. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  304. "local deauth request");
  305. ap_free_sta(hapd, sta);
  306. return;
  307. }
  308. if ((sta->flags & WLAN_STA_ASSOC) &&
  309. (sta->timeout_next == STA_NULLFUNC ||
  310. sta->timeout_next == STA_DISASSOC)) {
  311. int inactive_sec;
  312. /*
  313. * Add random value to timeout so that we don't end up bouncing
  314. * all stations at the same time if we have lots of associated
  315. * stations that are idle (but keep re-associating).
  316. */
  317. int fuzz = os_random() % 20;
  318. inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
  319. if (inactive_sec == -1) {
  320. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  321. "Check inactivity: Could not "
  322. "get station info from kernel driver for "
  323. MACSTR, MAC2STR(sta->addr));
  324. /*
  325. * The driver may not support this functionality.
  326. * Anyway, try again after the next inactivity timeout,
  327. * but do not disconnect the station now.
  328. */
  329. next_time = hapd->conf->ap_max_inactivity + fuzz;
  330. } else if (inactive_sec == -ENOENT) {
  331. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  332. "Station " MACSTR " has lost its driver entry",
  333. MAC2STR(sta->addr));
  334. /* Avoid sending client probe on removed client */
  335. sta->timeout_next = STA_DISASSOC;
  336. goto skip_poll;
  337. } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
  338. /* station activity detected; reset timeout state */
  339. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  340. "Station " MACSTR " has been active %is ago",
  341. MAC2STR(sta->addr), inactive_sec);
  342. sta->timeout_next = STA_NULLFUNC;
  343. next_time = hapd->conf->ap_max_inactivity + fuzz -
  344. inactive_sec;
  345. } else {
  346. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  347. "Station " MACSTR " has been "
  348. "inactive too long: %d sec, max allowed: %d",
  349. MAC2STR(sta->addr), inactive_sec,
  350. hapd->conf->ap_max_inactivity);
  351. if (hapd->conf->skip_inactivity_poll)
  352. sta->timeout_next = STA_DISASSOC;
  353. }
  354. }
  355. if ((sta->flags & WLAN_STA_ASSOC) &&
  356. sta->timeout_next == STA_DISASSOC &&
  357. !(sta->flags & WLAN_STA_PENDING_POLL) &&
  358. !hapd->conf->skip_inactivity_poll) {
  359. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
  360. " has ACKed data poll", MAC2STR(sta->addr));
  361. /* data nullfunc frame poll did not produce TX errors; assume
  362. * station ACKed it */
  363. sta->timeout_next = STA_NULLFUNC;
  364. next_time = hapd->conf->ap_max_inactivity;
  365. }
  366. skip_poll:
  367. if (next_time) {
  368. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  369. "for " MACSTR " (%lu seconds)",
  370. __func__, MAC2STR(sta->addr), next_time);
  371. eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
  372. sta);
  373. return;
  374. }
  375. if (sta->timeout_next == STA_NULLFUNC &&
  376. (sta->flags & WLAN_STA_ASSOC)) {
  377. wpa_printf(MSG_DEBUG, " Polling STA");
  378. sta->flags |= WLAN_STA_PENDING_POLL;
  379. hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
  380. sta->flags & WLAN_STA_WMM);
  381. } else if (sta->timeout_next != STA_REMOVE) {
  382. int deauth = sta->timeout_next == STA_DEAUTH;
  383. wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
  384. "Timeout, sending %s info to STA " MACSTR,
  385. deauth ? "deauthentication" : "disassociation",
  386. MAC2STR(sta->addr));
  387. if (deauth) {
  388. hostapd_drv_sta_deauth(
  389. hapd, sta->addr,
  390. WLAN_REASON_PREV_AUTH_NOT_VALID);
  391. } else {
  392. reason = (sta->timeout_next == STA_DISASSOC) ?
  393. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
  394. WLAN_REASON_PREV_AUTH_NOT_VALID;
  395. hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
  396. }
  397. }
  398. switch (sta->timeout_next) {
  399. case STA_NULLFUNC:
  400. sta->timeout_next = STA_DISASSOC;
  401. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  402. "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
  403. __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
  404. eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
  405. hapd, sta);
  406. break;
  407. case STA_DISASSOC:
  408. case STA_DISASSOC_FROM_CLI:
  409. ap_sta_set_authorized(hapd, sta, 0);
  410. sta->flags &= ~WLAN_STA_ASSOC;
  411. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  412. if (!sta->acct_terminate_cause)
  413. sta->acct_terminate_cause =
  414. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  415. accounting_sta_stop(hapd, sta);
  416. ieee802_1x_free_station(hapd, sta);
  417. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  418. HOSTAPD_LEVEL_INFO, "disassociated due to "
  419. "inactivity");
  420. reason = (sta->timeout_next == STA_DISASSOC) ?
  421. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
  422. WLAN_REASON_PREV_AUTH_NOT_VALID;
  423. sta->timeout_next = STA_DEAUTH;
  424. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  425. "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
  426. __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
  427. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  428. hapd, sta);
  429. mlme_disassociate_indication(hapd, sta, reason);
  430. break;
  431. case STA_DEAUTH:
  432. case STA_REMOVE:
  433. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  434. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  435. "inactivity (timer DEAUTH/REMOVE)");
  436. if (!sta->acct_terminate_cause)
  437. sta->acct_terminate_cause =
  438. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  439. mlme_deauthenticate_indication(
  440. hapd, sta,
  441. WLAN_REASON_PREV_AUTH_NOT_VALID);
  442. ap_free_sta(hapd, sta);
  443. break;
  444. }
  445. }
  446. static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
  447. {
  448. struct hostapd_data *hapd = eloop_ctx;
  449. struct sta_info *sta = timeout_ctx;
  450. wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
  451. hapd->conf->iface, MAC2STR(sta->addr));
  452. if (!(sta->flags & WLAN_STA_AUTH)) {
  453. if (sta->flags & WLAN_STA_GAS) {
  454. wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
  455. "entry " MACSTR, MAC2STR(sta->addr));
  456. ap_free_sta(hapd, sta);
  457. }
  458. return;
  459. }
  460. hostapd_drv_sta_deauth(hapd, sta->addr,
  461. WLAN_REASON_PREV_AUTH_NOT_VALID);
  462. mlme_deauthenticate_indication(hapd, sta,
  463. WLAN_REASON_PREV_AUTH_NOT_VALID);
  464. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  465. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  466. "session timeout");
  467. sta->acct_terminate_cause =
  468. RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
  469. ap_free_sta(hapd, sta);
  470. }
  471. void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
  472. u32 session_timeout)
  473. {
  474. if (eloop_replenish_timeout(session_timeout, 0,
  475. ap_handle_session_timer, hapd, sta) == 1) {
  476. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  477. HOSTAPD_LEVEL_DEBUG, "setting session timeout "
  478. "to %d seconds", session_timeout);
  479. }
  480. }
  481. void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
  482. u32 session_timeout)
  483. {
  484. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  485. HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
  486. "seconds", session_timeout);
  487. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  488. eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
  489. hapd, sta);
  490. }
  491. void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  492. {
  493. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  494. }
  495. static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
  496. {
  497. #ifdef CONFIG_WNM
  498. struct hostapd_data *hapd = eloop_ctx;
  499. struct sta_info *sta = timeout_ctx;
  500. wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
  501. MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
  502. if (sta->hs20_session_info_url == NULL)
  503. return;
  504. wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
  505. sta->hs20_disassoc_timer);
  506. #endif /* CONFIG_WNM */
  507. }
  508. void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
  509. struct sta_info *sta, int warning_time)
  510. {
  511. eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
  512. eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
  513. hapd, sta);
  514. }
  515. struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
  516. {
  517. struct sta_info *sta;
  518. sta = ap_get_sta(hapd, addr);
  519. if (sta)
  520. return sta;
  521. wpa_printf(MSG_DEBUG, " New STA");
  522. if (hapd->num_sta >= hapd->conf->max_num_sta) {
  523. /* FIX: might try to remove some old STAs first? */
  524. wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
  525. hapd->num_sta, hapd->conf->max_num_sta);
  526. return NULL;
  527. }
  528. sta = os_zalloc(sizeof(struct sta_info));
  529. if (sta == NULL) {
  530. wpa_printf(MSG_ERROR, "malloc failed");
  531. return NULL;
  532. }
  533. sta->acct_interim_interval = hapd->conf->acct_interim_interval;
  534. if (accounting_sta_get_id(hapd, sta) < 0) {
  535. os_free(sta);
  536. return NULL;
  537. }
  538. if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
  539. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  540. "for " MACSTR " (%d seconds - ap_max_inactivity)",
  541. __func__, MAC2STR(addr),
  542. hapd->conf->ap_max_inactivity);
  543. eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
  544. ap_handle_timer, hapd, sta);
  545. }
  546. /* initialize STA info data */
  547. os_memcpy(sta->addr, addr, ETH_ALEN);
  548. sta->next = hapd->sta_list;
  549. hapd->sta_list = sta;
  550. hapd->num_sta++;
  551. ap_sta_hash_add(hapd, sta);
  552. ap_sta_remove_in_other_bss(hapd, sta);
  553. sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
  554. dl_list_init(&sta->ip6addr);
  555. return sta;
  556. }
  557. static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
  558. {
  559. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  560. if (sta->ipaddr)
  561. hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
  562. ap_sta_ip6addr_del(hapd, sta);
  563. wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
  564. hapd->conf->iface, MAC2STR(sta->addr));
  565. if (hostapd_drv_sta_remove(hapd, sta->addr) &&
  566. sta->flags & WLAN_STA_ASSOC) {
  567. wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
  568. " from kernel driver",
  569. hapd->conf->iface, MAC2STR(sta->addr));
  570. return -1;
  571. }
  572. return 0;
  573. }
  574. static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
  575. struct sta_info *sta)
  576. {
  577. struct hostapd_iface *iface = hapd->iface;
  578. size_t i;
  579. for (i = 0; i < iface->num_bss; i++) {
  580. struct hostapd_data *bss = iface->bss[i];
  581. struct sta_info *sta2;
  582. /* bss should always be set during operation, but it may be
  583. * NULL during reconfiguration. Assume the STA is not
  584. * associated to another BSS in that case to avoid NULL pointer
  585. * dereferences. */
  586. if (bss == hapd || bss == NULL)
  587. continue;
  588. sta2 = ap_get_sta(bss, sta->addr);
  589. if (!sta2)
  590. continue;
  591. wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR
  592. " association from another BSS %s",
  593. hapd->conf->iface, MAC2STR(sta2->addr),
  594. bss->conf->iface);
  595. ap_sta_disconnect(bss, sta2, sta2->addr,
  596. WLAN_REASON_PREV_AUTH_NOT_VALID);
  597. }
  598. }
  599. static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
  600. {
  601. struct hostapd_data *hapd = eloop_ctx;
  602. struct sta_info *sta = timeout_ctx;
  603. wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
  604. hapd->conf->iface, MAC2STR(sta->addr));
  605. ap_sta_remove(hapd, sta);
  606. mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
  607. }
  608. void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
  609. u16 reason)
  610. {
  611. wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
  612. hapd->conf->iface, MAC2STR(sta->addr));
  613. sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
  614. sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
  615. ap_sta_set_authorized(hapd, sta, 0);
  616. sta->timeout_next = STA_DEAUTH;
  617. wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
  618. "for " MACSTR " (%d seconds - "
  619. "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
  620. __func__, MAC2STR(sta->addr),
  621. AP_MAX_INACTIVITY_AFTER_DISASSOC);
  622. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  623. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
  624. ap_handle_timer, hapd, sta);
  625. accounting_sta_stop(hapd, sta);
  626. ieee802_1x_free_station(hapd, sta);
  627. sta->disassoc_reason = reason;
  628. sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
  629. eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
  630. eloop_register_timeout(hapd->iface->drv_flags &
  631. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  632. ap_sta_disassoc_cb_timeout, hapd, sta);
  633. }
  634. static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
  635. {
  636. struct hostapd_data *hapd = eloop_ctx;
  637. struct sta_info *sta = timeout_ctx;
  638. wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
  639. hapd->conf->iface, MAC2STR(sta->addr));
  640. ap_sta_remove(hapd, sta);
  641. mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
  642. }
  643. void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
  644. u16 reason)
  645. {
  646. wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
  647. hapd->conf->iface, MAC2STR(sta->addr));
  648. sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
  649. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
  650. ap_sta_set_authorized(hapd, sta, 0);
  651. sta->timeout_next = STA_REMOVE;
  652. wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
  653. "for " MACSTR " (%d seconds - "
  654. "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
  655. __func__, MAC2STR(sta->addr),
  656. AP_MAX_INACTIVITY_AFTER_DEAUTH);
  657. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  658. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
  659. ap_handle_timer, hapd, sta);
  660. accounting_sta_stop(hapd, sta);
  661. ieee802_1x_free_station(hapd, sta);
  662. sta->deauth_reason = reason;
  663. sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
  664. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  665. eloop_register_timeout(hapd->iface->drv_flags &
  666. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  667. ap_sta_deauth_cb_timeout, hapd, sta);
  668. }
  669. #ifdef CONFIG_WPS
  670. int ap_sta_wps_cancel(struct hostapd_data *hapd,
  671. struct sta_info *sta, void *ctx)
  672. {
  673. if (sta && (sta->flags & WLAN_STA_WPS)) {
  674. ap_sta_deauthenticate(hapd, sta,
  675. WLAN_REASON_PREV_AUTH_NOT_VALID);
  676. wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
  677. __func__, MAC2STR(sta->addr));
  678. return 1;
  679. }
  680. return 0;
  681. }
  682. #endif /* CONFIG_WPS */
  683. static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
  684. {
  685. struct hostapd_vlan *vlan;
  686. int vlan_id = MAX_VLAN_ID + 2;
  687. retry:
  688. for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
  689. if (vlan->vlan_id == vlan_id) {
  690. vlan_id++;
  691. goto retry;
  692. }
  693. }
  694. return vlan_id;
  695. }
  696. int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
  697. struct vlan_description *vlan_desc)
  698. {
  699. struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
  700. int old_vlan_id, vlan_id = 0, ret = 0;
  701. if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  702. vlan_desc = NULL;
  703. /* Check if there is something to do */
  704. if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
  705. /* This sta is lacking its own vif */
  706. } else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
  707. !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
  708. /* sta->vlan_id needs to be reset */
  709. } else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
  710. return 0; /* nothing to change */
  711. }
  712. /* Now the real VLAN changed or the STA just needs its own vif */
  713. if (hapd->conf->ssid.per_sta_vif) {
  714. /* Assign a new vif, always */
  715. /* find a free vlan_id sufficiently big */
  716. vlan_id = ap_sta_get_free_vlan_id(hapd);
  717. /* Get wildcard VLAN */
  718. for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
  719. if (vlan->vlan_id == VLAN_ID_WILDCARD)
  720. break;
  721. }
  722. if (!vlan) {
  723. hostapd_logger(hapd, sta->addr,
  724. HOSTAPD_MODULE_IEEE80211,
  725. HOSTAPD_LEVEL_DEBUG,
  726. "per_sta_vif missing wildcard");
  727. vlan_id = 0;
  728. ret = -1;
  729. goto done;
  730. }
  731. } else if (vlan_desc && vlan_desc->notempty) {
  732. for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
  733. if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
  734. break;
  735. if (vlan->vlan_id == VLAN_ID_WILDCARD)
  736. wildcard_vlan = vlan;
  737. }
  738. if (vlan) {
  739. vlan_id = vlan->vlan_id;
  740. } else if (wildcard_vlan) {
  741. vlan = wildcard_vlan;
  742. vlan_id = vlan_desc->untagged;
  743. if (vlan_desc->tagged[0]) {
  744. /* Tagged VLAN configuration */
  745. vlan_id = ap_sta_get_free_vlan_id(hapd);
  746. }
  747. } else {
  748. hostapd_logger(hapd, sta->addr,
  749. HOSTAPD_MODULE_IEEE80211,
  750. HOSTAPD_LEVEL_DEBUG,
  751. "missing vlan and wildcard for vlan=%d%s",
  752. vlan_desc->untagged,
  753. vlan_desc->tagged[0] ? "+" : "");
  754. vlan_id = 0;
  755. ret = -1;
  756. goto done;
  757. }
  758. }
  759. if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
  760. vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
  761. if (vlan == NULL) {
  762. hostapd_logger(hapd, sta->addr,
  763. HOSTAPD_MODULE_IEEE80211,
  764. HOSTAPD_LEVEL_DEBUG,
  765. "could not add dynamic VLAN interface for vlan=%d%s",
  766. vlan_desc->untagged,
  767. vlan_desc->tagged[0] ? "+" : "");
  768. vlan_id = 0;
  769. ret = -1;
  770. goto done;
  771. }
  772. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  773. HOSTAPD_LEVEL_DEBUG,
  774. "added new dynamic VLAN interface '%s'",
  775. vlan->ifname);
  776. } else if (vlan && vlan->dynamic_vlan > 0) {
  777. vlan->dynamic_vlan++;
  778. hostapd_logger(hapd, sta->addr,
  779. HOSTAPD_MODULE_IEEE80211,
  780. HOSTAPD_LEVEL_DEBUG,
  781. "updated existing dynamic VLAN interface '%s'",
  782. vlan->ifname);
  783. }
  784. done:
  785. old_vlan_id = sta->vlan_id;
  786. sta->vlan_id = vlan_id;
  787. sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
  788. if (vlan_id != old_vlan_id && old_vlan_id)
  789. vlan_remove_dynamic(hapd, old_vlan_id);
  790. return ret;
  791. }
  792. int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
  793. {
  794. #ifndef CONFIG_NO_VLAN
  795. const char *iface;
  796. struct hostapd_vlan *vlan = NULL;
  797. int ret;
  798. int old_vlanid = sta->vlan_id_bound;
  799. iface = hapd->conf->iface;
  800. if (hapd->conf->ssid.vlan[0])
  801. iface = hapd->conf->ssid.vlan;
  802. if (sta->vlan_id > 0) {
  803. for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
  804. if (vlan->vlan_id == sta->vlan_id)
  805. break;
  806. }
  807. if (vlan)
  808. iface = vlan->ifname;
  809. }
  810. /*
  811. * Do not increment ref counters if the VLAN ID remains same, but do
  812. * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
  813. * have been called before.
  814. */
  815. if (sta->vlan_id == old_vlanid)
  816. goto skip_counting;
  817. if (sta->vlan_id > 0 && vlan == NULL) {
  818. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  819. HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
  820. "binding station to (vlan_id=%d)",
  821. sta->vlan_id);
  822. ret = -1;
  823. goto done;
  824. } else if (vlan && vlan->dynamic_vlan > 0) {
  825. vlan->dynamic_vlan++;
  826. hostapd_logger(hapd, sta->addr,
  827. HOSTAPD_MODULE_IEEE80211,
  828. HOSTAPD_LEVEL_DEBUG,
  829. "updated existing dynamic VLAN interface '%s'",
  830. iface);
  831. }
  832. /* ref counters have been increased, so mark the station */
  833. sta->vlan_id_bound = sta->vlan_id;
  834. skip_counting:
  835. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  836. HOSTAPD_LEVEL_DEBUG, "binding station to interface "
  837. "'%s'", iface);
  838. if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
  839. wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
  840. ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
  841. if (ret < 0) {
  842. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  843. HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
  844. "entry to vlan_id=%d", sta->vlan_id);
  845. }
  846. /* During 1x reauth, if the vlan id changes, then remove the old id. */
  847. if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
  848. vlan_remove_dynamic(hapd, old_vlanid);
  849. done:
  850. return ret;
  851. #else /* CONFIG_NO_VLAN */
  852. return 0;
  853. #endif /* CONFIG_NO_VLAN */
  854. }
  855. #ifdef CONFIG_IEEE80211W
  856. int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  857. {
  858. u32 tu;
  859. struct os_reltime now, passed;
  860. os_get_reltime(&now);
  861. os_reltime_sub(&now, &sta->sa_query_start, &passed);
  862. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  863. if (hapd->conf->assoc_sa_query_max_timeout < tu) {
  864. hostapd_logger(hapd, sta->addr,
  865. HOSTAPD_MODULE_IEEE80211,
  866. HOSTAPD_LEVEL_DEBUG,
  867. "association SA Query timed out");
  868. sta->sa_query_timed_out = 1;
  869. os_free(sta->sa_query_trans_id);
  870. sta->sa_query_trans_id = NULL;
  871. sta->sa_query_count = 0;
  872. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  873. return 1;
  874. }
  875. return 0;
  876. }
  877. static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
  878. {
  879. struct hostapd_data *hapd = eloop_ctx;
  880. struct sta_info *sta = timeout_ctx;
  881. unsigned int timeout, sec, usec;
  882. u8 *trans_id, *nbuf;
  883. wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
  884. " (count=%d)",
  885. hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
  886. if (sta->sa_query_count > 0 &&
  887. ap_check_sa_query_timeout(hapd, sta))
  888. return;
  889. nbuf = os_realloc_array(sta->sa_query_trans_id,
  890. sta->sa_query_count + 1,
  891. WLAN_SA_QUERY_TR_ID_LEN);
  892. if (nbuf == NULL)
  893. return;
  894. if (sta->sa_query_count == 0) {
  895. /* Starting a new SA Query procedure */
  896. os_get_reltime(&sta->sa_query_start);
  897. }
  898. trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
  899. sta->sa_query_trans_id = nbuf;
  900. sta->sa_query_count++;
  901. if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
  902. /*
  903. * We don't really care which ID is used here, so simply
  904. * hardcode this if the mostly theoretical os_get_random()
  905. * failure happens.
  906. */
  907. trans_id[0] = 0x12;
  908. trans_id[1] = 0x34;
  909. }
  910. timeout = hapd->conf->assoc_sa_query_retry_timeout;
  911. sec = ((timeout / 1000) * 1024) / 1000;
  912. usec = (timeout % 1000) * 1024;
  913. eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
  914. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  915. HOSTAPD_LEVEL_DEBUG,
  916. "association SA Query attempt %d", sta->sa_query_count);
  917. ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
  918. }
  919. void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  920. {
  921. ap_sa_query_timer(hapd, sta);
  922. }
  923. void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  924. {
  925. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  926. os_free(sta->sa_query_trans_id);
  927. sta->sa_query_trans_id = NULL;
  928. sta->sa_query_count = 0;
  929. }
  930. #endif /* CONFIG_IEEE80211W */
  931. void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
  932. int authorized)
  933. {
  934. const u8 *dev_addr = NULL;
  935. char buf[100];
  936. #ifdef CONFIG_P2P
  937. u8 addr[ETH_ALEN];
  938. u8 ip_addr_buf[4];
  939. #endif /* CONFIG_P2P */
  940. if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
  941. return;
  942. if (authorized)
  943. sta->flags |= WLAN_STA_AUTHORIZED;
  944. else
  945. sta->flags &= ~WLAN_STA_AUTHORIZED;
  946. #ifdef CONFIG_P2P
  947. if (hapd->p2p_group == NULL) {
  948. if (sta->p2p_ie != NULL &&
  949. p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
  950. dev_addr = addr;
  951. } else
  952. dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
  953. if (dev_addr)
  954. os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
  955. MAC2STR(sta->addr), MAC2STR(dev_addr));
  956. else
  957. #endif /* CONFIG_P2P */
  958. os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
  959. if (hapd->sta_authorized_cb)
  960. hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
  961. sta->addr, authorized, dev_addr);
  962. if (authorized) {
  963. char ip_addr[100];
  964. ip_addr[0] = '\0';
  965. #ifdef CONFIG_P2P
  966. if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
  967. os_snprintf(ip_addr, sizeof(ip_addr),
  968. " ip_addr=%u.%u.%u.%u",
  969. ip_addr_buf[0], ip_addr_buf[1],
  970. ip_addr_buf[2], ip_addr_buf[3]);
  971. }
  972. #endif /* CONFIG_P2P */
  973. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
  974. buf, ip_addr);
  975. if (hapd->msg_ctx_parent &&
  976. hapd->msg_ctx_parent != hapd->msg_ctx)
  977. wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
  978. AP_STA_CONNECTED "%s%s",
  979. buf, ip_addr);
  980. } else {
  981. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
  982. if (hapd->msg_ctx_parent &&
  983. hapd->msg_ctx_parent != hapd->msg_ctx)
  984. wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
  985. AP_STA_DISCONNECTED "%s", buf);
  986. }
  987. #ifdef CONFIG_FST
  988. if (hapd->iface->fst) {
  989. if (authorized)
  990. fst_notify_peer_connected(hapd->iface->fst, sta->addr);
  991. else
  992. fst_notify_peer_disconnected(hapd->iface->fst,
  993. sta->addr);
  994. }
  995. #endif /* CONFIG_FST */
  996. }
  997. void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
  998. const u8 *addr, u16 reason)
  999. {
  1000. if (sta)
  1001. wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
  1002. hapd->conf->iface, __func__, MAC2STR(sta->addr),
  1003. reason);
  1004. else if (addr)
  1005. wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
  1006. hapd->conf->iface, __func__, MAC2STR(addr),
  1007. reason);
  1008. if (sta == NULL && addr)
  1009. sta = ap_get_sta(hapd, addr);
  1010. if (addr)
  1011. hostapd_drv_sta_deauth(hapd, addr, reason);
  1012. if (sta == NULL)
  1013. return;
  1014. ap_sta_set_authorized(hapd, sta, 0);
  1015. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  1016. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1017. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  1018. wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
  1019. "for " MACSTR " (%d seconds - "
  1020. "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
  1021. hapd->conf->iface, __func__, MAC2STR(sta->addr),
  1022. AP_MAX_INACTIVITY_AFTER_DEAUTH);
  1023. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  1024. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
  1025. ap_handle_timer, hapd, sta);
  1026. sta->timeout_next = STA_REMOVE;
  1027. sta->deauth_reason = reason;
  1028. sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
  1029. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  1030. eloop_register_timeout(hapd->iface->drv_flags &
  1031. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  1032. ap_sta_deauth_cb_timeout, hapd, sta);
  1033. }
  1034. void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
  1035. {
  1036. if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
  1037. wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
  1038. return;
  1039. }
  1040. sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
  1041. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  1042. ap_sta_deauth_cb_timeout(hapd, sta);
  1043. }
  1044. void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
  1045. {
  1046. if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
  1047. wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
  1048. return;
  1049. }
  1050. sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
  1051. eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
  1052. ap_sta_disassoc_cb_timeout(hapd, sta);
  1053. }
  1054. void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
  1055. struct sta_info *sta)
  1056. {
  1057. if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
  1058. wpa_printf(MSG_DEBUG,
  1059. "%s: Removed ap_sta_deauth_cb_timeout timeout for "
  1060. MACSTR,
  1061. hapd->conf->iface, MAC2STR(sta->addr));
  1062. if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
  1063. wpa_printf(MSG_DEBUG,
  1064. "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
  1065. MACSTR,
  1066. hapd->conf->iface, MAC2STR(sta->addr));
  1067. }
  1068. int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
  1069. {
  1070. int res;
  1071. buf[0] = '\0';
  1072. res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  1073. (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
  1074. (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
  1075. (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
  1076. (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
  1077. ""),
  1078. (flags & WLAN_STA_SHORT_PREAMBLE ?
  1079. "[SHORT_PREAMBLE]" : ""),
  1080. (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
  1081. (flags & WLAN_STA_WMM ? "[WMM]" : ""),
  1082. (flags & WLAN_STA_MFP ? "[MFP]" : ""),
  1083. (flags & WLAN_STA_WPS ? "[WPS]" : ""),
  1084. (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
  1085. (flags & WLAN_STA_WDS ? "[WDS]" : ""),
  1086. (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
  1087. (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
  1088. (flags & WLAN_STA_GAS ? "[GAS]" : ""),
  1089. (flags & WLAN_STA_VHT ? "[VHT]" : ""),
  1090. (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
  1091. (flags & WLAN_STA_WNM_SLEEP_MODE ?
  1092. "[WNM_SLEEP_MODE]" : ""));
  1093. if (os_snprintf_error(buflen, res))
  1094. res = -1;
  1095. return res;
  1096. }