bss.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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. 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. if (bss->last_update_idx == wpa_s->bss_update_idx) {
  497. struct os_reltime update_time;
  498. /*
  499. * Some drivers (e.g., cfg80211) include multiple BSS entries
  500. * for the same BSS if that BSS's channel changes. The BSS list
  501. * implementation in wpa_supplicant does not do that and we need
  502. * to filter out the obsolete results here to make sure only the
  503. * most current BSS information remains in the table.
  504. */
  505. wpa_printf(MSG_DEBUG, "BSS: " MACSTR
  506. " has multiple entries in the scan results - select the most current one",
  507. MAC2STR(bss->bssid));
  508. calculate_update_time(fetch_time, res->age, &update_time);
  509. wpa_printf(MSG_DEBUG,
  510. "Previous last_update: %u.%06u (freq %d%s)",
  511. (unsigned int) bss->last_update.sec,
  512. (unsigned int) bss->last_update.usec,
  513. bss->freq,
  514. (bss->flags & WPA_BSS_ASSOCIATED) ? " assoc" : "");
  515. wpa_printf(MSG_DEBUG, "New last_update: %u.%06u (freq %d%s)",
  516. (unsigned int) update_time.sec,
  517. (unsigned int) update_time.usec,
  518. res->freq,
  519. (res->flags & WPA_SCAN_ASSOCIATED) ? " assoc" : "");
  520. if ((bss->flags & WPA_BSS_ASSOCIATED) ||
  521. (!(res->flags & WPA_SCAN_ASSOCIATED) &&
  522. !os_reltime_before(&bss->last_update, &update_time))) {
  523. wpa_printf(MSG_DEBUG,
  524. "Ignore this BSS entry since the previous update looks more current");
  525. return bss;
  526. }
  527. wpa_printf(MSG_DEBUG,
  528. "Accept this BSS entry since it looks more current than the previous update");
  529. }
  530. changes = wpa_bss_compare_res(bss, res);
  531. if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
  532. wpa_printf(MSG_DEBUG, "BSS: " MACSTR " changed freq %d --> %d",
  533. MAC2STR(bss->bssid), bss->freq, res->freq);
  534. bss->scan_miss_count = 0;
  535. bss->last_update_idx = wpa_s->bss_update_idx;
  536. wpa_bss_copy_res(bss, res, fetch_time);
  537. /* Move the entry to the end of the list */
  538. dl_list_del(&bss->list);
  539. #ifdef CONFIG_P2P
  540. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  541. !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
  542. /*
  543. * This can happen when non-P2P station interface runs a scan
  544. * without P2P IE in the Probe Request frame. P2P GO would reply
  545. * to that with a Probe Response that does not include P2P IE.
  546. * Do not update the IEs in this BSS entry to avoid such loss of
  547. * information that may be needed for P2P operations to
  548. * determine group information.
  549. */
  550. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
  551. MACSTR " since that would remove P2P IE information",
  552. MAC2STR(bss->bssid));
  553. } else
  554. #endif /* CONFIG_P2P */
  555. if (bss->ie_len + bss->beacon_ie_len >=
  556. res->ie_len + res->beacon_ie_len) {
  557. os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
  558. bss->ie_len = res->ie_len;
  559. bss->beacon_ie_len = res->beacon_ie_len;
  560. } else {
  561. struct wpa_bss *nbss;
  562. struct dl_list *prev = bss->list_id.prev;
  563. dl_list_del(&bss->list_id);
  564. nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
  565. res->beacon_ie_len);
  566. if (nbss) {
  567. unsigned int i;
  568. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  569. if (wpa_s->last_scan_res[i] == bss) {
  570. wpa_s->last_scan_res[i] = nbss;
  571. break;
  572. }
  573. }
  574. if (wpa_s->current_bss == bss)
  575. wpa_s->current_bss = nbss;
  576. wpa_bss_update_pending_connect(wpa_s, bss, nbss);
  577. bss = nbss;
  578. os_memcpy(bss + 1, res + 1,
  579. res->ie_len + res->beacon_ie_len);
  580. bss->ie_len = res->ie_len;
  581. bss->beacon_ie_len = res->beacon_ie_len;
  582. }
  583. dl_list_add(prev, &bss->list_id);
  584. }
  585. if (changes & WPA_BSS_IES_CHANGED_FLAG)
  586. wpa_bss_set_hessid(bss);
  587. dl_list_add_tail(&wpa_s->bss, &bss->list);
  588. notify_bss_changes(wpa_s, changes, bss);
  589. return bss;
  590. }
  591. /**
  592. * wpa_bss_update_start - Start a BSS table update from scan results
  593. * @wpa_s: Pointer to wpa_supplicant data
  594. *
  595. * This function is called at the start of each BSS table update round for new
  596. * scan results. The actual scan result entries are indicated with calls to
  597. * wpa_bss_update_scan_res() and the update round is finished with a call to
  598. * wpa_bss_update_end().
  599. */
  600. void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
  601. {
  602. wpa_s->bss_update_idx++;
  603. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
  604. wpa_s->bss_update_idx);
  605. wpa_s->last_scan_res_used = 0;
  606. }
  607. /**
  608. * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
  609. * @wpa_s: Pointer to wpa_supplicant data
  610. * @res: Scan result
  611. * @fetch_time: Time when the result was fetched from the driver
  612. *
  613. * This function updates a BSS table entry (or adds one) based on a scan result.
  614. * This is called separately for each scan result between the calls to
  615. * wpa_bss_update_start() and wpa_bss_update_end().
  616. */
  617. void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
  618. struct wpa_scan_res *res,
  619. struct os_reltime *fetch_time)
  620. {
  621. const u8 *ssid, *p2p, *mesh;
  622. struct wpa_bss *bss;
  623. if (wpa_s->conf->ignore_old_scan_res) {
  624. struct os_reltime update;
  625. calculate_update_time(fetch_time, res->age, &update);
  626. if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
  627. struct os_reltime age;
  628. os_reltime_sub(&wpa_s->scan_trigger_time, &update,
  629. &age);
  630. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
  631. "table entry that is %u.%06u seconds older "
  632. "than our scan trigger",
  633. (unsigned int) age.sec,
  634. (unsigned int) age.usec);
  635. return;
  636. }
  637. }
  638. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  639. if (ssid == NULL) {
  640. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
  641. MACSTR, MAC2STR(res->bssid));
  642. return;
  643. }
  644. if (ssid[1] > SSID_MAX_LEN) {
  645. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
  646. MACSTR, MAC2STR(res->bssid));
  647. return;
  648. }
  649. p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
  650. #ifdef CONFIG_P2P
  651. if (p2p == NULL &&
  652. wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  653. /*
  654. * If it's a P2P specific interface, then don't update
  655. * the scan result without a P2P IE.
  656. */
  657. wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
  658. " update for P2P interface", MAC2STR(res->bssid));
  659. return;
  660. }
  661. #endif /* CONFIG_P2P */
  662. if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
  663. os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
  664. return; /* Skip P2P listen discovery results here */
  665. /* TODO: add option for ignoring BSSes we are not interested in
  666. * (to save memory) */
  667. mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
  668. if (mesh && mesh[1] <= SSID_MAX_LEN)
  669. ssid = mesh;
  670. bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
  671. if (bss == NULL)
  672. bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
  673. else {
  674. bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
  675. if (wpa_s->last_scan_res) {
  676. unsigned int i;
  677. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  678. if (bss == wpa_s->last_scan_res[i]) {
  679. /* Already in the list */
  680. return;
  681. }
  682. }
  683. }
  684. }
  685. if (bss == NULL)
  686. return;
  687. if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
  688. struct wpa_bss **n;
  689. unsigned int siz;
  690. if (wpa_s->last_scan_res_size == 0)
  691. siz = 32;
  692. else
  693. siz = wpa_s->last_scan_res_size * 2;
  694. n = os_realloc_array(wpa_s->last_scan_res, siz,
  695. sizeof(struct wpa_bss *));
  696. if (n == NULL)
  697. return;
  698. wpa_s->last_scan_res = n;
  699. wpa_s->last_scan_res_size = siz;
  700. }
  701. if (wpa_s->last_scan_res)
  702. wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
  703. }
  704. static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
  705. const struct scan_info *info)
  706. {
  707. int found;
  708. size_t i;
  709. if (info == NULL)
  710. return 1;
  711. if (info->num_freqs) {
  712. found = 0;
  713. for (i = 0; i < info->num_freqs; i++) {
  714. if (bss->freq == info->freqs[i]) {
  715. found = 1;
  716. break;
  717. }
  718. }
  719. if (!found)
  720. return 0;
  721. }
  722. if (info->num_ssids) {
  723. found = 0;
  724. for (i = 0; i < info->num_ssids; i++) {
  725. const struct wpa_driver_scan_ssid *s = &info->ssids[i];
  726. if ((s->ssid == NULL || s->ssid_len == 0) ||
  727. (s->ssid_len == bss->ssid_len &&
  728. os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
  729. 0)) {
  730. found = 1;
  731. break;
  732. }
  733. }
  734. if (!found)
  735. return 0;
  736. }
  737. return 1;
  738. }
  739. /**
  740. * wpa_bss_update_end - End a BSS table update from scan results
  741. * @wpa_s: Pointer to wpa_supplicant data
  742. * @info: Information about scan parameters
  743. * @new_scan: Whether this update round was based on a new scan
  744. *
  745. * This function is called at the end of each BSS table update round for new
  746. * scan results. The start of the update was indicated with a call to
  747. * wpa_bss_update_start().
  748. */
  749. void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
  750. int new_scan)
  751. {
  752. struct wpa_bss *bss, *n;
  753. os_get_reltime(&wpa_s->last_scan);
  754. if ((info && info->aborted) || !new_scan)
  755. return; /* do not expire entries without new scan */
  756. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  757. if (wpa_bss_in_use(wpa_s, bss))
  758. continue;
  759. if (!wpa_bss_included_in_scan(bss, info))
  760. continue; /* expire only BSSes that were scanned */
  761. if (bss->last_update_idx < wpa_s->bss_update_idx)
  762. bss->scan_miss_count++;
  763. if (bss->scan_miss_count >=
  764. wpa_s->conf->bss_expiration_scan_count) {
  765. wpa_bss_remove(wpa_s, bss, "no match in scan");
  766. }
  767. }
  768. wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
  769. wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
  770. }
  771. /**
  772. * wpa_bss_flush_by_age - Flush old BSS entries
  773. * @wpa_s: Pointer to wpa_supplicant data
  774. * @age: Maximum entry age in seconds
  775. *
  776. * Remove BSS entries that have not been updated during the last @age seconds.
  777. */
  778. void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
  779. {
  780. struct wpa_bss *bss, *n;
  781. struct os_reltime t;
  782. if (dl_list_empty(&wpa_s->bss))
  783. return;
  784. os_get_reltime(&t);
  785. t.sec -= age;
  786. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  787. if (wpa_bss_in_use(wpa_s, bss))
  788. continue;
  789. if (os_reltime_before(&bss->last_update, &t)) {
  790. wpa_bss_remove(wpa_s, bss, __func__);
  791. } else
  792. break;
  793. }
  794. }
  795. /**
  796. * wpa_bss_init - Initialize BSS table
  797. * @wpa_s: Pointer to wpa_supplicant data
  798. * Returns: 0 on success, -1 on failure
  799. *
  800. * This prepares BSS table lists and timer for periodic updates. The BSS table
  801. * is deinitialized with wpa_bss_deinit() once not needed anymore.
  802. */
  803. int wpa_bss_init(struct wpa_supplicant *wpa_s)
  804. {
  805. dl_list_init(&wpa_s->bss);
  806. dl_list_init(&wpa_s->bss_id);
  807. return 0;
  808. }
  809. /**
  810. * wpa_bss_flush - Flush all unused BSS entries
  811. * @wpa_s: Pointer to wpa_supplicant data
  812. */
  813. void wpa_bss_flush(struct wpa_supplicant *wpa_s)
  814. {
  815. struct wpa_bss *bss, *n;
  816. wpa_s->clear_driver_scan_cache = 1;
  817. if (wpa_s->bss.next == NULL)
  818. return; /* BSS table not yet initialized */
  819. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  820. if (wpa_bss_in_use(wpa_s, bss))
  821. continue;
  822. wpa_bss_remove(wpa_s, bss, __func__);
  823. }
  824. }
  825. /**
  826. * wpa_bss_deinit - Deinitialize BSS table
  827. * @wpa_s: Pointer to wpa_supplicant data
  828. */
  829. void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
  830. {
  831. wpa_bss_flush(wpa_s);
  832. }
  833. /**
  834. * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
  835. * @wpa_s: Pointer to wpa_supplicant data
  836. * @bssid: BSSID
  837. * Returns: Pointer to the BSS entry or %NULL if not found
  838. */
  839. struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
  840. const u8 *bssid)
  841. {
  842. struct wpa_bss *bss;
  843. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  844. return NULL;
  845. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  846. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  847. return bss;
  848. }
  849. return NULL;
  850. }
  851. /**
  852. * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
  853. * @wpa_s: Pointer to wpa_supplicant data
  854. * @bssid: BSSID
  855. * Returns: Pointer to the BSS entry or %NULL if not found
  856. *
  857. * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
  858. * find the entry that has the most recent update. This can help in finding the
  859. * correct entry in cases where the SSID of the AP may have changed recently
  860. * (e.g., in WPS reconfiguration cases).
  861. */
  862. struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
  863. const u8 *bssid)
  864. {
  865. struct wpa_bss *bss, *found = NULL;
  866. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  867. return NULL;
  868. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  869. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
  870. continue;
  871. if (found == NULL ||
  872. os_reltime_before(&found->last_update, &bss->last_update))
  873. found = bss;
  874. }
  875. return found;
  876. }
  877. #ifdef CONFIG_P2P
  878. /**
  879. * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
  880. * @wpa_s: Pointer to wpa_supplicant data
  881. * @dev_addr: P2P Device Address of the GO
  882. * Returns: Pointer to the BSS entry or %NULL if not found
  883. */
  884. struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
  885. const u8 *dev_addr)
  886. {
  887. struct wpa_bss *bss;
  888. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  889. u8 addr[ETH_ALEN];
  890. if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
  891. addr) == 0 &&
  892. os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
  893. return bss;
  894. }
  895. return NULL;
  896. }
  897. #endif /* CONFIG_P2P */
  898. /**
  899. * wpa_bss_get_id - Fetch a BSS table entry based on identifier
  900. * @wpa_s: Pointer to wpa_supplicant data
  901. * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
  902. * Returns: Pointer to the BSS entry or %NULL if not found
  903. */
  904. struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  905. {
  906. struct wpa_bss *bss;
  907. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  908. if (bss->id == id)
  909. return bss;
  910. }
  911. return NULL;
  912. }
  913. /**
  914. * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
  915. * @wpa_s: Pointer to wpa_supplicant data
  916. * @idf: Smallest allowed identifier assigned for the entry
  917. * @idf: Largest allowed identifier assigned for the entry
  918. * Returns: Pointer to the BSS entry or %NULL if not found
  919. *
  920. * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
  921. * smallest id value to be fetched within the specified range without the
  922. * caller having to know the exact id.
  923. */
  924. struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
  925. unsigned int idf, unsigned int idl)
  926. {
  927. struct wpa_bss *bss;
  928. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  929. if (bss->id >= idf && bss->id <= idl)
  930. return bss;
  931. }
  932. return NULL;
  933. }
  934. /**
  935. * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
  936. * @bss: BSS table entry
  937. * @ie: Information element identitifier (WLAN_EID_*)
  938. * Returns: Pointer to the information element (id field) or %NULL if not found
  939. *
  940. * This function returns the first matching information element in the BSS
  941. * entry.
  942. */
  943. const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
  944. {
  945. return get_ie((const u8 *) (bss + 1), bss->ie_len, ie);
  946. }
  947. /**
  948. * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
  949. * @bss: BSS table entry
  950. * @vendor_type: Vendor type (four octets starting the IE payload)
  951. * Returns: Pointer to the information element (id field) or %NULL if not found
  952. *
  953. * This function returns the first matching information element in the BSS
  954. * entry.
  955. */
  956. const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
  957. {
  958. const u8 *end, *pos;
  959. pos = (const u8 *) (bss + 1);
  960. end = pos + bss->ie_len;
  961. while (end - pos > 1) {
  962. if (2 + pos[1] > end - pos)
  963. break;
  964. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  965. vendor_type == WPA_GET_BE32(&pos[2]))
  966. return pos;
  967. pos += 2 + pos[1];
  968. }
  969. return NULL;
  970. }
  971. /**
  972. * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
  973. * @bss: BSS table entry
  974. * @vendor_type: Vendor type (four octets starting the IE payload)
  975. * Returns: Pointer to the information element (id field) or %NULL if not found
  976. *
  977. * This function returns the first matching information element in the BSS
  978. * entry.
  979. *
  980. * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
  981. * from Beacon frames instead of either Beacon or Probe Response frames.
  982. */
  983. const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
  984. u32 vendor_type)
  985. {
  986. const u8 *end, *pos;
  987. if (bss->beacon_ie_len == 0)
  988. return NULL;
  989. pos = (const u8 *) (bss + 1);
  990. pos += bss->ie_len;
  991. end = pos + bss->beacon_ie_len;
  992. while (end - pos > 1) {
  993. if (2 + pos[1] > end - pos)
  994. break;
  995. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  996. vendor_type == WPA_GET_BE32(&pos[2]))
  997. return pos;
  998. pos += 2 + pos[1];
  999. }
  1000. return NULL;
  1001. }
  1002. /**
  1003. * wpa_bss_get_vendor_ie_multi - 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. struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
  1013. u32 vendor_type)
  1014. {
  1015. struct wpabuf *buf;
  1016. const u8 *end, *pos;
  1017. buf = wpabuf_alloc(bss->ie_len);
  1018. if (buf == NULL)
  1019. return NULL;
  1020. pos = (const u8 *) (bss + 1);
  1021. end = pos + bss->ie_len;
  1022. while (end - pos > 1) {
  1023. if (2 + pos[1] > end - pos)
  1024. break;
  1025. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1026. vendor_type == WPA_GET_BE32(&pos[2]))
  1027. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  1028. pos += 2 + pos[1];
  1029. }
  1030. if (wpabuf_len(buf) == 0) {
  1031. wpabuf_free(buf);
  1032. buf = NULL;
  1033. }
  1034. return buf;
  1035. }
  1036. /**
  1037. * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
  1038. * @bss: BSS table entry
  1039. * @vendor_type: Vendor type (four octets starting the IE payload)
  1040. * Returns: Pointer to the information element payload or %NULL if not found
  1041. *
  1042. * This function returns concatenated payload of possibly fragmented vendor
  1043. * specific information elements in the BSS entry. The caller is responsible for
  1044. * freeing the returned buffer.
  1045. *
  1046. * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
  1047. * from Beacon frames instead of either Beacon or Probe Response frames.
  1048. */
  1049. struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
  1050. u32 vendor_type)
  1051. {
  1052. struct wpabuf *buf;
  1053. const u8 *end, *pos;
  1054. buf = wpabuf_alloc(bss->beacon_ie_len);
  1055. if (buf == NULL)
  1056. return NULL;
  1057. pos = (const u8 *) (bss + 1);
  1058. pos += bss->ie_len;
  1059. end = pos + bss->beacon_ie_len;
  1060. while (end - pos > 1) {
  1061. if (2 + pos[1] > end - pos)
  1062. break;
  1063. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1064. vendor_type == WPA_GET_BE32(&pos[2]))
  1065. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  1066. pos += 2 + pos[1];
  1067. }
  1068. if (wpabuf_len(buf) == 0) {
  1069. wpabuf_free(buf);
  1070. buf = NULL;
  1071. }
  1072. return buf;
  1073. }
  1074. /**
  1075. * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
  1076. * @bss: BSS table entry
  1077. * Returns: Maximum legacy rate in units of 500 kbps
  1078. */
  1079. int wpa_bss_get_max_rate(const struct wpa_bss *bss)
  1080. {
  1081. int rate = 0;
  1082. const u8 *ie;
  1083. int i;
  1084. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  1085. for (i = 0; ie && i < ie[1]; i++) {
  1086. if ((ie[i + 2] & 0x7f) > rate)
  1087. rate = ie[i + 2] & 0x7f;
  1088. }
  1089. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  1090. for (i = 0; ie && i < ie[1]; i++) {
  1091. if ((ie[i + 2] & 0x7f) > rate)
  1092. rate = ie[i + 2] & 0x7f;
  1093. }
  1094. return rate;
  1095. }
  1096. /**
  1097. * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
  1098. * @bss: BSS table entry
  1099. * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
  1100. * Returns: number of legacy TX rates or -1 on failure
  1101. *
  1102. * The caller is responsible for freeing the returned buffer with os_free() in
  1103. * case of success.
  1104. */
  1105. int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
  1106. {
  1107. const u8 *ie, *ie2;
  1108. int i, j;
  1109. unsigned int len;
  1110. u8 *r;
  1111. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  1112. ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  1113. len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
  1114. r = os_malloc(len);
  1115. if (!r)
  1116. return -1;
  1117. for (i = 0; ie && i < ie[1]; i++)
  1118. r[i] = ie[i + 2] & 0x7f;
  1119. for (j = 0; ie2 && j < ie2[1]; j++)
  1120. r[i + j] = ie2[j + 2] & 0x7f;
  1121. *rates = r;
  1122. return len;
  1123. }