bss.c 33 KB

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