bss.c 32 KB

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