rrm.c 38 KB

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