sta_info.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /*
  2. * hostapd / Station table
  3. * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "common/wpa_ctrl.h"
  13. #include "common/sae.h"
  14. #include "radius/radius.h"
  15. #include "radius/radius_client.h"
  16. #include "p2p/p2p.h"
  17. #include "fst/fst.h"
  18. #include "hostapd.h"
  19. #include "accounting.h"
  20. #include "ieee802_1x.h"
  21. #include "ieee802_11.h"
  22. #include "ieee802_11_auth.h"
  23. #include "wpa_auth.h"
  24. #include "preauth_auth.h"
  25. #include "ap_config.h"
  26. #include "beacon.h"
  27. #include "ap_mlme.h"
  28. #include "vlan_init.h"
  29. #include "p2p_hostapd.h"
  30. #include "ap_drv_ops.h"
  31. #include "gas_serv.h"
  32. #include "wnm_ap.h"
  33. #include "mbo_ap.h"
  34. #include "ndisc_snoop.h"
  35. #include "sta_info.h"
  36. #include "vlan.h"
  37. static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
  38. struct sta_info *sta);
  39. static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
  40. static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
  41. static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
  42. static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
  43. #ifdef CONFIG_IEEE80211W
  44. static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
  45. #endif /* CONFIG_IEEE80211W */
  46. static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
  47. int ap_for_each_sta(struct hostapd_data *hapd,
  48. int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
  49. void *ctx),
  50. void *ctx)
  51. {
  52. struct sta_info *sta;
  53. for (sta = hapd->sta_list; sta; sta = sta->next) {
  54. if (cb(hapd, sta, ctx))
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
  60. {
  61. struct sta_info *s;
  62. s = hapd->sta_hash[STA_HASH(sta)];
  63. while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
  64. s = s->hnext;
  65. return s;
  66. }
  67. #ifdef CONFIG_P2P
  68. struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
  69. {
  70. struct sta_info *sta;
  71. for (sta = hapd->sta_list; sta; sta = sta->next) {
  72. const u8 *p2p_dev_addr;
  73. if (sta->p2p_ie == NULL)
  74. continue;
  75. p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
  76. if (p2p_dev_addr == NULL)
  77. continue;
  78. if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
  79. return sta;
  80. }
  81. return NULL;
  82. }
  83. #endif /* CONFIG_P2P */
  84. static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
  85. {
  86. struct sta_info *tmp;
  87. if (hapd->sta_list == sta) {
  88. hapd->sta_list = sta->next;
  89. return;
  90. }
  91. tmp = hapd->sta_list;
  92. while (tmp != NULL && tmp->next != sta)
  93. tmp = tmp->next;
  94. if (tmp == NULL) {
  95. wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
  96. "list.", MAC2STR(sta->addr));
  97. } else
  98. tmp->next = sta->next;
  99. }
  100. void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
  101. {
  102. sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
  103. hapd->sta_hash[STA_HASH(sta->addr)] = sta;
  104. }
  105. static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
  106. {
  107. struct sta_info *s;
  108. s = hapd->sta_hash[STA_HASH(sta->addr)];
  109. if (s == NULL) return;
  110. if (os_memcmp(s->addr, sta->addr, 6) == 0) {
  111. hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
  112. return;
  113. }
  114. while (s->hnext != NULL &&
  115. os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
  116. s = s->hnext;
  117. if (s->hnext != NULL)
  118. s->hnext = s->hnext->hnext;
  119. else
  120. wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
  121. " from hash table", MAC2STR(sta->addr));
  122. }
  123. void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
  124. {
  125. sta_ip6addr_del(hapd, sta);
  126. }
  127. void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
  128. {
  129. int set_beacon = 0;
  130. accounting_sta_stop(hapd, sta);
  131. /* just in case */
  132. ap_sta_set_authorized(hapd, sta, 0);
  133. if (sta->flags & WLAN_STA_WDS)
  134. hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
  135. if (sta->ipaddr)
  136. hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
  137. ap_sta_ip6addr_del(hapd, sta);
  138. if (!hapd->iface->driver_ap_teardown &&
  139. !(sta->flags & WLAN_STA_PREAUTH)) {
  140. hostapd_drv_sta_remove(hapd, sta->addr);
  141. sta->added_unassoc = 0;
  142. }
  143. ap_sta_hash_del(hapd, sta);
  144. ap_sta_list_del(hapd, sta);
  145. if (sta->aid > 0)
  146. hapd->sta_aid[(sta->aid - 1) / 32] &=
  147. ~BIT((sta->aid - 1) % 32);
  148. hapd->num_sta--;
  149. if (sta->nonerp_set) {
  150. sta->nonerp_set = 0;
  151. hapd->iface->num_sta_non_erp--;
  152. if (hapd->iface->num_sta_non_erp == 0)
  153. set_beacon++;
  154. }
  155. if (sta->no_short_slot_time_set) {
  156. sta->no_short_slot_time_set = 0;
  157. hapd->iface->num_sta_no_short_slot_time--;
  158. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  159. && hapd->iface->num_sta_no_short_slot_time == 0)
  160. set_beacon++;
  161. }
  162. if (sta->no_short_preamble_set) {
  163. sta->no_short_preamble_set = 0;
  164. hapd->iface->num_sta_no_short_preamble--;
  165. if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
  166. && hapd->iface->num_sta_no_short_preamble == 0)
  167. set_beacon++;
  168. }
  169. if (sta->no_ht_gf_set) {
  170. sta->no_ht_gf_set = 0;
  171. hapd->iface->num_sta_ht_no_gf--;
  172. }
  173. if (sta->no_ht_set) {
  174. sta->no_ht_set = 0;
  175. hapd->iface->num_sta_no_ht--;
  176. }
  177. if (sta->ht_20mhz_set) {
  178. sta->ht_20mhz_set = 0;
  179. hapd->iface->num_sta_ht_20mhz--;
  180. }
  181. #ifdef CONFIG_IEEE80211N
  182. ht40_intolerant_remove(hapd->iface, sta);
  183. #endif /* CONFIG_IEEE80211N */
  184. #ifdef CONFIG_P2P
  185. if (sta->no_p2p_set) {
  186. sta->no_p2p_set = 0;
  187. hapd->num_sta_no_p2p--;
  188. if (hapd->num_sta_no_p2p == 0)
  189. hostapd_p2p_non_p2p_sta_disconnected(hapd);
  190. }
  191. #endif /* CONFIG_P2P */
  192. #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
  193. if (hostapd_ht_operation_update(hapd->iface) > 0)
  194. set_beacon++;
  195. #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
  196. #ifdef CONFIG_MESH
  197. if (hapd->mesh_sta_free_cb)
  198. hapd->mesh_sta_free_cb(hapd, sta);
  199. #endif /* CONFIG_MESH */
  200. if (set_beacon)
  201. ieee802_11_set_beacons(hapd->iface);
  202. wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
  203. __func__, MAC2STR(sta->addr));
  204. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  205. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  206. eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
  207. ap_sta_clear_disconnect_timeouts(hapd, sta);
  208. sae_clear_retransmit_timer(hapd, sta);
  209. ieee802_1x_free_station(hapd, sta);
  210. wpa_auth_sta_deinit(sta->wpa_sm);
  211. rsn_preauth_free_station(hapd, sta);
  212. #ifndef CONFIG_NO_RADIUS
  213. if (hapd->radius)
  214. radius_client_flush_auth(hapd->radius, sta->addr);
  215. #endif /* CONFIG_NO_RADIUS */
  216. #ifndef CONFIG_NO_VLAN
  217. /*
  218. * sta->wpa_sm->group needs to be released before so that
  219. * vlan_remove_dynamic() can check that no stations are left on the
  220. * AP_VLAN netdev.
  221. */
  222. if (sta->vlan_id)
  223. vlan_remove_dynamic(hapd, sta->vlan_id);
  224. if (sta->vlan_id_bound) {
  225. /*
  226. * Need to remove the STA entry before potentially removing the
  227. * VLAN.
  228. */
  229. if (hapd->iface->driver_ap_teardown &&
  230. !(sta->flags & WLAN_STA_PREAUTH)) {
  231. hostapd_drv_sta_remove(hapd, sta->addr);
  232. sta->added_unassoc = 0;
  233. }
  234. vlan_remove_dynamic(hapd, sta->vlan_id_bound);
  235. }
  236. #endif /* CONFIG_NO_VLAN */
  237. os_free(sta->challenge);
  238. #ifdef CONFIG_IEEE80211W
  239. os_free(sta->sa_query_trans_id);
  240. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  241. #endif /* CONFIG_IEEE80211W */
  242. #ifdef CONFIG_P2P
  243. p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
  244. #endif /* CONFIG_P2P */
  245. #ifdef CONFIG_INTERWORKING
  246. if (sta->gas_dialog) {
  247. int i;
  248. for (i = 0; i < GAS_DIALOG_MAX; i++)
  249. gas_serv_dialog_clear(&sta->gas_dialog[i]);
  250. os_free(sta->gas_dialog);
  251. }
  252. #endif /* CONFIG_INTERWORKING */
  253. wpabuf_free(sta->wps_ie);
  254. wpabuf_free(sta->p2p_ie);
  255. wpabuf_free(sta->hs20_ie);
  256. #ifdef CONFIG_FST
  257. wpabuf_free(sta->mb_ies);
  258. #endif /* CONFIG_FST */
  259. os_free(sta->ht_capabilities);
  260. os_free(sta->vht_capabilities);
  261. hostapd_free_psk_list(sta->psk);
  262. os_free(sta->identity);
  263. os_free(sta->radius_cui);
  264. os_free(sta->remediation_url);
  265. wpabuf_free(sta->hs20_deauth_req);
  266. os_free(sta->hs20_session_info_url);
  267. #ifdef CONFIG_SAE
  268. sae_clear_data(sta->sae);
  269. os_free(sta->sae);
  270. #endif /* CONFIG_SAE */
  271. mbo_ap_sta_free(sta);
  272. os_free(sta->supp_op_classes);
  273. os_free(sta);
  274. }
  275. void hostapd_free_stas(struct hostapd_data *hapd)
  276. {
  277. struct sta_info *sta, *prev;
  278. sta = hapd->sta_list;
  279. while (sta) {
  280. prev = sta;
  281. if (sta->flags & WLAN_STA_AUTH) {
  282. mlme_deauthenticate_indication(
  283. hapd, sta, WLAN_REASON_UNSPECIFIED);
  284. }
  285. sta = sta->next;
  286. wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
  287. MAC2STR(prev->addr));
  288. ap_free_sta(hapd, prev);
  289. }
  290. }
  291. /**
  292. * ap_handle_timer - Per STA timer handler
  293. * @eloop_ctx: struct hostapd_data *
  294. * @timeout_ctx: struct sta_info *
  295. *
  296. * This function is called to check station activity and to remove inactive
  297. * stations.
  298. */
  299. void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
  300. {
  301. struct hostapd_data *hapd = eloop_ctx;
  302. struct sta_info *sta = timeout_ctx;
  303. unsigned long next_time = 0;
  304. int reason;
  305. wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
  306. hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
  307. sta->timeout_next);
  308. if (sta->timeout_next == STA_REMOVE) {
  309. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  310. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  311. "local deauth request");
  312. ap_free_sta(hapd, sta);
  313. return;
  314. }
  315. if ((sta->flags & WLAN_STA_ASSOC) &&
  316. (sta->timeout_next == STA_NULLFUNC ||
  317. sta->timeout_next == STA_DISASSOC)) {
  318. int inactive_sec;
  319. /*
  320. * Add random value to timeout so that we don't end up bouncing
  321. * all stations at the same time if we have lots of associated
  322. * stations that are idle (but keep re-associating).
  323. */
  324. int fuzz = os_random() % 20;
  325. inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
  326. if (inactive_sec == -1) {
  327. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  328. "Check inactivity: Could not "
  329. "get station info from kernel driver for "
  330. MACSTR, MAC2STR(sta->addr));
  331. /*
  332. * The driver may not support this functionality.
  333. * Anyway, try again after the next inactivity timeout,
  334. * but do not disconnect the station now.
  335. */
  336. next_time = hapd->conf->ap_max_inactivity + fuzz;
  337. } else if (inactive_sec == -ENOENT) {
  338. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  339. "Station " MACSTR " has lost its driver entry",
  340. MAC2STR(sta->addr));
  341. /* Avoid sending client probe on removed client */
  342. sta->timeout_next = STA_DISASSOC;
  343. goto skip_poll;
  344. } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
  345. /* station activity detected; reset timeout state */
  346. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  347. "Station " MACSTR " has been active %is ago",
  348. MAC2STR(sta->addr), inactive_sec);
  349. sta->timeout_next = STA_NULLFUNC;
  350. next_time = hapd->conf->ap_max_inactivity + fuzz -
  351. inactive_sec;
  352. } else {
  353. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  354. "Station " MACSTR " has been "
  355. "inactive too long: %d sec, max allowed: %d",
  356. MAC2STR(sta->addr), inactive_sec,
  357. hapd->conf->ap_max_inactivity);
  358. if (hapd->conf->skip_inactivity_poll)
  359. sta->timeout_next = STA_DISASSOC;
  360. }
  361. }
  362. if ((sta->flags & WLAN_STA_ASSOC) &&
  363. sta->timeout_next == STA_DISASSOC &&
  364. !(sta->flags & WLAN_STA_PENDING_POLL) &&
  365. !hapd->conf->skip_inactivity_poll) {
  366. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
  367. " has ACKed data poll", MAC2STR(sta->addr));
  368. /* data nullfunc frame poll did not produce TX errors; assume
  369. * station ACKed it */
  370. sta->timeout_next = STA_NULLFUNC;
  371. next_time = hapd->conf->ap_max_inactivity;
  372. }
  373. skip_poll:
  374. if (next_time) {
  375. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  376. "for " MACSTR " (%lu seconds)",
  377. __func__, MAC2STR(sta->addr), next_time);
  378. eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
  379. sta);
  380. return;
  381. }
  382. if (sta->timeout_next == STA_NULLFUNC &&
  383. (sta->flags & WLAN_STA_ASSOC)) {
  384. wpa_printf(MSG_DEBUG, " Polling STA");
  385. sta->flags |= WLAN_STA_PENDING_POLL;
  386. hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
  387. sta->flags & WLAN_STA_WMM);
  388. } else if (sta->timeout_next != STA_REMOVE) {
  389. int deauth = sta->timeout_next == STA_DEAUTH;
  390. wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
  391. "Timeout, sending %s info to STA " MACSTR,
  392. deauth ? "deauthentication" : "disassociation",
  393. MAC2STR(sta->addr));
  394. if (deauth) {
  395. hostapd_drv_sta_deauth(
  396. hapd, sta->addr,
  397. WLAN_REASON_PREV_AUTH_NOT_VALID);
  398. } else {
  399. reason = (sta->timeout_next == STA_DISASSOC) ?
  400. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
  401. WLAN_REASON_PREV_AUTH_NOT_VALID;
  402. hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
  403. }
  404. }
  405. switch (sta->timeout_next) {
  406. case STA_NULLFUNC:
  407. sta->timeout_next = STA_DISASSOC;
  408. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  409. "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
  410. __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
  411. eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
  412. hapd, sta);
  413. break;
  414. case STA_DISASSOC:
  415. case STA_DISASSOC_FROM_CLI:
  416. ap_sta_set_authorized(hapd, sta, 0);
  417. sta->flags &= ~WLAN_STA_ASSOC;
  418. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  419. if (!sta->acct_terminate_cause)
  420. sta->acct_terminate_cause =
  421. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  422. accounting_sta_stop(hapd, sta);
  423. ieee802_1x_free_station(hapd, sta);
  424. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  425. HOSTAPD_LEVEL_INFO, "disassociated due to "
  426. "inactivity");
  427. reason = (sta->timeout_next == STA_DISASSOC) ?
  428. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
  429. WLAN_REASON_PREV_AUTH_NOT_VALID;
  430. sta->timeout_next = STA_DEAUTH;
  431. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  432. "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
  433. __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
  434. eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
  435. hapd, sta);
  436. mlme_disassociate_indication(hapd, sta, reason);
  437. break;
  438. case STA_DEAUTH:
  439. case STA_REMOVE:
  440. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  441. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  442. "inactivity (timer DEAUTH/REMOVE)");
  443. if (!sta->acct_terminate_cause)
  444. sta->acct_terminate_cause =
  445. RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
  446. mlme_deauthenticate_indication(
  447. hapd, sta,
  448. WLAN_REASON_PREV_AUTH_NOT_VALID);
  449. ap_free_sta(hapd, sta);
  450. break;
  451. }
  452. }
  453. static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
  454. {
  455. struct hostapd_data *hapd = eloop_ctx;
  456. struct sta_info *sta = timeout_ctx;
  457. wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
  458. hapd->conf->iface, MAC2STR(sta->addr));
  459. if (!(sta->flags & WLAN_STA_AUTH)) {
  460. if (sta->flags & WLAN_STA_GAS) {
  461. wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
  462. "entry " MACSTR, MAC2STR(sta->addr));
  463. ap_free_sta(hapd, sta);
  464. }
  465. return;
  466. }
  467. hostapd_drv_sta_deauth(hapd, sta->addr,
  468. WLAN_REASON_PREV_AUTH_NOT_VALID);
  469. mlme_deauthenticate_indication(hapd, sta,
  470. WLAN_REASON_PREV_AUTH_NOT_VALID);
  471. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  472. HOSTAPD_LEVEL_INFO, "deauthenticated due to "
  473. "session timeout");
  474. sta->acct_terminate_cause =
  475. RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
  476. ap_free_sta(hapd, sta);
  477. }
  478. void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
  479. u32 session_timeout)
  480. {
  481. if (eloop_replenish_timeout(session_timeout, 0,
  482. ap_handle_session_timer, hapd, sta) == 1) {
  483. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  484. HOSTAPD_LEVEL_DEBUG, "setting session timeout "
  485. "to %d seconds", session_timeout);
  486. }
  487. }
  488. void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
  489. u32 session_timeout)
  490. {
  491. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  492. HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
  493. "seconds", session_timeout);
  494. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  495. eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
  496. hapd, sta);
  497. }
  498. void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  499. {
  500. eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
  501. }
  502. static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
  503. {
  504. #ifdef CONFIG_WNM
  505. struct hostapd_data *hapd = eloop_ctx;
  506. struct sta_info *sta = timeout_ctx;
  507. wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
  508. MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
  509. if (sta->hs20_session_info_url == NULL)
  510. return;
  511. wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
  512. sta->hs20_disassoc_timer);
  513. #endif /* CONFIG_WNM */
  514. }
  515. void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
  516. struct sta_info *sta, int warning_time)
  517. {
  518. eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
  519. eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
  520. hapd, sta);
  521. }
  522. struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
  523. {
  524. struct sta_info *sta;
  525. sta = ap_get_sta(hapd, addr);
  526. if (sta)
  527. return sta;
  528. wpa_printf(MSG_DEBUG, " New STA");
  529. if (hapd->num_sta >= hapd->conf->max_num_sta) {
  530. /* FIX: might try to remove some old STAs first? */
  531. wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
  532. hapd->num_sta, hapd->conf->max_num_sta);
  533. return NULL;
  534. }
  535. sta = os_zalloc(sizeof(struct sta_info));
  536. if (sta == NULL) {
  537. wpa_printf(MSG_ERROR, "malloc failed");
  538. return NULL;
  539. }
  540. sta->acct_interim_interval = hapd->conf->acct_interim_interval;
  541. if (accounting_sta_get_id(hapd, sta) < 0) {
  542. os_free(sta);
  543. return NULL;
  544. }
  545. if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
  546. wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
  547. "for " MACSTR " (%d seconds - ap_max_inactivity)",
  548. __func__, MAC2STR(addr),
  549. hapd->conf->ap_max_inactivity);
  550. eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
  551. ap_handle_timer, hapd, sta);
  552. }
  553. /* initialize STA info data */
  554. os_memcpy(sta->addr, addr, ETH_ALEN);
  555. sta->next = hapd->sta_list;
  556. hapd->sta_list = sta;
  557. hapd->num_sta++;
  558. ap_sta_hash_add(hapd, sta);
  559. ap_sta_remove_in_other_bss(hapd, sta);
  560. sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
  561. dl_list_init(&sta->ip6addr);
  562. return sta;
  563. }
  564. static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
  565. {
  566. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  567. if (sta->ipaddr)
  568. hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
  569. ap_sta_ip6addr_del(hapd, sta);
  570. wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
  571. hapd->conf->iface, MAC2STR(sta->addr));
  572. if (hostapd_drv_sta_remove(hapd, sta->addr) &&
  573. sta->flags & WLAN_STA_ASSOC) {
  574. wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
  575. " from kernel driver",
  576. hapd->conf->iface, MAC2STR(sta->addr));
  577. return -1;
  578. }
  579. sta->added_unassoc = 0;
  580. return 0;
  581. }
  582. static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
  583. struct sta_info *sta)
  584. {
  585. struct hostapd_iface *iface = hapd->iface;
  586. size_t i;
  587. for (i = 0; i < iface->num_bss; i++) {
  588. struct hostapd_data *bss = iface->bss[i];
  589. struct sta_info *sta2;
  590. /* bss should always be set during operation, but it may be
  591. * NULL during reconfiguration. Assume the STA is not
  592. * associated to another BSS in that case to avoid NULL pointer
  593. * dereferences. */
  594. if (bss == hapd || bss == NULL)
  595. continue;
  596. sta2 = ap_get_sta(bss, sta->addr);
  597. if (!sta2)
  598. continue;
  599. wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR
  600. " association from another BSS %s",
  601. hapd->conf->iface, MAC2STR(sta2->addr),
  602. bss->conf->iface);
  603. ap_sta_disconnect(bss, sta2, sta2->addr,
  604. WLAN_REASON_PREV_AUTH_NOT_VALID);
  605. }
  606. }
  607. static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
  608. {
  609. struct hostapd_data *hapd = eloop_ctx;
  610. struct sta_info *sta = timeout_ctx;
  611. wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
  612. hapd->conf->iface, MAC2STR(sta->addr));
  613. ap_sta_remove(hapd, sta);
  614. mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
  615. }
  616. void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
  617. u16 reason)
  618. {
  619. wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
  620. hapd->conf->iface, MAC2STR(sta->addr));
  621. sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
  622. sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
  623. ap_sta_set_authorized(hapd, sta, 0);
  624. sta->timeout_next = STA_DEAUTH;
  625. wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
  626. "for " MACSTR " (%d seconds - "
  627. "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
  628. __func__, MAC2STR(sta->addr),
  629. AP_MAX_INACTIVITY_AFTER_DISASSOC);
  630. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  631. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
  632. ap_handle_timer, hapd, sta);
  633. accounting_sta_stop(hapd, sta);
  634. ieee802_1x_free_station(hapd, sta);
  635. sta->disassoc_reason = reason;
  636. sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
  637. eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
  638. eloop_register_timeout(hapd->iface->drv_flags &
  639. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  640. ap_sta_disassoc_cb_timeout, hapd, sta);
  641. }
  642. static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
  643. {
  644. struct hostapd_data *hapd = eloop_ctx;
  645. struct sta_info *sta = timeout_ctx;
  646. wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
  647. hapd->conf->iface, MAC2STR(sta->addr));
  648. ap_sta_remove(hapd, sta);
  649. mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
  650. }
  651. void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
  652. u16 reason)
  653. {
  654. wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
  655. hapd->conf->iface, MAC2STR(sta->addr));
  656. sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
  657. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
  658. ap_sta_set_authorized(hapd, sta, 0);
  659. sta->timeout_next = STA_REMOVE;
  660. wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
  661. "for " MACSTR " (%d seconds - "
  662. "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
  663. __func__, MAC2STR(sta->addr),
  664. AP_MAX_INACTIVITY_AFTER_DEAUTH);
  665. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  666. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
  667. ap_handle_timer, hapd, sta);
  668. accounting_sta_stop(hapd, sta);
  669. ieee802_1x_free_station(hapd, sta);
  670. sta->deauth_reason = reason;
  671. sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
  672. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  673. eloop_register_timeout(hapd->iface->drv_flags &
  674. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  675. ap_sta_deauth_cb_timeout, hapd, sta);
  676. }
  677. #ifdef CONFIG_WPS
  678. int ap_sta_wps_cancel(struct hostapd_data *hapd,
  679. struct sta_info *sta, void *ctx)
  680. {
  681. if (sta && (sta->flags & WLAN_STA_WPS)) {
  682. ap_sta_deauthenticate(hapd, sta,
  683. WLAN_REASON_PREV_AUTH_NOT_VALID);
  684. wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
  685. __func__, MAC2STR(sta->addr));
  686. return 1;
  687. }
  688. return 0;
  689. }
  690. #endif /* CONFIG_WPS */
  691. static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
  692. {
  693. struct hostapd_vlan *vlan;
  694. int vlan_id = MAX_VLAN_ID + 2;
  695. retry:
  696. for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
  697. if (vlan->vlan_id == vlan_id) {
  698. vlan_id++;
  699. goto retry;
  700. }
  701. }
  702. return vlan_id;
  703. }
  704. int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
  705. struct vlan_description *vlan_desc)
  706. {
  707. struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
  708. int old_vlan_id, vlan_id = 0, ret = 0;
  709. if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
  710. vlan_desc = NULL;
  711. /* Check if there is something to do */
  712. if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
  713. /* This sta is lacking its own vif */
  714. } else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
  715. !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
  716. /* sta->vlan_id needs to be reset */
  717. } else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
  718. return 0; /* nothing to change */
  719. }
  720. /* Now the real VLAN changed or the STA just needs its own vif */
  721. if (hapd->conf->ssid.per_sta_vif) {
  722. /* Assign a new vif, always */
  723. /* find a free vlan_id sufficiently big */
  724. vlan_id = ap_sta_get_free_vlan_id(hapd);
  725. /* Get wildcard VLAN */
  726. for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
  727. if (vlan->vlan_id == VLAN_ID_WILDCARD)
  728. break;
  729. }
  730. if (!vlan) {
  731. hostapd_logger(hapd, sta->addr,
  732. HOSTAPD_MODULE_IEEE80211,
  733. HOSTAPD_LEVEL_DEBUG,
  734. "per_sta_vif missing wildcard");
  735. vlan_id = 0;
  736. ret = -1;
  737. goto done;
  738. }
  739. } else if (vlan_desc && vlan_desc->notempty) {
  740. for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
  741. if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
  742. break;
  743. if (vlan->vlan_id == VLAN_ID_WILDCARD)
  744. wildcard_vlan = vlan;
  745. }
  746. if (vlan) {
  747. vlan_id = vlan->vlan_id;
  748. } else if (wildcard_vlan) {
  749. vlan = wildcard_vlan;
  750. vlan_id = vlan_desc->untagged;
  751. if (vlan_desc->tagged[0]) {
  752. /* Tagged VLAN configuration */
  753. vlan_id = ap_sta_get_free_vlan_id(hapd);
  754. }
  755. } else {
  756. hostapd_logger(hapd, sta->addr,
  757. HOSTAPD_MODULE_IEEE80211,
  758. HOSTAPD_LEVEL_DEBUG,
  759. "missing vlan and wildcard for vlan=%d%s",
  760. vlan_desc->untagged,
  761. vlan_desc->tagged[0] ? "+" : "");
  762. vlan_id = 0;
  763. ret = -1;
  764. goto done;
  765. }
  766. }
  767. if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
  768. vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
  769. if (vlan == NULL) {
  770. hostapd_logger(hapd, sta->addr,
  771. HOSTAPD_MODULE_IEEE80211,
  772. HOSTAPD_LEVEL_DEBUG,
  773. "could not add dynamic VLAN interface for vlan=%d%s",
  774. vlan_desc ? vlan_desc->untagged : -1,
  775. (vlan_desc && vlan_desc->tagged[0]) ?
  776. "+" : "");
  777. vlan_id = 0;
  778. ret = -1;
  779. goto done;
  780. }
  781. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  782. HOSTAPD_LEVEL_DEBUG,
  783. "added new dynamic VLAN interface '%s'",
  784. vlan->ifname);
  785. } else if (vlan && vlan->dynamic_vlan > 0) {
  786. vlan->dynamic_vlan++;
  787. hostapd_logger(hapd, sta->addr,
  788. HOSTAPD_MODULE_IEEE80211,
  789. HOSTAPD_LEVEL_DEBUG,
  790. "updated existing dynamic VLAN interface '%s'",
  791. vlan->ifname);
  792. }
  793. done:
  794. old_vlan_id = sta->vlan_id;
  795. sta->vlan_id = vlan_id;
  796. sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
  797. if (vlan_id != old_vlan_id && old_vlan_id)
  798. vlan_remove_dynamic(hapd, old_vlan_id);
  799. return ret;
  800. }
  801. int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
  802. {
  803. #ifndef CONFIG_NO_VLAN
  804. const char *iface;
  805. struct hostapd_vlan *vlan = NULL;
  806. int ret;
  807. int old_vlanid = sta->vlan_id_bound;
  808. iface = hapd->conf->iface;
  809. if (hapd->conf->ssid.vlan[0])
  810. iface = hapd->conf->ssid.vlan;
  811. if (sta->vlan_id > 0) {
  812. for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
  813. if (vlan->vlan_id == sta->vlan_id)
  814. break;
  815. }
  816. if (vlan)
  817. iface = vlan->ifname;
  818. }
  819. /*
  820. * Do not increment ref counters if the VLAN ID remains same, but do
  821. * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
  822. * have been called before.
  823. */
  824. if (sta->vlan_id == old_vlanid)
  825. goto skip_counting;
  826. if (sta->vlan_id > 0 && vlan == NULL) {
  827. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  828. HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
  829. "binding station to (vlan_id=%d)",
  830. sta->vlan_id);
  831. ret = -1;
  832. goto done;
  833. } else if (vlan && vlan->dynamic_vlan > 0) {
  834. vlan->dynamic_vlan++;
  835. hostapd_logger(hapd, sta->addr,
  836. HOSTAPD_MODULE_IEEE80211,
  837. HOSTAPD_LEVEL_DEBUG,
  838. "updated existing dynamic VLAN interface '%s'",
  839. iface);
  840. }
  841. /* ref counters have been increased, so mark the station */
  842. sta->vlan_id_bound = sta->vlan_id;
  843. skip_counting:
  844. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  845. HOSTAPD_LEVEL_DEBUG, "binding station to interface "
  846. "'%s'", iface);
  847. if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
  848. wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
  849. ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
  850. if (ret < 0) {
  851. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  852. HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
  853. "entry to vlan_id=%d", sta->vlan_id);
  854. }
  855. /* During 1x reauth, if the vlan id changes, then remove the old id. */
  856. if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
  857. vlan_remove_dynamic(hapd, old_vlanid);
  858. done:
  859. return ret;
  860. #else /* CONFIG_NO_VLAN */
  861. return 0;
  862. #endif /* CONFIG_NO_VLAN */
  863. }
  864. #ifdef CONFIG_IEEE80211W
  865. int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
  866. {
  867. u32 tu;
  868. struct os_reltime now, passed;
  869. os_get_reltime(&now);
  870. os_reltime_sub(&now, &sta->sa_query_start, &passed);
  871. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  872. if (hapd->conf->assoc_sa_query_max_timeout < tu) {
  873. hostapd_logger(hapd, sta->addr,
  874. HOSTAPD_MODULE_IEEE80211,
  875. HOSTAPD_LEVEL_DEBUG,
  876. "association SA Query timed out");
  877. sta->sa_query_timed_out = 1;
  878. os_free(sta->sa_query_trans_id);
  879. sta->sa_query_trans_id = NULL;
  880. sta->sa_query_count = 0;
  881. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  882. return 1;
  883. }
  884. return 0;
  885. }
  886. static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
  887. {
  888. struct hostapd_data *hapd = eloop_ctx;
  889. struct sta_info *sta = timeout_ctx;
  890. unsigned int timeout, sec, usec;
  891. u8 *trans_id, *nbuf;
  892. wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
  893. " (count=%d)",
  894. hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
  895. if (sta->sa_query_count > 0 &&
  896. ap_check_sa_query_timeout(hapd, sta))
  897. return;
  898. nbuf = os_realloc_array(sta->sa_query_trans_id,
  899. sta->sa_query_count + 1,
  900. WLAN_SA_QUERY_TR_ID_LEN);
  901. if (nbuf == NULL)
  902. return;
  903. if (sta->sa_query_count == 0) {
  904. /* Starting a new SA Query procedure */
  905. os_get_reltime(&sta->sa_query_start);
  906. }
  907. trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
  908. sta->sa_query_trans_id = nbuf;
  909. sta->sa_query_count++;
  910. if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
  911. /*
  912. * We don't really care which ID is used here, so simply
  913. * hardcode this if the mostly theoretical os_get_random()
  914. * failure happens.
  915. */
  916. trans_id[0] = 0x12;
  917. trans_id[1] = 0x34;
  918. }
  919. timeout = hapd->conf->assoc_sa_query_retry_timeout;
  920. sec = ((timeout / 1000) * 1024) / 1000;
  921. usec = (timeout % 1000) * 1024;
  922. eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
  923. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  924. HOSTAPD_LEVEL_DEBUG,
  925. "association SA Query attempt %d", sta->sa_query_count);
  926. ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
  927. }
  928. void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  929. {
  930. ap_sa_query_timer(hapd, sta);
  931. }
  932. void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
  933. {
  934. eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
  935. os_free(sta->sa_query_trans_id);
  936. sta->sa_query_trans_id = NULL;
  937. sta->sa_query_count = 0;
  938. }
  939. #endif /* CONFIG_IEEE80211W */
  940. void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
  941. int authorized)
  942. {
  943. const u8 *dev_addr = NULL;
  944. char buf[100];
  945. #ifdef CONFIG_P2P
  946. u8 addr[ETH_ALEN];
  947. u8 ip_addr_buf[4];
  948. #endif /* CONFIG_P2P */
  949. if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
  950. return;
  951. if (authorized)
  952. sta->flags |= WLAN_STA_AUTHORIZED;
  953. else
  954. sta->flags &= ~WLAN_STA_AUTHORIZED;
  955. #ifdef CONFIG_P2P
  956. if (hapd->p2p_group == NULL) {
  957. if (sta->p2p_ie != NULL &&
  958. p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
  959. dev_addr = addr;
  960. } else
  961. dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
  962. if (dev_addr)
  963. os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
  964. MAC2STR(sta->addr), MAC2STR(dev_addr));
  965. else
  966. #endif /* CONFIG_P2P */
  967. os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
  968. if (hapd->sta_authorized_cb)
  969. hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
  970. sta->addr, authorized, dev_addr);
  971. if (authorized) {
  972. char ip_addr[100];
  973. ip_addr[0] = '\0';
  974. #ifdef CONFIG_P2P
  975. if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
  976. os_snprintf(ip_addr, sizeof(ip_addr),
  977. " ip_addr=%u.%u.%u.%u",
  978. ip_addr_buf[0], ip_addr_buf[1],
  979. ip_addr_buf[2], ip_addr_buf[3]);
  980. }
  981. #endif /* CONFIG_P2P */
  982. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
  983. buf, ip_addr);
  984. if (hapd->msg_ctx_parent &&
  985. hapd->msg_ctx_parent != hapd->msg_ctx)
  986. wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
  987. AP_STA_CONNECTED "%s%s",
  988. buf, ip_addr);
  989. } else {
  990. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
  991. if (hapd->msg_ctx_parent &&
  992. hapd->msg_ctx_parent != hapd->msg_ctx)
  993. wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
  994. AP_STA_DISCONNECTED "%s", buf);
  995. }
  996. #ifdef CONFIG_FST
  997. if (hapd->iface->fst) {
  998. if (authorized)
  999. fst_notify_peer_connected(hapd->iface->fst, sta->addr);
  1000. else
  1001. fst_notify_peer_disconnected(hapd->iface->fst,
  1002. sta->addr);
  1003. }
  1004. #endif /* CONFIG_FST */
  1005. }
  1006. void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
  1007. const u8 *addr, u16 reason)
  1008. {
  1009. if (sta)
  1010. wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
  1011. hapd->conf->iface, __func__, MAC2STR(sta->addr),
  1012. reason);
  1013. else if (addr)
  1014. wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
  1015. hapd->conf->iface, __func__, MAC2STR(addr),
  1016. reason);
  1017. if (sta == NULL && addr)
  1018. sta = ap_get_sta(hapd, addr);
  1019. if (addr)
  1020. hostapd_drv_sta_deauth(hapd, addr, reason);
  1021. if (sta == NULL)
  1022. return;
  1023. ap_sta_set_authorized(hapd, sta, 0);
  1024. wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
  1025. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  1026. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  1027. wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
  1028. "for " MACSTR " (%d seconds - "
  1029. "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
  1030. hapd->conf->iface, __func__, MAC2STR(sta->addr),
  1031. AP_MAX_INACTIVITY_AFTER_DEAUTH);
  1032. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  1033. eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
  1034. ap_handle_timer, hapd, sta);
  1035. sta->timeout_next = STA_REMOVE;
  1036. sta->deauth_reason = reason;
  1037. sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
  1038. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  1039. eloop_register_timeout(hapd->iface->drv_flags &
  1040. WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
  1041. ap_sta_deauth_cb_timeout, hapd, sta);
  1042. }
  1043. void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
  1044. {
  1045. if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
  1046. wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
  1047. return;
  1048. }
  1049. sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
  1050. eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
  1051. ap_sta_deauth_cb_timeout(hapd, sta);
  1052. }
  1053. void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
  1054. {
  1055. if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
  1056. wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
  1057. return;
  1058. }
  1059. sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
  1060. eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
  1061. ap_sta_disassoc_cb_timeout(hapd, sta);
  1062. }
  1063. void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
  1064. struct sta_info *sta)
  1065. {
  1066. if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
  1067. wpa_printf(MSG_DEBUG,
  1068. "%s: Removed ap_sta_deauth_cb_timeout timeout for "
  1069. MACSTR,
  1070. hapd->conf->iface, MAC2STR(sta->addr));
  1071. if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
  1072. wpa_printf(MSG_DEBUG,
  1073. "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
  1074. MACSTR,
  1075. hapd->conf->iface, MAC2STR(sta->addr));
  1076. }
  1077. int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
  1078. {
  1079. int res;
  1080. buf[0] = '\0';
  1081. res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  1082. (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
  1083. (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
  1084. (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
  1085. (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
  1086. ""),
  1087. (flags & WLAN_STA_SHORT_PREAMBLE ?
  1088. "[SHORT_PREAMBLE]" : ""),
  1089. (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
  1090. (flags & WLAN_STA_WMM ? "[WMM]" : ""),
  1091. (flags & WLAN_STA_MFP ? "[MFP]" : ""),
  1092. (flags & WLAN_STA_WPS ? "[WPS]" : ""),
  1093. (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
  1094. (flags & WLAN_STA_WDS ? "[WDS]" : ""),
  1095. (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
  1096. (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
  1097. (flags & WLAN_STA_GAS ? "[GAS]" : ""),
  1098. (flags & WLAN_STA_VHT ? "[VHT]" : ""),
  1099. (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
  1100. (flags & WLAN_STA_WNM_SLEEP_MODE ?
  1101. "[WNM_SLEEP_MODE]" : ""));
  1102. if (os_snprintf_error(buflen, res))
  1103. res = -1;
  1104. return res;
  1105. }