sta_info.c 27 KB

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