bss.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 struct wpa_bss *
  323. wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  324. struct wpa_scan_res *res)
  325. {
  326. u32 changes;
  327. changes = wpa_bss_compare_res(bss, res);
  328. bss->scan_miss_count = 0;
  329. bss->last_update_idx = wpa_s->bss_update_idx;
  330. wpa_bss_copy_res(bss, res);
  331. /* Move the entry to the end of the list */
  332. dl_list_del(&bss->list);
  333. if (bss->ie_len + bss->beacon_ie_len >=
  334. res->ie_len + res->beacon_ie_len) {
  335. os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
  336. bss->ie_len = res->ie_len;
  337. bss->beacon_ie_len = res->beacon_ie_len;
  338. } else {
  339. struct wpa_bss *nbss;
  340. struct dl_list *prev = bss->list_id.prev;
  341. dl_list_del(&bss->list_id);
  342. nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
  343. res->beacon_ie_len);
  344. if (nbss) {
  345. unsigned int i;
  346. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  347. if (wpa_s->last_scan_res[i] == bss) {
  348. wpa_s->last_scan_res[i] = nbss;
  349. break;
  350. }
  351. }
  352. if (wpa_s->current_bss == bss)
  353. wpa_s->current_bss = nbss;
  354. bss = nbss;
  355. os_memcpy(bss + 1, res + 1,
  356. res->ie_len + res->beacon_ie_len);
  357. bss->ie_len = res->ie_len;
  358. bss->beacon_ie_len = res->beacon_ie_len;
  359. }
  360. dl_list_add(prev, &bss->list_id);
  361. }
  362. if (changes & WPA_BSS_IES_CHANGED_FLAG)
  363. wpa_bss_set_hessid(bss);
  364. dl_list_add_tail(&wpa_s->bss, &bss->list);
  365. notify_bss_changes(wpa_s, changes, bss);
  366. return bss;
  367. }
  368. void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
  369. {
  370. wpa_s->bss_update_idx++;
  371. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
  372. wpa_s->bss_update_idx);
  373. wpa_s->last_scan_res_used = 0;
  374. }
  375. void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
  376. struct wpa_scan_res *res)
  377. {
  378. const u8 *ssid, *p2p;
  379. struct wpa_bss *bss;
  380. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  381. if (ssid == NULL) {
  382. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
  383. MACSTR, MAC2STR(res->bssid));
  384. return;
  385. }
  386. if (ssid[1] > 32) {
  387. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
  388. MACSTR, MAC2STR(res->bssid));
  389. return;
  390. }
  391. p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
  392. #ifdef CONFIG_P2P
  393. if (p2p == NULL &&
  394. wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  395. /*
  396. * If it's a P2P specific interface, then don't update
  397. * the scan result without a P2P IE.
  398. */
  399. wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
  400. " update for P2P interface", MAC2STR(res->bssid));
  401. return;
  402. }
  403. #endif /* CONFIG_P2P */
  404. if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
  405. os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
  406. return; /* Skip P2P listen discovery results here */
  407. /* TODO: add option for ignoring BSSes we are not interested in
  408. * (to save memory) */
  409. bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
  410. if (bss == NULL)
  411. bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
  412. else
  413. bss = wpa_bss_update(wpa_s, bss, res);
  414. if (bss == NULL)
  415. return;
  416. if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
  417. struct wpa_bss **n;
  418. unsigned int siz;
  419. if (wpa_s->last_scan_res_size == 0)
  420. siz = 32;
  421. else
  422. siz = wpa_s->last_scan_res_size * 2;
  423. n = os_realloc_array(wpa_s->last_scan_res, siz,
  424. sizeof(struct wpa_bss *));
  425. if (n == NULL)
  426. return;
  427. wpa_s->last_scan_res = n;
  428. wpa_s->last_scan_res_size = siz;
  429. }
  430. wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
  431. }
  432. static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
  433. const struct scan_info *info)
  434. {
  435. int found;
  436. size_t i;
  437. if (info == NULL)
  438. return 1;
  439. if (info->num_freqs) {
  440. found = 0;
  441. for (i = 0; i < info->num_freqs; i++) {
  442. if (bss->freq == info->freqs[i]) {
  443. found = 1;
  444. break;
  445. }
  446. }
  447. if (!found)
  448. return 0;
  449. }
  450. if (info->num_ssids) {
  451. found = 0;
  452. for (i = 0; i < info->num_ssids; i++) {
  453. const struct wpa_driver_scan_ssid *s = &info->ssids[i];
  454. if ((s->ssid == NULL || s->ssid_len == 0) ||
  455. (s->ssid_len == bss->ssid_len &&
  456. os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
  457. 0)) {
  458. found = 1;
  459. break;
  460. }
  461. }
  462. if (!found)
  463. return 0;
  464. }
  465. return 1;
  466. }
  467. void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
  468. int new_scan)
  469. {
  470. struct wpa_bss *bss, *n;
  471. wpa_s->last_scan_full = 0;
  472. os_get_time(&wpa_s->last_scan);
  473. if (!new_scan)
  474. return; /* do not expire entries without new scan */
  475. if (info && !info->aborted && !info->freqs) {
  476. size_t i;
  477. if (info->num_ssids == 0) {
  478. wpa_s->last_scan_full = 1;
  479. } else {
  480. for (i = 0; i < info->num_ssids; i++) {
  481. if (info->ssids[i].ssid == NULL ||
  482. info->ssids[i].ssid_len == 0) {
  483. wpa_s->last_scan_full = 1;
  484. break;
  485. }
  486. }
  487. }
  488. }
  489. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  490. if (wpa_bss_in_use(wpa_s, bss))
  491. continue;
  492. if (!wpa_bss_included_in_scan(bss, info))
  493. continue; /* expire only BSSes that were scanned */
  494. if (bss->last_update_idx < wpa_s->bss_update_idx)
  495. bss->scan_miss_count++;
  496. if (bss->scan_miss_count >=
  497. wpa_s->conf->bss_expiration_scan_count) {
  498. wpa_bss_remove(wpa_s, bss, "no match in scan");
  499. }
  500. }
  501. wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u "
  502. "last_scan_full=%d",
  503. wpa_s->last_scan_res_used, wpa_s->last_scan_res_size,
  504. wpa_s->last_scan_full);
  505. }
  506. void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
  507. {
  508. struct wpa_bss *bss, *n;
  509. struct os_time t;
  510. if (dl_list_empty(&wpa_s->bss))
  511. return;
  512. os_get_time(&t);
  513. t.sec -= age;
  514. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  515. if (wpa_bss_in_use(wpa_s, bss))
  516. continue;
  517. if (os_time_before(&bss->last_update, &t)) {
  518. wpa_bss_remove(wpa_s, bss, __func__);
  519. } else
  520. break;
  521. }
  522. }
  523. static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
  524. {
  525. struct wpa_supplicant *wpa_s = eloop_ctx;
  526. wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
  527. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  528. wpa_bss_timeout, wpa_s, NULL);
  529. }
  530. int wpa_bss_init(struct wpa_supplicant *wpa_s)
  531. {
  532. dl_list_init(&wpa_s->bss);
  533. dl_list_init(&wpa_s->bss_id);
  534. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  535. wpa_bss_timeout, wpa_s, NULL);
  536. return 0;
  537. }
  538. void wpa_bss_flush(struct wpa_supplicant *wpa_s)
  539. {
  540. struct wpa_bss *bss, *n;
  541. if (wpa_s->bss.next == NULL)
  542. return; /* BSS table not yet initialized */
  543. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  544. if (wpa_bss_in_use(wpa_s, bss))
  545. continue;
  546. wpa_bss_remove(wpa_s, bss, __func__);
  547. }
  548. }
  549. void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
  550. {
  551. eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
  552. wpa_bss_flush(wpa_s);
  553. }
  554. struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
  555. const u8 *bssid)
  556. {
  557. struct wpa_bss *bss;
  558. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  559. return NULL;
  560. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  561. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  562. return bss;
  563. }
  564. return NULL;
  565. }
  566. #ifdef CONFIG_P2P
  567. struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
  568. const u8 *dev_addr)
  569. {
  570. struct wpa_bss *bss;
  571. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  572. u8 addr[ETH_ALEN];
  573. if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
  574. addr) == 0 &&
  575. os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
  576. return bss;
  577. }
  578. return NULL;
  579. }
  580. #endif /* CONFIG_P2P */
  581. struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  582. {
  583. struct wpa_bss *bss;
  584. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  585. if (bss->id == id)
  586. return bss;
  587. }
  588. return NULL;
  589. }
  590. const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
  591. {
  592. const u8 *end, *pos;
  593. pos = (const u8 *) (bss + 1);
  594. end = pos + bss->ie_len;
  595. while (pos + 1 < end) {
  596. if (pos + 2 + pos[1] > end)
  597. break;
  598. if (pos[0] == ie)
  599. return pos;
  600. pos += 2 + pos[1];
  601. }
  602. return NULL;
  603. }
  604. const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
  605. {
  606. const u8 *end, *pos;
  607. pos = (const u8 *) (bss + 1);
  608. end = pos + bss->ie_len;
  609. while (pos + 1 < end) {
  610. if (pos + 2 + pos[1] > end)
  611. break;
  612. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  613. vendor_type == WPA_GET_BE32(&pos[2]))
  614. return pos;
  615. pos += 2 + pos[1];
  616. }
  617. return NULL;
  618. }
  619. struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
  620. u32 vendor_type)
  621. {
  622. struct wpabuf *buf;
  623. const u8 *end, *pos;
  624. buf = wpabuf_alloc(bss->ie_len);
  625. if (buf == NULL)
  626. return NULL;
  627. pos = (const u8 *) (bss + 1);
  628. end = pos + bss->ie_len;
  629. while (pos + 1 < end) {
  630. if (pos + 2 + pos[1] > end)
  631. break;
  632. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  633. vendor_type == WPA_GET_BE32(&pos[2]))
  634. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  635. pos += 2 + pos[1];
  636. }
  637. if (wpabuf_len(buf) == 0) {
  638. wpabuf_free(buf);
  639. buf = NULL;
  640. }
  641. return buf;
  642. }
  643. struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
  644. u32 vendor_type)
  645. {
  646. struct wpabuf *buf;
  647. const u8 *end, *pos;
  648. buf = wpabuf_alloc(bss->beacon_ie_len);
  649. if (buf == NULL)
  650. return NULL;
  651. pos = (const u8 *) (bss + 1);
  652. pos += bss->ie_len;
  653. end = pos + bss->beacon_ie_len;
  654. while (pos + 1 < end) {
  655. if (pos + 2 + pos[1] > end)
  656. break;
  657. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  658. vendor_type == WPA_GET_BE32(&pos[2]))
  659. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  660. pos += 2 + pos[1];
  661. }
  662. if (wpabuf_len(buf) == 0) {
  663. wpabuf_free(buf);
  664. buf = NULL;
  665. }
  666. return buf;
  667. }
  668. int wpa_bss_get_max_rate(const struct wpa_bss *bss)
  669. {
  670. int rate = 0;
  671. const u8 *ie;
  672. int i;
  673. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  674. for (i = 0; ie && i < ie[1]; i++) {
  675. if ((ie[i + 2] & 0x7f) > rate)
  676. rate = ie[i + 2] & 0x7f;
  677. }
  678. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  679. for (i = 0; ie && i < ie[1]; i++) {
  680. if ((ie[i + 2] & 0x7f) > rate)
  681. rate = ie[i + 2] & 0x7f;
  682. }
  683. return rate;
  684. }
  685. int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
  686. {
  687. const u8 *ie, *ie2;
  688. int i, j;
  689. unsigned int len;
  690. u8 *r;
  691. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  692. ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  693. len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
  694. r = os_malloc(len);
  695. if (!r)
  696. return -1;
  697. for (i = 0; ie && i < ie[1]; i++)
  698. r[i] = ie[i + 2] & 0x7f;
  699. for (j = 0; ie2 && j < ie2[1]; j++)
  700. r[i + j] = ie2[j + 2] & 0x7f;
  701. *rates = r;
  702. return len;
  703. }