bss.c 27 KB

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