bss.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /*
  2. * BSS table
  3. * Copyright (c) 2009-2015, 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. /**
  46. * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
  47. * Returns: Allocated ANQP data structure or %NULL on failure
  48. *
  49. * The allocated ANQP data structure has its users count set to 1. It may be
  50. * shared by multiple BSS entries and each shared entry is freed with
  51. * wpa_bss_anqp_free().
  52. */
  53. struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
  54. {
  55. struct wpa_bss_anqp *anqp;
  56. anqp = os_zalloc(sizeof(*anqp));
  57. if (anqp == NULL)
  58. return NULL;
  59. anqp->users = 1;
  60. return anqp;
  61. }
  62. /**
  63. * wpa_bss_anqp_clone - Clone an ANQP data structure
  64. * @anqp: ANQP data structure from wpa_bss_anqp_alloc()
  65. * Returns: Cloned ANQP data structure or %NULL on failure
  66. */
  67. static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
  68. {
  69. struct wpa_bss_anqp *n;
  70. n = os_zalloc(sizeof(*n));
  71. if (n == NULL)
  72. return NULL;
  73. #define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
  74. #ifdef CONFIG_INTERWORKING
  75. ANQP_DUP(capability_list);
  76. ANQP_DUP(venue_name);
  77. ANQP_DUP(network_auth_type);
  78. ANQP_DUP(roaming_consortium);
  79. ANQP_DUP(ip_addr_type_availability);
  80. ANQP_DUP(nai_realm);
  81. ANQP_DUP(anqp_3gpp);
  82. ANQP_DUP(domain_name);
  83. #endif /* CONFIG_INTERWORKING */
  84. #ifdef CONFIG_HS20
  85. ANQP_DUP(hs20_capability_list);
  86. ANQP_DUP(hs20_operator_friendly_name);
  87. ANQP_DUP(hs20_wan_metrics);
  88. ANQP_DUP(hs20_connection_capability);
  89. ANQP_DUP(hs20_operating_class);
  90. ANQP_DUP(hs20_osu_providers_list);
  91. #endif /* CONFIG_HS20 */
  92. #undef ANQP_DUP
  93. return n;
  94. }
  95. /**
  96. * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
  97. * @bss: BSS entry
  98. * Returns: 0 on success, -1 on failure
  99. *
  100. * This function ensures the specific BSS entry has an ANQP data structure that
  101. * is not shared with any other BSS entry.
  102. */
  103. int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
  104. {
  105. struct wpa_bss_anqp *anqp;
  106. if (bss->anqp && bss->anqp->users > 1) {
  107. /* allocated, but shared - clone an unshared copy */
  108. anqp = wpa_bss_anqp_clone(bss->anqp);
  109. if (anqp == NULL)
  110. return -1;
  111. anqp->users = 1;
  112. bss->anqp->users--;
  113. bss->anqp = anqp;
  114. return 0;
  115. }
  116. if (bss->anqp)
  117. return 0; /* already allocated and not shared */
  118. /* not allocated - allocate a new storage area */
  119. bss->anqp = wpa_bss_anqp_alloc();
  120. return bss->anqp ? 0 : -1;
  121. }
  122. /**
  123. * wpa_bss_anqp_free - Free an ANQP data structure
  124. * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
  125. */
  126. static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
  127. {
  128. if (anqp == NULL)
  129. return;
  130. anqp->users--;
  131. if (anqp->users > 0) {
  132. /* Another BSS entry holds a pointer to this ANQP info */
  133. return;
  134. }
  135. #ifdef CONFIG_INTERWORKING
  136. wpabuf_free(anqp->capability_list);
  137. wpabuf_free(anqp->venue_name);
  138. wpabuf_free(anqp->network_auth_type);
  139. wpabuf_free(anqp->roaming_consortium);
  140. wpabuf_free(anqp->ip_addr_type_availability);
  141. wpabuf_free(anqp->nai_realm);
  142. wpabuf_free(anqp->anqp_3gpp);
  143. wpabuf_free(anqp->domain_name);
  144. #endif /* CONFIG_INTERWORKING */
  145. #ifdef CONFIG_HS20
  146. wpabuf_free(anqp->hs20_capability_list);
  147. wpabuf_free(anqp->hs20_operator_friendly_name);
  148. wpabuf_free(anqp->hs20_wan_metrics);
  149. wpabuf_free(anqp->hs20_connection_capability);
  150. wpabuf_free(anqp->hs20_operating_class);
  151. wpabuf_free(anqp->hs20_osu_providers_list);
  152. #endif /* CONFIG_HS20 */
  153. os_free(anqp);
  154. }
  155. static void wpa_bss_update_pending_connect(struct wpa_supplicant *wpa_s,
  156. struct wpa_bss *old_bss,
  157. struct wpa_bss *new_bss)
  158. {
  159. struct wpa_radio_work *work;
  160. struct wpa_connect_work *cwork;
  161. work = radio_work_pending(wpa_s, "sme-connect");
  162. if (!work)
  163. work = radio_work_pending(wpa_s, "connect");
  164. if (!work)
  165. return;
  166. cwork = work->ctx;
  167. if (cwork->bss != old_bss)
  168. return;
  169. wpa_printf(MSG_DEBUG,
  170. "Update BSS pointer for the pending connect radio work");
  171. cwork->bss = new_bss;
  172. if (!new_bss)
  173. cwork->bss_removed = 1;
  174. }
  175. static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  176. const char *reason)
  177. {
  178. if (wpa_s->last_scan_res) {
  179. unsigned int i;
  180. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  181. if (wpa_s->last_scan_res[i] == bss) {
  182. os_memmove(&wpa_s->last_scan_res[i],
  183. &wpa_s->last_scan_res[i + 1],
  184. (wpa_s->last_scan_res_used - i - 1)
  185. * sizeof(struct wpa_bss *));
  186. wpa_s->last_scan_res_used--;
  187. break;
  188. }
  189. }
  190. }
  191. wpa_bss_update_pending_connect(wpa_s, bss, NULL);
  192. dl_list_del(&bss->list);
  193. dl_list_del(&bss->list_id);
  194. wpa_s->num_bss--;
  195. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
  196. " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
  197. wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
  198. wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
  199. wpa_bss_anqp_free(bss->anqp);
  200. os_free(bss);
  201. }
  202. /**
  203. * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
  204. * @wpa_s: Pointer to wpa_supplicant data
  205. * @bssid: BSSID
  206. * @ssid: SSID
  207. * @ssid_len: Length of @ssid
  208. * Returns: Pointer to the BSS entry or %NULL if not found
  209. */
  210. struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
  211. const u8 *ssid, size_t ssid_len)
  212. {
  213. struct wpa_bss *bss;
  214. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  215. return NULL;
  216. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  217. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
  218. bss->ssid_len == ssid_len &&
  219. os_memcmp(bss->ssid, ssid, ssid_len) == 0)
  220. return bss;
  221. }
  222. return NULL;
  223. }
  224. static void calculate_update_time(const struct os_reltime *fetch_time,
  225. unsigned int age_ms,
  226. struct os_reltime *update_time)
  227. {
  228. os_time_t usec;
  229. update_time->sec = fetch_time->sec;
  230. update_time->usec = fetch_time->usec;
  231. update_time->sec -= age_ms / 1000;
  232. usec = (age_ms % 1000) * 1000;
  233. if (update_time->usec < usec) {
  234. update_time->sec--;
  235. update_time->usec += 1000000;
  236. }
  237. update_time->usec -= usec;
  238. }
  239. static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
  240. struct os_reltime *fetch_time)
  241. {
  242. dst->flags = src->flags;
  243. os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
  244. dst->freq = src->freq;
  245. dst->beacon_int = src->beacon_int;
  246. dst->caps = src->caps;
  247. dst->qual = src->qual;
  248. dst->noise = src->noise;
  249. dst->level = src->level;
  250. dst->tsf = src->tsf;
  251. dst->est_throughput = src->est_throughput;
  252. dst->snr = src->snr;
  253. calculate_update_time(fetch_time, src->age, &dst->last_update);
  254. }
  255. static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  256. {
  257. struct wpa_ssid *ssid;
  258. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  259. if (ssid->ssid == NULL || ssid->ssid_len == 0)
  260. continue;
  261. if (ssid->ssid_len == bss->ssid_len &&
  262. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
  263. return 1;
  264. }
  265. return 0;
  266. }
  267. static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  268. {
  269. return bss == wpa_s->current_bss ||
  270. (!is_zero_ether_addr(bss->bssid) &&
  271. (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
  272. os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0));
  273. }
  274. static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
  275. {
  276. struct wpa_bss *bss;
  277. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  278. if (!wpa_bss_known(wpa_s, bss)) {
  279. wpa_bss_remove(wpa_s, bss, __func__);
  280. return 0;
  281. }
  282. }
  283. return -1;
  284. }
  285. static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
  286. {
  287. struct wpa_bss *bss;
  288. /*
  289. * Remove the oldest entry that does not match with any configured
  290. * network.
  291. */
  292. if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
  293. return 0;
  294. /*
  295. * Remove the oldest entry that isn't currently in use.
  296. */
  297. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  298. if (!wpa_bss_in_use(wpa_s, bss)) {
  299. wpa_bss_remove(wpa_s, bss, __func__);
  300. return 0;
  301. }
  302. }
  303. return -1;
  304. }
  305. static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
  306. const u8 *ssid, size_t ssid_len,
  307. struct wpa_scan_res *res,
  308. struct os_reltime *fetch_time)
  309. {
  310. struct wpa_bss *bss;
  311. bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
  312. if (bss == NULL)
  313. return NULL;
  314. bss->id = wpa_s->bss_next_id++;
  315. bss->last_update_idx = wpa_s->bss_update_idx;
  316. wpa_bss_copy_res(bss, res, fetch_time);
  317. os_memcpy(bss->ssid, ssid, ssid_len);
  318. bss->ssid_len = ssid_len;
  319. bss->ie_len = res->ie_len;
  320. bss->beacon_ie_len = res->beacon_ie_len;
  321. os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
  322. wpa_bss_set_hessid(bss);
  323. if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
  324. wpa_bss_remove_oldest(wpa_s) != 0) {
  325. wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
  326. "because all BSSes are in use. We should normally "
  327. "not get here!", (int) wpa_s->num_bss + 1);
  328. wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
  329. }
  330. dl_list_add_tail(&wpa_s->bss, &bss->list);
  331. dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
  332. wpa_s->num_bss++;
  333. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
  334. " SSID '%s'",
  335. bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
  336. wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
  337. return bss;
  338. }
  339. static int are_ies_equal(const struct wpa_bss *old,
  340. const struct wpa_scan_res *new, u32 ie)
  341. {
  342. const u8 *old_ie, *new_ie;
  343. struct wpabuf *old_ie_buff = NULL;
  344. struct wpabuf *new_ie_buff = NULL;
  345. int new_ie_len, old_ie_len, ret, is_multi;
  346. switch (ie) {
  347. case WPA_IE_VENDOR_TYPE:
  348. old_ie = wpa_bss_get_vendor_ie(old, ie);
  349. new_ie = wpa_scan_get_vendor_ie(new, ie);
  350. is_multi = 0;
  351. break;
  352. case WPS_IE_VENDOR_TYPE:
  353. old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
  354. new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
  355. is_multi = 1;
  356. break;
  357. case WLAN_EID_RSN:
  358. case WLAN_EID_SUPP_RATES:
  359. case WLAN_EID_EXT_SUPP_RATES:
  360. old_ie = wpa_bss_get_ie(old, ie);
  361. new_ie = wpa_scan_get_ie(new, ie);
  362. is_multi = 0;
  363. break;
  364. default:
  365. wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
  366. return 0;
  367. }
  368. if (is_multi) {
  369. /* in case of multiple IEs stored in buffer */
  370. old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
  371. new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
  372. old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
  373. new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
  374. } else {
  375. /* in case of single IE */
  376. old_ie_len = old_ie ? old_ie[1] + 2 : 0;
  377. new_ie_len = new_ie ? new_ie[1] + 2 : 0;
  378. }
  379. if (!old_ie || !new_ie)
  380. ret = !old_ie && !new_ie;
  381. else
  382. ret = (old_ie_len == new_ie_len &&
  383. os_memcmp(old_ie, new_ie, old_ie_len) == 0);
  384. wpabuf_free(old_ie_buff);
  385. wpabuf_free(new_ie_buff);
  386. return ret;
  387. }
  388. static u32 wpa_bss_compare_res(const struct wpa_bss *old,
  389. const struct wpa_scan_res *new)
  390. {
  391. u32 changes = 0;
  392. int caps_diff = old->caps ^ new->caps;
  393. if (old->freq != new->freq)
  394. changes |= WPA_BSS_FREQ_CHANGED_FLAG;
  395. if (old->level != new->level)
  396. changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
  397. if (caps_diff & IEEE80211_CAP_PRIVACY)
  398. changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
  399. if (caps_diff & IEEE80211_CAP_IBSS)
  400. changes |= WPA_BSS_MODE_CHANGED_FLAG;
  401. if (old->ie_len == new->ie_len &&
  402. os_memcmp(old + 1, new + 1, old->ie_len) == 0)
  403. return changes;
  404. changes |= WPA_BSS_IES_CHANGED_FLAG;
  405. if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
  406. changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
  407. if (!are_ies_equal(old, new, WLAN_EID_RSN))
  408. changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
  409. if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
  410. changes |= WPA_BSS_WPS_CHANGED_FLAG;
  411. if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
  412. !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
  413. changes |= WPA_BSS_RATES_CHANGED_FLAG;
  414. return changes;
  415. }
  416. static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
  417. const struct wpa_bss *bss)
  418. {
  419. if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
  420. wpas_notify_bss_freq_changed(wpa_s, bss->id);
  421. if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
  422. wpas_notify_bss_signal_changed(wpa_s, bss->id);
  423. if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
  424. wpas_notify_bss_privacy_changed(wpa_s, bss->id);
  425. if (changes & WPA_BSS_MODE_CHANGED_FLAG)
  426. wpas_notify_bss_mode_changed(wpa_s, bss->id);
  427. if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
  428. wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
  429. if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
  430. wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
  431. if (changes & WPA_BSS_WPS_CHANGED_FLAG)
  432. wpas_notify_bss_wps_changed(wpa_s, bss->id);
  433. if (changes & WPA_BSS_IES_CHANGED_FLAG)
  434. wpas_notify_bss_ies_changed(wpa_s, bss->id);
  435. if (changes & WPA_BSS_RATES_CHANGED_FLAG)
  436. wpas_notify_bss_rates_changed(wpa_s, bss->id);
  437. wpas_notify_bss_seen(wpa_s, bss->id);
  438. }
  439. static struct wpa_bss *
  440. wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  441. struct wpa_scan_res *res, struct os_reltime *fetch_time)
  442. {
  443. u32 changes;
  444. changes = wpa_bss_compare_res(bss, res);
  445. bss->scan_miss_count = 0;
  446. bss->last_update_idx = wpa_s->bss_update_idx;
  447. wpa_bss_copy_res(bss, res, fetch_time);
  448. /* Move the entry to the end of the list */
  449. dl_list_del(&bss->list);
  450. #ifdef CONFIG_P2P
  451. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  452. !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
  453. /*
  454. * This can happen when non-P2P station interface runs a scan
  455. * without P2P IE in the Probe Request frame. P2P GO would reply
  456. * to that with a Probe Response that does not include P2P IE.
  457. * Do not update the IEs in this BSS entry to avoid such loss of
  458. * information that may be needed for P2P operations to
  459. * determine group information.
  460. */
  461. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
  462. MACSTR " since that would remove P2P IE information",
  463. MAC2STR(bss->bssid));
  464. } else
  465. #endif /* CONFIG_P2P */
  466. if (bss->ie_len + bss->beacon_ie_len >=
  467. res->ie_len + res->beacon_ie_len) {
  468. os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
  469. bss->ie_len = res->ie_len;
  470. bss->beacon_ie_len = res->beacon_ie_len;
  471. } else {
  472. struct wpa_bss *nbss;
  473. struct dl_list *prev = bss->list_id.prev;
  474. dl_list_del(&bss->list_id);
  475. nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
  476. res->beacon_ie_len);
  477. if (nbss) {
  478. unsigned int i;
  479. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  480. if (wpa_s->last_scan_res[i] == bss) {
  481. wpa_s->last_scan_res[i] = nbss;
  482. break;
  483. }
  484. }
  485. if (wpa_s->current_bss == bss)
  486. wpa_s->current_bss = nbss;
  487. wpa_bss_update_pending_connect(wpa_s, bss, nbss);
  488. bss = nbss;
  489. os_memcpy(bss + 1, res + 1,
  490. res->ie_len + res->beacon_ie_len);
  491. bss->ie_len = res->ie_len;
  492. bss->beacon_ie_len = res->beacon_ie_len;
  493. }
  494. dl_list_add(prev, &bss->list_id);
  495. }
  496. if (changes & WPA_BSS_IES_CHANGED_FLAG)
  497. wpa_bss_set_hessid(bss);
  498. dl_list_add_tail(&wpa_s->bss, &bss->list);
  499. notify_bss_changes(wpa_s, changes, bss);
  500. return bss;
  501. }
  502. /**
  503. * wpa_bss_update_start - Start a BSS table update from scan results
  504. * @wpa_s: Pointer to wpa_supplicant data
  505. *
  506. * This function is called at the start of each BSS table update round for new
  507. * scan results. The actual scan result entries are indicated with calls to
  508. * wpa_bss_update_scan_res() and the update round is finished with a call to
  509. * wpa_bss_update_end().
  510. */
  511. void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
  512. {
  513. wpa_s->bss_update_idx++;
  514. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
  515. wpa_s->bss_update_idx);
  516. wpa_s->last_scan_res_used = 0;
  517. }
  518. /**
  519. * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
  520. * @wpa_s: Pointer to wpa_supplicant data
  521. * @res: Scan result
  522. * @fetch_time: Time when the result was fetched from the driver
  523. *
  524. * This function updates a BSS table entry (or adds one) based on a scan result.
  525. * This is called separately for each scan result between the calls to
  526. * wpa_bss_update_start() and wpa_bss_update_end().
  527. */
  528. void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
  529. struct wpa_scan_res *res,
  530. struct os_reltime *fetch_time)
  531. {
  532. const u8 *ssid, *p2p, *mesh;
  533. struct wpa_bss *bss;
  534. if (wpa_s->conf->ignore_old_scan_res) {
  535. struct os_reltime update;
  536. calculate_update_time(fetch_time, res->age, &update);
  537. if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
  538. struct os_reltime age;
  539. os_reltime_sub(&wpa_s->scan_trigger_time, &update,
  540. &age);
  541. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
  542. "table entry that is %u.%06u seconds older "
  543. "than our scan trigger",
  544. (unsigned int) age.sec,
  545. (unsigned int) age.usec);
  546. return;
  547. }
  548. }
  549. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  550. if (ssid == NULL) {
  551. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
  552. MACSTR, MAC2STR(res->bssid));
  553. return;
  554. }
  555. if (ssid[1] > SSID_MAX_LEN) {
  556. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
  557. MACSTR, MAC2STR(res->bssid));
  558. return;
  559. }
  560. p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
  561. #ifdef CONFIG_P2P
  562. if (p2p == NULL &&
  563. wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  564. /*
  565. * If it's a P2P specific interface, then don't update
  566. * the scan result without a P2P IE.
  567. */
  568. wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
  569. " update for P2P interface", MAC2STR(res->bssid));
  570. return;
  571. }
  572. #endif /* CONFIG_P2P */
  573. if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
  574. os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
  575. return; /* Skip P2P listen discovery results here */
  576. /* TODO: add option for ignoring BSSes we are not interested in
  577. * (to save memory) */
  578. mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
  579. if (mesh && mesh[1] <= SSID_MAX_LEN)
  580. ssid = mesh;
  581. bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
  582. if (bss == NULL)
  583. bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
  584. else {
  585. bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
  586. if (wpa_s->last_scan_res) {
  587. unsigned int i;
  588. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  589. if (bss == wpa_s->last_scan_res[i]) {
  590. /* Already in the list */
  591. return;
  592. }
  593. }
  594. }
  595. }
  596. if (bss == NULL)
  597. return;
  598. if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
  599. struct wpa_bss **n;
  600. unsigned int siz;
  601. if (wpa_s->last_scan_res_size == 0)
  602. siz = 32;
  603. else
  604. siz = wpa_s->last_scan_res_size * 2;
  605. n = os_realloc_array(wpa_s->last_scan_res, siz,
  606. sizeof(struct wpa_bss *));
  607. if (n == NULL)
  608. return;
  609. wpa_s->last_scan_res = n;
  610. wpa_s->last_scan_res_size = siz;
  611. }
  612. if (wpa_s->last_scan_res)
  613. wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
  614. }
  615. static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
  616. const struct scan_info *info)
  617. {
  618. int found;
  619. size_t i;
  620. if (info == NULL)
  621. return 1;
  622. if (info->num_freqs) {
  623. found = 0;
  624. for (i = 0; i < info->num_freqs; i++) {
  625. if (bss->freq == info->freqs[i]) {
  626. found = 1;
  627. break;
  628. }
  629. }
  630. if (!found)
  631. return 0;
  632. }
  633. if (info->num_ssids) {
  634. found = 0;
  635. for (i = 0; i < info->num_ssids; i++) {
  636. const struct wpa_driver_scan_ssid *s = &info->ssids[i];
  637. if ((s->ssid == NULL || s->ssid_len == 0) ||
  638. (s->ssid_len == bss->ssid_len &&
  639. os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
  640. 0)) {
  641. found = 1;
  642. break;
  643. }
  644. }
  645. if (!found)
  646. return 0;
  647. }
  648. return 1;
  649. }
  650. /**
  651. * wpa_bss_update_end - End a BSS table update from scan results
  652. * @wpa_s: Pointer to wpa_supplicant data
  653. * @info: Information about scan parameters
  654. * @new_scan: Whether this update round was based on a new scan
  655. *
  656. * This function is called at the end of each BSS table update round for new
  657. * scan results. The start of the update was indicated with a call to
  658. * wpa_bss_update_start().
  659. */
  660. void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
  661. int new_scan)
  662. {
  663. struct wpa_bss *bss, *n;
  664. os_get_reltime(&wpa_s->last_scan);
  665. if (!new_scan)
  666. return; /* do not expire entries without new scan */
  667. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  668. if (wpa_bss_in_use(wpa_s, bss))
  669. continue;
  670. if (!wpa_bss_included_in_scan(bss, info))
  671. continue; /* expire only BSSes that were scanned */
  672. if (bss->last_update_idx < wpa_s->bss_update_idx)
  673. bss->scan_miss_count++;
  674. if (bss->scan_miss_count >=
  675. wpa_s->conf->bss_expiration_scan_count) {
  676. wpa_bss_remove(wpa_s, bss, "no match in scan");
  677. }
  678. }
  679. wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
  680. wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
  681. }
  682. /**
  683. * wpa_bss_flush_by_age - Flush old BSS entries
  684. * @wpa_s: Pointer to wpa_supplicant data
  685. * @age: Maximum entry age in seconds
  686. *
  687. * Remove BSS entries that have not been updated during the last @age seconds.
  688. */
  689. void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
  690. {
  691. struct wpa_bss *bss, *n;
  692. struct os_reltime t;
  693. if (dl_list_empty(&wpa_s->bss))
  694. return;
  695. os_get_reltime(&t);
  696. t.sec -= age;
  697. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  698. if (wpa_bss_in_use(wpa_s, bss))
  699. continue;
  700. if (os_reltime_before(&bss->last_update, &t)) {
  701. wpa_bss_remove(wpa_s, bss, __func__);
  702. } else
  703. break;
  704. }
  705. }
  706. static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
  707. {
  708. struct wpa_supplicant *wpa_s = eloop_ctx;
  709. wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
  710. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  711. wpa_bss_timeout, wpa_s, NULL);
  712. }
  713. /**
  714. * wpa_bss_init - Initialize BSS table
  715. * @wpa_s: Pointer to wpa_supplicant data
  716. * Returns: 0 on success, -1 on failure
  717. *
  718. * This prepares BSS table lists and timer for periodic updates. The BSS table
  719. * is deinitialized with wpa_bss_deinit() once not needed anymore.
  720. */
  721. int wpa_bss_init(struct wpa_supplicant *wpa_s)
  722. {
  723. dl_list_init(&wpa_s->bss);
  724. dl_list_init(&wpa_s->bss_id);
  725. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  726. wpa_bss_timeout, wpa_s, NULL);
  727. return 0;
  728. }
  729. /**
  730. * wpa_bss_flush - Flush all unused BSS entries
  731. * @wpa_s: Pointer to wpa_supplicant data
  732. */
  733. void wpa_bss_flush(struct wpa_supplicant *wpa_s)
  734. {
  735. struct wpa_bss *bss, *n;
  736. wpa_s->clear_driver_scan_cache = 1;
  737. if (wpa_s->bss.next == NULL)
  738. return; /* BSS table not yet initialized */
  739. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  740. if (wpa_bss_in_use(wpa_s, bss))
  741. continue;
  742. wpa_bss_remove(wpa_s, bss, __func__);
  743. }
  744. }
  745. /**
  746. * wpa_bss_deinit - Deinitialize BSS table
  747. * @wpa_s: Pointer to wpa_supplicant data
  748. */
  749. void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
  750. {
  751. eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
  752. wpa_bss_flush(wpa_s);
  753. }
  754. /**
  755. * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
  756. * @wpa_s: Pointer to wpa_supplicant data
  757. * @bssid: BSSID
  758. * Returns: Pointer to the BSS entry or %NULL if not found
  759. */
  760. struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
  761. const u8 *bssid)
  762. {
  763. struct wpa_bss *bss;
  764. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  765. return NULL;
  766. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  767. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  768. return bss;
  769. }
  770. return NULL;
  771. }
  772. /**
  773. * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
  774. * @wpa_s: Pointer to wpa_supplicant data
  775. * @bssid: BSSID
  776. * Returns: Pointer to the BSS entry or %NULL if not found
  777. *
  778. * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
  779. * find the entry that has the most recent update. This can help in finding the
  780. * correct entry in cases where the SSID of the AP may have changed recently
  781. * (e.g., in WPS reconfiguration cases).
  782. */
  783. struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
  784. const u8 *bssid)
  785. {
  786. struct wpa_bss *bss, *found = NULL;
  787. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  788. return NULL;
  789. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  790. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
  791. continue;
  792. if (found == NULL ||
  793. os_reltime_before(&found->last_update, &bss->last_update))
  794. found = bss;
  795. }
  796. return found;
  797. }
  798. #ifdef CONFIG_P2P
  799. /**
  800. * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
  801. * @wpa_s: Pointer to wpa_supplicant data
  802. * @dev_addr: P2P Device Address of the GO
  803. * Returns: Pointer to the BSS entry or %NULL if not found
  804. */
  805. struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
  806. const u8 *dev_addr)
  807. {
  808. struct wpa_bss *bss;
  809. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  810. u8 addr[ETH_ALEN];
  811. if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
  812. addr) == 0 &&
  813. os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
  814. return bss;
  815. }
  816. return NULL;
  817. }
  818. #endif /* CONFIG_P2P */
  819. /**
  820. * wpa_bss_get_id - Fetch a BSS table entry based on identifier
  821. * @wpa_s: Pointer to wpa_supplicant data
  822. * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
  823. * Returns: Pointer to the BSS entry or %NULL if not found
  824. */
  825. struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  826. {
  827. struct wpa_bss *bss;
  828. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  829. if (bss->id == id)
  830. return bss;
  831. }
  832. return NULL;
  833. }
  834. /**
  835. * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
  836. * @wpa_s: Pointer to wpa_supplicant data
  837. * @idf: Smallest allowed identifier assigned for the entry
  838. * @idf: Largest allowed identifier assigned for the entry
  839. * Returns: Pointer to the BSS entry or %NULL if not found
  840. *
  841. * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
  842. * smallest id value to be fetched within the specified range without the
  843. * caller having to know the exact id.
  844. */
  845. struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
  846. unsigned int idf, unsigned int idl)
  847. {
  848. struct wpa_bss *bss;
  849. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  850. if (bss->id >= idf && bss->id <= idl)
  851. return bss;
  852. }
  853. return NULL;
  854. }
  855. /**
  856. * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
  857. * @bss: BSS table entry
  858. * @ie: Information element identitifier (WLAN_EID_*)
  859. * Returns: Pointer to the information element (id field) or %NULL if not found
  860. *
  861. * This function returns the first matching information element in the BSS
  862. * entry.
  863. */
  864. const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
  865. {
  866. const u8 *end, *pos;
  867. pos = (const u8 *) (bss + 1);
  868. end = pos + bss->ie_len;
  869. while (pos + 1 < end) {
  870. if (pos + 2 + pos[1] > end)
  871. break;
  872. if (pos[0] == ie)
  873. return pos;
  874. pos += 2 + pos[1];
  875. }
  876. return NULL;
  877. }
  878. /**
  879. * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
  880. * @bss: BSS table entry
  881. * @vendor_type: Vendor type (four octets starting the IE payload)
  882. * Returns: Pointer to the information element (id field) or %NULL if not found
  883. *
  884. * This function returns the first matching information element in the BSS
  885. * entry.
  886. */
  887. const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
  888. {
  889. const u8 *end, *pos;
  890. pos = (const u8 *) (bss + 1);
  891. end = pos + bss->ie_len;
  892. while (pos + 1 < end) {
  893. if (pos + 2 + pos[1] > end)
  894. break;
  895. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  896. vendor_type == WPA_GET_BE32(&pos[2]))
  897. return pos;
  898. pos += 2 + pos[1];
  899. }
  900. return NULL;
  901. }
  902. /**
  903. * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
  904. * @bss: BSS table entry
  905. * @vendor_type: Vendor type (four octets starting the IE payload)
  906. * Returns: Pointer to the information element (id field) or %NULL if not found
  907. *
  908. * This function returns the first matching information element in the BSS
  909. * entry.
  910. *
  911. * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
  912. * from Beacon frames instead of either Beacon or Probe Response frames.
  913. */
  914. const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
  915. u32 vendor_type)
  916. {
  917. const u8 *end, *pos;
  918. if (bss->beacon_ie_len == 0)
  919. return NULL;
  920. pos = (const u8 *) (bss + 1);
  921. pos += bss->ie_len;
  922. end = pos + bss->beacon_ie_len;
  923. while (pos + 1 < end) {
  924. if (pos + 2 + pos[1] > end)
  925. break;
  926. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  927. vendor_type == WPA_GET_BE32(&pos[2]))
  928. return pos;
  929. pos += 2 + pos[1];
  930. }
  931. return NULL;
  932. }
  933. /**
  934. * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
  935. * @bss: BSS table entry
  936. * @vendor_type: Vendor type (four octets starting the IE payload)
  937. * Returns: Pointer to the information element payload or %NULL if not found
  938. *
  939. * This function returns concatenated payload of possibly fragmented vendor
  940. * specific information elements in the BSS entry. The caller is responsible for
  941. * freeing the returned buffer.
  942. */
  943. struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
  944. u32 vendor_type)
  945. {
  946. struct wpabuf *buf;
  947. const u8 *end, *pos;
  948. buf = wpabuf_alloc(bss->ie_len);
  949. if (buf == NULL)
  950. return NULL;
  951. pos = (const u8 *) (bss + 1);
  952. end = pos + bss->ie_len;
  953. while (pos + 1 < end) {
  954. if (pos + 2 + pos[1] > end)
  955. break;
  956. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  957. vendor_type == WPA_GET_BE32(&pos[2]))
  958. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  959. pos += 2 + pos[1];
  960. }
  961. if (wpabuf_len(buf) == 0) {
  962. wpabuf_free(buf);
  963. buf = NULL;
  964. }
  965. return buf;
  966. }
  967. /**
  968. * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
  969. * @bss: BSS table entry
  970. * @vendor_type: Vendor type (four octets starting the IE payload)
  971. * Returns: Pointer to the information element payload or %NULL if not found
  972. *
  973. * This function returns concatenated payload of possibly fragmented vendor
  974. * specific information elements in the BSS entry. The caller is responsible for
  975. * freeing the returned buffer.
  976. *
  977. * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
  978. * from Beacon frames instead of either Beacon or Probe Response frames.
  979. */
  980. struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
  981. u32 vendor_type)
  982. {
  983. struct wpabuf *buf;
  984. const u8 *end, *pos;
  985. buf = wpabuf_alloc(bss->beacon_ie_len);
  986. if (buf == NULL)
  987. return NULL;
  988. pos = (const u8 *) (bss + 1);
  989. pos += bss->ie_len;
  990. end = pos + bss->beacon_ie_len;
  991. while (pos + 1 < end) {
  992. if (pos + 2 + pos[1] > end)
  993. break;
  994. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  995. vendor_type == WPA_GET_BE32(&pos[2]))
  996. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  997. pos += 2 + pos[1];
  998. }
  999. if (wpabuf_len(buf) == 0) {
  1000. wpabuf_free(buf);
  1001. buf = NULL;
  1002. }
  1003. return buf;
  1004. }
  1005. /**
  1006. * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
  1007. * @bss: BSS table entry
  1008. * Returns: Maximum legacy rate in units of 500 kbps
  1009. */
  1010. int wpa_bss_get_max_rate(const struct wpa_bss *bss)
  1011. {
  1012. int rate = 0;
  1013. const u8 *ie;
  1014. int i;
  1015. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  1016. for (i = 0; ie && i < ie[1]; i++) {
  1017. if ((ie[i + 2] & 0x7f) > rate)
  1018. rate = ie[i + 2] & 0x7f;
  1019. }
  1020. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  1021. for (i = 0; ie && i < ie[1]; i++) {
  1022. if ((ie[i + 2] & 0x7f) > rate)
  1023. rate = ie[i + 2] & 0x7f;
  1024. }
  1025. return rate;
  1026. }
  1027. /**
  1028. * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
  1029. * @bss: BSS table entry
  1030. * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
  1031. * Returns: number of legacy TX rates or -1 on failure
  1032. *
  1033. * The caller is responsible for freeing the returned buffer with os_free() in
  1034. * case of success.
  1035. */
  1036. int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
  1037. {
  1038. const u8 *ie, *ie2;
  1039. int i, j;
  1040. unsigned int len;
  1041. u8 *r;
  1042. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  1043. ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  1044. len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
  1045. r = os_malloc(len);
  1046. if (!r)
  1047. return -1;
  1048. for (i = 0; ie && i < ie[1]; i++)
  1049. r[i] = ie[i + 2] & 0x7f;
  1050. for (j = 0; ie2 && j < ie2[1]; j++)
  1051. r[i + j] = ie2[j + 2] & 0x7f;
  1052. *rates = r;
  1053. return len;
  1054. }