sta_info.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * hostapd / Station table
  3. * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2007-2008, Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #include "hostapd.h"
  17. #include "sta_info.h"
  18. #include "eloop.h"
  19. #include "accounting.h"
  20. #include "ieee802_1x.h"
  21. #include "ieee802_11.h"
  22. #include "radius/radius.h"
  23. #include "wpa.h"
  24. #include "preauth.h"
  25. #include "radius/radius_client.h"
  26. #include "driver_i.h"
  27. #include "beacon.h"
  28. #include "hw_features.h"
  29. #include "mlme.h"
  30. #include "vlan_init.h"
  31. static int ap_sta_in_other_bss(struct hostapd_data *hapd,
  32. struct sta_info *sta, u32 flags);
  33. static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
  34. #ifdef CONFIG_IEEE80211W
  35. static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
  36. #endif /* CONFIG_IEEE80211W */
  37. int ap_for_each_sta(struct hostapd_data *hapd,
  38. int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
  39. void *ctx),
  40. void *ctx)
  41. {
  42. struct sta_info *sta;
  43. for (sta = hapd->sta_list; sta; sta = sta->next) {
  44. if (cb(hapd, sta, ctx))
  45. return 1;
  46. }
  47. return 0;
  48. }
  49. struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
  50. {
  51. struct sta_info *s;
  52. s = hapd->sta_hash[STA_HASH(sta)];
  53. while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
  54. s = s->hnext;
  55. return s;
  56. }
  57. static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
  58. {
  59. struct sta_info *tmp;
  60. if (hapd->sta_list == sta) {
  61. hapd->sta_list = sta->next;
  62. return;
  63. }
  64. tmp = hapd->sta_list;
  65. while (tmp != NULL && tmp->next != sta)
  66. tmp = tmp->next;
  67. if (tmp == NULL) {
  68. wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
  69. "list.", MAC2STR(sta->addr));
  70. } else
  71. tmp->next = sta->next;
  72. }
  73. void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
  74. {
  75. sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
  76. hapd->sta_hash[STA_HASH(sta->addr)] = sta;
  77. }
  78. static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
  79. {
  80. struct sta_info *s;
  81. s = hapd->sta_hash[STA_HASH(sta->addr)];
  82. if (s == NULL) return;
  83. if (os_memcmp(s->addr, sta->addr, 6) == 0) {
  84. hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
  85. return;
  86. }
  87. while (s->hnext != NULL &&
  88. os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
  89. s = s->hnext;
  90. if (s->hnext != NULL)
  91. s->hnext = s->hnext->hnext;
  92. else
  93. wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
  94. " from hash table", MAC2STR(sta->addr));
  95. }
  96. void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
  97. {
  98. int set_beacon = 0;
  99. accounting_sta_stop(hapd, sta);
  100. if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC) &&
  101. !(sta->flags & WLAN_STA_PREAUTH))
  102. hostapd_sta_remove(hapd, sta->addr);
  103. ap_sta_hash_del(hapd, sta);
  104. ap_sta_list_del(hapd, sta);
  105. if (sta->aid > 0)
  106. hapd->sta_aid[sta->aid - 1] = NULL;
  107. hapd->num_sta--;
  108. if (sta->nonerp_set) {
  109. sta->nonerp_set = 0;
  110. hapd->iface->num_sta_non_erp--;
  111. if (hapd->iface->num_sta_non_erp == 0)
  112. set_beacon++;
  113. }
  114. if (sta->no_short_slot_time_set) {
  115. sta->no_short_slot_time_set = 0;
  116. hapd->iface->num_sta_no_short_slot_time--;
  117. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  118. && hapd->iface->num_sta_no_short_slot_time == 0)
  119. set_beacon++;
  120. }
  121. if (sta->no_short_preamble_set) {
  122. sta->no_short_preamble_set = 0;
  123. hapd->iface->num_sta_no_short_preamble--;
  124. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  125. && hapd->iface->num_sta_no_short_preamble == 0)
  126. set_beacon++;
  127. }
  128. #ifdef CONFIG_IEEE80211N
  129. if (sta->no_ht_gf_set) {
  130. sta->no_ht_gf_set = 0;
  131. hapd->iface->num_sta_ht_no_gf--;
  132. }
  133. if (sta->no_ht_set) {
  134. sta->no_ht_set = 0;
  135. hapd->iface->num_sta_no_ht--;
  136. }
  137. if (sta->ht_20mhz_set) {
  138. sta->ht_20mhz_set = 0;
  139. hapd->iface->num_sta_ht_20mhz--;
  140. }
  141. if (hostapd_ht_operation_update(hapd->iface) > 0)
  142. set_beacon++;
  143. #endif /* CONFIG_IEEE80211N */
  144. if (set_beacon)
  145. ieee802_11_set_beacons(hapd->iface);
  146. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  147. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  148. ieee802_1x_free_station(sta);
  149. wpa_auth_sta_deinit(sta->wpa_sm);
  150. rsn_preauth_free_station(hapd, sta);
  151. radius_client_flush_auth(hapd->radius, sta->addr);
  152. os_free(sta->last_assoc_req);
  153. os_free(sta->challenge);
  154. #ifdef CONFIG_IEEE80211W
  155. os_free(sta->sa_query_trans_id);
  156. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  157. #endif /* CONFIG_IEEE80211W */
  158. wpabuf_free(sta->wps_ie);
  159. os_free(sta);
  160. }
  161. void hostapd_free_stas(struct hostapd_data *hapd)
  162. {
  163. struct sta_info *sta, *prev;
  164. sta = hapd->sta_list;
  165. while (sta) {
  166. prev = sta;
  167. if (sta->flags & WLAN_STA_AUTH) {
  168. mlme_deauthenticate_indication(
  169. hapd, sta, WLAN_REASON_UNSPECIFIED);
  170. }
  171. sta = sta->next;
  172. wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
  173. MAC2STR(prev->addr));
  174. ap_free_sta(hapd, prev);
  175. }
  176. }
  177. /**
  178. * ap_handle_timer - Per STA timer handler
  179. * @eloop_ctx: struct hostapd_data *
  180. * @timeout_ctx: struct sta_info *
  181. *
  182. * This function is called to check station activity and to remove inactive
  183. * stations.
  184. */
  185. void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
  186. {
  187. struct hostapd_data *hapd = eloop_ctx;
  188. struct sta_info *sta = timeout_ctx;
  189. unsigned long next_time = 0;
  190. if (sta->timeout_next == STA_REMOVE) {
  191. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  192. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  193. "local deauth request");
  194. ap_free_sta(hapd, sta);
  195. return;
  196. }
  197. if ((sta->flags & WLAN_STA_ASSOC) &&
  198. (sta->timeout_next == STA_NULLFUNC ||
  199. sta->timeout_next == STA_DISASSOC)) {
  200. int inactive_sec;
  201. wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
  202. MAC2STR(sta->addr));
  203. inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
  204. if (inactive_sec == -1) {
  205. wpa_printf(MSG_DEBUG, "Could not get station info "
  206. "from kernel driver for " MACSTR ".",
  207. MAC2STR(sta->addr));
  208. } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
  209. sta->flags & WLAN_STA_ASSOC) {
  210. /* station activity detected; reset timeout state */
  211. wpa_printf(MSG_DEBUG, " Station has been active");
  212. sta->timeout_next = STA_NULLFUNC;
  213. next_time = hapd->conf->ap_max_inactivity -
  214. inactive_sec;
  215. }
  216. }
  217. if ((sta->flags & WLAN_STA_ASSOC) &&
  218. sta->timeout_next == STA_DISASSOC &&
  219. !(sta->flags & WLAN_STA_PENDING_POLL)) {
  220. wpa_printf(MSG_DEBUG, " Station has ACKed data poll");
  221. /* data nullfunc frame poll did not produce TX errors; assume
  222. * station ACKed it */
  223. sta->timeout_next = STA_NULLFUNC;
  224. next_time = hapd->conf->ap_max_inactivity;
  225. }
  226. if (next_time) {
  227. eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
  228. sta);
  229. return;
  230. }
  231. if (sta->timeout_next == STA_NULLFUNC &&
  232. (sta->flags & WLAN_STA_ASSOC)) {
  233. /* send data frame to poll STA and check whether this frame
  234. * is ACKed */
  235. struct ieee80211_hdr hdr;
  236. wpa_printf(MSG_DEBUG, " Polling STA with data frame");
  237. sta->flags |= WLAN_STA_PENDING_POLL;
  238. #ifndef CONFIG_NATIVE_WINDOWS
  239. os_memset(&hdr, 0, sizeof(hdr));
  240. if (hapd->driver &&
  241. os_strcmp(hapd->driver->name, "hostap") == 0) {
  242. /*
  243. * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
  244. * but it is apparently not retried so TX Exc events
  245. * are not received for it.
  246. */
  247. hdr.frame_control =
  248. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  249. WLAN_FC_STYPE_DATA);
  250. } else {
  251. hdr.frame_control =
  252. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  253. WLAN_FC_STYPE_NULLFUNC);
  254. }
  255. hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
  256. os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
  257. os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
  258. ETH_ALEN);
  259. os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
  260. if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr), 0) < 0)
  261. perror("ap_handle_timer: send");
  262. #endif /* CONFIG_NATIVE_WINDOWS */
  263. } else if (sta->timeout_next != STA_REMOVE) {
  264. int deauth = sta->timeout_next == STA_DEAUTH;
  265. wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
  266. deauth ? "deauthentication" : "disassociation",
  267. MAC2STR(sta->addr));
  268. if (deauth) {
  269. hostapd_sta_deauth(hapd, sta->addr,
  270. WLAN_REASON_PREV_AUTH_NOT_VALID);
  271. } else {
  272. hostapd_sta_disassoc(
  273. hapd, sta->addr,
  274. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  275. }
  276. }
  277. switch (sta->timeout_next) {
  278. case STA_NULLFUNC:
  279. sta->timeout_next = STA_DISASSOC;
  280. eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
  281. hapd, sta);
  282. break;
  283. case STA_DISASSOC:
  284. sta->flags &= ~WLAN_STA_ASSOC;
  285. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  286. if (!sta->acct_terminate_cause)
  287. sta->acct_terminate_cause =
  288. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  289. accounting_sta_stop(hapd, sta);
  290. ieee802_1x_free_station(sta);
  291. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  292. HOSTAPD_LEVEL_INFO, "disassociated due to "
  293. "inactivity");
  294. sta->timeout_next = STA_DEAUTH;
  295. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  296. hapd, sta);
  297. mlme_disassociate_indication(
  298. hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  299. break;
  300. case STA_DEAUTH:
  301. case STA_REMOVE:
  302. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  303. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  304. "inactivity");
  305. if (!sta->acct_terminate_cause)
  306. sta->acct_terminate_cause =
  307. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  308. mlme_deauthenticate_indication(
  309. hapd, sta,
  310. WLAN_REASON_PREV_AUTH_NOT_VALID);
  311. ap_free_sta(hapd, sta);
  312. break;
  313. }
  314. }
  315. static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
  316. {
  317. struct hostapd_data *hapd = eloop_ctx;
  318. struct sta_info *sta = timeout_ctx;
  319. u8 addr[ETH_ALEN];
  320. if (!(sta->flags & WLAN_STA_AUTH))
  321. return;
  322. mlme_deauthenticate_indication(hapd, sta,
  323. WLAN_REASON_PREV_AUTH_NOT_VALID);
  324. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  325. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  326. "session timeout");
  327. sta->acct_terminate_cause =
  328. RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
  329. os_memcpy(addr, sta->addr, ETH_ALEN);
  330. ap_free_sta(hapd, sta);
  331. hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
  332. }
  333. void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
  334. u32 session_timeout)
  335. {
  336. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  337. HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
  338. "seconds", session_timeout);
  339. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  340. eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
  341. hapd, sta);
  342. }
  343. void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  344. {
  345. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  346. }
  347. struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
  348. {
  349. struct sta_info *sta;
  350. sta = ap_get_sta(hapd, addr);
  351. if (sta)
  352. return sta;
  353. wpa_printf(MSG_DEBUG, " New STA");
  354. if (hapd->num_sta >= hapd->conf->max_num_sta) {
  355. /* FIX: might try to remove some old STAs first? */
  356. wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
  357. hapd->num_sta, hapd->conf->max_num_sta);
  358. return NULL;
  359. }
  360. sta = os_zalloc(sizeof(struct sta_info));
  361. if (sta == NULL) {
  362. wpa_printf(MSG_ERROR, "malloc failed");
  363. return NULL;
  364. }
  365. sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
  366. /* initialize STA info data */
  367. eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
  368. ap_handle_timer, hapd, sta);
  369. os_memcpy(sta->addr, addr, ETH_ALEN);
  370. sta->next = hapd->sta_list;
  371. hapd->sta_list = sta;
  372. hapd->num_sta++;
  373. ap_sta_hash_add(hapd, sta);
  374. sta->ssid = &hapd->conf->ssid;
  375. return sta;
  376. }
  377. static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
  378. {
  379. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  380. wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
  381. MAC2STR(sta->addr));
  382. if (hostapd_sta_remove(hapd, sta->addr) &&
  383. sta->flags & WLAN_STA_ASSOC) {
  384. wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
  385. " from kernel driver.", MAC2STR(sta->addr));
  386. return -1;
  387. }
  388. return 0;
  389. }
  390. static int ap_sta_in_other_bss(struct hostapd_data *hapd,
  391. struct sta_info *sta, u32 flags)
  392. {
  393. struct hostapd_iface *iface = hapd->iface;
  394. size_t i;
  395. for (i = 0; i < iface->num_bss; i++) {
  396. struct hostapd_data *bss = iface->bss[i];
  397. struct sta_info *sta2;
  398. /* bss should always be set during operation, but it may be
  399. * NULL during reconfiguration. Assume the STA is not
  400. * associated to another BSS in that case to avoid NULL pointer
  401. * dereferences. */
  402. if (bss == hapd || bss == NULL)
  403. continue;
  404. sta2 = ap_get_sta(bss, sta->addr);
  405. if (sta2 && ((sta2->flags & flags) == flags))
  406. return 1;
  407. }
  408. return 0;
  409. }
  410. void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
  411. u16 reason)
  412. {
  413. wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
  414. hapd->conf->iface, MAC2STR(sta->addr));
  415. sta->flags &= ~WLAN_STA_ASSOC;
  416. if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
  417. ap_sta_remove(hapd, sta);
  418. sta->timeout_next = STA_DEAUTH;
  419. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  420. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
  421. ap_handle_timer, hapd, sta);
  422. accounting_sta_stop(hapd, sta);
  423. ieee802_1x_free_station(sta);
  424. mlme_disassociate_indication(hapd, sta, reason);
  425. }
  426. void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
  427. u16 reason)
  428. {
  429. wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
  430. hapd->conf->iface, MAC2STR(sta->addr));
  431. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  432. if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
  433. ap_sta_remove(hapd, sta);
  434. sta->timeout_next = STA_REMOVE;
  435. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  436. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
  437. ap_handle_timer, hapd, sta);
  438. accounting_sta_stop(hapd, sta);
  439. ieee802_1x_free_station(sta);
  440. mlme_deauthenticate_indication(hapd, sta, reason);
  441. }
  442. int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
  443. int old_vlanid)
  444. {
  445. #ifndef CONFIG_NO_VLAN
  446. const char *iface;
  447. struct hostapd_vlan *vlan = NULL;
  448. /*
  449. * Do not proceed furthur if the vlan id remains same. We do not want
  450. * duplicate dynamic vlan entries.
  451. */
  452. if (sta->vlan_id == old_vlanid)
  453. return 0;
  454. /*
  455. * During 1x reauth, if the vlan id changes, then remove the old id and
  456. * proceed furthur to add the new one.
  457. */
  458. if (old_vlanid > 0)
  459. vlan_remove_dynamic(hapd, old_vlanid);
  460. iface = hapd->conf->iface;
  461. if (sta->ssid->vlan[0])
  462. iface = sta->ssid->vlan;
  463. if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  464. sta->vlan_id = 0;
  465. else if (sta->vlan_id > 0) {
  466. vlan = hapd->conf->vlan;
  467. while (vlan) {
  468. if (vlan->vlan_id == sta->vlan_id ||
  469. vlan->vlan_id == VLAN_ID_WILDCARD) {
  470. iface = vlan->ifname;
  471. break;
  472. }
  473. vlan = vlan->next;
  474. }
  475. }
  476. if (sta->vlan_id > 0 && vlan == NULL) {
  477. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  478. HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
  479. "binding station to (vlan_id=%d)",
  480. sta->vlan_id);
  481. return -1;
  482. } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
  483. vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
  484. if (vlan == NULL) {
  485. hostapd_logger(hapd, sta->addr,
  486. HOSTAPD_MODULE_IEEE80211,
  487. HOSTAPD_LEVEL_DEBUG, "could not add "
  488. "dynamic VLAN interface for vlan_id=%d",
  489. sta->vlan_id);
  490. return -1;
  491. }
  492. iface = vlan->ifname;
  493. if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
  494. hostapd_logger(hapd, sta->addr,
  495. HOSTAPD_MODULE_IEEE80211,
  496. HOSTAPD_LEVEL_DEBUG, "could not "
  497. "configure encryption for dynamic VLAN "
  498. "interface for vlan_id=%d",
  499. sta->vlan_id);
  500. }
  501. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  502. HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
  503. "interface '%s'", iface);
  504. } else if (vlan && vlan->vlan_id == sta->vlan_id) {
  505. if (sta->vlan_id > 0) {
  506. vlan->dynamic_vlan++;
  507. hostapd_logger(hapd, sta->addr,
  508. HOSTAPD_MODULE_IEEE80211,
  509. HOSTAPD_LEVEL_DEBUG, "updated existing "
  510. "dynamic VLAN interface '%s'", iface);
  511. }
  512. /*
  513. * Update encryption configuration for statically generated
  514. * VLAN interface. This is only used for static WEP
  515. * configuration for the case where hostapd did not yet know
  516. * which keys are to be used when the interface was added.
  517. */
  518. if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
  519. hostapd_logger(hapd, sta->addr,
  520. HOSTAPD_MODULE_IEEE80211,
  521. HOSTAPD_LEVEL_DEBUG, "could not "
  522. "configure encryption for VLAN "
  523. "interface for vlan_id=%d",
  524. sta->vlan_id);
  525. }
  526. }
  527. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  528. HOSTAPD_LEVEL_DEBUG, "binding station to interface "
  529. "'%s'", iface);
  530. if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
  531. wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
  532. return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
  533. #else /* CONFIG_NO_VLAN */
  534. return 0;
  535. #endif /* CONFIG_NO_VLAN */
  536. }
  537. #ifdef CONFIG_IEEE80211W
  538. int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  539. {
  540. u32 tu;
  541. struct os_time now, passed;
  542. os_get_time(&now);
  543. os_time_sub(&now, &sta->sa_query_start, &passed);
  544. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  545. if (hapd->conf->assoc_sa_query_max_timeout < tu) {
  546. hostapd_logger(hapd, sta->addr,
  547. HOSTAPD_MODULE_IEEE80211,
  548. HOSTAPD_LEVEL_DEBUG,
  549. "association SA Query timed out");
  550. sta->sa_query_timed_out = 1;
  551. os_free(sta->sa_query_trans_id);
  552. sta->sa_query_trans_id = NULL;
  553. sta->sa_query_count = 0;
  554. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  555. return 1;
  556. }
  557. return 0;
  558. }
  559. static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
  560. {
  561. struct hostapd_data *hapd = eloop_ctx;
  562. struct sta_info *sta = timeout_ctx;
  563. unsigned int timeout, sec, usec;
  564. u8 *trans_id, *nbuf;
  565. if (sta->sa_query_count > 0 &&
  566. ap_check_sa_query_timeout(hapd, sta))
  567. return;
  568. nbuf = os_realloc(sta->sa_query_trans_id,
  569. (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
  570. if (nbuf == NULL)
  571. return;
  572. if (sta->sa_query_count == 0) {
  573. /* Starting a new SA Query procedure */
  574. os_get_time(&sta->sa_query_start);
  575. }
  576. trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
  577. sta->sa_query_trans_id = nbuf;
  578. sta->sa_query_count++;
  579. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  580. timeout = hapd->conf->assoc_sa_query_retry_timeout;
  581. sec = ((timeout / 1000) * 1024) / 1000;
  582. usec = (timeout % 1000) * 1024;
  583. eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
  584. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  585. HOSTAPD_LEVEL_DEBUG,
  586. "association SA Query attempt %d", sta->sa_query_count);
  587. ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
  588. }
  589. void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  590. {
  591. ap_sa_query_timer(hapd, sta);
  592. }
  593. void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  594. {
  595. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  596. os_free(sta->sa_query_trans_id);
  597. sta->sa_query_trans_id = NULL;
  598. sta->sa_query_count = 0;
  599. }
  600. #endif /* CONFIG_IEEE80211W */