sta_info.c 23 KB

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