bss.c 29 KB

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