sta_info.c 19 KB

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