bss.c 21 KB

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