sta_info.c 19 KB

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