bss.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * BSS table
  3. * Copyright (c) 2009-2010, 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 "drivers/driver.h"
  13. #include "wpa_supplicant_i.h"
  14. #include "config.h"
  15. #include "notify.h"
  16. #include "scan.h"
  17. #include "bss.h"
  18. /**
  19. * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
  20. */
  21. #define WPA_BSS_EXPIRATION_PERIOD 10
  22. #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0)
  23. #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1)
  24. #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2)
  25. #define WPA_BSS_MODE_CHANGED_FLAG BIT(3)
  26. #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4)
  27. #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5)
  28. #define WPA_BSS_WPS_CHANGED_FLAG BIT(6)
  29. #define WPA_BSS_RATES_CHANGED_FLAG BIT(7)
  30. #define WPA_BSS_IES_CHANGED_FLAG BIT(8)
  31. static void wpa_bss_set_hessid(struct wpa_bss *bss)
  32. {
  33. #ifdef CONFIG_INTERWORKING
  34. const u8 *ie = wpa_bss_get_ie(bss, WLAN_EID_INTERWORKING);
  35. if (ie == NULL || (ie[1] != 7 && ie[1] != 9)) {
  36. os_memset(bss->hessid, 0, ETH_ALEN);
  37. return;
  38. }
  39. if (ie[1] == 7)
  40. os_memcpy(bss->hessid, ie + 3, ETH_ALEN);
  41. else
  42. os_memcpy(bss->hessid, ie + 5, ETH_ALEN);
  43. #endif /* CONFIG_INTERWORKING */
  44. }
  45. struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
  46. {
  47. struct wpa_bss_anqp *anqp;
  48. anqp = os_zalloc(sizeof(*anqp));
  49. if (anqp == NULL)
  50. return NULL;
  51. anqp->users = 1;
  52. return anqp;
  53. }
  54. static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
  55. {
  56. if (anqp == NULL)
  57. return;
  58. anqp->users--;
  59. if (anqp->users > 0) {
  60. /* Another BSS entry holds a pointer to this ANQP info */
  61. return;
  62. }
  63. #ifdef CONFIG_INTERWORKING
  64. wpabuf_free(anqp->venue_name);
  65. wpabuf_free(anqp->network_auth_type);
  66. wpabuf_free(anqp->roaming_consortium);
  67. wpabuf_free(anqp->ip_addr_type_availability);
  68. wpabuf_free(anqp->nai_realm);
  69. wpabuf_free(anqp->anqp_3gpp);
  70. wpabuf_free(anqp->domain_name);
  71. #endif /* CONFIG_INTERWORKING */
  72. #ifdef CONFIG_HS20
  73. wpabuf_free(anqp->hs20_operator_friendly_name);
  74. wpabuf_free(anqp->hs20_wan_metrics);
  75. wpabuf_free(anqp->hs20_connection_capability);
  76. wpabuf_free(anqp->hs20_operating_class);
  77. #endif /* CONFIG_HS20 */
  78. os_free(anqp);
  79. }
  80. static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  81. const char *reason)
  82. {
  83. if (wpa_s->last_scan_res) {
  84. unsigned int i;
  85. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  86. if (wpa_s->last_scan_res[i] == bss) {
  87. os_memmove(&wpa_s->last_scan_res[i],
  88. &wpa_s->last_scan_res[i + 1],
  89. (wpa_s->last_scan_res_used - i - 1)
  90. * sizeof(struct wpa_bss *));
  91. wpa_s->last_scan_res_used--;
  92. break;
  93. }
  94. }
  95. }
  96. dl_list_del(&bss->list);
  97. dl_list_del(&bss->list_id);
  98. wpa_s->num_bss--;
  99. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
  100. " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
  101. wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
  102. wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
  103. wpa_bss_anqp_free(bss->anqp);
  104. os_free(bss);
  105. }
  106. struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
  107. const u8 *ssid, size_t ssid_len)
  108. {
  109. struct wpa_bss *bss;
  110. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  111. return NULL;
  112. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  113. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
  114. bss->ssid_len == ssid_len &&
  115. os_memcmp(bss->ssid, ssid, ssid_len) == 0)
  116. return bss;
  117. }
  118. return NULL;
  119. }
  120. static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
  121. {
  122. os_time_t usec;
  123. dst->flags = src->flags;
  124. os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
  125. dst->freq = src->freq;
  126. dst->beacon_int = src->beacon_int;
  127. dst->caps = src->caps;
  128. dst->qual = src->qual;
  129. dst->noise = src->noise;
  130. dst->level = src->level;
  131. dst->tsf = src->tsf;
  132. os_get_time(&dst->last_update);
  133. dst->last_update.sec -= src->age / 1000;
  134. usec = (src->age % 1000) * 1000;
  135. if (dst->last_update.usec < usec) {
  136. dst->last_update.sec--;
  137. dst->last_update.usec += 1000000;
  138. }
  139. dst->last_update.usec -= usec;
  140. }
  141. static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  142. {
  143. struct wpa_ssid *ssid;
  144. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  145. if (ssid->ssid == NULL || ssid->ssid_len == 0)
  146. continue;
  147. if (ssid->ssid_len == bss->ssid_len &&
  148. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  154. {
  155. return bss == wpa_s->current_bss ||
  156. os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
  157. os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
  158. }
  159. static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
  160. {
  161. struct wpa_bss *bss;
  162. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  163. if (!wpa_bss_known(wpa_s, bss)) {
  164. wpa_bss_remove(wpa_s, bss, __func__);
  165. return 0;
  166. }
  167. }
  168. return -1;
  169. }
  170. static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
  171. {
  172. struct wpa_bss *bss;
  173. /*
  174. * Remove the oldest entry that does not match with any configured
  175. * network.
  176. */
  177. if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
  178. return 0;
  179. /*
  180. * Remove the oldest entry that isn't currently in use.
  181. */
  182. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  183. if (!wpa_bss_in_use(wpa_s, bss)) {
  184. wpa_bss_remove(wpa_s, bss, __func__);
  185. return 0;
  186. }
  187. }
  188. return -1;
  189. }
  190. static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
  191. const u8 *ssid, size_t ssid_len,
  192. struct wpa_scan_res *res)
  193. {
  194. struct wpa_bss *bss;
  195. bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
  196. if (bss == NULL)
  197. return NULL;
  198. bss->id = wpa_s->bss_next_id++;
  199. bss->last_update_idx = wpa_s->bss_update_idx;
  200. wpa_bss_copy_res(bss, res);
  201. os_memcpy(bss->ssid, ssid, ssid_len);
  202. bss->ssid_len = ssid_len;
  203. bss->ie_len = res->ie_len;
  204. bss->beacon_ie_len = res->beacon_ie_len;
  205. os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
  206. wpa_bss_set_hessid(bss);
  207. dl_list_add_tail(&wpa_s->bss, &bss->list);
  208. dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
  209. wpa_s->num_bss++;
  210. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
  211. " SSID '%s'",
  212. bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
  213. wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
  214. if (wpa_s->num_bss > wpa_s->conf->bss_max_count &&
  215. wpa_bss_remove_oldest(wpa_s) != 0) {
  216. wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
  217. "because all BSSes are in use. We should normally "
  218. "not get here!", (int) wpa_s->num_bss);
  219. wpa_s->conf->bss_max_count = wpa_s->num_bss;
  220. }
  221. return bss;
  222. }
  223. static int are_ies_equal(const struct wpa_bss *old,
  224. const struct wpa_scan_res *new, u32 ie)
  225. {
  226. const u8 *old_ie, *new_ie;
  227. struct wpabuf *old_ie_buff = NULL;
  228. struct wpabuf *new_ie_buff = NULL;
  229. int new_ie_len, old_ie_len, ret, is_multi;
  230. switch (ie) {
  231. case WPA_IE_VENDOR_TYPE:
  232. old_ie = wpa_bss_get_vendor_ie(old, ie);
  233. new_ie = wpa_scan_get_vendor_ie(new, ie);
  234. is_multi = 0;
  235. break;
  236. case WPS_IE_VENDOR_TYPE:
  237. old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
  238. new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
  239. is_multi = 1;
  240. break;
  241. case WLAN_EID_RSN:
  242. case WLAN_EID_SUPP_RATES:
  243. case WLAN_EID_EXT_SUPP_RATES:
  244. old_ie = wpa_bss_get_ie(old, ie);
  245. new_ie = wpa_scan_get_ie(new, ie);
  246. is_multi = 0;
  247. break;
  248. default:
  249. wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
  250. return 0;
  251. }
  252. if (is_multi) {
  253. /* in case of multiple IEs stored in buffer */
  254. old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
  255. new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
  256. old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
  257. new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
  258. } else {
  259. /* in case of single IE */
  260. old_ie_len = old_ie ? old_ie[1] + 2 : 0;
  261. new_ie_len = new_ie ? new_ie[1] + 2 : 0;
  262. }
  263. if (!old_ie || !new_ie)
  264. ret = !old_ie && !new_ie;
  265. else
  266. ret = (old_ie_len == new_ie_len &&
  267. os_memcmp(old_ie, new_ie, old_ie_len) == 0);
  268. wpabuf_free(old_ie_buff);
  269. wpabuf_free(new_ie_buff);
  270. return ret;
  271. }
  272. static u32 wpa_bss_compare_res(const struct wpa_bss *old,
  273. const struct wpa_scan_res *new)
  274. {
  275. u32 changes = 0;
  276. int caps_diff = old->caps ^ new->caps;
  277. if (old->freq != new->freq)
  278. changes |= WPA_BSS_FREQ_CHANGED_FLAG;
  279. if (old->level != new->level)
  280. changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
  281. if (caps_diff & IEEE80211_CAP_PRIVACY)
  282. changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
  283. if (caps_diff & IEEE80211_CAP_IBSS)
  284. changes |= WPA_BSS_MODE_CHANGED_FLAG;
  285. if (old->ie_len == new->ie_len &&
  286. os_memcmp(old + 1, new + 1, old->ie_len) == 0)
  287. return changes;
  288. changes |= WPA_BSS_IES_CHANGED_FLAG;
  289. if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
  290. changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
  291. if (!are_ies_equal(old, new, WLAN_EID_RSN))
  292. changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
  293. if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
  294. changes |= WPA_BSS_WPS_CHANGED_FLAG;
  295. if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
  296. !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
  297. changes |= WPA_BSS_RATES_CHANGED_FLAG;
  298. return changes;
  299. }
  300. static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
  301. const struct wpa_bss *bss)
  302. {
  303. if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
  304. wpas_notify_bss_freq_changed(wpa_s, bss->id);
  305. if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
  306. wpas_notify_bss_signal_changed(wpa_s, bss->id);
  307. if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
  308. wpas_notify_bss_privacy_changed(wpa_s, bss->id);
  309. if (changes & WPA_BSS_MODE_CHANGED_FLAG)
  310. wpas_notify_bss_mode_changed(wpa_s, bss->id);
  311. if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
  312. wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
  313. if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
  314. wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
  315. if (changes & WPA_BSS_WPS_CHANGED_FLAG)
  316. wpas_notify_bss_wps_changed(wpa_s, bss->id);
  317. if (changes & WPA_BSS_IES_CHANGED_FLAG)
  318. wpas_notify_bss_ies_changed(wpa_s, bss->id);
  319. if (changes & WPA_BSS_RATES_CHANGED_FLAG)
  320. wpas_notify_bss_rates_changed(wpa_s, bss->id);
  321. }
  322. static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  323. struct wpa_scan_res *res)
  324. {
  325. u32 changes;
  326. changes = wpa_bss_compare_res(bss, res);
  327. bss->scan_miss_count = 0;
  328. bss->last_update_idx = wpa_s->bss_update_idx;
  329. wpa_bss_copy_res(bss, res);
  330. /* Move the entry to the end of the list */
  331. dl_list_del(&bss->list);
  332. if (bss->ie_len + bss->beacon_ie_len >=
  333. res->ie_len + res->beacon_ie_len) {
  334. os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
  335. bss->ie_len = res->ie_len;
  336. bss->beacon_ie_len = res->beacon_ie_len;
  337. } else {
  338. struct wpa_bss *nbss;
  339. struct dl_list *prev = bss->list_id.prev;
  340. dl_list_del(&bss->list_id);
  341. nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
  342. res->beacon_ie_len);
  343. if (nbss) {
  344. if (wpa_s->current_bss == bss)
  345. wpa_s->current_bss = nbss;
  346. bss = nbss;
  347. os_memcpy(bss + 1, res + 1,
  348. res->ie_len + res->beacon_ie_len);
  349. bss->ie_len = res->ie_len;
  350. bss->beacon_ie_len = res->beacon_ie_len;
  351. }
  352. dl_list_add(prev, &bss->list_id);
  353. }
  354. if (changes & WPA_BSS_IES_CHANGED_FLAG)
  355. wpa_bss_set_hessid(bss);
  356. dl_list_add_tail(&wpa_s->bss, &bss->list);
  357. notify_bss_changes(wpa_s, changes, bss);
  358. }
  359. void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
  360. {
  361. wpa_s->bss_update_idx++;
  362. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
  363. wpa_s->bss_update_idx);
  364. wpa_s->last_scan_res_used = 0;
  365. }
  366. void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
  367. struct wpa_scan_res *res)
  368. {
  369. const u8 *ssid, *p2p;
  370. struct wpa_bss *bss;
  371. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  372. if (ssid == NULL) {
  373. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
  374. MACSTR, MAC2STR(res->bssid));
  375. return;
  376. }
  377. if (ssid[1] > 32) {
  378. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
  379. MACSTR, MAC2STR(res->bssid));
  380. return;
  381. }
  382. p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
  383. #ifdef CONFIG_P2P
  384. if (p2p == NULL &&
  385. wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  386. /*
  387. * If it's a P2P specific interface, then don't update
  388. * the scan result without a P2P IE.
  389. */
  390. wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
  391. " update for P2P interface", MAC2STR(res->bssid));
  392. return;
  393. }
  394. #endif /* CONFIG_P2P */
  395. if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
  396. os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
  397. return; /* Skip P2P listen discovery results here */
  398. /* TODO: add option for ignoring BSSes we are not interested in
  399. * (to save memory) */
  400. bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
  401. if (bss == NULL)
  402. bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
  403. else
  404. wpa_bss_update(wpa_s, bss, res);
  405. if (bss == NULL)
  406. return;
  407. if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
  408. struct wpa_bss **n;
  409. unsigned int siz;
  410. if (wpa_s->last_scan_res_size == 0)
  411. siz = 32;
  412. else
  413. siz = wpa_s->last_scan_res_size * 2;
  414. n = os_realloc_array(wpa_s->last_scan_res, siz,
  415. sizeof(struct wpa_bss *));
  416. if (n == NULL)
  417. return;
  418. wpa_s->last_scan_res = n;
  419. wpa_s->last_scan_res_size = siz;
  420. }
  421. wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
  422. }
  423. static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
  424. const struct scan_info *info)
  425. {
  426. int found;
  427. size_t i;
  428. if (info == NULL)
  429. return 1;
  430. if (info->num_freqs) {
  431. found = 0;
  432. for (i = 0; i < info->num_freqs; i++) {
  433. if (bss->freq == info->freqs[i]) {
  434. found = 1;
  435. break;
  436. }
  437. }
  438. if (!found)
  439. return 0;
  440. }
  441. if (info->num_ssids) {
  442. found = 0;
  443. for (i = 0; i < info->num_ssids; i++) {
  444. const struct wpa_driver_scan_ssid *s = &info->ssids[i];
  445. if ((s->ssid == NULL || s->ssid_len == 0) ||
  446. (s->ssid_len == bss->ssid_len &&
  447. os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
  448. 0)) {
  449. found = 1;
  450. break;
  451. }
  452. }
  453. if (!found)
  454. return 0;
  455. }
  456. return 1;
  457. }
  458. void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
  459. int new_scan)
  460. {
  461. struct wpa_bss *bss, *n;
  462. wpa_s->last_scan_full = 0;
  463. os_get_time(&wpa_s->last_scan);
  464. if (!new_scan)
  465. return; /* do not expire entries without new scan */
  466. if (info && !info->aborted && !info->freqs) {
  467. size_t i;
  468. if (info->num_ssids == 0) {
  469. wpa_s->last_scan_full = 1;
  470. } else {
  471. for (i = 0; i < info->num_ssids; i++) {
  472. if (info->ssids[i].ssid == NULL ||
  473. info->ssids[i].ssid_len == 0) {
  474. wpa_s->last_scan_full = 1;
  475. break;
  476. }
  477. }
  478. }
  479. }
  480. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  481. if (wpa_bss_in_use(wpa_s, bss))
  482. continue;
  483. if (!wpa_bss_included_in_scan(bss, info))
  484. continue; /* expire only BSSes that were scanned */
  485. if (bss->last_update_idx < wpa_s->bss_update_idx)
  486. bss->scan_miss_count++;
  487. if (bss->scan_miss_count >=
  488. wpa_s->conf->bss_expiration_scan_count) {
  489. wpa_bss_remove(wpa_s, bss, "no match in scan");
  490. }
  491. }
  492. wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u "
  493. "last_scan_full=%d",
  494. wpa_s->last_scan_res_used, wpa_s->last_scan_res_size,
  495. wpa_s->last_scan_full);
  496. }
  497. void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
  498. {
  499. struct wpa_bss *bss, *n;
  500. struct os_time t;
  501. if (dl_list_empty(&wpa_s->bss))
  502. return;
  503. os_get_time(&t);
  504. t.sec -= age;
  505. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  506. if (wpa_bss_in_use(wpa_s, bss))
  507. continue;
  508. if (os_time_before(&bss->last_update, &t)) {
  509. wpa_bss_remove(wpa_s, bss, __func__);
  510. } else
  511. break;
  512. }
  513. }
  514. static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
  515. {
  516. struct wpa_supplicant *wpa_s = eloop_ctx;
  517. wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
  518. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  519. wpa_bss_timeout, wpa_s, NULL);
  520. }
  521. int wpa_bss_init(struct wpa_supplicant *wpa_s)
  522. {
  523. dl_list_init(&wpa_s->bss);
  524. dl_list_init(&wpa_s->bss_id);
  525. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  526. wpa_bss_timeout, wpa_s, NULL);
  527. return 0;
  528. }
  529. void wpa_bss_flush(struct wpa_supplicant *wpa_s)
  530. {
  531. struct wpa_bss *bss, *n;
  532. if (wpa_s->bss.next == NULL)
  533. return; /* BSS table not yet initialized */
  534. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  535. if (wpa_bss_in_use(wpa_s, bss))
  536. continue;
  537. wpa_bss_remove(wpa_s, bss, __func__);
  538. }
  539. }
  540. void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
  541. {
  542. eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
  543. wpa_bss_flush(wpa_s);
  544. }
  545. struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
  546. const u8 *bssid)
  547. {
  548. struct wpa_bss *bss;
  549. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  550. return NULL;
  551. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  552. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  553. return bss;
  554. }
  555. return NULL;
  556. }
  557. #ifdef CONFIG_P2P
  558. struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
  559. const u8 *dev_addr)
  560. {
  561. struct wpa_bss *bss;
  562. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  563. u8 addr[ETH_ALEN];
  564. if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
  565. addr) == 0 &&
  566. os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
  567. return bss;
  568. }
  569. return NULL;
  570. }
  571. #endif /* CONFIG_P2P */
  572. struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  573. {
  574. struct wpa_bss *bss;
  575. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  576. if (bss->id == id)
  577. return bss;
  578. }
  579. return NULL;
  580. }
  581. const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
  582. {
  583. const u8 *end, *pos;
  584. pos = (const u8 *) (bss + 1);
  585. end = pos + bss->ie_len;
  586. while (pos + 1 < end) {
  587. if (pos + 2 + pos[1] > end)
  588. break;
  589. if (pos[0] == ie)
  590. return pos;
  591. pos += 2 + pos[1];
  592. }
  593. return NULL;
  594. }
  595. const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
  596. {
  597. const u8 *end, *pos;
  598. pos = (const u8 *) (bss + 1);
  599. end = pos + bss->ie_len;
  600. while (pos + 1 < end) {
  601. if (pos + 2 + pos[1] > end)
  602. break;
  603. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  604. vendor_type == WPA_GET_BE32(&pos[2]))
  605. return pos;
  606. pos += 2 + pos[1];
  607. }
  608. return NULL;
  609. }
  610. struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
  611. u32 vendor_type)
  612. {
  613. struct wpabuf *buf;
  614. const u8 *end, *pos;
  615. buf = wpabuf_alloc(bss->ie_len);
  616. if (buf == NULL)
  617. return NULL;
  618. pos = (const u8 *) (bss + 1);
  619. end = pos + bss->ie_len;
  620. while (pos + 1 < end) {
  621. if (pos + 2 + pos[1] > end)
  622. break;
  623. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  624. vendor_type == WPA_GET_BE32(&pos[2]))
  625. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  626. pos += 2 + pos[1];
  627. }
  628. if (wpabuf_len(buf) == 0) {
  629. wpabuf_free(buf);
  630. buf = NULL;
  631. }
  632. return buf;
  633. }
  634. struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
  635. u32 vendor_type)
  636. {
  637. struct wpabuf *buf;
  638. const u8 *end, *pos;
  639. buf = wpabuf_alloc(bss->beacon_ie_len);
  640. if (buf == NULL)
  641. return NULL;
  642. pos = (const u8 *) (bss + 1);
  643. pos += bss->ie_len;
  644. end = pos + bss->beacon_ie_len;
  645. while (pos + 1 < end) {
  646. if (pos + 2 + pos[1] > end)
  647. break;
  648. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  649. vendor_type == WPA_GET_BE32(&pos[2]))
  650. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  651. pos += 2 + pos[1];
  652. }
  653. if (wpabuf_len(buf) == 0) {
  654. wpabuf_free(buf);
  655. buf = NULL;
  656. }
  657. return buf;
  658. }
  659. int wpa_bss_get_max_rate(const struct wpa_bss *bss)
  660. {
  661. int rate = 0;
  662. const u8 *ie;
  663. int i;
  664. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  665. for (i = 0; ie && i < ie[1]; i++) {
  666. if ((ie[i + 2] & 0x7f) > rate)
  667. rate = ie[i + 2] & 0x7f;
  668. }
  669. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  670. for (i = 0; ie && i < ie[1]; i++) {
  671. if ((ie[i + 2] & 0x7f) > rate)
  672. rate = ie[i + 2] & 0x7f;
  673. }
  674. return rate;
  675. }
  676. int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
  677. {
  678. const u8 *ie, *ie2;
  679. int i, j;
  680. unsigned int len;
  681. u8 *r;
  682. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  683. ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  684. len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
  685. r = os_malloc(len);
  686. if (!r)
  687. return -1;
  688. for (i = 0; ie && i < ie[1]; i++)
  689. r[i] = ie[i + 2] & 0x7f;
  690. for (j = 0; ie2 && j < ie2[1]; j++)
  691. r[i + j] = ie2[j + 2] & 0x7f;
  692. *rates = r;
  693. return len;
  694. }