sta_info.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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 from kernel driver for "
  236. MACSTR, MAC2STR(sta->addr));
  237. /*
  238. * The driver may not support this functionality.
  239. * Anyway, try again after the next inactivity timeout,
  240. * but do not disconnect the station now.
  241. */
  242. next_time = hapd->conf->ap_max_inactivity;
  243. } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
  244. sta->flags & WLAN_STA_ASSOC) {
  245. /* station activity detected; reset timeout state */
  246. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  247. "Station " MACSTR " has been active %is ago",
  248. MAC2STR(sta->addr), inactive_sec);
  249. sta->timeout_next = STA_NULLFUNC;
  250. next_time = hapd->conf->ap_max_inactivity -
  251. inactive_sec;
  252. } else {
  253. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  254. "Station " MACSTR " has been "
  255. "inactive too long: %d sec, max allowed: %d",
  256. MAC2STR(sta->addr), inactive_sec,
  257. hapd->conf->ap_max_inactivity);
  258. if (hapd->conf->skip_inactivity_poll)
  259. sta->timeout_next = STA_DISASSOC;
  260. }
  261. }
  262. if ((sta->flags & WLAN_STA_ASSOC) &&
  263. sta->timeout_next == STA_DISASSOC &&
  264. !(sta->flags & WLAN_STA_PENDING_POLL) &&
  265. !hapd->conf->skip_inactivity_poll) {
  266. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
  267. " has ACKed data poll", MAC2STR(sta->addr));
  268. /* data nullfunc frame poll did not produce TX errors; assume
  269. * station ACKed it */
  270. sta->timeout_next = STA_NULLFUNC;
  271. next_time = hapd->conf->ap_max_inactivity;
  272. }
  273. if (next_time) {
  274. eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
  275. sta);
  276. return;
  277. }
  278. if (sta->timeout_next == STA_NULLFUNC &&
  279. (sta->flags & WLAN_STA_ASSOC)) {
  280. wpa_printf(MSG_DEBUG, " Polling STA");
  281. sta->flags |= WLAN_STA_PENDING_POLL;
  282. hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
  283. sta->flags & WLAN_STA_WMM);
  284. } else if (sta->timeout_next != STA_REMOVE) {
  285. int deauth = sta->timeout_next == STA_DEAUTH;
  286. wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
  287. "Timeout, sending %s info to STA " MACSTR,
  288. deauth ? "deauthentication" : "disassociation",
  289. MAC2STR(sta->addr));
  290. if (deauth) {
  291. hostapd_drv_sta_deauth(
  292. hapd, sta->addr,
  293. WLAN_REASON_PREV_AUTH_NOT_VALID);
  294. } else {
  295. hostapd_drv_sta_disassoc(
  296. hapd, sta->addr,
  297. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  298. }
  299. }
  300. switch (sta->timeout_next) {
  301. case STA_NULLFUNC:
  302. sta->timeout_next = STA_DISASSOC;
  303. eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
  304. hapd, sta);
  305. break;
  306. case STA_DISASSOC:
  307. ap_sta_set_authorized(hapd, sta, 0);
  308. sta->flags &= ~WLAN_STA_ASSOC;
  309. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  310. if (!sta->acct_terminate_cause)
  311. sta->acct_terminate_cause =
  312. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  313. accounting_sta_stop(hapd, sta);
  314. ieee802_1x_free_station(sta);
  315. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  316. HOSTAPD_LEVEL_INFO, "disassociated due to "
  317. "inactivity");
  318. sta->timeout_next = STA_DEAUTH;
  319. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  320. hapd, sta);
  321. mlme_disassociate_indication(
  322. hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  323. break;
  324. case STA_DEAUTH:
  325. case STA_REMOVE:
  326. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  327. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  328. "inactivity (timer DEAUTH/REMOVE)");
  329. if (!sta->acct_terminate_cause)
  330. sta->acct_terminate_cause =
  331. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  332. mlme_deauthenticate_indication(
  333. hapd, sta,
  334. WLAN_REASON_PREV_AUTH_NOT_VALID);
  335. ap_free_sta(hapd, sta);
  336. break;
  337. }
  338. }
  339. static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
  340. {
  341. struct hostapd_data *hapd = eloop_ctx;
  342. struct sta_info *sta = timeout_ctx;
  343. u8 addr[ETH_ALEN];
  344. if (!(sta->flags & WLAN_STA_AUTH))
  345. return;
  346. mlme_deauthenticate_indication(hapd, sta,
  347. WLAN_REASON_PREV_AUTH_NOT_VALID);
  348. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  349. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  350. "session timeout");
  351. sta->acct_terminate_cause =
  352. RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
  353. os_memcpy(addr, sta->addr, ETH_ALEN);
  354. ap_free_sta(hapd, sta);
  355. hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
  356. }
  357. void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
  358. u32 session_timeout)
  359. {
  360. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  361. HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
  362. "seconds", session_timeout);
  363. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  364. eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
  365. hapd, sta);
  366. }
  367. void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  368. {
  369. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  370. }
  371. struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
  372. {
  373. struct sta_info *sta;
  374. sta = ap_get_sta(hapd, addr);
  375. if (sta)
  376. return sta;
  377. wpa_printf(MSG_DEBUG, " New STA");
  378. if (hapd->num_sta >= hapd->conf->max_num_sta) {
  379. /* FIX: might try to remove some old STAs first? */
  380. wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
  381. hapd->num_sta, hapd->conf->max_num_sta);
  382. return NULL;
  383. }
  384. sta = os_zalloc(sizeof(struct sta_info));
  385. if (sta == NULL) {
  386. wpa_printf(MSG_ERROR, "malloc failed");
  387. return NULL;
  388. }
  389. sta->acct_interim_interval = hapd->conf->acct_interim_interval;
  390. /* initialize STA info data */
  391. eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
  392. ap_handle_timer, hapd, sta);
  393. os_memcpy(sta->addr, addr, ETH_ALEN);
  394. sta->next = hapd->sta_list;
  395. hapd->sta_list = sta;
  396. hapd->num_sta++;
  397. ap_sta_hash_add(hapd, sta);
  398. sta->ssid = &hapd->conf->ssid;
  399. ap_sta_remove_in_other_bss(hapd, sta);
  400. return sta;
  401. }
  402. static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
  403. {
  404. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  405. wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
  406. MAC2STR(sta->addr));
  407. if (hostapd_drv_sta_remove(hapd, sta->addr) &&
  408. sta->flags & WLAN_STA_ASSOC) {
  409. wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
  410. " from kernel driver.", MAC2STR(sta->addr));
  411. return -1;
  412. }
  413. return 0;
  414. }
  415. static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
  416. struct sta_info *sta)
  417. {
  418. struct hostapd_iface *iface = hapd->iface;
  419. size_t i;
  420. for (i = 0; i < iface->num_bss; i++) {
  421. struct hostapd_data *bss = iface->bss[i];
  422. struct sta_info *sta2;
  423. /* bss should always be set during operation, but it may be
  424. * NULL during reconfiguration. Assume the STA is not
  425. * associated to another BSS in that case to avoid NULL pointer
  426. * dereferences. */
  427. if (bss == hapd || bss == NULL)
  428. continue;
  429. sta2 = ap_get_sta(bss, sta->addr);
  430. if (!sta2)
  431. continue;
  432. ap_sta_disconnect(bss, sta2, sta2->addr,
  433. WLAN_REASON_PREV_AUTH_NOT_VALID);
  434. }
  435. }
  436. static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
  437. {
  438. struct hostapd_data *hapd = eloop_ctx;
  439. struct sta_info *sta = timeout_ctx;
  440. ap_sta_remove(hapd, sta);
  441. mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
  442. }
  443. void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
  444. u16 reason)
  445. {
  446. wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
  447. hapd->conf->iface, MAC2STR(sta->addr));
  448. sta->flags &= ~WLAN_STA_ASSOC;
  449. ap_sta_set_authorized(hapd, sta, 0);
  450. sta->timeout_next = STA_DEAUTH;
  451. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  452. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
  453. ap_handle_timer, hapd, sta);
  454. accounting_sta_stop(hapd, sta);
  455. ieee802_1x_free_station(sta);
  456. sta->disassoc_reason = reason;
  457. sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
  458. eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
  459. eloop_register_timeout(hapd->iface->drv_flags &
  460. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  461. ap_sta_disassoc_cb_timeout, hapd, sta);
  462. }
  463. static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
  464. {
  465. struct hostapd_data *hapd = eloop_ctx;
  466. struct sta_info *sta = timeout_ctx;
  467. ap_sta_remove(hapd, sta);
  468. mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
  469. }
  470. void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
  471. u16 reason)
  472. {
  473. wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
  474. hapd->conf->iface, MAC2STR(sta->addr));
  475. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  476. ap_sta_set_authorized(hapd, sta, 0);
  477. sta->timeout_next = STA_REMOVE;
  478. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  479. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
  480. ap_handle_timer, hapd, sta);
  481. accounting_sta_stop(hapd, sta);
  482. ieee802_1x_free_station(sta);
  483. sta->deauth_reason = reason;
  484. sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
  485. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  486. eloop_register_timeout(hapd->iface->drv_flags &
  487. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  488. ap_sta_deauth_cb_timeout, hapd, sta);
  489. }
  490. int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
  491. int old_vlanid)
  492. {
  493. #ifndef CONFIG_NO_VLAN
  494. const char *iface;
  495. struct hostapd_vlan *vlan = NULL;
  496. int ret;
  497. /*
  498. * Do not proceed furthur if the vlan id remains same. We do not want
  499. * duplicate dynamic vlan entries.
  500. */
  501. if (sta->vlan_id == old_vlanid)
  502. return 0;
  503. /*
  504. * During 1x reauth, if the vlan id changes, then remove the old id and
  505. * proceed furthur to add the new one.
  506. */
  507. if (old_vlanid > 0)
  508. vlan_remove_dynamic(hapd, old_vlanid);
  509. iface = hapd->conf->iface;
  510. if (sta->ssid->vlan[0])
  511. iface = sta->ssid->vlan;
  512. if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  513. sta->vlan_id = 0;
  514. else if (sta->vlan_id > 0) {
  515. vlan = hapd->conf->vlan;
  516. while (vlan) {
  517. if (vlan->vlan_id == sta->vlan_id ||
  518. vlan->vlan_id == VLAN_ID_WILDCARD) {
  519. iface = vlan->ifname;
  520. break;
  521. }
  522. vlan = vlan->next;
  523. }
  524. }
  525. if (sta->vlan_id > 0 && vlan == NULL) {
  526. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  527. HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
  528. "binding station to (vlan_id=%d)",
  529. sta->vlan_id);
  530. return -1;
  531. } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
  532. vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
  533. if (vlan == NULL) {
  534. hostapd_logger(hapd, sta->addr,
  535. HOSTAPD_MODULE_IEEE80211,
  536. HOSTAPD_LEVEL_DEBUG, "could not add "
  537. "dynamic VLAN interface for vlan_id=%d",
  538. sta->vlan_id);
  539. return -1;
  540. }
  541. iface = vlan->ifname;
  542. if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
  543. hostapd_logger(hapd, sta->addr,
  544. HOSTAPD_MODULE_IEEE80211,
  545. HOSTAPD_LEVEL_DEBUG, "could not "
  546. "configure encryption for dynamic VLAN "
  547. "interface for vlan_id=%d",
  548. sta->vlan_id);
  549. }
  550. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  551. HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
  552. "interface '%s'", iface);
  553. } else if (vlan && vlan->vlan_id == sta->vlan_id) {
  554. if (sta->vlan_id > 0) {
  555. vlan->dynamic_vlan++;
  556. hostapd_logger(hapd, sta->addr,
  557. HOSTAPD_MODULE_IEEE80211,
  558. HOSTAPD_LEVEL_DEBUG, "updated existing "
  559. "dynamic VLAN interface '%s'", iface);
  560. }
  561. /*
  562. * Update encryption configuration for statically generated
  563. * VLAN interface. This is only used for static WEP
  564. * configuration for the case where hostapd did not yet know
  565. * which keys are to be used when the interface was added.
  566. */
  567. if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
  568. hostapd_logger(hapd, sta->addr,
  569. HOSTAPD_MODULE_IEEE80211,
  570. HOSTAPD_LEVEL_DEBUG, "could not "
  571. "configure encryption for VLAN "
  572. "interface for vlan_id=%d",
  573. sta->vlan_id);
  574. }
  575. }
  576. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  577. HOSTAPD_LEVEL_DEBUG, "binding station to interface "
  578. "'%s'", iface);
  579. if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
  580. wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
  581. ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
  582. if (ret < 0) {
  583. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  584. HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
  585. "entry to vlan_id=%d", sta->vlan_id);
  586. }
  587. return ret;
  588. #else /* CONFIG_NO_VLAN */
  589. return 0;
  590. #endif /* CONFIG_NO_VLAN */
  591. }
  592. #ifdef CONFIG_IEEE80211W
  593. int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  594. {
  595. u32 tu;
  596. struct os_time now, passed;
  597. os_get_time(&now);
  598. os_time_sub(&now, &sta->sa_query_start, &passed);
  599. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  600. if (hapd->conf->assoc_sa_query_max_timeout < tu) {
  601. hostapd_logger(hapd, sta->addr,
  602. HOSTAPD_MODULE_IEEE80211,
  603. HOSTAPD_LEVEL_DEBUG,
  604. "association SA Query timed out");
  605. sta->sa_query_timed_out = 1;
  606. os_free(sta->sa_query_trans_id);
  607. sta->sa_query_trans_id = NULL;
  608. sta->sa_query_count = 0;
  609. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  610. return 1;
  611. }
  612. return 0;
  613. }
  614. static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
  615. {
  616. struct hostapd_data *hapd = eloop_ctx;
  617. struct sta_info *sta = timeout_ctx;
  618. unsigned int timeout, sec, usec;
  619. u8 *trans_id, *nbuf;
  620. if (sta->sa_query_count > 0 &&
  621. ap_check_sa_query_timeout(hapd, sta))
  622. return;
  623. nbuf = os_realloc(sta->sa_query_trans_id,
  624. (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
  625. if (nbuf == NULL)
  626. return;
  627. if (sta->sa_query_count == 0) {
  628. /* Starting a new SA Query procedure */
  629. os_get_time(&sta->sa_query_start);
  630. }
  631. trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
  632. sta->sa_query_trans_id = nbuf;
  633. sta->sa_query_count++;
  634. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  635. timeout = hapd->conf->assoc_sa_query_retry_timeout;
  636. sec = ((timeout / 1000) * 1024) / 1000;
  637. usec = (timeout % 1000) * 1024;
  638. eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
  639. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  640. HOSTAPD_LEVEL_DEBUG,
  641. "association SA Query attempt %d", sta->sa_query_count);
  642. #ifdef NEED_AP_MLME
  643. ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
  644. #endif /* NEED_AP_MLME */
  645. }
  646. void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  647. {
  648. ap_sa_query_timer(hapd, sta);
  649. }
  650. void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  651. {
  652. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  653. os_free(sta->sa_query_trans_id);
  654. sta->sa_query_trans_id = NULL;
  655. sta->sa_query_count = 0;
  656. }
  657. #endif /* CONFIG_IEEE80211W */
  658. void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
  659. int authorized)
  660. {
  661. const u8 *dev_addr = NULL;
  662. if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
  663. return;
  664. #ifdef CONFIG_P2P
  665. dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
  666. #endif /* CONFIG_P2P */
  667. if (authorized) {
  668. if (dev_addr)
  669. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
  670. MACSTR " p2p_dev_addr=" MACSTR,
  671. MAC2STR(sta->addr), MAC2STR(dev_addr));
  672. else
  673. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
  674. MACSTR, MAC2STR(sta->addr));
  675. if (hapd->msg_ctx_parent &&
  676. hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
  677. wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
  678. AP_STA_CONNECTED MACSTR " p2p_dev_addr="
  679. MACSTR,
  680. MAC2STR(sta->addr), MAC2STR(dev_addr));
  681. else if (hapd->msg_ctx_parent &&
  682. hapd->msg_ctx_parent != hapd->msg_ctx)
  683. wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
  684. AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
  685. sta->flags |= WLAN_STA_AUTHORIZED;
  686. } else {
  687. if (dev_addr)
  688. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
  689. MACSTR " p2p_dev_addr=" MACSTR,
  690. MAC2STR(sta->addr), MAC2STR(dev_addr));
  691. else
  692. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
  693. MACSTR, MAC2STR(sta->addr));
  694. if (hapd->msg_ctx_parent &&
  695. hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
  696. wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
  697. AP_STA_DISCONNECTED MACSTR " p2p_dev_addr="
  698. MACSTR, MAC2STR(sta->addr), MAC2STR(dev_addr));
  699. else if (hapd->msg_ctx_parent &&
  700. hapd->msg_ctx_parent != hapd->msg_ctx)
  701. wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
  702. AP_STA_DISCONNECTED MACSTR,
  703. MAC2STR(sta->addr));
  704. sta->flags &= ~WLAN_STA_AUTHORIZED;
  705. }
  706. if (hapd->sta_authorized_cb)
  707. hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
  708. sta->addr, authorized, dev_addr);
  709. }
  710. void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
  711. const u8 *addr, u16 reason)
  712. {
  713. if (sta == NULL && addr)
  714. sta = ap_get_sta(hapd, addr);
  715. if (addr)
  716. hostapd_drv_sta_deauth(hapd, addr, reason);
  717. if (sta == NULL)
  718. return;
  719. ap_sta_set_authorized(hapd, sta, 0);
  720. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  721. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  722. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  723. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  724. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
  725. ap_handle_timer, hapd, sta);
  726. sta->timeout_next = STA_REMOVE;
  727. sta->deauth_reason = reason;
  728. sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
  729. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  730. eloop_register_timeout(hapd->iface->drv_flags &
  731. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  732. ap_sta_deauth_cb_timeout, hapd, sta);
  733. }
  734. void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
  735. {
  736. if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
  737. wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
  738. return;
  739. }
  740. sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
  741. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  742. ap_sta_deauth_cb_timeout(hapd, sta);
  743. }
  744. void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
  745. {
  746. if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
  747. wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
  748. return;
  749. }
  750. sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
  751. eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
  752. ap_sta_disassoc_cb_timeout(hapd, sta);
  753. }