sta_info.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. /* FIX: WLAN_FC_STYPE_NULLFUNC would be more appropriate, but
  240. * it is apparently not retried so TX Exc events are not
  241. * received for it */
  242. os_memset(&hdr, 0, sizeof(hdr));
  243. hdr.frame_control =
  244. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  245. hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
  246. os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
  247. os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
  248. ETH_ALEN);
  249. os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
  250. if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr), 0) < 0)
  251. perror("ap_handle_timer: send");
  252. #endif /* CONFIG_NATIVE_WINDOWS */
  253. } else if (sta->timeout_next != STA_REMOVE) {
  254. int deauth = sta->timeout_next == STA_DEAUTH;
  255. wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
  256. deauth ? "deauthentication" : "disassociation",
  257. MAC2STR(sta->addr));
  258. if (deauth) {
  259. hostapd_sta_deauth(hapd, sta->addr,
  260. WLAN_REASON_PREV_AUTH_NOT_VALID);
  261. } else {
  262. hostapd_sta_disassoc(
  263. hapd, sta->addr,
  264. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  265. }
  266. }
  267. switch (sta->timeout_next) {
  268. case STA_NULLFUNC:
  269. sta->timeout_next = STA_DISASSOC;
  270. eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
  271. hapd, sta);
  272. break;
  273. case STA_DISASSOC:
  274. sta->flags &= ~WLAN_STA_ASSOC;
  275. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  276. if (!sta->acct_terminate_cause)
  277. sta->acct_terminate_cause =
  278. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  279. accounting_sta_stop(hapd, sta);
  280. ieee802_1x_free_station(sta);
  281. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  282. HOSTAPD_LEVEL_INFO, "disassociated due to "
  283. "inactivity");
  284. sta->timeout_next = STA_DEAUTH;
  285. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  286. hapd, sta);
  287. mlme_disassociate_indication(
  288. hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  289. break;
  290. case STA_DEAUTH:
  291. case STA_REMOVE:
  292. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  293. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  294. "inactivity");
  295. if (!sta->acct_terminate_cause)
  296. sta->acct_terminate_cause =
  297. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  298. mlme_deauthenticate_indication(
  299. hapd, sta,
  300. WLAN_REASON_PREV_AUTH_NOT_VALID);
  301. ap_free_sta(hapd, sta);
  302. break;
  303. }
  304. }
  305. static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
  306. {
  307. struct hostapd_data *hapd = eloop_ctx;
  308. struct sta_info *sta = timeout_ctx;
  309. u8 addr[ETH_ALEN];
  310. if (!(sta->flags & WLAN_STA_AUTH))
  311. return;
  312. mlme_deauthenticate_indication(hapd, sta,
  313. WLAN_REASON_PREV_AUTH_NOT_VALID);
  314. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  315. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  316. "session timeout");
  317. sta->acct_terminate_cause =
  318. RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
  319. os_memcpy(addr, sta->addr, ETH_ALEN);
  320. ap_free_sta(hapd, sta);
  321. hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
  322. }
  323. void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
  324. u32 session_timeout)
  325. {
  326. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  327. HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
  328. "seconds", session_timeout);
  329. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  330. eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
  331. hapd, sta);
  332. }
  333. void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  334. {
  335. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  336. }
  337. struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
  338. {
  339. struct sta_info *sta;
  340. sta = ap_get_sta(hapd, addr);
  341. if (sta)
  342. return sta;
  343. wpa_printf(MSG_DEBUG, " New STA");
  344. if (hapd->num_sta >= hapd->conf->max_num_sta) {
  345. /* FIX: might try to remove some old STAs first? */
  346. wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
  347. hapd->num_sta, hapd->conf->max_num_sta);
  348. return NULL;
  349. }
  350. sta = os_zalloc(sizeof(struct sta_info));
  351. if (sta == NULL) {
  352. wpa_printf(MSG_ERROR, "malloc failed");
  353. return NULL;
  354. }
  355. sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
  356. /* initialize STA info data */
  357. eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
  358. ap_handle_timer, hapd, sta);
  359. os_memcpy(sta->addr, addr, ETH_ALEN);
  360. sta->next = hapd->sta_list;
  361. hapd->sta_list = sta;
  362. hapd->num_sta++;
  363. ap_sta_hash_add(hapd, sta);
  364. sta->ssid = &hapd->conf->ssid;
  365. return sta;
  366. }
  367. static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
  368. {
  369. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  370. wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
  371. MAC2STR(sta->addr));
  372. if (hostapd_sta_remove(hapd, sta->addr) &&
  373. sta->flags & WLAN_STA_ASSOC) {
  374. wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
  375. " from kernel driver.", MAC2STR(sta->addr));
  376. return -1;
  377. }
  378. return 0;
  379. }
  380. static int ap_sta_in_other_bss(struct hostapd_data *hapd,
  381. struct sta_info *sta, u32 flags)
  382. {
  383. struct hostapd_iface *iface = hapd->iface;
  384. size_t i;
  385. for (i = 0; i < iface->num_bss; i++) {
  386. struct hostapd_data *bss = iface->bss[i];
  387. struct sta_info *sta2;
  388. /* bss should always be set during operation, but it may be
  389. * NULL during reconfiguration. Assume the STA is not
  390. * associated to another BSS in that case to avoid NULL pointer
  391. * dereferences. */
  392. if (bss == hapd || bss == NULL)
  393. continue;
  394. sta2 = ap_get_sta(bss, sta->addr);
  395. if (sta2 && ((sta2->flags & flags) == flags))
  396. return 1;
  397. }
  398. return 0;
  399. }
  400. void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
  401. u16 reason)
  402. {
  403. wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
  404. hapd->conf->iface, MAC2STR(sta->addr));
  405. sta->flags &= ~WLAN_STA_ASSOC;
  406. if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
  407. ap_sta_remove(hapd, sta);
  408. sta->timeout_next = STA_DEAUTH;
  409. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  410. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
  411. ap_handle_timer, hapd, sta);
  412. accounting_sta_stop(hapd, sta);
  413. ieee802_1x_free_station(sta);
  414. mlme_disassociate_indication(hapd, sta, reason);
  415. }
  416. void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
  417. u16 reason)
  418. {
  419. wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
  420. hapd->conf->iface, MAC2STR(sta->addr));
  421. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  422. if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
  423. ap_sta_remove(hapd, sta);
  424. sta->timeout_next = STA_REMOVE;
  425. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  426. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
  427. ap_handle_timer, hapd, sta);
  428. accounting_sta_stop(hapd, sta);
  429. ieee802_1x_free_station(sta);
  430. mlme_deauthenticate_indication(hapd, sta, reason);
  431. }
  432. int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
  433. int old_vlanid)
  434. {
  435. const char *iface;
  436. struct hostapd_vlan *vlan = NULL;
  437. /*
  438. * Do not proceed furthur if the vlan id remains same. We do not want
  439. * duplicate dynamic vlan entries.
  440. */
  441. if (sta->vlan_id == old_vlanid)
  442. return 0;
  443. /*
  444. * During 1x reauth, if the vlan id changes, then remove the old id and
  445. * proceed furthur to add the new one.
  446. */
  447. if (old_vlanid > 0)
  448. vlan_remove_dynamic(hapd, old_vlanid);
  449. iface = hapd->conf->iface;
  450. if (sta->ssid->vlan[0])
  451. iface = sta->ssid->vlan;
  452. if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  453. sta->vlan_id = 0;
  454. else if (sta->vlan_id > 0) {
  455. vlan = hapd->conf->vlan;
  456. while (vlan) {
  457. if (vlan->vlan_id == sta->vlan_id ||
  458. vlan->vlan_id == VLAN_ID_WILDCARD) {
  459. iface = vlan->ifname;
  460. break;
  461. }
  462. vlan = vlan->next;
  463. }
  464. }
  465. if (sta->vlan_id > 0 && vlan == NULL) {
  466. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  467. HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
  468. "binding station to (vlan_id=%d)",
  469. sta->vlan_id);
  470. return -1;
  471. } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
  472. vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
  473. if (vlan == NULL) {
  474. hostapd_logger(hapd, sta->addr,
  475. HOSTAPD_MODULE_IEEE80211,
  476. HOSTAPD_LEVEL_DEBUG, "could not add "
  477. "dynamic VLAN interface for vlan_id=%d",
  478. sta->vlan_id);
  479. return -1;
  480. }
  481. iface = vlan->ifname;
  482. if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
  483. hostapd_logger(hapd, sta->addr,
  484. HOSTAPD_MODULE_IEEE80211,
  485. HOSTAPD_LEVEL_DEBUG, "could not "
  486. "configure encryption for dynamic VLAN "
  487. "interface for vlan_id=%d",
  488. sta->vlan_id);
  489. }
  490. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  491. HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
  492. "interface '%s'", iface);
  493. } else if (vlan && vlan->vlan_id == sta->vlan_id) {
  494. if (sta->vlan_id > 0) {
  495. vlan->dynamic_vlan++;
  496. hostapd_logger(hapd, sta->addr,
  497. HOSTAPD_MODULE_IEEE80211,
  498. HOSTAPD_LEVEL_DEBUG, "updated existing "
  499. "dynamic VLAN interface '%s'", iface);
  500. }
  501. /*
  502. * Update encryption configuration for statically generated
  503. * VLAN interface. This is only used for static WEP
  504. * configuration for the case where hostapd did not yet know
  505. * which keys are to be used when the interface was added.
  506. */
  507. if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
  508. hostapd_logger(hapd, sta->addr,
  509. HOSTAPD_MODULE_IEEE80211,
  510. HOSTAPD_LEVEL_DEBUG, "could not "
  511. "configure encryption for VLAN "
  512. "interface for vlan_id=%d",
  513. sta->vlan_id);
  514. }
  515. }
  516. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  517. HOSTAPD_LEVEL_DEBUG, "binding station to interface "
  518. "'%s'", iface);
  519. if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
  520. wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
  521. return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
  522. }
  523. #ifdef CONFIG_IEEE80211W
  524. int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  525. {
  526. u32 tu;
  527. struct os_time now, passed;
  528. os_get_time(&now);
  529. os_time_sub(&now, &sta->sa_query_start, &passed);
  530. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  531. if (hapd->conf->assoc_sa_query_max_timeout < tu) {
  532. hostapd_logger(hapd, sta->addr,
  533. HOSTAPD_MODULE_IEEE80211,
  534. HOSTAPD_LEVEL_DEBUG,
  535. "association SA Query timed out");
  536. sta->sa_query_timed_out = 1;
  537. os_free(sta->sa_query_trans_id);
  538. sta->sa_query_trans_id = NULL;
  539. sta->sa_query_count = 0;
  540. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  541. return 1;
  542. }
  543. return 0;
  544. }
  545. static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
  546. {
  547. struct hostapd_data *hapd = eloop_ctx;
  548. struct sta_info *sta = timeout_ctx;
  549. unsigned int timeout, sec, usec;
  550. u8 *trans_id, *nbuf;
  551. if (sta->sa_query_count > 0 &&
  552. ap_check_sa_query_timeout(hapd, sta))
  553. return;
  554. nbuf = os_realloc(sta->sa_query_trans_id,
  555. (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
  556. if (nbuf == NULL)
  557. return;
  558. if (sta->sa_query_count == 0) {
  559. /* Starting a new SA Query procedure */
  560. os_get_time(&sta->sa_query_start);
  561. }
  562. trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
  563. sta->sa_query_trans_id = nbuf;
  564. sta->sa_query_count++;
  565. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  566. timeout = hapd->conf->assoc_sa_query_retry_timeout;
  567. sec = ((timeout / 1000) * 1024) / 1000;
  568. usec = (timeout % 1000) * 1024;
  569. eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
  570. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  571. HOSTAPD_LEVEL_DEBUG,
  572. "association SA Query attempt %d", sta->sa_query_count);
  573. ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
  574. }
  575. void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  576. {
  577. ap_sa_query_timer(hapd, sta);
  578. }
  579. void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  580. {
  581. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  582. os_free(sta->sa_query_trans_id);
  583. sta->sa_query_trans_id = NULL;
  584. sta->sa_query_count = 0;
  585. }
  586. #endif /* CONFIG_IEEE80211W */