rrm.c 37 KB

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