bss.c 30 KB

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