xref: /linux/net/wireless/pmsr.c (revision 165f2d2858013253042809df082b8df7e34e86d7)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2018 - 2019 Intel Corporation
4  */
5 #ifndef __PMSR_H
6 #define __PMSR_H
7 #include <net/cfg80211.h>
8 #include "core.h"
9 #include "nl80211.h"
10 #include "rdev-ops.h"
11 
12 static int pmsr_parse_ftm(struct cfg80211_registered_device *rdev,
13 			  struct nlattr *ftmreq,
14 			  struct cfg80211_pmsr_request_peer *out,
15 			  struct genl_info *info)
16 {
17 	const struct cfg80211_pmsr_capabilities *capa = rdev->wiphy.pmsr_capa;
18 	struct nlattr *tb[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1];
19 	u32 preamble = NL80211_PREAMBLE_DMG; /* only optional in DMG */
20 
21 	/* validate existing data */
22 	if (!(rdev->wiphy.pmsr_capa->ftm.bandwidths & BIT(out->chandef.width))) {
23 		NL_SET_ERR_MSG(info->extack, "FTM: unsupported bandwidth");
24 		return -EINVAL;
25 	}
26 
27 	/* no validation needed - was already done via nested policy */
28 	nla_parse_nested_deprecated(tb, NL80211_PMSR_FTM_REQ_ATTR_MAX, ftmreq,
29 				    NULL, NULL);
30 
31 	if (tb[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE])
32 		preamble = nla_get_u32(tb[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE]);
33 
34 	/* set up values - struct is 0-initialized */
35 	out->ftm.requested = true;
36 
37 	switch (out->chandef.chan->band) {
38 	case NL80211_BAND_60GHZ:
39 		/* optional */
40 		break;
41 	default:
42 		if (!tb[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE]) {
43 			NL_SET_ERR_MSG(info->extack,
44 				       "FTM: must specify preamble");
45 			return -EINVAL;
46 		}
47 	}
48 
49 	if (!(capa->ftm.preambles & BIT(preamble))) {
50 		NL_SET_ERR_MSG_ATTR(info->extack,
51 				    tb[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE],
52 				    "FTM: invalid preamble");
53 		return -EINVAL;
54 	}
55 
56 	out->ftm.preamble = preamble;
57 
58 	out->ftm.burst_period = 0;
59 	if (tb[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD])
60 		out->ftm.burst_period =
61 			nla_get_u32(tb[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD]);
62 
63 	out->ftm.asap = !!tb[NL80211_PMSR_FTM_REQ_ATTR_ASAP];
64 	if (out->ftm.asap && !capa->ftm.asap) {
65 		NL_SET_ERR_MSG_ATTR(info->extack,
66 				    tb[NL80211_PMSR_FTM_REQ_ATTR_ASAP],
67 				    "FTM: ASAP mode not supported");
68 		return -EINVAL;
69 	}
70 
71 	if (!out->ftm.asap && !capa->ftm.non_asap) {
72 		NL_SET_ERR_MSG(info->extack,
73 			       "FTM: non-ASAP mode not supported");
74 		return -EINVAL;
75 	}
76 
77 	out->ftm.num_bursts_exp = 0;
78 	if (tb[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP])
79 		out->ftm.num_bursts_exp =
80 			nla_get_u32(tb[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP]);
81 
82 	if (capa->ftm.max_bursts_exponent >= 0 &&
83 	    out->ftm.num_bursts_exp > capa->ftm.max_bursts_exponent) {
84 		NL_SET_ERR_MSG_ATTR(info->extack,
85 				    tb[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP],
86 				    "FTM: max NUM_BURSTS_EXP must be set lower than the device limit");
87 		return -EINVAL;
88 	}
89 
90 	out->ftm.burst_duration = 15;
91 	if (tb[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION])
92 		out->ftm.burst_duration =
93 			nla_get_u32(tb[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION]);
94 
95 	out->ftm.ftms_per_burst = 0;
96 	if (tb[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST])
97 		out->ftm.ftms_per_burst =
98 			nla_get_u32(tb[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST]);
99 
100 	if (capa->ftm.max_ftms_per_burst &&
101 	    (out->ftm.ftms_per_burst > capa->ftm.max_ftms_per_burst ||
102 	     out->ftm.ftms_per_burst == 0)) {
103 		NL_SET_ERR_MSG_ATTR(info->extack,
104 				    tb[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST],
105 				    "FTM: FTMs per burst must be set lower than the device limit but non-zero");
106 		return -EINVAL;
107 	}
108 
109 	out->ftm.ftmr_retries = 3;
110 	if (tb[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES])
111 		out->ftm.ftmr_retries =
112 			nla_get_u32(tb[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES]);
113 
114 	out->ftm.request_lci = !!tb[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI];
115 	if (out->ftm.request_lci && !capa->ftm.request_lci) {
116 		NL_SET_ERR_MSG_ATTR(info->extack,
117 				    tb[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI],
118 				    "FTM: LCI request not supported");
119 	}
120 
121 	out->ftm.request_civicloc =
122 		!!tb[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC];
123 	if (out->ftm.request_civicloc && !capa->ftm.request_civicloc) {
124 		NL_SET_ERR_MSG_ATTR(info->extack,
125 				    tb[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC],
126 			    "FTM: civic location request not supported");
127 	}
128 
129 	out->ftm.trigger_based =
130 		!!tb[NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED];
131 	if (out->ftm.trigger_based && !capa->ftm.trigger_based) {
132 		NL_SET_ERR_MSG_ATTR(info->extack,
133 				    tb[NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED],
134 				    "FTM: trigger based ranging is not supported");
135 		return -EINVAL;
136 	}
137 
138 	out->ftm.non_trigger_based =
139 		!!tb[NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED];
140 	if (out->ftm.non_trigger_based && !capa->ftm.non_trigger_based) {
141 		NL_SET_ERR_MSG_ATTR(info->extack,
142 				    tb[NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED],
143 				    "FTM: trigger based ranging is not supported");
144 		return -EINVAL;
145 	}
146 
147 	if (out->ftm.trigger_based && out->ftm.non_trigger_based) {
148 		NL_SET_ERR_MSG(info->extack,
149 			       "FTM: can't set both trigger based and non trigger based");
150 		return -EINVAL;
151 	}
152 
153 	if ((out->ftm.trigger_based || out->ftm.non_trigger_based) &&
154 	    out->ftm.preamble != NL80211_PREAMBLE_HE) {
155 		NL_SET_ERR_MSG_ATTR(info->extack,
156 				    tb[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE],
157 				    "FTM: non EDCA based ranging must use HE preamble");
158 		return -EINVAL;
159 	}
160 
161 	return 0;
162 }
163 
164 static int pmsr_parse_peer(struct cfg80211_registered_device *rdev,
165 			   struct nlattr *peer,
166 			   struct cfg80211_pmsr_request_peer *out,
167 			   struct genl_info *info)
168 {
169 	struct nlattr *tb[NL80211_PMSR_PEER_ATTR_MAX + 1];
170 	struct nlattr *req[NL80211_PMSR_REQ_ATTR_MAX + 1];
171 	struct nlattr *treq;
172 	int err, rem;
173 
174 	/* no validation needed - was already done via nested policy */
175 	nla_parse_nested_deprecated(tb, NL80211_PMSR_PEER_ATTR_MAX, peer,
176 				    NULL, NULL);
177 
178 	if (!tb[NL80211_PMSR_PEER_ATTR_ADDR] ||
179 	    !tb[NL80211_PMSR_PEER_ATTR_CHAN] ||
180 	    !tb[NL80211_PMSR_PEER_ATTR_REQ]) {
181 		NL_SET_ERR_MSG_ATTR(info->extack, peer,
182 				    "insufficient peer data");
183 		return -EINVAL;
184 	}
185 
186 	memcpy(out->addr, nla_data(tb[NL80211_PMSR_PEER_ATTR_ADDR]), ETH_ALEN);
187 
188 	/* reuse info->attrs */
189 	memset(info->attrs, 0, sizeof(*info->attrs) * (NL80211_ATTR_MAX + 1));
190 	/* need to validate here, we don't want to have validation recursion */
191 	err = nla_parse_nested_deprecated(info->attrs, NL80211_ATTR_MAX,
192 					  tb[NL80211_PMSR_PEER_ATTR_CHAN],
193 					  nl80211_policy, info->extack);
194 	if (err)
195 		return err;
196 
197 	err = nl80211_parse_chandef(rdev, info, &out->chandef);
198 	if (err)
199 		return err;
200 
201 	/* no validation needed - was already done via nested policy */
202 	nla_parse_nested_deprecated(req, NL80211_PMSR_REQ_ATTR_MAX,
203 				    tb[NL80211_PMSR_PEER_ATTR_REQ], NULL,
204 				    NULL);
205 
206 	if (!req[NL80211_PMSR_REQ_ATTR_DATA]) {
207 		NL_SET_ERR_MSG_ATTR(info->extack,
208 				    tb[NL80211_PMSR_PEER_ATTR_REQ],
209 				    "missing request type/data");
210 		return -EINVAL;
211 	}
212 
213 	if (req[NL80211_PMSR_REQ_ATTR_GET_AP_TSF])
214 		out->report_ap_tsf = true;
215 
216 	if (out->report_ap_tsf && !rdev->wiphy.pmsr_capa->report_ap_tsf) {
217 		NL_SET_ERR_MSG_ATTR(info->extack,
218 				    req[NL80211_PMSR_REQ_ATTR_GET_AP_TSF],
219 				    "reporting AP TSF is not supported");
220 		return -EINVAL;
221 	}
222 
223 	nla_for_each_nested(treq, req[NL80211_PMSR_REQ_ATTR_DATA], rem) {
224 		switch (nla_type(treq)) {
225 		case NL80211_PMSR_TYPE_FTM:
226 			err = pmsr_parse_ftm(rdev, treq, out, info);
227 			break;
228 		default:
229 			NL_SET_ERR_MSG_ATTR(info->extack, treq,
230 					    "unsupported measurement type");
231 			err = -EINVAL;
232 		}
233 	}
234 
235 	if (err)
236 		return err;
237 
238 	return 0;
239 }
240 
241 int nl80211_pmsr_start(struct sk_buff *skb, struct genl_info *info)
242 {
243 	struct nlattr *reqattr = info->attrs[NL80211_ATTR_PEER_MEASUREMENTS];
244 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
245 	struct wireless_dev *wdev = info->user_ptr[1];
246 	struct cfg80211_pmsr_request *req;
247 	struct nlattr *peers, *peer;
248 	int count, rem, err, idx;
249 
250 	if (!rdev->wiphy.pmsr_capa)
251 		return -EOPNOTSUPP;
252 
253 	if (!reqattr)
254 		return -EINVAL;
255 
256 	peers = nla_find(nla_data(reqattr), nla_len(reqattr),
257 			 NL80211_PMSR_ATTR_PEERS);
258 	if (!peers)
259 		return -EINVAL;
260 
261 	count = 0;
262 	nla_for_each_nested(peer, peers, rem) {
263 		count++;
264 
265 		if (count > rdev->wiphy.pmsr_capa->max_peers) {
266 			NL_SET_ERR_MSG_ATTR(info->extack, peer,
267 					    "Too many peers used");
268 			return -EINVAL;
269 		}
270 	}
271 
272 	req = kzalloc(struct_size(req, peers, count), GFP_KERNEL);
273 	if (!req)
274 		return -ENOMEM;
275 
276 	if (info->attrs[NL80211_ATTR_TIMEOUT])
277 		req->timeout = nla_get_u32(info->attrs[NL80211_ATTR_TIMEOUT]);
278 
279 	if (info->attrs[NL80211_ATTR_MAC]) {
280 		if (!rdev->wiphy.pmsr_capa->randomize_mac_addr) {
281 			NL_SET_ERR_MSG_ATTR(info->extack,
282 					    info->attrs[NL80211_ATTR_MAC],
283 					    "device cannot randomize MAC address");
284 			err = -EINVAL;
285 			goto out_err;
286 		}
287 
288 		err = nl80211_parse_random_mac(info->attrs, req->mac_addr,
289 					       req->mac_addr_mask);
290 		if (err)
291 			goto out_err;
292 	} else {
293 		memcpy(req->mac_addr, wdev_address(wdev), ETH_ALEN);
294 		eth_broadcast_addr(req->mac_addr_mask);
295 	}
296 
297 	idx = 0;
298 	nla_for_each_nested(peer, peers, rem) {
299 		/* NB: this reuses info->attrs, but we no longer need it */
300 		err = pmsr_parse_peer(rdev, peer, &req->peers[idx], info);
301 		if (err)
302 			goto out_err;
303 		idx++;
304 	}
305 
306 	req->n_peers = count;
307 	req->cookie = cfg80211_assign_cookie(rdev);
308 	req->nl_portid = info->snd_portid;
309 
310 	err = rdev_start_pmsr(rdev, wdev, req);
311 	if (err)
312 		goto out_err;
313 
314 	list_add_tail(&req->list, &wdev->pmsr_list);
315 
316 	nl_set_extack_cookie_u64(info->extack, req->cookie);
317 	return 0;
318 out_err:
319 	kfree(req);
320 	return err;
321 }
322 
323 void cfg80211_pmsr_complete(struct wireless_dev *wdev,
324 			    struct cfg80211_pmsr_request *req,
325 			    gfp_t gfp)
326 {
327 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
328 	struct sk_buff *msg;
329 	void *hdr;
330 
331 	trace_cfg80211_pmsr_complete(wdev->wiphy, wdev, req->cookie);
332 
333 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
334 	if (!msg)
335 		goto free_request;
336 
337 	hdr = nl80211hdr_put(msg, 0, 0, 0,
338 			     NL80211_CMD_PEER_MEASUREMENT_COMPLETE);
339 	if (!hdr)
340 		goto free_msg;
341 
342 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
343 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
344 			      NL80211_ATTR_PAD))
345 		goto free_msg;
346 
347 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->cookie,
348 			      NL80211_ATTR_PAD))
349 		goto free_msg;
350 
351 	genlmsg_end(msg, hdr);
352 	genlmsg_unicast(wiphy_net(wdev->wiphy), msg, req->nl_portid);
353 	goto free_request;
354 free_msg:
355 	nlmsg_free(msg);
356 free_request:
357 	spin_lock_bh(&wdev->pmsr_lock);
358 	list_del(&req->list);
359 	spin_unlock_bh(&wdev->pmsr_lock);
360 	kfree(req);
361 }
362 EXPORT_SYMBOL_GPL(cfg80211_pmsr_complete);
363 
364 static int nl80211_pmsr_send_ftm_res(struct sk_buff *msg,
365 				     struct cfg80211_pmsr_result *res)
366 {
367 	if (res->status == NL80211_PMSR_STATUS_FAILURE) {
368 		if (nla_put_u32(msg, NL80211_PMSR_FTM_RESP_ATTR_FAIL_REASON,
369 				res->ftm.failure_reason))
370 			goto error;
371 
372 		if (res->ftm.failure_reason ==
373 			NL80211_PMSR_FTM_FAILURE_PEER_BUSY &&
374 		    res->ftm.busy_retry_time &&
375 		    nla_put_u32(msg, NL80211_PMSR_FTM_RESP_ATTR_BUSY_RETRY_TIME,
376 				res->ftm.busy_retry_time))
377 			goto error;
378 
379 		return 0;
380 	}
381 
382 #define PUT(tp, attr, val)						\
383 	do {								\
384 		if (nla_put_##tp(msg,					\
385 				 NL80211_PMSR_FTM_RESP_ATTR_##attr,	\
386 				 res->ftm.val))				\
387 			goto error;					\
388 	} while (0)
389 
390 #define PUTOPT(tp, attr, val)						\
391 	do {								\
392 		if (res->ftm.val##_valid)				\
393 			PUT(tp, attr, val);				\
394 	} while (0)
395 
396 #define PUT_U64(attr, val)						\
397 	do {								\
398 		if (nla_put_u64_64bit(msg,				\
399 				      NL80211_PMSR_FTM_RESP_ATTR_##attr,\
400 				      res->ftm.val,			\
401 				      NL80211_PMSR_FTM_RESP_ATTR_PAD))	\
402 			goto error;					\
403 	} while (0)
404 
405 #define PUTOPT_U64(attr, val)						\
406 	do {								\
407 		if (res->ftm.val##_valid)				\
408 			PUT_U64(attr, val);				\
409 	} while (0)
410 
411 	if (res->ftm.burst_index >= 0)
412 		PUT(u32, BURST_INDEX, burst_index);
413 	PUTOPT(u32, NUM_FTMR_ATTEMPTS, num_ftmr_attempts);
414 	PUTOPT(u32, NUM_FTMR_SUCCESSES, num_ftmr_successes);
415 	PUT(u8, NUM_BURSTS_EXP, num_bursts_exp);
416 	PUT(u8, BURST_DURATION, burst_duration);
417 	PUT(u8, FTMS_PER_BURST, ftms_per_burst);
418 	PUTOPT(s32, RSSI_AVG, rssi_avg);
419 	PUTOPT(s32, RSSI_SPREAD, rssi_spread);
420 	if (res->ftm.tx_rate_valid &&
421 	    !nl80211_put_sta_rate(msg, &res->ftm.tx_rate,
422 				  NL80211_PMSR_FTM_RESP_ATTR_TX_RATE))
423 		goto error;
424 	if (res->ftm.rx_rate_valid &&
425 	    !nl80211_put_sta_rate(msg, &res->ftm.rx_rate,
426 				  NL80211_PMSR_FTM_RESP_ATTR_RX_RATE))
427 		goto error;
428 	PUTOPT_U64(RTT_AVG, rtt_avg);
429 	PUTOPT_U64(RTT_VARIANCE, rtt_variance);
430 	PUTOPT_U64(RTT_SPREAD, rtt_spread);
431 	PUTOPT_U64(DIST_AVG, dist_avg);
432 	PUTOPT_U64(DIST_VARIANCE, dist_variance);
433 	PUTOPT_U64(DIST_SPREAD, dist_spread);
434 	if (res->ftm.lci && res->ftm.lci_len &&
435 	    nla_put(msg, NL80211_PMSR_FTM_RESP_ATTR_LCI,
436 		    res->ftm.lci_len, res->ftm.lci))
437 		goto error;
438 	if (res->ftm.civicloc && res->ftm.civicloc_len &&
439 	    nla_put(msg, NL80211_PMSR_FTM_RESP_ATTR_CIVICLOC,
440 		    res->ftm.civicloc_len, res->ftm.civicloc))
441 		goto error;
442 #undef PUT
443 #undef PUTOPT
444 #undef PUT_U64
445 #undef PUTOPT_U64
446 
447 	return 0;
448 error:
449 	return -ENOSPC;
450 }
451 
452 static int nl80211_pmsr_send_result(struct sk_buff *msg,
453 				    struct cfg80211_pmsr_result *res)
454 {
455 	struct nlattr *pmsr, *peers, *peer, *resp, *data, *typedata;
456 
457 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
458 	if (!pmsr)
459 		goto error;
460 
461 	peers = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_PEERS);
462 	if (!peers)
463 		goto error;
464 
465 	peer = nla_nest_start_noflag(msg, 1);
466 	if (!peer)
467 		goto error;
468 
469 	if (nla_put(msg, NL80211_PMSR_PEER_ATTR_ADDR, ETH_ALEN, res->addr))
470 		goto error;
471 
472 	resp = nla_nest_start_noflag(msg, NL80211_PMSR_PEER_ATTR_RESP);
473 	if (!resp)
474 		goto error;
475 
476 	if (nla_put_u32(msg, NL80211_PMSR_RESP_ATTR_STATUS, res->status) ||
477 	    nla_put_u64_64bit(msg, NL80211_PMSR_RESP_ATTR_HOST_TIME,
478 			      res->host_time, NL80211_PMSR_RESP_ATTR_PAD))
479 		goto error;
480 
481 	if (res->ap_tsf_valid &&
482 	    nla_put_u64_64bit(msg, NL80211_PMSR_RESP_ATTR_AP_TSF,
483 			      res->ap_tsf, NL80211_PMSR_RESP_ATTR_PAD))
484 		goto error;
485 
486 	if (res->final && nla_put_flag(msg, NL80211_PMSR_RESP_ATTR_FINAL))
487 		goto error;
488 
489 	data = nla_nest_start_noflag(msg, NL80211_PMSR_RESP_ATTR_DATA);
490 	if (!data)
491 		goto error;
492 
493 	typedata = nla_nest_start_noflag(msg, res->type);
494 	if (!typedata)
495 		goto error;
496 
497 	switch (res->type) {
498 	case NL80211_PMSR_TYPE_FTM:
499 		if (nl80211_pmsr_send_ftm_res(msg, res))
500 			goto error;
501 		break;
502 	default:
503 		WARN_ON(1);
504 	}
505 
506 	nla_nest_end(msg, typedata);
507 	nla_nest_end(msg, data);
508 	nla_nest_end(msg, resp);
509 	nla_nest_end(msg, peer);
510 	nla_nest_end(msg, peers);
511 	nla_nest_end(msg, pmsr);
512 
513 	return 0;
514 error:
515 	return -ENOSPC;
516 }
517 
518 void cfg80211_pmsr_report(struct wireless_dev *wdev,
519 			  struct cfg80211_pmsr_request *req,
520 			  struct cfg80211_pmsr_result *result,
521 			  gfp_t gfp)
522 {
523 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
524 	struct sk_buff *msg;
525 	void *hdr;
526 	int err;
527 
528 	trace_cfg80211_pmsr_report(wdev->wiphy, wdev, req->cookie,
529 				   result->addr);
530 
531 	/*
532 	 * Currently, only variable items are LCI and civic location,
533 	 * both of which are reasonably short so we don't need to
534 	 * worry about them here for the allocation.
535 	 */
536 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
537 	if (!msg)
538 		return;
539 
540 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PEER_MEASUREMENT_RESULT);
541 	if (!hdr)
542 		goto free;
543 
544 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
545 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
546 			      NL80211_ATTR_PAD))
547 		goto free;
548 
549 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->cookie,
550 			      NL80211_ATTR_PAD))
551 		goto free;
552 
553 	err = nl80211_pmsr_send_result(msg, result);
554 	if (err) {
555 		pr_err_ratelimited("peer measurement result: message didn't fit!");
556 		goto free;
557 	}
558 
559 	genlmsg_end(msg, hdr);
560 	genlmsg_unicast(wiphy_net(wdev->wiphy), msg, req->nl_portid);
561 	return;
562 free:
563 	nlmsg_free(msg);
564 }
565 EXPORT_SYMBOL_GPL(cfg80211_pmsr_report);
566 
567 static void cfg80211_pmsr_process_abort(struct wireless_dev *wdev)
568 {
569 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
570 	struct cfg80211_pmsr_request *req, *tmp;
571 	LIST_HEAD(free_list);
572 
573 	lockdep_assert_held(&wdev->mtx);
574 
575 	spin_lock_bh(&wdev->pmsr_lock);
576 	list_for_each_entry_safe(req, tmp, &wdev->pmsr_list, list) {
577 		if (req->nl_portid)
578 			continue;
579 		list_move_tail(&req->list, &free_list);
580 	}
581 	spin_unlock_bh(&wdev->pmsr_lock);
582 
583 	list_for_each_entry_safe(req, tmp, &free_list, list) {
584 		rdev_abort_pmsr(rdev, wdev, req);
585 
586 		kfree(req);
587 	}
588 }
589 
590 void cfg80211_pmsr_free_wk(struct work_struct *work)
591 {
592 	struct wireless_dev *wdev = container_of(work, struct wireless_dev,
593 						 pmsr_free_wk);
594 
595 	wdev_lock(wdev);
596 	cfg80211_pmsr_process_abort(wdev);
597 	wdev_unlock(wdev);
598 }
599 
600 void cfg80211_pmsr_wdev_down(struct wireless_dev *wdev)
601 {
602 	struct cfg80211_pmsr_request *req;
603 	bool found = false;
604 
605 	spin_lock_bh(&wdev->pmsr_lock);
606 	list_for_each_entry(req, &wdev->pmsr_list, list) {
607 		found = true;
608 		req->nl_portid = 0;
609 	}
610 	spin_unlock_bh(&wdev->pmsr_lock);
611 
612 	if (found)
613 		cfg80211_pmsr_process_abort(wdev);
614 
615 	WARN_ON(!list_empty(&wdev->pmsr_list));
616 }
617 
618 void cfg80211_release_pmsr(struct wireless_dev *wdev, u32 portid)
619 {
620 	struct cfg80211_pmsr_request *req;
621 
622 	spin_lock_bh(&wdev->pmsr_lock);
623 	list_for_each_entry(req, &wdev->pmsr_list, list) {
624 		if (req->nl_portid == portid) {
625 			req->nl_portid = 0;
626 			schedule_work(&wdev->pmsr_free_wk);
627 		}
628 	}
629 	spin_unlock_bh(&wdev->pmsr_lock);
630 }
631 
632 #endif /* __PMSR_H */
633