sta_info.c 19 KB

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