sta_info.c 19 KB

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