rrm.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /*
  2. * wpa_supplicant - Radio Measurements
  3. * Copyright (c) 2003-2016, 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 "includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "common/ieee802_11_common.h"
  12. #include "wpa_supplicant_i.h"
  13. #include "driver_i.h"
  14. #include "bss.h"
  15. #include "scan.h"
  16. #include "p2p_supplicant.h"
  17. static void wpas_rrm_neighbor_rep_timeout_handler(void *data, void *user_ctx)
  18. {
  19. struct rrm_data *rrm = data;
  20. if (!rrm->notify_neighbor_rep) {
  21. wpa_printf(MSG_ERROR,
  22. "RRM: Unexpected neighbor report timeout");
  23. return;
  24. }
  25. wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE");
  26. rrm->notify_neighbor_rep(rrm->neighbor_rep_cb_ctx, NULL);
  27. rrm->notify_neighbor_rep = NULL;
  28. rrm->neighbor_rep_cb_ctx = NULL;
  29. }
  30. /*
  31. * wpas_rrm_reset - Clear and reset all RRM data in wpa_supplicant
  32. * @wpa_s: Pointer to wpa_supplicant
  33. */
  34. void wpas_rrm_reset(struct wpa_supplicant *wpa_s)
  35. {
  36. wpa_s->rrm.rrm_used = 0;
  37. eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
  38. NULL);
  39. if (wpa_s->rrm.notify_neighbor_rep)
  40. wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
  41. wpa_s->rrm.next_neighbor_rep_token = 1;
  42. wpas_clear_beacon_rep_data(wpa_s);
  43. }
  44. /*
  45. * wpas_rrm_process_neighbor_rep - Handle incoming neighbor report
  46. * @wpa_s: Pointer to wpa_supplicant
  47. * @report: Neighbor report buffer, prefixed by a 1-byte dialog token
  48. * @report_len: Length of neighbor report buffer
  49. */
  50. void wpas_rrm_process_neighbor_rep(struct wpa_supplicant *wpa_s,
  51. const u8 *report, size_t report_len)
  52. {
  53. struct wpabuf *neighbor_rep;
  54. wpa_hexdump(MSG_DEBUG, "RRM: New Neighbor Report", report, report_len);
  55. if (report_len < 1)
  56. return;
  57. if (report[0] != wpa_s->rrm.next_neighbor_rep_token - 1) {
  58. wpa_printf(MSG_DEBUG,
  59. "RRM: Discarding neighbor report with token %d (expected %d)",
  60. report[0], wpa_s->rrm.next_neighbor_rep_token - 1);
  61. return;
  62. }
  63. eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
  64. NULL);
  65. if (!wpa_s->rrm.notify_neighbor_rep) {
  66. wpa_printf(MSG_ERROR, "RRM: Unexpected neighbor report");
  67. return;
  68. }
  69. /* skipping the first byte, which is only an id (dialog token) */
  70. neighbor_rep = wpabuf_alloc(report_len - 1);
  71. if (!neighbor_rep) {
  72. wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
  73. return;
  74. }
  75. wpabuf_put_data(neighbor_rep, report + 1, report_len - 1);
  76. wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report (token = %d)",
  77. report[0]);
  78. wpa_s->rrm.notify_neighbor_rep(wpa_s->rrm.neighbor_rep_cb_ctx,
  79. neighbor_rep);
  80. wpa_s->rrm.notify_neighbor_rep = NULL;
  81. wpa_s->rrm.neighbor_rep_cb_ctx = NULL;
  82. }
  83. #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
  84. /* Workaround different, undefined for Windows, error codes used here */
  85. #define ENOTCONN -1
  86. #define EOPNOTSUPP -1
  87. #define ECANCELED -1
  88. #endif
  89. /* Measurement Request element + Location Subject + Maximum Age subelement */
  90. #define MEASURE_REQUEST_LCI_LEN (3 + 1 + 4)
  91. /* Measurement Request element + Location Civic Request */
  92. #define MEASURE_REQUEST_CIVIC_LEN (3 + 5)
  93. /**
  94. * wpas_rrm_send_neighbor_rep_request - Request a neighbor report from our AP
  95. * @wpa_s: Pointer to wpa_supplicant
  96. * @ssid: if not null, this is sent in the request. Otherwise, no SSID IE
  97. * is sent in the request.
  98. * @lci: if set, neighbor request will include LCI request
  99. * @civic: if set, neighbor request will include civic location request
  100. * @cb: Callback function to be called once the requested report arrives, or
  101. * timed out after RRM_NEIGHBOR_REPORT_TIMEOUT seconds.
  102. * In the former case, 'neighbor_rep' is a newly allocated wpabuf, and it's
  103. * the requester's responsibility to free it.
  104. * In the latter case NULL will be sent in 'neighbor_rep'.
  105. * @cb_ctx: Context value to send the callback function
  106. * Returns: 0 in case of success, negative error code otherwise
  107. *
  108. * In case there is a previous request which has not been answered yet, the
  109. * new request fails. The caller may retry after RRM_NEIGHBOR_REPORT_TIMEOUT.
  110. * Request must contain a callback function.
  111. */
  112. int wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant *wpa_s,
  113. const struct wpa_ssid_value *ssid,
  114. int lci, int civic,
  115. void (*cb)(void *ctx,
  116. struct wpabuf *neighbor_rep),
  117. void *cb_ctx)
  118. {
  119. struct wpabuf *buf;
  120. const u8 *rrm_ie;
  121. if (wpa_s->wpa_state != WPA_COMPLETED || wpa_s->current_ssid == NULL) {
  122. wpa_printf(MSG_DEBUG, "RRM: No connection, no RRM.");
  123. return -ENOTCONN;
  124. }
  125. if (!wpa_s->rrm.rrm_used) {
  126. wpa_printf(MSG_DEBUG, "RRM: No RRM in current connection.");
  127. return -EOPNOTSUPP;
  128. }
  129. rrm_ie = wpa_bss_get_ie(wpa_s->current_bss,
  130. WLAN_EID_RRM_ENABLED_CAPABILITIES);
  131. if (!rrm_ie || !(wpa_s->current_bss->caps & IEEE80211_CAP_RRM) ||
  132. !(rrm_ie[2] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
  133. wpa_printf(MSG_DEBUG,
  134. "RRM: No network support for Neighbor Report.");
  135. return -EOPNOTSUPP;
  136. }
  137. /* Refuse if there's a live request */
  138. if (wpa_s->rrm.notify_neighbor_rep) {
  139. wpa_printf(MSG_DEBUG,
  140. "RRM: Currently handling previous Neighbor Report.");
  141. return -EBUSY;
  142. }
  143. /* 3 = action category + action code + dialog token */
  144. buf = wpabuf_alloc(3 + (ssid ? 2 + ssid->ssid_len : 0) +
  145. (lci ? 2 + MEASURE_REQUEST_LCI_LEN : 0) +
  146. (civic ? 2 + MEASURE_REQUEST_CIVIC_LEN : 0));
  147. if (buf == NULL) {
  148. wpa_printf(MSG_DEBUG,
  149. "RRM: Failed to allocate Neighbor Report Request");
  150. return -ENOMEM;
  151. }
  152. wpa_printf(MSG_DEBUG, "RRM: Neighbor report request (for %s), token=%d",
  153. (ssid ? wpa_ssid_txt(ssid->ssid, ssid->ssid_len) : ""),
  154. wpa_s->rrm.next_neighbor_rep_token);
  155. wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
  156. wpabuf_put_u8(buf, WLAN_RRM_NEIGHBOR_REPORT_REQUEST);
  157. wpabuf_put_u8(buf, wpa_s->rrm.next_neighbor_rep_token);
  158. if (ssid) {
  159. wpabuf_put_u8(buf, WLAN_EID_SSID);
  160. wpabuf_put_u8(buf, ssid->ssid_len);
  161. wpabuf_put_data(buf, ssid->ssid, ssid->ssid_len);
  162. }
  163. if (lci) {
  164. /* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
  165. wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
  166. wpabuf_put_u8(buf, MEASURE_REQUEST_LCI_LEN);
  167. /*
  168. * Measurement token; nonzero number that is unique among the
  169. * Measurement Request elements in a particular frame.
  170. */
  171. wpabuf_put_u8(buf, 1); /* Measurement Token */
  172. /*
  173. * Parallel, Enable, Request, and Report bits are 0, Duration is
  174. * reserved.
  175. */
  176. wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
  177. wpabuf_put_u8(buf, MEASURE_TYPE_LCI); /* Measurement Type */
  178. /* IEEE P802.11-REVmc/D5.0 9.4.2.21.10 - LCI request */
  179. /* Location Subject */
  180. wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
  181. /* Optional Subelements */
  182. /*
  183. * IEEE P802.11-REVmc/D5.0 Figure 9-170
  184. * The Maximum Age subelement is required, otherwise the AP can
  185. * send only data that was determined after receiving the
  186. * request. Setting it here to unlimited age.
  187. */
  188. wpabuf_put_u8(buf, LCI_REQ_SUBELEM_MAX_AGE);
  189. wpabuf_put_u8(buf, 2);
  190. wpabuf_put_le16(buf, 0xffff);
  191. }
  192. if (civic) {
  193. /* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
  194. wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
  195. wpabuf_put_u8(buf, MEASURE_REQUEST_CIVIC_LEN);
  196. /*
  197. * Measurement token; nonzero number that is unique among the
  198. * Measurement Request elements in a particular frame.
  199. */
  200. wpabuf_put_u8(buf, 2); /* Measurement Token */
  201. /*
  202. * Parallel, Enable, Request, and Report bits are 0, Duration is
  203. * reserved.
  204. */
  205. wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
  206. /* Measurement Type */
  207. wpabuf_put_u8(buf, MEASURE_TYPE_LOCATION_CIVIC);
  208. /* IEEE P802.11-REVmc/D5.0 9.4.2.21.14:
  209. * Location Civic request */
  210. /* Location Subject */
  211. wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
  212. wpabuf_put_u8(buf, 0); /* Civic Location Type: IETF RFC 4776 */
  213. /* Location Service Interval Units: Seconds */
  214. wpabuf_put_u8(buf, 0);
  215. /* Location Service Interval: 0 - Only one report is requested
  216. */
  217. wpabuf_put_le16(buf, 0);
  218. /* No optional subelements */
  219. }
  220. wpa_s->rrm.next_neighbor_rep_token++;
  221. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  222. wpa_s->own_addr, wpa_s->bssid,
  223. wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
  224. wpa_printf(MSG_DEBUG,
  225. "RRM: Failed to send Neighbor Report Request");
  226. wpabuf_free(buf);
  227. return -ECANCELED;
  228. }
  229. wpa_s->rrm.neighbor_rep_cb_ctx = cb_ctx;
  230. wpa_s->rrm.notify_neighbor_rep = cb;
  231. eloop_register_timeout(RRM_NEIGHBOR_REPORT_TIMEOUT, 0,
  232. wpas_rrm_neighbor_rep_timeout_handler,
  233. &wpa_s->rrm, NULL);
  234. wpabuf_free(buf);
  235. return 0;
  236. }
  237. static int wpas_rrm_report_elem(struct wpabuf **buf, u8 token, u8 mode, u8 type,
  238. const u8 *data, size_t data_len)
  239. {
  240. if (wpabuf_resize(buf, 5 + data_len))
  241. return -1;
  242. wpabuf_put_u8(*buf, WLAN_EID_MEASURE_REPORT);
  243. wpabuf_put_u8(*buf, 3 + data_len);
  244. wpabuf_put_u8(*buf, token);
  245. wpabuf_put_u8(*buf, mode);
  246. wpabuf_put_u8(*buf, type);
  247. if (data_len)
  248. wpabuf_put_data(*buf, data, data_len);
  249. return 0;
  250. }
  251. static int
  252. wpas_rrm_build_lci_report(struct wpa_supplicant *wpa_s,
  253. const struct rrm_measurement_request_element *req,
  254. struct wpabuf **buf)
  255. {
  256. u8 subject;
  257. u16 max_age = 0;
  258. struct os_reltime t, diff;
  259. unsigned long diff_l;
  260. const u8 *subelem;
  261. const u8 *request = req->variable;
  262. size_t len = req->len - 3;
  263. if (len < 1)
  264. return -1;
  265. if (!wpa_s->lci)
  266. goto reject;
  267. subject = *request++;
  268. len--;
  269. wpa_printf(MSG_DEBUG, "Measurement request location subject=%u",
  270. subject);
  271. if (subject != LOCATION_SUBJECT_REMOTE) {
  272. wpa_printf(MSG_INFO,
  273. "Not building LCI report - bad location subject");
  274. return 0;
  275. }
  276. /* Subelements are formatted exactly like elements */
  277. wpa_hexdump(MSG_DEBUG, "LCI request subelements", request, len);
  278. subelem = get_ie(request, len, LCI_REQ_SUBELEM_MAX_AGE);
  279. if (subelem && subelem[1] == 2)
  280. max_age = WPA_GET_LE16(subelem + 2);
  281. if (os_get_reltime(&t))
  282. goto reject;
  283. os_reltime_sub(&t, &wpa_s->lci_time, &diff);
  284. /* LCI age is calculated in 10th of a second units. */
  285. diff_l = diff.sec * 10 + diff.usec / 100000;
  286. if (max_age != 0xffff && max_age < diff_l)
  287. goto reject;
  288. if (wpas_rrm_report_elem(buf, req->token,
  289. MEASUREMENT_REPORT_MODE_ACCEPT, req->type,
  290. wpabuf_head_u8(wpa_s->lci),
  291. wpabuf_len(wpa_s->lci)) < 0) {
  292. wpa_printf(MSG_DEBUG, "Failed to add LCI report element");
  293. return -1;
  294. }
  295. return 0;
  296. reject:
  297. if (wpas_rrm_report_elem(buf, req->token,
  298. MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
  299. req->type, NULL, 0) < 0) {
  300. wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
  301. return -1;
  302. }
  303. return 0;
  304. }
  305. static void wpas_rrm_send_msr_report_mpdu(struct wpa_supplicant *wpa_s,
  306. const u8 *data, size_t len)
  307. {
  308. struct wpabuf *report = wpabuf_alloc(len + 3);
  309. if (!report)
  310. return;
  311. wpabuf_put_u8(report, WLAN_ACTION_RADIO_MEASUREMENT);
  312. wpabuf_put_u8(report, WLAN_RRM_RADIO_MEASUREMENT_REPORT);
  313. wpabuf_put_u8(report, wpa_s->rrm.token);
  314. wpabuf_put_data(report, data, len);
  315. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  316. wpa_s->own_addr, wpa_s->bssid,
  317. wpabuf_head(report), wpabuf_len(report), 0)) {
  318. wpa_printf(MSG_ERROR,
  319. "RRM: Radio measurement report failed: Sending Action frame failed");
  320. }
  321. wpabuf_free(report);
  322. }
  323. static void wpas_rrm_send_msr_report(struct wpa_supplicant *wpa_s,
  324. struct wpabuf *buf)
  325. {
  326. int len = wpabuf_len(buf);
  327. const u8 *pos = wpabuf_head_u8(buf), *next = pos;
  328. #define MPDU_REPORT_LEN (int) (IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN - 3)
  329. while (len) {
  330. int send_len = (len > MPDU_REPORT_LEN) ? next - pos : len;
  331. if (send_len == len ||
  332. (send_len + next[1] + 2) > MPDU_REPORT_LEN) {
  333. wpas_rrm_send_msr_report_mpdu(wpa_s, pos, send_len);
  334. len -= send_len;
  335. pos = next;
  336. }
  337. next += next[1] + 2;
  338. }
  339. #undef MPDU_REPORT_LEN
  340. }
  341. static int wpas_add_channel(u8 op_class, u8 chan, u8 num_primary_channels,
  342. int *freqs)
  343. {
  344. size_t i;
  345. for (i = 0; i < num_primary_channels; i++) {
  346. u8 primary_chan = chan - (2 * num_primary_channels - 2) + i * 4;
  347. freqs[i] = ieee80211_chan_to_freq(NULL, op_class, primary_chan);
  348. /* ieee80211_chan_to_freq() is not really meant for this
  349. * conversion of 20 MHz primary channel numbers for wider VHT
  350. * channels, so handle those as special cases here for now. */
  351. if (freqs[i] < 0 &&
  352. (op_class == 128 || op_class == 129 || op_class == 130))
  353. freqs[i] = 5000 + 5 * primary_chan;
  354. if (freqs[i] < 0) {
  355. wpa_printf(MSG_DEBUG,
  356. "Beacon Report: Invalid channel %u",
  357. chan);
  358. return -1;
  359. }
  360. }
  361. return 0;
  362. }
  363. static int * wpas_add_channels(const struct oper_class_map *op,
  364. struct hostapd_hw_modes *mode, int active,
  365. const u8 *channels, const u8 size)
  366. {
  367. int *freqs, *next_freq;
  368. u8 num_primary_channels, i;
  369. u8 num_chans;
  370. num_chans = channels ? size :
  371. (op->max_chan - op->min_chan) / op->inc + 1;
  372. if (op->bw == BW80 || op->bw == BW80P80)
  373. num_primary_channels = 4;
  374. else if (op->bw == BW160)
  375. num_primary_channels = 8;
  376. else
  377. num_primary_channels = 1;
  378. /* one extra place for the zero-terminator */
  379. freqs = os_calloc(num_chans * num_primary_channels + 1, sizeof(*freqs));
  380. if (!freqs) {
  381. wpa_printf(MSG_ERROR,
  382. "Beacon Report: Failed to allocate freqs array");
  383. return NULL;
  384. }
  385. next_freq = freqs;
  386. for (i = 0; i < num_chans; i++) {
  387. u8 chan = channels ? channels[i] : op->min_chan + i * op->inc;
  388. enum chan_allowed res = verify_channel(mode, chan, op->bw);
  389. if (res == NOT_ALLOWED || (res == NO_IR && active))
  390. continue;
  391. if (wpas_add_channel(op->op_class, chan, num_primary_channels,
  392. next_freq) < 0) {
  393. os_free(freqs);
  394. return NULL;
  395. }
  396. next_freq += num_primary_channels;
  397. }
  398. if (!freqs[0]) {
  399. os_free(freqs);
  400. return NULL;
  401. }
  402. return freqs;
  403. }
  404. static int * wpas_op_class_freqs(const struct oper_class_map *op,
  405. struct hostapd_hw_modes *mode, int active)
  406. {
  407. u8 channels_80mhz[] = { 42, 58, 106, 122, 138, 155 };
  408. u8 channels_160mhz[] = { 50, 114 };
  409. /*
  410. * When adding all channels in the operating class, 80 + 80 MHz
  411. * operating classes are like 80 MHz channels because we add all valid
  412. * channels anyway.
  413. */
  414. if (op->bw == BW80 || op->bw == BW80P80)
  415. return wpas_add_channels(op, mode, active, channels_80mhz,
  416. ARRAY_SIZE(channels_80mhz));
  417. if (op->bw == BW160)
  418. return wpas_add_channels(op, mode, active, channels_160mhz,
  419. ARRAY_SIZE(channels_160mhz));
  420. return wpas_add_channels(op, mode, active, NULL, 0);
  421. }
  422. static int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s, int active,
  423. const char *country, const u8 *subelems,
  424. size_t len)
  425. {
  426. int *freqs = NULL, *new_freqs;
  427. const u8 *end = subelems + len;
  428. while (end - subelems > 2) {
  429. const struct oper_class_map *op;
  430. const u8 *ap_chan_elem, *pos;
  431. u8 left;
  432. struct hostapd_hw_modes *mode;
  433. ap_chan_elem = get_ie(subelems, end - subelems,
  434. WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
  435. if (!ap_chan_elem)
  436. break;
  437. pos = ap_chan_elem + 2;
  438. left = ap_chan_elem[1];
  439. if (left < 1)
  440. break;
  441. subelems = ap_chan_elem + 2 + left;
  442. op = get_oper_class(country, *pos);
  443. if (!op) {
  444. wpa_printf(MSG_DEBUG,
  445. "Beacon request: unknown operating class in AP Channel Report subelement %u",
  446. *pos);
  447. goto out;
  448. }
  449. pos++;
  450. left--;
  451. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
  452. if (!mode)
  453. continue;
  454. /*
  455. * For 80 + 80 MHz operating classes, this AP Channel Report
  456. * element should be followed by another element specifying
  457. * the second 80 MHz channel. For now just add this 80 MHz
  458. * channel, the second 80 MHz channel will be added when the
  459. * next element is parsed.
  460. * TODO: Verify that this AP Channel Report element is followed
  461. * by a corresponding AP Channel Report element as specified in
  462. * IEEE Std 802.11-2016, 11.11.9.1.
  463. */
  464. new_freqs = wpas_add_channels(op, mode, active, pos, left);
  465. if (new_freqs)
  466. int_array_concat(&freqs, new_freqs);
  467. os_free(new_freqs);
  468. }
  469. return freqs;
  470. out:
  471. os_free(freqs);
  472. return NULL;
  473. }
  474. static int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
  475. u8 op_class, u8 chan, int active,
  476. const u8 *subelems, size_t len)
  477. {
  478. int *freqs = NULL, *ext_freqs = NULL;
  479. struct hostapd_hw_modes *mode;
  480. const char *country = NULL;
  481. const struct oper_class_map *op;
  482. const u8 *elem;
  483. if (!wpa_s->current_bss)
  484. return NULL;
  485. elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
  486. if (elem && elem[1] >= 2)
  487. country = (const char *) (elem + 2);
  488. op = get_oper_class(country, op_class);
  489. if (!op) {
  490. wpa_printf(MSG_DEBUG,
  491. "Beacon request: invalid operating class %d",
  492. op_class);
  493. return NULL;
  494. }
  495. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
  496. if (!mode)
  497. return NULL;
  498. switch (chan) {
  499. case 0:
  500. freqs = wpas_op_class_freqs(op, mode, active);
  501. if (!freqs)
  502. return NULL;
  503. break;
  504. case 255:
  505. /* freqs will be added from AP channel subelements */
  506. break;
  507. default:
  508. freqs = wpas_add_channels(op, mode, active, &chan, 1);
  509. if (!freqs)
  510. return NULL;
  511. break;
  512. }
  513. ext_freqs = wpas_channel_report_freqs(wpa_s, active, country, subelems,
  514. len);
  515. if (ext_freqs) {
  516. int_array_concat(&freqs, ext_freqs);
  517. os_free(ext_freqs);
  518. }
  519. return freqs;
  520. }
  521. int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
  522. u8 *op_class, u8 *chan, u8 *phy_type)
  523. {
  524. const u8 *ie;
  525. int sec_chan = 0, vht = 0;
  526. struct ieee80211_ht_operation *ht_oper = NULL;
  527. struct ieee80211_vht_operation *vht_oper = NULL;
  528. u8 seg0, seg1;
  529. ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
  530. if (ie && ie[1] >= sizeof(struct ieee80211_ht_operation)) {
  531. u8 sec_chan_offset;
  532. ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
  533. sec_chan_offset = ht_oper->ht_param &
  534. HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
  535. if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
  536. sec_chan = 1;
  537. else if (sec_chan_offset ==
  538. HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
  539. sec_chan = -1;
  540. }
  541. ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
  542. if (ie && ie[1] >= sizeof(struct ieee80211_vht_operation)) {
  543. vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
  544. switch (vht_oper->vht_op_info_chwidth) {
  545. case 1:
  546. seg0 = vht_oper->vht_op_info_chan_center_freq_seg0_idx;
  547. seg1 = vht_oper->vht_op_info_chan_center_freq_seg1_idx;
  548. if (seg1 && abs(seg1 - seg0) == 8)
  549. vht = VHT_CHANWIDTH_160MHZ;
  550. else if (seg1)
  551. vht = VHT_CHANWIDTH_80P80MHZ;
  552. else
  553. vht = VHT_CHANWIDTH_80MHZ;
  554. break;
  555. case 2:
  556. vht = VHT_CHANWIDTH_160MHZ;
  557. break;
  558. case 3:
  559. vht = VHT_CHANWIDTH_80P80MHZ;
  560. break;
  561. default:
  562. vht = VHT_CHANWIDTH_USE_HT;
  563. break;
  564. }
  565. }
  566. if (ieee80211_freq_to_channel_ext(freq, sec_chan, vht, op_class,
  567. chan) == NUM_HOSTAPD_MODES) {
  568. wpa_printf(MSG_DEBUG,
  569. "Cannot determine operating class and channel");
  570. return -1;
  571. }
  572. *phy_type = ieee80211_get_phy_type(freq, ht_oper != NULL,
  573. vht_oper != NULL);
  574. if (*phy_type == PHY_TYPE_UNSPECIFIED) {
  575. wpa_printf(MSG_DEBUG, "Cannot determine phy type");
  576. return -1;
  577. }
  578. return 0;
  579. }
  580. static int wpas_beacon_rep_add_frame_body(struct bitfield *eids,
  581. enum beacon_report_detail detail,
  582. struct wpa_bss *bss, u8 *buf,
  583. size_t buf_len)
  584. {
  585. u8 *ies = (u8 *) (bss + 1);
  586. size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
  587. u8 *pos = buf;
  588. int rem_len;
  589. rem_len = 255 - sizeof(struct rrm_measurement_beacon_report) -
  590. sizeof(struct rrm_measurement_report_element) - 2;
  591. if (detail > BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
  592. wpa_printf(MSG_DEBUG,
  593. "Beacon Request: Invalid reporting detail: %d",
  594. detail);
  595. return -1;
  596. }
  597. if (detail == BEACON_REPORT_DETAIL_NONE)
  598. return 0;
  599. /*
  600. * Minimal frame body subelement size: EID(1) + length(1) + TSF(8) +
  601. * beacon interval(2) + capabilities(2) = 14 bytes
  602. */
  603. if (buf_len < 14)
  604. return 0;
  605. *pos++ = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY;
  606. /* The length will be filled later */
  607. pos++;
  608. WPA_PUT_LE64(pos, bss->tsf);
  609. pos += sizeof(bss->tsf);
  610. WPA_PUT_LE16(pos, bss->beacon_int);
  611. pos += 2;
  612. WPA_PUT_LE16(pos, bss->caps);
  613. pos += 2;
  614. rem_len -= pos - buf;
  615. /*
  616. * According to IEEE Std 802.11-2016, 9.4.2.22.7, if the reported frame
  617. * body subelement causes the element to exceed the maximum element
  618. * size, the subelement is truncated so that the last IE is a complete
  619. * IE. So even when required to report all IEs, add elements one after
  620. * the other and stop once there is no more room in the measurement
  621. * element.
  622. */
  623. while (ies_len > 2 && 2U + ies[1] <= ies_len && rem_len > 0) {
  624. if (detail == BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS ||
  625. (eids && bitfield_is_set(eids, ies[0]))) {
  626. u8 eid = ies[0], elen = ies[1];
  627. if ((eid == WLAN_EID_TIM || eid == WLAN_EID_RSN) &&
  628. elen > 4)
  629. elen = 4;
  630. /*
  631. * TODO: Truncate IBSS DFS element as described in
  632. * IEEE Std 802.11-2016, 9.4.2.22.7.
  633. */
  634. if (2 + elen > buf + buf_len - pos ||
  635. 2 + elen > rem_len)
  636. break;
  637. *pos++ = ies[0];
  638. *pos++ = elen;
  639. os_memcpy(pos, ies + 2, elen);
  640. pos += elen;
  641. rem_len -= 2 + elen;
  642. }
  643. ies_len -= 2 + ies[1];
  644. ies += 2 + ies[1];
  645. }
  646. /* Now the length is known */
  647. buf[1] = pos - buf - 2;
  648. return pos - buf;
  649. }
  650. static int wpas_add_beacon_rep(struct wpa_supplicant *wpa_s,
  651. struct wpabuf **wpa_buf, struct wpa_bss *bss,
  652. u64 start, u64 parent_tsf)
  653. {
  654. struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
  655. u8 *ie = (u8 *) (bss + 1);
  656. size_t ie_len = bss->ie_len + bss->beacon_ie_len;
  657. int ret;
  658. u8 buf[2000];
  659. struct rrm_measurement_beacon_report *rep;
  660. if (os_memcmp(data->bssid, broadcast_ether_addr, ETH_ALEN) != 0 &&
  661. os_memcmp(data->bssid, bss->bssid, ETH_ALEN) != 0)
  662. return 0;
  663. if (data->ssid_len &&
  664. (data->ssid_len != bss->ssid_len ||
  665. os_memcmp(data->ssid, bss->ssid, bss->ssid_len) != 0))
  666. return 0;
  667. rep = (struct rrm_measurement_beacon_report *) buf;
  668. if (wpas_get_op_chan_phy(bss->freq, ie, ie_len, &rep->op_class,
  669. &rep->channel, &rep->report_info) < 0)
  670. return 0;
  671. rep->start_time = host_to_le64(start);
  672. rep->duration = host_to_le16(data->scan_params.duration);
  673. rep->rcpi = rssi_to_rcpi(bss->level);
  674. rep->rsni = 255; /* 255 indicates that RSNI is not available */
  675. os_memcpy(rep->bssid, bss->bssid, ETH_ALEN);
  676. rep->antenna_id = 0; /* unknown */
  677. rep->parent_tsf = host_to_le32(parent_tsf);
  678. ret = wpas_beacon_rep_add_frame_body(data->eids, data->report_detail,
  679. bss, rep->variable,
  680. sizeof(buf) - sizeof(*rep));
  681. if (ret < 0)
  682. return -1;
  683. return wpas_rrm_report_elem(wpa_buf, wpa_s->beacon_rep_data.token,
  684. MEASUREMENT_REPORT_MODE_ACCEPT,
  685. MEASURE_TYPE_BEACON, buf,
  686. ret + sizeof(*rep));
  687. }
  688. static int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
  689. struct wpabuf **buf)
  690. {
  691. return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
  692. MEASUREMENT_REPORT_MODE_ACCEPT,
  693. MEASURE_TYPE_BEACON, NULL, 0);
  694. }
  695. static void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
  696. struct wpabuf **buf)
  697. {
  698. size_t i;
  699. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  700. if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
  701. 0, 0) < 0)
  702. break;
  703. }
  704. if (!(*buf))
  705. wpas_beacon_rep_no_results(wpa_s, buf);
  706. wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
  707. }
  708. static void wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
  709. {
  710. struct wpabuf *buf = NULL;
  711. if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
  712. MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
  713. MEASURE_TYPE_BEACON, NULL, 0)) {
  714. wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
  715. wpabuf_free(buf);
  716. return;
  717. }
  718. wpas_rrm_send_msr_report(wpa_s, buf);
  719. wpas_clear_beacon_rep_data(wpa_s);
  720. wpabuf_free(buf);
  721. }
  722. static void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  723. {
  724. struct wpa_supplicant *wpa_s = eloop_ctx;
  725. struct wpa_driver_scan_params *params =
  726. &wpa_s->beacon_rep_data.scan_params;
  727. u16 prev_duration = params->duration;
  728. if (!wpa_s->current_bss)
  729. return;
  730. if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
  731. params->duration) {
  732. wpa_printf(MSG_DEBUG,
  733. "RRM: Cannot set scan duration due to missing driver support");
  734. params->duration = 0;
  735. }
  736. os_get_reltime(&wpa_s->beacon_rep_scan);
  737. if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
  738. wpa_supplicant_trigger_scan(wpa_s, params))
  739. wpas_rrm_refuse_request(wpa_s);
  740. params->duration = prev_duration;
  741. }
  742. static int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
  743. struct beacon_rep_data *data,
  744. u8 sid, u8 slen, const u8 *subelem)
  745. {
  746. u8 report_info, i;
  747. switch (sid) {
  748. case WLAN_BEACON_REQUEST_SUBELEM_SSID:
  749. if (!slen) {
  750. wpa_printf(MSG_DEBUG,
  751. "SSID subelement with zero length - wildcard SSID");
  752. break;
  753. }
  754. if (slen > SSID_MAX_LEN) {
  755. wpa_printf(MSG_DEBUG,
  756. "Invalid SSID subelement length: %u", slen);
  757. return -1;
  758. }
  759. data->ssid_len = slen;
  760. os_memcpy(data->ssid, subelem, data->ssid_len);
  761. break;
  762. case WLAN_BEACON_REQUEST_SUBELEM_INFO:
  763. if (slen != 2) {
  764. wpa_printf(MSG_DEBUG,
  765. "Invalid reporting information subelement length: %u",
  766. slen);
  767. return -1;
  768. }
  769. report_info = subelem[0];
  770. if (report_info != 0) {
  771. wpa_printf(MSG_DEBUG,
  772. "reporting information=%u is not supported",
  773. report_info);
  774. return 0;
  775. }
  776. break;
  777. case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
  778. if (slen != 1) {
  779. wpa_printf(MSG_DEBUG,
  780. "Invalid reporting detail subelement length: %u",
  781. slen);
  782. return -1;
  783. }
  784. data->report_detail = subelem[0];
  785. if (data->report_detail >
  786. BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
  787. wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
  788. subelem[0]);
  789. return 0;
  790. }
  791. break;
  792. case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
  793. if (data->report_detail !=
  794. BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
  795. wpa_printf(MSG_DEBUG,
  796. "Beacon request: request subelement is present but report detail is %u",
  797. data->report_detail);
  798. return -1;
  799. }
  800. if (!slen) {
  801. wpa_printf(MSG_DEBUG,
  802. "Invalid request subelement length: %u",
  803. slen);
  804. return -1;
  805. }
  806. if (data->eids) {
  807. wpa_printf(MSG_DEBUG,
  808. "Beacon Request: Request subelement appears more than once");
  809. return -1;
  810. }
  811. data->eids = bitfield_alloc(255);
  812. if (!data->eids) {
  813. wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
  814. return -1;
  815. }
  816. for (i = 0; i < slen; i++)
  817. bitfield_set(data->eids, subelem[i]);
  818. break;
  819. case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
  820. /* Skip - it will be processed when freqs are added */
  821. break;
  822. default:
  823. wpa_printf(MSG_DEBUG,
  824. "Beacon request: Unknown subelement id %u", sid);
  825. break;
  826. }
  827. return 1;
  828. }
  829. /**
  830. * Returns 0 if the next element can be processed, 1 if some operation was
  831. * triggered, and -1 if processing failed (i.e., the element is in invalid
  832. * format or an internal error occurred).
  833. */
  834. static int
  835. wpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
  836. u8 elem_token, int duration_mandatory,
  837. const struct rrm_measurement_beacon_request *req,
  838. size_t len, struct wpabuf **buf)
  839. {
  840. struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
  841. struct wpa_driver_scan_params *params = &data->scan_params;
  842. const u8 *subelems;
  843. size_t elems_len;
  844. u16 rand_interval;
  845. u32 interval_usec;
  846. u32 _rand;
  847. int ret = 0, res;
  848. if (len < sizeof(*req))
  849. return -1;
  850. if (req->mode != BEACON_REPORT_MODE_PASSIVE &&
  851. req->mode != BEACON_REPORT_MODE_ACTIVE &&
  852. req->mode != BEACON_REPORT_MODE_TABLE)
  853. return 0;
  854. subelems = req->variable;
  855. elems_len = len - sizeof(*req);
  856. rand_interval = le_to_host16(req->rand_interval);
  857. os_memset(params, 0, sizeof(*params));
  858. data->token = elem_token;
  859. /* default reporting detail is all fixed length fields and all
  860. * elements */
  861. data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
  862. os_memcpy(data->bssid, req->bssid, ETH_ALEN);
  863. while (elems_len >= 2) {
  864. if (subelems[1] > elems_len - 2) {
  865. wpa_printf(MSG_DEBUG,
  866. "Beacon Request: Truncated subelement");
  867. ret = -1;
  868. goto out;
  869. }
  870. res = wpas_rm_handle_beacon_req_subelem(
  871. wpa_s, data, subelems[0], subelems[1], &subelems[2]);
  872. if (res != 1) {
  873. ret = res;
  874. goto out;
  875. }
  876. elems_len -= 2 + subelems[1];
  877. subelems += 2 + subelems[1];
  878. }
  879. if (req->mode == BEACON_REPORT_MODE_TABLE) {
  880. wpas_beacon_rep_table(wpa_s, buf);
  881. goto out;
  882. }
  883. params->freqs = wpas_beacon_request_freqs(
  884. wpa_s, req->oper_class, req->channel,
  885. req->mode == BEACON_REPORT_MODE_ACTIVE,
  886. req->variable, len - sizeof(*req));
  887. if (!params->freqs) {
  888. wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
  889. goto out;
  890. }
  891. params->duration = le_to_host16(req->duration);
  892. params->duration_mandatory = duration_mandatory;
  893. if (!params->duration) {
  894. wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
  895. ret = -1;
  896. goto out;
  897. }
  898. params->only_new_results = 1;
  899. if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
  900. params->ssids[params->num_ssids].ssid = data->ssid;
  901. params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
  902. }
  903. if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
  904. _rand = os_random();
  905. interval_usec = (_rand % (rand_interval + 1)) * 1024;
  906. eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
  907. NULL);
  908. return 1;
  909. out:
  910. wpas_clear_beacon_rep_data(wpa_s);
  911. return ret;
  912. }
  913. static int
  914. wpas_rrm_handle_msr_req_element(
  915. struct wpa_supplicant *wpa_s,
  916. const struct rrm_measurement_request_element *req,
  917. struct wpabuf **buf)
  918. {
  919. int duration_mandatory;
  920. wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
  921. req->type, req->token);
  922. if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
  923. /* Enable bit is not supported for now */
  924. wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
  925. return 0;
  926. }
  927. if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
  928. req->type > MEASURE_TYPE_RPI_HIST) {
  929. /* Parallel measurements are not supported for now */
  930. wpa_printf(MSG_DEBUG,
  931. "RRM: Parallel measurements are not supported, reject");
  932. goto reject;
  933. }
  934. duration_mandatory =
  935. !!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
  936. switch (req->type) {
  937. case MEASURE_TYPE_LCI:
  938. return wpas_rrm_build_lci_report(wpa_s, req, buf);
  939. case MEASURE_TYPE_BEACON:
  940. if (duration_mandatory &&
  941. !(wpa_s->drv_rrm_flags &
  942. WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
  943. wpa_printf(MSG_DEBUG,
  944. "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
  945. goto reject;
  946. }
  947. return wpas_rm_handle_beacon_req(wpa_s, req->token,
  948. duration_mandatory,
  949. (const void *) req->variable,
  950. req->len - 3, buf);
  951. default:
  952. wpa_printf(MSG_INFO,
  953. "RRM: Unsupported radio measurement type %u",
  954. req->type);
  955. break;
  956. }
  957. reject:
  958. if (wpas_rrm_report_elem(buf, req->token,
  959. MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
  960. req->type, NULL, 0) < 0) {
  961. wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
  962. return -1;
  963. }
  964. return 0;
  965. }
  966. static struct wpabuf *
  967. wpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
  968. size_t len)
  969. {
  970. struct wpabuf *buf = NULL;
  971. while (len) {
  972. const struct rrm_measurement_request_element *req;
  973. int res;
  974. if (len < 2) {
  975. wpa_printf(MSG_DEBUG, "RRM: Truncated element");
  976. goto out;
  977. }
  978. req = (const struct rrm_measurement_request_element *) pos;
  979. if (req->eid != WLAN_EID_MEASURE_REQUEST) {
  980. wpa_printf(MSG_DEBUG,
  981. "RRM: Expected Measurement Request element, but EID is %u",
  982. req->eid);
  983. goto out;
  984. }
  985. if (req->len < 3) {
  986. wpa_printf(MSG_DEBUG, "RRM: Element length too short");
  987. goto out;
  988. }
  989. if (req->len > len - 2) {
  990. wpa_printf(MSG_DEBUG, "RRM: Element length too long");
  991. goto out;
  992. }
  993. res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
  994. if (res < 0)
  995. goto out;
  996. pos += req->len + 2;
  997. len -= req->len + 2;
  998. }
  999. return buf;
  1000. out:
  1001. wpabuf_free(buf);
  1002. return NULL;
  1003. }
  1004. void wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
  1005. const u8 *src,
  1006. const u8 *frame, size_t len)
  1007. {
  1008. struct wpabuf *report;
  1009. if (wpa_s->wpa_state != WPA_COMPLETED) {
  1010. wpa_printf(MSG_INFO,
  1011. "RRM: Ignoring radio measurement request: Not associated");
  1012. return;
  1013. }
  1014. if (!wpa_s->rrm.rrm_used) {
  1015. wpa_printf(MSG_INFO,
  1016. "RRM: Ignoring radio measurement request: Not RRM network");
  1017. return;
  1018. }
  1019. if (len < 3) {
  1020. wpa_printf(MSG_INFO,
  1021. "RRM: Ignoring too short radio measurement request");
  1022. return;
  1023. }
  1024. wpa_s->rrm.token = *frame;
  1025. /* Number of repetitions is not supported */
  1026. report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
  1027. if (!report)
  1028. return;
  1029. wpas_rrm_send_msr_report(wpa_s, report);
  1030. wpabuf_free(report);
  1031. }
  1032. void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
  1033. const u8 *src,
  1034. const u8 *frame, size_t len,
  1035. int rssi)
  1036. {
  1037. struct wpabuf *buf;
  1038. const struct rrm_link_measurement_request *req;
  1039. struct rrm_link_measurement_report report;
  1040. if (wpa_s->wpa_state != WPA_COMPLETED) {
  1041. wpa_printf(MSG_INFO,
  1042. "RRM: Ignoring link measurement request. Not associated");
  1043. return;
  1044. }
  1045. if (!wpa_s->rrm.rrm_used) {
  1046. wpa_printf(MSG_INFO,
  1047. "RRM: Ignoring link measurement request. Not RRM network");
  1048. return;
  1049. }
  1050. if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
  1051. wpa_printf(MSG_INFO,
  1052. "RRM: Measurement report failed. TX power insertion not supported");
  1053. return;
  1054. }
  1055. req = (const struct rrm_link_measurement_request *) frame;
  1056. if (len < sizeof(*req)) {
  1057. wpa_printf(MSG_INFO,
  1058. "RRM: Link measurement report failed. Request too short");
  1059. return;
  1060. }
  1061. os_memset(&report, 0, sizeof(report));
  1062. report.tpc.eid = WLAN_EID_TPC_REPORT;
  1063. report.tpc.len = 2;
  1064. report.rsni = 255; /* 255 indicates that RSNI is not available */
  1065. report.dialog_token = req->dialog_token;
  1066. report.rcpi = rssi_to_rcpi(rssi);
  1067. /* action_category + action_code */
  1068. buf = wpabuf_alloc(2 + sizeof(report));
  1069. if (buf == NULL) {
  1070. wpa_printf(MSG_ERROR,
  1071. "RRM: Link measurement report failed. Buffer allocation failed");
  1072. return;
  1073. }
  1074. wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
  1075. wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
  1076. wpabuf_put_data(buf, &report, sizeof(report));
  1077. wpa_hexdump(MSG_DEBUG, "RRM: Link measurement report:",
  1078. wpabuf_head(buf), wpabuf_len(buf));
  1079. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
  1080. wpa_s->own_addr, wpa_s->bssid,
  1081. wpabuf_head(buf), wpabuf_len(buf), 0)) {
  1082. wpa_printf(MSG_ERROR,
  1083. "RRM: Link measurement report failed. Send action failed");
  1084. }
  1085. wpabuf_free(buf);
  1086. }
  1087. int wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
  1088. struct wpa_scan_results *scan_res,
  1089. struct scan_info *info)
  1090. {
  1091. size_t i = 0;
  1092. struct wpabuf *buf = NULL;
  1093. if (!wpa_s->beacon_rep_data.token)
  1094. return 0;
  1095. if (!wpa_s->current_bss)
  1096. goto out;
  1097. /* If the measurement was aborted, don't report partial results */
  1098. if (info->aborted)
  1099. goto out;
  1100. wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
  1101. MAC2STR(info->scan_start_tsf_bssid),
  1102. MAC2STR(wpa_s->current_bss->bssid));
  1103. if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
  1104. os_memcmp(info->scan_start_tsf_bssid, wpa_s->current_bss->bssid,
  1105. ETH_ALEN) != 0) {
  1106. wpa_printf(MSG_DEBUG,
  1107. "RRM: Ignore scan results due to mismatching TSF BSSID");
  1108. goto out;
  1109. }
  1110. for (i = 0; i < scan_res->num; i++) {
  1111. struct wpa_bss *bss =
  1112. wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
  1113. if (!bss)
  1114. continue;
  1115. if ((wpa_s->drv_rrm_flags &
  1116. WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
  1117. os_memcmp(scan_res->res[i]->tsf_bssid,
  1118. wpa_s->current_bss->bssid, ETH_ALEN) != 0) {
  1119. wpa_printf(MSG_DEBUG,
  1120. "RRM: Ignore scan result for " MACSTR
  1121. " due to mismatching TSF BSSID" MACSTR,
  1122. MAC2STR(scan_res->res[i]->bssid),
  1123. MAC2STR(scan_res->res[i]->tsf_bssid));
  1124. continue;
  1125. }
  1126. if (!(wpa_s->drv_rrm_flags &
  1127. WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
  1128. struct os_reltime update_time, diff;
  1129. /* For now, allow 8 ms older results due to some
  1130. * unknown issue with cfg80211 BSS table updates during
  1131. * a scan with the current BSS.
  1132. * TODO: Fix this more properly to avoid having to have
  1133. * this type of hacks in place. */
  1134. calculate_update_time(&scan_res->fetch_time,
  1135. scan_res->res[i]->age,
  1136. &update_time);
  1137. os_reltime_sub(&wpa_s->beacon_rep_scan,
  1138. &update_time, &diff);
  1139. if (os_reltime_before(&update_time,
  1140. &wpa_s->beacon_rep_scan) &&
  1141. (diff.sec || diff.usec >= 8000)) {
  1142. wpa_printf(MSG_DEBUG,
  1143. "RRM: Ignore scan result for " MACSTR
  1144. " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
  1145. MAC2STR(scan_res->res[i]->bssid),
  1146. scan_res->res[i]->age,
  1147. (unsigned int) diff.sec,
  1148. (unsigned int) diff.usec);
  1149. continue;
  1150. }
  1151. }
  1152. /*
  1153. * Don't report results that were not received during the
  1154. * current measurement.
  1155. */
  1156. if (info->scan_start_tsf > scan_res->res[i]->parent_tsf)
  1157. continue;
  1158. if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
  1159. scan_res->res[i]->parent_tsf) < 0)
  1160. break;
  1161. }
  1162. if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
  1163. goto out;
  1164. wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
  1165. wpas_rrm_send_msr_report(wpa_s, buf);
  1166. wpabuf_free(buf);
  1167. out:
  1168. wpas_clear_beacon_rep_data(wpa_s);
  1169. return 1;
  1170. }
  1171. void wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
  1172. {
  1173. struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
  1174. eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
  1175. bitfield_free(data->eids);
  1176. os_free(data->scan_params.freqs);
  1177. os_memset(data, 0, sizeof(*data));
  1178. }