rrm.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  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 (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
  298. wpas_rrm_report_elem(buf, req->token,
  299. MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
  300. req->type, NULL, 0) < 0) {
  301. wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
  302. return -1;
  303. }
  304. return 0;
  305. }
  306. static void wpas_rrm_send_msr_report_mpdu(struct wpa_supplicant *wpa_s,
  307. const u8 *data, size_t len)
  308. {
  309. struct wpabuf *report = wpabuf_alloc(len + 3);
  310. if (!report)
  311. return;
  312. wpabuf_put_u8(report, WLAN_ACTION_RADIO_MEASUREMENT);
  313. wpabuf_put_u8(report, WLAN_RRM_RADIO_MEASUREMENT_REPORT);
  314. wpabuf_put_u8(report, wpa_s->rrm.token);
  315. wpabuf_put_data(report, data, len);
  316. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  317. wpa_s->own_addr, wpa_s->bssid,
  318. wpabuf_head(report), wpabuf_len(report), 0)) {
  319. wpa_printf(MSG_ERROR,
  320. "RRM: Radio measurement report failed: Sending Action frame failed");
  321. }
  322. wpabuf_free(report);
  323. }
  324. static void wpas_rrm_send_msr_report(struct wpa_supplicant *wpa_s,
  325. struct wpabuf *buf)
  326. {
  327. int len = wpabuf_len(buf);
  328. const u8 *pos = wpabuf_head_u8(buf), *next = pos;
  329. #define MPDU_REPORT_LEN (int) (IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN - 3)
  330. while (len) {
  331. int send_len = (len > MPDU_REPORT_LEN) ? next - pos : len;
  332. if (send_len == len ||
  333. (send_len + next[1] + 2) > MPDU_REPORT_LEN) {
  334. wpas_rrm_send_msr_report_mpdu(wpa_s, pos, send_len);
  335. len -= send_len;
  336. pos = next;
  337. }
  338. if (len)
  339. next += next[1] + 2;
  340. }
  341. #undef MPDU_REPORT_LEN
  342. }
  343. static int wpas_add_channel(u8 op_class, u8 chan, u8 num_primary_channels,
  344. int *freqs)
  345. {
  346. size_t i;
  347. for (i = 0; i < num_primary_channels; i++) {
  348. u8 primary_chan = chan - (2 * num_primary_channels - 2) + i * 4;
  349. freqs[i] = ieee80211_chan_to_freq(NULL, op_class, primary_chan);
  350. /* ieee80211_chan_to_freq() is not really meant for this
  351. * conversion of 20 MHz primary channel numbers for wider VHT
  352. * channels, so handle those as special cases here for now. */
  353. if (freqs[i] < 0 &&
  354. (op_class == 128 || op_class == 129 || op_class == 130))
  355. freqs[i] = 5000 + 5 * primary_chan;
  356. if (freqs[i] < 0) {
  357. wpa_printf(MSG_DEBUG,
  358. "Beacon Report: Invalid channel %u",
  359. chan);
  360. return -1;
  361. }
  362. }
  363. return 0;
  364. }
  365. static int * wpas_add_channels(const struct oper_class_map *op,
  366. struct hostapd_hw_modes *mode, int active,
  367. const u8 *channels, const u8 size)
  368. {
  369. int *freqs, *next_freq;
  370. u8 num_primary_channels, i;
  371. u8 num_chans;
  372. num_chans = channels ? size :
  373. (op->max_chan - op->min_chan) / op->inc + 1;
  374. if (op->bw == BW80 || op->bw == BW80P80)
  375. num_primary_channels = 4;
  376. else if (op->bw == BW160)
  377. num_primary_channels = 8;
  378. else
  379. num_primary_channels = 1;
  380. /* one extra place for the zero-terminator */
  381. freqs = os_calloc(num_chans * num_primary_channels + 1, sizeof(*freqs));
  382. if (!freqs) {
  383. wpa_printf(MSG_ERROR,
  384. "Beacon Report: Failed to allocate freqs array");
  385. return NULL;
  386. }
  387. next_freq = freqs;
  388. for (i = 0; i < num_chans; i++) {
  389. u8 chan = channels ? channels[i] : op->min_chan + i * op->inc;
  390. enum chan_allowed res = verify_channel(mode, chan, op->bw);
  391. if (res == NOT_ALLOWED || (res == NO_IR && active))
  392. continue;
  393. if (wpas_add_channel(op->op_class, chan, num_primary_channels,
  394. next_freq) < 0) {
  395. os_free(freqs);
  396. return NULL;
  397. }
  398. next_freq += num_primary_channels;
  399. }
  400. if (!freqs[0]) {
  401. os_free(freqs);
  402. return NULL;
  403. }
  404. return freqs;
  405. }
  406. static int * wpas_op_class_freqs(const struct oper_class_map *op,
  407. struct hostapd_hw_modes *mode, int active)
  408. {
  409. u8 channels_80mhz[] = { 42, 58, 106, 122, 138, 155 };
  410. u8 channels_160mhz[] = { 50, 114 };
  411. /*
  412. * When adding all channels in the operating class, 80 + 80 MHz
  413. * operating classes are like 80 MHz channels because we add all valid
  414. * channels anyway.
  415. */
  416. if (op->bw == BW80 || op->bw == BW80P80)
  417. return wpas_add_channels(op, mode, active, channels_80mhz,
  418. ARRAY_SIZE(channels_80mhz));
  419. if (op->bw == BW160)
  420. return wpas_add_channels(op, mode, active, channels_160mhz,
  421. ARRAY_SIZE(channels_160mhz));
  422. return wpas_add_channels(op, mode, active, NULL, 0);
  423. }
  424. static int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s, int active,
  425. const char *country, const u8 *subelems,
  426. size_t len)
  427. {
  428. int *freqs = NULL, *new_freqs;
  429. const u8 *end = subelems + len;
  430. while (end - subelems > 2) {
  431. const struct oper_class_map *op;
  432. const u8 *ap_chan_elem, *pos;
  433. u8 left;
  434. struct hostapd_hw_modes *mode;
  435. ap_chan_elem = get_ie(subelems, end - subelems,
  436. WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
  437. if (!ap_chan_elem)
  438. break;
  439. pos = ap_chan_elem + 2;
  440. left = ap_chan_elem[1];
  441. if (left < 1)
  442. break;
  443. subelems = ap_chan_elem + 2 + left;
  444. op = get_oper_class(country, *pos);
  445. if (!op) {
  446. wpa_printf(MSG_DEBUG,
  447. "Beacon request: unknown operating class in AP Channel Report subelement %u",
  448. *pos);
  449. goto out;
  450. }
  451. pos++;
  452. left--;
  453. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
  454. if (!mode)
  455. continue;
  456. /*
  457. * For 80 + 80 MHz operating classes, this AP Channel Report
  458. * element should be followed by another element specifying
  459. * the second 80 MHz channel. For now just add this 80 MHz
  460. * channel, the second 80 MHz channel will be added when the
  461. * next element is parsed.
  462. * TODO: Verify that this AP Channel Report element is followed
  463. * by a corresponding AP Channel Report element as specified in
  464. * IEEE Std 802.11-2016, 11.11.9.1.
  465. */
  466. new_freqs = wpas_add_channels(op, mode, active, pos, left);
  467. if (new_freqs)
  468. int_array_concat(&freqs, new_freqs);
  469. os_free(new_freqs);
  470. }
  471. return freqs;
  472. out:
  473. os_free(freqs);
  474. return NULL;
  475. }
  476. static int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
  477. u8 op_class, u8 chan, int active,
  478. const u8 *subelems, size_t len)
  479. {
  480. int *freqs = NULL, *ext_freqs = NULL;
  481. struct hostapd_hw_modes *mode;
  482. const char *country = NULL;
  483. const struct oper_class_map *op;
  484. const u8 *elem;
  485. if (!wpa_s->current_bss)
  486. return NULL;
  487. elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
  488. if (elem && elem[1] >= 2)
  489. country = (const char *) (elem + 2);
  490. op = get_oper_class(country, op_class);
  491. if (!op) {
  492. wpa_printf(MSG_DEBUG,
  493. "Beacon request: invalid operating class %d",
  494. op_class);
  495. return NULL;
  496. }
  497. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
  498. if (!mode)
  499. return NULL;
  500. switch (chan) {
  501. case 0:
  502. freqs = wpas_op_class_freqs(op, mode, active);
  503. if (!freqs)
  504. return NULL;
  505. break;
  506. case 255:
  507. /* freqs will be added from AP channel subelements */
  508. break;
  509. default:
  510. freqs = wpas_add_channels(op, mode, active, &chan, 1);
  511. if (!freqs)
  512. return NULL;
  513. break;
  514. }
  515. ext_freqs = wpas_channel_report_freqs(wpa_s, active, country, subelems,
  516. len);
  517. if (ext_freqs) {
  518. int_array_concat(&freqs, ext_freqs);
  519. os_free(ext_freqs);
  520. int_array_sort_unique(freqs);
  521. }
  522. return freqs;
  523. }
  524. static 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;
  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. /* Maximum element length: beacon report element + reported frame body
  671. * subelement + all IEs of the reported beacon */
  672. buf = os_malloc(sizeof(*rep) + 14 + ie_len);
  673. if (!buf)
  674. return -1;
  675. rep = (struct rrm_measurement_beacon_report *) buf;
  676. if (wpas_get_op_chan_phy(bss->freq, ie, ie_len, &rep->op_class,
  677. &rep->channel, &rep->report_info) < 0) {
  678. ret = 0;
  679. goto out;
  680. }
  681. rep->start_time = host_to_le64(start);
  682. rep->duration = host_to_le16(data->scan_params.duration);
  683. rep->rcpi = rssi_to_rcpi(bss->level);
  684. rep->rsni = 255; /* 255 indicates that RSNI is not available */
  685. os_memcpy(rep->bssid, bss->bssid, ETH_ALEN);
  686. rep->antenna_id = 0; /* unknown */
  687. rep->parent_tsf = host_to_le32(parent_tsf);
  688. ret = wpas_beacon_rep_add_frame_body(data->eids, data->report_detail,
  689. bss, rep->variable, 14 + ie_len);
  690. if (ret < 0)
  691. goto out;
  692. ret = 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. out:
  697. os_free(buf);
  698. return ret;
  699. }
  700. static int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
  701. struct wpabuf **buf)
  702. {
  703. return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
  704. MEASUREMENT_REPORT_MODE_ACCEPT,
  705. MEASURE_TYPE_BEACON, NULL, 0);
  706. }
  707. static void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
  708. struct wpabuf **buf)
  709. {
  710. size_t i;
  711. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  712. if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
  713. 0, 0) < 0)
  714. break;
  715. }
  716. if (!(*buf))
  717. wpas_beacon_rep_no_results(wpa_s, buf);
  718. wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
  719. }
  720. void wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
  721. {
  722. if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr)) {
  723. struct wpabuf *buf = NULL;
  724. if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
  725. MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
  726. MEASURE_TYPE_BEACON, NULL, 0)) {
  727. wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
  728. wpabuf_free(buf);
  729. return;
  730. }
  731. wpas_rrm_send_msr_report(wpa_s, buf);
  732. wpabuf_free(buf);
  733. }
  734. wpas_clear_beacon_rep_data(wpa_s);
  735. }
  736. static void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  737. {
  738. struct wpa_supplicant *wpa_s = eloop_ctx;
  739. struct wpa_driver_scan_params *params =
  740. &wpa_s->beacon_rep_data.scan_params;
  741. u16 prev_duration = params->duration;
  742. if (!wpa_s->current_bss)
  743. return;
  744. if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
  745. params->duration) {
  746. wpa_printf(MSG_DEBUG,
  747. "RRM: Cannot set scan duration due to missing driver support");
  748. params->duration = 0;
  749. }
  750. os_get_reltime(&wpa_s->beacon_rep_scan);
  751. if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
  752. wpa_supplicant_trigger_scan(wpa_s, params))
  753. wpas_rrm_refuse_request(wpa_s);
  754. params->duration = prev_duration;
  755. }
  756. static int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
  757. struct beacon_rep_data *data,
  758. u8 sid, u8 slen, const u8 *subelem)
  759. {
  760. u8 report_info, i;
  761. switch (sid) {
  762. case WLAN_BEACON_REQUEST_SUBELEM_SSID:
  763. if (!slen) {
  764. wpa_printf(MSG_DEBUG,
  765. "SSID subelement with zero length - wildcard SSID");
  766. break;
  767. }
  768. if (slen > SSID_MAX_LEN) {
  769. wpa_printf(MSG_DEBUG,
  770. "Invalid SSID subelement length: %u", slen);
  771. return -1;
  772. }
  773. data->ssid_len = slen;
  774. os_memcpy(data->ssid, subelem, data->ssid_len);
  775. break;
  776. case WLAN_BEACON_REQUEST_SUBELEM_INFO:
  777. if (slen != 2) {
  778. wpa_printf(MSG_DEBUG,
  779. "Invalid reporting information subelement length: %u",
  780. slen);
  781. return -1;
  782. }
  783. report_info = subelem[0];
  784. if (report_info != 0) {
  785. wpa_printf(MSG_DEBUG,
  786. "reporting information=%u is not supported",
  787. report_info);
  788. return 0;
  789. }
  790. break;
  791. case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
  792. if (slen != 1) {
  793. wpa_printf(MSG_DEBUG,
  794. "Invalid reporting detail subelement length: %u",
  795. slen);
  796. return -1;
  797. }
  798. data->report_detail = subelem[0];
  799. if (data->report_detail >
  800. BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
  801. wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
  802. subelem[0]);
  803. return -1;
  804. }
  805. break;
  806. case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
  807. if (data->report_detail !=
  808. BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
  809. wpa_printf(MSG_DEBUG,
  810. "Beacon request: request subelement is present but report detail is %u",
  811. data->report_detail);
  812. return -1;
  813. }
  814. if (!slen) {
  815. wpa_printf(MSG_DEBUG,
  816. "Invalid request subelement length: %u",
  817. slen);
  818. return -1;
  819. }
  820. if (data->eids) {
  821. wpa_printf(MSG_DEBUG,
  822. "Beacon Request: Request subelement appears more than once");
  823. return -1;
  824. }
  825. data->eids = bitfield_alloc(255);
  826. if (!data->eids) {
  827. wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
  828. return -1;
  829. }
  830. for (i = 0; i < slen; i++)
  831. bitfield_set(data->eids, subelem[i]);
  832. break;
  833. case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
  834. /* Skip - it will be processed when freqs are added */
  835. break;
  836. default:
  837. wpa_printf(MSG_DEBUG,
  838. "Beacon request: Unknown subelement id %u", sid);
  839. break;
  840. }
  841. return 1;
  842. }
  843. /**
  844. * Returns 0 if the next element can be processed, 1 if some operation was
  845. * triggered, and -1 if processing failed (i.e., the element is in invalid
  846. * format or an internal error occurred).
  847. */
  848. static int
  849. wpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
  850. u8 elem_token, int duration_mandatory,
  851. const struct rrm_measurement_beacon_request *req,
  852. size_t len, struct wpabuf **buf)
  853. {
  854. struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
  855. struct wpa_driver_scan_params *params = &data->scan_params;
  856. const u8 *subelems;
  857. size_t elems_len;
  858. u16 rand_interval;
  859. u32 interval_usec;
  860. u32 _rand;
  861. int ret = 0, res;
  862. u8 reject_mode;
  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_free(params->freqs);
  873. os_memset(params, 0, sizeof(*params));
  874. data->token = elem_token;
  875. /* default reporting detail is all fixed length fields and all
  876. * elements */
  877. data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
  878. os_memcpy(data->bssid, req->bssid, ETH_ALEN);
  879. while (elems_len >= 2) {
  880. if (subelems[1] > elems_len - 2) {
  881. wpa_printf(MSG_DEBUG,
  882. "Beacon Request: Truncated subelement");
  883. ret = -1;
  884. goto out;
  885. }
  886. res = wpas_rm_handle_beacon_req_subelem(
  887. wpa_s, data, subelems[0], subelems[1], &subelems[2]);
  888. if (res < 0) {
  889. ret = res;
  890. goto out;
  891. } else if (!res) {
  892. reject_mode = MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE;
  893. goto out_reject;
  894. }
  895. elems_len -= 2 + subelems[1];
  896. subelems += 2 + subelems[1];
  897. }
  898. if (req->mode == BEACON_REPORT_MODE_TABLE) {
  899. wpas_beacon_rep_table(wpa_s, buf);
  900. goto out;
  901. }
  902. params->freqs = wpas_beacon_request_freqs(
  903. wpa_s, req->oper_class, req->channel,
  904. req->mode == BEACON_REPORT_MODE_ACTIVE,
  905. req->variable, len - sizeof(*req));
  906. if (!params->freqs) {
  907. wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
  908. reject_mode = MEASUREMENT_REPORT_MODE_REJECT_REFUSED;
  909. goto out_reject;
  910. }
  911. params->duration = le_to_host16(req->duration);
  912. params->duration_mandatory = duration_mandatory;
  913. if (!params->duration) {
  914. wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
  915. ret = -1;
  916. goto out;
  917. }
  918. params->only_new_results = 1;
  919. if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
  920. params->ssids[params->num_ssids].ssid = data->ssid;
  921. params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
  922. }
  923. if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
  924. _rand = os_random();
  925. interval_usec = (_rand % (rand_interval + 1)) * 1024;
  926. eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
  927. NULL);
  928. return 1;
  929. out_reject:
  930. if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
  931. wpas_rrm_report_elem(buf, elem_token, reject_mode,
  932. MEASURE_TYPE_BEACON, NULL, 0) < 0) {
  933. wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
  934. ret = -1;
  935. }
  936. out:
  937. wpas_clear_beacon_rep_data(wpa_s);
  938. return ret;
  939. }
  940. static int
  941. wpas_rrm_handle_msr_req_element(
  942. struct wpa_supplicant *wpa_s,
  943. const struct rrm_measurement_request_element *req,
  944. struct wpabuf **buf)
  945. {
  946. int duration_mandatory;
  947. wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
  948. req->type, req->token);
  949. if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
  950. /* Enable bit is not supported for now */
  951. wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
  952. return 0;
  953. }
  954. if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
  955. req->type > MEASURE_TYPE_RPI_HIST) {
  956. /* Parallel measurements are not supported for now */
  957. wpa_printf(MSG_DEBUG,
  958. "RRM: Parallel measurements are not supported, reject");
  959. goto reject;
  960. }
  961. duration_mandatory =
  962. !!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
  963. switch (req->type) {
  964. case MEASURE_TYPE_LCI:
  965. return wpas_rrm_build_lci_report(wpa_s, req, buf);
  966. case MEASURE_TYPE_BEACON:
  967. if (duration_mandatory &&
  968. !(wpa_s->drv_rrm_flags &
  969. WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
  970. wpa_printf(MSG_DEBUG,
  971. "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
  972. goto reject;
  973. }
  974. return wpas_rm_handle_beacon_req(wpa_s, req->token,
  975. duration_mandatory,
  976. (const void *) req->variable,
  977. req->len - 3, buf);
  978. default:
  979. wpa_printf(MSG_INFO,
  980. "RRM: Unsupported radio measurement type %u",
  981. req->type);
  982. break;
  983. }
  984. reject:
  985. if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
  986. wpas_rrm_report_elem(buf, req->token,
  987. MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
  988. req->type, NULL, 0) < 0) {
  989. wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
  990. return -1;
  991. }
  992. return 0;
  993. }
  994. static struct wpabuf *
  995. wpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
  996. size_t len)
  997. {
  998. struct wpabuf *buf = NULL;
  999. while (len) {
  1000. const struct rrm_measurement_request_element *req;
  1001. int res;
  1002. if (len < 2) {
  1003. wpa_printf(MSG_DEBUG, "RRM: Truncated element");
  1004. goto out;
  1005. }
  1006. req = (const struct rrm_measurement_request_element *) pos;
  1007. if (req->eid != WLAN_EID_MEASURE_REQUEST) {
  1008. wpa_printf(MSG_DEBUG,
  1009. "RRM: Expected Measurement Request element, but EID is %u",
  1010. req->eid);
  1011. goto out;
  1012. }
  1013. if (req->len < 3) {
  1014. wpa_printf(MSG_DEBUG, "RRM: Element length too short");
  1015. goto out;
  1016. }
  1017. if (req->len > len - 2) {
  1018. wpa_printf(MSG_DEBUG, "RRM: Element length too long");
  1019. goto out;
  1020. }
  1021. res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
  1022. if (res < 0)
  1023. goto out;
  1024. pos += req->len + 2;
  1025. len -= req->len + 2;
  1026. }
  1027. return buf;
  1028. out:
  1029. wpabuf_free(buf);
  1030. return NULL;
  1031. }
  1032. void wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
  1033. const u8 *src, const u8 *dst,
  1034. const u8 *frame, size_t len)
  1035. {
  1036. struct wpabuf *report;
  1037. if (wpa_s->wpa_state != WPA_COMPLETED) {
  1038. wpa_printf(MSG_INFO,
  1039. "RRM: Ignoring radio measurement request: Not associated");
  1040. return;
  1041. }
  1042. if (!wpa_s->rrm.rrm_used) {
  1043. wpa_printf(MSG_INFO,
  1044. "RRM: Ignoring radio measurement request: Not RRM network");
  1045. return;
  1046. }
  1047. if (len < 3) {
  1048. wpa_printf(MSG_INFO,
  1049. "RRM: Ignoring too short radio measurement request");
  1050. return;
  1051. }
  1052. wpa_s->rrm.token = *frame;
  1053. os_memcpy(wpa_s->rrm.dst_addr, dst, ETH_ALEN);
  1054. /* Number of repetitions is not supported */
  1055. report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
  1056. if (!report)
  1057. return;
  1058. wpas_rrm_send_msr_report(wpa_s, report);
  1059. wpabuf_free(report);
  1060. }
  1061. void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
  1062. const u8 *src,
  1063. const u8 *frame, size_t len,
  1064. int rssi)
  1065. {
  1066. struct wpabuf *buf;
  1067. const struct rrm_link_measurement_request *req;
  1068. struct rrm_link_measurement_report report;
  1069. if (wpa_s->wpa_state != WPA_COMPLETED) {
  1070. wpa_printf(MSG_INFO,
  1071. "RRM: Ignoring link measurement request. Not associated");
  1072. return;
  1073. }
  1074. if (!wpa_s->rrm.rrm_used) {
  1075. wpa_printf(MSG_INFO,
  1076. "RRM: Ignoring link measurement request. Not RRM network");
  1077. return;
  1078. }
  1079. if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
  1080. wpa_printf(MSG_INFO,
  1081. "RRM: Measurement report failed. TX power insertion not supported");
  1082. return;
  1083. }
  1084. req = (const struct rrm_link_measurement_request *) frame;
  1085. if (len < sizeof(*req)) {
  1086. wpa_printf(MSG_INFO,
  1087. "RRM: Link measurement report failed. Request too short");
  1088. return;
  1089. }
  1090. os_memset(&report, 0, sizeof(report));
  1091. report.dialog_token = req->dialog_token;
  1092. report.tpc.eid = WLAN_EID_TPC_REPORT;
  1093. report.tpc.len = 2;
  1094. /* Note: The driver is expected to update report.tpc.tx_power and
  1095. * report.tpc.link_margin subfields when sending out this frame.
  1096. * Similarly, the driver would need to update report.rx_ant_id and
  1097. * report.tx_ant_id subfields. */
  1098. report.rsni = 255; /* 255 indicates that RSNI is not available */
  1099. report.rcpi = rssi_to_rcpi(rssi);
  1100. /* action_category + action_code */
  1101. buf = wpabuf_alloc(2 + sizeof(report));
  1102. if (buf == NULL) {
  1103. wpa_printf(MSG_ERROR,
  1104. "RRM: Link measurement report failed. Buffer allocation failed");
  1105. return;
  1106. }
  1107. wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
  1108. wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
  1109. wpabuf_put_data(buf, &report, sizeof(report));
  1110. wpa_hexdump_buf(MSG_DEBUG, "RRM: Link measurement report", buf);
  1111. if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
  1112. wpa_s->own_addr, wpa_s->bssid,
  1113. wpabuf_head(buf), wpabuf_len(buf), 0)) {
  1114. wpa_printf(MSG_ERROR,
  1115. "RRM: Link measurement report failed. Send action failed");
  1116. }
  1117. wpabuf_free(buf);
  1118. }
  1119. int wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
  1120. struct wpa_scan_results *scan_res,
  1121. struct scan_info *info)
  1122. {
  1123. size_t i = 0;
  1124. struct wpabuf *buf = NULL;
  1125. if (!wpa_s->beacon_rep_data.token)
  1126. return 0;
  1127. if (!wpa_s->current_bss)
  1128. goto out;
  1129. /* If the measurement was aborted, don't report partial results */
  1130. if (info->aborted)
  1131. goto out;
  1132. wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
  1133. MAC2STR(info->scan_start_tsf_bssid),
  1134. MAC2STR(wpa_s->current_bss->bssid));
  1135. if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
  1136. os_memcmp(info->scan_start_tsf_bssid, wpa_s->current_bss->bssid,
  1137. ETH_ALEN) != 0) {
  1138. wpa_printf(MSG_DEBUG,
  1139. "RRM: Ignore scan results due to mismatching TSF BSSID");
  1140. goto out;
  1141. }
  1142. for (i = 0; i < scan_res->num; i++) {
  1143. struct wpa_bss *bss =
  1144. wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
  1145. if (!bss)
  1146. continue;
  1147. if ((wpa_s->drv_rrm_flags &
  1148. WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
  1149. os_memcmp(scan_res->res[i]->tsf_bssid,
  1150. wpa_s->current_bss->bssid, ETH_ALEN) != 0) {
  1151. wpa_printf(MSG_DEBUG,
  1152. "RRM: Ignore scan result for " MACSTR
  1153. " due to mismatching TSF BSSID" MACSTR,
  1154. MAC2STR(scan_res->res[i]->bssid),
  1155. MAC2STR(scan_res->res[i]->tsf_bssid));
  1156. continue;
  1157. }
  1158. /*
  1159. * Don't report results that were not received during the
  1160. * current measurement.
  1161. */
  1162. if (!(wpa_s->drv_rrm_flags &
  1163. WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
  1164. struct os_reltime update_time, diff;
  1165. /* For now, allow 8 ms older results due to some
  1166. * unknown issue with cfg80211 BSS table updates during
  1167. * a scan with the current BSS.
  1168. * TODO: Fix this more properly to avoid having to have
  1169. * this type of hacks in place. */
  1170. calculate_update_time(&scan_res->fetch_time,
  1171. scan_res->res[i]->age,
  1172. &update_time);
  1173. os_reltime_sub(&wpa_s->beacon_rep_scan,
  1174. &update_time, &diff);
  1175. if (os_reltime_before(&update_time,
  1176. &wpa_s->beacon_rep_scan) &&
  1177. (diff.sec || diff.usec >= 8000)) {
  1178. wpa_printf(MSG_DEBUG,
  1179. "RRM: Ignore scan result for " MACSTR
  1180. " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
  1181. MAC2STR(scan_res->res[i]->bssid),
  1182. scan_res->res[i]->age,
  1183. (unsigned int) diff.sec,
  1184. (unsigned int) diff.usec);
  1185. continue;
  1186. }
  1187. } else if (info->scan_start_tsf >
  1188. scan_res->res[i]->parent_tsf) {
  1189. continue;
  1190. }
  1191. if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
  1192. scan_res->res[i]->parent_tsf) < 0)
  1193. break;
  1194. }
  1195. if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
  1196. goto out;
  1197. wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
  1198. wpas_rrm_send_msr_report(wpa_s, buf);
  1199. wpabuf_free(buf);
  1200. out:
  1201. wpas_clear_beacon_rep_data(wpa_s);
  1202. return 1;
  1203. }
  1204. void wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
  1205. {
  1206. struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
  1207. eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
  1208. bitfield_free(data->eids);
  1209. os_free(data->scan_params.freqs);
  1210. os_memset(data, 0, sizeof(*data));
  1211. }