sta_info.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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 "hostapd.h"
  18. #include "accounting.h"
  19. #include "ieee802_1x.h"
  20. #include "ieee802_11.h"
  21. #include "ieee802_11_auth.h"
  22. #include "wpa_auth.h"
  23. #include "preauth_auth.h"
  24. #include "ap_config.h"
  25. #include "beacon.h"
  26. #include "ap_mlme.h"
  27. #include "vlan_init.h"
  28. #include "p2p_hostapd.h"
  29. #include "ap_drv_ops.h"
  30. #include "gas_serv.h"
  31. #include "sta_info.h"
  32. static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
  33. struct sta_info *sta);
  34. static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
  35. static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
  36. static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
  37. #ifdef CONFIG_IEEE80211W
  38. static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
  39. #endif /* CONFIG_IEEE80211W */
  40. static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
  41. int ap_for_each_sta(struct hostapd_data *hapd,
  42. int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
  43. void *ctx),
  44. void *ctx)
  45. {
  46. struct sta_info *sta;
  47. for (sta = hapd->sta_list; sta; sta = sta->next) {
  48. if (cb(hapd, sta, ctx))
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
  54. {
  55. struct sta_info *s;
  56. s = hapd->sta_hash[STA_HASH(sta)];
  57. while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
  58. s = s->hnext;
  59. return s;
  60. }
  61. #ifdef CONFIG_P2P
  62. struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
  63. {
  64. struct sta_info *sta;
  65. for (sta = hapd->sta_list; sta; sta = sta->next) {
  66. const u8 *p2p_dev_addr;
  67. if (sta->p2p_ie == NULL)
  68. continue;
  69. p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
  70. if (p2p_dev_addr == NULL)
  71. continue;
  72. if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
  73. return sta;
  74. }
  75. return NULL;
  76. }
  77. #endif /* CONFIG_P2P */
  78. static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
  79. {
  80. struct sta_info *tmp;
  81. if (hapd->sta_list == sta) {
  82. hapd->sta_list = sta->next;
  83. return;
  84. }
  85. tmp = hapd->sta_list;
  86. while (tmp != NULL && tmp->next != sta)
  87. tmp = tmp->next;
  88. if (tmp == NULL) {
  89. wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
  90. "list.", MAC2STR(sta->addr));
  91. } else
  92. tmp->next = sta->next;
  93. }
  94. void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
  95. {
  96. sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
  97. hapd->sta_hash[STA_HASH(sta->addr)] = sta;
  98. }
  99. static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
  100. {
  101. struct sta_info *s;
  102. s = hapd->sta_hash[STA_HASH(sta->addr)];
  103. if (s == NULL) return;
  104. if (os_memcmp(s->addr, sta->addr, 6) == 0) {
  105. hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
  106. return;
  107. }
  108. while (s->hnext != NULL &&
  109. os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
  110. s = s->hnext;
  111. if (s->hnext != NULL)
  112. s->hnext = s->hnext->hnext;
  113. else
  114. wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
  115. " from hash table", MAC2STR(sta->addr));
  116. }
  117. void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
  118. {
  119. int set_beacon = 0;
  120. accounting_sta_stop(hapd, sta);
  121. /* just in case */
  122. ap_sta_set_authorized(hapd, sta, 0);
  123. if (sta->flags & WLAN_STA_WDS)
  124. hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
  125. if (!(sta->flags & WLAN_STA_PREAUTH))
  126. hostapd_drv_sta_remove(hapd, sta->addr);
  127. ap_sta_hash_del(hapd, sta);
  128. ap_sta_list_del(hapd, sta);
  129. if (sta->aid > 0)
  130. hapd->sta_aid[(sta->aid - 1) / 32] &=
  131. ~BIT((sta->aid - 1) % 32);
  132. hapd->num_sta--;
  133. if (sta->nonerp_set) {
  134. sta->nonerp_set = 0;
  135. hapd->iface->num_sta_non_erp--;
  136. if (hapd->iface->num_sta_non_erp == 0)
  137. set_beacon++;
  138. }
  139. if (sta->no_short_slot_time_set) {
  140. sta->no_short_slot_time_set = 0;
  141. hapd->iface->num_sta_no_short_slot_time--;
  142. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  143. && hapd->iface->num_sta_no_short_slot_time == 0)
  144. set_beacon++;
  145. }
  146. if (sta->no_short_preamble_set) {
  147. sta->no_short_preamble_set = 0;
  148. hapd->iface->num_sta_no_short_preamble--;
  149. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  150. && hapd->iface->num_sta_no_short_preamble == 0)
  151. set_beacon++;
  152. }
  153. if (sta->no_ht_gf_set) {
  154. sta->no_ht_gf_set = 0;
  155. hapd->iface->num_sta_ht_no_gf--;
  156. }
  157. if (sta->no_ht_set) {
  158. sta->no_ht_set = 0;
  159. hapd->iface->num_sta_no_ht--;
  160. }
  161. if (sta->ht_20mhz_set) {
  162. sta->ht_20mhz_set = 0;
  163. hapd->iface->num_sta_ht_20mhz--;
  164. }
  165. #ifdef CONFIG_P2P
  166. if (sta->no_p2p_set) {
  167. sta->no_p2p_set = 0;
  168. hapd->num_sta_no_p2p--;
  169. if (hapd->num_sta_no_p2p == 0)
  170. hostapd_p2p_non_p2p_sta_disconnected(hapd);
  171. }
  172. #endif /* CONFIG_P2P */
  173. #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
  174. if (hostapd_ht_operation_update(hapd->iface) > 0)
  175. set_beacon++;
  176. #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
  177. if (set_beacon)
  178. ieee802_11_set_beacons(hapd->iface);
  179. wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
  180. __func__, MAC2STR(sta->addr));
  181. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  182. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  183. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  184. eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
  185. ieee802_1x_free_station(sta);
  186. wpa_auth_sta_deinit(sta->wpa_sm);
  187. rsn_preauth_free_station(hapd, sta);
  188. #ifndef CONFIG_NO_RADIUS
  189. if (hapd->radius)
  190. radius_client_flush_auth(hapd->radius, sta->addr);
  191. #endif /* CONFIG_NO_RADIUS */
  192. os_free(sta->last_assoc_req);
  193. os_free(sta->challenge);
  194. #ifdef CONFIG_IEEE80211W
  195. os_free(sta->sa_query_trans_id);
  196. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  197. #endif /* CONFIG_IEEE80211W */
  198. #ifdef CONFIG_P2P
  199. p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
  200. #endif /* CONFIG_P2P */
  201. #ifdef CONFIG_INTERWORKING
  202. if (sta->gas_dialog) {
  203. int i;
  204. for (i = 0; i < GAS_DIALOG_MAX; i++)
  205. gas_serv_dialog_clear(&sta->gas_dialog[i]);
  206. os_free(sta->gas_dialog);
  207. }
  208. #endif /* CONFIG_INTERWORKING */
  209. wpabuf_free(sta->wps_ie);
  210. wpabuf_free(sta->p2p_ie);
  211. wpabuf_free(sta->hs20_ie);
  212. os_free(sta->ht_capabilities);
  213. os_free(sta->vht_capabilities);
  214. hostapd_free_psk_list(sta->psk);
  215. os_free(sta->identity);
  216. os_free(sta->radius_cui);
  217. #ifdef CONFIG_SAE
  218. sae_clear_data(sta->sae);
  219. os_free(sta->sae);
  220. #endif /* CONFIG_SAE */
  221. os_free(sta);
  222. }
  223. void hostapd_free_stas(struct hostapd_data *hapd)
  224. {
  225. struct sta_info *sta, *prev;
  226. sta = hapd->sta_list;
  227. while (sta) {
  228. prev = sta;
  229. if (sta->flags & WLAN_STA_AUTH) {
  230. mlme_deauthenticate_indication(
  231. hapd, sta, WLAN_REASON_UNSPECIFIED);
  232. }
  233. sta = sta->next;
  234. wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
  235. MAC2STR(prev->addr));
  236. ap_free_sta(hapd, prev);
  237. }
  238. }
  239. /**
  240. * ap_handle_timer - Per STA timer handler
  241. * @eloop_ctx: struct hostapd_data *
  242. * @timeout_ctx: struct sta_info *
  243. *
  244. * This function is called to check station activity and to remove inactive
  245. * stations.
  246. */
  247. void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
  248. {
  249. struct hostapd_data *hapd = eloop_ctx;
  250. struct sta_info *sta = timeout_ctx;
  251. unsigned long next_time = 0;
  252. int reason;
  253. wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
  254. __func__, MAC2STR(sta->addr), sta->flags,
  255. sta->timeout_next);
  256. if (sta->timeout_next == STA_REMOVE) {
  257. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  258. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  259. "local deauth request");
  260. ap_free_sta(hapd, sta);
  261. return;
  262. }
  263. if ((sta->flags & WLAN_STA_ASSOC) &&
  264. (sta->timeout_next == STA_NULLFUNC ||
  265. sta->timeout_next == STA_DISASSOC)) {
  266. int inactive_sec;
  267. /*
  268. * Add random value to timeout so that we don't end up bouncing
  269. * all stations at the same time if we have lots of associated
  270. * stations that are idle (but keep re-associating).
  271. */
  272. int fuzz = os_random() % 20;
  273. inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
  274. if (inactive_sec == -1) {
  275. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  276. "Check inactivity: Could not "
  277. "get station info from kernel driver for "
  278. MACSTR, MAC2STR(sta->addr));
  279. /*
  280. * The driver may not support this functionality.
  281. * Anyway, try again after the next inactivity timeout,
  282. * but do not disconnect the station now.
  283. */
  284. next_time = hapd->conf->ap_max_inactivity + fuzz;
  285. } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
  286. sta->flags & WLAN_STA_ASSOC) {
  287. /* station activity detected; reset timeout state */
  288. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  289. "Station " MACSTR " has been active %is ago",
  290. MAC2STR(sta->addr), inactive_sec);
  291. sta->timeout_next = STA_NULLFUNC;
  292. next_time = hapd->conf->ap_max_inactivity + fuzz -
  293. inactive_sec;
  294. } else {
  295. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  296. "Station " MACSTR " has been "
  297. "inactive too long: %d sec, max allowed: %d",
  298. MAC2STR(sta->addr), inactive_sec,
  299. hapd->conf->ap_max_inactivity);
  300. if (hapd->conf->skip_inactivity_poll)
  301. sta->timeout_next = STA_DISASSOC;
  302. }
  303. }
  304. if ((sta->flags & WLAN_STA_ASSOC) &&
  305. sta->timeout_next == STA_DISASSOC &&
  306. !(sta->flags & WLAN_STA_PENDING_POLL) &&
  307. !hapd->conf->skip_inactivity_poll) {
  308. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
  309. " has ACKed data poll", MAC2STR(sta->addr));
  310. /* data nullfunc frame poll did not produce TX errors; assume
  311. * station ACKed it */
  312. sta->timeout_next = STA_NULLFUNC;
  313. next_time = hapd->conf->ap_max_inactivity;
  314. }
  315. if (next_time) {
  316. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  317. "for " MACSTR " (%lu seconds)",
  318. __func__, MAC2STR(sta->addr), next_time);
  319. eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
  320. sta);
  321. return;
  322. }
  323. if (sta->timeout_next == STA_NULLFUNC &&
  324. (sta->flags & WLAN_STA_ASSOC)) {
  325. wpa_printf(MSG_DEBUG, " Polling STA");
  326. sta->flags |= WLAN_STA_PENDING_POLL;
  327. hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
  328. sta->flags & WLAN_STA_WMM);
  329. } else if (sta->timeout_next != STA_REMOVE) {
  330. int deauth = sta->timeout_next == STA_DEAUTH;
  331. wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
  332. "Timeout, sending %s info to STA " MACSTR,
  333. deauth ? "deauthentication" : "disassociation",
  334. MAC2STR(sta->addr));
  335. if (deauth) {
  336. hostapd_drv_sta_deauth(
  337. hapd, sta->addr,
  338. WLAN_REASON_PREV_AUTH_NOT_VALID);
  339. } else {
  340. reason = (sta->timeout_next == STA_DISASSOC) ?
  341. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
  342. WLAN_REASON_PREV_AUTH_NOT_VALID;
  343. hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
  344. }
  345. }
  346. switch (sta->timeout_next) {
  347. case STA_NULLFUNC:
  348. sta->timeout_next = STA_DISASSOC;
  349. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  350. "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
  351. __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
  352. eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
  353. hapd, sta);
  354. break;
  355. case STA_DISASSOC:
  356. case STA_DISASSOC_FROM_CLI:
  357. ap_sta_set_authorized(hapd, sta, 0);
  358. sta->flags &= ~WLAN_STA_ASSOC;
  359. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  360. if (!sta->acct_terminate_cause)
  361. sta->acct_terminate_cause =
  362. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  363. accounting_sta_stop(hapd, sta);
  364. ieee802_1x_free_station(sta);
  365. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  366. HOSTAPD_LEVEL_INFO, "disassociated due to "
  367. "inactivity");
  368. reason = (sta->timeout_next == STA_DISASSOC) ?
  369. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
  370. WLAN_REASON_PREV_AUTH_NOT_VALID;
  371. sta->timeout_next = STA_DEAUTH;
  372. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  373. "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
  374. __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
  375. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  376. hapd, sta);
  377. mlme_disassociate_indication(hapd, sta, reason);
  378. break;
  379. case STA_DEAUTH:
  380. case STA_REMOVE:
  381. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  382. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  383. "inactivity (timer DEAUTH/REMOVE)");
  384. if (!sta->acct_terminate_cause)
  385. sta->acct_terminate_cause =
  386. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  387. mlme_deauthenticate_indication(
  388. hapd, sta,
  389. WLAN_REASON_PREV_AUTH_NOT_VALID);
  390. ap_free_sta(hapd, sta);
  391. break;
  392. }
  393. }
  394. static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
  395. {
  396. struct hostapd_data *hapd = eloop_ctx;
  397. struct sta_info *sta = timeout_ctx;
  398. u8 addr[ETH_ALEN];
  399. if (!(sta->flags & WLAN_STA_AUTH)) {
  400. if (sta->flags & WLAN_STA_GAS) {
  401. wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
  402. "entry " MACSTR, MAC2STR(sta->addr));
  403. ap_free_sta(hapd, sta);
  404. }
  405. return;
  406. }
  407. mlme_deauthenticate_indication(hapd, sta,
  408. WLAN_REASON_PREV_AUTH_NOT_VALID);
  409. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  410. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  411. "session timeout");
  412. sta->acct_terminate_cause =
  413. RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
  414. os_memcpy(addr, sta->addr, ETH_ALEN);
  415. ap_free_sta(hapd, sta);
  416. hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
  417. }
  418. void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
  419. u32 session_timeout)
  420. {
  421. if (eloop_replenish_timeout(session_timeout, 0,
  422. ap_handle_session_timer, hapd, sta) == 1) {
  423. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  424. HOSTAPD_LEVEL_DEBUG, "setting session timeout "
  425. "to %d seconds", session_timeout);
  426. }
  427. }
  428. void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
  429. u32 session_timeout)
  430. {
  431. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  432. HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
  433. "seconds", session_timeout);
  434. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  435. eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
  436. hapd, sta);
  437. }
  438. void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  439. {
  440. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  441. }
  442. struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
  443. {
  444. struct sta_info *sta;
  445. sta = ap_get_sta(hapd, addr);
  446. if (sta)
  447. return sta;
  448. wpa_printf(MSG_DEBUG, " New STA");
  449. if (hapd->num_sta >= hapd->conf->max_num_sta) {
  450. /* FIX: might try to remove some old STAs first? */
  451. wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
  452. hapd->num_sta, hapd->conf->max_num_sta);
  453. return NULL;
  454. }
  455. sta = os_zalloc(sizeof(struct sta_info));
  456. if (sta == NULL) {
  457. wpa_printf(MSG_ERROR, "malloc failed");
  458. return NULL;
  459. }
  460. sta->acct_interim_interval = hapd->conf->acct_interim_interval;
  461. accounting_sta_get_id(hapd, sta);
  462. if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
  463. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  464. "for " MACSTR " (%d seconds - ap_max_inactivity)",
  465. __func__, MAC2STR(addr),
  466. hapd->conf->ap_max_inactivity);
  467. eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
  468. ap_handle_timer, hapd, sta);
  469. }
  470. /* initialize STA info data */
  471. os_memcpy(sta->addr, addr, ETH_ALEN);
  472. sta->next = hapd->sta_list;
  473. hapd->sta_list = sta;
  474. hapd->num_sta++;
  475. ap_sta_hash_add(hapd, sta);
  476. sta->ssid = &hapd->conf->ssid;
  477. ap_sta_remove_in_other_bss(hapd, sta);
  478. return sta;
  479. }
  480. static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
  481. {
  482. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  483. wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
  484. MAC2STR(sta->addr));
  485. if (hostapd_drv_sta_remove(hapd, sta->addr) &&
  486. sta->flags & WLAN_STA_ASSOC) {
  487. wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
  488. " from kernel driver.", MAC2STR(sta->addr));
  489. return -1;
  490. }
  491. return 0;
  492. }
  493. static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
  494. struct sta_info *sta)
  495. {
  496. struct hostapd_iface *iface = hapd->iface;
  497. size_t i;
  498. for (i = 0; i < iface->num_bss; i++) {
  499. struct hostapd_data *bss = iface->bss[i];
  500. struct sta_info *sta2;
  501. /* bss should always be set during operation, but it may be
  502. * NULL during reconfiguration. Assume the STA is not
  503. * associated to another BSS in that case to avoid NULL pointer
  504. * dereferences. */
  505. if (bss == hapd || bss == NULL)
  506. continue;
  507. sta2 = ap_get_sta(bss, sta->addr);
  508. if (!sta2)
  509. continue;
  510. ap_sta_disconnect(bss, sta2, sta2->addr,
  511. WLAN_REASON_PREV_AUTH_NOT_VALID);
  512. }
  513. }
  514. static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
  515. {
  516. struct hostapd_data *hapd = eloop_ctx;
  517. struct sta_info *sta = timeout_ctx;
  518. ap_sta_remove(hapd, sta);
  519. mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
  520. }
  521. void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
  522. u16 reason)
  523. {
  524. wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
  525. hapd->conf->iface, MAC2STR(sta->addr));
  526. sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
  527. ap_sta_set_authorized(hapd, sta, 0);
  528. sta->timeout_next = STA_DEAUTH;
  529. wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
  530. "for " MACSTR " (%d seconds - "
  531. "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
  532. __func__, MAC2STR(sta->addr),
  533. AP_MAX_INACTIVITY_AFTER_DISASSOC);
  534. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  535. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
  536. ap_handle_timer, hapd, sta);
  537. accounting_sta_stop(hapd, sta);
  538. ieee802_1x_free_station(sta);
  539. sta->disassoc_reason = reason;
  540. sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
  541. eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
  542. eloop_register_timeout(hapd->iface->drv_flags &
  543. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  544. ap_sta_disassoc_cb_timeout, hapd, sta);
  545. }
  546. static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
  547. {
  548. struct hostapd_data *hapd = eloop_ctx;
  549. struct sta_info *sta = timeout_ctx;
  550. ap_sta_remove(hapd, sta);
  551. mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
  552. }
  553. void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
  554. u16 reason)
  555. {
  556. wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
  557. hapd->conf->iface, MAC2STR(sta->addr));
  558. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  559. ap_sta_set_authorized(hapd, sta, 0);
  560. sta->timeout_next = STA_REMOVE;
  561. wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
  562. "for " MACSTR " (%d seconds - "
  563. "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
  564. __func__, MAC2STR(sta->addr),
  565. AP_MAX_INACTIVITY_AFTER_DEAUTH);
  566. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  567. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
  568. ap_handle_timer, hapd, sta);
  569. accounting_sta_stop(hapd, sta);
  570. ieee802_1x_free_station(sta);
  571. sta->deauth_reason = reason;
  572. sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
  573. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  574. eloop_register_timeout(hapd->iface->drv_flags &
  575. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  576. ap_sta_deauth_cb_timeout, hapd, sta);
  577. }
  578. #ifdef CONFIG_WPS
  579. int ap_sta_wps_cancel(struct hostapd_data *hapd,
  580. struct sta_info *sta, void *ctx)
  581. {
  582. if (sta && (sta->flags & WLAN_STA_WPS)) {
  583. ap_sta_deauthenticate(hapd, sta,
  584. WLAN_REASON_PREV_AUTH_NOT_VALID);
  585. wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
  586. __func__, MAC2STR(sta->addr));
  587. return 1;
  588. }
  589. return 0;
  590. }
  591. #endif /* CONFIG_WPS */
  592. int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
  593. int old_vlanid)
  594. {
  595. #ifndef CONFIG_NO_VLAN
  596. const char *iface;
  597. struct hostapd_vlan *vlan = NULL;
  598. int ret;
  599. /*
  600. * Do not proceed furthur if the vlan id remains same. We do not want
  601. * duplicate dynamic vlan entries.
  602. */
  603. if (sta->vlan_id == old_vlanid)
  604. return 0;
  605. /*
  606. * During 1x reauth, if the vlan id changes, then remove the old id and
  607. * proceed furthur to add the new one.
  608. */
  609. if (old_vlanid > 0)
  610. vlan_remove_dynamic(hapd, old_vlanid);
  611. iface = hapd->conf->iface;
  612. if (sta->ssid->vlan[0])
  613. iface = sta->ssid->vlan;
  614. if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  615. sta->vlan_id = 0;
  616. else if (sta->vlan_id > 0) {
  617. struct hostapd_vlan *wildcard_vlan = NULL;
  618. vlan = hapd->conf->vlan;
  619. while (vlan) {
  620. if (vlan->vlan_id == sta->vlan_id)
  621. break;
  622. if (vlan->vlan_id == VLAN_ID_WILDCARD)
  623. wildcard_vlan = vlan;
  624. vlan = vlan->next;
  625. }
  626. if (!vlan)
  627. vlan = wildcard_vlan;
  628. if (vlan)
  629. iface = vlan->ifname;
  630. }
  631. if (sta->vlan_id > 0 && vlan == NULL) {
  632. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  633. HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
  634. "binding station to (vlan_id=%d)",
  635. sta->vlan_id);
  636. return -1;
  637. } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
  638. vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
  639. if (vlan == NULL) {
  640. hostapd_logger(hapd, sta->addr,
  641. HOSTAPD_MODULE_IEEE80211,
  642. HOSTAPD_LEVEL_DEBUG, "could not add "
  643. "dynamic VLAN interface for vlan_id=%d",
  644. sta->vlan_id);
  645. return -1;
  646. }
  647. iface = vlan->ifname;
  648. if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
  649. hostapd_logger(hapd, sta->addr,
  650. HOSTAPD_MODULE_IEEE80211,
  651. HOSTAPD_LEVEL_DEBUG, "could not "
  652. "configure encryption for dynamic VLAN "
  653. "interface for vlan_id=%d",
  654. sta->vlan_id);
  655. }
  656. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  657. HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
  658. "interface '%s'", iface);
  659. } else if (vlan && vlan->vlan_id == sta->vlan_id) {
  660. if (sta->vlan_id > 0) {
  661. vlan->dynamic_vlan++;
  662. hostapd_logger(hapd, sta->addr,
  663. HOSTAPD_MODULE_IEEE80211,
  664. HOSTAPD_LEVEL_DEBUG, "updated existing "
  665. "dynamic VLAN interface '%s'", iface);
  666. }
  667. /*
  668. * Update encryption configuration for statically generated
  669. * VLAN interface. This is only used for static WEP
  670. * configuration for the case where hostapd did not yet know
  671. * which keys are to be used when the interface was added.
  672. */
  673. if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
  674. hostapd_logger(hapd, sta->addr,
  675. HOSTAPD_MODULE_IEEE80211,
  676. HOSTAPD_LEVEL_DEBUG, "could not "
  677. "configure encryption for VLAN "
  678. "interface for vlan_id=%d",
  679. sta->vlan_id);
  680. }
  681. }
  682. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  683. HOSTAPD_LEVEL_DEBUG, "binding station to interface "
  684. "'%s'", iface);
  685. if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
  686. wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
  687. ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
  688. if (ret < 0) {
  689. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  690. HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
  691. "entry to vlan_id=%d", sta->vlan_id);
  692. }
  693. return ret;
  694. #else /* CONFIG_NO_VLAN */
  695. return 0;
  696. #endif /* CONFIG_NO_VLAN */
  697. }
  698. #ifdef CONFIG_IEEE80211W
  699. int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  700. {
  701. u32 tu;
  702. struct os_reltime now, passed;
  703. os_get_reltime(&now);
  704. os_reltime_sub(&now, &sta->sa_query_start, &passed);
  705. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  706. if (hapd->conf->assoc_sa_query_max_timeout < tu) {
  707. hostapd_logger(hapd, sta->addr,
  708. HOSTAPD_MODULE_IEEE80211,
  709. HOSTAPD_LEVEL_DEBUG,
  710. "association SA Query timed out");
  711. sta->sa_query_timed_out = 1;
  712. os_free(sta->sa_query_trans_id);
  713. sta->sa_query_trans_id = NULL;
  714. sta->sa_query_count = 0;
  715. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  716. return 1;
  717. }
  718. return 0;
  719. }
  720. static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
  721. {
  722. struct hostapd_data *hapd = eloop_ctx;
  723. struct sta_info *sta = timeout_ctx;
  724. unsigned int timeout, sec, usec;
  725. u8 *trans_id, *nbuf;
  726. if (sta->sa_query_count > 0 &&
  727. ap_check_sa_query_timeout(hapd, sta))
  728. return;
  729. nbuf = os_realloc_array(sta->sa_query_trans_id,
  730. sta->sa_query_count + 1,
  731. WLAN_SA_QUERY_TR_ID_LEN);
  732. if (nbuf == NULL)
  733. return;
  734. if (sta->sa_query_count == 0) {
  735. /* Starting a new SA Query procedure */
  736. os_get_reltime(&sta->sa_query_start);
  737. }
  738. trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
  739. sta->sa_query_trans_id = nbuf;
  740. sta->sa_query_count++;
  741. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  742. timeout = hapd->conf->assoc_sa_query_retry_timeout;
  743. sec = ((timeout / 1000) * 1024) / 1000;
  744. usec = (timeout % 1000) * 1024;
  745. eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
  746. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  747. HOSTAPD_LEVEL_DEBUG,
  748. "association SA Query attempt %d", sta->sa_query_count);
  749. ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
  750. }
  751. void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  752. {
  753. ap_sa_query_timer(hapd, sta);
  754. }
  755. void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  756. {
  757. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  758. os_free(sta->sa_query_trans_id);
  759. sta->sa_query_trans_id = NULL;
  760. sta->sa_query_count = 0;
  761. }
  762. #endif /* CONFIG_IEEE80211W */
  763. void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
  764. int authorized)
  765. {
  766. const u8 *dev_addr = NULL;
  767. char buf[100];
  768. #ifdef CONFIG_P2P
  769. u8 addr[ETH_ALEN];
  770. #endif /* CONFIG_P2P */
  771. if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
  772. return;
  773. #ifdef CONFIG_P2P
  774. if (hapd->p2p_group == NULL) {
  775. if (sta->p2p_ie != NULL &&
  776. p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
  777. dev_addr = addr;
  778. } else
  779. dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
  780. #endif /* CONFIG_P2P */
  781. if (dev_addr)
  782. os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
  783. MAC2STR(sta->addr), MAC2STR(dev_addr));
  784. else
  785. os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
  786. if (authorized) {
  787. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s", buf);
  788. if (hapd->msg_ctx_parent &&
  789. hapd->msg_ctx_parent != hapd->msg_ctx)
  790. wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
  791. AP_STA_CONNECTED "%s", buf);
  792. sta->flags |= WLAN_STA_AUTHORIZED;
  793. } else {
  794. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
  795. if (hapd->msg_ctx_parent &&
  796. hapd->msg_ctx_parent != hapd->msg_ctx)
  797. wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
  798. AP_STA_DISCONNECTED "%s", buf);
  799. sta->flags &= ~WLAN_STA_AUTHORIZED;
  800. }
  801. if (hapd->sta_authorized_cb)
  802. hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
  803. sta->addr, authorized, dev_addr);
  804. }
  805. void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
  806. const u8 *addr, u16 reason)
  807. {
  808. if (sta == NULL && addr)
  809. sta = ap_get_sta(hapd, addr);
  810. if (addr)
  811. hostapd_drv_sta_deauth(hapd, addr, reason);
  812. if (sta == NULL)
  813. return;
  814. ap_sta_set_authorized(hapd, sta, 0);
  815. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  816. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  817. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  818. wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
  819. "for " MACSTR " (%d seconds - "
  820. "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
  821. __func__, MAC2STR(sta->addr),
  822. AP_MAX_INACTIVITY_AFTER_DEAUTH);
  823. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  824. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
  825. ap_handle_timer, hapd, sta);
  826. sta->timeout_next = STA_REMOVE;
  827. sta->deauth_reason = reason;
  828. sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
  829. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  830. eloop_register_timeout(hapd->iface->drv_flags &
  831. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  832. ap_sta_deauth_cb_timeout, hapd, sta);
  833. }
  834. void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
  835. {
  836. if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
  837. wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
  838. return;
  839. }
  840. sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
  841. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  842. ap_sta_deauth_cb_timeout(hapd, sta);
  843. }
  844. void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
  845. {
  846. if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
  847. wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
  848. return;
  849. }
  850. sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
  851. eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
  852. ap_sta_disassoc_cb_timeout(hapd, sta);
  853. }
  854. int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
  855. {
  856. int res;
  857. buf[0] = '\0';
  858. res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  859. (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
  860. (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
  861. (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
  862. (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
  863. ""),
  864. (flags & WLAN_STA_SHORT_PREAMBLE ?
  865. "[SHORT_PREAMBLE]" : ""),
  866. (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
  867. (flags & WLAN_STA_WMM ? "[WMM]" : ""),
  868. (flags & WLAN_STA_MFP ? "[MFP]" : ""),
  869. (flags & WLAN_STA_WPS ? "[WPS]" : ""),
  870. (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
  871. (flags & WLAN_STA_WDS ? "[WDS]" : ""),
  872. (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
  873. (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
  874. (flags & WLAN_STA_GAS ? "[GAS]" : ""),
  875. (flags & WLAN_STA_VHT ? "[VHT]" : ""),
  876. (flags & WLAN_STA_WNM_SLEEP_MODE ?
  877. "[WNM_SLEEP_MODE]" : ""));
  878. return res;
  879. }