sta_info.c 24 KB

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