rrm.c 38 KB

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