bss.c 32 KB

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