rrm.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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. if (len)
  338. next += next[1] + 2;
  339. }
  340. #undef MPDU_REPORT_LEN
  341. }
  342. static int wpas_add_channel(u8 op_class, u8 chan, u8 num_primary_channels,
  343. int *freqs)
  344. {
  345. size_t i;
  346. for (i = 0; i < num_primary_channels; i++) {
  347. u8 primary_chan = chan - (2 * num_primary_channels - 2) + i * 4;
  348. freqs[i] = ieee80211_chan_to_freq(NULL, op_class, primary_chan);
  349. /* ieee80211_chan_to_freq() is not really meant for this
  350. * conversion of 20 MHz primary channel numbers for wider VHT
  351. * channels, so handle those as special cases here for now. */
  352. if (freqs[i] < 0 &&
  353. (op_class == 128 || op_class == 129 || op_class == 130))
  354. freqs[i] = 5000 + 5 * primary_chan;
  355. if (freqs[i] < 0) {
  356. wpa_printf(MSG_DEBUG,
  357. "Beacon Report: Invalid channel %u",
  358. chan);
  359. return -1;
  360. }
  361. }
  362. return 0;
  363. }
  364. static int * wpas_add_channels(const struct oper_class_map *op,
  365. struct hostapd_hw_modes *mode, int active,
  366. const u8 *channels, const u8 size)
  367. {
  368. int *freqs, *next_freq;
  369. u8 num_primary_channels, i;
  370. u8 num_chans;
  371. num_chans = channels ? size :
  372. (op->max_chan - op->min_chan) / op->inc + 1;
  373. if (op->bw == BW80 || op->bw == BW80P80)
  374. num_primary_channels = 4;
  375. else if (op->bw == BW160)
  376. num_primary_channels = 8;
  377. else
  378. num_primary_channels = 1;
  379. /* one extra place for the zero-terminator */
  380. freqs = os_calloc(num_chans * num_primary_channels + 1, sizeof(*freqs));
  381. if (!freqs) {
  382. wpa_printf(MSG_ERROR,
  383. "Beacon Report: Failed to allocate freqs array");
  384. return NULL;
  385. }
  386. next_freq = freqs;
  387. for (i = 0; i < num_chans; i++) {
  388. u8 chan = channels ? channels[i] : op->min_chan + i * op->inc;
  389. enum chan_allowed res = verify_channel(mode, chan, op->bw);
  390. if (res == NOT_ALLOWED || (res == NO_IR && active))
  391. continue;
  392. if (wpas_add_channel(op->op_class, chan, num_primary_channels,
  393. next_freq) < 0) {
  394. os_free(freqs);
  395. return NULL;
  396. }
  397. next_freq += num_primary_channels;
  398. }
  399. if (!freqs[0]) {
  400. os_free(freqs);
  401. return NULL;
  402. }
  403. return freqs;
  404. }
  405. static int * wpas_op_class_freqs(const struct oper_class_map *op,
  406. struct hostapd_hw_modes *mode, int active)
  407. {
  408. u8 channels_80mhz[] = { 42, 58, 106, 122, 138, 155 };
  409. u8 channels_160mhz[] = { 50, 114 };
  410. /*
  411. * When adding all channels in the operating class, 80 + 80 MHz
  412. * operating classes are like 80 MHz channels because we add all valid
  413. * channels anyway.
  414. */
  415. if (op->bw == BW80 || op->bw == BW80P80)
  416. return wpas_add_channels(op, mode, active, channels_80mhz,
  417. ARRAY_SIZE(channels_80mhz));
  418. if (op->bw == BW160)
  419. return wpas_add_channels(op, mode, active, channels_160mhz,
  420. ARRAY_SIZE(channels_160mhz));
  421. return wpas_add_channels(op, mode, active, NULL, 0);
  422. }
  423. static int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s, int active,
  424. const char *country, const u8 *subelems,
  425. size_t len)
  426. {
  427. int *freqs = NULL, *new_freqs;
  428. const u8 *end = subelems + len;
  429. while (end - subelems > 2) {
  430. const struct oper_class_map *op;
  431. const u8 *ap_chan_elem, *pos;
  432. u8 left;
  433. struct hostapd_hw_modes *mode;
  434. ap_chan_elem = get_ie(subelems, end - subelems,
  435. WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
  436. if (!ap_chan_elem)
  437. break;
  438. pos = ap_chan_elem + 2;
  439. left = ap_chan_elem[1];
  440. if (left < 1)
  441. break;
  442. subelems = ap_chan_elem + 2 + left;
  443. op = get_oper_class(country, *pos);
  444. if (!op) {
  445. wpa_printf(MSG_DEBUG,
  446. "Beacon request: unknown operating class in AP Channel Report subelement %u",
  447. *pos);
  448. goto out;
  449. }
  450. pos++;
  451. left--;
  452. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
  453. if (!mode)
  454. continue;
  455. /*
  456. * For 80 + 80 MHz operating classes, this AP Channel Report
  457. * element should be followed by another element specifying
  458. * the second 80 MHz channel. For now just add this 80 MHz
  459. * channel, the second 80 MHz channel will be added when the
  460. * next element is parsed.
  461. * TODO: Verify that this AP Channel Report element is followed
  462. * by a corresponding AP Channel Report element as specified in
  463. * IEEE Std 802.11-2016, 11.11.9.1.
  464. */
  465. new_freqs = wpas_add_channels(op, mode, active, pos, left);
  466. if (new_freqs)
  467. int_array_concat(&freqs, new_freqs);
  468. os_free(new_freqs);
  469. }
  470. return freqs;
  471. out:
  472. os_free(freqs);
  473. return NULL;
  474. }
  475. static int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
  476. u8 op_class, u8 chan, int active,
  477. const u8 *subelems, size_t len)
  478. {
  479. int *freqs = NULL, *ext_freqs = NULL;
  480. struct hostapd_hw_modes *mode;
  481. const char *country = NULL;
  482. const struct oper_class_map *op;
  483. const u8 *elem;
  484. if (!wpa_s->current_bss)
  485. return NULL;
  486. elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
  487. if (elem && elem[1] >= 2)
  488. country = (const char *) (elem + 2);
  489. op = get_oper_class(country, op_class);
  490. if (!op) {
  491. wpa_printf(MSG_DEBUG,
  492. "Beacon request: invalid operating class %d",
  493. op_class);
  494. return NULL;
  495. }
  496. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
  497. if (!mode)
  498. return NULL;
  499. switch (chan) {
  500. case 0:
  501. freqs = wpas_op_class_freqs(op, mode, active);
  502. if (!freqs)
  503. return NULL;
  504. break;
  505. case 255:
  506. /* freqs will be added from AP channel subelements */
  507. break;
  508. default:
  509. freqs = wpas_add_channels(op, mode, active, &chan, 1);
  510. if (!freqs)
  511. return NULL;
  512. break;
  513. }
  514. ext_freqs = wpas_channel_report_freqs(wpa_s, active, country, subelems,
  515. len);
  516. if (ext_freqs) {
  517. int_array_concat(&freqs, ext_freqs);
  518. os_free(ext_freqs);
  519. }
  520. return freqs;
  521. }
  522. static int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
  523. u8 *op_class, u8 *chan, u8 *phy_type)
  524. {
  525. const u8 *ie;
  526. int sec_chan = 0, vht = 0;
  527. struct ieee80211_ht_operation *ht_oper = NULL;
  528. struct ieee80211_vht_operation *vht_oper = NULL;
  529. u8 seg0, seg1;
  530. ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
  531. if (ie && ie[1] >= sizeof(struct ieee80211_ht_operation)) {
  532. u8 sec_chan_offset;
  533. ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
  534. sec_chan_offset = ht_oper->ht_param &
  535. HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
  536. if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
  537. sec_chan = 1;
  538. else if (sec_chan_offset ==
  539. HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
  540. sec_chan = -1;
  541. }
  542. ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
  543. if (ie && ie[1] >= sizeof(struct ieee80211_vht_operation)) {
  544. vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
  545. switch (vht_oper->vht_op_info_chwidth) {
  546. case 1:
  547. seg0 = vht_oper->vht_op_info_chan_center_freq_seg0_idx;
  548. seg1 = vht_oper->vht_op_info_chan_center_freq_seg1_idx;
  549. if (seg1 && abs(seg1 - seg0) == 8)
  550. vht = VHT_CHANWIDTH_160MHZ;
  551. else if (seg1)
  552. vht = VHT_CHANWIDTH_80P80MHZ;
  553. else
  554. vht = VHT_CHANWIDTH_80MHZ;
  555. break;
  556. case 2:
  557. vht = VHT_CHANWIDTH_160MHZ;
  558. break;
  559. case 3:
  560. vht = VHT_CHANWIDTH_80P80MHZ;
  561. break;
  562. default:
  563. vht = VHT_CHANWIDTH_USE_HT;
  564. break;
  565. }
  566. }
  567. if (ieee80211_freq_to_channel_ext(freq, sec_chan, vht, op_class,
  568. chan) == NUM_HOSTAPD_MODES) {
  569. wpa_printf(MSG_DEBUG,
  570. "Cannot determine operating class and channel");
  571. return -1;
  572. }
  573. *phy_type = ieee80211_get_phy_type(freq, ht_oper != NULL,
  574. vht_oper != NULL);
  575. if (*phy_type == PHY_TYPE_UNSPECIFIED) {
  576. wpa_printf(MSG_DEBUG, "Cannot determine phy type");
  577. return -1;
  578. }
  579. return 0;
  580. }
  581. static int wpas_beacon_rep_add_frame_body(struct bitfield *eids,
  582. enum beacon_report_detail detail,
  583. struct wpa_bss *bss, u8 *buf,
  584. size_t buf_len)
  585. {
  586. u8 *ies = (u8 *) (bss + 1);
  587. size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
  588. u8 *pos = buf;
  589. int rem_len;
  590. rem_len = 255 - sizeof(struct rrm_measurement_beacon_report) -
  591. sizeof(struct rrm_measurement_report_element) - 2;
  592. if (detail > BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
  593. wpa_printf(MSG_DEBUG,
  594. "Beacon Request: Invalid reporting detail: %d",
  595. detail);
  596. return -1;
  597. }
  598. if (detail == BEACON_REPORT_DETAIL_NONE)
  599. return 0;
  600. /*
  601. * Minimal frame body subelement size: EID(1) + length(1) + TSF(8) +
  602. * beacon interval(2) + capabilities(2) = 14 bytes
  603. */
  604. if (buf_len < 14)
  605. return 0;
  606. *pos++ = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY;
  607. /* The length will be filled later */
  608. pos++;
  609. WPA_PUT_LE64(pos, bss->tsf);
  610. pos += sizeof(bss->tsf);
  611. WPA_PUT_LE16(pos, bss->beacon_int);
  612. pos += 2;
  613. WPA_PUT_LE16(pos, bss->caps);
  614. pos += 2;
  615. rem_len -= pos - buf;
  616. /*
  617. * According to IEEE Std 802.11-2016, 9.4.2.22.7, if the reported frame
  618. * body subelement causes the element to exceed the maximum element
  619. * size, the subelement is truncated so that the last IE is a complete
  620. * IE. So even when required to report all IEs, add elements one after
  621. * the other and stop once there is no more room in the measurement
  622. * element.
  623. */
  624. while (ies_len > 2 && 2U + ies[1] <= ies_len && rem_len > 0) {
  625. if (detail == BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS ||
  626. (eids && bitfield_is_set(eids, ies[0]))) {
  627. u8 eid = ies[0], elen = ies[1];
  628. if ((eid == WLAN_EID_TIM || eid == WLAN_EID_RSN) &&
  629. elen > 4)
  630. elen = 4;
  631. /*
  632. * TODO: Truncate IBSS DFS element as described in
  633. * IEEE Std 802.11-2016, 9.4.2.22.7.
  634. */
  635. if (2 + elen > buf + buf_len - pos ||
  636. 2 + elen > rem_len)
  637. break;
  638. *pos++ = ies[0];
  639. *pos++ = elen;
  640. os_memcpy(pos, ies + 2, elen);
  641. pos += elen;
  642. rem_len -= 2 + elen;
  643. }
  644. ies_len -= 2 + ies[1];
  645. ies += 2 + ies[1];
  646. }
  647. /* Now the length is known */
  648. buf[1] = pos - buf - 2;
  649. return pos - buf;
  650. }
  651. static int wpas_add_beacon_rep(struct wpa_supplicant *wpa_s,
  652. struct wpabuf **wpa_buf, struct wpa_bss *bss,
  653. u64 start, u64 parent_tsf)
  654. {
  655. struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
  656. u8 *ie = (u8 *) (bss + 1);
  657. size_t ie_len = bss->ie_len + bss->beacon_ie_len;
  658. int ret;
  659. u8 *buf;
  660. struct rrm_measurement_beacon_report *rep;
  661. if (os_memcmp(data->bssid, broadcast_ether_addr, ETH_ALEN) != 0 &&
  662. os_memcmp(data->bssid, bss->bssid, ETH_ALEN) != 0)
  663. return 0;
  664. if (data->ssid_len &&
  665. (data->ssid_len != bss->ssid_len ||
  666. os_memcmp(data->ssid, bss->ssid, bss->ssid_len) != 0))
  667. return 0;
  668. /* Maximum element length: beacon report element + reported frame body
  669. * subelement + all IEs of the reported beacon */
  670. buf = os_malloc(sizeof(*rep) + 14 + ie_len);
  671. if (!buf)
  672. return -1;
  673. rep = (struct rrm_measurement_beacon_report *) buf;
  674. if (wpas_get_op_chan_phy(bss->freq, ie, ie_len, &rep->op_class,
  675. &rep->channel, &rep->report_info) < 0) {
  676. ret = 0;
  677. goto out;
  678. }
  679. rep->start_time = host_to_le64(start);
  680. rep->duration = host_to_le16(data->scan_params.duration);
  681. rep->rcpi = rssi_to_rcpi(bss->level);
  682. rep->rsni = 255; /* 255 indicates that RSNI is not available */
  683. os_memcpy(rep->bssid, bss->bssid, ETH_ALEN);
  684. rep->antenna_id = 0; /* unknown */
  685. rep->parent_tsf = host_to_le32(parent_tsf);
  686. ret = wpas_beacon_rep_add_frame_body(data->eids, data->report_detail,
  687. bss, rep->variable, 14 + ie_len);
  688. if (ret < 0)
  689. goto out;
  690. ret = wpas_rrm_report_elem(wpa_buf, wpa_s->beacon_rep_data.token,
  691. MEASUREMENT_REPORT_MODE_ACCEPT,
  692. MEASURE_TYPE_BEACON, buf,
  693. ret + sizeof(*rep));
  694. out:
  695. os_free(buf);
  696. return ret;
  697. }
  698. static int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
  699. struct wpabuf **buf)
  700. {
  701. return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
  702. MEASUREMENT_REPORT_MODE_ACCEPT,
  703. MEASURE_TYPE_BEACON, NULL, 0);
  704. }
  705. static void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
  706. struct wpabuf **buf)
  707. {
  708. size_t i;
  709. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  710. if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
  711. 0, 0) < 0)
  712. break;
  713. }
  714. if (!(*buf))
  715. wpas_beacon_rep_no_results(wpa_s, buf);
  716. wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
  717. }
  718. static void wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
  719. {
  720. struct wpabuf *buf = NULL;
  721. if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
  722. MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
  723. MEASURE_TYPE_BEACON, NULL, 0)) {
  724. wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
  725. wpabuf_free(buf);
  726. return;
  727. }
  728. wpas_rrm_send_msr_report(wpa_s, buf);
  729. wpas_clear_beacon_rep_data(wpa_s);
  730. wpabuf_free(buf);
  731. }
  732. static void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  733. {
  734. struct wpa_supplicant *wpa_s = eloop_ctx;
  735. struct wpa_driver_scan_params *params =
  736. &wpa_s->beacon_rep_data.scan_params;
  737. u16 prev_duration = params->duration;
  738. if (!wpa_s->current_bss)
  739. return;
  740. if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
  741. params->duration) {
  742. wpa_printf(MSG_DEBUG,
  743. "RRM: Cannot set scan duration due to missing driver support");
  744. params->duration = 0;
  745. }
  746. os_get_reltime(&wpa_s->beacon_rep_scan);
  747. if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
  748. wpa_supplicant_trigger_scan(wpa_s, params))
  749. wpas_rrm_refuse_request(wpa_s);
  750. params->duration = prev_duration;
  751. }
  752. static int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
  753. struct beacon_rep_data *data,
  754. u8 sid, u8 slen, const u8 *subelem)
  755. {
  756. u8 report_info, i;
  757. switch (sid) {
  758. case WLAN_BEACON_REQUEST_SUBELEM_SSID:
  759. if (!slen) {
  760. wpa_printf(MSG_DEBUG,
  761. "SSID subelement with zero length - wildcard SSID");
  762. break;
  763. }
  764. if (slen > SSID_MAX_LEN) {
  765. wpa_printf(MSG_DEBUG,
  766. "Invalid SSID subelement length: %u", slen);
  767. return -1;
  768. }
  769. data->ssid_len = slen;
  770. os_memcpy(data->ssid, subelem, data->ssid_len);
  771. break;
  772. case WLAN_BEACON_REQUEST_SUBELEM_INFO:
  773. if (slen != 2) {
  774. wpa_printf(MSG_DEBUG,
  775. "Invalid reporting information subelement length: %u",
  776. slen);
  777. return -1;
  778. }
  779. report_info = subelem[0];
  780. if (report_info != 0) {
  781. wpa_printf(MSG_DEBUG,
  782. "reporting information=%u is not supported",
  783. report_info);
  784. return 0;
  785. }
  786. break;
  787. case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
  788. if (slen != 1) {
  789. wpa_printf(MSG_DEBUG,
  790. "Invalid reporting detail subelement length: %u",
  791. slen);
  792. return -1;
  793. }
  794. data->report_detail = subelem[0];
  795. if (data->report_detail >
  796. BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
  797. wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
  798. subelem[0]);
  799. return 0;
  800. }
  801. break;
  802. case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
  803. if (data->report_detail !=
  804. BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
  805. wpa_printf(MSG_DEBUG,
  806. "Beacon request: request subelement is present but report detail is %u",
  807. data->report_detail);
  808. return -1;
  809. }
  810. if (!slen) {
  811. wpa_printf(MSG_DEBUG,
  812. "Invalid request subelement length: %u",
  813. slen);
  814. return -1;
  815. }
  816. if (data->eids) {
  817. wpa_printf(MSG_DEBUG,
  818. "Beacon Request: Request subelement appears more than once");
  819. return -1;
  820. }
  821. data->eids = bitfield_alloc(255);
  822. if (!data->eids) {
  823. wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
  824. return -1;
  825. }
  826. for (i = 0; i < slen; i++)
  827. bitfield_set(data->eids, subelem[i]);
  828. break;
  829. case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
  830. /* Skip - it will be processed when freqs are added */
  831. break;
  832. default:
  833. wpa_printf(MSG_DEBUG,
  834. "Beacon request: Unknown subelement id %u", sid);
  835. break;
  836. }
  837. return 1;
  838. }
  839. /**
  840. * Returns 0 if the next element can be processed, 1 if some operation was
  841. * triggered, and -1 if processing failed (i.e., the element is in invalid
  842. * format or an internal error occurred).
  843. */
  844. static int
  845. wpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
  846. u8 elem_token, int duration_mandatory,
  847. const struct rrm_measurement_beacon_request *req,
  848. size_t len, struct wpabuf **buf)
  849. {
  850. struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
  851. struct wpa_driver_scan_params *params = &data->scan_params;
  852. const u8 *subelems;
  853. size_t elems_len;
  854. u16 rand_interval;
  855. u32 interval_usec;
  856. u32 _rand;
  857. int ret = 0, res;
  858. if (len < sizeof(*req))
  859. return -1;
  860. if (req->mode != BEACON_REPORT_MODE_PASSIVE &&
  861. req->mode != BEACON_REPORT_MODE_ACTIVE &&
  862. req->mode != BEACON_REPORT_MODE_TABLE)
  863. return 0;
  864. subelems = req->variable;
  865. elems_len = len - sizeof(*req);
  866. rand_interval = le_to_host16(req->rand_interval);
  867. os_free(params->freqs);
  868. os_memset(params, 0, sizeof(*params));
  869. data->token = elem_token;
  870. /* default reporting detail is all fixed length fields and all
  871. * elements */
  872. data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
  873. os_memcpy(data->bssid, req->bssid, ETH_ALEN);
  874. while (elems_len >= 2) {
  875. if (subelems[1] > elems_len - 2) {
  876. wpa_printf(MSG_DEBUG,
  877. "Beacon Request: Truncated subelement");
  878. ret = -1;
  879. goto out;
  880. }
  881. res = wpas_rm_handle_beacon_req_subelem(
  882. wpa_s, data, subelems[0], subelems[1], &subelems[2]);
  883. if (res != 1) {
  884. ret = res;
  885. goto out;
  886. }
  887. elems_len -= 2 + subelems[1];
  888. subelems += 2 + subelems[1];
  889. }
  890. if (req->mode == BEACON_REPORT_MODE_TABLE) {
  891. wpas_beacon_rep_table(wpa_s, buf);
  892. goto out;
  893. }
  894. params->freqs = wpas_beacon_request_freqs(
  895. wpa_s, req->oper_class, req->channel,
  896. req->mode == BEACON_REPORT_MODE_ACTIVE,
  897. req->variable, len - sizeof(*req));
  898. if (!params->freqs) {
  899. wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
  900. goto out;
  901. }
  902. params->duration = le_to_host16(req->duration);
  903. params->duration_mandatory = duration_mandatory;
  904. if (!params->duration) {
  905. wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
  906. ret = -1;
  907. goto out;
  908. }
  909. params->only_new_results = 1;
  910. if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
  911. params->ssids[params->num_ssids].ssid = data->ssid;
  912. params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
  913. }
  914. if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
  915. _rand = os_random();
  916. interval_usec = (_rand % (rand_interval + 1)) * 1024;
  917. eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
  918. NULL);
  919. return 1;
  920. out:
  921. wpas_clear_beacon_rep_data(wpa_s);
  922. return ret;
  923. }
  924. static int
  925. wpas_rrm_handle_msr_req_element(
  926. struct wpa_supplicant *wpa_s,
  927. const struct rrm_measurement_request_element *req,
  928. struct wpabuf **buf)
  929. {
  930. int duration_mandatory;
  931. wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
  932. req->type, req->token);
  933. if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
  934. /* Enable bit is not supported for now */
  935. wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
  936. return 0;
  937. }
  938. if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
  939. req->type > MEASURE_TYPE_RPI_HIST) {
  940. /* Parallel measurements are not supported for now */
  941. wpa_printf(MSG_DEBUG,
  942. "RRM: Parallel measurements are not supported, reject");
  943. goto reject;
  944. }
  945. duration_mandatory =
  946. !!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
  947. switch (req->type) {
  948. case MEASURE_TYPE_LCI:
  949. return wpas_rrm_build_lci_report(wpa_s, req, buf);
  950. case MEASURE_TYPE_BEACON:
  951. if (duration_mandatory &&
  952. !(wpa_s->drv_rrm_flags &
  953. WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
  954. wpa_printf(MSG_DEBUG,
  955. "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
  956. goto reject;
  957. }
  958. return wpas_rm_handle_beacon_req(wpa_s, req->token,
  959. duration_mandatory,
  960. (const void *) req->variable,
  961. req->len - 3, buf);
  962. default:
  963. wpa_printf(MSG_INFO,
  964. "RRM: Unsupported radio measurement type %u",
  965. req->type);
  966. break;
  967. }
  968. reject:
  969. if (wpas_rrm_report_elem(buf, req->token,
  970. MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
  971. req->type, NULL, 0) < 0) {
  972. wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
  973. return -1;
  974. }
  975. return 0;
  976. }
  977. static struct wpabuf *
  978. wpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
  979. size_t len)
  980. {
  981. struct wpabuf *buf = NULL;
  982. while (len) {
  983. const struct rrm_measurement_request_element *req;
  984. int res;
  985. if (len < 2) {
  986. wpa_printf(MSG_DEBUG, "RRM: Truncated element");
  987. goto out;
  988. }
  989. req = (const struct rrm_measurement_request_element *) pos;
  990. if (req->eid != WLAN_EID_MEASURE_REQUEST) {
  991. wpa_printf(MSG_DEBUG,
  992. "RRM: Expected Measurement Request element, but EID is %u",
  993. req->eid);
  994. goto out;
  995. }
  996. if (req->len < 3) {
  997. wpa_printf(MSG_DEBUG, "RRM: Element length too short");
  998. goto out;
  999. }
  1000. if (req->len > len - 2) {
  1001. wpa_printf(MSG_DEBUG, "RRM: Element length too long");
  1002. goto out;
  1003. }
  1004. res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
  1005. if (res < 0)
  1006. goto out;
  1007. pos += req->len + 2;
  1008. len -= req->len + 2;
  1009. }
  1010. return buf;
  1011. out:
  1012. wpabuf_free(buf);
  1013. return NULL;
  1014. }
  1015. void wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
  1016. const u8 *src,
  1017. const u8 *frame, size_t len)
  1018. {
  1019. struct wpabuf *report;
  1020. if (wpa_s->wpa_state != WPA_COMPLETED) {
  1021. wpa_printf(MSG_INFO,
  1022. "RRM: Ignoring radio measurement request: Not associated");
  1023. return;
  1024. }
  1025. if (!wpa_s->rrm.rrm_used) {
  1026. wpa_printf(MSG_INFO,
  1027. "RRM: Ignoring radio measurement request: Not RRM network");
  1028. return;
  1029. }
  1030. if (len < 3) {
  1031. wpa_printf(MSG_INFO,
  1032. "RRM: Ignoring too short radio measurement request");
  1033. return;
  1034. }
  1035. wpa_s->rrm.token = *frame;
  1036. /* Number of repetitions is not supported */
  1037. report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
  1038. if (!report)
  1039. return;
  1040. wpas_rrm_send_msr_report(wpa_s, report);
  1041. wpabuf_free(report);
  1042. }
  1043. void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
  1044. const u8 *src,
  1045. const u8 *frame, size_t len,
  1046. int rssi)
  1047. {
  1048. struct wpabuf *buf;
  1049. const struct rrm_link_measurement_request *req;
  1050. struct rrm_link_measurement_report report;
  1051. if (wpa_s->wpa_state != WPA_COMPLETED) {
  1052. wpa_printf(MSG_INFO,
  1053. "RRM: Ignoring link measurement request. Not associated");
  1054. return;
  1055. }
  1056. if (!wpa_s->rrm.rrm_used) {
  1057. wpa_printf(MSG_INFO,
  1058. "RRM: Ignoring link measurement request. Not RRM network");
  1059. return;
  1060. }
  1061. if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
  1062. wpa_printf(MSG_INFO,
  1063. "RRM: Measurement report failed. TX power insertion not supported");
  1064. return;
  1065. }
  1066. req = (const struct rrm_link_measurement_request *) frame;
  1067. if (len < sizeof(*req)) {
  1068. wpa_printf(MSG_INFO,
  1069. "RRM: Link measurement report failed. Request too short");
  1070. return;
  1071. }
  1072. os_memset(&report, 0, sizeof(report));
  1073. report.dialog_token = req->dialog_token;
  1074. report.tpc.eid = WLAN_EID_TPC_REPORT;
  1075. report.tpc.len = 2;
  1076. /* Note: The driver is expected to update report.tpc.tx_power and
  1077. * report.tpc.link_margin subfields when sending out this frame.
  1078. * Similarly, the driver would need to update report.rx_ant_id and
  1079. * report.tx_ant_id subfields. */
  1080. report.rsni = 255; /* 255 indicates that RSNI is not available */
  1081. report.rcpi = rssi_to_rcpi(rssi);
  1082. /* action_category + action_code */
  1083. buf = wpabuf_alloc(2 + sizeof(report));
  1084. if (buf == NULL) {
  1085. wpa_printf(MSG_ERROR,
  1086. "RRM: Link measurement report failed. Buffer allocation failed");
  1087. return;
  1088. }
  1089. wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
  1090. wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
  1091. wpabuf_put_data(buf, &report, sizeof(report));
  1092. wpa_hexdump_buf(MSG_DEBUG, "RRM: Link measurement report", buf);
  1093. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
  1094. wpa_s->own_addr, wpa_s->bssid,
  1095. wpabuf_head(buf), wpabuf_len(buf), 0)) {
  1096. wpa_printf(MSG_ERROR,
  1097. "RRM: Link measurement report failed. Send action failed");
  1098. }
  1099. wpabuf_free(buf);
  1100. }
  1101. int wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
  1102. struct wpa_scan_results *scan_res,
  1103. struct scan_info *info)
  1104. {
  1105. size_t i = 0;
  1106. struct wpabuf *buf = NULL;
  1107. if (!wpa_s->beacon_rep_data.token)
  1108. return 0;
  1109. if (!wpa_s->current_bss)
  1110. goto out;
  1111. /* If the measurement was aborted, don't report partial results */
  1112. if (info->aborted)
  1113. goto out;
  1114. wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
  1115. MAC2STR(info->scan_start_tsf_bssid),
  1116. MAC2STR(wpa_s->current_bss->bssid));
  1117. if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
  1118. os_memcmp(info->scan_start_tsf_bssid, wpa_s->current_bss->bssid,
  1119. ETH_ALEN) != 0) {
  1120. wpa_printf(MSG_DEBUG,
  1121. "RRM: Ignore scan results due to mismatching TSF BSSID");
  1122. goto out;
  1123. }
  1124. for (i = 0; i < scan_res->num; i++) {
  1125. struct wpa_bss *bss =
  1126. wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
  1127. if (!bss)
  1128. continue;
  1129. if ((wpa_s->drv_rrm_flags &
  1130. WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
  1131. os_memcmp(scan_res->res[i]->tsf_bssid,
  1132. wpa_s->current_bss->bssid, ETH_ALEN) != 0) {
  1133. wpa_printf(MSG_DEBUG,
  1134. "RRM: Ignore scan result for " MACSTR
  1135. " due to mismatching TSF BSSID" MACSTR,
  1136. MAC2STR(scan_res->res[i]->bssid),
  1137. MAC2STR(scan_res->res[i]->tsf_bssid));
  1138. continue;
  1139. }
  1140. if (!(wpa_s->drv_rrm_flags &
  1141. WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
  1142. struct os_reltime update_time, diff;
  1143. /* For now, allow 8 ms older results due to some
  1144. * unknown issue with cfg80211 BSS table updates during
  1145. * a scan with the current BSS.
  1146. * TODO: Fix this more properly to avoid having to have
  1147. * this type of hacks in place. */
  1148. calculate_update_time(&scan_res->fetch_time,
  1149. scan_res->res[i]->age,
  1150. &update_time);
  1151. os_reltime_sub(&wpa_s->beacon_rep_scan,
  1152. &update_time, &diff);
  1153. if (os_reltime_before(&update_time,
  1154. &wpa_s->beacon_rep_scan) &&
  1155. (diff.sec || diff.usec >= 8000)) {
  1156. wpa_printf(MSG_DEBUG,
  1157. "RRM: Ignore scan result for " MACSTR
  1158. " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
  1159. MAC2STR(scan_res->res[i]->bssid),
  1160. scan_res->res[i]->age,
  1161. (unsigned int) diff.sec,
  1162. (unsigned int) diff.usec);
  1163. continue;
  1164. }
  1165. }
  1166. /*
  1167. * Don't report results that were not received during the
  1168. * current measurement.
  1169. */
  1170. if (info->scan_start_tsf > scan_res->res[i]->parent_tsf)
  1171. continue;
  1172. if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
  1173. scan_res->res[i]->parent_tsf) < 0)
  1174. break;
  1175. }
  1176. if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
  1177. goto out;
  1178. wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
  1179. wpas_rrm_send_msr_report(wpa_s, buf);
  1180. wpabuf_free(buf);
  1181. out:
  1182. wpas_clear_beacon_rep_data(wpa_s);
  1183. return 1;
  1184. }
  1185. void wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
  1186. {
  1187. struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
  1188. eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
  1189. bitfield_free(data->eids);
  1190. os_free(data->scan_params.freqs);
  1191. os_memset(data, 0, sizeof(*data));
  1192. }