xref: /openbmc/linux/net/wireless/nl80211.c (revision d4c52c6a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This is the new netlink-based wireless configuration interface.
4  *
5  * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2015-2017	Intel Deutschland GmbH
8  * Copyright (C) 2018-2023 Intel Corporation
9  */
10 
11 #include <linux/if.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/list.h>
16 #include <linux/if_ether.h>
17 #include <linux/ieee80211.h>
18 #include <linux/nl80211.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/netlink.h>
21 #include <linux/nospec.h>
22 #include <linux/etherdevice.h>
23 #include <linux/if_vlan.h>
24 #include <net/net_namespace.h>
25 #include <net/genetlink.h>
26 #include <net/cfg80211.h>
27 #include <net/sock.h>
28 #include <net/inet_connection_sock.h>
29 #include "core.h"
30 #include "nl80211.h"
31 #include "reg.h"
32 #include "rdev-ops.h"
33 
34 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
35 				   struct genl_info *info,
36 				   struct cfg80211_crypto_settings *settings,
37 				   int cipher_limit);
38 
39 /* the netlink family */
40 static struct genl_family nl80211_fam;
41 
42 /* multicast groups */
43 enum nl80211_multicast_groups {
44 	NL80211_MCGRP_CONFIG,
45 	NL80211_MCGRP_SCAN,
46 	NL80211_MCGRP_REGULATORY,
47 	NL80211_MCGRP_MLME,
48 	NL80211_MCGRP_VENDOR,
49 	NL80211_MCGRP_NAN,
50 	NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
51 };
52 
53 static const struct genl_multicast_group nl80211_mcgrps[] = {
54 	[NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
55 	[NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
56 	[NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
57 	[NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
58 	[NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
59 	[NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
60 #ifdef CONFIG_NL80211_TESTMODE
61 	[NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
62 #endif
63 };
64 
65 /* returns ERR_PTR values */
66 static struct wireless_dev *
67 __cfg80211_wdev_from_attrs(struct cfg80211_registered_device *rdev,
68 			   struct net *netns, struct nlattr **attrs)
69 {
70 	struct wireless_dev *result = NULL;
71 	bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
72 	bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
73 	u64 wdev_id = 0;
74 	int wiphy_idx = -1;
75 	int ifidx = -1;
76 
77 	if (!have_ifidx && !have_wdev_id)
78 		return ERR_PTR(-EINVAL);
79 
80 	if (have_ifidx)
81 		ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
82 	if (have_wdev_id) {
83 		wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
84 		wiphy_idx = wdev_id >> 32;
85 	}
86 
87 	if (rdev) {
88 		struct wireless_dev *wdev;
89 
90 		lockdep_assert_held(&rdev->wiphy.mtx);
91 
92 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
93 			if (have_ifidx && wdev->netdev &&
94 			    wdev->netdev->ifindex == ifidx) {
95 				result = wdev;
96 				break;
97 			}
98 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
99 				result = wdev;
100 				break;
101 			}
102 		}
103 
104 		return result ?: ERR_PTR(-ENODEV);
105 	}
106 
107 	ASSERT_RTNL();
108 
109 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
110 		struct wireless_dev *wdev;
111 
112 		if (wiphy_net(&rdev->wiphy) != netns)
113 			continue;
114 
115 		if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
116 			continue;
117 
118 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
119 			if (have_ifidx && wdev->netdev &&
120 			    wdev->netdev->ifindex == ifidx) {
121 				result = wdev;
122 				break;
123 			}
124 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
125 				result = wdev;
126 				break;
127 			}
128 		}
129 
130 		if (result)
131 			break;
132 	}
133 
134 	if (result)
135 		return result;
136 	return ERR_PTR(-ENODEV);
137 }
138 
139 static struct cfg80211_registered_device *
140 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
141 {
142 	struct cfg80211_registered_device *rdev = NULL, *tmp;
143 	struct net_device *netdev;
144 
145 	ASSERT_RTNL();
146 
147 	if (!attrs[NL80211_ATTR_WIPHY] &&
148 	    !attrs[NL80211_ATTR_IFINDEX] &&
149 	    !attrs[NL80211_ATTR_WDEV])
150 		return ERR_PTR(-EINVAL);
151 
152 	if (attrs[NL80211_ATTR_WIPHY])
153 		rdev = cfg80211_rdev_by_wiphy_idx(
154 				nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
155 
156 	if (attrs[NL80211_ATTR_WDEV]) {
157 		u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
158 		struct wireless_dev *wdev;
159 		bool found = false;
160 
161 		tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
162 		if (tmp) {
163 			/* make sure wdev exists */
164 			list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
165 				if (wdev->identifier != (u32)wdev_id)
166 					continue;
167 				found = true;
168 				break;
169 			}
170 
171 			if (!found)
172 				tmp = NULL;
173 
174 			if (rdev && tmp != rdev)
175 				return ERR_PTR(-EINVAL);
176 			rdev = tmp;
177 		}
178 	}
179 
180 	if (attrs[NL80211_ATTR_IFINDEX]) {
181 		int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
182 
183 		netdev = __dev_get_by_index(netns, ifindex);
184 		if (netdev) {
185 			if (netdev->ieee80211_ptr)
186 				tmp = wiphy_to_rdev(
187 					netdev->ieee80211_ptr->wiphy);
188 			else
189 				tmp = NULL;
190 
191 			/* not wireless device -- return error */
192 			if (!tmp)
193 				return ERR_PTR(-EINVAL);
194 
195 			/* mismatch -- return error */
196 			if (rdev && tmp != rdev)
197 				return ERR_PTR(-EINVAL);
198 
199 			rdev = tmp;
200 		}
201 	}
202 
203 	if (!rdev)
204 		return ERR_PTR(-ENODEV);
205 
206 	if (netns != wiphy_net(&rdev->wiphy))
207 		return ERR_PTR(-ENODEV);
208 
209 	return rdev;
210 }
211 
212 /*
213  * This function returns a pointer to the driver
214  * that the genl_info item that is passed refers to.
215  *
216  * The result of this can be a PTR_ERR and hence must
217  * be checked with IS_ERR() for errors.
218  */
219 static struct cfg80211_registered_device *
220 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
221 {
222 	return __cfg80211_rdev_from_attrs(netns, info->attrs);
223 }
224 
225 static int validate_beacon_head(const struct nlattr *attr,
226 				struct netlink_ext_ack *extack)
227 {
228 	const u8 *data = nla_data(attr);
229 	unsigned int len = nla_len(attr);
230 	const struct element *elem;
231 	const struct ieee80211_mgmt *mgmt = (void *)data;
232 	unsigned int fixedlen, hdrlen;
233 	bool s1g_bcn;
234 
235 	if (len < offsetofend(typeof(*mgmt), frame_control))
236 		goto err;
237 
238 	s1g_bcn = ieee80211_is_s1g_beacon(mgmt->frame_control);
239 	if (s1g_bcn) {
240 		fixedlen = offsetof(struct ieee80211_ext,
241 				    u.s1g_beacon.variable);
242 		hdrlen = offsetof(struct ieee80211_ext, u.s1g_beacon);
243 	} else {
244 		fixedlen = offsetof(struct ieee80211_mgmt,
245 				    u.beacon.variable);
246 		hdrlen = offsetof(struct ieee80211_mgmt, u.beacon);
247 	}
248 
249 	if (len < fixedlen)
250 		goto err;
251 
252 	if (ieee80211_hdrlen(mgmt->frame_control) != hdrlen)
253 		goto err;
254 
255 	data += fixedlen;
256 	len -= fixedlen;
257 
258 	for_each_element(elem, data, len) {
259 		/* nothing */
260 	}
261 
262 	if (for_each_element_completed(elem, data, len))
263 		return 0;
264 
265 err:
266 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
267 	return -EINVAL;
268 }
269 
270 static int validate_ie_attr(const struct nlattr *attr,
271 			    struct netlink_ext_ack *extack)
272 {
273 	const u8 *data = nla_data(attr);
274 	unsigned int len = nla_len(attr);
275 	const struct element *elem;
276 
277 	for_each_element(elem, data, len) {
278 		/* nothing */
279 	}
280 
281 	if (for_each_element_completed(elem, data, len))
282 		return 0;
283 
284 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
285 	return -EINVAL;
286 }
287 
288 static int validate_he_capa(const struct nlattr *attr,
289 			    struct netlink_ext_ack *extack)
290 {
291 	if (!ieee80211_he_capa_size_ok(nla_data(attr), nla_len(attr)))
292 		return -EINVAL;
293 
294 	return 0;
295 }
296 
297 /* policy for the attributes */
298 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR];
299 
300 static const struct nla_policy
301 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
302 	[NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
303 	[NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
304 					.len = U8_MAX },
305 	[NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
306 					     .len = U8_MAX },
307 };
308 
309 static const struct nla_policy
310 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
311 	[NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
312 	[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
313 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
314 		NLA_POLICY_MAX(NLA_U8, 15),
315 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
316 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
317 		NLA_POLICY_MAX(NLA_U8, 15),
318 	[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
319 		NLA_POLICY_MAX(NLA_U8, 31),
320 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
321 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
322 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
323 	[NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED] = { .type = NLA_FLAG },
324 	[NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED] = { .type = NLA_FLAG },
325 	[NL80211_PMSR_FTM_REQ_ATTR_LMR_FEEDBACK] = { .type = NLA_FLAG },
326 	[NL80211_PMSR_FTM_REQ_ATTR_BSS_COLOR] = { .type = NLA_U8 },
327 };
328 
329 static const struct nla_policy
330 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
331 	[NL80211_PMSR_TYPE_FTM] =
332 		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
333 };
334 
335 static const struct nla_policy
336 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
337 	[NL80211_PMSR_REQ_ATTR_DATA] =
338 		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
339 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
340 };
341 
342 static const struct nla_policy
343 nl80211_pmsr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
344 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
345 	[NL80211_PMSR_PEER_ATTR_CHAN] = NLA_POLICY_NESTED(nl80211_policy),
346 	[NL80211_PMSR_PEER_ATTR_REQ] =
347 		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
348 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
349 };
350 
351 static const struct nla_policy
352 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
353 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
354 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
355 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
356 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
357 	[NL80211_PMSR_ATTR_PEERS] =
358 		NLA_POLICY_NESTED_ARRAY(nl80211_pmsr_peer_attr_policy),
359 };
360 
361 static const struct nla_policy
362 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
363 	[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
364 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
365 	[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
366 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
367 	[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET] =
368 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
369 	[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP] =
370 		NLA_POLICY_EXACT_LEN(8),
371 	[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP] =
372 		NLA_POLICY_EXACT_LEN(8),
373 	[NL80211_HE_OBSS_PD_ATTR_SR_CTRL] = { .type = NLA_U8 },
374 };
375 
376 static const struct nla_policy
377 he_bss_color_policy[NL80211_HE_BSS_COLOR_ATTR_MAX + 1] = {
378 	[NL80211_HE_BSS_COLOR_ATTR_COLOR] = NLA_POLICY_RANGE(NLA_U8, 1, 63),
379 	[NL80211_HE_BSS_COLOR_ATTR_DISABLED] = { .type = NLA_FLAG },
380 	[NL80211_HE_BSS_COLOR_ATTR_PARTIAL] = { .type = NLA_FLAG },
381 };
382 
383 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
384 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
385 				    .len = NL80211_MAX_SUPP_RATES },
386 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
387 				.len = NL80211_MAX_SUPP_HT_RATES },
388 	[NL80211_TXRATE_VHT] = NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_txrate_vht)),
389 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
390 	[NL80211_TXRATE_HE] = NLA_POLICY_EXACT_LEN(sizeof(struct nl80211_txrate_he)),
391 	[NL80211_TXRATE_HE_GI] =  NLA_POLICY_RANGE(NLA_U8,
392 						   NL80211_RATE_INFO_HE_GI_0_8,
393 						   NL80211_RATE_INFO_HE_GI_3_2),
394 	[NL80211_TXRATE_HE_LTF] = NLA_POLICY_RANGE(NLA_U8,
395 						   NL80211_RATE_INFO_HE_1XLTF,
396 						   NL80211_RATE_INFO_HE_4XLTF),
397 };
398 
399 static const struct nla_policy
400 nl80211_tid_config_attr_policy[NL80211_TID_CONFIG_ATTR_MAX + 1] = {
401 	[NL80211_TID_CONFIG_ATTR_VIF_SUPP] = { .type = NLA_U64 },
402 	[NL80211_TID_CONFIG_ATTR_PEER_SUPP] = { .type = NLA_U64 },
403 	[NL80211_TID_CONFIG_ATTR_OVERRIDE] = { .type = NLA_FLAG },
404 	[NL80211_TID_CONFIG_ATTR_TIDS] = NLA_POLICY_RANGE(NLA_U16, 1, 0xff),
405 	[NL80211_TID_CONFIG_ATTR_NOACK] =
406 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
407 	[NL80211_TID_CONFIG_ATTR_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
408 	[NL80211_TID_CONFIG_ATTR_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
409 	[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL] =
410 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
411 	[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL] =
412 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
413 	[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL] =
414 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
415 	[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE] =
416 			NLA_POLICY_MAX(NLA_U8, NL80211_TX_RATE_FIXED),
417 	[NL80211_TID_CONFIG_ATTR_TX_RATE] =
418 			NLA_POLICY_NESTED(nl80211_txattr_policy),
419 };
420 
421 static const struct nla_policy
422 nl80211_fils_discovery_policy[NL80211_FILS_DISCOVERY_ATTR_MAX + 1] = {
423 	[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] = NLA_POLICY_MAX(NLA_U32, 10000),
424 	[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] = NLA_POLICY_MAX(NLA_U32, 10000),
425 	[NL80211_FILS_DISCOVERY_ATTR_TMPL] =
426 			NLA_POLICY_RANGE(NLA_BINARY,
427 					 NL80211_FILS_DISCOVERY_TMPL_MIN_LEN,
428 					 IEEE80211_MAX_DATA_LEN),
429 };
430 
431 static const struct nla_policy
432 nl80211_unsol_bcast_probe_resp_policy[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1] = {
433 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] = NLA_POLICY_MAX(NLA_U32, 20),
434 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL] = { .type = NLA_BINARY,
435 						       .len = IEEE80211_MAX_DATA_LEN }
436 };
437 
438 static const struct nla_policy
439 sar_specs_policy[NL80211_SAR_ATTR_SPECS_MAX + 1] = {
440 	[NL80211_SAR_ATTR_SPECS_POWER] = { .type = NLA_S32 },
441 	[NL80211_SAR_ATTR_SPECS_RANGE_INDEX] = {.type = NLA_U32 },
442 };
443 
444 static const struct nla_policy
445 sar_policy[NL80211_SAR_ATTR_MAX + 1] = {
446 	[NL80211_SAR_ATTR_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_SAR_TYPE),
447 	[NL80211_SAR_ATTR_SPECS] = NLA_POLICY_NESTED_ARRAY(sar_specs_policy),
448 };
449 
450 static const struct nla_policy
451 nl80211_mbssid_config_policy[NL80211_MBSSID_CONFIG_ATTR_MAX + 1] = {
452 	[NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES] = NLA_POLICY_MIN(NLA_U8, 2),
453 	[NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY] =
454 						NLA_POLICY_MIN(NLA_U8, 1),
455 	[NL80211_MBSSID_CONFIG_ATTR_INDEX] = { .type = NLA_U8 },
456 	[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX] = { .type = NLA_U32 },
457 	[NL80211_MBSSID_CONFIG_ATTR_EMA] = { .type = NLA_FLAG },
458 };
459 
460 static const struct nla_policy
461 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
462 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
463 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
464 };
465 
466 static struct netlink_range_validation nl80211_punct_bitmap_range = {
467 	.min = 0,
468 	.max = 0xffff,
469 };
470 
471 static struct netlink_range_validation q_range = {
472 	.max = INT_MAX,
473 };
474 
475 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
476 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
477 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
478 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
479 				      .len = 20-1 },
480 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
481 
482 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
483 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
484 	[NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
485 						NL80211_EDMG_CHANNELS_MIN,
486 						NL80211_EDMG_CHANNELS_MAX),
487 	[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
488 						NL80211_EDMG_BW_CONFIG_MIN,
489 						NL80211_EDMG_BW_CONFIG_MAX),
490 
491 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
492 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
493 	[NL80211_ATTR_CENTER_FREQ1_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
494 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
495 
496 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
497 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
498 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
499 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
500 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
501 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
502 
503 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
504 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
505 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
506 
507 	[NL80211_ATTR_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
508 	[NL80211_ATTR_PREV_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
509 
510 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
511 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
512 				    .len = WLAN_MAX_KEY_LEN },
513 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 7),
514 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
515 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
516 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
517 	[NL80211_ATTR_KEY_TYPE] =
518 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
519 
520 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
521 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
522 	[NL80211_ATTR_BEACON_HEAD] =
523 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
524 				       IEEE80211_MAX_DATA_LEN),
525 	[NL80211_ATTR_BEACON_TAIL] =
526 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
527 				       IEEE80211_MAX_DATA_LEN),
528 	[NL80211_ATTR_STA_AID] =
529 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
530 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
531 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
532 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
533 					       .len = NL80211_MAX_SUPP_RATES },
534 	[NL80211_ATTR_STA_PLINK_ACTION] =
535 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
536 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
537 		NLA_POLICY_RANGE(NLA_U8,
538 				 NL80211_TX_POWER_AUTOMATIC,
539 				 NL80211_TX_POWER_FIXED),
540 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
541 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
542 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
543 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
544 				   .len = IEEE80211_MAX_MESH_ID_LEN },
545 	[NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
546 
547 	/* allow 3 for NUL-termination, we used to declare this NLA_STRING */
548 	[NL80211_ATTR_REG_ALPHA2] = NLA_POLICY_RANGE(NLA_BINARY, 2, 3),
549 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
550 
551 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
552 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
553 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
554 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
555 					   .len = NL80211_MAX_SUPP_RATES },
556 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
557 
558 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
559 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
560 
561 	[NL80211_ATTR_HT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_HT_CAPABILITY_LEN),
562 
563 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
564 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
565 						   validate_ie_attr,
566 						   IEEE80211_MAX_DATA_LEN),
567 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
568 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
569 
570 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
571 				.len = IEEE80211_MAX_SSID_LEN },
572 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
573 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
574 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
575 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
576 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
577 						  NL80211_MFP_NO,
578 						  NL80211_MFP_OPTIONAL),
579 	[NL80211_ATTR_STA_FLAGS2] =
580 		NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_sta_flag_update)),
581 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
582 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
583 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
584 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
585 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
586 	[NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
587 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
588 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
589 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
590 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
591 	[NL80211_ATTR_PMKID] = NLA_POLICY_EXACT_LEN_WARN(WLAN_PMKID_LEN),
592 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
593 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
594 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
595 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
596 				 .len = IEEE80211_MAX_DATA_LEN },
597 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
598 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
599 						   NL80211_PS_DISABLED,
600 						   NL80211_PS_ENABLED),
601 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
602 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
603 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
604 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
605 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
606 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
607 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
608 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
609 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
610 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
611 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
612 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
613 	[NL80211_ATTR_STA_PLINK_STATE] =
614 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
615 	[NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
616 	[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
617 	[NL80211_ATTR_MESH_PEER_AID] =
618 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
619 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
620 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
621 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
622 	[NL80211_ATTR_HIDDEN_SSID] =
623 		NLA_POLICY_RANGE(NLA_U32,
624 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
625 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
626 	[NL80211_ATTR_IE_PROBE_RESP] =
627 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
628 				       IEEE80211_MAX_DATA_LEN),
629 	[NL80211_ATTR_IE_ASSOC_RESP] =
630 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
631 				       IEEE80211_MAX_DATA_LEN),
632 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
633 	[NL80211_ATTR_STA_WME] = NLA_POLICY_NESTED(nl80211_sta_wme_policy),
634 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
635 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
636 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
637 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
638 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
639 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
640 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
641 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
642 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
643 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
644 				      .len = IEEE80211_MAX_DATA_LEN },
645 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
646 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
647 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
648 		.len = NL80211_HT_CAPABILITY_LEN
649 	},
650 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
651 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
652 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
653 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
654 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
655 
656 	/* need to include at least Auth Transaction and Status Code */
657 	[NL80211_ATTR_AUTH_DATA] = NLA_POLICY_MIN_LEN(4),
658 
659 	[NL80211_ATTR_VHT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_VHT_CAPABILITY_LEN),
660 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
661 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
662 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
663 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
664 		NLA_POLICY_RANGE(NLA_U32,
665 				 NL80211_MESH_POWER_UNKNOWN + 1,
666 				 NL80211_MESH_POWER_MAX),
667 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
668 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
669 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
670 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
671 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
672 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
673 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
674 		.len = NL80211_VHT_CAPABILITY_LEN,
675 	},
676 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
677 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
678 				  .len = IEEE80211_MAX_DATA_LEN },
679 	[NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
680 	[NL80211_ATTR_MAX_CRIT_PROT_DURATION] =
681 		NLA_POLICY_MAX(NLA_U16, NL80211_CRIT_PROTO_MAX_DURATION),
682 	[NL80211_ATTR_PEER_AID] =
683 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
684 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
685 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
686 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
687 	[NL80211_ATTR_CNTDWN_OFFS_BEACON] = { .type = NLA_BINARY },
688 	[NL80211_ATTR_CNTDWN_OFFS_PRESP] = { .type = NLA_BINARY },
689 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = NLA_POLICY_MIN_LEN(2),
690 	/*
691 	 * The value of the Length field of the Supported Operating
692 	 * Classes element is between 2 and 253.
693 	 */
694 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] =
695 		NLA_POLICY_RANGE(NLA_BINARY, 2, 253),
696 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
697 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
698 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
699 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
700 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
701 	[NL80211_ATTR_QOS_MAP] = NLA_POLICY_RANGE(NLA_BINARY,
702 						  IEEE80211_QOS_MAP_LEN_MIN,
703 						  IEEE80211_QOS_MAP_LEN_MAX),
704 	[NL80211_ATTR_MAC_HINT] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
705 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
706 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
707 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
708 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
709 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
710 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
711 	[NL80211_ATTR_USER_PRIO] =
712 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
713 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
714 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
715 	[NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
716 	[NL80211_ATTR_MAC_MASK] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
717 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
718 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
719 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
720 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
721 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
722 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
723 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
724 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
725 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
726 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
727 	},
728 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
729 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
730 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
731 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
732 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
733 				    .len = FILS_MAX_KEK_LEN },
734 	[NL80211_ATTR_FILS_NONCES] = NLA_POLICY_EXACT_LEN_WARN(2 * FILS_NONCE_LEN),
735 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
736 	[NL80211_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
737 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
738 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
739 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
740 	},
741 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
742 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
743 					     .len = FILS_ERP_MAX_USERNAME_LEN },
744 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
745 					  .len = FILS_ERP_MAX_REALM_LEN },
746 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
747 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
748 					.len = FILS_ERP_MAX_RRK_LEN },
749 	[NL80211_ATTR_FILS_CACHE_ID] = NLA_POLICY_EXACT_LEN_WARN(2),
750 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
751 	[NL80211_ATTR_PMKR0_NAME] = NLA_POLICY_EXACT_LEN(WLAN_PMK_NAME_LEN),
752 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
753 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
754 
755 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
756 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
757 	[NL80211_ATTR_TXQ_QUANTUM] = NLA_POLICY_FULL_RANGE(NLA_U32, &q_range),
758 	[NL80211_ATTR_HE_CAPABILITY] =
759 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_he_capa,
760 				       NL80211_HE_MAX_CAPABILITY_LEN),
761 	[NL80211_ATTR_FTM_RESPONDER] =
762 		NLA_POLICY_NESTED(nl80211_ftm_responder_policy),
763 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
764 	[NL80211_ATTR_PEER_MEASUREMENTS] =
765 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
766 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
767 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
768 					.len = SAE_PASSWORD_MAX_LEN },
769 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
770 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
771 	[NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
772 	[NL80211_ATTR_HE_BSS_COLOR] = NLA_POLICY_NESTED(he_bss_color_policy),
773 	[NL80211_ATTR_TID_CONFIG] =
774 		NLA_POLICY_NESTED_ARRAY(nl80211_tid_config_attr_policy),
775 	[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH] = { .type = NLA_FLAG },
776 	[NL80211_ATTR_PMK_LIFETIME] = NLA_POLICY_MIN(NLA_U32, 1),
777 	[NL80211_ATTR_PMK_REAUTH_THRESHOLD] = NLA_POLICY_RANGE(NLA_U8, 1, 100),
778 	[NL80211_ATTR_RECEIVE_MULTICAST] = { .type = NLA_FLAG },
779 	[NL80211_ATTR_WIPHY_FREQ_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
780 	[NL80211_ATTR_SCAN_FREQ_KHZ] = { .type = NLA_NESTED },
781 	[NL80211_ATTR_HE_6GHZ_CAPABILITY] =
782 		NLA_POLICY_EXACT_LEN(sizeof(struct ieee80211_he_6ghz_capa)),
783 	[NL80211_ATTR_FILS_DISCOVERY] =
784 		NLA_POLICY_NESTED(nl80211_fils_discovery_policy),
785 	[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP] =
786 		NLA_POLICY_NESTED(nl80211_unsol_bcast_probe_resp_policy),
787 	[NL80211_ATTR_S1G_CAPABILITY] =
788 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
789 	[NL80211_ATTR_S1G_CAPABILITY_MASK] =
790 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
791 	[NL80211_ATTR_SAE_PWE] =
792 		NLA_POLICY_RANGE(NLA_U8, NL80211_SAE_PWE_HUNT_AND_PECK,
793 				 NL80211_SAE_PWE_BOTH),
794 	[NL80211_ATTR_RECONNECT_REQUESTED] = { .type = NLA_REJECT },
795 	[NL80211_ATTR_SAR_SPEC] = NLA_POLICY_NESTED(sar_policy),
796 	[NL80211_ATTR_DISABLE_HE] = { .type = NLA_FLAG },
797 	[NL80211_ATTR_OBSS_COLOR_BITMAP] = { .type = NLA_U64 },
798 	[NL80211_ATTR_COLOR_CHANGE_COUNT] = { .type = NLA_U8 },
799 	[NL80211_ATTR_COLOR_CHANGE_COLOR] = { .type = NLA_U8 },
800 	[NL80211_ATTR_COLOR_CHANGE_ELEMS] = NLA_POLICY_NESTED(nl80211_policy),
801 	[NL80211_ATTR_MBSSID_CONFIG] =
802 			NLA_POLICY_NESTED(nl80211_mbssid_config_policy),
803 	[NL80211_ATTR_MBSSID_ELEMS] = { .type = NLA_NESTED },
804 	[NL80211_ATTR_RADAR_BACKGROUND] = { .type = NLA_FLAG },
805 	[NL80211_ATTR_AP_SETTINGS_FLAGS] = { .type = NLA_U32 },
806 	[NL80211_ATTR_EHT_CAPABILITY] =
807 		NLA_POLICY_RANGE(NLA_BINARY,
808 				 NL80211_EHT_MIN_CAPABILITY_LEN,
809 				 NL80211_EHT_MAX_CAPABILITY_LEN),
810 	[NL80211_ATTR_DISABLE_EHT] = { .type = NLA_FLAG },
811 	[NL80211_ATTR_MLO_LINKS] =
812 		NLA_POLICY_NESTED_ARRAY(nl80211_policy),
813 	[NL80211_ATTR_MLO_LINK_ID] =
814 		NLA_POLICY_RANGE(NLA_U8, 0, IEEE80211_MLD_MAX_NUM_LINKS),
815 	[NL80211_ATTR_MLD_ADDR] = NLA_POLICY_EXACT_LEN(ETH_ALEN),
816 	[NL80211_ATTR_MLO_SUPPORT] = { .type = NLA_FLAG },
817 	[NL80211_ATTR_MAX_NUM_AKM_SUITES] = { .type = NLA_REJECT },
818 	[NL80211_ATTR_PUNCT_BITMAP] =
819 		NLA_POLICY_FULL_RANGE(NLA_U32, &nl80211_punct_bitmap_range),
820 
821 	[NL80211_ATTR_MAX_HW_TIMESTAMP_PEERS] = { .type = NLA_U16 },
822 	[NL80211_ATTR_HW_TIMESTAMP_ENABLED] = { .type = NLA_FLAG },
823 	[NL80211_ATTR_EMA_RNR_ELEMS] = { .type = NLA_NESTED },
824 	[NL80211_ATTR_MLO_LINK_DISABLED] = { .type = NLA_FLAG },
825 };
826 
827 /* policy for the key attributes */
828 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
829 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
830 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
831 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
832 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
833 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
834 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
835 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
836 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
837 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
838 };
839 
840 /* policy for the key default flags */
841 static const struct nla_policy
842 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
843 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
844 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
845 };
846 
847 #ifdef CONFIG_PM
848 /* policy for WoWLAN attributes */
849 static const struct nla_policy
850 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
851 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
852 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
853 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
854 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
855 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
856 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
857 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
858 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
859 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
860 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
861 };
862 
863 static const struct nla_policy
864 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
865 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
866 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
867 	[NL80211_WOWLAN_TCP_DST_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
868 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
869 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
870 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
871 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
872 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
873 	},
874 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
875 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
876 	},
877 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
878 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
879 	[NL80211_WOWLAN_TCP_WAKE_MASK] = NLA_POLICY_MIN_LEN(1),
880 };
881 #endif /* CONFIG_PM */
882 
883 /* policy for coalesce rule attributes */
884 static const struct nla_policy
885 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
886 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
887 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
888 		NLA_POLICY_RANGE(NLA_U32,
889 				 NL80211_COALESCE_CONDITION_MATCH,
890 				 NL80211_COALESCE_CONDITION_NO_MATCH),
891 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
892 };
893 
894 /* policy for GTK rekey offload attributes */
895 static const struct nla_policy
896 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
897 	[NL80211_REKEY_DATA_KEK] = {
898 		.type = NLA_BINARY,
899 		.len = NL80211_KEK_EXT_LEN
900 	},
901 	[NL80211_REKEY_DATA_KCK] = {
902 		.type = NLA_BINARY,
903 		.len = NL80211_KCK_EXT_LEN_32
904 	},
905 	[NL80211_REKEY_DATA_REPLAY_CTR] = NLA_POLICY_EXACT_LEN(NL80211_REPLAY_CTR_LEN),
906 	[NL80211_REKEY_DATA_AKM] = { .type = NLA_U32 },
907 };
908 
909 static const struct nla_policy
910 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
911 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
912 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
913 	[NL80211_BAND_6GHZ] = { .type = NLA_S32 },
914 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
915 	[NL80211_BAND_LC]    = { .type = NLA_S32 },
916 };
917 
918 static const struct nla_policy
919 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
920 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
921 						 .len = IEEE80211_MAX_SSID_LEN },
922 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
923 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
924 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
925 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
926 };
927 
928 static const struct nla_policy
929 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
930 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
931 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
932 };
933 
934 static const struct nla_policy
935 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
936 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
937 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
938 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
939 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
940 	},
941 };
942 
943 /* policy for NAN function attributes */
944 static const struct nla_policy
945 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
946 	[NL80211_NAN_FUNC_TYPE] =
947 		NLA_POLICY_MAX(NLA_U8, NL80211_NAN_FUNC_MAX_TYPE),
948 	[NL80211_NAN_FUNC_SERVICE_ID] = {
949 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
950 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
951 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
952 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
953 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
954 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
955 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
956 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
957 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
958 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
959 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
960 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
961 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
962 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
963 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
964 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
965 };
966 
967 /* policy for Service Response Filter attributes */
968 static const struct nla_policy
969 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
970 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
971 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
972 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
973 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
974 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
975 };
976 
977 /* policy for packet pattern attributes */
978 static const struct nla_policy
979 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
980 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
981 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
982 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
983 };
984 
985 static int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
986 				     struct cfg80211_registered_device **rdev,
987 				     struct wireless_dev **wdev,
988 				     struct nlattr **attrbuf)
989 {
990 	int err;
991 
992 	if (!cb->args[0]) {
993 		struct nlattr **attrbuf_free = NULL;
994 
995 		if (!attrbuf) {
996 			attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
997 					  GFP_KERNEL);
998 			if (!attrbuf)
999 				return -ENOMEM;
1000 			attrbuf_free = attrbuf;
1001 		}
1002 
1003 		err = nlmsg_parse_deprecated(cb->nlh,
1004 					     GENL_HDRLEN + nl80211_fam.hdrsize,
1005 					     attrbuf, nl80211_fam.maxattr,
1006 					     nl80211_policy, NULL);
1007 		if (err) {
1008 			kfree(attrbuf_free);
1009 			return err;
1010 		}
1011 
1012 		rtnl_lock();
1013 		*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(cb->skb->sk),
1014 						   attrbuf);
1015 		kfree(attrbuf_free);
1016 		if (IS_ERR(*wdev)) {
1017 			rtnl_unlock();
1018 			return PTR_ERR(*wdev);
1019 		}
1020 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
1021 		mutex_lock(&(*rdev)->wiphy.mtx);
1022 		rtnl_unlock();
1023 		/* 0 is the first index - add 1 to parse only once */
1024 		cb->args[0] = (*rdev)->wiphy_idx + 1;
1025 		cb->args[1] = (*wdev)->identifier;
1026 	} else {
1027 		/* subtract the 1 again here */
1028 		struct wiphy *wiphy;
1029 		struct wireless_dev *tmp;
1030 
1031 		rtnl_lock();
1032 		wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
1033 		if (!wiphy) {
1034 			rtnl_unlock();
1035 			return -ENODEV;
1036 		}
1037 		*rdev = wiphy_to_rdev(wiphy);
1038 		*wdev = NULL;
1039 
1040 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
1041 			if (tmp->identifier == cb->args[1]) {
1042 				*wdev = tmp;
1043 				break;
1044 			}
1045 		}
1046 
1047 		if (!*wdev) {
1048 			rtnl_unlock();
1049 			return -ENODEV;
1050 		}
1051 		mutex_lock(&(*rdev)->wiphy.mtx);
1052 		rtnl_unlock();
1053 	}
1054 
1055 	return 0;
1056 }
1057 
1058 /* message building helper */
1059 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1060 		     int flags, u8 cmd)
1061 {
1062 	/* since there is no private header just add the generic one */
1063 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
1064 }
1065 
1066 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
1067 				     const struct ieee80211_reg_rule *rule)
1068 {
1069 	int j;
1070 	struct nlattr *nl_wmm_rules =
1071 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
1072 
1073 	if (!nl_wmm_rules)
1074 		goto nla_put_failure;
1075 
1076 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
1077 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
1078 
1079 		if (!nl_wmm_rule)
1080 			goto nla_put_failure;
1081 
1082 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
1083 				rule->wmm_rule.client[j].cw_min) ||
1084 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
1085 				rule->wmm_rule.client[j].cw_max) ||
1086 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
1087 			       rule->wmm_rule.client[j].aifsn) ||
1088 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
1089 			        rule->wmm_rule.client[j].cot))
1090 			goto nla_put_failure;
1091 
1092 		nla_nest_end(msg, nl_wmm_rule);
1093 	}
1094 	nla_nest_end(msg, nl_wmm_rules);
1095 
1096 	return 0;
1097 
1098 nla_put_failure:
1099 	return -ENOBUFS;
1100 }
1101 
1102 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
1103 				   struct ieee80211_channel *chan,
1104 				   bool large)
1105 {
1106 	/* Some channels must be completely excluded from the
1107 	 * list to protect old user-space tools from breaking
1108 	 */
1109 	if (!large && chan->flags &
1110 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
1111 		return 0;
1112 	if (!large && chan->freq_offset)
1113 		return 0;
1114 
1115 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
1116 			chan->center_freq))
1117 		goto nla_put_failure;
1118 
1119 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_OFFSET, chan->freq_offset))
1120 		goto nla_put_failure;
1121 
1122 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
1123 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
1124 		goto nla_put_failure;
1125 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
1126 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
1127 			goto nla_put_failure;
1128 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
1129 			goto nla_put_failure;
1130 	}
1131 	if (chan->flags & IEEE80211_CHAN_RADAR) {
1132 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
1133 			goto nla_put_failure;
1134 		if (large) {
1135 			u32 time;
1136 
1137 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
1138 
1139 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
1140 					chan->dfs_state))
1141 				goto nla_put_failure;
1142 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
1143 					time))
1144 				goto nla_put_failure;
1145 			if (nla_put_u32(msg,
1146 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
1147 					chan->dfs_cac_ms))
1148 				goto nla_put_failure;
1149 		}
1150 	}
1151 
1152 	if (large) {
1153 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
1154 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
1155 			goto nla_put_failure;
1156 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
1157 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
1158 			goto nla_put_failure;
1159 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
1160 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
1161 			goto nla_put_failure;
1162 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
1163 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
1164 			goto nla_put_failure;
1165 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
1166 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
1167 			goto nla_put_failure;
1168 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
1169 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
1170 			goto nla_put_failure;
1171 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
1172 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
1173 			goto nla_put_failure;
1174 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
1175 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
1176 			goto nla_put_failure;
1177 		if ((chan->flags & IEEE80211_CHAN_NO_HE) &&
1178 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HE))
1179 			goto nla_put_failure;
1180 		if ((chan->flags & IEEE80211_CHAN_1MHZ) &&
1181 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_1MHZ))
1182 			goto nla_put_failure;
1183 		if ((chan->flags & IEEE80211_CHAN_2MHZ) &&
1184 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_2MHZ))
1185 			goto nla_put_failure;
1186 		if ((chan->flags & IEEE80211_CHAN_4MHZ) &&
1187 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_4MHZ))
1188 			goto nla_put_failure;
1189 		if ((chan->flags & IEEE80211_CHAN_8MHZ) &&
1190 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_8MHZ))
1191 			goto nla_put_failure;
1192 		if ((chan->flags & IEEE80211_CHAN_16MHZ) &&
1193 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_16MHZ))
1194 			goto nla_put_failure;
1195 		if ((chan->flags & IEEE80211_CHAN_NO_320MHZ) &&
1196 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_320MHZ))
1197 			goto nla_put_failure;
1198 		if ((chan->flags & IEEE80211_CHAN_NO_EHT) &&
1199 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_EHT))
1200 			goto nla_put_failure;
1201 	}
1202 
1203 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
1204 			DBM_TO_MBM(chan->max_power)))
1205 		goto nla_put_failure;
1206 
1207 	if (large) {
1208 		const struct ieee80211_reg_rule *rule =
1209 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1210 
1211 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
1212 			if (nl80211_msg_put_wmm_rules(msg, rule))
1213 				goto nla_put_failure;
1214 		}
1215 	}
1216 
1217 	return 0;
1218 
1219  nla_put_failure:
1220 	return -ENOBUFS;
1221 }
1222 
1223 static bool nl80211_put_txq_stats(struct sk_buff *msg,
1224 				  struct cfg80211_txq_stats *txqstats,
1225 				  int attrtype)
1226 {
1227 	struct nlattr *txqattr;
1228 
1229 #define PUT_TXQVAL_U32(attr, memb) do {					  \
1230 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
1231 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
1232 		return false;						  \
1233 	} while (0)
1234 
1235 	txqattr = nla_nest_start_noflag(msg, attrtype);
1236 	if (!txqattr)
1237 		return false;
1238 
1239 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1240 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1241 	PUT_TXQVAL_U32(FLOWS, flows);
1242 	PUT_TXQVAL_U32(DROPS, drops);
1243 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1244 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1245 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1246 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1247 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1248 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1249 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1250 	nla_nest_end(msg, txqattr);
1251 
1252 #undef PUT_TXQVAL_U32
1253 	return true;
1254 }
1255 
1256 /* netlink command implementations */
1257 
1258 /**
1259  * nl80211_link_id - return link ID
1260  * @attrs: attributes to look at
1261  *
1262  * Returns: the link ID or 0 if not given
1263  *
1264  * Note this function doesn't do any validation of the link
1265  * ID validity wrt. links that were actually added, so it must
1266  * be called only from ops with %NL80211_FLAG_MLO_VALID_LINK_ID
1267  * or if additional validation is done.
1268  */
1269 static unsigned int nl80211_link_id(struct nlattr **attrs)
1270 {
1271 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1272 
1273 	if (!linkid)
1274 		return 0;
1275 
1276 	return nla_get_u8(linkid);
1277 }
1278 
1279 static int nl80211_link_id_or_invalid(struct nlattr **attrs)
1280 {
1281 	struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1282 
1283 	if (!linkid)
1284 		return -1;
1285 
1286 	return nla_get_u8(linkid);
1287 }
1288 
1289 struct key_parse {
1290 	struct key_params p;
1291 	int idx;
1292 	int type;
1293 	bool def, defmgmt, defbeacon;
1294 	bool def_uni, def_multi;
1295 };
1296 
1297 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1298 				 struct key_parse *k)
1299 {
1300 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1301 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1302 					      nl80211_key_policy,
1303 					      info->extack);
1304 	if (err)
1305 		return err;
1306 
1307 	k->def = !!tb[NL80211_KEY_DEFAULT];
1308 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1309 	k->defbeacon = !!tb[NL80211_KEY_DEFAULT_BEACON];
1310 
1311 	if (k->def) {
1312 		k->def_uni = true;
1313 		k->def_multi = true;
1314 	}
1315 	if (k->defmgmt || k->defbeacon)
1316 		k->def_multi = true;
1317 
1318 	if (tb[NL80211_KEY_IDX])
1319 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1320 
1321 	if (tb[NL80211_KEY_DATA]) {
1322 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1323 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1324 	}
1325 
1326 	if (tb[NL80211_KEY_SEQ]) {
1327 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1328 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1329 	}
1330 
1331 	if (tb[NL80211_KEY_CIPHER])
1332 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1333 
1334 	if (tb[NL80211_KEY_TYPE])
1335 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1336 
1337 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1338 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1339 
1340 		err = nla_parse_nested_deprecated(kdt,
1341 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1342 						  tb[NL80211_KEY_DEFAULT_TYPES],
1343 						  nl80211_key_default_policy,
1344 						  info->extack);
1345 		if (err)
1346 			return err;
1347 
1348 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1349 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1350 	}
1351 
1352 	if (tb[NL80211_KEY_MODE])
1353 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1354 
1355 	return 0;
1356 }
1357 
1358 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1359 {
1360 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1361 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1362 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1363 	}
1364 
1365 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1366 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1367 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1368 	}
1369 
1370 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1371 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1372 
1373 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1374 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1375 
1376 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1377 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1378 
1379 	if (k->def) {
1380 		k->def_uni = true;
1381 		k->def_multi = true;
1382 	}
1383 	if (k->defmgmt)
1384 		k->def_multi = true;
1385 
1386 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1387 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1388 
1389 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1390 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1391 		int err = nla_parse_nested_deprecated(kdt,
1392 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1393 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1394 						      nl80211_key_default_policy,
1395 						      info->extack);
1396 		if (err)
1397 			return err;
1398 
1399 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1400 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1401 	}
1402 
1403 	return 0;
1404 }
1405 
1406 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1407 {
1408 	int err;
1409 
1410 	memset(k, 0, sizeof(*k));
1411 	k->idx = -1;
1412 	k->type = -1;
1413 
1414 	if (info->attrs[NL80211_ATTR_KEY])
1415 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1416 	else
1417 		err = nl80211_parse_key_old(info, k);
1418 
1419 	if (err)
1420 		return err;
1421 
1422 	if ((k->def ? 1 : 0) + (k->defmgmt ? 1 : 0) +
1423 	    (k->defbeacon ? 1 : 0) > 1) {
1424 		GENL_SET_ERR_MSG(info,
1425 				 "key with multiple default flags is invalid");
1426 		return -EINVAL;
1427 	}
1428 
1429 	if (k->defmgmt || k->defbeacon) {
1430 		if (k->def_uni || !k->def_multi) {
1431 			GENL_SET_ERR_MSG(info,
1432 					 "defmgmt/defbeacon key must be mcast");
1433 			return -EINVAL;
1434 		}
1435 	}
1436 
1437 	if (k->idx != -1) {
1438 		if (k->defmgmt) {
1439 			if (k->idx < 4 || k->idx > 5) {
1440 				GENL_SET_ERR_MSG(info,
1441 						 "defmgmt key idx not 4 or 5");
1442 				return -EINVAL;
1443 			}
1444 		} else if (k->defbeacon) {
1445 			if (k->idx < 6 || k->idx > 7) {
1446 				GENL_SET_ERR_MSG(info,
1447 						 "defbeacon key idx not 6 or 7");
1448 				return -EINVAL;
1449 			}
1450 		} else if (k->def) {
1451 			if (k->idx < 0 || k->idx > 3) {
1452 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1453 				return -EINVAL;
1454 			}
1455 		} else {
1456 			if (k->idx < 0 || k->idx > 7) {
1457 				GENL_SET_ERR_MSG(info, "key idx not 0-7");
1458 				return -EINVAL;
1459 			}
1460 		}
1461 	}
1462 
1463 	return 0;
1464 }
1465 
1466 static struct cfg80211_cached_keys *
1467 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1468 		       struct genl_info *info, bool *no_ht)
1469 {
1470 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1471 	struct key_parse parse;
1472 	struct nlattr *key;
1473 	struct cfg80211_cached_keys *result;
1474 	int rem, err, def = 0;
1475 	bool have_key = false;
1476 
1477 	nla_for_each_nested(key, keys, rem) {
1478 		have_key = true;
1479 		break;
1480 	}
1481 
1482 	if (!have_key)
1483 		return NULL;
1484 
1485 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1486 	if (!result)
1487 		return ERR_PTR(-ENOMEM);
1488 
1489 	result->def = -1;
1490 
1491 	nla_for_each_nested(key, keys, rem) {
1492 		memset(&parse, 0, sizeof(parse));
1493 		parse.idx = -1;
1494 
1495 		err = nl80211_parse_key_new(info, key, &parse);
1496 		if (err)
1497 			goto error;
1498 		err = -EINVAL;
1499 		if (!parse.p.key)
1500 			goto error;
1501 		if (parse.idx < 0 || parse.idx > 3) {
1502 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1503 			goto error;
1504 		}
1505 		if (parse.def) {
1506 			if (def) {
1507 				GENL_SET_ERR_MSG(info,
1508 						 "only one key can be default");
1509 				goto error;
1510 			}
1511 			def = 1;
1512 			result->def = parse.idx;
1513 			if (!parse.def_uni || !parse.def_multi)
1514 				goto error;
1515 		} else if (parse.defmgmt)
1516 			goto error;
1517 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1518 						     parse.idx, false, NULL);
1519 		if (err)
1520 			goto error;
1521 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1522 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1523 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1524 			err = -EINVAL;
1525 			goto error;
1526 		}
1527 		result->params[parse.idx].cipher = parse.p.cipher;
1528 		result->params[parse.idx].key_len = parse.p.key_len;
1529 		result->params[parse.idx].key = result->data[parse.idx];
1530 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1531 
1532 		/* must be WEP key if we got here */
1533 		if (no_ht)
1534 			*no_ht = true;
1535 	}
1536 
1537 	if (result->def < 0) {
1538 		err = -EINVAL;
1539 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1540 		goto error;
1541 	}
1542 
1543 	return result;
1544  error:
1545 	kfree(result);
1546 	return ERR_PTR(err);
1547 }
1548 
1549 static int nl80211_key_allowed(struct wireless_dev *wdev)
1550 {
1551 	ASSERT_WDEV_LOCK(wdev);
1552 
1553 	switch (wdev->iftype) {
1554 	case NL80211_IFTYPE_AP:
1555 	case NL80211_IFTYPE_AP_VLAN:
1556 	case NL80211_IFTYPE_P2P_GO:
1557 	case NL80211_IFTYPE_MESH_POINT:
1558 		break;
1559 	case NL80211_IFTYPE_ADHOC:
1560 		if (wdev->u.ibss.current_bss)
1561 			return 0;
1562 		return -ENOLINK;
1563 	case NL80211_IFTYPE_STATION:
1564 	case NL80211_IFTYPE_P2P_CLIENT:
1565 		if (wdev->connected)
1566 			return 0;
1567 		return -ENOLINK;
1568 	case NL80211_IFTYPE_NAN:
1569 		if (wiphy_ext_feature_isset(wdev->wiphy,
1570 					    NL80211_EXT_FEATURE_SECURE_NAN))
1571 			return 0;
1572 		return -EINVAL;
1573 	case NL80211_IFTYPE_UNSPECIFIED:
1574 	case NL80211_IFTYPE_OCB:
1575 	case NL80211_IFTYPE_MONITOR:
1576 	case NL80211_IFTYPE_P2P_DEVICE:
1577 	case NL80211_IFTYPE_WDS:
1578 	case NUM_NL80211_IFTYPES:
1579 		return -EINVAL;
1580 	}
1581 
1582 	return 0;
1583 }
1584 
1585 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1586 							u32 freq)
1587 {
1588 	struct ieee80211_channel *chan;
1589 
1590 	chan = ieee80211_get_channel_khz(wiphy, freq);
1591 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1592 		return NULL;
1593 	return chan;
1594 }
1595 
1596 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1597 {
1598 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1599 	int i;
1600 
1601 	if (!nl_modes)
1602 		goto nla_put_failure;
1603 
1604 	i = 0;
1605 	while (ifmodes) {
1606 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1607 			goto nla_put_failure;
1608 		ifmodes >>= 1;
1609 		i++;
1610 	}
1611 
1612 	nla_nest_end(msg, nl_modes);
1613 	return 0;
1614 
1615 nla_put_failure:
1616 	return -ENOBUFS;
1617 }
1618 
1619 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1620 					  struct sk_buff *msg,
1621 					  bool large)
1622 {
1623 	struct nlattr *nl_combis;
1624 	int i, j;
1625 
1626 	nl_combis = nla_nest_start_noflag(msg,
1627 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1628 	if (!nl_combis)
1629 		goto nla_put_failure;
1630 
1631 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1632 		const struct ieee80211_iface_combination *c;
1633 		struct nlattr *nl_combi, *nl_limits;
1634 
1635 		c = &wiphy->iface_combinations[i];
1636 
1637 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1638 		if (!nl_combi)
1639 			goto nla_put_failure;
1640 
1641 		nl_limits = nla_nest_start_noflag(msg,
1642 						  NL80211_IFACE_COMB_LIMITS);
1643 		if (!nl_limits)
1644 			goto nla_put_failure;
1645 
1646 		for (j = 0; j < c->n_limits; j++) {
1647 			struct nlattr *nl_limit;
1648 
1649 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1650 			if (!nl_limit)
1651 				goto nla_put_failure;
1652 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1653 					c->limits[j].max))
1654 				goto nla_put_failure;
1655 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1656 						c->limits[j].types))
1657 				goto nla_put_failure;
1658 			nla_nest_end(msg, nl_limit);
1659 		}
1660 
1661 		nla_nest_end(msg, nl_limits);
1662 
1663 		if (c->beacon_int_infra_match &&
1664 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1665 			goto nla_put_failure;
1666 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1667 				c->num_different_channels) ||
1668 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1669 				c->max_interfaces))
1670 			goto nla_put_failure;
1671 		if (large &&
1672 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1673 				c->radar_detect_widths) ||
1674 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1675 				c->radar_detect_regions)))
1676 			goto nla_put_failure;
1677 		if (c->beacon_int_min_gcd &&
1678 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1679 				c->beacon_int_min_gcd))
1680 			goto nla_put_failure;
1681 
1682 		nla_nest_end(msg, nl_combi);
1683 	}
1684 
1685 	nla_nest_end(msg, nl_combis);
1686 
1687 	return 0;
1688 nla_put_failure:
1689 	return -ENOBUFS;
1690 }
1691 
1692 #ifdef CONFIG_PM
1693 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1694 					struct sk_buff *msg)
1695 {
1696 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1697 	struct nlattr *nl_tcp;
1698 
1699 	if (!tcp)
1700 		return 0;
1701 
1702 	nl_tcp = nla_nest_start_noflag(msg,
1703 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1704 	if (!nl_tcp)
1705 		return -ENOBUFS;
1706 
1707 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1708 			tcp->data_payload_max))
1709 		return -ENOBUFS;
1710 
1711 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1712 			tcp->data_payload_max))
1713 		return -ENOBUFS;
1714 
1715 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1716 		return -ENOBUFS;
1717 
1718 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1719 				sizeof(*tcp->tok), tcp->tok))
1720 		return -ENOBUFS;
1721 
1722 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1723 			tcp->data_interval_max))
1724 		return -ENOBUFS;
1725 
1726 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1727 			tcp->wake_payload_max))
1728 		return -ENOBUFS;
1729 
1730 	nla_nest_end(msg, nl_tcp);
1731 	return 0;
1732 }
1733 
1734 static int nl80211_send_wowlan(struct sk_buff *msg,
1735 			       struct cfg80211_registered_device *rdev,
1736 			       bool large)
1737 {
1738 	struct nlattr *nl_wowlan;
1739 
1740 	if (!rdev->wiphy.wowlan)
1741 		return 0;
1742 
1743 	nl_wowlan = nla_nest_start_noflag(msg,
1744 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1745 	if (!nl_wowlan)
1746 		return -ENOBUFS;
1747 
1748 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1749 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1750 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1751 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1752 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1753 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1754 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1755 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1756 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1757 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1758 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1759 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1760 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1761 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1762 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1763 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1764 		return -ENOBUFS;
1765 
1766 	if (rdev->wiphy.wowlan->n_patterns) {
1767 		struct nl80211_pattern_support pat = {
1768 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1769 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1770 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1771 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1772 		};
1773 
1774 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1775 			    sizeof(pat), &pat))
1776 			return -ENOBUFS;
1777 	}
1778 
1779 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1780 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1781 			rdev->wiphy.wowlan->max_nd_match_sets))
1782 		return -ENOBUFS;
1783 
1784 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1785 		return -ENOBUFS;
1786 
1787 	nla_nest_end(msg, nl_wowlan);
1788 
1789 	return 0;
1790 }
1791 #endif
1792 
1793 static int nl80211_send_coalesce(struct sk_buff *msg,
1794 				 struct cfg80211_registered_device *rdev)
1795 {
1796 	struct nl80211_coalesce_rule_support rule;
1797 
1798 	if (!rdev->wiphy.coalesce)
1799 		return 0;
1800 
1801 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1802 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1803 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1804 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1805 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1806 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1807 
1808 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1809 		return -ENOBUFS;
1810 
1811 	return 0;
1812 }
1813 
1814 static int
1815 nl80211_send_iftype_data(struct sk_buff *msg,
1816 			 const struct ieee80211_supported_band *sband,
1817 			 const struct ieee80211_sband_iftype_data *iftdata)
1818 {
1819 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1820 	const struct ieee80211_sta_eht_cap *eht_cap = &iftdata->eht_cap;
1821 
1822 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1823 				iftdata->types_mask))
1824 		return -ENOBUFS;
1825 
1826 	if (he_cap->has_he) {
1827 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1828 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1829 			    he_cap->he_cap_elem.mac_cap_info) ||
1830 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1831 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1832 			    he_cap->he_cap_elem.phy_cap_info) ||
1833 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1834 			    sizeof(he_cap->he_mcs_nss_supp),
1835 			    &he_cap->he_mcs_nss_supp) ||
1836 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1837 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1838 			return -ENOBUFS;
1839 	}
1840 
1841 	if (eht_cap->has_eht && he_cap->has_he) {
1842 		u8 mcs_nss_size, ppe_thresh_size;
1843 		u16 ppe_thres_hdr;
1844 		bool is_ap;
1845 
1846 		is_ap = iftdata->types_mask & BIT(NL80211_IFTYPE_AP) ||
1847 			iftdata->types_mask & BIT(NL80211_IFTYPE_P2P_GO);
1848 
1849 		mcs_nss_size =
1850 			ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
1851 						   &eht_cap->eht_cap_elem,
1852 						   is_ap);
1853 
1854 		ppe_thres_hdr = get_unaligned_le16(&eht_cap->eht_ppe_thres[0]);
1855 		ppe_thresh_size =
1856 			ieee80211_eht_ppe_size(ppe_thres_hdr,
1857 					       eht_cap->eht_cap_elem.phy_cap_info);
1858 
1859 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC,
1860 			    sizeof(eht_cap->eht_cap_elem.mac_cap_info),
1861 			    eht_cap->eht_cap_elem.mac_cap_info) ||
1862 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY,
1863 			    sizeof(eht_cap->eht_cap_elem.phy_cap_info),
1864 			    eht_cap->eht_cap_elem.phy_cap_info) ||
1865 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET,
1866 			    mcs_nss_size, &eht_cap->eht_mcs_nss_supp) ||
1867 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE,
1868 			    ppe_thresh_size, eht_cap->eht_ppe_thres))
1869 			return -ENOBUFS;
1870 	}
1871 
1872 	if (sband->band == NL80211_BAND_6GHZ &&
1873 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA,
1874 		    sizeof(iftdata->he_6ghz_capa),
1875 		    &iftdata->he_6ghz_capa))
1876 		return -ENOBUFS;
1877 
1878 	if (iftdata->vendor_elems.data && iftdata->vendor_elems.len &&
1879 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_VENDOR_ELEMS,
1880 		    iftdata->vendor_elems.len, iftdata->vendor_elems.data))
1881 		return -ENOBUFS;
1882 
1883 	return 0;
1884 }
1885 
1886 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1887 				      struct ieee80211_supported_band *sband,
1888 				      bool large)
1889 {
1890 	struct nlattr *nl_rates, *nl_rate;
1891 	struct ieee80211_rate *rate;
1892 	int i;
1893 
1894 	/* add HT info */
1895 	if (sband->ht_cap.ht_supported &&
1896 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1897 		     sizeof(sband->ht_cap.mcs),
1898 		     &sband->ht_cap.mcs) ||
1899 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1900 			 sband->ht_cap.cap) ||
1901 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1902 			sband->ht_cap.ampdu_factor) ||
1903 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1904 			sband->ht_cap.ampdu_density)))
1905 		return -ENOBUFS;
1906 
1907 	/* add VHT info */
1908 	if (sband->vht_cap.vht_supported &&
1909 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1910 		     sizeof(sband->vht_cap.vht_mcs),
1911 		     &sband->vht_cap.vht_mcs) ||
1912 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1913 			 sband->vht_cap.cap)))
1914 		return -ENOBUFS;
1915 
1916 	if (large && sband->n_iftype_data) {
1917 		struct nlattr *nl_iftype_data =
1918 			nla_nest_start_noflag(msg,
1919 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1920 		int err;
1921 
1922 		if (!nl_iftype_data)
1923 			return -ENOBUFS;
1924 
1925 		for (i = 0; i < sband->n_iftype_data; i++) {
1926 			struct nlattr *iftdata;
1927 
1928 			iftdata = nla_nest_start_noflag(msg, i + 1);
1929 			if (!iftdata)
1930 				return -ENOBUFS;
1931 
1932 			err = nl80211_send_iftype_data(msg, sband,
1933 						       &sband->iftype_data[i]);
1934 			if (err)
1935 				return err;
1936 
1937 			nla_nest_end(msg, iftdata);
1938 		}
1939 
1940 		nla_nest_end(msg, nl_iftype_data);
1941 	}
1942 
1943 	/* add EDMG info */
1944 	if (large && sband->edmg_cap.channels &&
1945 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1946 		       sband->edmg_cap.channels) ||
1947 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1948 		       sband->edmg_cap.bw_config)))
1949 
1950 		return -ENOBUFS;
1951 
1952 	/* add bitrates */
1953 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1954 	if (!nl_rates)
1955 		return -ENOBUFS;
1956 
1957 	for (i = 0; i < sband->n_bitrates; i++) {
1958 		nl_rate = nla_nest_start_noflag(msg, i);
1959 		if (!nl_rate)
1960 			return -ENOBUFS;
1961 
1962 		rate = &sband->bitrates[i];
1963 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1964 				rate->bitrate))
1965 			return -ENOBUFS;
1966 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1967 		    nla_put_flag(msg,
1968 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1969 			return -ENOBUFS;
1970 
1971 		nla_nest_end(msg, nl_rate);
1972 	}
1973 
1974 	nla_nest_end(msg, nl_rates);
1975 
1976 	/* S1G capabilities */
1977 	if (sband->band == NL80211_BAND_S1GHZ && sband->s1g_cap.s1g &&
1978 	    (nla_put(msg, NL80211_BAND_ATTR_S1G_CAPA,
1979 		     sizeof(sband->s1g_cap.cap),
1980 		     sband->s1g_cap.cap) ||
1981 	     nla_put(msg, NL80211_BAND_ATTR_S1G_MCS_NSS_SET,
1982 		     sizeof(sband->s1g_cap.nss_mcs),
1983 		     sband->s1g_cap.nss_mcs)))
1984 		return -ENOBUFS;
1985 
1986 	return 0;
1987 }
1988 
1989 static int
1990 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1991 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1992 {
1993 	u16 stypes;
1994 	struct nlattr *nl_ftypes, *nl_ifs;
1995 	enum nl80211_iftype ift;
1996 	int i;
1997 
1998 	if (!mgmt_stypes)
1999 		return 0;
2000 
2001 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
2002 	if (!nl_ifs)
2003 		return -ENOBUFS;
2004 
2005 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
2006 		nl_ftypes = nla_nest_start_noflag(msg, ift);
2007 		if (!nl_ftypes)
2008 			return -ENOBUFS;
2009 		i = 0;
2010 		stypes = mgmt_stypes[ift].tx;
2011 		while (stypes) {
2012 			if ((stypes & 1) &&
2013 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2014 					(i << 4) | IEEE80211_FTYPE_MGMT))
2015 				return -ENOBUFS;
2016 			stypes >>= 1;
2017 			i++;
2018 		}
2019 		nla_nest_end(msg, nl_ftypes);
2020 	}
2021 
2022 	nla_nest_end(msg, nl_ifs);
2023 
2024 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
2025 	if (!nl_ifs)
2026 		return -ENOBUFS;
2027 
2028 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
2029 		nl_ftypes = nla_nest_start_noflag(msg, ift);
2030 		if (!nl_ftypes)
2031 			return -ENOBUFS;
2032 		i = 0;
2033 		stypes = mgmt_stypes[ift].rx;
2034 		while (stypes) {
2035 			if ((stypes & 1) &&
2036 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2037 					(i << 4) | IEEE80211_FTYPE_MGMT))
2038 				return -ENOBUFS;
2039 			stypes >>= 1;
2040 			i++;
2041 		}
2042 		nla_nest_end(msg, nl_ftypes);
2043 	}
2044 	nla_nest_end(msg, nl_ifs);
2045 
2046 	return 0;
2047 }
2048 
2049 #define CMD(op, n)							\
2050 	 do {								\
2051 		if (rdev->ops->op) {					\
2052 			i++;						\
2053 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
2054 				goto nla_put_failure;			\
2055 		}							\
2056 	} while (0)
2057 
2058 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
2059 					struct sk_buff *msg)
2060 {
2061 	int i = 0;
2062 
2063 	/*
2064 	 * do *NOT* add anything into this function, new things need to be
2065 	 * advertised only to new versions of userspace that can deal with
2066 	 * the split (and they can't possibly care about new features...
2067 	 */
2068 	CMD(add_virtual_intf, NEW_INTERFACE);
2069 	CMD(change_virtual_intf, SET_INTERFACE);
2070 	CMD(add_key, NEW_KEY);
2071 	CMD(start_ap, START_AP);
2072 	CMD(add_station, NEW_STATION);
2073 	CMD(add_mpath, NEW_MPATH);
2074 	CMD(update_mesh_config, SET_MESH_CONFIG);
2075 	CMD(change_bss, SET_BSS);
2076 	CMD(auth, AUTHENTICATE);
2077 	CMD(assoc, ASSOCIATE);
2078 	CMD(deauth, DEAUTHENTICATE);
2079 	CMD(disassoc, DISASSOCIATE);
2080 	CMD(join_ibss, JOIN_IBSS);
2081 	CMD(join_mesh, JOIN_MESH);
2082 	CMD(set_pmksa, SET_PMKSA);
2083 	CMD(del_pmksa, DEL_PMKSA);
2084 	CMD(flush_pmksa, FLUSH_PMKSA);
2085 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
2086 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
2087 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
2088 	CMD(mgmt_tx, FRAME);
2089 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
2090 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
2091 		i++;
2092 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
2093 			goto nla_put_failure;
2094 	}
2095 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
2096 	    rdev->ops->join_mesh) {
2097 		i++;
2098 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
2099 			goto nla_put_failure;
2100 	}
2101 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2102 		CMD(tdls_mgmt, TDLS_MGMT);
2103 		CMD(tdls_oper, TDLS_OPER);
2104 	}
2105 	if (rdev->wiphy.max_sched_scan_reqs)
2106 		CMD(sched_scan_start, START_SCHED_SCAN);
2107 	CMD(probe_client, PROBE_CLIENT);
2108 	CMD(set_noack_map, SET_NOACK_MAP);
2109 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
2110 		i++;
2111 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
2112 			goto nla_put_failure;
2113 	}
2114 	CMD(start_p2p_device, START_P2P_DEVICE);
2115 	CMD(set_mcast_rate, SET_MCAST_RATE);
2116 #ifdef CONFIG_NL80211_TESTMODE
2117 	CMD(testmode_cmd, TESTMODE);
2118 #endif
2119 
2120 	if (rdev->ops->connect || rdev->ops->auth) {
2121 		i++;
2122 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
2123 			goto nla_put_failure;
2124 	}
2125 
2126 	if (rdev->ops->disconnect || rdev->ops->deauth) {
2127 		i++;
2128 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
2129 			goto nla_put_failure;
2130 	}
2131 
2132 	return i;
2133  nla_put_failure:
2134 	return -ENOBUFS;
2135 }
2136 
2137 static int
2138 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
2139 			   struct sk_buff *msg)
2140 {
2141 	struct nlattr *ftm;
2142 
2143 	if (!cap->ftm.supported)
2144 		return 0;
2145 
2146 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
2147 	if (!ftm)
2148 		return -ENOBUFS;
2149 
2150 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
2151 		return -ENOBUFS;
2152 	if (cap->ftm.non_asap &&
2153 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
2154 		return -ENOBUFS;
2155 	if (cap->ftm.request_lci &&
2156 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
2157 		return -ENOBUFS;
2158 	if (cap->ftm.request_civicloc &&
2159 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
2160 		return -ENOBUFS;
2161 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
2162 			cap->ftm.preambles))
2163 		return -ENOBUFS;
2164 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
2165 			cap->ftm.bandwidths))
2166 		return -ENOBUFS;
2167 	if (cap->ftm.max_bursts_exponent >= 0 &&
2168 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
2169 			cap->ftm.max_bursts_exponent))
2170 		return -ENOBUFS;
2171 	if (cap->ftm.max_ftms_per_burst &&
2172 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
2173 			cap->ftm.max_ftms_per_burst))
2174 		return -ENOBUFS;
2175 	if (cap->ftm.trigger_based &&
2176 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED))
2177 		return -ENOBUFS;
2178 	if (cap->ftm.non_trigger_based &&
2179 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED))
2180 		return -ENOBUFS;
2181 
2182 	nla_nest_end(msg, ftm);
2183 	return 0;
2184 }
2185 
2186 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
2187 				  struct sk_buff *msg)
2188 {
2189 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
2190 	struct nlattr *pmsr, *caps;
2191 
2192 	if (!cap)
2193 		return 0;
2194 
2195 	/*
2196 	 * we don't need to clean up anything here since the caller
2197 	 * will genlmsg_cancel() if we fail
2198 	 */
2199 
2200 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
2201 	if (!pmsr)
2202 		return -ENOBUFS;
2203 
2204 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
2205 		return -ENOBUFS;
2206 
2207 	if (cap->report_ap_tsf &&
2208 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
2209 		return -ENOBUFS;
2210 
2211 	if (cap->randomize_mac_addr &&
2212 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
2213 		return -ENOBUFS;
2214 
2215 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
2216 	if (!caps)
2217 		return -ENOBUFS;
2218 
2219 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
2220 		return -ENOBUFS;
2221 
2222 	nla_nest_end(msg, caps);
2223 	nla_nest_end(msg, pmsr);
2224 
2225 	return 0;
2226 }
2227 
2228 static int
2229 nl80211_put_iftype_akm_suites(struct cfg80211_registered_device *rdev,
2230 			      struct sk_buff *msg)
2231 {
2232 	int i;
2233 	struct nlattr *nested, *nested_akms;
2234 	const struct wiphy_iftype_akm_suites *iftype_akms;
2235 
2236 	if (!rdev->wiphy.num_iftype_akm_suites ||
2237 	    !rdev->wiphy.iftype_akm_suites)
2238 		return 0;
2239 
2240 	nested = nla_nest_start(msg, NL80211_ATTR_IFTYPE_AKM_SUITES);
2241 	if (!nested)
2242 		return -ENOBUFS;
2243 
2244 	for (i = 0; i < rdev->wiphy.num_iftype_akm_suites; i++) {
2245 		nested_akms = nla_nest_start(msg, i + 1);
2246 		if (!nested_akms)
2247 			return -ENOBUFS;
2248 
2249 		iftype_akms = &rdev->wiphy.iftype_akm_suites[i];
2250 
2251 		if (nl80211_put_iftypes(msg, NL80211_IFTYPE_AKM_ATTR_IFTYPES,
2252 					iftype_akms->iftypes_mask))
2253 			return -ENOBUFS;
2254 
2255 		if (nla_put(msg, NL80211_IFTYPE_AKM_ATTR_SUITES,
2256 			    sizeof(u32) * iftype_akms->n_akm_suites,
2257 			    iftype_akms->akm_suites)) {
2258 			return -ENOBUFS;
2259 		}
2260 		nla_nest_end(msg, nested_akms);
2261 	}
2262 
2263 	nla_nest_end(msg, nested);
2264 
2265 	return 0;
2266 }
2267 
2268 static int
2269 nl80211_put_tid_config_support(struct cfg80211_registered_device *rdev,
2270 			       struct sk_buff *msg)
2271 {
2272 	struct nlattr *supp;
2273 
2274 	if (!rdev->wiphy.tid_config_support.vif &&
2275 	    !rdev->wiphy.tid_config_support.peer)
2276 		return 0;
2277 
2278 	supp = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
2279 	if (!supp)
2280 		return -ENOSPC;
2281 
2282 	if (rdev->wiphy.tid_config_support.vif &&
2283 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_VIF_SUPP,
2284 			      rdev->wiphy.tid_config_support.vif,
2285 			      NL80211_TID_CONFIG_ATTR_PAD))
2286 		goto fail;
2287 
2288 	if (rdev->wiphy.tid_config_support.peer &&
2289 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_PEER_SUPP,
2290 			      rdev->wiphy.tid_config_support.peer,
2291 			      NL80211_TID_CONFIG_ATTR_PAD))
2292 		goto fail;
2293 
2294 	/* for now we just use the same value ... makes more sense */
2295 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT,
2296 		       rdev->wiphy.tid_config_support.max_retry))
2297 		goto fail;
2298 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG,
2299 		       rdev->wiphy.tid_config_support.max_retry))
2300 		goto fail;
2301 
2302 	nla_nest_end(msg, supp);
2303 
2304 	return 0;
2305 fail:
2306 	nla_nest_cancel(msg, supp);
2307 	return -ENOBUFS;
2308 }
2309 
2310 static int
2311 nl80211_put_sar_specs(struct cfg80211_registered_device *rdev,
2312 		      struct sk_buff *msg)
2313 {
2314 	struct nlattr *sar_capa, *specs, *sub_freq_range;
2315 	u8 num_freq_ranges;
2316 	int i;
2317 
2318 	if (!rdev->wiphy.sar_capa)
2319 		return 0;
2320 
2321 	num_freq_ranges = rdev->wiphy.sar_capa->num_freq_ranges;
2322 
2323 	sar_capa = nla_nest_start(msg, NL80211_ATTR_SAR_SPEC);
2324 	if (!sar_capa)
2325 		return -ENOSPC;
2326 
2327 	if (nla_put_u32(msg, NL80211_SAR_ATTR_TYPE, rdev->wiphy.sar_capa->type))
2328 		goto fail;
2329 
2330 	specs = nla_nest_start(msg, NL80211_SAR_ATTR_SPECS);
2331 	if (!specs)
2332 		goto fail;
2333 
2334 	/* report supported freq_ranges */
2335 	for (i = 0; i < num_freq_ranges; i++) {
2336 		sub_freq_range = nla_nest_start(msg, i + 1);
2337 		if (!sub_freq_range)
2338 			goto fail;
2339 
2340 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_START_FREQ,
2341 				rdev->wiphy.sar_capa->freq_ranges[i].start_freq))
2342 			goto fail;
2343 
2344 		if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_END_FREQ,
2345 				rdev->wiphy.sar_capa->freq_ranges[i].end_freq))
2346 			goto fail;
2347 
2348 		nla_nest_end(msg, sub_freq_range);
2349 	}
2350 
2351 	nla_nest_end(msg, specs);
2352 	nla_nest_end(msg, sar_capa);
2353 
2354 	return 0;
2355 fail:
2356 	nla_nest_cancel(msg, sar_capa);
2357 	return -ENOBUFS;
2358 }
2359 
2360 static int nl80211_put_mbssid_support(struct wiphy *wiphy, struct sk_buff *msg)
2361 {
2362 	struct nlattr *config;
2363 
2364 	if (!wiphy->mbssid_max_interfaces)
2365 		return 0;
2366 
2367 	config = nla_nest_start(msg, NL80211_ATTR_MBSSID_CONFIG);
2368 	if (!config)
2369 		return -ENOBUFS;
2370 
2371 	if (nla_put_u8(msg, NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES,
2372 		       wiphy->mbssid_max_interfaces))
2373 		goto fail;
2374 
2375 	if (wiphy->ema_max_profile_periodicity &&
2376 	    nla_put_u8(msg,
2377 		       NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY,
2378 		       wiphy->ema_max_profile_periodicity))
2379 		goto fail;
2380 
2381 	nla_nest_end(msg, config);
2382 	return 0;
2383 
2384 fail:
2385 	nla_nest_cancel(msg, config);
2386 	return -ENOBUFS;
2387 }
2388 
2389 struct nl80211_dump_wiphy_state {
2390 	s64 filter_wiphy;
2391 	long start;
2392 	long split_start, band_start, chan_start, capa_start;
2393 	bool split;
2394 };
2395 
2396 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
2397 			      enum nl80211_commands cmd,
2398 			      struct sk_buff *msg, u32 portid, u32 seq,
2399 			      int flags, struct nl80211_dump_wiphy_state *state)
2400 {
2401 	void *hdr;
2402 	struct nlattr *nl_bands, *nl_band;
2403 	struct nlattr *nl_freqs, *nl_freq;
2404 	struct nlattr *nl_cmds;
2405 	enum nl80211_band band;
2406 	struct ieee80211_channel *chan;
2407 	int i;
2408 	const struct ieee80211_txrx_stypes *mgmt_stypes =
2409 				rdev->wiphy.mgmt_stypes;
2410 	u32 features;
2411 
2412 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2413 	if (!hdr)
2414 		return -ENOBUFS;
2415 
2416 	if (WARN_ON(!state))
2417 		return -EINVAL;
2418 
2419 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2420 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
2421 			   wiphy_name(&rdev->wiphy)) ||
2422 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2423 			cfg80211_rdev_list_generation))
2424 		goto nla_put_failure;
2425 
2426 	if (cmd != NL80211_CMD_NEW_WIPHY)
2427 		goto finish;
2428 
2429 	switch (state->split_start) {
2430 	case 0:
2431 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
2432 			       rdev->wiphy.retry_short) ||
2433 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
2434 			       rdev->wiphy.retry_long) ||
2435 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
2436 				rdev->wiphy.frag_threshold) ||
2437 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
2438 				rdev->wiphy.rts_threshold) ||
2439 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
2440 			       rdev->wiphy.coverage_class) ||
2441 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
2442 			       rdev->wiphy.max_scan_ssids) ||
2443 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
2444 			       rdev->wiphy.max_sched_scan_ssids) ||
2445 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
2446 				rdev->wiphy.max_scan_ie_len) ||
2447 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
2448 				rdev->wiphy.max_sched_scan_ie_len) ||
2449 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
2450 			       rdev->wiphy.max_match_sets))
2451 			goto nla_put_failure;
2452 
2453 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
2454 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
2455 			goto nla_put_failure;
2456 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
2457 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
2458 			goto nla_put_failure;
2459 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2460 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
2461 			goto nla_put_failure;
2462 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
2463 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
2464 			goto nla_put_failure;
2465 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2466 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
2467 			goto nla_put_failure;
2468 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
2469 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
2470 			goto nla_put_failure;
2471 		state->split_start++;
2472 		if (state->split)
2473 			break;
2474 		fallthrough;
2475 	case 1:
2476 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
2477 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
2478 			    rdev->wiphy.cipher_suites))
2479 			goto nla_put_failure;
2480 
2481 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
2482 			       rdev->wiphy.max_num_pmkids))
2483 			goto nla_put_failure;
2484 
2485 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
2486 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
2487 			goto nla_put_failure;
2488 
2489 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
2490 				rdev->wiphy.available_antennas_tx) ||
2491 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
2492 				rdev->wiphy.available_antennas_rx))
2493 			goto nla_put_failure;
2494 
2495 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2496 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2497 				rdev->wiphy.probe_resp_offload))
2498 			goto nla_put_failure;
2499 
2500 		if ((rdev->wiphy.available_antennas_tx ||
2501 		     rdev->wiphy.available_antennas_rx) &&
2502 		    rdev->ops->get_antenna) {
2503 			u32 tx_ant = 0, rx_ant = 0;
2504 			int res;
2505 
2506 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2507 			if (!res) {
2508 				if (nla_put_u32(msg,
2509 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2510 						tx_ant) ||
2511 				    nla_put_u32(msg,
2512 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2513 						rx_ant))
2514 					goto nla_put_failure;
2515 			}
2516 		}
2517 
2518 		state->split_start++;
2519 		if (state->split)
2520 			break;
2521 		fallthrough;
2522 	case 2:
2523 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2524 					rdev->wiphy.interface_modes))
2525 				goto nla_put_failure;
2526 		state->split_start++;
2527 		if (state->split)
2528 			break;
2529 		fallthrough;
2530 	case 3:
2531 		nl_bands = nla_nest_start_noflag(msg,
2532 						 NL80211_ATTR_WIPHY_BANDS);
2533 		if (!nl_bands)
2534 			goto nla_put_failure;
2535 
2536 		for (band = state->band_start;
2537 		     band < (state->split ?
2538 				NUM_NL80211_BANDS :
2539 				NL80211_BAND_60GHZ + 1);
2540 		     band++) {
2541 			struct ieee80211_supported_band *sband;
2542 
2543 			/* omit higher bands for ancient software */
2544 			if (band > NL80211_BAND_5GHZ && !state->split)
2545 				break;
2546 
2547 			sband = rdev->wiphy.bands[band];
2548 
2549 			if (!sband)
2550 				continue;
2551 
2552 			nl_band = nla_nest_start_noflag(msg, band);
2553 			if (!nl_band)
2554 				goto nla_put_failure;
2555 
2556 			switch (state->chan_start) {
2557 			case 0:
2558 				if (nl80211_send_band_rateinfo(msg, sband,
2559 							       state->split))
2560 					goto nla_put_failure;
2561 				state->chan_start++;
2562 				if (state->split)
2563 					break;
2564 				fallthrough;
2565 			default:
2566 				/* add frequencies */
2567 				nl_freqs = nla_nest_start_noflag(msg,
2568 								 NL80211_BAND_ATTR_FREQS);
2569 				if (!nl_freqs)
2570 					goto nla_put_failure;
2571 
2572 				for (i = state->chan_start - 1;
2573 				     i < sband->n_channels;
2574 				     i++) {
2575 					nl_freq = nla_nest_start_noflag(msg,
2576 									i);
2577 					if (!nl_freq)
2578 						goto nla_put_failure;
2579 
2580 					chan = &sband->channels[i];
2581 
2582 					if (nl80211_msg_put_channel(
2583 							msg, &rdev->wiphy, chan,
2584 							state->split))
2585 						goto nla_put_failure;
2586 
2587 					nla_nest_end(msg, nl_freq);
2588 					if (state->split)
2589 						break;
2590 				}
2591 				if (i < sband->n_channels)
2592 					state->chan_start = i + 2;
2593 				else
2594 					state->chan_start = 0;
2595 				nla_nest_end(msg, nl_freqs);
2596 			}
2597 
2598 			nla_nest_end(msg, nl_band);
2599 
2600 			if (state->split) {
2601 				/* start again here */
2602 				if (state->chan_start)
2603 					band--;
2604 				break;
2605 			}
2606 		}
2607 		nla_nest_end(msg, nl_bands);
2608 
2609 		if (band < NUM_NL80211_BANDS)
2610 			state->band_start = band + 1;
2611 		else
2612 			state->band_start = 0;
2613 
2614 		/* if bands & channels are done, continue outside */
2615 		if (state->band_start == 0 && state->chan_start == 0)
2616 			state->split_start++;
2617 		if (state->split)
2618 			break;
2619 		fallthrough;
2620 	case 4:
2621 		nl_cmds = nla_nest_start_noflag(msg,
2622 						NL80211_ATTR_SUPPORTED_COMMANDS);
2623 		if (!nl_cmds)
2624 			goto nla_put_failure;
2625 
2626 		i = nl80211_add_commands_unsplit(rdev, msg);
2627 		if (i < 0)
2628 			goto nla_put_failure;
2629 		if (state->split) {
2630 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2631 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2632 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2633 				CMD(channel_switch, CHANNEL_SWITCH);
2634 			CMD(set_qos_map, SET_QOS_MAP);
2635 			if (rdev->wiphy.features &
2636 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2637 				CMD(add_tx_ts, ADD_TX_TS);
2638 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2639 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2640 			CMD(update_ft_ies, UPDATE_FT_IES);
2641 			if (rdev->wiphy.sar_capa)
2642 				CMD(set_sar_specs, SET_SAR_SPECS);
2643 		}
2644 #undef CMD
2645 
2646 		nla_nest_end(msg, nl_cmds);
2647 		state->split_start++;
2648 		if (state->split)
2649 			break;
2650 		fallthrough;
2651 	case 5:
2652 		if (rdev->ops->remain_on_channel &&
2653 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2654 		    nla_put_u32(msg,
2655 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2656 				rdev->wiphy.max_remain_on_channel_duration))
2657 			goto nla_put_failure;
2658 
2659 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2660 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2661 			goto nla_put_failure;
2662 
2663 		state->split_start++;
2664 		if (state->split)
2665 			break;
2666 		fallthrough;
2667 	case 6:
2668 #ifdef CONFIG_PM
2669 		if (nl80211_send_wowlan(msg, rdev, state->split))
2670 			goto nla_put_failure;
2671 		state->split_start++;
2672 		if (state->split)
2673 			break;
2674 #else
2675 		state->split_start++;
2676 #endif
2677 		fallthrough;
2678 	case 7:
2679 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2680 					rdev->wiphy.software_iftypes))
2681 			goto nla_put_failure;
2682 
2683 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2684 						   state->split))
2685 			goto nla_put_failure;
2686 
2687 		state->split_start++;
2688 		if (state->split)
2689 			break;
2690 		fallthrough;
2691 	case 8:
2692 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2693 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2694 				rdev->wiphy.ap_sme_capa))
2695 			goto nla_put_failure;
2696 
2697 		features = rdev->wiphy.features;
2698 		/*
2699 		 * We can only add the per-channel limit information if the
2700 		 * dump is split, otherwise it makes it too big. Therefore
2701 		 * only advertise it in that case.
2702 		 */
2703 		if (state->split)
2704 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2705 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2706 			goto nla_put_failure;
2707 
2708 		if (rdev->wiphy.ht_capa_mod_mask &&
2709 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2710 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2711 			    rdev->wiphy.ht_capa_mod_mask))
2712 			goto nla_put_failure;
2713 
2714 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2715 		    rdev->wiphy.max_acl_mac_addrs &&
2716 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2717 				rdev->wiphy.max_acl_mac_addrs))
2718 			goto nla_put_failure;
2719 
2720 		/*
2721 		 * Any information below this point is only available to
2722 		 * applications that can deal with it being split. This
2723 		 * helps ensure that newly added capabilities don't break
2724 		 * older tools by overrunning their buffers.
2725 		 *
2726 		 * We still increment split_start so that in the split
2727 		 * case we'll continue with more data in the next round,
2728 		 * but break unconditionally so unsplit data stops here.
2729 		 */
2730 		if (state->split)
2731 			state->split_start++;
2732 		else
2733 			state->split_start = 0;
2734 		break;
2735 	case 9:
2736 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2737 			goto nla_put_failure;
2738 
2739 		if (nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
2740 				rdev->wiphy.max_sched_scan_plans) ||
2741 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
2742 				rdev->wiphy.max_sched_scan_plan_interval) ||
2743 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
2744 				rdev->wiphy.max_sched_scan_plan_iterations))
2745 			goto nla_put_failure;
2746 
2747 		if (rdev->wiphy.extended_capabilities &&
2748 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2749 			     rdev->wiphy.extended_capabilities_len,
2750 			     rdev->wiphy.extended_capabilities) ||
2751 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2752 			     rdev->wiphy.extended_capabilities_len,
2753 			     rdev->wiphy.extended_capabilities_mask)))
2754 			goto nla_put_failure;
2755 
2756 		if (rdev->wiphy.vht_capa_mod_mask &&
2757 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2758 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2759 			    rdev->wiphy.vht_capa_mod_mask))
2760 			goto nla_put_failure;
2761 
2762 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2763 			    rdev->wiphy.perm_addr))
2764 			goto nla_put_failure;
2765 
2766 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2767 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2768 			    rdev->wiphy.addr_mask))
2769 			goto nla_put_failure;
2770 
2771 		if (rdev->wiphy.n_addresses > 1) {
2772 			void *attr;
2773 
2774 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2775 			if (!attr)
2776 				goto nla_put_failure;
2777 
2778 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2779 				if (nla_put(msg, i + 1, ETH_ALEN,
2780 					    rdev->wiphy.addresses[i].addr))
2781 					goto nla_put_failure;
2782 
2783 			nla_nest_end(msg, attr);
2784 		}
2785 
2786 		state->split_start++;
2787 		break;
2788 	case 10:
2789 		if (nl80211_send_coalesce(msg, rdev))
2790 			goto nla_put_failure;
2791 
2792 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2793 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2794 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2795 			goto nla_put_failure;
2796 
2797 		if (rdev->wiphy.max_ap_assoc_sta &&
2798 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2799 				rdev->wiphy.max_ap_assoc_sta))
2800 			goto nla_put_failure;
2801 
2802 		state->split_start++;
2803 		break;
2804 	case 11:
2805 		if (rdev->wiphy.n_vendor_commands) {
2806 			const struct nl80211_vendor_cmd_info *info;
2807 			struct nlattr *nested;
2808 
2809 			nested = nla_nest_start_noflag(msg,
2810 						       NL80211_ATTR_VENDOR_DATA);
2811 			if (!nested)
2812 				goto nla_put_failure;
2813 
2814 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2815 				info = &rdev->wiphy.vendor_commands[i].info;
2816 				if (nla_put(msg, i + 1, sizeof(*info), info))
2817 					goto nla_put_failure;
2818 			}
2819 			nla_nest_end(msg, nested);
2820 		}
2821 
2822 		if (rdev->wiphy.n_vendor_events) {
2823 			const struct nl80211_vendor_cmd_info *info;
2824 			struct nlattr *nested;
2825 
2826 			nested = nla_nest_start_noflag(msg,
2827 						       NL80211_ATTR_VENDOR_EVENTS);
2828 			if (!nested)
2829 				goto nla_put_failure;
2830 
2831 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2832 				info = &rdev->wiphy.vendor_events[i];
2833 				if (nla_put(msg, i + 1, sizeof(*info), info))
2834 					goto nla_put_failure;
2835 			}
2836 			nla_nest_end(msg, nested);
2837 		}
2838 		state->split_start++;
2839 		break;
2840 	case 12:
2841 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2842 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2843 			       rdev->wiphy.max_num_csa_counters))
2844 			goto nla_put_failure;
2845 
2846 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2847 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2848 			goto nla_put_failure;
2849 
2850 		if (rdev->wiphy.max_sched_scan_reqs &&
2851 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2852 				rdev->wiphy.max_sched_scan_reqs))
2853 			goto nla_put_failure;
2854 
2855 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2856 			    sizeof(rdev->wiphy.ext_features),
2857 			    rdev->wiphy.ext_features))
2858 			goto nla_put_failure;
2859 
2860 		if (rdev->wiphy.bss_select_support) {
2861 			struct nlattr *nested;
2862 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2863 
2864 			nested = nla_nest_start_noflag(msg,
2865 						       NL80211_ATTR_BSS_SELECT);
2866 			if (!nested)
2867 				goto nla_put_failure;
2868 
2869 			i = 0;
2870 			while (bss_select_support) {
2871 				if ((bss_select_support & 1) &&
2872 				    nla_put_flag(msg, i))
2873 					goto nla_put_failure;
2874 				i++;
2875 				bss_select_support >>= 1;
2876 			}
2877 			nla_nest_end(msg, nested);
2878 		}
2879 
2880 		state->split_start++;
2881 		break;
2882 	case 13:
2883 		if (rdev->wiphy.num_iftype_ext_capab &&
2884 		    rdev->wiphy.iftype_ext_capab) {
2885 			struct nlattr *nested_ext_capab, *nested;
2886 
2887 			nested = nla_nest_start_noflag(msg,
2888 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2889 			if (!nested)
2890 				goto nla_put_failure;
2891 
2892 			for (i = state->capa_start;
2893 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2894 				const struct wiphy_iftype_ext_capab *capab;
2895 
2896 				capab = &rdev->wiphy.iftype_ext_capab[i];
2897 
2898 				nested_ext_capab = nla_nest_start_noflag(msg,
2899 									 i);
2900 				if (!nested_ext_capab ||
2901 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2902 						capab->iftype) ||
2903 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2904 					    capab->extended_capabilities_len,
2905 					    capab->extended_capabilities) ||
2906 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2907 					    capab->extended_capabilities_len,
2908 					    capab->extended_capabilities_mask))
2909 					goto nla_put_failure;
2910 
2911 				if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO &&
2912 				    (nla_put_u16(msg,
2913 						 NL80211_ATTR_EML_CAPABILITY,
2914 						 capab->eml_capabilities) ||
2915 				     nla_put_u16(msg,
2916 						 NL80211_ATTR_MLD_CAPA_AND_OPS,
2917 						 capab->mld_capa_and_ops)))
2918 					goto nla_put_failure;
2919 
2920 				nla_nest_end(msg, nested_ext_capab);
2921 				if (state->split)
2922 					break;
2923 			}
2924 			nla_nest_end(msg, nested);
2925 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2926 				state->capa_start = i + 1;
2927 				break;
2928 			}
2929 		}
2930 
2931 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2932 				rdev->wiphy.nan_supported_bands))
2933 			goto nla_put_failure;
2934 
2935 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2936 					    NL80211_EXT_FEATURE_TXQS)) {
2937 			struct cfg80211_txq_stats txqstats = {};
2938 			int res;
2939 
2940 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2941 			if (!res &&
2942 			    !nl80211_put_txq_stats(msg, &txqstats,
2943 						   NL80211_ATTR_TXQ_STATS))
2944 				goto nla_put_failure;
2945 
2946 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2947 					rdev->wiphy.txq_limit))
2948 				goto nla_put_failure;
2949 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2950 					rdev->wiphy.txq_memory_limit))
2951 				goto nla_put_failure;
2952 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2953 					rdev->wiphy.txq_quantum))
2954 				goto nla_put_failure;
2955 		}
2956 
2957 		state->split_start++;
2958 		break;
2959 	case 14:
2960 		if (nl80211_send_pmsr_capa(rdev, msg))
2961 			goto nla_put_failure;
2962 
2963 		state->split_start++;
2964 		break;
2965 	case 15:
2966 		if (rdev->wiphy.akm_suites &&
2967 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2968 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2969 			    rdev->wiphy.akm_suites))
2970 			goto nla_put_failure;
2971 
2972 		if (nl80211_put_iftype_akm_suites(rdev, msg))
2973 			goto nla_put_failure;
2974 
2975 		if (nl80211_put_tid_config_support(rdev, msg))
2976 			goto nla_put_failure;
2977 		state->split_start++;
2978 		break;
2979 	case 16:
2980 		if (nl80211_put_sar_specs(rdev, msg))
2981 			goto nla_put_failure;
2982 
2983 		if (nl80211_put_mbssid_support(&rdev->wiphy, msg))
2984 			goto nla_put_failure;
2985 
2986 		if (nla_put_u16(msg, NL80211_ATTR_MAX_NUM_AKM_SUITES,
2987 				rdev->wiphy.max_num_akm_suites))
2988 			goto nla_put_failure;
2989 
2990 		if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO)
2991 			nla_put_flag(msg, NL80211_ATTR_MLO_SUPPORT);
2992 
2993 		if (rdev->wiphy.hw_timestamp_max_peers &&
2994 		    nla_put_u16(msg, NL80211_ATTR_MAX_HW_TIMESTAMP_PEERS,
2995 				rdev->wiphy.hw_timestamp_max_peers))
2996 			goto nla_put_failure;
2997 
2998 		/* done */
2999 		state->split_start = 0;
3000 		break;
3001 	}
3002  finish:
3003 	genlmsg_end(msg, hdr);
3004 	return 0;
3005 
3006  nla_put_failure:
3007 	genlmsg_cancel(msg, hdr);
3008 	return -EMSGSIZE;
3009 }
3010 
3011 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
3012 				    struct netlink_callback *cb,
3013 				    struct nl80211_dump_wiphy_state *state)
3014 {
3015 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
3016 	int ret;
3017 
3018 	if (!tb)
3019 		return -ENOMEM;
3020 
3021 	ret = nlmsg_parse_deprecated(cb->nlh,
3022 				     GENL_HDRLEN + nl80211_fam.hdrsize,
3023 				     tb, nl80211_fam.maxattr,
3024 				     nl80211_policy, NULL);
3025 	/* ignore parse errors for backward compatibility */
3026 	if (ret) {
3027 		ret = 0;
3028 		goto out;
3029 	}
3030 
3031 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
3032 	if (tb[NL80211_ATTR_WIPHY])
3033 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
3034 	if (tb[NL80211_ATTR_WDEV])
3035 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
3036 	if (tb[NL80211_ATTR_IFINDEX]) {
3037 		struct net_device *netdev;
3038 		struct cfg80211_registered_device *rdev;
3039 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3040 
3041 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3042 		if (!netdev) {
3043 			ret = -ENODEV;
3044 			goto out;
3045 		}
3046 		if (netdev->ieee80211_ptr) {
3047 			rdev = wiphy_to_rdev(
3048 				netdev->ieee80211_ptr->wiphy);
3049 			state->filter_wiphy = rdev->wiphy_idx;
3050 		}
3051 	}
3052 
3053 	ret = 0;
3054 out:
3055 	kfree(tb);
3056 	return ret;
3057 }
3058 
3059 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
3060 {
3061 	int idx = 0, ret;
3062 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
3063 	struct cfg80211_registered_device *rdev;
3064 
3065 	rtnl_lock();
3066 	if (!state) {
3067 		state = kzalloc(sizeof(*state), GFP_KERNEL);
3068 		if (!state) {
3069 			rtnl_unlock();
3070 			return -ENOMEM;
3071 		}
3072 		state->filter_wiphy = -1;
3073 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
3074 		if (ret) {
3075 			kfree(state);
3076 			rtnl_unlock();
3077 			return ret;
3078 		}
3079 		cb->args[0] = (long)state;
3080 	}
3081 
3082 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3083 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3084 			continue;
3085 		if (++idx <= state->start)
3086 			continue;
3087 		if (state->filter_wiphy != -1 &&
3088 		    state->filter_wiphy != rdev->wiphy_idx)
3089 			continue;
3090 		wiphy_lock(&rdev->wiphy);
3091 		/* attempt to fit multiple wiphy data chunks into the skb */
3092 		do {
3093 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
3094 						 skb,
3095 						 NETLINK_CB(cb->skb).portid,
3096 						 cb->nlh->nlmsg_seq,
3097 						 NLM_F_MULTI, state);
3098 			if (ret < 0) {
3099 				/*
3100 				 * If sending the wiphy data didn't fit (ENOBUFS
3101 				 * or EMSGSIZE returned), this SKB is still
3102 				 * empty (so it's not too big because another
3103 				 * wiphy dataset is already in the skb) and
3104 				 * we've not tried to adjust the dump allocation
3105 				 * yet ... then adjust the alloc size to be
3106 				 * bigger, and return 1 but with the empty skb.
3107 				 * This results in an empty message being RX'ed
3108 				 * in userspace, but that is ignored.
3109 				 *
3110 				 * We can then retry with the larger buffer.
3111 				 */
3112 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
3113 				    !skb->len && !state->split &&
3114 				    cb->min_dump_alloc < 4096) {
3115 					cb->min_dump_alloc = 4096;
3116 					state->split_start = 0;
3117 					wiphy_unlock(&rdev->wiphy);
3118 					rtnl_unlock();
3119 					return 1;
3120 				}
3121 				idx--;
3122 				break;
3123 			}
3124 		} while (state->split_start > 0);
3125 		wiphy_unlock(&rdev->wiphy);
3126 		break;
3127 	}
3128 	rtnl_unlock();
3129 
3130 	state->start = idx;
3131 
3132 	return skb->len;
3133 }
3134 
3135 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
3136 {
3137 	kfree((void *)cb->args[0]);
3138 	return 0;
3139 }
3140 
3141 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
3142 {
3143 	struct sk_buff *msg;
3144 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3145 	struct nl80211_dump_wiphy_state state = {};
3146 
3147 	msg = nlmsg_new(4096, GFP_KERNEL);
3148 	if (!msg)
3149 		return -ENOMEM;
3150 
3151 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
3152 			       info->snd_portid, info->snd_seq, 0,
3153 			       &state) < 0) {
3154 		nlmsg_free(msg);
3155 		return -ENOBUFS;
3156 	}
3157 
3158 	return genlmsg_reply(msg, info);
3159 }
3160 
3161 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
3162 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
3163 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
3164 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
3165 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
3166 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
3167 };
3168 
3169 static int parse_txq_params(struct nlattr *tb[],
3170 			    struct ieee80211_txq_params *txq_params)
3171 {
3172 	u8 ac;
3173 
3174 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
3175 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
3176 	    !tb[NL80211_TXQ_ATTR_AIFS])
3177 		return -EINVAL;
3178 
3179 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
3180 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
3181 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
3182 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
3183 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
3184 
3185 	if (ac >= NL80211_NUM_ACS)
3186 		return -EINVAL;
3187 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
3188 	return 0;
3189 }
3190 
3191 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
3192 {
3193 	/*
3194 	 * You can only set the channel explicitly for some interfaces,
3195 	 * most have their channel managed via their respective
3196 	 * "establish a connection" command (connect, join, ...)
3197 	 *
3198 	 * For AP/GO and mesh mode, the channel can be set with the
3199 	 * channel userspace API, but is only stored and passed to the
3200 	 * low-level driver when the AP starts or the mesh is joined.
3201 	 * This is for backward compatibility, userspace can also give
3202 	 * the channel in the start-ap or join-mesh commands instead.
3203 	 *
3204 	 * Monitors are special as they are normally slaved to
3205 	 * whatever else is going on, so they have their own special
3206 	 * operation to set the monitor channel if possible.
3207 	 */
3208 	return !wdev ||
3209 		wdev->iftype == NL80211_IFTYPE_AP ||
3210 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
3211 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
3212 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
3213 }
3214 
3215 static int nl80211_parse_punct_bitmap(struct cfg80211_registered_device *rdev,
3216 				      struct genl_info *info,
3217 				      const struct cfg80211_chan_def *chandef,
3218 				      u16 *punct_bitmap)
3219 {
3220 	if (!wiphy_ext_feature_isset(&rdev->wiphy, NL80211_EXT_FEATURE_PUNCT))
3221 		return -EINVAL;
3222 
3223 	*punct_bitmap = nla_get_u32(info->attrs[NL80211_ATTR_PUNCT_BITMAP]);
3224 	if (!cfg80211_valid_disable_subchannel_bitmap(punct_bitmap, chandef))
3225 		return -EINVAL;
3226 
3227 	return 0;
3228 }
3229 
3230 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
3231 			  struct genl_info *info,
3232 			  struct cfg80211_chan_def *chandef)
3233 {
3234 	struct netlink_ext_ack *extack = info->extack;
3235 	struct nlattr **attrs = info->attrs;
3236 	u32 control_freq;
3237 
3238 	if (!attrs[NL80211_ATTR_WIPHY_FREQ]) {
3239 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3240 				    "Frequency is missing");
3241 		return -EINVAL;
3242 	}
3243 
3244 	control_freq = MHZ_TO_KHZ(
3245 			nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3246 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
3247 		control_freq +=
3248 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
3249 
3250 	memset(chandef, 0, sizeof(*chandef));
3251 	chandef->chan = ieee80211_get_channel_khz(&rdev->wiphy, control_freq);
3252 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3253 	chandef->center_freq1 = KHZ_TO_MHZ(control_freq);
3254 	chandef->freq1_offset = control_freq % 1000;
3255 	chandef->center_freq2 = 0;
3256 
3257 	/* Primary channel not allowed */
3258 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
3259 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3260 				    "Channel is disabled");
3261 		return -EINVAL;
3262 	}
3263 
3264 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3265 		enum nl80211_channel_type chantype;
3266 
3267 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
3268 
3269 		switch (chantype) {
3270 		case NL80211_CHAN_NO_HT:
3271 		case NL80211_CHAN_HT20:
3272 		case NL80211_CHAN_HT40PLUS:
3273 		case NL80211_CHAN_HT40MINUS:
3274 			cfg80211_chandef_create(chandef, chandef->chan,
3275 						chantype);
3276 			/* user input for center_freq is incorrect */
3277 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
3278 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
3279 				NL_SET_ERR_MSG_ATTR(extack,
3280 						    attrs[NL80211_ATTR_CENTER_FREQ1],
3281 						    "bad center frequency 1");
3282 				return -EINVAL;
3283 			}
3284 			/* center_freq2 must be zero */
3285 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
3286 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
3287 				NL_SET_ERR_MSG_ATTR(extack,
3288 						    attrs[NL80211_ATTR_CENTER_FREQ2],
3289 						    "center frequency 2 can't be used");
3290 				return -EINVAL;
3291 			}
3292 			break;
3293 		default:
3294 			NL_SET_ERR_MSG_ATTR(extack,
3295 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3296 					    "invalid channel type");
3297 			return -EINVAL;
3298 		}
3299 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
3300 		chandef->width =
3301 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
3302 		if (chandef->chan->band == NL80211_BAND_S1GHZ) {
3303 			/* User input error for channel width doesn't match channel  */
3304 			if (chandef->width != ieee80211_s1g_channel_width(chandef->chan)) {
3305 				NL_SET_ERR_MSG_ATTR(extack,
3306 						    attrs[NL80211_ATTR_CHANNEL_WIDTH],
3307 						    "bad channel width");
3308 				return -EINVAL;
3309 			}
3310 		}
3311 		if (attrs[NL80211_ATTR_CENTER_FREQ1]) {
3312 			chandef->center_freq1 =
3313 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
3314 			if (attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET])
3315 				chandef->freq1_offset = nla_get_u32(
3316 				      attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET]);
3317 			else
3318 				chandef->freq1_offset = 0;
3319 		}
3320 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
3321 			chandef->center_freq2 =
3322 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
3323 	}
3324 
3325 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
3326 		chandef->edmg.channels =
3327 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
3328 
3329 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
3330 			chandef->edmg.bw_config =
3331 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
3332 	} else {
3333 		chandef->edmg.bw_config = 0;
3334 		chandef->edmg.channels = 0;
3335 	}
3336 
3337 	if (!cfg80211_chandef_valid(chandef)) {
3338 		NL_SET_ERR_MSG(extack, "invalid channel definition");
3339 		return -EINVAL;
3340 	}
3341 
3342 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
3343 				     IEEE80211_CHAN_DISABLED)) {
3344 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
3345 		return -EINVAL;
3346 	}
3347 
3348 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
3349 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
3350 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
3351 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
3352 		return -EINVAL;
3353 	}
3354 
3355 	return 0;
3356 }
3357 
3358 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
3359 				 struct net_device *dev,
3360 				 struct genl_info *info,
3361 				 int _link_id)
3362 {
3363 	struct cfg80211_chan_def chandef;
3364 	int result;
3365 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
3366 	struct wireless_dev *wdev = NULL;
3367 	int link_id = _link_id;
3368 
3369 	if (dev)
3370 		wdev = dev->ieee80211_ptr;
3371 	if (!nl80211_can_set_dev_channel(wdev))
3372 		return -EOPNOTSUPP;
3373 	if (wdev)
3374 		iftype = wdev->iftype;
3375 
3376 	if (link_id < 0) {
3377 		if (wdev && wdev->valid_links)
3378 			return -EINVAL;
3379 		link_id = 0;
3380 	}
3381 
3382 	result = nl80211_parse_chandef(rdev, info, &chandef);
3383 	if (result)
3384 		return result;
3385 
3386 	switch (iftype) {
3387 	case NL80211_IFTYPE_AP:
3388 	case NL80211_IFTYPE_P2P_GO:
3389 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
3390 						   iftype))
3391 			return -EINVAL;
3392 		if (wdev->links[link_id].ap.beacon_interval) {
3393 			struct ieee80211_channel *cur_chan;
3394 
3395 			if (!dev || !rdev->ops->set_ap_chanwidth ||
3396 			    !(rdev->wiphy.features &
3397 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE))
3398 				return -EBUSY;
3399 
3400 			/* Only allow dynamic channel width changes */
3401 			cur_chan = wdev->links[link_id].ap.chandef.chan;
3402 			if (chandef.chan != cur_chan)
3403 				return -EBUSY;
3404 
3405 			/* only allow this for regular channel widths */
3406 			switch (wdev->links[link_id].ap.chandef.width) {
3407 			case NL80211_CHAN_WIDTH_20_NOHT:
3408 			case NL80211_CHAN_WIDTH_20:
3409 			case NL80211_CHAN_WIDTH_40:
3410 			case NL80211_CHAN_WIDTH_80:
3411 			case NL80211_CHAN_WIDTH_80P80:
3412 			case NL80211_CHAN_WIDTH_160:
3413 			case NL80211_CHAN_WIDTH_320:
3414 				break;
3415 			default:
3416 				return -EINVAL;
3417 			}
3418 
3419 			switch (chandef.width) {
3420 			case NL80211_CHAN_WIDTH_20_NOHT:
3421 			case NL80211_CHAN_WIDTH_20:
3422 			case NL80211_CHAN_WIDTH_40:
3423 			case NL80211_CHAN_WIDTH_80:
3424 			case NL80211_CHAN_WIDTH_80P80:
3425 			case NL80211_CHAN_WIDTH_160:
3426 			case NL80211_CHAN_WIDTH_320:
3427 				break;
3428 			default:
3429 				return -EINVAL;
3430 			}
3431 
3432 			result = rdev_set_ap_chanwidth(rdev, dev, link_id,
3433 						       &chandef);
3434 			if (result)
3435 				return result;
3436 			wdev->links[link_id].ap.chandef = chandef;
3437 		} else {
3438 			wdev->u.ap.preset_chandef = chandef;
3439 		}
3440 		return 0;
3441 	case NL80211_IFTYPE_MESH_POINT:
3442 		return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
3443 	case NL80211_IFTYPE_MONITOR:
3444 		return cfg80211_set_monitor_channel(rdev, &chandef);
3445 	default:
3446 		break;
3447 	}
3448 
3449 	return -EINVAL;
3450 }
3451 
3452 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
3453 {
3454 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3455 	int link_id = nl80211_link_id_or_invalid(info->attrs);
3456 	struct net_device *netdev = info->user_ptr[1];
3457 	int ret;
3458 
3459 	wdev_lock(netdev->ieee80211_ptr);
3460 	ret = __nl80211_set_channel(rdev, netdev, info, link_id);
3461 	wdev_unlock(netdev->ieee80211_ptr);
3462 
3463 	return ret;
3464 }
3465 
3466 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
3467 {
3468 	struct cfg80211_registered_device *rdev = NULL;
3469 	struct net_device *netdev = NULL;
3470 	struct wireless_dev *wdev;
3471 	int result = 0, rem_txq_params = 0;
3472 	struct nlattr *nl_txq_params;
3473 	u32 changed;
3474 	u8 retry_short = 0, retry_long = 0;
3475 	u32 frag_threshold = 0, rts_threshold = 0;
3476 	u8 coverage_class = 0;
3477 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
3478 
3479 	rtnl_lock();
3480 	/*
3481 	 * Try to find the wiphy and netdev. Normally this
3482 	 * function shouldn't need the netdev, but this is
3483 	 * done for backward compatibility -- previously
3484 	 * setting the channel was done per wiphy, but now
3485 	 * it is per netdev. Previous userland like hostapd
3486 	 * also passed a netdev to set_wiphy, so that it is
3487 	 * possible to let that go to the right netdev!
3488 	 */
3489 
3490 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
3491 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
3492 
3493 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
3494 		if (netdev && netdev->ieee80211_ptr)
3495 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
3496 		else
3497 			netdev = NULL;
3498 	}
3499 
3500 	if (!netdev) {
3501 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
3502 						  info->attrs);
3503 		if (IS_ERR(rdev)) {
3504 			rtnl_unlock();
3505 			return PTR_ERR(rdev);
3506 		}
3507 		wdev = NULL;
3508 		netdev = NULL;
3509 		result = 0;
3510 	} else
3511 		wdev = netdev->ieee80211_ptr;
3512 
3513 	wiphy_lock(&rdev->wiphy);
3514 
3515 	/*
3516 	 * end workaround code, by now the rdev is available
3517 	 * and locked, and wdev may or may not be NULL.
3518 	 */
3519 
3520 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
3521 		result = cfg80211_dev_rename(
3522 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
3523 	rtnl_unlock();
3524 
3525 	if (result)
3526 		goto out;
3527 
3528 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
3529 		struct ieee80211_txq_params txq_params;
3530 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
3531 
3532 		if (!rdev->ops->set_txq_params) {
3533 			result = -EOPNOTSUPP;
3534 			goto out;
3535 		}
3536 
3537 		if (!netdev) {
3538 			result = -EINVAL;
3539 			goto out;
3540 		}
3541 
3542 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3543 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3544 			result = -EINVAL;
3545 			goto out;
3546 		}
3547 
3548 		if (!netif_running(netdev)) {
3549 			result = -ENETDOWN;
3550 			goto out;
3551 		}
3552 
3553 		nla_for_each_nested(nl_txq_params,
3554 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
3555 				    rem_txq_params) {
3556 			result = nla_parse_nested_deprecated(tb,
3557 							     NL80211_TXQ_ATTR_MAX,
3558 							     nl_txq_params,
3559 							     txq_params_policy,
3560 							     info->extack);
3561 			if (result)
3562 				goto out;
3563 			result = parse_txq_params(tb, &txq_params);
3564 			if (result)
3565 				goto out;
3566 
3567 			txq_params.link_id =
3568 				nl80211_link_id_or_invalid(info->attrs);
3569 
3570 			wdev_lock(netdev->ieee80211_ptr);
3571 			if (txq_params.link_id >= 0 &&
3572 			    !(netdev->ieee80211_ptr->valid_links &
3573 			      BIT(txq_params.link_id)))
3574 				result = -ENOLINK;
3575 			else if (txq_params.link_id >= 0 &&
3576 				 !netdev->ieee80211_ptr->valid_links)
3577 				result = -EINVAL;
3578 			else
3579 				result = rdev_set_txq_params(rdev, netdev,
3580 							     &txq_params);
3581 			wdev_unlock(netdev->ieee80211_ptr);
3582 			if (result)
3583 				goto out;
3584 		}
3585 	}
3586 
3587 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3588 		int link_id = nl80211_link_id_or_invalid(info->attrs);
3589 
3590 		if (wdev) {
3591 			wdev_lock(wdev);
3592 			result = __nl80211_set_channel(
3593 				rdev,
3594 				nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
3595 				info, link_id);
3596 			wdev_unlock(wdev);
3597 		} else {
3598 			result = __nl80211_set_channel(rdev, netdev, info, link_id);
3599 		}
3600 
3601 		if (result)
3602 			goto out;
3603 	}
3604 
3605 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
3606 		struct wireless_dev *txp_wdev = wdev;
3607 		enum nl80211_tx_power_setting type;
3608 		int idx, mbm = 0;
3609 
3610 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
3611 			txp_wdev = NULL;
3612 
3613 		if (!rdev->ops->set_tx_power) {
3614 			result = -EOPNOTSUPP;
3615 			goto out;
3616 		}
3617 
3618 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
3619 		type = nla_get_u32(info->attrs[idx]);
3620 
3621 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
3622 		    (type != NL80211_TX_POWER_AUTOMATIC)) {
3623 			result = -EINVAL;
3624 			goto out;
3625 		}
3626 
3627 		if (type != NL80211_TX_POWER_AUTOMATIC) {
3628 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
3629 			mbm = nla_get_u32(info->attrs[idx]);
3630 		}
3631 
3632 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
3633 		if (result)
3634 			goto out;
3635 	}
3636 
3637 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
3638 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
3639 		u32 tx_ant, rx_ant;
3640 
3641 		if ((!rdev->wiphy.available_antennas_tx &&
3642 		     !rdev->wiphy.available_antennas_rx) ||
3643 		    !rdev->ops->set_antenna) {
3644 			result = -EOPNOTSUPP;
3645 			goto out;
3646 		}
3647 
3648 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3649 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3650 
3651 		/* reject antenna configurations which don't match the
3652 		 * available antenna masks, except for the "all" mask */
3653 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3654 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
3655 			result = -EINVAL;
3656 			goto out;
3657 		}
3658 
3659 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3660 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3661 
3662 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3663 		if (result)
3664 			goto out;
3665 	}
3666 
3667 	changed = 0;
3668 
3669 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3670 		retry_short = nla_get_u8(
3671 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3672 
3673 		changed |= WIPHY_PARAM_RETRY_SHORT;
3674 	}
3675 
3676 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3677 		retry_long = nla_get_u8(
3678 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3679 
3680 		changed |= WIPHY_PARAM_RETRY_LONG;
3681 	}
3682 
3683 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3684 		frag_threshold = nla_get_u32(
3685 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3686 		if (frag_threshold < 256) {
3687 			result = -EINVAL;
3688 			goto out;
3689 		}
3690 
3691 		if (frag_threshold != (u32) -1) {
3692 			/*
3693 			 * Fragments (apart from the last one) are required to
3694 			 * have even length. Make the fragmentation code
3695 			 * simpler by stripping LSB should someone try to use
3696 			 * odd threshold value.
3697 			 */
3698 			frag_threshold &= ~0x1;
3699 		}
3700 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3701 	}
3702 
3703 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3704 		rts_threshold = nla_get_u32(
3705 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3706 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3707 	}
3708 
3709 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3710 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3711 			result = -EINVAL;
3712 			goto out;
3713 		}
3714 
3715 		coverage_class = nla_get_u8(
3716 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3717 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3718 	}
3719 
3720 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3721 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION)) {
3722 			result = -EOPNOTSUPP;
3723 			goto out;
3724 		}
3725 
3726 		changed |= WIPHY_PARAM_DYN_ACK;
3727 	}
3728 
3729 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3730 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3731 					     NL80211_EXT_FEATURE_TXQS)) {
3732 			result = -EOPNOTSUPP;
3733 			goto out;
3734 		}
3735 		txq_limit = nla_get_u32(
3736 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3737 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3738 	}
3739 
3740 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3741 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3742 					     NL80211_EXT_FEATURE_TXQS)) {
3743 			result = -EOPNOTSUPP;
3744 			goto out;
3745 		}
3746 		txq_memory_limit = nla_get_u32(
3747 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3748 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3749 	}
3750 
3751 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3752 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3753 					     NL80211_EXT_FEATURE_TXQS)) {
3754 			result = -EOPNOTSUPP;
3755 			goto out;
3756 		}
3757 		txq_quantum = nla_get_u32(
3758 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3759 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3760 	}
3761 
3762 	if (changed) {
3763 		u8 old_retry_short, old_retry_long;
3764 		u32 old_frag_threshold, old_rts_threshold;
3765 		u8 old_coverage_class;
3766 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3767 
3768 		if (!rdev->ops->set_wiphy_params) {
3769 			result = -EOPNOTSUPP;
3770 			goto out;
3771 		}
3772 
3773 		old_retry_short = rdev->wiphy.retry_short;
3774 		old_retry_long = rdev->wiphy.retry_long;
3775 		old_frag_threshold = rdev->wiphy.frag_threshold;
3776 		old_rts_threshold = rdev->wiphy.rts_threshold;
3777 		old_coverage_class = rdev->wiphy.coverage_class;
3778 		old_txq_limit = rdev->wiphy.txq_limit;
3779 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3780 		old_txq_quantum = rdev->wiphy.txq_quantum;
3781 
3782 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3783 			rdev->wiphy.retry_short = retry_short;
3784 		if (changed & WIPHY_PARAM_RETRY_LONG)
3785 			rdev->wiphy.retry_long = retry_long;
3786 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3787 			rdev->wiphy.frag_threshold = frag_threshold;
3788 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3789 			rdev->wiphy.rts_threshold = rts_threshold;
3790 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3791 			rdev->wiphy.coverage_class = coverage_class;
3792 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3793 			rdev->wiphy.txq_limit = txq_limit;
3794 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3795 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3796 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3797 			rdev->wiphy.txq_quantum = txq_quantum;
3798 
3799 		result = rdev_set_wiphy_params(rdev, changed);
3800 		if (result) {
3801 			rdev->wiphy.retry_short = old_retry_short;
3802 			rdev->wiphy.retry_long = old_retry_long;
3803 			rdev->wiphy.frag_threshold = old_frag_threshold;
3804 			rdev->wiphy.rts_threshold = old_rts_threshold;
3805 			rdev->wiphy.coverage_class = old_coverage_class;
3806 			rdev->wiphy.txq_limit = old_txq_limit;
3807 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3808 			rdev->wiphy.txq_quantum = old_txq_quantum;
3809 			goto out;
3810 		}
3811 	}
3812 
3813 	result = 0;
3814 
3815 out:
3816 	wiphy_unlock(&rdev->wiphy);
3817 	return result;
3818 }
3819 
3820 int nl80211_send_chandef(struct sk_buff *msg, const struct cfg80211_chan_def *chandef)
3821 {
3822 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3823 		return -EINVAL;
3824 
3825 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3826 			chandef->chan->center_freq))
3827 		return -ENOBUFS;
3828 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
3829 			chandef->chan->freq_offset))
3830 		return -ENOBUFS;
3831 	switch (chandef->width) {
3832 	case NL80211_CHAN_WIDTH_20_NOHT:
3833 	case NL80211_CHAN_WIDTH_20:
3834 	case NL80211_CHAN_WIDTH_40:
3835 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3836 				cfg80211_get_chandef_type(chandef)))
3837 			return -ENOBUFS;
3838 		break;
3839 	default:
3840 		break;
3841 	}
3842 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3843 		return -ENOBUFS;
3844 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3845 		return -ENOBUFS;
3846 	if (chandef->center_freq2 &&
3847 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3848 		return -ENOBUFS;
3849 	return 0;
3850 }
3851 EXPORT_SYMBOL(nl80211_send_chandef);
3852 
3853 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3854 			      struct cfg80211_registered_device *rdev,
3855 			      struct wireless_dev *wdev,
3856 			      enum nl80211_commands cmd)
3857 {
3858 	struct net_device *dev = wdev->netdev;
3859 	void *hdr;
3860 
3861 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3862 		cmd != NL80211_CMD_DEL_INTERFACE &&
3863 		cmd != NL80211_CMD_SET_INTERFACE);
3864 
3865 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3866 	if (!hdr)
3867 		return -1;
3868 
3869 	if (dev &&
3870 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3871 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3872 		goto nla_put_failure;
3873 
3874 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3875 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3876 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3877 			      NL80211_ATTR_PAD) ||
3878 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3879 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3880 			rdev->devlist_generation ^
3881 			(cfg80211_rdev_list_generation << 2)) ||
3882 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3883 		goto nla_put_failure;
3884 
3885 	if (rdev->ops->get_channel && !wdev->valid_links) {
3886 		struct cfg80211_chan_def chandef = {};
3887 		int ret;
3888 
3889 		ret = rdev_get_channel(rdev, wdev, 0, &chandef);
3890 		if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3891 			goto nla_put_failure;
3892 	}
3893 
3894 	if (rdev->ops->get_tx_power) {
3895 		int dbm, ret;
3896 
3897 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3898 		if (ret == 0 &&
3899 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3900 				DBM_TO_MBM(dbm)))
3901 			goto nla_put_failure;
3902 	}
3903 
3904 	wdev_lock(wdev);
3905 	switch (wdev->iftype) {
3906 	case NL80211_IFTYPE_AP:
3907 	case NL80211_IFTYPE_P2P_GO:
3908 		if (wdev->u.ap.ssid_len &&
3909 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
3910 			    wdev->u.ap.ssid))
3911 			goto nla_put_failure_locked;
3912 		break;
3913 	case NL80211_IFTYPE_STATION:
3914 	case NL80211_IFTYPE_P2P_CLIENT:
3915 		if (wdev->u.client.ssid_len &&
3916 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.client.ssid_len,
3917 			    wdev->u.client.ssid))
3918 			goto nla_put_failure_locked;
3919 		break;
3920 	case NL80211_IFTYPE_ADHOC:
3921 		if (wdev->u.ibss.ssid_len &&
3922 		    nla_put(msg, NL80211_ATTR_SSID, wdev->u.ibss.ssid_len,
3923 			    wdev->u.ibss.ssid))
3924 			goto nla_put_failure_locked;
3925 		break;
3926 	default:
3927 		/* nothing */
3928 		break;
3929 	}
3930 	wdev_unlock(wdev);
3931 
3932 	if (rdev->ops->get_txq_stats) {
3933 		struct cfg80211_txq_stats txqstats = {};
3934 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3935 
3936 		if (ret == 0 &&
3937 		    !nl80211_put_txq_stats(msg, &txqstats,
3938 					   NL80211_ATTR_TXQ_STATS))
3939 			goto nla_put_failure;
3940 	}
3941 
3942 	if (wdev->valid_links) {
3943 		unsigned int link_id;
3944 		struct nlattr *links = nla_nest_start(msg,
3945 						      NL80211_ATTR_MLO_LINKS);
3946 
3947 		if (!links)
3948 			goto nla_put_failure;
3949 
3950 		for_each_valid_link(wdev, link_id) {
3951 			struct nlattr *link = nla_nest_start(msg, link_id + 1);
3952 			struct cfg80211_chan_def chandef = {};
3953 			int ret;
3954 
3955 			if (!link)
3956 				goto nla_put_failure;
3957 
3958 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3959 				goto nla_put_failure;
3960 			if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
3961 				    wdev->links[link_id].addr))
3962 				goto nla_put_failure;
3963 
3964 			ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
3965 			if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3966 				goto nla_put_failure;
3967 
3968 			nla_nest_end(msg, link);
3969 		}
3970 
3971 		nla_nest_end(msg, links);
3972 	}
3973 
3974 	genlmsg_end(msg, hdr);
3975 	return 0;
3976 
3977  nla_put_failure_locked:
3978 	wdev_unlock(wdev);
3979  nla_put_failure:
3980 	genlmsg_cancel(msg, hdr);
3981 	return -EMSGSIZE;
3982 }
3983 
3984 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3985 {
3986 	int wp_idx = 0;
3987 	int if_idx = 0;
3988 	int wp_start = cb->args[0];
3989 	int if_start = cb->args[1];
3990 	int filter_wiphy = -1;
3991 	struct cfg80211_registered_device *rdev;
3992 	struct wireless_dev *wdev;
3993 	int ret;
3994 
3995 	rtnl_lock();
3996 	if (!cb->args[2]) {
3997 		struct nl80211_dump_wiphy_state state = {
3998 			.filter_wiphy = -1,
3999 		};
4000 
4001 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
4002 		if (ret)
4003 			goto out_unlock;
4004 
4005 		filter_wiphy = state.filter_wiphy;
4006 
4007 		/*
4008 		 * if filtering, set cb->args[2] to +1 since 0 is the default
4009 		 * value needed to determine that parsing is necessary.
4010 		 */
4011 		if (filter_wiphy >= 0)
4012 			cb->args[2] = filter_wiphy + 1;
4013 		else
4014 			cb->args[2] = -1;
4015 	} else if (cb->args[2] > 0) {
4016 		filter_wiphy = cb->args[2] - 1;
4017 	}
4018 
4019 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
4020 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
4021 			continue;
4022 		if (wp_idx < wp_start) {
4023 			wp_idx++;
4024 			continue;
4025 		}
4026 
4027 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
4028 			continue;
4029 
4030 		if_idx = 0;
4031 
4032 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4033 			if (if_idx < if_start) {
4034 				if_idx++;
4035 				continue;
4036 			}
4037 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
4038 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
4039 					       rdev, wdev,
4040 					       NL80211_CMD_NEW_INTERFACE) < 0) {
4041 				goto out;
4042 			}
4043 			if_idx++;
4044 		}
4045 
4046 		if_start = 0;
4047 		wp_idx++;
4048 	}
4049  out:
4050 	cb->args[0] = wp_idx;
4051 	cb->args[1] = if_idx;
4052 
4053 	ret = skb->len;
4054  out_unlock:
4055 	rtnl_unlock();
4056 
4057 	return ret;
4058 }
4059 
4060 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
4061 {
4062 	struct sk_buff *msg;
4063 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4064 	struct wireless_dev *wdev = info->user_ptr[1];
4065 
4066 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4067 	if (!msg)
4068 		return -ENOMEM;
4069 
4070 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4071 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4072 		nlmsg_free(msg);
4073 		return -ENOBUFS;
4074 	}
4075 
4076 	return genlmsg_reply(msg, info);
4077 }
4078 
4079 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
4080 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
4081 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
4082 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
4083 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
4084 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
4085 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
4086 };
4087 
4088 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
4089 {
4090 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
4091 	int flag;
4092 
4093 	*mntrflags = 0;
4094 
4095 	if (!nla)
4096 		return -EINVAL;
4097 
4098 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
4099 		return -EINVAL;
4100 
4101 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
4102 		if (flags[flag])
4103 			*mntrflags |= (1<<flag);
4104 
4105 	*mntrflags |= MONITOR_FLAG_CHANGED;
4106 
4107 	return 0;
4108 }
4109 
4110 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
4111 				     enum nl80211_iftype type,
4112 				     struct genl_info *info,
4113 				     struct vif_params *params)
4114 {
4115 	bool change = false;
4116 	int err;
4117 
4118 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
4119 		if (type != NL80211_IFTYPE_MONITOR)
4120 			return -EINVAL;
4121 
4122 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
4123 					  &params->flags);
4124 		if (err)
4125 			return err;
4126 
4127 		change = true;
4128 	}
4129 
4130 	if (params->flags & MONITOR_FLAG_ACTIVE &&
4131 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
4132 		return -EOPNOTSUPP;
4133 
4134 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
4135 		const u8 *mumimo_groups;
4136 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4137 
4138 		if (type != NL80211_IFTYPE_MONITOR)
4139 			return -EINVAL;
4140 
4141 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4142 			return -EOPNOTSUPP;
4143 
4144 		mumimo_groups =
4145 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
4146 
4147 		/* bits 0 and 63 are reserved and must be zero */
4148 		if ((mumimo_groups[0] & BIT(0)) ||
4149 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
4150 			return -EINVAL;
4151 
4152 		params->vht_mumimo_groups = mumimo_groups;
4153 		change = true;
4154 	}
4155 
4156 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
4157 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4158 
4159 		if (type != NL80211_IFTYPE_MONITOR)
4160 			return -EINVAL;
4161 
4162 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4163 			return -EOPNOTSUPP;
4164 
4165 		params->vht_mumimo_follow_addr =
4166 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
4167 		change = true;
4168 	}
4169 
4170 	return change ? 1 : 0;
4171 }
4172 
4173 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
4174 			       struct net_device *netdev, u8 use_4addr,
4175 			       enum nl80211_iftype iftype)
4176 {
4177 	if (!use_4addr) {
4178 		if (netdev && netif_is_bridge_port(netdev))
4179 			return -EBUSY;
4180 		return 0;
4181 	}
4182 
4183 	switch (iftype) {
4184 	case NL80211_IFTYPE_AP_VLAN:
4185 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
4186 			return 0;
4187 		break;
4188 	case NL80211_IFTYPE_STATION:
4189 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
4190 			return 0;
4191 		break;
4192 	default:
4193 		break;
4194 	}
4195 
4196 	return -EOPNOTSUPP;
4197 }
4198 
4199 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
4200 {
4201 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4202 	struct vif_params params;
4203 	int err;
4204 	enum nl80211_iftype otype, ntype;
4205 	struct net_device *dev = info->user_ptr[1];
4206 	bool change = false;
4207 
4208 	memset(&params, 0, sizeof(params));
4209 
4210 	otype = ntype = dev->ieee80211_ptr->iftype;
4211 
4212 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
4213 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4214 		if (otype != ntype)
4215 			change = true;
4216 	}
4217 
4218 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
4219 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4220 
4221 		if (ntype != NL80211_IFTYPE_MESH_POINT)
4222 			return -EINVAL;
4223 		if (otype != NL80211_IFTYPE_MESH_POINT)
4224 			return -EINVAL;
4225 		if (netif_running(dev))
4226 			return -EBUSY;
4227 
4228 		wdev_lock(wdev);
4229 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4230 			     IEEE80211_MAX_MESH_ID_LEN);
4231 		wdev->u.mesh.id_up_len =
4232 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4233 		memcpy(wdev->u.mesh.id,
4234 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4235 		       wdev->u.mesh.id_up_len);
4236 		wdev_unlock(wdev);
4237 	}
4238 
4239 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4240 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4241 		change = true;
4242 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
4243 		if (err)
4244 			return err;
4245 	} else {
4246 		params.use_4addr = -1;
4247 	}
4248 
4249 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
4250 	if (err < 0)
4251 		return err;
4252 	if (err > 0)
4253 		change = true;
4254 
4255 	if (change)
4256 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
4257 	else
4258 		err = 0;
4259 
4260 	if (!err && params.use_4addr != -1)
4261 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
4262 
4263 	if (change && !err) {
4264 		struct wireless_dev *wdev = dev->ieee80211_ptr;
4265 
4266 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
4267 	}
4268 
4269 	return err;
4270 }
4271 
4272 static int _nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4273 {
4274 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4275 	struct vif_params params;
4276 	struct wireless_dev *wdev;
4277 	struct sk_buff *msg;
4278 	int err;
4279 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
4280 
4281 	memset(&params, 0, sizeof(params));
4282 
4283 	if (!info->attrs[NL80211_ATTR_IFNAME])
4284 		return -EINVAL;
4285 
4286 	if (info->attrs[NL80211_ATTR_IFTYPE])
4287 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4288 
4289 	if (!rdev->ops->add_virtual_intf)
4290 		return -EOPNOTSUPP;
4291 
4292 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
4293 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
4294 	    info->attrs[NL80211_ATTR_MAC]) {
4295 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
4296 			   ETH_ALEN);
4297 		if (!is_valid_ether_addr(params.macaddr))
4298 			return -EADDRNOTAVAIL;
4299 	}
4300 
4301 	if (info->attrs[NL80211_ATTR_4ADDR]) {
4302 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4303 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
4304 		if (err)
4305 			return err;
4306 	}
4307 
4308 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
4309 		return -EOPNOTSUPP;
4310 
4311 	err = nl80211_parse_mon_options(rdev, type, info, &params);
4312 	if (err < 0)
4313 		return err;
4314 
4315 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4316 	if (!msg)
4317 		return -ENOMEM;
4318 
4319 	wdev = rdev_add_virtual_intf(rdev,
4320 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
4321 				NET_NAME_USER, type, &params);
4322 	if (WARN_ON(!wdev)) {
4323 		nlmsg_free(msg);
4324 		return -EPROTO;
4325 	} else if (IS_ERR(wdev)) {
4326 		nlmsg_free(msg);
4327 		return PTR_ERR(wdev);
4328 	}
4329 
4330 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4331 		wdev->owner_nlportid = info->snd_portid;
4332 
4333 	switch (type) {
4334 	case NL80211_IFTYPE_MESH_POINT:
4335 		if (!info->attrs[NL80211_ATTR_MESH_ID])
4336 			break;
4337 		wdev_lock(wdev);
4338 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4339 			     IEEE80211_MAX_MESH_ID_LEN);
4340 		wdev->u.mesh.id_up_len =
4341 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4342 		memcpy(wdev->u.mesh.id,
4343 		       nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4344 		       wdev->u.mesh.id_up_len);
4345 		wdev_unlock(wdev);
4346 		break;
4347 	case NL80211_IFTYPE_NAN:
4348 	case NL80211_IFTYPE_P2P_DEVICE:
4349 		/*
4350 		 * P2P Device and NAN do not have a netdev, so don't go
4351 		 * through the netdev notifier and must be added here
4352 		 */
4353 		cfg80211_init_wdev(wdev);
4354 		cfg80211_register_wdev(rdev, wdev);
4355 		break;
4356 	default:
4357 		break;
4358 	}
4359 
4360 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4361 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4362 		nlmsg_free(msg);
4363 		return -ENOBUFS;
4364 	}
4365 
4366 	return genlmsg_reply(msg, info);
4367 }
4368 
4369 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4370 {
4371 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4372 	int ret;
4373 
4374 	/* to avoid failing a new interface creation due to pending removal */
4375 	cfg80211_destroy_ifaces(rdev);
4376 
4377 	wiphy_lock(&rdev->wiphy);
4378 	ret = _nl80211_new_interface(skb, info);
4379 	wiphy_unlock(&rdev->wiphy);
4380 
4381 	return ret;
4382 }
4383 
4384 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
4385 {
4386 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4387 	struct wireless_dev *wdev = info->user_ptr[1];
4388 
4389 	if (!rdev->ops->del_virtual_intf)
4390 		return -EOPNOTSUPP;
4391 
4392 	/*
4393 	 * We hold RTNL, so this is safe, without RTNL opencount cannot
4394 	 * reach 0, and thus the rdev cannot be deleted.
4395 	 *
4396 	 * We need to do it for the dev_close(), since that will call
4397 	 * the netdev notifiers, and we need to acquire the mutex there
4398 	 * but don't know if we get there from here or from some other
4399 	 * place (e.g. "ip link set ... down").
4400 	 */
4401 	mutex_unlock(&rdev->wiphy.mtx);
4402 
4403 	/*
4404 	 * If we remove a wireless device without a netdev then clear
4405 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
4406 	 * to check if it needs to do dev_put(). Otherwise it crashes
4407 	 * since the wdev has been freed, unlike with a netdev where
4408 	 * we need the dev_put() for the netdev to really be freed.
4409 	 */
4410 	if (!wdev->netdev)
4411 		info->user_ptr[1] = NULL;
4412 	else
4413 		dev_close(wdev->netdev);
4414 
4415 	mutex_lock(&rdev->wiphy.mtx);
4416 
4417 	return cfg80211_remove_virtual_intf(rdev, wdev);
4418 }
4419 
4420 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
4421 {
4422 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4423 	struct net_device *dev = info->user_ptr[1];
4424 	u16 noack_map;
4425 
4426 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
4427 		return -EINVAL;
4428 
4429 	if (!rdev->ops->set_noack_map)
4430 		return -EOPNOTSUPP;
4431 
4432 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
4433 
4434 	return rdev_set_noack_map(rdev, dev, noack_map);
4435 }
4436 
4437 static int nl80211_validate_key_link_id(struct genl_info *info,
4438 					struct wireless_dev *wdev,
4439 					int link_id, bool pairwise)
4440 {
4441 	if (pairwise) {
4442 		if (link_id != -1) {
4443 			GENL_SET_ERR_MSG(info,
4444 					 "link ID not allowed for pairwise key");
4445 			return -EINVAL;
4446 		}
4447 
4448 		return 0;
4449 	}
4450 
4451 	if (wdev->valid_links) {
4452 		if (link_id == -1) {
4453 			GENL_SET_ERR_MSG(info,
4454 					 "link ID must for MLO group key");
4455 			return -EINVAL;
4456 		}
4457 		if (!(wdev->valid_links & BIT(link_id))) {
4458 			GENL_SET_ERR_MSG(info, "invalid link ID for MLO group key");
4459 			return -EINVAL;
4460 		}
4461 	} else if (link_id != -1) {
4462 		GENL_SET_ERR_MSG(info, "link ID not allowed for non-MLO group key");
4463 		return -EINVAL;
4464 	}
4465 
4466 	return 0;
4467 }
4468 
4469 struct get_key_cookie {
4470 	struct sk_buff *msg;
4471 	int error;
4472 	int idx;
4473 };
4474 
4475 static void get_key_callback(void *c, struct key_params *params)
4476 {
4477 	struct nlattr *key;
4478 	struct get_key_cookie *cookie = c;
4479 
4480 	if ((params->seq &&
4481 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
4482 		     params->seq_len, params->seq)) ||
4483 	    (params->cipher &&
4484 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
4485 			 params->cipher)))
4486 		goto nla_put_failure;
4487 
4488 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
4489 	if (!key)
4490 		goto nla_put_failure;
4491 
4492 	if ((params->seq &&
4493 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
4494 		     params->seq_len, params->seq)) ||
4495 	    (params->cipher &&
4496 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
4497 			 params->cipher)))
4498 		goto nla_put_failure;
4499 
4500 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
4501 		goto nla_put_failure;
4502 
4503 	nla_nest_end(cookie->msg, key);
4504 
4505 	return;
4506  nla_put_failure:
4507 	cookie->error = 1;
4508 }
4509 
4510 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
4511 {
4512 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4513 	int err;
4514 	struct net_device *dev = info->user_ptr[1];
4515 	u8 key_idx = 0;
4516 	const u8 *mac_addr = NULL;
4517 	bool pairwise;
4518 	struct get_key_cookie cookie = {
4519 		.error = 0,
4520 	};
4521 	void *hdr;
4522 	struct sk_buff *msg;
4523 	bool bigtk_support = false;
4524 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4525 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4526 
4527 	if (wiphy_ext_feature_isset(&rdev->wiphy,
4528 				    NL80211_EXT_FEATURE_BEACON_PROTECTION))
4529 		bigtk_support = true;
4530 
4531 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
4532 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
4533 	    wiphy_ext_feature_isset(&rdev->wiphy,
4534 				    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
4535 		bigtk_support = true;
4536 
4537 	if (info->attrs[NL80211_ATTR_KEY_IDX]) {
4538 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
4539 
4540 		if (key_idx >= 6 && key_idx <= 7 && !bigtk_support) {
4541 			GENL_SET_ERR_MSG(info, "BIGTK not supported");
4542 			return -EINVAL;
4543 		}
4544 	}
4545 
4546 	if (info->attrs[NL80211_ATTR_MAC])
4547 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4548 
4549 	pairwise = !!mac_addr;
4550 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
4551 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
4552 
4553 		if (kt != NL80211_KEYTYPE_GROUP &&
4554 		    kt != NL80211_KEYTYPE_PAIRWISE)
4555 			return -EINVAL;
4556 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
4557 	}
4558 
4559 	if (!rdev->ops->get_key)
4560 		return -EOPNOTSUPP;
4561 
4562 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4563 		return -ENOENT;
4564 
4565 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4566 	if (!msg)
4567 		return -ENOMEM;
4568 
4569 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4570 			     NL80211_CMD_NEW_KEY);
4571 	if (!hdr)
4572 		goto nla_put_failure;
4573 
4574 	cookie.msg = msg;
4575 	cookie.idx = key_idx;
4576 
4577 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4578 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
4579 		goto nla_put_failure;
4580 	if (mac_addr &&
4581 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
4582 		goto nla_put_failure;
4583 
4584 	err = nl80211_validate_key_link_id(info, wdev, link_id, pairwise);
4585 	if (err)
4586 		goto free_msg;
4587 
4588 	err = rdev_get_key(rdev, dev, link_id, key_idx, pairwise, mac_addr,
4589 			   &cookie, get_key_callback);
4590 
4591 	if (err)
4592 		goto free_msg;
4593 
4594 	if (cookie.error)
4595 		goto nla_put_failure;
4596 
4597 	genlmsg_end(msg, hdr);
4598 	return genlmsg_reply(msg, info);
4599 
4600  nla_put_failure:
4601 	err = -ENOBUFS;
4602  free_msg:
4603 	nlmsg_free(msg);
4604 	return err;
4605 }
4606 
4607 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
4608 {
4609 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4610 	struct key_parse key;
4611 	int err;
4612 	struct net_device *dev = info->user_ptr[1];
4613 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4614 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4615 
4616 	err = nl80211_parse_key(info, &key);
4617 	if (err)
4618 		return err;
4619 
4620 	if (key.idx < 0)
4621 		return -EINVAL;
4622 
4623 	/* Only support setting default key and
4624 	 * Extended Key ID action NL80211_KEY_SET_TX.
4625 	 */
4626 	if (!key.def && !key.defmgmt && !key.defbeacon &&
4627 	    !(key.p.mode == NL80211_KEY_SET_TX))
4628 		return -EINVAL;
4629 
4630 	wdev_lock(wdev);
4631 
4632 	if (key.def) {
4633 		if (!rdev->ops->set_default_key) {
4634 			err = -EOPNOTSUPP;
4635 			goto out;
4636 		}
4637 
4638 		err = nl80211_key_allowed(wdev);
4639 		if (err)
4640 			goto out;
4641 
4642 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4643 		if (err)
4644 			goto out;
4645 
4646 		err = rdev_set_default_key(rdev, dev, link_id, key.idx,
4647 					   key.def_uni, key.def_multi);
4648 
4649 		if (err)
4650 			goto out;
4651 
4652 #ifdef CONFIG_CFG80211_WEXT
4653 		wdev->wext.default_key = key.idx;
4654 #endif
4655 	} else if (key.defmgmt) {
4656 		if (key.def_uni || !key.def_multi) {
4657 			err = -EINVAL;
4658 			goto out;
4659 		}
4660 
4661 		if (!rdev->ops->set_default_mgmt_key) {
4662 			err = -EOPNOTSUPP;
4663 			goto out;
4664 		}
4665 
4666 		err = nl80211_key_allowed(wdev);
4667 		if (err)
4668 			goto out;
4669 
4670 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4671 		if (err)
4672 			goto out;
4673 
4674 		err = rdev_set_default_mgmt_key(rdev, dev, link_id, key.idx);
4675 		if (err)
4676 			goto out;
4677 
4678 #ifdef CONFIG_CFG80211_WEXT
4679 		wdev->wext.default_mgmt_key = key.idx;
4680 #endif
4681 	} else if (key.defbeacon) {
4682 		if (key.def_uni || !key.def_multi) {
4683 			err = -EINVAL;
4684 			goto out;
4685 		}
4686 
4687 		if (!rdev->ops->set_default_beacon_key) {
4688 			err = -EOPNOTSUPP;
4689 			goto out;
4690 		}
4691 
4692 		err = nl80211_key_allowed(wdev);
4693 		if (err)
4694 			goto out;
4695 
4696 		err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4697 		if (err)
4698 			goto out;
4699 
4700 		err = rdev_set_default_beacon_key(rdev, dev, link_id, key.idx);
4701 		if (err)
4702 			goto out;
4703 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
4704 		   wiphy_ext_feature_isset(&rdev->wiphy,
4705 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
4706 		u8 *mac_addr = NULL;
4707 
4708 		if (info->attrs[NL80211_ATTR_MAC])
4709 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4710 
4711 		if (!mac_addr || key.idx < 0 || key.idx > 1) {
4712 			err = -EINVAL;
4713 			goto out;
4714 		}
4715 
4716 		err = nl80211_validate_key_link_id(info, wdev, link_id, true);
4717 		if (err)
4718 			goto out;
4719 
4720 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4721 				   NL80211_KEYTYPE_PAIRWISE,
4722 				   mac_addr, &key.p);
4723 	} else {
4724 		err = -EINVAL;
4725 	}
4726  out:
4727 	wdev_unlock(wdev);
4728 
4729 	return err;
4730 }
4731 
4732 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
4733 {
4734 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4735 	int err;
4736 	struct net_device *dev = info->user_ptr[1];
4737 	struct key_parse key;
4738 	const u8 *mac_addr = NULL;
4739 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4740 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4741 
4742 	err = nl80211_parse_key(info, &key);
4743 	if (err)
4744 		return err;
4745 
4746 	if (!key.p.key) {
4747 		GENL_SET_ERR_MSG(info, "no key");
4748 		return -EINVAL;
4749 	}
4750 
4751 	if (info->attrs[NL80211_ATTR_MAC])
4752 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4753 
4754 	if (key.type == -1) {
4755 		if (mac_addr)
4756 			key.type = NL80211_KEYTYPE_PAIRWISE;
4757 		else
4758 			key.type = NL80211_KEYTYPE_GROUP;
4759 	}
4760 
4761 	/* for now */
4762 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4763 	    key.type != NL80211_KEYTYPE_GROUP) {
4764 		GENL_SET_ERR_MSG(info, "key type not pairwise or group");
4765 		return -EINVAL;
4766 	}
4767 
4768 	if (key.type == NL80211_KEYTYPE_GROUP &&
4769 	    info->attrs[NL80211_ATTR_VLAN_ID])
4770 		key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
4771 
4772 	if (!rdev->ops->add_key)
4773 		return -EOPNOTSUPP;
4774 
4775 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
4776 					   key.type == NL80211_KEYTYPE_PAIRWISE,
4777 					   mac_addr)) {
4778 		GENL_SET_ERR_MSG(info, "key setting validation failed");
4779 		return -EINVAL;
4780 	}
4781 
4782 	wdev_lock(wdev);
4783 	err = nl80211_key_allowed(wdev);
4784 	if (err)
4785 		GENL_SET_ERR_MSG(info, "key not allowed");
4786 
4787 	if (!err)
4788 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4789 				key.type == NL80211_KEYTYPE_PAIRWISE);
4790 
4791 	if (!err) {
4792 		err = rdev_add_key(rdev, dev, link_id, key.idx,
4793 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4794 				    mac_addr, &key.p);
4795 		if (err)
4796 			GENL_SET_ERR_MSG(info, "key addition failed");
4797 	}
4798 	wdev_unlock(wdev);
4799 
4800 	return err;
4801 }
4802 
4803 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
4804 {
4805 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4806 	int err;
4807 	struct net_device *dev = info->user_ptr[1];
4808 	u8 *mac_addr = NULL;
4809 	struct key_parse key;
4810 	int link_id = nl80211_link_id_or_invalid(info->attrs);
4811 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4812 
4813 	err = nl80211_parse_key(info, &key);
4814 	if (err)
4815 		return err;
4816 
4817 	if (info->attrs[NL80211_ATTR_MAC])
4818 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4819 
4820 	if (key.type == -1) {
4821 		if (mac_addr)
4822 			key.type = NL80211_KEYTYPE_PAIRWISE;
4823 		else
4824 			key.type = NL80211_KEYTYPE_GROUP;
4825 	}
4826 
4827 	/* for now */
4828 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4829 	    key.type != NL80211_KEYTYPE_GROUP)
4830 		return -EINVAL;
4831 
4832 	if (!cfg80211_valid_key_idx(rdev, key.idx,
4833 				    key.type == NL80211_KEYTYPE_PAIRWISE))
4834 		return -EINVAL;
4835 
4836 	if (!rdev->ops->del_key)
4837 		return -EOPNOTSUPP;
4838 
4839 	wdev_lock(wdev);
4840 	err = nl80211_key_allowed(wdev);
4841 
4842 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4843 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4844 		err = -ENOENT;
4845 
4846 	if (!err)
4847 		err = nl80211_validate_key_link_id(info, wdev, link_id,
4848 				key.type == NL80211_KEYTYPE_PAIRWISE);
4849 
4850 	if (!err)
4851 		err = rdev_del_key(rdev, dev, link_id, key.idx,
4852 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4853 				   mac_addr);
4854 
4855 #ifdef CONFIG_CFG80211_WEXT
4856 	if (!err) {
4857 		if (key.idx == wdev->wext.default_key)
4858 			wdev->wext.default_key = -1;
4859 		else if (key.idx == wdev->wext.default_mgmt_key)
4860 			wdev->wext.default_mgmt_key = -1;
4861 	}
4862 #endif
4863 	wdev_unlock(wdev);
4864 
4865 	return err;
4866 }
4867 
4868 /* This function returns an error or the number of nested attributes */
4869 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4870 {
4871 	struct nlattr *attr;
4872 	int n_entries = 0, tmp;
4873 
4874 	nla_for_each_nested(attr, nl_attr, tmp) {
4875 		if (nla_len(attr) != ETH_ALEN)
4876 			return -EINVAL;
4877 
4878 		n_entries++;
4879 	}
4880 
4881 	return n_entries;
4882 }
4883 
4884 /*
4885  * This function parses ACL information and allocates memory for ACL data.
4886  * On successful return, the calling function is responsible to free the
4887  * ACL buffer returned by this function.
4888  */
4889 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4890 						struct genl_info *info)
4891 {
4892 	enum nl80211_acl_policy acl_policy;
4893 	struct nlattr *attr;
4894 	struct cfg80211_acl_data *acl;
4895 	int i = 0, n_entries, tmp;
4896 
4897 	if (!wiphy->max_acl_mac_addrs)
4898 		return ERR_PTR(-EOPNOTSUPP);
4899 
4900 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4901 		return ERR_PTR(-EINVAL);
4902 
4903 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4904 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4905 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4906 		return ERR_PTR(-EINVAL);
4907 
4908 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4909 		return ERR_PTR(-EINVAL);
4910 
4911 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4912 	if (n_entries < 0)
4913 		return ERR_PTR(n_entries);
4914 
4915 	if (n_entries > wiphy->max_acl_mac_addrs)
4916 		return ERR_PTR(-ENOTSUPP);
4917 
4918 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4919 	if (!acl)
4920 		return ERR_PTR(-ENOMEM);
4921 	acl->n_acl_entries = n_entries;
4922 
4923 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4924 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4925 		i++;
4926 	}
4927 	acl->acl_policy = acl_policy;
4928 
4929 	return acl;
4930 }
4931 
4932 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4933 {
4934 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4935 	struct net_device *dev = info->user_ptr[1];
4936 	struct cfg80211_acl_data *acl;
4937 	int err;
4938 
4939 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4940 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4941 		return -EOPNOTSUPP;
4942 
4943 	if (!dev->ieee80211_ptr->links[0].ap.beacon_interval)
4944 		return -EINVAL;
4945 
4946 	acl = parse_acl_data(&rdev->wiphy, info);
4947 	if (IS_ERR(acl))
4948 		return PTR_ERR(acl);
4949 
4950 	err = rdev_set_mac_acl(rdev, dev, acl);
4951 
4952 	kfree(acl);
4953 
4954 	return err;
4955 }
4956 
4957 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4958 			   u8 *rates, u8 rates_len)
4959 {
4960 	u8 i;
4961 	u32 mask = 0;
4962 
4963 	for (i = 0; i < rates_len; i++) {
4964 		int rate = (rates[i] & 0x7f) * 5;
4965 		int ridx;
4966 
4967 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4968 			struct ieee80211_rate *srate =
4969 				&sband->bitrates[ridx];
4970 			if (rate == srate->bitrate) {
4971 				mask |= 1 << ridx;
4972 				break;
4973 			}
4974 		}
4975 		if (ridx == sband->n_bitrates)
4976 			return 0; /* rate not found */
4977 	}
4978 
4979 	return mask;
4980 }
4981 
4982 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4983 			       u8 *rates, u8 rates_len,
4984 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4985 {
4986 	u8 i;
4987 
4988 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4989 
4990 	for (i = 0; i < rates_len; i++) {
4991 		int ridx, rbit;
4992 
4993 		ridx = rates[i] / 8;
4994 		rbit = BIT(rates[i] % 8);
4995 
4996 		/* check validity */
4997 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4998 			return false;
4999 
5000 		/* check availability */
5001 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
5002 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5003 			mcs[ridx] |= rbit;
5004 		else
5005 			return false;
5006 	}
5007 
5008 	return true;
5009 }
5010 
5011 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
5012 {
5013 	u16 mcs_mask = 0;
5014 
5015 	switch (vht_mcs_map) {
5016 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
5017 		break;
5018 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
5019 		mcs_mask = 0x00FF;
5020 		break;
5021 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
5022 		mcs_mask = 0x01FF;
5023 		break;
5024 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
5025 		mcs_mask = 0x03FF;
5026 		break;
5027 	default:
5028 		break;
5029 	}
5030 
5031 	return mcs_mask;
5032 }
5033 
5034 static void vht_build_mcs_mask(u16 vht_mcs_map,
5035 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
5036 {
5037 	u8 nss;
5038 
5039 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
5040 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
5041 		vht_mcs_map >>= 2;
5042 	}
5043 }
5044 
5045 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
5046 			     struct nl80211_txrate_vht *txrate,
5047 			     u16 mcs[NL80211_VHT_NSS_MAX])
5048 {
5049 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5050 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
5051 	u8 i;
5052 
5053 	if (!sband->vht_cap.vht_supported)
5054 		return false;
5055 
5056 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
5057 
5058 	/* Build vht_mcs_mask from VHT capabilities */
5059 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5060 
5061 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5062 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5063 			mcs[i] = txrate->mcs[i];
5064 		else
5065 			return false;
5066 	}
5067 
5068 	return true;
5069 }
5070 
5071 static u16 he_mcs_map_to_mcs_mask(u8 he_mcs_map)
5072 {
5073 	switch (he_mcs_map) {
5074 	case IEEE80211_HE_MCS_NOT_SUPPORTED:
5075 		return 0;
5076 	case IEEE80211_HE_MCS_SUPPORT_0_7:
5077 		return 0x00FF;
5078 	case IEEE80211_HE_MCS_SUPPORT_0_9:
5079 		return 0x03FF;
5080 	case IEEE80211_HE_MCS_SUPPORT_0_11:
5081 		return 0xFFF;
5082 	default:
5083 		break;
5084 	}
5085 	return 0;
5086 }
5087 
5088 static void he_build_mcs_mask(u16 he_mcs_map,
5089 			      u16 he_mcs_mask[NL80211_HE_NSS_MAX])
5090 {
5091 	u8 nss;
5092 
5093 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
5094 		he_mcs_mask[nss] = he_mcs_map_to_mcs_mask(he_mcs_map & 0x03);
5095 		he_mcs_map >>= 2;
5096 	}
5097 }
5098 
5099 static u16 he_get_txmcsmap(struct genl_info *info, unsigned int link_id,
5100 			   const struct ieee80211_sta_he_cap *he_cap)
5101 {
5102 	struct net_device *dev = info->user_ptr[1];
5103 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5104 	struct cfg80211_chan_def *chandef;
5105 	__le16 tx_mcs;
5106 
5107 	chandef = wdev_chandef(wdev, link_id);
5108 	if (!chandef) {
5109 		/*
5110 		 * This is probably broken, but we never maintained
5111 		 * a chandef in these cases, so it always was.
5112 		 */
5113 		return le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
5114 	}
5115 
5116 	switch (chandef->width) {
5117 	case NL80211_CHAN_WIDTH_80P80:
5118 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80p80;
5119 		break;
5120 	case NL80211_CHAN_WIDTH_160:
5121 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_160;
5122 		break;
5123 	default:
5124 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80;
5125 		break;
5126 	}
5127 
5128 	return le16_to_cpu(tx_mcs);
5129 }
5130 
5131 static bool he_set_mcs_mask(struct genl_info *info,
5132 			    struct wireless_dev *wdev,
5133 			    struct ieee80211_supported_band *sband,
5134 			    struct nl80211_txrate_he *txrate,
5135 			    u16 mcs[NL80211_HE_NSS_MAX],
5136 			    unsigned int link_id)
5137 {
5138 	const struct ieee80211_sta_he_cap *he_cap;
5139 	u16 tx_mcs_mask[NL80211_HE_NSS_MAX] = {};
5140 	u16 tx_mcs_map = 0;
5141 	u8 i;
5142 
5143 	he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5144 	if (!he_cap)
5145 		return false;
5146 
5147 	memset(mcs, 0, sizeof(u16) * NL80211_HE_NSS_MAX);
5148 
5149 	tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5150 
5151 	/* Build he_mcs_mask from HE capabilities */
5152 	he_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5153 
5154 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5155 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5156 			mcs[i] = txrate->mcs[i];
5157 		else
5158 			return false;
5159 	}
5160 
5161 	return true;
5162 }
5163 
5164 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
5165 					 struct nlattr *attrs[],
5166 					 enum nl80211_attrs attr,
5167 					 struct cfg80211_bitrate_mask *mask,
5168 					 struct net_device *dev,
5169 					 bool default_all_enabled,
5170 					 unsigned int link_id)
5171 {
5172 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5173 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5174 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5175 	int rem, i;
5176 	struct nlattr *tx_rates;
5177 	struct ieee80211_supported_band *sband;
5178 	u16 vht_tx_mcs_map, he_tx_mcs_map;
5179 
5180 	memset(mask, 0, sizeof(*mask));
5181 	/* Default to all rates enabled */
5182 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
5183 		const struct ieee80211_sta_he_cap *he_cap;
5184 
5185 		if (!default_all_enabled)
5186 			break;
5187 
5188 		sband = rdev->wiphy.bands[i];
5189 
5190 		if (!sband)
5191 			continue;
5192 
5193 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
5194 		memcpy(mask->control[i].ht_mcs,
5195 		       sband->ht_cap.mcs.rx_mask,
5196 		       sizeof(mask->control[i].ht_mcs));
5197 
5198 		if (sband->vht_cap.vht_supported) {
5199 			vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5200 			vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
5201 		}
5202 
5203 		he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5204 		if (!he_cap)
5205 			continue;
5206 
5207 		he_tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5208 		he_build_mcs_mask(he_tx_mcs_map, mask->control[i].he_mcs);
5209 
5210 		mask->control[i].he_gi = 0xFF;
5211 		mask->control[i].he_ltf = 0xFF;
5212 	}
5213 
5214 	/* if no rates are given set it back to the defaults */
5215 	if (!attrs[attr])
5216 		goto out;
5217 
5218 	/* The nested attribute uses enum nl80211_band as the index. This maps
5219 	 * directly to the enum nl80211_band values used in cfg80211.
5220 	 */
5221 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
5222 	nla_for_each_nested(tx_rates, attrs[attr], rem) {
5223 		enum nl80211_band band = nla_type(tx_rates);
5224 		int err;
5225 
5226 		if (band < 0 || band >= NUM_NL80211_BANDS)
5227 			return -EINVAL;
5228 		sband = rdev->wiphy.bands[band];
5229 		if (sband == NULL)
5230 			return -EINVAL;
5231 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
5232 						  tx_rates,
5233 						  nl80211_txattr_policy,
5234 						  info->extack);
5235 		if (err)
5236 			return err;
5237 		if (tb[NL80211_TXRATE_LEGACY]) {
5238 			mask->control[band].legacy = rateset_to_mask(
5239 				sband,
5240 				nla_data(tb[NL80211_TXRATE_LEGACY]),
5241 				nla_len(tb[NL80211_TXRATE_LEGACY]));
5242 			if ((mask->control[band].legacy == 0) &&
5243 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
5244 				return -EINVAL;
5245 		}
5246 		if (tb[NL80211_TXRATE_HT]) {
5247 			if (!ht_rateset_to_mask(
5248 					sband,
5249 					nla_data(tb[NL80211_TXRATE_HT]),
5250 					nla_len(tb[NL80211_TXRATE_HT]),
5251 					mask->control[band].ht_mcs))
5252 				return -EINVAL;
5253 		}
5254 
5255 		if (tb[NL80211_TXRATE_VHT]) {
5256 			if (!vht_set_mcs_mask(
5257 					sband,
5258 					nla_data(tb[NL80211_TXRATE_VHT]),
5259 					mask->control[band].vht_mcs))
5260 				return -EINVAL;
5261 		}
5262 
5263 		if (tb[NL80211_TXRATE_GI]) {
5264 			mask->control[band].gi =
5265 				nla_get_u8(tb[NL80211_TXRATE_GI]);
5266 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
5267 				return -EINVAL;
5268 		}
5269 		if (tb[NL80211_TXRATE_HE] &&
5270 		    !he_set_mcs_mask(info, wdev, sband,
5271 				     nla_data(tb[NL80211_TXRATE_HE]),
5272 				     mask->control[band].he_mcs,
5273 				     link_id))
5274 			return -EINVAL;
5275 
5276 		if (tb[NL80211_TXRATE_HE_GI])
5277 			mask->control[band].he_gi =
5278 				nla_get_u8(tb[NL80211_TXRATE_HE_GI]);
5279 		if (tb[NL80211_TXRATE_HE_LTF])
5280 			mask->control[band].he_ltf =
5281 				nla_get_u8(tb[NL80211_TXRATE_HE_LTF]);
5282 
5283 		if (mask->control[band].legacy == 0) {
5284 			/* don't allow empty legacy rates if HT, VHT or HE
5285 			 * are not even supported.
5286 			 */
5287 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
5288 			      rdev->wiphy.bands[band]->vht_cap.vht_supported ||
5289 			      ieee80211_get_he_iftype_cap(sband, wdev->iftype)))
5290 				return -EINVAL;
5291 
5292 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5293 				if (mask->control[band].ht_mcs[i])
5294 					goto out;
5295 
5296 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5297 				if (mask->control[band].vht_mcs[i])
5298 					goto out;
5299 
5300 			for (i = 0; i < NL80211_HE_NSS_MAX; i++)
5301 				if (mask->control[band].he_mcs[i])
5302 					goto out;
5303 
5304 			/* legacy and mcs rates may not be both empty */
5305 			return -EINVAL;
5306 		}
5307 	}
5308 
5309 out:
5310 	return 0;
5311 }
5312 
5313 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
5314 				   enum nl80211_band band,
5315 				   struct cfg80211_bitrate_mask *beacon_rate)
5316 {
5317 	u32 count_ht, count_vht, count_he, i;
5318 	u32 rate = beacon_rate->control[band].legacy;
5319 
5320 	/* Allow only one rate */
5321 	if (hweight32(rate) > 1)
5322 		return -EINVAL;
5323 
5324 	count_ht = 0;
5325 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5326 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
5327 			return -EINVAL;
5328 		} else if (beacon_rate->control[band].ht_mcs[i]) {
5329 			count_ht++;
5330 			if (count_ht > 1)
5331 				return -EINVAL;
5332 		}
5333 		if (count_ht && rate)
5334 			return -EINVAL;
5335 	}
5336 
5337 	count_vht = 0;
5338 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5339 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
5340 			return -EINVAL;
5341 		} else if (beacon_rate->control[band].vht_mcs[i]) {
5342 			count_vht++;
5343 			if (count_vht > 1)
5344 				return -EINVAL;
5345 		}
5346 		if (count_vht && rate)
5347 			return -EINVAL;
5348 	}
5349 
5350 	count_he = 0;
5351 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5352 		if (hweight16(beacon_rate->control[band].he_mcs[i]) > 1) {
5353 			return -EINVAL;
5354 		} else if (beacon_rate->control[band].he_mcs[i]) {
5355 			count_he++;
5356 			if (count_he > 1)
5357 				return -EINVAL;
5358 		}
5359 		if (count_he && rate)
5360 			return -EINVAL;
5361 	}
5362 
5363 	if ((count_ht && count_vht && count_he) ||
5364 	    (!rate && !count_ht && !count_vht && !count_he))
5365 		return -EINVAL;
5366 
5367 	if (rate &&
5368 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5369 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
5370 		return -EINVAL;
5371 	if (count_ht &&
5372 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5373 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
5374 		return -EINVAL;
5375 	if (count_vht &&
5376 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5377 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
5378 		return -EINVAL;
5379 	if (count_he &&
5380 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5381 				     NL80211_EXT_FEATURE_BEACON_RATE_HE))
5382 		return -EINVAL;
5383 
5384 	return 0;
5385 }
5386 
5387 static int nl80211_parse_mbssid_config(struct wiphy *wiphy,
5388 				       struct net_device *dev,
5389 				       struct nlattr *attrs,
5390 				       struct cfg80211_mbssid_config *config,
5391 				       u8 num_elems)
5392 {
5393 	struct nlattr *tb[NL80211_MBSSID_CONFIG_ATTR_MAX + 1];
5394 
5395 	if (!wiphy->mbssid_max_interfaces)
5396 		return -EOPNOTSUPP;
5397 
5398 	if (nla_parse_nested(tb, NL80211_MBSSID_CONFIG_ATTR_MAX, attrs, NULL,
5399 			     NULL) ||
5400 	    !tb[NL80211_MBSSID_CONFIG_ATTR_INDEX])
5401 		return -EINVAL;
5402 
5403 	config->ema = nla_get_flag(tb[NL80211_MBSSID_CONFIG_ATTR_EMA]);
5404 	if (config->ema) {
5405 		if (!wiphy->ema_max_profile_periodicity)
5406 			return -EOPNOTSUPP;
5407 
5408 		if (num_elems > wiphy->ema_max_profile_periodicity)
5409 			return -EINVAL;
5410 	}
5411 
5412 	config->index = nla_get_u8(tb[NL80211_MBSSID_CONFIG_ATTR_INDEX]);
5413 	if (config->index >= wiphy->mbssid_max_interfaces ||
5414 	    (!config->index && !num_elems))
5415 		return -EINVAL;
5416 
5417 	if (tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]) {
5418 		u32 tx_ifindex =
5419 			nla_get_u32(tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]);
5420 
5421 		if ((!config->index && tx_ifindex != dev->ifindex) ||
5422 		    (config->index && tx_ifindex == dev->ifindex))
5423 			return -EINVAL;
5424 
5425 		if (tx_ifindex != dev->ifindex) {
5426 			struct net_device *tx_netdev =
5427 				dev_get_by_index(wiphy_net(wiphy), tx_ifindex);
5428 
5429 			if (!tx_netdev || !tx_netdev->ieee80211_ptr ||
5430 			    tx_netdev->ieee80211_ptr->wiphy != wiphy ||
5431 			    tx_netdev->ieee80211_ptr->iftype !=
5432 							NL80211_IFTYPE_AP) {
5433 				dev_put(tx_netdev);
5434 				return -EINVAL;
5435 			}
5436 
5437 			config->tx_wdev = tx_netdev->ieee80211_ptr;
5438 		} else {
5439 			config->tx_wdev = dev->ieee80211_ptr;
5440 		}
5441 	} else if (!config->index) {
5442 		config->tx_wdev = dev->ieee80211_ptr;
5443 	} else {
5444 		return -EINVAL;
5445 	}
5446 
5447 	return 0;
5448 }
5449 
5450 static struct cfg80211_mbssid_elems *
5451 nl80211_parse_mbssid_elems(struct wiphy *wiphy, struct nlattr *attrs)
5452 {
5453 	struct nlattr *nl_elems;
5454 	struct cfg80211_mbssid_elems *elems;
5455 	int rem_elems;
5456 	u8 i = 0, num_elems = 0;
5457 
5458 	if (!wiphy->mbssid_max_interfaces)
5459 		return ERR_PTR(-EINVAL);
5460 
5461 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5462 		if (num_elems >= 255)
5463 			return ERR_PTR(-EINVAL);
5464 		num_elems++;
5465 	}
5466 
5467 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5468 	if (!elems)
5469 		return ERR_PTR(-ENOMEM);
5470 	elems->cnt = num_elems;
5471 
5472 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5473 		elems->elem[i].data = nla_data(nl_elems);
5474 		elems->elem[i].len = nla_len(nl_elems);
5475 		i++;
5476 	}
5477 	return elems;
5478 }
5479 
5480 static struct cfg80211_rnr_elems *
5481 nl80211_parse_rnr_elems(struct wiphy *wiphy, struct nlattr *attrs,
5482 			struct netlink_ext_ack *extack)
5483 {
5484 	struct nlattr *nl_elems;
5485 	struct cfg80211_rnr_elems *elems;
5486 	int rem_elems;
5487 	u8 i = 0, num_elems = 0;
5488 
5489 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5490 		int ret;
5491 
5492 		ret = validate_ie_attr(nl_elems, extack);
5493 		if (ret)
5494 			return ERR_PTR(ret);
5495 
5496 		num_elems++;
5497 	}
5498 
5499 	elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5500 	if (!elems)
5501 		return ERR_PTR(-ENOMEM);
5502 	elems->cnt = num_elems;
5503 
5504 	nla_for_each_nested(nl_elems, attrs, rem_elems) {
5505 		elems->elem[i].data = nla_data(nl_elems);
5506 		elems->elem[i].len = nla_len(nl_elems);
5507 		i++;
5508 	}
5509 	return elems;
5510 }
5511 
5512 static int nl80211_parse_he_bss_color(struct nlattr *attrs,
5513 				      struct cfg80211_he_bss_color *he_bss_color)
5514 {
5515 	struct nlattr *tb[NL80211_HE_BSS_COLOR_ATTR_MAX + 1];
5516 	int err;
5517 
5518 	err = nla_parse_nested(tb, NL80211_HE_BSS_COLOR_ATTR_MAX, attrs,
5519 			       he_bss_color_policy, NULL);
5520 	if (err)
5521 		return err;
5522 
5523 	if (!tb[NL80211_HE_BSS_COLOR_ATTR_COLOR])
5524 		return -EINVAL;
5525 
5526 	he_bss_color->color =
5527 		nla_get_u8(tb[NL80211_HE_BSS_COLOR_ATTR_COLOR]);
5528 	he_bss_color->enabled =
5529 		!nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_DISABLED]);
5530 	he_bss_color->partial =
5531 		nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_PARTIAL]);
5532 
5533 	return 0;
5534 }
5535 
5536 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
5537 				struct nlattr *attrs[],
5538 				struct cfg80211_beacon_data *bcn,
5539 				struct netlink_ext_ack *extack)
5540 {
5541 	bool haveinfo = false;
5542 	int err;
5543 
5544 	memset(bcn, 0, sizeof(*bcn));
5545 
5546 	bcn->link_id = nl80211_link_id(attrs);
5547 
5548 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
5549 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
5550 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
5551 		if (!bcn->head_len)
5552 			return -EINVAL;
5553 		haveinfo = true;
5554 	}
5555 
5556 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
5557 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
5558 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
5559 		haveinfo = true;
5560 	}
5561 
5562 	if (!haveinfo)
5563 		return -EINVAL;
5564 
5565 	if (attrs[NL80211_ATTR_IE]) {
5566 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
5567 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
5568 	}
5569 
5570 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
5571 		bcn->proberesp_ies =
5572 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5573 		bcn->proberesp_ies_len =
5574 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5575 	}
5576 
5577 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
5578 		bcn->assocresp_ies =
5579 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5580 		bcn->assocresp_ies_len =
5581 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5582 	}
5583 
5584 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
5585 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
5586 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
5587 	}
5588 
5589 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
5590 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
5591 
5592 		err = nla_parse_nested_deprecated(tb,
5593 						  NL80211_FTM_RESP_ATTR_MAX,
5594 						  attrs[NL80211_ATTR_FTM_RESPONDER],
5595 						  NULL, NULL);
5596 		if (err)
5597 			return err;
5598 
5599 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
5600 		    wiphy_ext_feature_isset(&rdev->wiphy,
5601 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
5602 			bcn->ftm_responder = 1;
5603 		else
5604 			return -EOPNOTSUPP;
5605 
5606 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
5607 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
5608 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
5609 		}
5610 
5611 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
5612 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5613 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5614 		}
5615 	} else {
5616 		bcn->ftm_responder = -1;
5617 	}
5618 
5619 	if (attrs[NL80211_ATTR_HE_BSS_COLOR]) {
5620 		err = nl80211_parse_he_bss_color(attrs[NL80211_ATTR_HE_BSS_COLOR],
5621 						 &bcn->he_bss_color);
5622 		if (err)
5623 			return err;
5624 		bcn->he_bss_color_valid = true;
5625 	}
5626 
5627 	if (attrs[NL80211_ATTR_MBSSID_ELEMS]) {
5628 		struct cfg80211_mbssid_elems *mbssid =
5629 			nl80211_parse_mbssid_elems(&rdev->wiphy,
5630 						   attrs[NL80211_ATTR_MBSSID_ELEMS]);
5631 
5632 		if (IS_ERR(mbssid))
5633 			return PTR_ERR(mbssid);
5634 
5635 		bcn->mbssid_ies = mbssid;
5636 
5637 		if (bcn->mbssid_ies && attrs[NL80211_ATTR_EMA_RNR_ELEMS]) {
5638 			struct cfg80211_rnr_elems *rnr =
5639 				nl80211_parse_rnr_elems(&rdev->wiphy,
5640 							attrs[NL80211_ATTR_EMA_RNR_ELEMS],
5641 							extack);
5642 
5643 			if (IS_ERR(rnr))
5644 				return PTR_ERR(rnr);
5645 
5646 			if (rnr && rnr->cnt < bcn->mbssid_ies->cnt)
5647 				return -EINVAL;
5648 
5649 			bcn->rnr_ies = rnr;
5650 		}
5651 	}
5652 
5653 	return 0;
5654 }
5655 
5656 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
5657 				    struct ieee80211_he_obss_pd *he_obss_pd)
5658 {
5659 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
5660 	int err;
5661 
5662 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
5663 			       he_obss_pd_policy, NULL);
5664 	if (err)
5665 		return err;
5666 
5667 	if (!tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL])
5668 		return -EINVAL;
5669 
5670 	he_obss_pd->sr_ctrl = nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL]);
5671 
5672 	if (tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET])
5673 		he_obss_pd->min_offset =
5674 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
5675 	if (tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
5676 		he_obss_pd->max_offset =
5677 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
5678 	if (tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET])
5679 		he_obss_pd->non_srg_max_offset =
5680 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET]);
5681 
5682 	if (he_obss_pd->min_offset > he_obss_pd->max_offset)
5683 		return -EINVAL;
5684 
5685 	if (tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP])
5686 		memcpy(he_obss_pd->bss_color_bitmap,
5687 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP]),
5688 		       sizeof(he_obss_pd->bss_color_bitmap));
5689 
5690 	if (tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP])
5691 		memcpy(he_obss_pd->partial_bssid_bitmap,
5692 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP]),
5693 		       sizeof(he_obss_pd->partial_bssid_bitmap));
5694 
5695 	he_obss_pd->enable = true;
5696 
5697 	return 0;
5698 }
5699 
5700 static int nl80211_parse_fils_discovery(struct cfg80211_registered_device *rdev,
5701 					struct nlattr *attrs,
5702 					struct cfg80211_ap_settings *params)
5703 {
5704 	struct nlattr *tb[NL80211_FILS_DISCOVERY_ATTR_MAX + 1];
5705 	int ret;
5706 	struct cfg80211_fils_discovery *fd = &params->fils_discovery;
5707 
5708 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5709 				     NL80211_EXT_FEATURE_FILS_DISCOVERY))
5710 		return -EINVAL;
5711 
5712 	ret = nla_parse_nested(tb, NL80211_FILS_DISCOVERY_ATTR_MAX, attrs,
5713 			       NULL, NULL);
5714 	if (ret)
5715 		return ret;
5716 
5717 	if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] ||
5718 	    !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] ||
5719 	    !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL])
5720 		return -EINVAL;
5721 
5722 	fd->tmpl_len = nla_len(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5723 	fd->tmpl = nla_data(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5724 	fd->min_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN]);
5725 	fd->max_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX]);
5726 
5727 	return 0;
5728 }
5729 
5730 static int
5731 nl80211_parse_unsol_bcast_probe_resp(struct cfg80211_registered_device *rdev,
5732 				     struct nlattr *attrs,
5733 				     struct cfg80211_ap_settings *params)
5734 {
5735 	struct nlattr *tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1];
5736 	int ret;
5737 	struct cfg80211_unsol_bcast_probe_resp *presp =
5738 					&params->unsol_bcast_probe_resp;
5739 
5740 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5741 				     NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP))
5742 		return -EINVAL;
5743 
5744 	ret = nla_parse_nested(tb, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX,
5745 			       attrs, NULL, NULL);
5746 	if (ret)
5747 		return ret;
5748 
5749 	if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] ||
5750 	    !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL])
5751 		return -EINVAL;
5752 
5753 	presp->tmpl = nla_data(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5754 	presp->tmpl_len = nla_len(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5755 	presp->interval = nla_get_u32(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT]);
5756 	return 0;
5757 }
5758 
5759 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
5760 					    const struct element *rates)
5761 {
5762 	int i;
5763 
5764 	if (!rates)
5765 		return;
5766 
5767 	for (i = 0; i < rates->datalen; i++) {
5768 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
5769 			params->ht_required = true;
5770 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
5771 			params->vht_required = true;
5772 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HE_PHY)
5773 			params->he_required = true;
5774 		if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_SAE_H2E)
5775 			params->sae_h2e_required = true;
5776 	}
5777 }
5778 
5779 /*
5780  * Since the nl80211 API didn't include, from the beginning, attributes about
5781  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
5782  * benefit of drivers that rebuild IEs in the firmware.
5783  */
5784 static int nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
5785 {
5786 	const struct cfg80211_beacon_data *bcn = &params->beacon;
5787 	size_t ies_len = bcn->tail_len;
5788 	const u8 *ies = bcn->tail;
5789 	const struct element *rates;
5790 	const struct element *cap;
5791 
5792 	rates = cfg80211_find_elem(WLAN_EID_SUPP_RATES, ies, ies_len);
5793 	nl80211_check_ap_rate_selectors(params, rates);
5794 
5795 	rates = cfg80211_find_elem(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
5796 	nl80211_check_ap_rate_selectors(params, rates);
5797 
5798 	cap = cfg80211_find_elem(WLAN_EID_HT_CAPABILITY, ies, ies_len);
5799 	if (cap && cap->datalen >= sizeof(*params->ht_cap))
5800 		params->ht_cap = (void *)cap->data;
5801 	cap = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
5802 	if (cap && cap->datalen >= sizeof(*params->vht_cap))
5803 		params->vht_cap = (void *)cap->data;
5804 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
5805 	if (cap && cap->datalen >= sizeof(*params->he_cap) + 1)
5806 		params->he_cap = (void *)(cap->data + 1);
5807 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ies, ies_len);
5808 	if (cap && cap->datalen >= sizeof(*params->he_oper) + 1)
5809 		params->he_oper = (void *)(cap->data + 1);
5810 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_CAPABILITY, ies, ies_len);
5811 	if (cap) {
5812 		if (!cap->datalen)
5813 			return -EINVAL;
5814 		params->eht_cap = (void *)(cap->data + 1);
5815 		if (!ieee80211_eht_capa_size_ok((const u8 *)params->he_cap,
5816 						(const u8 *)params->eht_cap,
5817 						cap->datalen - 1, true))
5818 			return -EINVAL;
5819 	}
5820 	cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_OPERATION, ies, ies_len);
5821 	if (cap) {
5822 		if (!cap->datalen)
5823 			return -EINVAL;
5824 		params->eht_oper = (void *)(cap->data + 1);
5825 		if (!ieee80211_eht_oper_size_ok((const u8 *)params->eht_oper,
5826 						cap->datalen - 1))
5827 			return -EINVAL;
5828 	}
5829 	return 0;
5830 }
5831 
5832 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
5833 				   struct cfg80211_ap_settings *params)
5834 {
5835 	struct wireless_dev *wdev;
5836 
5837 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
5838 		if (wdev->iftype != NL80211_IFTYPE_AP &&
5839 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
5840 			continue;
5841 
5842 		if (!wdev->u.ap.preset_chandef.chan)
5843 			continue;
5844 
5845 		params->chandef = wdev->u.ap.preset_chandef;
5846 		return true;
5847 	}
5848 
5849 	return false;
5850 }
5851 
5852 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
5853 				    enum nl80211_auth_type auth_type,
5854 				    enum nl80211_commands cmd)
5855 {
5856 	if (auth_type > NL80211_AUTHTYPE_MAX)
5857 		return false;
5858 
5859 	switch (cmd) {
5860 	case NL80211_CMD_AUTHENTICATE:
5861 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5862 		    auth_type == NL80211_AUTHTYPE_SAE)
5863 			return false;
5864 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5865 					     NL80211_EXT_FEATURE_FILS_STA) &&
5866 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5867 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5868 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
5869 			return false;
5870 		return true;
5871 	case NL80211_CMD_CONNECT:
5872 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5873 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5874 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
5875 		    auth_type == NL80211_AUTHTYPE_SAE)
5876 			return false;
5877 
5878 		/* FILS with SK PFS or PK not supported yet */
5879 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5880 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5881 			return false;
5882 		if (!wiphy_ext_feature_isset(
5883 			    &rdev->wiphy,
5884 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
5885 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
5886 			return false;
5887 		return true;
5888 	case NL80211_CMD_START_AP:
5889 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5890 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP) &&
5891 		    auth_type == NL80211_AUTHTYPE_SAE)
5892 			return false;
5893 		/* FILS not supported yet */
5894 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5895 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5896 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5897 			return false;
5898 		return true;
5899 	default:
5900 		return false;
5901 	}
5902 }
5903 
5904 static void nl80211_send_ap_started(struct wireless_dev *wdev,
5905 				    unsigned int link_id)
5906 {
5907 	struct wiphy *wiphy = wdev->wiphy;
5908 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
5909 	struct sk_buff *msg;
5910 	void *hdr;
5911 
5912 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5913 	if (!msg)
5914 		return;
5915 
5916 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_START_AP);
5917 	if (!hdr)
5918 		goto out;
5919 
5920 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
5921 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
5922 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
5923 			      NL80211_ATTR_PAD) ||
5924 	    (wdev->u.ap.ssid_len &&
5925 	     nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
5926 		     wdev->u.ap.ssid)) ||
5927 	    (wdev->valid_links &&
5928 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)))
5929 		goto out;
5930 
5931 	genlmsg_end(msg, hdr);
5932 
5933 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
5934 				NL80211_MCGRP_MLME, GFP_KERNEL);
5935 	return;
5936 out:
5937 	nlmsg_free(msg);
5938 }
5939 
5940 static int nl80211_validate_ap_phy_operation(struct cfg80211_ap_settings *params)
5941 {
5942 	struct ieee80211_channel *channel = params->chandef.chan;
5943 
5944 	if ((params->he_cap ||  params->he_oper) &&
5945 	    (channel->flags & IEEE80211_CHAN_NO_HE))
5946 		return -EOPNOTSUPP;
5947 
5948 	if ((params->eht_cap || params->eht_oper) &&
5949 	    (channel->flags & IEEE80211_CHAN_NO_EHT))
5950 		return -EOPNOTSUPP;
5951 
5952 	return 0;
5953 }
5954 
5955 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
5956 {
5957 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5958 	unsigned int link_id = nl80211_link_id(info->attrs);
5959 	struct net_device *dev = info->user_ptr[1];
5960 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5961 	struct cfg80211_ap_settings *params;
5962 	int err;
5963 
5964 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5965 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5966 		return -EOPNOTSUPP;
5967 
5968 	if (!rdev->ops->start_ap)
5969 		return -EOPNOTSUPP;
5970 
5971 	if (wdev->links[link_id].ap.beacon_interval)
5972 		return -EALREADY;
5973 
5974 	/* these are required for START_AP */
5975 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
5976 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
5977 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
5978 		return -EINVAL;
5979 
5980 	params = kzalloc(sizeof(*params), GFP_KERNEL);
5981 	if (!params)
5982 		return -ENOMEM;
5983 
5984 	err = nl80211_parse_beacon(rdev, info->attrs, &params->beacon,
5985 				   info->extack);
5986 	if (err)
5987 		goto out;
5988 
5989 	params->beacon_interval =
5990 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5991 	params->dtim_period =
5992 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
5993 
5994 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
5995 					   params->beacon_interval);
5996 	if (err)
5997 		goto out;
5998 
5999 	/*
6000 	 * In theory, some of these attributes should be required here
6001 	 * but since they were not used when the command was originally
6002 	 * added, keep them optional for old user space programs to let
6003 	 * them continue to work with drivers that do not need the
6004 	 * additional information -- drivers must check!
6005 	 */
6006 	if (info->attrs[NL80211_ATTR_SSID]) {
6007 		params->ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6008 		params->ssid_len =
6009 			nla_len(info->attrs[NL80211_ATTR_SSID]);
6010 		if (params->ssid_len == 0) {
6011 			err = -EINVAL;
6012 			goto out;
6013 		}
6014 
6015 		if (wdev->u.ap.ssid_len &&
6016 		    (wdev->u.ap.ssid_len != params->ssid_len ||
6017 		     memcmp(wdev->u.ap.ssid, params->ssid, params->ssid_len))) {
6018 			/* require identical SSID for MLO */
6019 			err = -EINVAL;
6020 			goto out;
6021 		}
6022 	} else if (wdev->valid_links) {
6023 		/* require SSID for MLO */
6024 		err = -EINVAL;
6025 		goto out;
6026 	}
6027 
6028 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
6029 		params->hidden_ssid = nla_get_u32(
6030 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
6031 
6032 	params->privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
6033 
6034 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6035 		params->auth_type = nla_get_u32(
6036 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
6037 		if (!nl80211_valid_auth_type(rdev, params->auth_type,
6038 					     NL80211_CMD_START_AP)) {
6039 			err = -EINVAL;
6040 			goto out;
6041 		}
6042 	} else
6043 		params->auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6044 
6045 	err = nl80211_crypto_settings(rdev, info, &params->crypto,
6046 				      NL80211_MAX_NR_CIPHER_SUITES);
6047 	if (err)
6048 		goto out;
6049 
6050 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
6051 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER)) {
6052 			err = -EOPNOTSUPP;
6053 			goto out;
6054 		}
6055 		params->inactivity_timeout = nla_get_u16(
6056 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
6057 	}
6058 
6059 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
6060 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6061 			err = -EINVAL;
6062 			goto out;
6063 		}
6064 		params->p2p_ctwindow =
6065 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
6066 		if (params->p2p_ctwindow != 0 &&
6067 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)) {
6068 			err = -EINVAL;
6069 			goto out;
6070 		}
6071 	}
6072 
6073 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6074 		u8 tmp;
6075 
6076 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6077 			err = -EINVAL;
6078 			goto out;
6079 		}
6080 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6081 		params->p2p_opp_ps = tmp;
6082 		if (params->p2p_opp_ps != 0 &&
6083 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)) {
6084 			err = -EINVAL;
6085 			goto out;
6086 		}
6087 	}
6088 
6089 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6090 		err = nl80211_parse_chandef(rdev, info, &params->chandef);
6091 		if (err)
6092 			goto out;
6093 	} else if (wdev->valid_links) {
6094 		/* with MLD need to specify the channel configuration */
6095 		err = -EINVAL;
6096 		goto out;
6097 	} else if (wdev->u.ap.preset_chandef.chan) {
6098 		params->chandef = wdev->u.ap.preset_chandef;
6099 	} else if (!nl80211_get_ap_channel(rdev, params)) {
6100 		err = -EINVAL;
6101 		goto out;
6102 	}
6103 
6104 	if (info->attrs[NL80211_ATTR_PUNCT_BITMAP]) {
6105 		err = nl80211_parse_punct_bitmap(rdev, info,
6106 						 &params->chandef,
6107 						 &params->punct_bitmap);
6108 		if (err)
6109 			goto out;
6110 	}
6111 
6112 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params->chandef,
6113 					   wdev->iftype)) {
6114 		err = -EINVAL;
6115 		goto out;
6116 	}
6117 
6118 	wdev_lock(wdev);
6119 
6120 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
6121 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
6122 						    NL80211_ATTR_TX_RATES,
6123 						    &params->beacon_rate,
6124 						    dev, false, link_id);
6125 		if (err)
6126 			goto out_unlock;
6127 
6128 		err = validate_beacon_tx_rate(rdev, params->chandef.chan->band,
6129 					      &params->beacon_rate);
6130 		if (err)
6131 			goto out_unlock;
6132 	}
6133 
6134 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
6135 		params->smps_mode =
6136 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
6137 		switch (params->smps_mode) {
6138 		case NL80211_SMPS_OFF:
6139 			break;
6140 		case NL80211_SMPS_STATIC:
6141 			if (!(rdev->wiphy.features &
6142 			      NL80211_FEATURE_STATIC_SMPS)) {
6143 				err = -EINVAL;
6144 				goto out_unlock;
6145 			}
6146 			break;
6147 		case NL80211_SMPS_DYNAMIC:
6148 			if (!(rdev->wiphy.features &
6149 			      NL80211_FEATURE_DYNAMIC_SMPS)) {
6150 				err = -EINVAL;
6151 				goto out_unlock;
6152 			}
6153 			break;
6154 		default:
6155 			err = -EINVAL;
6156 			goto out_unlock;
6157 		}
6158 	} else {
6159 		params->smps_mode = NL80211_SMPS_OFF;
6160 	}
6161 
6162 	params->pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
6163 	if (params->pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
6164 		err = -EOPNOTSUPP;
6165 		goto out_unlock;
6166 	}
6167 
6168 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
6169 		params->acl = parse_acl_data(&rdev->wiphy, info);
6170 		if (IS_ERR(params->acl)) {
6171 			err = PTR_ERR(params->acl);
6172 			params->acl = NULL;
6173 			goto out_unlock;
6174 		}
6175 	}
6176 
6177 	params->twt_responder =
6178 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
6179 
6180 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
6181 		err = nl80211_parse_he_obss_pd(
6182 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
6183 					&params->he_obss_pd);
6184 		if (err)
6185 			goto out_unlock;
6186 	}
6187 
6188 	if (info->attrs[NL80211_ATTR_FILS_DISCOVERY]) {
6189 		err = nl80211_parse_fils_discovery(rdev,
6190 						   info->attrs[NL80211_ATTR_FILS_DISCOVERY],
6191 						   params);
6192 		if (err)
6193 			goto out_unlock;
6194 	}
6195 
6196 	if (info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP]) {
6197 		err = nl80211_parse_unsol_bcast_probe_resp(
6198 			rdev, info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP],
6199 			params);
6200 		if (err)
6201 			goto out_unlock;
6202 	}
6203 
6204 	if (info->attrs[NL80211_ATTR_MBSSID_CONFIG]) {
6205 		err = nl80211_parse_mbssid_config(&rdev->wiphy, dev,
6206 						  info->attrs[NL80211_ATTR_MBSSID_CONFIG],
6207 						  &params->mbssid_config,
6208 						  params->beacon.mbssid_ies ?
6209 							params->beacon.mbssid_ies->cnt :
6210 							0);
6211 		if (err)
6212 			goto out_unlock;
6213 	}
6214 
6215 	if (!params->mbssid_config.ema && params->beacon.rnr_ies) {
6216 		err = -EINVAL;
6217 		goto out_unlock;
6218 	}
6219 
6220 	err = nl80211_calculate_ap_params(params);
6221 	if (err)
6222 		goto out_unlock;
6223 
6224 	err = nl80211_validate_ap_phy_operation(params);
6225 	if (err)
6226 		goto out_unlock;
6227 
6228 	if (info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS])
6229 		params->flags = nla_get_u32(
6230 			info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS]);
6231 	else if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
6232 		params->flags |= NL80211_AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
6233 
6234 	if (wdev->conn_owner_nlportid &&
6235 	    info->attrs[NL80211_ATTR_SOCKET_OWNER] &&
6236 	    wdev->conn_owner_nlportid != info->snd_portid) {
6237 		err = -EINVAL;
6238 		goto out_unlock;
6239 	}
6240 
6241 	/* FIXME: validate MLO/link-id against driver capabilities */
6242 
6243 	err = rdev_start_ap(rdev, dev, params);
6244 	if (!err) {
6245 		wdev->links[link_id].ap.beacon_interval = params->beacon_interval;
6246 		wdev->links[link_id].ap.chandef = params->chandef;
6247 		wdev->u.ap.ssid_len = params->ssid_len;
6248 		memcpy(wdev->u.ap.ssid, params->ssid,
6249 		       params->ssid_len);
6250 
6251 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
6252 			wdev->conn_owner_nlportid = info->snd_portid;
6253 
6254 		nl80211_send_ap_started(wdev, link_id);
6255 	}
6256 out_unlock:
6257 	wdev_unlock(wdev);
6258 out:
6259 	kfree(params->acl);
6260 	kfree(params->beacon.mbssid_ies);
6261 	if (params->mbssid_config.tx_wdev &&
6262 	    params->mbssid_config.tx_wdev->netdev &&
6263 	    params->mbssid_config.tx_wdev->netdev != dev)
6264 		dev_put(params->mbssid_config.tx_wdev->netdev);
6265 	kfree(params->beacon.rnr_ies);
6266 	kfree(params);
6267 
6268 	return err;
6269 }
6270 
6271 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
6272 {
6273 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6274 	unsigned int link_id = nl80211_link_id(info->attrs);
6275 	struct net_device *dev = info->user_ptr[1];
6276 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6277 	struct cfg80211_beacon_data params;
6278 	int err;
6279 
6280 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6281 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6282 		return -EOPNOTSUPP;
6283 
6284 	if (!rdev->ops->change_beacon)
6285 		return -EOPNOTSUPP;
6286 
6287 	if (!wdev->links[link_id].ap.beacon_interval)
6288 		return -EINVAL;
6289 
6290 	err = nl80211_parse_beacon(rdev, info->attrs, &params, info->extack);
6291 	if (err)
6292 		goto out;
6293 
6294 	wdev_lock(wdev);
6295 	err = rdev_change_beacon(rdev, dev, &params);
6296 	wdev_unlock(wdev);
6297 
6298 out:
6299 	kfree(params.mbssid_ies);
6300 	kfree(params.rnr_ies);
6301 	return err;
6302 }
6303 
6304 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
6305 {
6306 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6307 	unsigned int link_id = nl80211_link_id(info->attrs);
6308 	struct net_device *dev = info->user_ptr[1];
6309 
6310 	return cfg80211_stop_ap(rdev, dev, link_id, false);
6311 }
6312 
6313 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
6314 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
6315 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
6316 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
6317 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
6318 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
6319 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
6320 };
6321 
6322 static int parse_station_flags(struct genl_info *info,
6323 			       enum nl80211_iftype iftype,
6324 			       struct station_parameters *params)
6325 {
6326 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
6327 	struct nlattr *nla;
6328 	int flag;
6329 
6330 	/*
6331 	 * Try parsing the new attribute first so userspace
6332 	 * can specify both for older kernels.
6333 	 */
6334 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
6335 	if (nla) {
6336 		struct nl80211_sta_flag_update *sta_flags;
6337 
6338 		sta_flags = nla_data(nla);
6339 		params->sta_flags_mask = sta_flags->mask;
6340 		params->sta_flags_set = sta_flags->set;
6341 		params->sta_flags_set &= params->sta_flags_mask;
6342 		if ((params->sta_flags_mask |
6343 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
6344 			return -EINVAL;
6345 		return 0;
6346 	}
6347 
6348 	/* if present, parse the old attribute */
6349 
6350 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
6351 	if (!nla)
6352 		return 0;
6353 
6354 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
6355 		return -EINVAL;
6356 
6357 	/*
6358 	 * Only allow certain flags for interface types so that
6359 	 * other attributes are silently ignored. Remember that
6360 	 * this is backward compatibility code with old userspace
6361 	 * and shouldn't be hit in other cases anyway.
6362 	 */
6363 	switch (iftype) {
6364 	case NL80211_IFTYPE_AP:
6365 	case NL80211_IFTYPE_AP_VLAN:
6366 	case NL80211_IFTYPE_P2P_GO:
6367 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6368 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6369 					 BIT(NL80211_STA_FLAG_WME) |
6370 					 BIT(NL80211_STA_FLAG_MFP);
6371 		break;
6372 	case NL80211_IFTYPE_P2P_CLIENT:
6373 	case NL80211_IFTYPE_STATION:
6374 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6375 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
6376 		break;
6377 	case NL80211_IFTYPE_MESH_POINT:
6378 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6379 					 BIT(NL80211_STA_FLAG_MFP) |
6380 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
6381 		break;
6382 	default:
6383 		return -EINVAL;
6384 	}
6385 
6386 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
6387 		if (flags[flag]) {
6388 			params->sta_flags_set |= (1<<flag);
6389 
6390 			/* no longer support new API additions in old API */
6391 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
6392 				return -EINVAL;
6393 		}
6394 	}
6395 
6396 	return 0;
6397 }
6398 
6399 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
6400 {
6401 	struct nlattr *rate;
6402 	u32 bitrate;
6403 	u16 bitrate_compat;
6404 	enum nl80211_rate_info rate_flg;
6405 
6406 	rate = nla_nest_start_noflag(msg, attr);
6407 	if (!rate)
6408 		return false;
6409 
6410 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
6411 	bitrate = cfg80211_calculate_bitrate(info);
6412 	/* report 16-bit bitrate only if we can */
6413 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
6414 	if (bitrate > 0 &&
6415 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
6416 		return false;
6417 	if (bitrate_compat > 0 &&
6418 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
6419 		return false;
6420 
6421 	switch (info->bw) {
6422 	case RATE_INFO_BW_1:
6423 		rate_flg = NL80211_RATE_INFO_1_MHZ_WIDTH;
6424 		break;
6425 	case RATE_INFO_BW_2:
6426 		rate_flg = NL80211_RATE_INFO_2_MHZ_WIDTH;
6427 		break;
6428 	case RATE_INFO_BW_4:
6429 		rate_flg = NL80211_RATE_INFO_4_MHZ_WIDTH;
6430 		break;
6431 	case RATE_INFO_BW_5:
6432 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
6433 		break;
6434 	case RATE_INFO_BW_8:
6435 		rate_flg = NL80211_RATE_INFO_8_MHZ_WIDTH;
6436 		break;
6437 	case RATE_INFO_BW_10:
6438 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
6439 		break;
6440 	case RATE_INFO_BW_16:
6441 		rate_flg = NL80211_RATE_INFO_16_MHZ_WIDTH;
6442 		break;
6443 	default:
6444 		WARN_ON(1);
6445 		fallthrough;
6446 	case RATE_INFO_BW_20:
6447 		rate_flg = 0;
6448 		break;
6449 	case RATE_INFO_BW_40:
6450 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
6451 		break;
6452 	case RATE_INFO_BW_80:
6453 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
6454 		break;
6455 	case RATE_INFO_BW_160:
6456 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
6457 		break;
6458 	case RATE_INFO_BW_HE_RU:
6459 		rate_flg = 0;
6460 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
6461 		break;
6462 	case RATE_INFO_BW_320:
6463 		rate_flg = NL80211_RATE_INFO_320_MHZ_WIDTH;
6464 		break;
6465 	case RATE_INFO_BW_EHT_RU:
6466 		rate_flg = 0;
6467 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_EHT_MCS));
6468 		break;
6469 	}
6470 
6471 	if (rate_flg && nla_put_flag(msg, rate_flg))
6472 		return false;
6473 
6474 	if (info->flags & RATE_INFO_FLAGS_MCS) {
6475 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
6476 			return false;
6477 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6478 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6479 			return false;
6480 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
6481 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
6482 			return false;
6483 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
6484 			return false;
6485 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6486 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6487 			return false;
6488 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
6489 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
6490 			return false;
6491 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
6492 			return false;
6493 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
6494 			return false;
6495 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
6496 			return false;
6497 		if (info->bw == RATE_INFO_BW_HE_RU &&
6498 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
6499 			       info->he_ru_alloc))
6500 			return false;
6501 	} else if (info->flags & RATE_INFO_FLAGS_S1G_MCS) {
6502 		if (nla_put_u8(msg, NL80211_RATE_INFO_S1G_MCS, info->mcs))
6503 			return false;
6504 		if (nla_put_u8(msg, NL80211_RATE_INFO_S1G_NSS, info->nss))
6505 			return false;
6506 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6507 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6508 			return false;
6509 	} else if (info->flags & RATE_INFO_FLAGS_EHT_MCS) {
6510 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_MCS, info->mcs))
6511 			return false;
6512 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_NSS, info->nss))
6513 			return false;
6514 		if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_GI, info->eht_gi))
6515 			return false;
6516 		if (info->bw == RATE_INFO_BW_EHT_RU &&
6517 		    nla_put_u8(msg, NL80211_RATE_INFO_EHT_RU_ALLOC,
6518 			       info->eht_ru_alloc))
6519 			return false;
6520 	}
6521 
6522 	nla_nest_end(msg, rate);
6523 	return true;
6524 }
6525 
6526 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
6527 			       int id)
6528 {
6529 	void *attr;
6530 	int i = 0;
6531 
6532 	if (!mask)
6533 		return true;
6534 
6535 	attr = nla_nest_start_noflag(msg, id);
6536 	if (!attr)
6537 		return false;
6538 
6539 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
6540 		if (!(mask & BIT(i)))
6541 			continue;
6542 
6543 		if (nla_put_u8(msg, i, signal[i]))
6544 			return false;
6545 	}
6546 
6547 	nla_nest_end(msg, attr);
6548 
6549 	return true;
6550 }
6551 
6552 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
6553 				u32 seq, int flags,
6554 				struct cfg80211_registered_device *rdev,
6555 				struct net_device *dev,
6556 				const u8 *mac_addr, struct station_info *sinfo)
6557 {
6558 	void *hdr;
6559 	struct nlattr *sinfoattr, *bss_param;
6560 
6561 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
6562 	if (!hdr) {
6563 		cfg80211_sinfo_release_content(sinfo);
6564 		return -1;
6565 	}
6566 
6567 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6568 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
6569 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
6570 		goto nla_put_failure;
6571 
6572 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
6573 	if (!sinfoattr)
6574 		goto nla_put_failure;
6575 
6576 #define PUT_SINFO(attr, memb, type) do {				\
6577 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
6578 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6579 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
6580 			     sinfo->memb))				\
6581 		goto nla_put_failure;					\
6582 	} while (0)
6583 #define PUT_SINFO_U64(attr, memb) do {					\
6584 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
6585 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
6586 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
6587 		goto nla_put_failure;					\
6588 	} while (0)
6589 
6590 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
6591 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
6592 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
6593 
6594 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
6595 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
6596 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
6597 			(u32)sinfo->rx_bytes))
6598 		goto nla_put_failure;
6599 
6600 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
6601 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
6602 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
6603 			(u32)sinfo->tx_bytes))
6604 		goto nla_put_failure;
6605 
6606 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
6607 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
6608 	PUT_SINFO(LLID, llid, u16);
6609 	PUT_SINFO(PLID, plid, u16);
6610 	PUT_SINFO(PLINK_STATE, plink_state, u8);
6611 	PUT_SINFO_U64(RX_DURATION, rx_duration);
6612 	PUT_SINFO_U64(TX_DURATION, tx_duration);
6613 
6614 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6615 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6616 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
6617 
6618 	switch (rdev->wiphy.signal_type) {
6619 	case CFG80211_SIGNAL_TYPE_MBM:
6620 		PUT_SINFO(SIGNAL, signal, u8);
6621 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
6622 		break;
6623 	default:
6624 		break;
6625 	}
6626 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
6627 		if (!nl80211_put_signal(msg, sinfo->chains,
6628 					sinfo->chain_signal,
6629 					NL80211_STA_INFO_CHAIN_SIGNAL))
6630 			goto nla_put_failure;
6631 	}
6632 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
6633 		if (!nl80211_put_signal(msg, sinfo->chains,
6634 					sinfo->chain_signal_avg,
6635 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
6636 			goto nla_put_failure;
6637 	}
6638 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
6639 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
6640 					  NL80211_STA_INFO_TX_BITRATE))
6641 			goto nla_put_failure;
6642 	}
6643 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
6644 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
6645 					  NL80211_STA_INFO_RX_BITRATE))
6646 			goto nla_put_failure;
6647 	}
6648 
6649 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
6650 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
6651 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
6652 	PUT_SINFO(TX_FAILED, tx_failed, u32);
6653 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
6654 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
6655 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
6656 	PUT_SINFO(LOCAL_PM, local_pm, u32);
6657 	PUT_SINFO(PEER_PM, peer_pm, u32);
6658 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
6659 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
6660 	PUT_SINFO(CONNECTED_TO_AS, connected_to_as, u8);
6661 
6662 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
6663 		bss_param = nla_nest_start_noflag(msg,
6664 						  NL80211_STA_INFO_BSS_PARAM);
6665 		if (!bss_param)
6666 			goto nla_put_failure;
6667 
6668 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
6669 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
6670 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
6671 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
6672 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
6673 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
6674 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
6675 			       sinfo->bss_param.dtim_period) ||
6676 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
6677 				sinfo->bss_param.beacon_interval))
6678 			goto nla_put_failure;
6679 
6680 		nla_nest_end(msg, bss_param);
6681 	}
6682 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
6683 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
6684 		    sizeof(struct nl80211_sta_flag_update),
6685 		    &sinfo->sta_flags))
6686 		goto nla_put_failure;
6687 
6688 	PUT_SINFO_U64(T_OFFSET, t_offset);
6689 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
6690 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
6691 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
6692 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
6693 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
6694 	if (wiphy_ext_feature_isset(&rdev->wiphy,
6695 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
6696 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
6697 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
6698 	}
6699 
6700 #undef PUT_SINFO
6701 #undef PUT_SINFO_U64
6702 
6703 	if (sinfo->pertid) {
6704 		struct nlattr *tidsattr;
6705 		int tid;
6706 
6707 		tidsattr = nla_nest_start_noflag(msg,
6708 						 NL80211_STA_INFO_TID_STATS);
6709 		if (!tidsattr)
6710 			goto nla_put_failure;
6711 
6712 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
6713 			struct cfg80211_tid_stats *tidstats;
6714 			struct nlattr *tidattr;
6715 
6716 			tidstats = &sinfo->pertid[tid];
6717 
6718 			if (!tidstats->filled)
6719 				continue;
6720 
6721 			tidattr = nla_nest_start_noflag(msg, tid + 1);
6722 			if (!tidattr)
6723 				goto nla_put_failure;
6724 
6725 #define PUT_TIDVAL_U64(attr, memb) do {					\
6726 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
6727 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
6728 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
6729 		goto nla_put_failure;					\
6730 	} while (0)
6731 
6732 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
6733 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
6734 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
6735 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
6736 
6737 #undef PUT_TIDVAL_U64
6738 			if ((tidstats->filled &
6739 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
6740 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
6741 						   NL80211_TID_STATS_TXQ_STATS))
6742 				goto nla_put_failure;
6743 
6744 			nla_nest_end(msg, tidattr);
6745 		}
6746 
6747 		nla_nest_end(msg, tidsattr);
6748 	}
6749 
6750 	nla_nest_end(msg, sinfoattr);
6751 
6752 	if (sinfo->assoc_req_ies_len &&
6753 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
6754 		    sinfo->assoc_req_ies))
6755 		goto nla_put_failure;
6756 
6757 	if (sinfo->assoc_resp_ies_len &&
6758 	    nla_put(msg, NL80211_ATTR_RESP_IE, sinfo->assoc_resp_ies_len,
6759 		    sinfo->assoc_resp_ies))
6760 		goto nla_put_failure;
6761 
6762 	if (sinfo->mlo_params_valid) {
6763 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
6764 			       sinfo->assoc_link_id))
6765 			goto nla_put_failure;
6766 
6767 		if (!is_zero_ether_addr(sinfo->mld_addr) &&
6768 		    nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
6769 			    sinfo->mld_addr))
6770 			goto nla_put_failure;
6771 	}
6772 
6773 	cfg80211_sinfo_release_content(sinfo);
6774 	genlmsg_end(msg, hdr);
6775 	return 0;
6776 
6777  nla_put_failure:
6778 	cfg80211_sinfo_release_content(sinfo);
6779 	genlmsg_cancel(msg, hdr);
6780 	return -EMSGSIZE;
6781 }
6782 
6783 static int nl80211_dump_station(struct sk_buff *skb,
6784 				struct netlink_callback *cb)
6785 {
6786 	struct station_info sinfo;
6787 	struct cfg80211_registered_device *rdev;
6788 	struct wireless_dev *wdev;
6789 	u8 mac_addr[ETH_ALEN];
6790 	int sta_idx = cb->args[2];
6791 	int err;
6792 
6793 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
6794 	if (err)
6795 		return err;
6796 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
6797 	__acquire(&rdev->wiphy.mtx);
6798 
6799 	if (!wdev->netdev) {
6800 		err = -EINVAL;
6801 		goto out_err;
6802 	}
6803 
6804 	if (!rdev->ops->dump_station) {
6805 		err = -EOPNOTSUPP;
6806 		goto out_err;
6807 	}
6808 
6809 	while (1) {
6810 		memset(&sinfo, 0, sizeof(sinfo));
6811 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
6812 					mac_addr, &sinfo);
6813 		if (err == -ENOENT)
6814 			break;
6815 		if (err)
6816 			goto out_err;
6817 
6818 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
6819 				NETLINK_CB(cb->skb).portid,
6820 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
6821 				rdev, wdev->netdev, mac_addr,
6822 				&sinfo) < 0)
6823 			goto out;
6824 
6825 		sta_idx++;
6826 	}
6827 
6828  out:
6829 	cb->args[2] = sta_idx;
6830 	err = skb->len;
6831  out_err:
6832 	wiphy_unlock(&rdev->wiphy);
6833 
6834 	return err;
6835 }
6836 
6837 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
6838 {
6839 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6840 	struct net_device *dev = info->user_ptr[1];
6841 	struct station_info sinfo;
6842 	struct sk_buff *msg;
6843 	u8 *mac_addr = NULL;
6844 	int err;
6845 
6846 	memset(&sinfo, 0, sizeof(sinfo));
6847 
6848 	if (!info->attrs[NL80211_ATTR_MAC])
6849 		return -EINVAL;
6850 
6851 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6852 
6853 	if (!rdev->ops->get_station)
6854 		return -EOPNOTSUPP;
6855 
6856 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
6857 	if (err)
6858 		return err;
6859 
6860 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6861 	if (!msg) {
6862 		cfg80211_sinfo_release_content(&sinfo);
6863 		return -ENOMEM;
6864 	}
6865 
6866 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
6867 				 info->snd_portid, info->snd_seq, 0,
6868 				 rdev, dev, mac_addr, &sinfo) < 0) {
6869 		nlmsg_free(msg);
6870 		return -ENOBUFS;
6871 	}
6872 
6873 	return genlmsg_reply(msg, info);
6874 }
6875 
6876 int cfg80211_check_station_change(struct wiphy *wiphy,
6877 				  struct station_parameters *params,
6878 				  enum cfg80211_station_type statype)
6879 {
6880 	if (params->listen_interval != -1 &&
6881 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6882 		return -EINVAL;
6883 
6884 	if (params->support_p2p_ps != -1 &&
6885 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6886 		return -EINVAL;
6887 
6888 	if (params->aid &&
6889 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
6890 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6891 		return -EINVAL;
6892 
6893 	/* When you run into this, adjust the code below for the new flag */
6894 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
6895 
6896 	switch (statype) {
6897 	case CFG80211_STA_MESH_PEER_KERNEL:
6898 	case CFG80211_STA_MESH_PEER_USER:
6899 		/*
6900 		 * No ignoring the TDLS flag here -- the userspace mesh
6901 		 * code doesn't have the bug of including TDLS in the
6902 		 * mask everywhere.
6903 		 */
6904 		if (params->sta_flags_mask &
6905 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6906 				  BIT(NL80211_STA_FLAG_MFP) |
6907 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
6908 			return -EINVAL;
6909 		break;
6910 	case CFG80211_STA_TDLS_PEER_SETUP:
6911 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6912 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6913 			return -EINVAL;
6914 		/* ignore since it can't change */
6915 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6916 		break;
6917 	default:
6918 		/* disallow mesh-specific things */
6919 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
6920 			return -EINVAL;
6921 		if (params->local_pm)
6922 			return -EINVAL;
6923 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6924 			return -EINVAL;
6925 	}
6926 
6927 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6928 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
6929 		/* TDLS can't be set, ... */
6930 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
6931 			return -EINVAL;
6932 		/*
6933 		 * ... but don't bother the driver with it. This works around
6934 		 * a hostapd/wpa_supplicant issue -- it always includes the
6935 		 * TLDS_PEER flag in the mask even for AP mode.
6936 		 */
6937 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6938 	}
6939 
6940 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6941 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6942 		/* reject other things that can't change */
6943 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
6944 			return -EINVAL;
6945 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
6946 			return -EINVAL;
6947 		if (params->link_sta_params.supported_rates)
6948 			return -EINVAL;
6949 		if (params->ext_capab || params->link_sta_params.ht_capa ||
6950 		    params->link_sta_params.vht_capa ||
6951 		    params->link_sta_params.he_capa ||
6952 		    params->link_sta_params.eht_capa)
6953 			return -EINVAL;
6954 	}
6955 
6956 	if (statype != CFG80211_STA_AP_CLIENT &&
6957 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6958 		if (params->vlan)
6959 			return -EINVAL;
6960 	}
6961 
6962 	switch (statype) {
6963 	case CFG80211_STA_AP_MLME_CLIENT:
6964 		/* Use this only for authorizing/unauthorizing a station */
6965 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
6966 			return -EOPNOTSUPP;
6967 		break;
6968 	case CFG80211_STA_AP_CLIENT:
6969 	case CFG80211_STA_AP_CLIENT_UNASSOC:
6970 		/* accept only the listed bits */
6971 		if (params->sta_flags_mask &
6972 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6973 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6974 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
6975 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6976 				  BIT(NL80211_STA_FLAG_WME) |
6977 				  BIT(NL80211_STA_FLAG_MFP)))
6978 			return -EINVAL;
6979 
6980 		/* but authenticated/associated only if driver handles it */
6981 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6982 		    params->sta_flags_mask &
6983 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6984 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
6985 			return -EINVAL;
6986 		break;
6987 	case CFG80211_STA_IBSS:
6988 	case CFG80211_STA_AP_STA:
6989 		/* reject any changes other than AUTHORIZED */
6990 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
6991 			return -EINVAL;
6992 		break;
6993 	case CFG80211_STA_TDLS_PEER_SETUP:
6994 		/* reject any changes other than AUTHORIZED or WME */
6995 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6996 					       BIT(NL80211_STA_FLAG_WME)))
6997 			return -EINVAL;
6998 		/* force (at least) rates when authorizing */
6999 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
7000 		    !params->link_sta_params.supported_rates)
7001 			return -EINVAL;
7002 		break;
7003 	case CFG80211_STA_TDLS_PEER_ACTIVE:
7004 		/* reject any changes */
7005 		return -EINVAL;
7006 	case CFG80211_STA_MESH_PEER_KERNEL:
7007 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
7008 			return -EINVAL;
7009 		break;
7010 	case CFG80211_STA_MESH_PEER_USER:
7011 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
7012 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
7013 			return -EINVAL;
7014 		break;
7015 	}
7016 
7017 	/*
7018 	 * Older kernel versions ignored this attribute entirely, so don't
7019 	 * reject attempts to update it but mark it as unused instead so the
7020 	 * driver won't look at the data.
7021 	 */
7022 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
7023 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
7024 		params->link_sta_params.opmode_notif_used = false;
7025 
7026 	return 0;
7027 }
7028 EXPORT_SYMBOL(cfg80211_check_station_change);
7029 
7030 /*
7031  * Get vlan interface making sure it is running and on the right wiphy.
7032  */
7033 static struct net_device *get_vlan(struct genl_info *info,
7034 				   struct cfg80211_registered_device *rdev)
7035 {
7036 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
7037 	struct net_device *v;
7038 	int ret;
7039 
7040 	if (!vlanattr)
7041 		return NULL;
7042 
7043 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
7044 	if (!v)
7045 		return ERR_PTR(-ENODEV);
7046 
7047 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
7048 		ret = -EINVAL;
7049 		goto error;
7050 	}
7051 
7052 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
7053 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
7054 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
7055 		ret = -EINVAL;
7056 		goto error;
7057 	}
7058 
7059 	if (!netif_running(v)) {
7060 		ret = -ENETDOWN;
7061 		goto error;
7062 	}
7063 
7064 	return v;
7065  error:
7066 	dev_put(v);
7067 	return ERR_PTR(ret);
7068 }
7069 
7070 static int nl80211_parse_sta_wme(struct genl_info *info,
7071 				 struct station_parameters *params)
7072 {
7073 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
7074 	struct nlattr *nla;
7075 	int err;
7076 
7077 	/* parse WME attributes if present */
7078 	if (!info->attrs[NL80211_ATTR_STA_WME])
7079 		return 0;
7080 
7081 	nla = info->attrs[NL80211_ATTR_STA_WME];
7082 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
7083 					  nl80211_sta_wme_policy,
7084 					  info->extack);
7085 	if (err)
7086 		return err;
7087 
7088 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
7089 		params->uapsd_queues = nla_get_u8(
7090 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
7091 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
7092 		return -EINVAL;
7093 
7094 	if (tb[NL80211_STA_WME_MAX_SP])
7095 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
7096 
7097 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
7098 		return -EINVAL;
7099 
7100 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
7101 
7102 	return 0;
7103 }
7104 
7105 static int nl80211_parse_sta_channel_info(struct genl_info *info,
7106 				      struct station_parameters *params)
7107 {
7108 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
7109 		params->supported_channels =
7110 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
7111 		params->supported_channels_len =
7112 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
7113 		/*
7114 		 * Need to include at least one (first channel, number of
7115 		 * channels) tuple for each subband (checked in policy),
7116 		 * and must have proper tuples for the rest of the data as well.
7117 		 */
7118 		if (params->supported_channels_len % 2)
7119 			return -EINVAL;
7120 	}
7121 
7122 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
7123 		params->supported_oper_classes =
7124 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
7125 		params->supported_oper_classes_len =
7126 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
7127 	}
7128 	return 0;
7129 }
7130 
7131 static int nl80211_set_station_tdls(struct genl_info *info,
7132 				    struct station_parameters *params)
7133 {
7134 	int err;
7135 	/* Dummy STA entry gets updated once the peer capabilities are known */
7136 	if (info->attrs[NL80211_ATTR_PEER_AID])
7137 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7138 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7139 		params->link_sta_params.ht_capa =
7140 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7141 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7142 		params->link_sta_params.vht_capa =
7143 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7144 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7145 		params->link_sta_params.he_capa =
7146 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7147 		params->link_sta_params.he_capa_len =
7148 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7149 
7150 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7151 			params->link_sta_params.eht_capa =
7152 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7153 			params->link_sta_params.eht_capa_len =
7154 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7155 
7156 			if (!ieee80211_eht_capa_size_ok((const u8 *)params->link_sta_params.he_capa,
7157 							(const u8 *)params->link_sta_params.eht_capa,
7158 							params->link_sta_params.eht_capa_len,
7159 							false))
7160 				return -EINVAL;
7161 		}
7162 	}
7163 
7164 	err = nl80211_parse_sta_channel_info(info, params);
7165 	if (err)
7166 		return err;
7167 
7168 	return nl80211_parse_sta_wme(info, params);
7169 }
7170 
7171 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
7172 					     struct sta_txpwr *txpwr,
7173 					     bool *txpwr_set)
7174 {
7175 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7176 	int idx;
7177 
7178 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
7179 		if (!rdev->ops->set_tx_power ||
7180 		    !wiphy_ext_feature_isset(&rdev->wiphy,
7181 					 NL80211_EXT_FEATURE_STA_TX_PWR))
7182 			return -EOPNOTSUPP;
7183 
7184 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
7185 		txpwr->type = nla_get_u8(info->attrs[idx]);
7186 
7187 		if (txpwr->type == NL80211_TX_POWER_LIMITED) {
7188 			idx = NL80211_ATTR_STA_TX_POWER;
7189 
7190 			if (info->attrs[idx])
7191 				txpwr->power = nla_get_s16(info->attrs[idx]);
7192 			else
7193 				return -EINVAL;
7194 		}
7195 
7196 		*txpwr_set = true;
7197 	} else {
7198 		*txpwr_set = false;
7199 	}
7200 
7201 	return 0;
7202 }
7203 
7204 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
7205 {
7206 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7207 	struct net_device *dev = info->user_ptr[1];
7208 	struct station_parameters params;
7209 	u8 *mac_addr;
7210 	int err;
7211 
7212 	memset(&params, 0, sizeof(params));
7213 
7214 	if (!rdev->ops->change_station)
7215 		return -EOPNOTSUPP;
7216 
7217 	/*
7218 	 * AID and listen_interval properties can be set only for unassociated
7219 	 * station. Include these parameters here and will check them in
7220 	 * cfg80211_check_station_change().
7221 	 */
7222 	if (info->attrs[NL80211_ATTR_STA_AID])
7223 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7224 
7225 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7226 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7227 
7228 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7229 		params.listen_interval =
7230 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7231 	else
7232 		params.listen_interval = -1;
7233 
7234 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
7235 		params.support_p2p_ps =
7236 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7237 	else
7238 		params.support_p2p_ps = -1;
7239 
7240 	if (!info->attrs[NL80211_ATTR_MAC])
7241 		return -EINVAL;
7242 
7243 	params.link_sta_params.link_id =
7244 		nl80211_link_id_or_invalid(info->attrs);
7245 
7246 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7247 		/* If MLD_ADDR attribute is set then this is an MLD station
7248 		 * and the MLD_ADDR attribute holds the MLD address and the
7249 		 * MAC attribute holds for the LINK address.
7250 		 * In that case, the link_id is also expected to be valid.
7251 		 */
7252 		if (params.link_sta_params.link_id < 0)
7253 			return -EINVAL;
7254 
7255 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7256 		params.link_sta_params.mld_mac = mac_addr;
7257 		params.link_sta_params.link_mac =
7258 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7259 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7260 			return -EINVAL;
7261 	} else {
7262 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7263 	}
7264 
7265 
7266 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
7267 		params.link_sta_params.supported_rates =
7268 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7269 		params.link_sta_params.supported_rates_len =
7270 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7271 	}
7272 
7273 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7274 		params.capability =
7275 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7276 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7277 	}
7278 
7279 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7280 		params.ext_capab =
7281 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7282 		params.ext_capab_len =
7283 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7284 	}
7285 
7286 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7287 		return -EINVAL;
7288 
7289 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7290 		params.plink_action =
7291 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7292 
7293 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
7294 		params.plink_state =
7295 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
7296 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
7297 			params.peer_aid = nla_get_u16(
7298 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
7299 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
7300 	}
7301 
7302 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
7303 		params.local_pm = nla_get_u32(
7304 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
7305 
7306 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7307 		params.link_sta_params.opmode_notif_used = true;
7308 		params.link_sta_params.opmode_notif =
7309 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7310 	}
7311 
7312 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7313 		params.link_sta_params.he_6ghz_capa =
7314 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7315 
7316 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7317 		params.airtime_weight =
7318 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7319 
7320 	if (params.airtime_weight &&
7321 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7322 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7323 		return -EOPNOTSUPP;
7324 
7325 	err = nl80211_parse_sta_txpower_setting(info,
7326 						&params.link_sta_params.txpwr,
7327 						&params.link_sta_params.txpwr_set);
7328 	if (err)
7329 		return err;
7330 
7331 	/* Include parameters for TDLS peer (will check later) */
7332 	err = nl80211_set_station_tdls(info, &params);
7333 	if (err)
7334 		return err;
7335 
7336 	params.vlan = get_vlan(info, rdev);
7337 	if (IS_ERR(params.vlan))
7338 		return PTR_ERR(params.vlan);
7339 
7340 	switch (dev->ieee80211_ptr->iftype) {
7341 	case NL80211_IFTYPE_AP:
7342 	case NL80211_IFTYPE_AP_VLAN:
7343 	case NL80211_IFTYPE_P2P_GO:
7344 	case NL80211_IFTYPE_P2P_CLIENT:
7345 	case NL80211_IFTYPE_STATION:
7346 	case NL80211_IFTYPE_ADHOC:
7347 	case NL80211_IFTYPE_MESH_POINT:
7348 		break;
7349 	default:
7350 		err = -EOPNOTSUPP;
7351 		goto out_put_vlan;
7352 	}
7353 
7354 	/* driver will call cfg80211_check_station_change() */
7355 	wdev_lock(dev->ieee80211_ptr);
7356 	err = rdev_change_station(rdev, dev, mac_addr, &params);
7357 	wdev_unlock(dev->ieee80211_ptr);
7358 
7359  out_put_vlan:
7360 	dev_put(params.vlan);
7361 
7362 	return err;
7363 }
7364 
7365 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
7366 {
7367 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7368 	int err;
7369 	struct net_device *dev = info->user_ptr[1];
7370 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7371 	struct station_parameters params;
7372 	u8 *mac_addr = NULL;
7373 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
7374 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
7375 
7376 	memset(&params, 0, sizeof(params));
7377 
7378 	if (!rdev->ops->add_station)
7379 		return -EOPNOTSUPP;
7380 
7381 	if (!info->attrs[NL80211_ATTR_MAC])
7382 		return -EINVAL;
7383 
7384 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7385 		return -EINVAL;
7386 
7387 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
7388 		return -EINVAL;
7389 
7390 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
7391 	    !info->attrs[NL80211_ATTR_PEER_AID])
7392 		return -EINVAL;
7393 
7394 	params.link_sta_params.link_id =
7395 		nl80211_link_id_or_invalid(info->attrs);
7396 
7397 	if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7398 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7399 		params.link_sta_params.mld_mac = mac_addr;
7400 		params.link_sta_params.link_mac =
7401 			nla_data(info->attrs[NL80211_ATTR_MAC]);
7402 		if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7403 			return -EINVAL;
7404 	} else {
7405 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7406 	}
7407 
7408 	params.link_sta_params.supported_rates =
7409 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7410 	params.link_sta_params.supported_rates_len =
7411 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7412 	params.listen_interval =
7413 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7414 
7415 	if (info->attrs[NL80211_ATTR_VLAN_ID])
7416 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7417 
7418 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
7419 		params.support_p2p_ps =
7420 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7421 	} else {
7422 		/*
7423 		 * if not specified, assume it's supported for P2P GO interface,
7424 		 * and is NOT supported for AP interface
7425 		 */
7426 		params.support_p2p_ps =
7427 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
7428 	}
7429 
7430 	if (info->attrs[NL80211_ATTR_PEER_AID])
7431 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7432 	else
7433 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7434 
7435 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7436 		params.capability =
7437 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7438 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7439 	}
7440 
7441 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7442 		params.ext_capab =
7443 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7444 		params.ext_capab_len =
7445 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7446 	}
7447 
7448 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7449 		params.link_sta_params.ht_capa =
7450 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7451 
7452 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7453 		params.link_sta_params.vht_capa =
7454 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7455 
7456 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7457 		params.link_sta_params.he_capa =
7458 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7459 		params.link_sta_params.he_capa_len =
7460 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7461 
7462 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7463 			params.link_sta_params.eht_capa =
7464 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7465 			params.link_sta_params.eht_capa_len =
7466 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7467 
7468 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.link_sta_params.he_capa,
7469 							(const u8 *)params.link_sta_params.eht_capa,
7470 							params.link_sta_params.eht_capa_len,
7471 							false))
7472 				return -EINVAL;
7473 		}
7474 	}
7475 
7476 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7477 		params.link_sta_params.he_6ghz_capa =
7478 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7479 
7480 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7481 		params.link_sta_params.opmode_notif_used = true;
7482 		params.link_sta_params.opmode_notif =
7483 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7484 	}
7485 
7486 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7487 		params.plink_action =
7488 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7489 
7490 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7491 		params.airtime_weight =
7492 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7493 
7494 	if (params.airtime_weight &&
7495 	    !wiphy_ext_feature_isset(&rdev->wiphy,
7496 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7497 		return -EOPNOTSUPP;
7498 
7499 	err = nl80211_parse_sta_txpower_setting(info,
7500 						&params.link_sta_params.txpwr,
7501 						&params.link_sta_params.txpwr_set);
7502 	if (err)
7503 		return err;
7504 
7505 	err = nl80211_parse_sta_channel_info(info, &params);
7506 	if (err)
7507 		return err;
7508 
7509 	err = nl80211_parse_sta_wme(info, &params);
7510 	if (err)
7511 		return err;
7512 
7513 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7514 		return -EINVAL;
7515 
7516 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
7517 	 * as userspace might just pass through the capabilities from the IEs
7518 	 * directly, rather than enforcing this restriction and returning an
7519 	 * error in this case.
7520 	 */
7521 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
7522 		params.link_sta_params.ht_capa = NULL;
7523 		params.link_sta_params.vht_capa = NULL;
7524 
7525 		/* HE and EHT require WME */
7526 		if (params.link_sta_params.he_capa_len ||
7527 		    params.link_sta_params.he_6ghz_capa ||
7528 		    params.link_sta_params.eht_capa_len)
7529 			return -EINVAL;
7530 	}
7531 
7532 	/* Ensure that HT/VHT capabilities are not set for 6 GHz HE STA */
7533 	if (params.link_sta_params.he_6ghz_capa &&
7534 	    (params.link_sta_params.ht_capa || params.link_sta_params.vht_capa))
7535 		return -EINVAL;
7536 
7537 	/* When you run into this, adjust the code below for the new flag */
7538 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
7539 
7540 	switch (dev->ieee80211_ptr->iftype) {
7541 	case NL80211_IFTYPE_AP:
7542 	case NL80211_IFTYPE_AP_VLAN:
7543 	case NL80211_IFTYPE_P2P_GO:
7544 		/* ignore WME attributes if iface/sta is not capable */
7545 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
7546 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
7547 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7548 
7549 		/* TDLS peers cannot be added */
7550 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7551 		    info->attrs[NL80211_ATTR_PEER_AID])
7552 			return -EINVAL;
7553 		/* but don't bother the driver with it */
7554 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
7555 
7556 		/* allow authenticated/associated only if driver handles it */
7557 		if (!(rdev->wiphy.features &
7558 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
7559 		    params.sta_flags_mask & auth_assoc)
7560 			return -EINVAL;
7561 
7562 		/* Older userspace, or userspace wanting to be compatible with
7563 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
7564 		 * and assoc flags in the mask, but assumes the station will be
7565 		 * added as associated anyway since this was the required driver
7566 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
7567 		 * introduced.
7568 		 * In order to not bother drivers with this quirk in the API
7569 		 * set the flags in both the mask and set for new stations in
7570 		 * this case.
7571 		 */
7572 		if (!(params.sta_flags_mask & auth_assoc)) {
7573 			params.sta_flags_mask |= auth_assoc;
7574 			params.sta_flags_set |= auth_assoc;
7575 		}
7576 
7577 		/* must be last in here for error handling */
7578 		params.vlan = get_vlan(info, rdev);
7579 		if (IS_ERR(params.vlan))
7580 			return PTR_ERR(params.vlan);
7581 		break;
7582 	case NL80211_IFTYPE_MESH_POINT:
7583 		/* ignore uAPSD data */
7584 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7585 
7586 		/* associated is disallowed */
7587 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
7588 			return -EINVAL;
7589 		/* TDLS peers cannot be added */
7590 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7591 		    info->attrs[NL80211_ATTR_PEER_AID])
7592 			return -EINVAL;
7593 		break;
7594 	case NL80211_IFTYPE_STATION:
7595 	case NL80211_IFTYPE_P2P_CLIENT:
7596 		/* ignore uAPSD data */
7597 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7598 
7599 		/* these are disallowed */
7600 		if (params.sta_flags_mask &
7601 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
7602 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
7603 			return -EINVAL;
7604 		/* Only TDLS peers can be added */
7605 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
7606 			return -EINVAL;
7607 		/* Can only add if TDLS ... */
7608 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
7609 			return -EOPNOTSUPP;
7610 		/* ... with external setup is supported */
7611 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
7612 			return -EOPNOTSUPP;
7613 		/*
7614 		 * Older wpa_supplicant versions always mark the TDLS peer
7615 		 * as authorized, but it shouldn't yet be.
7616 		 */
7617 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
7618 		break;
7619 	default:
7620 		return -EOPNOTSUPP;
7621 	}
7622 
7623 	/* be aware of params.vlan when changing code here */
7624 
7625 	wdev_lock(dev->ieee80211_ptr);
7626 	if (wdev->valid_links) {
7627 		if (params.link_sta_params.link_id < 0) {
7628 			err = -EINVAL;
7629 			goto out;
7630 		}
7631 		if (!(wdev->valid_links & BIT(params.link_sta_params.link_id))) {
7632 			err = -ENOLINK;
7633 			goto out;
7634 		}
7635 	} else {
7636 		if (params.link_sta_params.link_id >= 0) {
7637 			err = -EINVAL;
7638 			goto out;
7639 		}
7640 	}
7641 	err = rdev_add_station(rdev, dev, mac_addr, &params);
7642 out:
7643 	wdev_unlock(dev->ieee80211_ptr);
7644 	dev_put(params.vlan);
7645 	return err;
7646 }
7647 
7648 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
7649 {
7650 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7651 	struct net_device *dev = info->user_ptr[1];
7652 	struct station_del_parameters params;
7653 	int ret;
7654 
7655 	memset(&params, 0, sizeof(params));
7656 
7657 	if (info->attrs[NL80211_ATTR_MAC])
7658 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
7659 
7660 	switch (dev->ieee80211_ptr->iftype) {
7661 	case NL80211_IFTYPE_AP:
7662 	case NL80211_IFTYPE_AP_VLAN:
7663 	case NL80211_IFTYPE_MESH_POINT:
7664 	case NL80211_IFTYPE_P2P_GO:
7665 		/* always accept these */
7666 		break;
7667 	case NL80211_IFTYPE_ADHOC:
7668 		/* conditionally accept */
7669 		if (wiphy_ext_feature_isset(&rdev->wiphy,
7670 					    NL80211_EXT_FEATURE_DEL_IBSS_STA))
7671 			break;
7672 		return -EINVAL;
7673 	default:
7674 		return -EINVAL;
7675 	}
7676 
7677 	if (!rdev->ops->del_station)
7678 		return -EOPNOTSUPP;
7679 
7680 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
7681 		params.subtype =
7682 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
7683 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
7684 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
7685 			return -EINVAL;
7686 	} else {
7687 		/* Default to Deauthentication frame */
7688 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
7689 	}
7690 
7691 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
7692 		params.reason_code =
7693 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7694 		if (params.reason_code == 0)
7695 			return -EINVAL; /* 0 is reserved */
7696 	} else {
7697 		/* Default to reason code 2 */
7698 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
7699 	}
7700 
7701 	wdev_lock(dev->ieee80211_ptr);
7702 	ret = rdev_del_station(rdev, dev, &params);
7703 	wdev_unlock(dev->ieee80211_ptr);
7704 
7705 	return ret;
7706 }
7707 
7708 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
7709 				int flags, struct net_device *dev,
7710 				u8 *dst, u8 *next_hop,
7711 				struct mpath_info *pinfo)
7712 {
7713 	void *hdr;
7714 	struct nlattr *pinfoattr;
7715 
7716 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
7717 	if (!hdr)
7718 		return -1;
7719 
7720 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7721 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7722 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
7723 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
7724 		goto nla_put_failure;
7725 
7726 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
7727 	if (!pinfoattr)
7728 		goto nla_put_failure;
7729 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
7730 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
7731 			pinfo->frame_qlen))
7732 		goto nla_put_failure;
7733 	if (((pinfo->filled & MPATH_INFO_SN) &&
7734 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
7735 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
7736 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
7737 			 pinfo->metric)) ||
7738 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
7739 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
7740 			 pinfo->exptime)) ||
7741 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
7742 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
7743 			pinfo->flags)) ||
7744 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
7745 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
7746 			 pinfo->discovery_timeout)) ||
7747 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
7748 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
7749 			pinfo->discovery_retries)) ||
7750 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
7751 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
7752 			pinfo->hop_count)) ||
7753 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
7754 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
7755 			 pinfo->path_change_count)))
7756 		goto nla_put_failure;
7757 
7758 	nla_nest_end(msg, pinfoattr);
7759 
7760 	genlmsg_end(msg, hdr);
7761 	return 0;
7762 
7763  nla_put_failure:
7764 	genlmsg_cancel(msg, hdr);
7765 	return -EMSGSIZE;
7766 }
7767 
7768 static int nl80211_dump_mpath(struct sk_buff *skb,
7769 			      struct netlink_callback *cb)
7770 {
7771 	struct mpath_info pinfo;
7772 	struct cfg80211_registered_device *rdev;
7773 	struct wireless_dev *wdev;
7774 	u8 dst[ETH_ALEN];
7775 	u8 next_hop[ETH_ALEN];
7776 	int path_idx = cb->args[2];
7777 	int err;
7778 
7779 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7780 	if (err)
7781 		return err;
7782 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7783 	__acquire(&rdev->wiphy.mtx);
7784 
7785 	if (!rdev->ops->dump_mpath) {
7786 		err = -EOPNOTSUPP;
7787 		goto out_err;
7788 	}
7789 
7790 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7791 		err = -EOPNOTSUPP;
7792 		goto out_err;
7793 	}
7794 
7795 	while (1) {
7796 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
7797 				      next_hop, &pinfo);
7798 		if (err == -ENOENT)
7799 			break;
7800 		if (err)
7801 			goto out_err;
7802 
7803 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7804 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
7805 				       wdev->netdev, dst, next_hop,
7806 				       &pinfo) < 0)
7807 			goto out;
7808 
7809 		path_idx++;
7810 	}
7811 
7812  out:
7813 	cb->args[2] = path_idx;
7814 	err = skb->len;
7815  out_err:
7816 	wiphy_unlock(&rdev->wiphy);
7817 	return err;
7818 }
7819 
7820 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
7821 {
7822 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7823 	int err;
7824 	struct net_device *dev = info->user_ptr[1];
7825 	struct mpath_info pinfo;
7826 	struct sk_buff *msg;
7827 	u8 *dst = NULL;
7828 	u8 next_hop[ETH_ALEN];
7829 
7830 	memset(&pinfo, 0, sizeof(pinfo));
7831 
7832 	if (!info->attrs[NL80211_ATTR_MAC])
7833 		return -EINVAL;
7834 
7835 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7836 
7837 	if (!rdev->ops->get_mpath)
7838 		return -EOPNOTSUPP;
7839 
7840 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7841 		return -EOPNOTSUPP;
7842 
7843 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
7844 	if (err)
7845 		return err;
7846 
7847 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7848 	if (!msg)
7849 		return -ENOMEM;
7850 
7851 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7852 				 dev, dst, next_hop, &pinfo) < 0) {
7853 		nlmsg_free(msg);
7854 		return -ENOBUFS;
7855 	}
7856 
7857 	return genlmsg_reply(msg, info);
7858 }
7859 
7860 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
7861 {
7862 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7863 	struct net_device *dev = info->user_ptr[1];
7864 	u8 *dst = NULL;
7865 	u8 *next_hop = NULL;
7866 
7867 	if (!info->attrs[NL80211_ATTR_MAC])
7868 		return -EINVAL;
7869 
7870 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7871 		return -EINVAL;
7872 
7873 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7874 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7875 
7876 	if (!rdev->ops->change_mpath)
7877 		return -EOPNOTSUPP;
7878 
7879 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7880 		return -EOPNOTSUPP;
7881 
7882 	return rdev_change_mpath(rdev, dev, dst, next_hop);
7883 }
7884 
7885 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
7886 {
7887 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7888 	struct net_device *dev = info->user_ptr[1];
7889 	u8 *dst = NULL;
7890 	u8 *next_hop = NULL;
7891 
7892 	if (!info->attrs[NL80211_ATTR_MAC])
7893 		return -EINVAL;
7894 
7895 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7896 		return -EINVAL;
7897 
7898 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7899 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7900 
7901 	if (!rdev->ops->add_mpath)
7902 		return -EOPNOTSUPP;
7903 
7904 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7905 		return -EOPNOTSUPP;
7906 
7907 	return rdev_add_mpath(rdev, dev, dst, next_hop);
7908 }
7909 
7910 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
7911 {
7912 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7913 	struct net_device *dev = info->user_ptr[1];
7914 	u8 *dst = NULL;
7915 
7916 	if (info->attrs[NL80211_ATTR_MAC])
7917 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7918 
7919 	if (!rdev->ops->del_mpath)
7920 		return -EOPNOTSUPP;
7921 
7922 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7923 		return -EOPNOTSUPP;
7924 
7925 	return rdev_del_mpath(rdev, dev, dst);
7926 }
7927 
7928 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
7929 {
7930 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7931 	int err;
7932 	struct net_device *dev = info->user_ptr[1];
7933 	struct mpath_info pinfo;
7934 	struct sk_buff *msg;
7935 	u8 *dst = NULL;
7936 	u8 mpp[ETH_ALEN];
7937 
7938 	memset(&pinfo, 0, sizeof(pinfo));
7939 
7940 	if (!info->attrs[NL80211_ATTR_MAC])
7941 		return -EINVAL;
7942 
7943 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7944 
7945 	if (!rdev->ops->get_mpp)
7946 		return -EOPNOTSUPP;
7947 
7948 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7949 		return -EOPNOTSUPP;
7950 
7951 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
7952 	if (err)
7953 		return err;
7954 
7955 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7956 	if (!msg)
7957 		return -ENOMEM;
7958 
7959 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7960 			       dev, dst, mpp, &pinfo) < 0) {
7961 		nlmsg_free(msg);
7962 		return -ENOBUFS;
7963 	}
7964 
7965 	return genlmsg_reply(msg, info);
7966 }
7967 
7968 static int nl80211_dump_mpp(struct sk_buff *skb,
7969 			    struct netlink_callback *cb)
7970 {
7971 	struct mpath_info pinfo;
7972 	struct cfg80211_registered_device *rdev;
7973 	struct wireless_dev *wdev;
7974 	u8 dst[ETH_ALEN];
7975 	u8 mpp[ETH_ALEN];
7976 	int path_idx = cb->args[2];
7977 	int err;
7978 
7979 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7980 	if (err)
7981 		return err;
7982 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
7983 	__acquire(&rdev->wiphy.mtx);
7984 
7985 	if (!rdev->ops->dump_mpp) {
7986 		err = -EOPNOTSUPP;
7987 		goto out_err;
7988 	}
7989 
7990 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7991 		err = -EOPNOTSUPP;
7992 		goto out_err;
7993 	}
7994 
7995 	while (1) {
7996 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
7997 				    mpp, &pinfo);
7998 		if (err == -ENOENT)
7999 			break;
8000 		if (err)
8001 			goto out_err;
8002 
8003 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
8004 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
8005 				       wdev->netdev, dst, mpp,
8006 				       &pinfo) < 0)
8007 			goto out;
8008 
8009 		path_idx++;
8010 	}
8011 
8012  out:
8013 	cb->args[2] = path_idx;
8014 	err = skb->len;
8015  out_err:
8016 	wiphy_unlock(&rdev->wiphy);
8017 	return err;
8018 }
8019 
8020 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
8021 {
8022 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8023 	struct net_device *dev = info->user_ptr[1];
8024 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8025 	struct bss_parameters params;
8026 	int err;
8027 
8028 	memset(&params, 0, sizeof(params));
8029 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
8030 	/* default to not changing parameters */
8031 	params.use_cts_prot = -1;
8032 	params.use_short_preamble = -1;
8033 	params.use_short_slot_time = -1;
8034 	params.ap_isolate = -1;
8035 	params.ht_opmode = -1;
8036 	params.p2p_ctwindow = -1;
8037 	params.p2p_opp_ps = -1;
8038 
8039 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
8040 		params.use_cts_prot =
8041 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
8042 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
8043 		params.use_short_preamble =
8044 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
8045 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
8046 		params.use_short_slot_time =
8047 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
8048 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8049 		params.basic_rates =
8050 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8051 		params.basic_rates_len =
8052 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8053 	}
8054 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
8055 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
8056 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
8057 		params.ht_opmode =
8058 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
8059 
8060 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
8061 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8062 			return -EINVAL;
8063 		params.p2p_ctwindow =
8064 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
8065 		if (params.p2p_ctwindow != 0 &&
8066 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
8067 			return -EINVAL;
8068 	}
8069 
8070 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
8071 		u8 tmp;
8072 
8073 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8074 			return -EINVAL;
8075 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
8076 		params.p2p_opp_ps = tmp;
8077 		if (params.p2p_opp_ps &&
8078 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
8079 			return -EINVAL;
8080 	}
8081 
8082 	if (!rdev->ops->change_bss)
8083 		return -EOPNOTSUPP;
8084 
8085 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
8086 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
8087 		return -EOPNOTSUPP;
8088 
8089 	wdev_lock(wdev);
8090 	err = rdev_change_bss(rdev, dev, &params);
8091 	wdev_unlock(wdev);
8092 
8093 	return err;
8094 }
8095 
8096 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
8097 {
8098 	char *data = NULL;
8099 	bool is_indoor;
8100 	enum nl80211_user_reg_hint_type user_reg_hint_type;
8101 	u32 owner_nlportid;
8102 
8103 	/*
8104 	 * You should only get this when cfg80211 hasn't yet initialized
8105 	 * completely when built-in to the kernel right between the time
8106 	 * window between nl80211_init() and regulatory_init(), if that is
8107 	 * even possible.
8108 	 */
8109 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
8110 		return -EINPROGRESS;
8111 
8112 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
8113 		user_reg_hint_type =
8114 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
8115 	else
8116 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
8117 
8118 	switch (user_reg_hint_type) {
8119 	case NL80211_USER_REG_HINT_USER:
8120 	case NL80211_USER_REG_HINT_CELL_BASE:
8121 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8122 			return -EINVAL;
8123 
8124 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8125 		return regulatory_hint_user(data, user_reg_hint_type);
8126 	case NL80211_USER_REG_HINT_INDOOR:
8127 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8128 			owner_nlportid = info->snd_portid;
8129 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
8130 		} else {
8131 			owner_nlportid = 0;
8132 			is_indoor = true;
8133 		}
8134 
8135 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
8136 	default:
8137 		return -EINVAL;
8138 	}
8139 }
8140 
8141 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
8142 {
8143 	return reg_reload_regdb();
8144 }
8145 
8146 static int nl80211_get_mesh_config(struct sk_buff *skb,
8147 				   struct genl_info *info)
8148 {
8149 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8150 	struct net_device *dev = info->user_ptr[1];
8151 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8152 	struct mesh_config cur_params;
8153 	int err = 0;
8154 	void *hdr;
8155 	struct nlattr *pinfoattr;
8156 	struct sk_buff *msg;
8157 
8158 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8159 		return -EOPNOTSUPP;
8160 
8161 	if (!rdev->ops->get_mesh_config)
8162 		return -EOPNOTSUPP;
8163 
8164 	wdev_lock(wdev);
8165 	/* If not connected, get default parameters */
8166 	if (!wdev->u.mesh.id_len)
8167 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
8168 	else
8169 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
8170 	wdev_unlock(wdev);
8171 
8172 	if (err)
8173 		return err;
8174 
8175 	/* Draw up a netlink message to send back */
8176 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8177 	if (!msg)
8178 		return -ENOMEM;
8179 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8180 			     NL80211_CMD_GET_MESH_CONFIG);
8181 	if (!hdr)
8182 		goto out;
8183 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
8184 	if (!pinfoattr)
8185 		goto nla_put_failure;
8186 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8187 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
8188 			cur_params.dot11MeshRetryTimeout) ||
8189 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
8190 			cur_params.dot11MeshConfirmTimeout) ||
8191 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
8192 			cur_params.dot11MeshHoldingTimeout) ||
8193 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
8194 			cur_params.dot11MeshMaxPeerLinks) ||
8195 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
8196 		       cur_params.dot11MeshMaxRetries) ||
8197 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
8198 		       cur_params.dot11MeshTTL) ||
8199 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
8200 		       cur_params.element_ttl) ||
8201 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8202 		       cur_params.auto_open_plinks) ||
8203 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8204 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
8205 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8206 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
8207 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
8208 			cur_params.path_refresh_time) ||
8209 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8210 			cur_params.min_discovery_timeout) ||
8211 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8212 			cur_params.dot11MeshHWMPactivePathTimeout) ||
8213 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8214 			cur_params.dot11MeshHWMPpreqMinInterval) ||
8215 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8216 			cur_params.dot11MeshHWMPperrMinInterval) ||
8217 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8218 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
8219 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
8220 		       cur_params.dot11MeshHWMPRootMode) ||
8221 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8222 			cur_params.dot11MeshHWMPRannInterval) ||
8223 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8224 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
8225 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
8226 		       cur_params.dot11MeshForwarding) ||
8227 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
8228 			cur_params.rssi_threshold) ||
8229 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
8230 			cur_params.ht_opmode) ||
8231 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8232 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
8233 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8234 			cur_params.dot11MeshHWMProotInterval) ||
8235 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8236 			cur_params.dot11MeshHWMPconfirmationInterval) ||
8237 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
8238 			cur_params.power_mode) ||
8239 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
8240 			cur_params.dot11MeshAwakeWindowDuration) ||
8241 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8242 			cur_params.plink_timeout) ||
8243 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
8244 		       cur_params.dot11MeshConnectedToMeshGate) ||
8245 	    nla_put_u8(msg, NL80211_MESHCONF_NOLEARN,
8246 		       cur_params.dot11MeshNolearn) ||
8247 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_AS,
8248 		       cur_params.dot11MeshConnectedToAuthServer))
8249 		goto nla_put_failure;
8250 	nla_nest_end(msg, pinfoattr);
8251 	genlmsg_end(msg, hdr);
8252 	return genlmsg_reply(msg, info);
8253 
8254  nla_put_failure:
8255  out:
8256 	nlmsg_free(msg);
8257 	return -ENOBUFS;
8258 }
8259 
8260 static const struct nla_policy
8261 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
8262 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
8263 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8264 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
8265 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8266 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
8267 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
8268 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
8269 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
8270 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
8271 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8272 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8273 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
8274 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
8275 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
8276 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
8277 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
8278 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
8279 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
8280 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
8281 		NLA_POLICY_MIN(NLA_U16, 1),
8282 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
8283 		NLA_POLICY_MIN(NLA_U16, 1),
8284 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
8285 		NLA_POLICY_MIN(NLA_U16, 1),
8286 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
8287 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
8288 		NLA_POLICY_MIN(NLA_U16, 1),
8289 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
8290 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
8291 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
8292 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
8293 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
8294 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
8295 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
8296 		NLA_POLICY_MIN(NLA_U16, 1),
8297 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
8298 		NLA_POLICY_MIN(NLA_U16, 1),
8299 	[NL80211_MESHCONF_POWER_MODE] =
8300 		NLA_POLICY_RANGE(NLA_U32,
8301 				 NL80211_MESH_POWER_ACTIVE,
8302 				 NL80211_MESH_POWER_MAX),
8303 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
8304 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
8305 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8306 	[NL80211_MESHCONF_NOLEARN] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8307 	[NL80211_MESHCONF_CONNECTED_TO_AS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8308 };
8309 
8310 static const struct nla_policy
8311 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
8312 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
8313 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
8314 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
8315 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
8316 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
8317 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
8318 	[NL80211_MESH_SETUP_IE] =
8319 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
8320 				       IEEE80211_MAX_DATA_LEN),
8321 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
8322 };
8323 
8324 static int nl80211_parse_mesh_config(struct genl_info *info,
8325 				     struct mesh_config *cfg,
8326 				     u32 *mask_out)
8327 {
8328 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
8329 	u32 mask = 0;
8330 	u16 ht_opmode;
8331 
8332 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
8333 do {									\
8334 	if (tb[attr]) {							\
8335 		cfg->param = fn(tb[attr]);				\
8336 		mask |= BIT((attr) - 1);				\
8337 	}								\
8338 } while (0)
8339 
8340 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
8341 		return -EINVAL;
8342 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
8343 		return -EINVAL;
8344 
8345 	/* This makes sure that there aren't more than 32 mesh config
8346 	 * parameters (otherwise our bitfield scheme would not work.) */
8347 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
8348 
8349 	/* Fill in the params struct */
8350 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
8351 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
8352 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
8353 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
8354 				  nla_get_u16);
8355 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
8356 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
8357 				  nla_get_u16);
8358 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
8359 				  NL80211_MESHCONF_MAX_PEER_LINKS,
8360 				  nla_get_u16);
8361 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
8362 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
8363 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
8364 				  NL80211_MESHCONF_TTL, nla_get_u8);
8365 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
8366 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
8367 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
8368 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8369 				  nla_get_u8);
8370 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
8371 				  mask,
8372 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8373 				  nla_get_u32);
8374 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
8375 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8376 				  nla_get_u8);
8377 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
8378 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
8379 				  nla_get_u32);
8380 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
8381 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
8382 		return -EINVAL;
8383 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
8384 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8385 				  nla_get_u16);
8386 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
8387 				  mask,
8388 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8389 				  nla_get_u32);
8390 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
8391 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
8392 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
8393 		return -EINVAL;
8394 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
8395 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8396 				  nla_get_u16);
8397 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
8398 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8399 				  nla_get_u16);
8400 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8401 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
8402 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8403 				  nla_get_u16);
8404 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
8405 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
8406 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
8407 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8408 				  nla_get_u16);
8409 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
8410 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8411 				  nla_get_u8);
8412 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
8413 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
8414 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
8415 				  NL80211_MESHCONF_RSSI_THRESHOLD,
8416 				  nla_get_s32);
8417 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
8418 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
8419 				  nla_get_u8);
8420 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToAuthServer, mask,
8421 				  NL80211_MESHCONF_CONNECTED_TO_AS,
8422 				  nla_get_u8);
8423 	/*
8424 	 * Check HT operation mode based on
8425 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
8426 	 */
8427 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
8428 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
8429 
8430 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
8431 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
8432 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
8433 			return -EINVAL;
8434 
8435 		/* NON_HT_STA bit is reserved, but some programs set it */
8436 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
8437 
8438 		cfg->ht_opmode = ht_opmode;
8439 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
8440 	}
8441 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8442 				  dot11MeshHWMPactivePathToRootTimeout, mask,
8443 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8444 				  nla_get_u32);
8445 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
8446 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
8447 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
8448 		return -EINVAL;
8449 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
8450 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8451 				  nla_get_u16);
8452 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
8453 				  mask,
8454 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8455 				  nla_get_u16);
8456 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
8457 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
8458 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
8459 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
8460 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
8461 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
8462 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNolearn, mask,
8463 				  NL80211_MESHCONF_NOLEARN, nla_get_u8);
8464 	if (mask_out)
8465 		*mask_out = mask;
8466 
8467 	return 0;
8468 
8469 #undef FILL_IN_MESH_PARAM_IF_SET
8470 }
8471 
8472 static int nl80211_parse_mesh_setup(struct genl_info *info,
8473 				     struct mesh_setup *setup)
8474 {
8475 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8476 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
8477 
8478 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
8479 		return -EINVAL;
8480 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
8481 		return -EINVAL;
8482 
8483 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
8484 		setup->sync_method =
8485 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
8486 		 IEEE80211_SYNC_METHOD_VENDOR :
8487 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
8488 
8489 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
8490 		setup->path_sel_proto =
8491 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
8492 		 IEEE80211_PATH_PROTOCOL_VENDOR :
8493 		 IEEE80211_PATH_PROTOCOL_HWMP;
8494 
8495 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
8496 		setup->path_metric =
8497 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
8498 		 IEEE80211_PATH_METRIC_VENDOR :
8499 		 IEEE80211_PATH_METRIC_AIRTIME;
8500 
8501 	if (tb[NL80211_MESH_SETUP_IE]) {
8502 		struct nlattr *ieattr =
8503 			tb[NL80211_MESH_SETUP_IE];
8504 		setup->ie = nla_data(ieattr);
8505 		setup->ie_len = nla_len(ieattr);
8506 	}
8507 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
8508 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
8509 		return -EINVAL;
8510 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
8511 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
8512 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
8513 	if (setup->is_secure)
8514 		setup->user_mpm = true;
8515 
8516 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
8517 		if (!setup->user_mpm)
8518 			return -EINVAL;
8519 		setup->auth_id =
8520 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
8521 	}
8522 
8523 	return 0;
8524 }
8525 
8526 static int nl80211_update_mesh_config(struct sk_buff *skb,
8527 				      struct genl_info *info)
8528 {
8529 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8530 	struct net_device *dev = info->user_ptr[1];
8531 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8532 	struct mesh_config cfg = {};
8533 	u32 mask;
8534 	int err;
8535 
8536 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8537 		return -EOPNOTSUPP;
8538 
8539 	if (!rdev->ops->update_mesh_config)
8540 		return -EOPNOTSUPP;
8541 
8542 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
8543 	if (err)
8544 		return err;
8545 
8546 	wdev_lock(wdev);
8547 	if (!wdev->u.mesh.id_len)
8548 		err = -ENOLINK;
8549 
8550 	if (!err)
8551 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
8552 
8553 	wdev_unlock(wdev);
8554 
8555 	return err;
8556 }
8557 
8558 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
8559 			      struct sk_buff *msg)
8560 {
8561 	struct nlattr *nl_reg_rules;
8562 	unsigned int i;
8563 
8564 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
8565 	    (regdom->dfs_region &&
8566 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
8567 		goto nla_put_failure;
8568 
8569 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
8570 	if (!nl_reg_rules)
8571 		goto nla_put_failure;
8572 
8573 	for (i = 0; i < regdom->n_reg_rules; i++) {
8574 		struct nlattr *nl_reg_rule;
8575 		const struct ieee80211_reg_rule *reg_rule;
8576 		const struct ieee80211_freq_range *freq_range;
8577 		const struct ieee80211_power_rule *power_rule;
8578 		unsigned int max_bandwidth_khz;
8579 
8580 		reg_rule = &regdom->reg_rules[i];
8581 		freq_range = &reg_rule->freq_range;
8582 		power_rule = &reg_rule->power_rule;
8583 
8584 		nl_reg_rule = nla_nest_start_noflag(msg, i);
8585 		if (!nl_reg_rule)
8586 			goto nla_put_failure;
8587 
8588 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
8589 		if (!max_bandwidth_khz)
8590 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
8591 								  reg_rule);
8592 
8593 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
8594 				reg_rule->flags) ||
8595 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
8596 				freq_range->start_freq_khz) ||
8597 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
8598 				freq_range->end_freq_khz) ||
8599 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
8600 				max_bandwidth_khz) ||
8601 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
8602 				power_rule->max_antenna_gain) ||
8603 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
8604 				power_rule->max_eirp) ||
8605 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
8606 				reg_rule->dfs_cac_ms))
8607 			goto nla_put_failure;
8608 
8609 		nla_nest_end(msg, nl_reg_rule);
8610 	}
8611 
8612 	nla_nest_end(msg, nl_reg_rules);
8613 	return 0;
8614 
8615 nla_put_failure:
8616 	return -EMSGSIZE;
8617 }
8618 
8619 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
8620 {
8621 	const struct ieee80211_regdomain *regdom = NULL;
8622 	struct cfg80211_registered_device *rdev;
8623 	struct wiphy *wiphy = NULL;
8624 	struct sk_buff *msg;
8625 	int err = -EMSGSIZE;
8626 	void *hdr;
8627 
8628 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8629 	if (!msg)
8630 		return -ENOBUFS;
8631 
8632 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8633 			     NL80211_CMD_GET_REG);
8634 	if (!hdr)
8635 		goto put_failure;
8636 
8637 	rtnl_lock();
8638 
8639 	if (info->attrs[NL80211_ATTR_WIPHY]) {
8640 		bool self_managed;
8641 
8642 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
8643 		if (IS_ERR(rdev)) {
8644 			err = PTR_ERR(rdev);
8645 			goto nla_put_failure;
8646 		}
8647 
8648 		wiphy = &rdev->wiphy;
8649 		self_managed = wiphy->regulatory_flags &
8650 			       REGULATORY_WIPHY_SELF_MANAGED;
8651 
8652 		rcu_read_lock();
8653 
8654 		regdom = get_wiphy_regdom(wiphy);
8655 
8656 		/* a self-managed-reg device must have a private regdom */
8657 		if (WARN_ON(!regdom && self_managed)) {
8658 			err = -EINVAL;
8659 			goto nla_put_failure_rcu;
8660 		}
8661 
8662 		if (regdom &&
8663 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8664 			goto nla_put_failure_rcu;
8665 	} else {
8666 		rcu_read_lock();
8667 	}
8668 
8669 	if (!wiphy && reg_last_request_cell_base() &&
8670 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8671 			NL80211_USER_REG_HINT_CELL_BASE))
8672 		goto nla_put_failure_rcu;
8673 
8674 	if (!regdom)
8675 		regdom = rcu_dereference(cfg80211_regdomain);
8676 
8677 	if (nl80211_put_regdom(regdom, msg))
8678 		goto nla_put_failure_rcu;
8679 
8680 	rcu_read_unlock();
8681 
8682 	genlmsg_end(msg, hdr);
8683 	rtnl_unlock();
8684 	return genlmsg_reply(msg, info);
8685 
8686 nla_put_failure_rcu:
8687 	rcu_read_unlock();
8688 nla_put_failure:
8689 	rtnl_unlock();
8690 put_failure:
8691 	nlmsg_free(msg);
8692 	return err;
8693 }
8694 
8695 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
8696 			       u32 seq, int flags, struct wiphy *wiphy,
8697 			       const struct ieee80211_regdomain *regdom)
8698 {
8699 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8700 				   NL80211_CMD_GET_REG);
8701 
8702 	if (!hdr)
8703 		return -1;
8704 
8705 	genl_dump_check_consistent(cb, hdr);
8706 
8707 	if (nl80211_put_regdom(regdom, msg))
8708 		goto nla_put_failure;
8709 
8710 	if (!wiphy && reg_last_request_cell_base() &&
8711 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8712 			NL80211_USER_REG_HINT_CELL_BASE))
8713 		goto nla_put_failure;
8714 
8715 	if (wiphy &&
8716 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8717 		goto nla_put_failure;
8718 
8719 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
8720 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
8721 		goto nla_put_failure;
8722 
8723 	genlmsg_end(msg, hdr);
8724 	return 0;
8725 
8726 nla_put_failure:
8727 	genlmsg_cancel(msg, hdr);
8728 	return -EMSGSIZE;
8729 }
8730 
8731 static int nl80211_get_reg_dump(struct sk_buff *skb,
8732 				struct netlink_callback *cb)
8733 {
8734 	const struct ieee80211_regdomain *regdom = NULL;
8735 	struct cfg80211_registered_device *rdev;
8736 	int err, reg_idx, start = cb->args[2];
8737 
8738 	rcu_read_lock();
8739 
8740 	if (cfg80211_regdomain && start == 0) {
8741 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8742 					  NLM_F_MULTI, NULL,
8743 					  rcu_dereference(cfg80211_regdomain));
8744 		if (err < 0)
8745 			goto out_err;
8746 	}
8747 
8748 	/* the global regdom is idx 0 */
8749 	reg_idx = 1;
8750 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
8751 		regdom = get_wiphy_regdom(&rdev->wiphy);
8752 		if (!regdom)
8753 			continue;
8754 
8755 		if (++reg_idx <= start)
8756 			continue;
8757 
8758 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8759 					  NLM_F_MULTI, &rdev->wiphy, regdom);
8760 		if (err < 0) {
8761 			reg_idx--;
8762 			break;
8763 		}
8764 	}
8765 
8766 	cb->args[2] = reg_idx;
8767 	err = skb->len;
8768 out_err:
8769 	rcu_read_unlock();
8770 	return err;
8771 }
8772 
8773 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
8774 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
8775 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
8776 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
8777 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
8778 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
8779 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
8780 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
8781 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
8782 };
8783 
8784 static int parse_reg_rule(struct nlattr *tb[],
8785 	struct ieee80211_reg_rule *reg_rule)
8786 {
8787 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
8788 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
8789 
8790 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
8791 		return -EINVAL;
8792 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
8793 		return -EINVAL;
8794 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
8795 		return -EINVAL;
8796 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
8797 		return -EINVAL;
8798 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
8799 		return -EINVAL;
8800 
8801 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
8802 
8803 	freq_range->start_freq_khz =
8804 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
8805 	freq_range->end_freq_khz =
8806 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
8807 	freq_range->max_bandwidth_khz =
8808 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
8809 
8810 	power_rule->max_eirp =
8811 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
8812 
8813 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
8814 		power_rule->max_antenna_gain =
8815 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
8816 
8817 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
8818 		reg_rule->dfs_cac_ms =
8819 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
8820 
8821 	return 0;
8822 }
8823 
8824 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
8825 {
8826 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
8827 	struct nlattr *nl_reg_rule;
8828 	char *alpha2;
8829 	int rem_reg_rules, r;
8830 	u32 num_rules = 0, rule_idx = 0;
8831 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
8832 	struct ieee80211_regdomain *rd;
8833 
8834 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8835 		return -EINVAL;
8836 
8837 	if (!info->attrs[NL80211_ATTR_REG_RULES])
8838 		return -EINVAL;
8839 
8840 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8841 
8842 	if (info->attrs[NL80211_ATTR_DFS_REGION])
8843 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
8844 
8845 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8846 			    rem_reg_rules) {
8847 		num_rules++;
8848 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
8849 			return -EINVAL;
8850 	}
8851 
8852 	rtnl_lock();
8853 	if (!reg_is_valid_request(alpha2)) {
8854 		r = -EINVAL;
8855 		goto out;
8856 	}
8857 
8858 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
8859 	if (!rd) {
8860 		r = -ENOMEM;
8861 		goto out;
8862 	}
8863 
8864 	rd->n_reg_rules = num_rules;
8865 	rd->alpha2[0] = alpha2[0];
8866 	rd->alpha2[1] = alpha2[1];
8867 
8868 	/*
8869 	 * Disable DFS master mode if the DFS region was
8870 	 * not supported or known on this kernel.
8871 	 */
8872 	if (reg_supported_dfs_region(dfs_region))
8873 		rd->dfs_region = dfs_region;
8874 
8875 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8876 			    rem_reg_rules) {
8877 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
8878 						nl_reg_rule, reg_rule_policy,
8879 						info->extack);
8880 		if (r)
8881 			goto bad_reg;
8882 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
8883 		if (r)
8884 			goto bad_reg;
8885 
8886 		rule_idx++;
8887 
8888 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
8889 			r = -EINVAL;
8890 			goto bad_reg;
8891 		}
8892 	}
8893 
8894 	r = set_regdom(rd, REGD_SOURCE_CRDA);
8895 	/* set_regdom takes ownership of rd */
8896 	rd = NULL;
8897  bad_reg:
8898 	kfree(rd);
8899  out:
8900 	rtnl_unlock();
8901 	return r;
8902 }
8903 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
8904 
8905 static int validate_scan_freqs(struct nlattr *freqs)
8906 {
8907 	struct nlattr *attr1, *attr2;
8908 	int n_channels = 0, tmp1, tmp2;
8909 
8910 	nla_for_each_nested(attr1, freqs, tmp1)
8911 		if (nla_len(attr1) != sizeof(u32))
8912 			return 0;
8913 
8914 	nla_for_each_nested(attr1, freqs, tmp1) {
8915 		n_channels++;
8916 		/*
8917 		 * Some hardware has a limited channel list for
8918 		 * scanning, and it is pretty much nonsensical
8919 		 * to scan for a channel twice, so disallow that
8920 		 * and don't require drivers to check that the
8921 		 * channel list they get isn't longer than what
8922 		 * they can scan, as long as they can scan all
8923 		 * the channels they registered at once.
8924 		 */
8925 		nla_for_each_nested(attr2, freqs, tmp2)
8926 			if (attr1 != attr2 &&
8927 			    nla_get_u32(attr1) == nla_get_u32(attr2))
8928 				return 0;
8929 	}
8930 
8931 	return n_channels;
8932 }
8933 
8934 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
8935 {
8936 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
8937 }
8938 
8939 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
8940 			    struct cfg80211_bss_selection *bss_select)
8941 {
8942 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
8943 	struct nlattr *nest;
8944 	int err;
8945 	bool found = false;
8946 	int i;
8947 
8948 	/* only process one nested attribute */
8949 	nest = nla_data(nla);
8950 	if (!nla_ok(nest, nla_len(nest)))
8951 		return -EINVAL;
8952 
8953 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
8954 					  nest, nl80211_bss_select_policy,
8955 					  NULL);
8956 	if (err)
8957 		return err;
8958 
8959 	/* only one attribute may be given */
8960 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
8961 		if (attr[i]) {
8962 			if (found)
8963 				return -EINVAL;
8964 			found = true;
8965 		}
8966 	}
8967 
8968 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
8969 
8970 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
8971 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
8972 
8973 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
8974 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
8975 		bss_select->param.band_pref =
8976 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
8977 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
8978 			return -EINVAL;
8979 	}
8980 
8981 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
8982 		struct nl80211_bss_select_rssi_adjust *adj_param;
8983 
8984 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
8985 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
8986 		bss_select->param.adjust.band = adj_param->band;
8987 		bss_select->param.adjust.delta = adj_param->delta;
8988 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
8989 			return -EINVAL;
8990 	}
8991 
8992 	/* user-space did not provide behaviour attribute */
8993 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
8994 		return -EINVAL;
8995 
8996 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
8997 		return -EINVAL;
8998 
8999 	return 0;
9000 }
9001 
9002 int nl80211_parse_random_mac(struct nlattr **attrs,
9003 			     u8 *mac_addr, u8 *mac_addr_mask)
9004 {
9005 	int i;
9006 
9007 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
9008 		eth_zero_addr(mac_addr);
9009 		eth_zero_addr(mac_addr_mask);
9010 		mac_addr[0] = 0x2;
9011 		mac_addr_mask[0] = 0x3;
9012 
9013 		return 0;
9014 	}
9015 
9016 	/* need both or none */
9017 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
9018 		return -EINVAL;
9019 
9020 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
9021 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
9022 
9023 	/* don't allow or configure an mcast address */
9024 	if (!is_multicast_ether_addr(mac_addr_mask) ||
9025 	    is_multicast_ether_addr(mac_addr))
9026 		return -EINVAL;
9027 
9028 	/*
9029 	 * allow users to pass a MAC address that has bits set outside
9030 	 * of the mask, but don't bother drivers with having to deal
9031 	 * with such bits
9032 	 */
9033 	for (i = 0; i < ETH_ALEN; i++)
9034 		mac_addr[i] &= mac_addr_mask[i];
9035 
9036 	return 0;
9037 }
9038 
9039 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev,
9040 					      struct ieee80211_channel *chan)
9041 {
9042 	unsigned int link_id;
9043 	bool all_ok = true;
9044 
9045 	ASSERT_WDEV_LOCK(wdev);
9046 
9047 	if (!cfg80211_beaconing_iface_active(wdev))
9048 		return true;
9049 
9050 	/*
9051 	 * FIXME: check if we have a free HW resource/link for chan
9052 	 *
9053 	 * This, as well as the FIXME below, requires knowing the link
9054 	 * capabilities of the hardware.
9055 	 */
9056 
9057 	/* we cannot leave radar channels */
9058 	for_each_valid_link(wdev, link_id) {
9059 		struct cfg80211_chan_def *chandef;
9060 
9061 		chandef = wdev_chandef(wdev, link_id);
9062 		if (!chandef || !chandef->chan)
9063 			continue;
9064 
9065 		/*
9066 		 * FIXME: don't require all_ok, but rather check only the
9067 		 *	  correct HW resource/link onto which 'chan' falls,
9068 		 *	  as only that link leaves the channel for doing
9069 		 *	  the off-channel operation.
9070 		 */
9071 
9072 		if (chandef->chan->flags & IEEE80211_CHAN_RADAR)
9073 			all_ok = false;
9074 	}
9075 
9076 	if (all_ok)
9077 		return true;
9078 
9079 	return regulatory_pre_cac_allowed(wdev->wiphy);
9080 }
9081 
9082 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
9083 				    enum nl80211_ext_feature_index feat)
9084 {
9085 	if (!(flags & flag))
9086 		return true;
9087 	if (wiphy_ext_feature_isset(wiphy, feat))
9088 		return true;
9089 	return false;
9090 }
9091 
9092 static int
9093 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
9094 			 void *request, struct nlattr **attrs,
9095 			 bool is_sched_scan)
9096 {
9097 	u8 *mac_addr, *mac_addr_mask;
9098 	u32 *flags;
9099 	enum nl80211_feature_flags randomness_flag;
9100 
9101 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
9102 		return 0;
9103 
9104 	if (is_sched_scan) {
9105 		struct cfg80211_sched_scan_request *req = request;
9106 
9107 		randomness_flag = wdev ?
9108 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
9109 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9110 		flags = &req->flags;
9111 		mac_addr = req->mac_addr;
9112 		mac_addr_mask = req->mac_addr_mask;
9113 	} else {
9114 		struct cfg80211_scan_request *req = request;
9115 
9116 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
9117 		flags = &req->flags;
9118 		mac_addr = req->mac_addr;
9119 		mac_addr_mask = req->mac_addr_mask;
9120 	}
9121 
9122 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
9123 
9124 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
9125 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
9126 	    !nl80211_check_scan_feat(wiphy, *flags,
9127 				     NL80211_SCAN_FLAG_LOW_SPAN,
9128 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
9129 	    !nl80211_check_scan_feat(wiphy, *flags,
9130 				     NL80211_SCAN_FLAG_LOW_POWER,
9131 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
9132 	    !nl80211_check_scan_feat(wiphy, *flags,
9133 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
9134 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
9135 	    !nl80211_check_scan_feat(wiphy, *flags,
9136 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
9137 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
9138 	    !nl80211_check_scan_feat(wiphy, *flags,
9139 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
9140 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
9141 	    !nl80211_check_scan_feat(wiphy, *flags,
9142 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
9143 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
9144 	    !nl80211_check_scan_feat(wiphy, *flags,
9145 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
9146 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
9147 	    !nl80211_check_scan_feat(wiphy, *flags,
9148 				     NL80211_SCAN_FLAG_RANDOM_SN,
9149 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
9150 	    !nl80211_check_scan_feat(wiphy, *flags,
9151 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
9152 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
9153 		return -EOPNOTSUPP;
9154 
9155 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
9156 		int err;
9157 
9158 		if (!(wiphy->features & randomness_flag) ||
9159 		    (wdev && wdev->connected))
9160 			return -EOPNOTSUPP;
9161 
9162 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
9163 		if (err)
9164 			return err;
9165 	}
9166 
9167 	return 0;
9168 }
9169 
9170 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
9171 {
9172 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9173 	struct wireless_dev *wdev = info->user_ptr[1];
9174 	struct cfg80211_scan_request *request;
9175 	struct nlattr *scan_freqs = NULL;
9176 	bool scan_freqs_khz = false;
9177 	struct nlattr *attr;
9178 	struct wiphy *wiphy;
9179 	int err, tmp, n_ssids = 0, n_channels, i;
9180 	size_t ie_len, size;
9181 	size_t ssids_offset, ie_offset;
9182 
9183 	wiphy = &rdev->wiphy;
9184 
9185 	if (wdev->iftype == NL80211_IFTYPE_NAN)
9186 		return -EOPNOTSUPP;
9187 
9188 	if (!rdev->ops->scan)
9189 		return -EOPNOTSUPP;
9190 
9191 	if (rdev->scan_req || rdev->scan_msg)
9192 		return -EBUSY;
9193 
9194 	if (info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ]) {
9195 		if (!wiphy_ext_feature_isset(wiphy,
9196 					     NL80211_EXT_FEATURE_SCAN_FREQ_KHZ))
9197 			return -EOPNOTSUPP;
9198 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ];
9199 		scan_freqs_khz = true;
9200 	} else if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES])
9201 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQUENCIES];
9202 
9203 	if (scan_freqs) {
9204 		n_channels = validate_scan_freqs(scan_freqs);
9205 		if (!n_channels)
9206 			return -EINVAL;
9207 	} else {
9208 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9209 	}
9210 
9211 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
9212 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
9213 			n_ssids++;
9214 
9215 	if (n_ssids > wiphy->max_scan_ssids)
9216 		return -EINVAL;
9217 
9218 	if (info->attrs[NL80211_ATTR_IE])
9219 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9220 	else
9221 		ie_len = 0;
9222 
9223 	if (ie_len > wiphy->max_scan_ie_len)
9224 		return -EINVAL;
9225 
9226 	size = struct_size(request, channels, n_channels);
9227 	ssids_offset = size;
9228 	size = size_add(size, array_size(sizeof(*request->ssids), n_ssids));
9229 	ie_offset = size;
9230 	size = size_add(size, ie_len);
9231 	request = kzalloc(size, GFP_KERNEL);
9232 	if (!request)
9233 		return -ENOMEM;
9234 	request->n_channels = n_channels;
9235 
9236 	if (n_ssids)
9237 		request->ssids = (void *)request + ssids_offset;
9238 	request->n_ssids = n_ssids;
9239 	if (ie_len)
9240 		request->ie = (void *)request + ie_offset;
9241 
9242 	i = 0;
9243 	if (scan_freqs) {
9244 		/* user specified, bail out if channel not found */
9245 		nla_for_each_nested(attr, scan_freqs, tmp) {
9246 			struct ieee80211_channel *chan;
9247 			int freq = nla_get_u32(attr);
9248 
9249 			if (!scan_freqs_khz)
9250 				freq = MHZ_TO_KHZ(freq);
9251 
9252 			chan = ieee80211_get_channel_khz(wiphy, freq);
9253 			if (!chan) {
9254 				err = -EINVAL;
9255 				goto out_free;
9256 			}
9257 
9258 			/* ignore disabled channels */
9259 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9260 				continue;
9261 
9262 			request->channels[i] = chan;
9263 			i++;
9264 		}
9265 	} else {
9266 		enum nl80211_band band;
9267 
9268 		/* all channels */
9269 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9270 			int j;
9271 
9272 			if (!wiphy->bands[band])
9273 				continue;
9274 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9275 				struct ieee80211_channel *chan;
9276 
9277 				chan = &wiphy->bands[band]->channels[j];
9278 
9279 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9280 					continue;
9281 
9282 				request->channels[i] = chan;
9283 				i++;
9284 			}
9285 		}
9286 	}
9287 
9288 	if (!i) {
9289 		err = -EINVAL;
9290 		goto out_free;
9291 	}
9292 
9293 	request->n_channels = i;
9294 
9295 	wdev_lock(wdev);
9296 	for (i = 0; i < request->n_channels; i++) {
9297 		struct ieee80211_channel *chan = request->channels[i];
9298 
9299 		/* if we can go off-channel to the target channel we're good */
9300 		if (cfg80211_off_channel_oper_allowed(wdev, chan))
9301 			continue;
9302 
9303 		if (!cfg80211_wdev_on_sub_chan(wdev, chan, true)) {
9304 			wdev_unlock(wdev);
9305 			err = -EBUSY;
9306 			goto out_free;
9307 		}
9308 	}
9309 	wdev_unlock(wdev);
9310 
9311 	i = 0;
9312 	if (n_ssids) {
9313 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
9314 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9315 				err = -EINVAL;
9316 				goto out_free;
9317 			}
9318 			request->ssids[i].ssid_len = nla_len(attr);
9319 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
9320 			i++;
9321 		}
9322 	}
9323 
9324 	if (info->attrs[NL80211_ATTR_IE]) {
9325 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9326 		memcpy((void *)request->ie,
9327 		       nla_data(info->attrs[NL80211_ATTR_IE]),
9328 		       request->ie_len);
9329 	}
9330 
9331 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9332 		if (wiphy->bands[i])
9333 			request->rates[i] =
9334 				(1 << wiphy->bands[i]->n_bitrates) - 1;
9335 
9336 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
9337 		nla_for_each_nested(attr,
9338 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
9339 				    tmp) {
9340 			enum nl80211_band band = nla_type(attr);
9341 
9342 			if (band < 0 || band >= NUM_NL80211_BANDS) {
9343 				err = -EINVAL;
9344 				goto out_free;
9345 			}
9346 
9347 			if (!wiphy->bands[band])
9348 				continue;
9349 
9350 			err = ieee80211_get_ratemask(wiphy->bands[band],
9351 						     nla_data(attr),
9352 						     nla_len(attr),
9353 						     &request->rates[band]);
9354 			if (err)
9355 				goto out_free;
9356 		}
9357 	}
9358 
9359 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
9360 		request->duration =
9361 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
9362 		request->duration_mandatory =
9363 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
9364 	}
9365 
9366 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
9367 				       false);
9368 	if (err)
9369 		goto out_free;
9370 
9371 	request->no_cck =
9372 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
9373 
9374 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
9375 	 * BSSID to scan for. This was problematic because that same attribute
9376 	 * was already used for another purpose (local random MAC address). The
9377 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
9378 	 * compatibility with older userspace components, also use the
9379 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
9380 	 * the specific BSSID use case instead of the random MAC address
9381 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
9382 	 */
9383 	if (info->attrs[NL80211_ATTR_BSSID])
9384 		memcpy(request->bssid,
9385 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
9386 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
9387 		 info->attrs[NL80211_ATTR_MAC])
9388 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
9389 		       ETH_ALEN);
9390 	else
9391 		eth_broadcast_addr(request->bssid);
9392 
9393 	request->wdev = wdev;
9394 	request->wiphy = &rdev->wiphy;
9395 	request->scan_start = jiffies;
9396 
9397 	rdev->scan_req = request;
9398 	err = cfg80211_scan(rdev);
9399 
9400 	if (err)
9401 		goto out_free;
9402 
9403 	nl80211_send_scan_start(rdev, wdev);
9404 	dev_hold(wdev->netdev);
9405 
9406 	return 0;
9407 
9408  out_free:
9409 	rdev->scan_req = NULL;
9410 	kfree(request);
9411 
9412 	return err;
9413 }
9414 
9415 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
9416 {
9417 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9418 	struct wireless_dev *wdev = info->user_ptr[1];
9419 
9420 	if (!rdev->ops->abort_scan)
9421 		return -EOPNOTSUPP;
9422 
9423 	if (rdev->scan_msg)
9424 		return 0;
9425 
9426 	if (!rdev->scan_req)
9427 		return -ENOENT;
9428 
9429 	rdev_abort_scan(rdev, wdev);
9430 	return 0;
9431 }
9432 
9433 static int
9434 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
9435 			       struct cfg80211_sched_scan_request *request,
9436 			       struct nlattr **attrs)
9437 {
9438 	int tmp, err, i = 0;
9439 	struct nlattr *attr;
9440 
9441 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9442 		u32 interval;
9443 
9444 		/*
9445 		 * If scan plans are not specified,
9446 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
9447 		 * case one scan plan will be set with the specified scan
9448 		 * interval and infinite number of iterations.
9449 		 */
9450 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
9451 		if (!interval)
9452 			return -EINVAL;
9453 
9454 		request->scan_plans[0].interval =
9455 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
9456 		if (!request->scan_plans[0].interval)
9457 			return -EINVAL;
9458 
9459 		if (request->scan_plans[0].interval >
9460 		    wiphy->max_sched_scan_plan_interval)
9461 			request->scan_plans[0].interval =
9462 				wiphy->max_sched_scan_plan_interval;
9463 
9464 		return 0;
9465 	}
9466 
9467 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
9468 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
9469 
9470 		if (WARN_ON(i >= n_plans))
9471 			return -EINVAL;
9472 
9473 		err = nla_parse_nested_deprecated(plan,
9474 						  NL80211_SCHED_SCAN_PLAN_MAX,
9475 						  attr, nl80211_plan_policy,
9476 						  NULL);
9477 		if (err)
9478 			return err;
9479 
9480 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
9481 			return -EINVAL;
9482 
9483 		request->scan_plans[i].interval =
9484 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
9485 		if (!request->scan_plans[i].interval ||
9486 		    request->scan_plans[i].interval >
9487 		    wiphy->max_sched_scan_plan_interval)
9488 			return -EINVAL;
9489 
9490 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
9491 			request->scan_plans[i].iterations =
9492 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
9493 			if (!request->scan_plans[i].iterations ||
9494 			    (request->scan_plans[i].iterations >
9495 			     wiphy->max_sched_scan_plan_iterations))
9496 				return -EINVAL;
9497 		} else if (i < n_plans - 1) {
9498 			/*
9499 			 * All scan plans but the last one must specify
9500 			 * a finite number of iterations
9501 			 */
9502 			return -EINVAL;
9503 		}
9504 
9505 		i++;
9506 	}
9507 
9508 	/*
9509 	 * The last scan plan must not specify the number of
9510 	 * iterations, it is supposed to run infinitely
9511 	 */
9512 	if (request->scan_plans[n_plans - 1].iterations)
9513 		return  -EINVAL;
9514 
9515 	return 0;
9516 }
9517 
9518 static int
9519 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
9520 				       struct cfg80211_match_set *match_sets,
9521 				       struct nlattr *tb_band_rssi,
9522 				       s32 rssi_thold)
9523 {
9524 	struct nlattr *attr;
9525 	int i, tmp, ret = 0;
9526 
9527 	if (!wiphy_ext_feature_isset(wiphy,
9528 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
9529 		if (tb_band_rssi)
9530 			ret = -EOPNOTSUPP;
9531 		else
9532 			for (i = 0; i < NUM_NL80211_BANDS; i++)
9533 				match_sets->per_band_rssi_thold[i] =
9534 					NL80211_SCAN_RSSI_THOLD_OFF;
9535 		return ret;
9536 	}
9537 
9538 	for (i = 0; i < NUM_NL80211_BANDS; i++)
9539 		match_sets->per_band_rssi_thold[i] = rssi_thold;
9540 
9541 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
9542 		enum nl80211_band band = nla_type(attr);
9543 
9544 		if (band < 0 || band >= NUM_NL80211_BANDS)
9545 			return -EINVAL;
9546 
9547 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
9548 	}
9549 
9550 	return 0;
9551 }
9552 
9553 static struct cfg80211_sched_scan_request *
9554 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
9555 			 struct nlattr **attrs, int max_match_sets)
9556 {
9557 	struct cfg80211_sched_scan_request *request;
9558 	struct nlattr *attr;
9559 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
9560 	enum nl80211_band band;
9561 	size_t ie_len, size;
9562 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
9563 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
9564 
9565 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9566 		n_channels = validate_scan_freqs(
9567 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
9568 		if (!n_channels)
9569 			return ERR_PTR(-EINVAL);
9570 	} else {
9571 		n_channels = ieee80211_get_num_supported_channels(wiphy);
9572 	}
9573 
9574 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
9575 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9576 				    tmp)
9577 			n_ssids++;
9578 
9579 	if (n_ssids > wiphy->max_sched_scan_ssids)
9580 		return ERR_PTR(-EINVAL);
9581 
9582 	/*
9583 	 * First, count the number of 'real' matchsets. Due to an issue with
9584 	 * the old implementation, matchsets containing only the RSSI attribute
9585 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
9586 	 * RSSI for all matchsets, rather than their own matchset for reporting
9587 	 * all APs with a strong RSSI. This is needed to be compatible with
9588 	 * older userspace that treated a matchset with only the RSSI as the
9589 	 * global RSSI for all other matchsets - if there are other matchsets.
9590 	 */
9591 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9592 		nla_for_each_nested(attr,
9593 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9594 				    tmp) {
9595 			struct nlattr *rssi;
9596 
9597 			err = nla_parse_nested_deprecated(tb,
9598 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9599 							  attr,
9600 							  nl80211_match_policy,
9601 							  NULL);
9602 			if (err)
9603 				return ERR_PTR(err);
9604 
9605 			/* SSID and BSSID are mutually exclusive */
9606 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
9607 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
9608 				return ERR_PTR(-EINVAL);
9609 
9610 			/* add other standalone attributes here */
9611 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
9612 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
9613 				n_match_sets++;
9614 				continue;
9615 			}
9616 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9617 			if (rssi)
9618 				default_match_rssi = nla_get_s32(rssi);
9619 		}
9620 	}
9621 
9622 	/* However, if there's no other matchset, add the RSSI one */
9623 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
9624 		n_match_sets = 1;
9625 
9626 	if (n_match_sets > max_match_sets)
9627 		return ERR_PTR(-EINVAL);
9628 
9629 	if (attrs[NL80211_ATTR_IE])
9630 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
9631 	else
9632 		ie_len = 0;
9633 
9634 	if (ie_len > wiphy->max_sched_scan_ie_len)
9635 		return ERR_PTR(-EINVAL);
9636 
9637 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9638 		/*
9639 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
9640 		 * each scan plan already specifies its own interval
9641 		 */
9642 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9643 			return ERR_PTR(-EINVAL);
9644 
9645 		nla_for_each_nested(attr,
9646 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
9647 			n_plans++;
9648 	} else {
9649 		/*
9650 		 * The scan interval attribute is kept for backward
9651 		 * compatibility. If no scan plans are specified and sched scan
9652 		 * interval is specified, one scan plan will be set with this
9653 		 * scan interval and infinite number of iterations.
9654 		 */
9655 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9656 			return ERR_PTR(-EINVAL);
9657 
9658 		n_plans = 1;
9659 	}
9660 
9661 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
9662 		return ERR_PTR(-EINVAL);
9663 
9664 	if (!wiphy_ext_feature_isset(
9665 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
9666 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
9667 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
9668 		return ERR_PTR(-EINVAL);
9669 
9670 	size = struct_size(request, channels, n_channels);
9671 	size = size_add(size, array_size(sizeof(*request->ssids), n_ssids));
9672 	size = size_add(size, array_size(sizeof(*request->match_sets),
9673 					 n_match_sets));
9674 	size = size_add(size, array_size(sizeof(*request->scan_plans),
9675 					 n_plans));
9676 	size = size_add(size, ie_len);
9677 	request = kzalloc(size, GFP_KERNEL);
9678 	if (!request)
9679 		return ERR_PTR(-ENOMEM);
9680 
9681 	if (n_ssids)
9682 		request->ssids = (void *)request +
9683 			struct_size(request, channels, n_channels);
9684 	request->n_ssids = n_ssids;
9685 	if (ie_len) {
9686 		if (n_ssids)
9687 			request->ie = (void *)(request->ssids + n_ssids);
9688 		else
9689 			request->ie = (void *)(request->channels + n_channels);
9690 	}
9691 
9692 	if (n_match_sets) {
9693 		if (request->ie)
9694 			request->match_sets = (void *)(request->ie + ie_len);
9695 		else if (n_ssids)
9696 			request->match_sets =
9697 				(void *)(request->ssids + n_ssids);
9698 		else
9699 			request->match_sets =
9700 				(void *)(request->channels + n_channels);
9701 	}
9702 	request->n_match_sets = n_match_sets;
9703 
9704 	if (n_match_sets)
9705 		request->scan_plans = (void *)(request->match_sets +
9706 					       n_match_sets);
9707 	else if (request->ie)
9708 		request->scan_plans = (void *)(request->ie + ie_len);
9709 	else if (n_ssids)
9710 		request->scan_plans = (void *)(request->ssids + n_ssids);
9711 	else
9712 		request->scan_plans = (void *)(request->channels + n_channels);
9713 
9714 	request->n_scan_plans = n_plans;
9715 
9716 	i = 0;
9717 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9718 		/* user specified, bail out if channel not found */
9719 		nla_for_each_nested(attr,
9720 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
9721 				    tmp) {
9722 			struct ieee80211_channel *chan;
9723 
9724 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
9725 
9726 			if (!chan) {
9727 				err = -EINVAL;
9728 				goto out_free;
9729 			}
9730 
9731 			/* ignore disabled channels */
9732 			if (chan->flags & IEEE80211_CHAN_DISABLED)
9733 				continue;
9734 
9735 			request->channels[i] = chan;
9736 			i++;
9737 		}
9738 	} else {
9739 		/* all channels */
9740 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
9741 			int j;
9742 
9743 			if (!wiphy->bands[band])
9744 				continue;
9745 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9746 				struct ieee80211_channel *chan;
9747 
9748 				chan = &wiphy->bands[band]->channels[j];
9749 
9750 				if (chan->flags & IEEE80211_CHAN_DISABLED)
9751 					continue;
9752 
9753 				request->channels[i] = chan;
9754 				i++;
9755 			}
9756 		}
9757 	}
9758 
9759 	if (!i) {
9760 		err = -EINVAL;
9761 		goto out_free;
9762 	}
9763 
9764 	request->n_channels = i;
9765 
9766 	i = 0;
9767 	if (n_ssids) {
9768 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9769 				    tmp) {
9770 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9771 				err = -EINVAL;
9772 				goto out_free;
9773 			}
9774 			request->ssids[i].ssid_len = nla_len(attr);
9775 			memcpy(request->ssids[i].ssid, nla_data(attr),
9776 			       nla_len(attr));
9777 			i++;
9778 		}
9779 	}
9780 
9781 	i = 0;
9782 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9783 		nla_for_each_nested(attr,
9784 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9785 				    tmp) {
9786 			struct nlattr *ssid, *bssid, *rssi;
9787 
9788 			err = nla_parse_nested_deprecated(tb,
9789 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9790 							  attr,
9791 							  nl80211_match_policy,
9792 							  NULL);
9793 			if (err)
9794 				goto out_free;
9795 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
9796 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
9797 
9798 			if (!ssid && !bssid) {
9799 				i++;
9800 				continue;
9801 			}
9802 
9803 			if (WARN_ON(i >= n_match_sets)) {
9804 				/* this indicates a programming error,
9805 				 * the loop above should have verified
9806 				 * things properly
9807 				 */
9808 				err = -EINVAL;
9809 				goto out_free;
9810 			}
9811 
9812 			if (ssid) {
9813 				memcpy(request->match_sets[i].ssid.ssid,
9814 				       nla_data(ssid), nla_len(ssid));
9815 				request->match_sets[i].ssid.ssid_len =
9816 					nla_len(ssid);
9817 			}
9818 			if (bssid)
9819 				memcpy(request->match_sets[i].bssid,
9820 				       nla_data(bssid), ETH_ALEN);
9821 
9822 			/* special attribute - old implementation w/a */
9823 			request->match_sets[i].rssi_thold = default_match_rssi;
9824 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9825 			if (rssi)
9826 				request->match_sets[i].rssi_thold =
9827 					nla_get_s32(rssi);
9828 
9829 			/* Parse per band RSSI attribute */
9830 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
9831 				&request->match_sets[i],
9832 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
9833 				request->match_sets[i].rssi_thold);
9834 			if (err)
9835 				goto out_free;
9836 
9837 			i++;
9838 		}
9839 
9840 		/* there was no other matchset, so the RSSI one is alone */
9841 		if (i == 0 && n_match_sets)
9842 			request->match_sets[0].rssi_thold = default_match_rssi;
9843 
9844 		request->min_rssi_thold = INT_MAX;
9845 		for (i = 0; i < n_match_sets; i++)
9846 			request->min_rssi_thold =
9847 				min(request->match_sets[i].rssi_thold,
9848 				    request->min_rssi_thold);
9849 	} else {
9850 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
9851 	}
9852 
9853 	if (ie_len) {
9854 		request->ie_len = ie_len;
9855 		memcpy((void *)request->ie,
9856 		       nla_data(attrs[NL80211_ATTR_IE]),
9857 		       request->ie_len);
9858 	}
9859 
9860 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
9861 	if (err)
9862 		goto out_free;
9863 
9864 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
9865 		request->delay =
9866 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
9867 
9868 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
9869 		request->relative_rssi = nla_get_s8(
9870 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
9871 		request->relative_rssi_set = true;
9872 	}
9873 
9874 	if (request->relative_rssi_set &&
9875 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
9876 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
9877 
9878 		rssi_adjust = nla_data(
9879 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
9880 		request->rssi_adjust.band = rssi_adjust->band;
9881 		request->rssi_adjust.delta = rssi_adjust->delta;
9882 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
9883 			err = -EINVAL;
9884 			goto out_free;
9885 		}
9886 	}
9887 
9888 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
9889 	if (err)
9890 		goto out_free;
9891 
9892 	request->scan_start = jiffies;
9893 
9894 	return request;
9895 
9896 out_free:
9897 	kfree(request);
9898 	return ERR_PTR(err);
9899 }
9900 
9901 static int nl80211_start_sched_scan(struct sk_buff *skb,
9902 				    struct genl_info *info)
9903 {
9904 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9905 	struct net_device *dev = info->user_ptr[1];
9906 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9907 	struct cfg80211_sched_scan_request *sched_scan_req;
9908 	bool want_multi;
9909 	int err;
9910 
9911 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
9912 		return -EOPNOTSUPP;
9913 
9914 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
9915 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
9916 	if (err)
9917 		return err;
9918 
9919 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
9920 						  info->attrs,
9921 						  rdev->wiphy.max_match_sets);
9922 
9923 	err = PTR_ERR_OR_ZERO(sched_scan_req);
9924 	if (err)
9925 		goto out_err;
9926 
9927 	/* leave request id zero for legacy request
9928 	 * or if driver does not support multi-scheduled scan
9929 	 */
9930 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
9931 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
9932 
9933 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
9934 	if (err)
9935 		goto out_free;
9936 
9937 	sched_scan_req->dev = dev;
9938 	sched_scan_req->wiphy = &rdev->wiphy;
9939 
9940 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9941 		sched_scan_req->owner_nlportid = info->snd_portid;
9942 
9943 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
9944 
9945 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
9946 	return 0;
9947 
9948 out_free:
9949 	kfree(sched_scan_req);
9950 out_err:
9951 	return err;
9952 }
9953 
9954 static int nl80211_stop_sched_scan(struct sk_buff *skb,
9955 				   struct genl_info *info)
9956 {
9957 	struct cfg80211_sched_scan_request *req;
9958 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9959 	u64 cookie;
9960 
9961 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
9962 		return -EOPNOTSUPP;
9963 
9964 	if (info->attrs[NL80211_ATTR_COOKIE]) {
9965 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9966 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
9967 	}
9968 
9969 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
9970 				     struct cfg80211_sched_scan_request,
9971 				     list);
9972 	if (!req || req->reqid ||
9973 	    (req->owner_nlportid &&
9974 	     req->owner_nlportid != info->snd_portid))
9975 		return -ENOENT;
9976 
9977 	return cfg80211_stop_sched_scan_req(rdev, req, false);
9978 }
9979 
9980 static int nl80211_start_radar_detection(struct sk_buff *skb,
9981 					 struct genl_info *info)
9982 {
9983 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9984 	struct net_device *dev = info->user_ptr[1];
9985 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9986 	struct wiphy *wiphy = wdev->wiphy;
9987 	struct cfg80211_chan_def chandef;
9988 	enum nl80211_dfs_regions dfs_region;
9989 	unsigned int cac_time_ms;
9990 	int err = -EINVAL;
9991 
9992 	flush_delayed_work(&rdev->dfs_update_channels_wk);
9993 
9994 	wiphy_lock(wiphy);
9995 
9996 	dfs_region = reg_get_dfs_region(wiphy);
9997 	if (dfs_region == NL80211_DFS_UNSET)
9998 		goto unlock;
9999 
10000 	err = nl80211_parse_chandef(rdev, info, &chandef);
10001 	if (err)
10002 		goto unlock;
10003 
10004 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
10005 	if (err < 0)
10006 		goto unlock;
10007 
10008 	if (err == 0) {
10009 		err = -EINVAL;
10010 		goto unlock;
10011 	}
10012 
10013 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef)) {
10014 		err = -EINVAL;
10015 		goto unlock;
10016 	}
10017 
10018 	if (nla_get_flag(info->attrs[NL80211_ATTR_RADAR_BACKGROUND])) {
10019 		err = cfg80211_start_background_radar_detection(rdev, wdev,
10020 								&chandef);
10021 		goto unlock;
10022 	}
10023 
10024 	if (netif_carrier_ok(dev)) {
10025 		err = -EBUSY;
10026 		goto unlock;
10027 	}
10028 
10029 	if (wdev->cac_started) {
10030 		err = -EBUSY;
10031 		goto unlock;
10032 	}
10033 
10034 	/* CAC start is offloaded to HW and can't be started manually */
10035 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD)) {
10036 		err = -EOPNOTSUPP;
10037 		goto unlock;
10038 	}
10039 
10040 	if (!rdev->ops->start_radar_detection) {
10041 		err = -EOPNOTSUPP;
10042 		goto unlock;
10043 	}
10044 
10045 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
10046 	if (WARN_ON(!cac_time_ms))
10047 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
10048 
10049 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
10050 	if (!err) {
10051 		switch (wdev->iftype) {
10052 		case NL80211_IFTYPE_AP:
10053 		case NL80211_IFTYPE_P2P_GO:
10054 			wdev->links[0].ap.chandef = chandef;
10055 			break;
10056 		case NL80211_IFTYPE_ADHOC:
10057 			wdev->u.ibss.chandef = chandef;
10058 			break;
10059 		case NL80211_IFTYPE_MESH_POINT:
10060 			wdev->u.mesh.chandef = chandef;
10061 			break;
10062 		default:
10063 			break;
10064 		}
10065 		wdev->cac_started = true;
10066 		wdev->cac_start_time = jiffies;
10067 		wdev->cac_time_ms = cac_time_ms;
10068 	}
10069 unlock:
10070 	wiphy_unlock(wiphy);
10071 
10072 	return err;
10073 }
10074 
10075 static int nl80211_notify_radar_detection(struct sk_buff *skb,
10076 					  struct genl_info *info)
10077 {
10078 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10079 	struct net_device *dev = info->user_ptr[1];
10080 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10081 	struct wiphy *wiphy = wdev->wiphy;
10082 	struct cfg80211_chan_def chandef;
10083 	enum nl80211_dfs_regions dfs_region;
10084 	int err;
10085 
10086 	dfs_region = reg_get_dfs_region(wiphy);
10087 	if (dfs_region == NL80211_DFS_UNSET) {
10088 		GENL_SET_ERR_MSG(info,
10089 				 "DFS Region is not set. Unexpected Radar indication");
10090 		return -EINVAL;
10091 	}
10092 
10093 	err = nl80211_parse_chandef(rdev, info, &chandef);
10094 	if (err) {
10095 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
10096 		return err;
10097 	}
10098 
10099 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
10100 	if (err < 0) {
10101 		GENL_SET_ERR_MSG(info, "chandef is invalid");
10102 		return err;
10103 	}
10104 
10105 	if (err == 0) {
10106 		GENL_SET_ERR_MSG(info,
10107 				 "Unexpected Radar indication for chandef/iftype");
10108 		return -EINVAL;
10109 	}
10110 
10111 	/* Do not process this notification if radar is already detected
10112 	 * by kernel on this channel, and return success.
10113 	 */
10114 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
10115 		return 0;
10116 
10117 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
10118 
10119 	cfg80211_sched_dfs_chan_update(rdev);
10120 
10121 	rdev->radar_chandef = chandef;
10122 
10123 	/* Propagate this notification to other radios as well */
10124 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
10125 
10126 	return 0;
10127 }
10128 
10129 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
10130 {
10131 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10132 	unsigned int link_id = nl80211_link_id(info->attrs);
10133 	struct net_device *dev = info->user_ptr[1];
10134 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10135 	struct cfg80211_csa_settings params;
10136 	struct nlattr **csa_attrs = NULL;
10137 	int err;
10138 	bool need_new_beacon = false;
10139 	bool need_handle_dfs_flag = true;
10140 	int len, i;
10141 	u32 cs_count;
10142 
10143 	if (!rdev->ops->channel_switch ||
10144 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
10145 		return -EOPNOTSUPP;
10146 
10147 	switch (dev->ieee80211_ptr->iftype) {
10148 	case NL80211_IFTYPE_AP:
10149 	case NL80211_IFTYPE_P2P_GO:
10150 		need_new_beacon = true;
10151 		/* For all modes except AP the handle_dfs flag needs to be
10152 		 * supplied to tell the kernel that userspace will handle radar
10153 		 * events when they happen. Otherwise a switch to a channel
10154 		 * requiring DFS will be rejected.
10155 		 */
10156 		need_handle_dfs_flag = false;
10157 
10158 		/* useless if AP is not running */
10159 		if (!wdev->links[link_id].ap.beacon_interval)
10160 			return -ENOTCONN;
10161 		break;
10162 	case NL80211_IFTYPE_ADHOC:
10163 		if (!wdev->u.ibss.ssid_len)
10164 			return -ENOTCONN;
10165 		break;
10166 	case NL80211_IFTYPE_MESH_POINT:
10167 		if (!wdev->u.mesh.id_len)
10168 			return -ENOTCONN;
10169 		break;
10170 	default:
10171 		return -EOPNOTSUPP;
10172 	}
10173 
10174 	memset(&params, 0, sizeof(params));
10175 	params.beacon_csa.ftm_responder = -1;
10176 
10177 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10178 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
10179 		return -EINVAL;
10180 
10181 	/* only important for AP, IBSS and mesh create IEs internally */
10182 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
10183 		return -EINVAL;
10184 
10185 	/* Even though the attribute is u32, the specification says
10186 	 * u8, so let's make sure we don't overflow.
10187 	 */
10188 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
10189 	if (cs_count > 255)
10190 		return -EINVAL;
10191 
10192 	params.count = cs_count;
10193 
10194 	if (!need_new_beacon)
10195 		goto skip_beacons;
10196 
10197 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after,
10198 				   info->extack);
10199 	if (err)
10200 		goto free;
10201 
10202 	csa_attrs = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*csa_attrs),
10203 			    GFP_KERNEL);
10204 	if (!csa_attrs) {
10205 		err = -ENOMEM;
10206 		goto free;
10207 	}
10208 
10209 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
10210 					  info->attrs[NL80211_ATTR_CSA_IES],
10211 					  nl80211_policy, info->extack);
10212 	if (err)
10213 		goto free;
10214 
10215 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa,
10216 				   info->extack);
10217 	if (err)
10218 		goto free;
10219 
10220 	if (!csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
10221 		err = -EINVAL;
10222 		goto free;
10223 	}
10224 
10225 	len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
10226 	if (!len || (len % sizeof(u16))) {
10227 		err = -EINVAL;
10228 		goto free;
10229 	}
10230 
10231 	params.n_counter_offsets_beacon = len / sizeof(u16);
10232 	if (rdev->wiphy.max_num_csa_counters &&
10233 	    (params.n_counter_offsets_beacon >
10234 	     rdev->wiphy.max_num_csa_counters)) {
10235 		err = -EINVAL;
10236 		goto free;
10237 	}
10238 
10239 	params.counter_offsets_beacon =
10240 		nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
10241 
10242 	/* sanity checks - counters should fit and be the same */
10243 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
10244 		u16 offset = params.counter_offsets_beacon[i];
10245 
10246 		if (offset >= params.beacon_csa.tail_len) {
10247 			err = -EINVAL;
10248 			goto free;
10249 		}
10250 
10251 		if (params.beacon_csa.tail[offset] != params.count) {
10252 			err = -EINVAL;
10253 			goto free;
10254 		}
10255 	}
10256 
10257 	if (csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
10258 		len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10259 		if (!len || (len % sizeof(u16))) {
10260 			err = -EINVAL;
10261 			goto free;
10262 		}
10263 
10264 		params.n_counter_offsets_presp = len / sizeof(u16);
10265 		if (rdev->wiphy.max_num_csa_counters &&
10266 		    (params.n_counter_offsets_presp >
10267 		     rdev->wiphy.max_num_csa_counters)) {
10268 			err = -EINVAL;
10269 			goto free;
10270 		}
10271 
10272 		params.counter_offsets_presp =
10273 			nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10274 
10275 		/* sanity checks - counters should fit and be the same */
10276 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
10277 			u16 offset = params.counter_offsets_presp[i];
10278 
10279 			if (offset >= params.beacon_csa.probe_resp_len) {
10280 				err = -EINVAL;
10281 				goto free;
10282 			}
10283 
10284 			if (params.beacon_csa.probe_resp[offset] !=
10285 			    params.count) {
10286 				err = -EINVAL;
10287 				goto free;
10288 			}
10289 		}
10290 	}
10291 
10292 skip_beacons:
10293 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
10294 	if (err)
10295 		goto free;
10296 
10297 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
10298 					   wdev->iftype)) {
10299 		err = -EINVAL;
10300 		goto free;
10301 	}
10302 
10303 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
10304 					    &params.chandef,
10305 					    wdev->iftype);
10306 	if (err < 0)
10307 		goto free;
10308 
10309 	if (err > 0) {
10310 		params.radar_required = true;
10311 		if (need_handle_dfs_flag &&
10312 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
10313 			err = -EINVAL;
10314 			goto free;
10315 		}
10316 	}
10317 
10318 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
10319 		params.block_tx = true;
10320 
10321 	if (info->attrs[NL80211_ATTR_PUNCT_BITMAP]) {
10322 		err = nl80211_parse_punct_bitmap(rdev, info,
10323 						 &params.chandef,
10324 						 &params.punct_bitmap);
10325 		if (err)
10326 			goto free;
10327 	}
10328 
10329 	wdev_lock(wdev);
10330 	err = rdev_channel_switch(rdev, dev, &params);
10331 	wdev_unlock(wdev);
10332 
10333 free:
10334 	kfree(params.beacon_after.mbssid_ies);
10335 	kfree(params.beacon_csa.mbssid_ies);
10336 	kfree(params.beacon_after.rnr_ies);
10337 	kfree(params.beacon_csa.rnr_ies);
10338 	kfree(csa_attrs);
10339 	return err;
10340 }
10341 
10342 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
10343 			    u32 seq, int flags,
10344 			    struct cfg80211_registered_device *rdev,
10345 			    struct wireless_dev *wdev,
10346 			    struct cfg80211_internal_bss *intbss)
10347 {
10348 	struct cfg80211_bss *res = &intbss->pub;
10349 	const struct cfg80211_bss_ies *ies;
10350 	unsigned int link_id;
10351 	void *hdr;
10352 	struct nlattr *bss;
10353 
10354 	ASSERT_WDEV_LOCK(wdev);
10355 
10356 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
10357 			     NL80211_CMD_NEW_SCAN_RESULTS);
10358 	if (!hdr)
10359 		return -1;
10360 
10361 	genl_dump_check_consistent(cb, hdr);
10362 
10363 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
10364 		goto nla_put_failure;
10365 	if (wdev->netdev &&
10366 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
10367 		goto nla_put_failure;
10368 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
10369 			      NL80211_ATTR_PAD))
10370 		goto nla_put_failure;
10371 
10372 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
10373 	if (!bss)
10374 		goto nla_put_failure;
10375 	if ((!is_zero_ether_addr(res->bssid) &&
10376 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
10377 		goto nla_put_failure;
10378 
10379 	rcu_read_lock();
10380 	/* indicate whether we have probe response data or not */
10381 	if (rcu_access_pointer(res->proberesp_ies) &&
10382 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
10383 		goto fail_unlock_rcu;
10384 
10385 	/* this pointer prefers to be pointed to probe response data
10386 	 * but is always valid
10387 	 */
10388 	ies = rcu_dereference(res->ies);
10389 	if (ies) {
10390 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
10391 				      NL80211_BSS_PAD))
10392 			goto fail_unlock_rcu;
10393 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
10394 					ies->len, ies->data))
10395 			goto fail_unlock_rcu;
10396 	}
10397 
10398 	/* and this pointer is always (unless driver didn't know) beacon data */
10399 	ies = rcu_dereference(res->beacon_ies);
10400 	if (ies && ies->from_beacon) {
10401 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
10402 				      NL80211_BSS_PAD))
10403 			goto fail_unlock_rcu;
10404 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
10405 					ies->len, ies->data))
10406 			goto fail_unlock_rcu;
10407 	}
10408 	rcu_read_unlock();
10409 
10410 	if (res->beacon_interval &&
10411 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
10412 		goto nla_put_failure;
10413 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
10414 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
10415 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
10416 			res->channel->freq_offset) ||
10417 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
10418 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
10419 			jiffies_to_msecs(jiffies - intbss->ts)))
10420 		goto nla_put_failure;
10421 
10422 	if (intbss->parent_tsf &&
10423 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
10424 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
10425 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
10426 		     intbss->parent_bssid)))
10427 		goto nla_put_failure;
10428 
10429 	if (intbss->ts_boottime &&
10430 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
10431 			      intbss->ts_boottime, NL80211_BSS_PAD))
10432 		goto nla_put_failure;
10433 
10434 	if (!nl80211_put_signal(msg, intbss->pub.chains,
10435 				intbss->pub.chain_signal,
10436 				NL80211_BSS_CHAIN_SIGNAL))
10437 		goto nla_put_failure;
10438 
10439 	switch (rdev->wiphy.signal_type) {
10440 	case CFG80211_SIGNAL_TYPE_MBM:
10441 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
10442 			goto nla_put_failure;
10443 		break;
10444 	case CFG80211_SIGNAL_TYPE_UNSPEC:
10445 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
10446 			goto nla_put_failure;
10447 		break;
10448 	default:
10449 		break;
10450 	}
10451 
10452 	switch (wdev->iftype) {
10453 	case NL80211_IFTYPE_P2P_CLIENT:
10454 	case NL80211_IFTYPE_STATION:
10455 		for_each_valid_link(wdev, link_id) {
10456 			if (intbss == wdev->links[link_id].client.current_bss &&
10457 			    (nla_put_u32(msg, NL80211_BSS_STATUS,
10458 					 NL80211_BSS_STATUS_ASSOCIATED) ||
10459 			     (wdev->valid_links &&
10460 			      (nla_put_u8(msg, NL80211_BSS_MLO_LINK_ID,
10461 					  link_id) ||
10462 			       nla_put(msg, NL80211_BSS_MLD_ADDR, ETH_ALEN,
10463 				       wdev->u.client.connected_addr)))))
10464 				goto nla_put_failure;
10465 		}
10466 		break;
10467 	case NL80211_IFTYPE_ADHOC:
10468 		if (intbss == wdev->u.ibss.current_bss &&
10469 		    nla_put_u32(msg, NL80211_BSS_STATUS,
10470 				NL80211_BSS_STATUS_IBSS_JOINED))
10471 			goto nla_put_failure;
10472 		break;
10473 	default:
10474 		break;
10475 	}
10476 
10477 	nla_nest_end(msg, bss);
10478 
10479 	genlmsg_end(msg, hdr);
10480 	return 0;
10481 
10482  fail_unlock_rcu:
10483 	rcu_read_unlock();
10484  nla_put_failure:
10485 	genlmsg_cancel(msg, hdr);
10486 	return -EMSGSIZE;
10487 }
10488 
10489 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
10490 {
10491 	struct cfg80211_registered_device *rdev;
10492 	struct cfg80211_internal_bss *scan;
10493 	struct wireless_dev *wdev;
10494 	int start = cb->args[2], idx = 0;
10495 	int err;
10496 
10497 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
10498 	if (err)
10499 		return err;
10500 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10501 	__acquire(&rdev->wiphy.mtx);
10502 
10503 	wdev_lock(wdev);
10504 	spin_lock_bh(&rdev->bss_lock);
10505 
10506 	/*
10507 	 * dump_scan will be called multiple times to break up the scan results
10508 	 * into multiple messages.  It is unlikely that any more bss-es will be
10509 	 * expired after the first call, so only call only call this on the
10510 	 * first dump_scan invocation.
10511 	 */
10512 	if (start == 0)
10513 		cfg80211_bss_expire(rdev);
10514 
10515 	cb->seq = rdev->bss_generation;
10516 
10517 	list_for_each_entry(scan, &rdev->bss_list, list) {
10518 		if (++idx <= start)
10519 			continue;
10520 		if (nl80211_send_bss(skb, cb,
10521 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10522 				rdev, wdev, scan) < 0) {
10523 			idx--;
10524 			break;
10525 		}
10526 	}
10527 
10528 	spin_unlock_bh(&rdev->bss_lock);
10529 	wdev_unlock(wdev);
10530 
10531 	cb->args[2] = idx;
10532 	wiphy_unlock(&rdev->wiphy);
10533 
10534 	return skb->len;
10535 }
10536 
10537 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
10538 			       int flags, struct net_device *dev,
10539 			       bool allow_radio_stats,
10540 			       struct survey_info *survey)
10541 {
10542 	void *hdr;
10543 	struct nlattr *infoattr;
10544 
10545 	/* skip radio stats if userspace didn't request them */
10546 	if (!survey->channel && !allow_radio_stats)
10547 		return 0;
10548 
10549 	hdr = nl80211hdr_put(msg, portid, seq, flags,
10550 			     NL80211_CMD_NEW_SURVEY_RESULTS);
10551 	if (!hdr)
10552 		return -ENOMEM;
10553 
10554 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
10555 		goto nla_put_failure;
10556 
10557 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
10558 	if (!infoattr)
10559 		goto nla_put_failure;
10560 
10561 	if (survey->channel &&
10562 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
10563 			survey->channel->center_freq))
10564 		goto nla_put_failure;
10565 
10566 	if (survey->channel && survey->channel->freq_offset &&
10567 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY_OFFSET,
10568 			survey->channel->freq_offset))
10569 		goto nla_put_failure;
10570 
10571 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
10572 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
10573 		goto nla_put_failure;
10574 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
10575 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
10576 		goto nla_put_failure;
10577 	if ((survey->filled & SURVEY_INFO_TIME) &&
10578 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
10579 			survey->time, NL80211_SURVEY_INFO_PAD))
10580 		goto nla_put_failure;
10581 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
10582 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
10583 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
10584 		goto nla_put_failure;
10585 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
10586 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
10587 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
10588 		goto nla_put_failure;
10589 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
10590 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
10591 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
10592 		goto nla_put_failure;
10593 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
10594 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
10595 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
10596 		goto nla_put_failure;
10597 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
10598 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
10599 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
10600 		goto nla_put_failure;
10601 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
10602 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
10603 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
10604 		goto nla_put_failure;
10605 
10606 	nla_nest_end(msg, infoattr);
10607 
10608 	genlmsg_end(msg, hdr);
10609 	return 0;
10610 
10611  nla_put_failure:
10612 	genlmsg_cancel(msg, hdr);
10613 	return -EMSGSIZE;
10614 }
10615 
10616 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
10617 {
10618 	struct nlattr **attrbuf;
10619 	struct survey_info survey;
10620 	struct cfg80211_registered_device *rdev;
10621 	struct wireless_dev *wdev;
10622 	int survey_idx = cb->args[2];
10623 	int res;
10624 	bool radio_stats;
10625 
10626 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
10627 	if (!attrbuf)
10628 		return -ENOMEM;
10629 
10630 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, attrbuf);
10631 	if (res) {
10632 		kfree(attrbuf);
10633 		return res;
10634 	}
10635 	/* nl80211_prepare_wdev_dump acquired it in the successful case */
10636 	__acquire(&rdev->wiphy.mtx);
10637 
10638 	/* prepare_wdev_dump parsed the attributes */
10639 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
10640 
10641 	if (!wdev->netdev) {
10642 		res = -EINVAL;
10643 		goto out_err;
10644 	}
10645 
10646 	if (!rdev->ops->dump_survey) {
10647 		res = -EOPNOTSUPP;
10648 		goto out_err;
10649 	}
10650 
10651 	while (1) {
10652 		wdev_lock(wdev);
10653 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
10654 		wdev_unlock(wdev);
10655 		if (res == -ENOENT)
10656 			break;
10657 		if (res)
10658 			goto out_err;
10659 
10660 		/* don't send disabled channels, but do send non-channel data */
10661 		if (survey.channel &&
10662 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
10663 			survey_idx++;
10664 			continue;
10665 		}
10666 
10667 		if (nl80211_send_survey(skb,
10668 				NETLINK_CB(cb->skb).portid,
10669 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
10670 				wdev->netdev, radio_stats, &survey) < 0)
10671 			goto out;
10672 		survey_idx++;
10673 	}
10674 
10675  out:
10676 	cb->args[2] = survey_idx;
10677 	res = skb->len;
10678  out_err:
10679 	kfree(attrbuf);
10680 	wiphy_unlock(&rdev->wiphy);
10681 	return res;
10682 }
10683 
10684 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
10685 {
10686 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
10687 				  NL80211_WPA_VERSION_2 |
10688 				  NL80211_WPA_VERSION_3));
10689 }
10690 
10691 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
10692 {
10693 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10694 	struct net_device *dev = info->user_ptr[1];
10695 	struct ieee80211_channel *chan;
10696 	const u8 *bssid, *ssid;
10697 	int err, ssid_len;
10698 	enum nl80211_auth_type auth_type;
10699 	struct key_parse key;
10700 	bool local_state_change;
10701 	struct cfg80211_auth_request req = {};
10702 	u32 freq;
10703 
10704 	if (!info->attrs[NL80211_ATTR_MAC])
10705 		return -EINVAL;
10706 
10707 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
10708 		return -EINVAL;
10709 
10710 	if (!info->attrs[NL80211_ATTR_SSID])
10711 		return -EINVAL;
10712 
10713 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10714 		return -EINVAL;
10715 
10716 	err = nl80211_parse_key(info, &key);
10717 	if (err)
10718 		return err;
10719 
10720 	if (key.idx >= 0) {
10721 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
10722 			return -EINVAL;
10723 		if (!key.p.key || !key.p.key_len)
10724 			return -EINVAL;
10725 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
10726 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
10727 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
10728 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
10729 			return -EINVAL;
10730 		if (key.idx > 3)
10731 			return -EINVAL;
10732 	} else {
10733 		key.p.key_len = 0;
10734 		key.p.key = NULL;
10735 	}
10736 
10737 	if (key.idx >= 0) {
10738 		int i;
10739 		bool ok = false;
10740 
10741 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
10742 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
10743 				ok = true;
10744 				break;
10745 			}
10746 		}
10747 		if (!ok)
10748 			return -EINVAL;
10749 	}
10750 
10751 	if (!rdev->ops->auth)
10752 		return -EOPNOTSUPP;
10753 
10754 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10755 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10756 		return -EOPNOTSUPP;
10757 
10758 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10759 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10760 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10761 		freq +=
10762 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10763 
10764 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10765 	if (!chan)
10766 		return -EINVAL;
10767 
10768 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10769 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10770 
10771 	if (info->attrs[NL80211_ATTR_IE]) {
10772 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10773 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10774 	}
10775 
10776 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10777 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
10778 		return -EINVAL;
10779 
10780 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
10781 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
10782 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
10783 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
10784 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
10785 		return -EINVAL;
10786 
10787 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
10788 		if (auth_type != NL80211_AUTHTYPE_SAE &&
10789 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
10790 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
10791 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
10792 			return -EINVAL;
10793 		req.auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
10794 		req.auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
10795 	}
10796 
10797 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
10798 
10799 	/*
10800 	 * Since we no longer track auth state, ignore
10801 	 * requests to only change local state.
10802 	 */
10803 	if (local_state_change)
10804 		return 0;
10805 
10806 	req.auth_type = auth_type;
10807 	req.key = key.p.key;
10808 	req.key_len = key.p.key_len;
10809 	req.key_idx = key.idx;
10810 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
10811 	if (req.link_id >= 0) {
10812 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10813 			return -EINVAL;
10814 		if (!info->attrs[NL80211_ATTR_MLD_ADDR])
10815 			return -EINVAL;
10816 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10817 		if (!is_valid_ether_addr(req.ap_mld_addr))
10818 			return -EINVAL;
10819 	}
10820 
10821 	req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
10822 				   IEEE80211_BSS_TYPE_ESS,
10823 				   IEEE80211_PRIVACY_ANY);
10824 	if (!req.bss)
10825 		return -ENOENT;
10826 
10827 	wdev_lock(dev->ieee80211_ptr);
10828 	err = cfg80211_mlme_auth(rdev, dev, &req);
10829 	wdev_unlock(dev->ieee80211_ptr);
10830 
10831 	cfg80211_put_bss(&rdev->wiphy, req.bss);
10832 
10833 	return err;
10834 }
10835 
10836 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
10837 				     struct genl_info *info)
10838 {
10839 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10840 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
10841 		return -EINVAL;
10842 	}
10843 
10844 	if (!rdev->ops->tx_control_port ||
10845 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10846 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
10847 		return -EOPNOTSUPP;
10848 
10849 	return 0;
10850 }
10851 
10852 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
10853 				   struct genl_info *info,
10854 				   struct cfg80211_crypto_settings *settings,
10855 				   int cipher_limit)
10856 {
10857 	memset(settings, 0, sizeof(*settings));
10858 
10859 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
10860 
10861 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
10862 		u16 proto;
10863 
10864 		proto = nla_get_u16(
10865 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
10866 		settings->control_port_ethertype = cpu_to_be16(proto);
10867 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
10868 		    proto != ETH_P_PAE)
10869 			return -EINVAL;
10870 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
10871 			settings->control_port_no_encrypt = true;
10872 	} else
10873 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
10874 
10875 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10876 		int r = validate_pae_over_nl80211(rdev, info);
10877 
10878 		if (r < 0)
10879 			return r;
10880 
10881 		settings->control_port_over_nl80211 = true;
10882 
10883 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
10884 			settings->control_port_no_preauth = true;
10885 	}
10886 
10887 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
10888 		void *data;
10889 		int len, i;
10890 
10891 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10892 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10893 		settings->n_ciphers_pairwise = len / sizeof(u32);
10894 
10895 		if (len % sizeof(u32))
10896 			return -EINVAL;
10897 
10898 		if (settings->n_ciphers_pairwise > cipher_limit)
10899 			return -EINVAL;
10900 
10901 		memcpy(settings->ciphers_pairwise, data, len);
10902 
10903 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
10904 			if (!cfg80211_supported_cipher_suite(
10905 					&rdev->wiphy,
10906 					settings->ciphers_pairwise[i]))
10907 				return -EINVAL;
10908 	}
10909 
10910 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
10911 		settings->cipher_group =
10912 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
10913 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
10914 						     settings->cipher_group))
10915 			return -EINVAL;
10916 	}
10917 
10918 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
10919 		settings->wpa_versions =
10920 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
10921 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
10922 			return -EINVAL;
10923 	}
10924 
10925 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
10926 		void *data;
10927 		int len;
10928 
10929 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
10930 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
10931 		settings->n_akm_suites = len / sizeof(u32);
10932 
10933 		if (len % sizeof(u32))
10934 			return -EINVAL;
10935 
10936 		if (settings->n_akm_suites > rdev->wiphy.max_num_akm_suites)
10937 			return -EINVAL;
10938 
10939 		memcpy(settings->akm_suites, data, len);
10940 	}
10941 
10942 	if (info->attrs[NL80211_ATTR_PMK]) {
10943 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
10944 			return -EINVAL;
10945 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10946 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
10947 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10948 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
10949 			return -EINVAL;
10950 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10951 	}
10952 
10953 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
10954 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10955 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
10956 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10957 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
10958 			return -EINVAL;
10959 		settings->sae_pwd =
10960 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10961 		settings->sae_pwd_len =
10962 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10963 	}
10964 
10965 	if (info->attrs[NL80211_ATTR_SAE_PWE])
10966 		settings->sae_pwe =
10967 			nla_get_u8(info->attrs[NL80211_ATTR_SAE_PWE]);
10968 	else
10969 		settings->sae_pwe = NL80211_SAE_PWE_UNSPECIFIED;
10970 
10971 	return 0;
10972 }
10973 
10974 static struct cfg80211_bss *nl80211_assoc_bss(struct cfg80211_registered_device *rdev,
10975 					      const u8 *ssid, int ssid_len,
10976 					      struct nlattr **attrs)
10977 {
10978 	struct ieee80211_channel *chan;
10979 	struct cfg80211_bss *bss;
10980 	const u8 *bssid;
10981 	u32 freq;
10982 
10983 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_WIPHY_FREQ])
10984 		return ERR_PTR(-EINVAL);
10985 
10986 	bssid = nla_data(attrs[NL80211_ATTR_MAC]);
10987 
10988 	freq = MHZ_TO_KHZ(nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]));
10989 	if (attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10990 		freq += nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10991 
10992 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10993 	if (!chan)
10994 		return ERR_PTR(-EINVAL);
10995 
10996 	bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid,
10997 			       ssid, ssid_len,
10998 			       IEEE80211_BSS_TYPE_ESS,
10999 			       IEEE80211_PRIVACY_ANY);
11000 	if (!bss)
11001 		return ERR_PTR(-ENOENT);
11002 
11003 	return bss;
11004 }
11005 
11006 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
11007 {
11008 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11009 	struct net_device *dev = info->user_ptr[1];
11010 	struct cfg80211_assoc_request req = {};
11011 	struct nlattr **attrs = NULL;
11012 	const u8 *ap_addr, *ssid;
11013 	unsigned int link_id;
11014 	int err, ssid_len;
11015 
11016 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11017 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11018 		return -EPERM;
11019 
11020 	if (!info->attrs[NL80211_ATTR_SSID])
11021 		return -EINVAL;
11022 
11023 	if (!rdev->ops->assoc)
11024 		return -EOPNOTSUPP;
11025 
11026 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11027 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11028 		return -EOPNOTSUPP;
11029 
11030 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11031 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11032 
11033 	if (info->attrs[NL80211_ATTR_IE]) {
11034 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11035 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11036 
11037 		if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
11038 					   req.ie, req.ie_len)) {
11039 			GENL_SET_ERR_MSG(info,
11040 					 "non-inheritance makes no sense");
11041 			return -EINVAL;
11042 		}
11043 	}
11044 
11045 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
11046 		enum nl80211_mfp mfp =
11047 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11048 		if (mfp == NL80211_MFP_REQUIRED)
11049 			req.use_mfp = true;
11050 		else if (mfp != NL80211_MFP_NO)
11051 			return -EINVAL;
11052 	}
11053 
11054 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
11055 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11056 
11057 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11058 		req.flags |= ASSOC_REQ_DISABLE_HT;
11059 
11060 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11061 		memcpy(&req.ht_capa_mask,
11062 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11063 		       sizeof(req.ht_capa_mask));
11064 
11065 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11066 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11067 			return -EINVAL;
11068 		memcpy(&req.ht_capa,
11069 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11070 		       sizeof(req.ht_capa));
11071 	}
11072 
11073 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11074 		req.flags |= ASSOC_REQ_DISABLE_VHT;
11075 
11076 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11077 		req.flags |= ASSOC_REQ_DISABLE_HE;
11078 
11079 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11080 		req.flags |= ASSOC_REQ_DISABLE_EHT;
11081 
11082 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11083 		memcpy(&req.vht_capa_mask,
11084 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11085 		       sizeof(req.vht_capa_mask));
11086 
11087 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11088 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11089 			return -EINVAL;
11090 		memcpy(&req.vht_capa,
11091 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11092 		       sizeof(req.vht_capa));
11093 	}
11094 
11095 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11096 		if (!((rdev->wiphy.features &
11097 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11098 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11099 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11100 					     NL80211_EXT_FEATURE_RRM))
11101 			return -EINVAL;
11102 		req.flags |= ASSOC_REQ_USE_RRM;
11103 	}
11104 
11105 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
11106 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
11107 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
11108 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
11109 			return -EINVAL;
11110 		req.fils_nonces =
11111 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
11112 	}
11113 
11114 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]) {
11115 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY])
11116 			return -EINVAL;
11117 		memcpy(&req.s1g_capa_mask,
11118 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]),
11119 		       sizeof(req.s1g_capa_mask));
11120 	}
11121 
11122 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY]) {
11123 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK])
11124 			return -EINVAL;
11125 		memcpy(&req.s1g_capa,
11126 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY]),
11127 		       sizeof(req.s1g_capa));
11128 	}
11129 
11130 	req.link_id = nl80211_link_id_or_invalid(info->attrs);
11131 
11132 	if (info->attrs[NL80211_ATTR_MLO_LINKS]) {
11133 		unsigned int attrsize = NUM_NL80211_ATTR * sizeof(*attrs);
11134 		struct nlattr *link;
11135 		int rem = 0;
11136 
11137 		if (req.link_id < 0)
11138 			return -EINVAL;
11139 
11140 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
11141 			return -EINVAL;
11142 
11143 		if (info->attrs[NL80211_ATTR_MAC] ||
11144 		    info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
11145 		    !info->attrs[NL80211_ATTR_MLD_ADDR])
11146 			return -EINVAL;
11147 
11148 		req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
11149 		ap_addr = req.ap_mld_addr;
11150 
11151 		attrs = kzalloc(attrsize, GFP_KERNEL);
11152 		if (!attrs)
11153 			return -ENOMEM;
11154 
11155 		nla_for_each_nested(link,
11156 				    info->attrs[NL80211_ATTR_MLO_LINKS],
11157 				    rem) {
11158 			memset(attrs, 0, attrsize);
11159 
11160 			nla_parse_nested(attrs, NL80211_ATTR_MAX,
11161 					 link, NULL, NULL);
11162 
11163 			if (!attrs[NL80211_ATTR_MLO_LINK_ID]) {
11164 				err = -EINVAL;
11165 				goto free;
11166 			}
11167 
11168 			link_id = nla_get_u8(attrs[NL80211_ATTR_MLO_LINK_ID]);
11169 			/* cannot use the same link ID again */
11170 			if (req.links[link_id].bss) {
11171 				err = -EINVAL;
11172 				goto free;
11173 			}
11174 			req.links[link_id].bss =
11175 				nl80211_assoc_bss(rdev, ssid, ssid_len, attrs);
11176 			if (IS_ERR(req.links[link_id].bss)) {
11177 				err = PTR_ERR(req.links[link_id].bss);
11178 				req.links[link_id].bss = NULL;
11179 				goto free;
11180 			}
11181 
11182 			if (attrs[NL80211_ATTR_IE]) {
11183 				req.links[link_id].elems =
11184 					nla_data(attrs[NL80211_ATTR_IE]);
11185 				req.links[link_id].elems_len =
11186 					nla_len(attrs[NL80211_ATTR_IE]);
11187 
11188 				if (cfg80211_find_elem(WLAN_EID_FRAGMENT,
11189 						       req.links[link_id].elems,
11190 						       req.links[link_id].elems_len)) {
11191 					GENL_SET_ERR_MSG(info,
11192 							 "cannot deal with fragmentation");
11193 					err = -EINVAL;
11194 					goto free;
11195 				}
11196 
11197 				if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
11198 							   req.links[link_id].elems,
11199 							   req.links[link_id].elems_len)) {
11200 					GENL_SET_ERR_MSG(info,
11201 							 "cannot deal with non-inheritance");
11202 					err = -EINVAL;
11203 					goto free;
11204 				}
11205 			}
11206 
11207 			req.links[link_id].disabled =
11208 				nla_get_flag(attrs[NL80211_ATTR_MLO_LINK_DISABLED]);
11209 		}
11210 
11211 		if (!req.links[req.link_id].bss) {
11212 			err = -EINVAL;
11213 			goto free;
11214 		}
11215 
11216 		if (req.links[req.link_id].elems_len) {
11217 			GENL_SET_ERR_MSG(info,
11218 					 "cannot have per-link elems on assoc link");
11219 			err = -EINVAL;
11220 			goto free;
11221 		}
11222 
11223 		if (req.links[req.link_id].disabled) {
11224 			GENL_SET_ERR_MSG(info,
11225 					 "cannot have assoc link disabled");
11226 			err = -EINVAL;
11227 			goto free;
11228 		}
11229 
11230 		kfree(attrs);
11231 		attrs = NULL;
11232 	} else {
11233 		if (req.link_id >= 0)
11234 			return -EINVAL;
11235 
11236 		req.bss = nl80211_assoc_bss(rdev, ssid, ssid_len, info->attrs);
11237 		if (IS_ERR(req.bss))
11238 			return PTR_ERR(req.bss);
11239 		ap_addr = req.bss->bssid;
11240 	}
11241 
11242 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
11243 	if (!err) {
11244 		wdev_lock(dev->ieee80211_ptr);
11245 
11246 		err = cfg80211_mlme_assoc(rdev, dev, &req);
11247 
11248 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11249 			dev->ieee80211_ptr->conn_owner_nlportid =
11250 				info->snd_portid;
11251 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
11252 			       ap_addr, ETH_ALEN);
11253 		}
11254 
11255 		wdev_unlock(dev->ieee80211_ptr);
11256 	}
11257 
11258 free:
11259 	for (link_id = 0; link_id < ARRAY_SIZE(req.links); link_id++)
11260 		cfg80211_put_bss(&rdev->wiphy, req.links[link_id].bss);
11261 	cfg80211_put_bss(&rdev->wiphy, req.bss);
11262 	kfree(attrs);
11263 
11264 	return err;
11265 }
11266 
11267 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
11268 {
11269 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11270 	struct net_device *dev = info->user_ptr[1];
11271 	const u8 *ie = NULL, *bssid;
11272 	int ie_len = 0, err;
11273 	u16 reason_code;
11274 	bool local_state_change;
11275 
11276 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11277 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11278 		return -EPERM;
11279 
11280 	if (!info->attrs[NL80211_ATTR_MAC])
11281 		return -EINVAL;
11282 
11283 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11284 		return -EINVAL;
11285 
11286 	if (!rdev->ops->deauth)
11287 		return -EOPNOTSUPP;
11288 
11289 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11290 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11291 		return -EOPNOTSUPP;
11292 
11293 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11294 
11295 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11296 	if (reason_code == 0) {
11297 		/* Reason Code 0 is reserved */
11298 		return -EINVAL;
11299 	}
11300 
11301 	if (info->attrs[NL80211_ATTR_IE]) {
11302 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11303 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11304 	}
11305 
11306 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11307 
11308 	wdev_lock(dev->ieee80211_ptr);
11309 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
11310 				   local_state_change);
11311 	wdev_unlock(dev->ieee80211_ptr);
11312 	return err;
11313 }
11314 
11315 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
11316 {
11317 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11318 	struct net_device *dev = info->user_ptr[1];
11319 	const u8 *ie = NULL, *bssid;
11320 	int ie_len = 0, err;
11321 	u16 reason_code;
11322 	bool local_state_change;
11323 
11324 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
11325 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11326 		return -EPERM;
11327 
11328 	if (!info->attrs[NL80211_ATTR_MAC])
11329 		return -EINVAL;
11330 
11331 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
11332 		return -EINVAL;
11333 
11334 	if (!rdev->ops->disassoc)
11335 		return -EOPNOTSUPP;
11336 
11337 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11338 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11339 		return -EOPNOTSUPP;
11340 
11341 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11342 
11343 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11344 	if (reason_code == 0) {
11345 		/* Reason Code 0 is reserved */
11346 		return -EINVAL;
11347 	}
11348 
11349 	if (info->attrs[NL80211_ATTR_IE]) {
11350 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11351 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11352 	}
11353 
11354 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11355 
11356 	wdev_lock(dev->ieee80211_ptr);
11357 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
11358 				     local_state_change);
11359 	wdev_unlock(dev->ieee80211_ptr);
11360 	return err;
11361 }
11362 
11363 static bool
11364 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
11365 			 int mcast_rate[NUM_NL80211_BANDS],
11366 			 int rateval)
11367 {
11368 	struct wiphy *wiphy = &rdev->wiphy;
11369 	bool found = false;
11370 	int band, i;
11371 
11372 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
11373 		struct ieee80211_supported_band *sband;
11374 
11375 		sband = wiphy->bands[band];
11376 		if (!sband)
11377 			continue;
11378 
11379 		for (i = 0; i < sband->n_bitrates; i++) {
11380 			if (sband->bitrates[i].bitrate == rateval) {
11381 				mcast_rate[band] = i + 1;
11382 				found = true;
11383 				break;
11384 			}
11385 		}
11386 	}
11387 
11388 	return found;
11389 }
11390 
11391 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
11392 {
11393 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11394 	struct net_device *dev = info->user_ptr[1];
11395 	struct cfg80211_ibss_params ibss;
11396 	struct wiphy *wiphy;
11397 	struct cfg80211_cached_keys *connkeys = NULL;
11398 	int err;
11399 
11400 	memset(&ibss, 0, sizeof(ibss));
11401 
11402 	if (!info->attrs[NL80211_ATTR_SSID] ||
11403 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11404 		return -EINVAL;
11405 
11406 	ibss.beacon_interval = 100;
11407 
11408 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
11409 		ibss.beacon_interval =
11410 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11411 
11412 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
11413 					   ibss.beacon_interval);
11414 	if (err)
11415 		return err;
11416 
11417 	if (!rdev->ops->join_ibss)
11418 		return -EOPNOTSUPP;
11419 
11420 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11421 		return -EOPNOTSUPP;
11422 
11423 	wiphy = &rdev->wiphy;
11424 
11425 	if (info->attrs[NL80211_ATTR_MAC]) {
11426 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11427 
11428 		if (!is_valid_ether_addr(ibss.bssid))
11429 			return -EINVAL;
11430 	}
11431 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11432 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11433 
11434 	if (info->attrs[NL80211_ATTR_IE]) {
11435 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11436 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11437 	}
11438 
11439 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
11440 	if (err)
11441 		return err;
11442 
11443 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
11444 				     NL80211_IFTYPE_ADHOC))
11445 		return -EINVAL;
11446 
11447 	switch (ibss.chandef.width) {
11448 	case NL80211_CHAN_WIDTH_5:
11449 	case NL80211_CHAN_WIDTH_10:
11450 	case NL80211_CHAN_WIDTH_20_NOHT:
11451 		break;
11452 	case NL80211_CHAN_WIDTH_20:
11453 	case NL80211_CHAN_WIDTH_40:
11454 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11455 			return -EINVAL;
11456 		break;
11457 	case NL80211_CHAN_WIDTH_80:
11458 	case NL80211_CHAN_WIDTH_80P80:
11459 	case NL80211_CHAN_WIDTH_160:
11460 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11461 			return -EINVAL;
11462 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
11463 					     NL80211_EXT_FEATURE_VHT_IBSS))
11464 			return -EINVAL;
11465 		break;
11466 	case NL80211_CHAN_WIDTH_320:
11467 		return -EINVAL;
11468 	default:
11469 		return -EINVAL;
11470 	}
11471 
11472 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
11473 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
11474 
11475 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11476 		u8 *rates =
11477 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11478 		int n_rates =
11479 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11480 		struct ieee80211_supported_band *sband =
11481 			wiphy->bands[ibss.chandef.chan->band];
11482 
11483 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11484 					     &ibss.basic_rates);
11485 		if (err)
11486 			return err;
11487 	}
11488 
11489 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11490 		memcpy(&ibss.ht_capa_mask,
11491 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11492 		       sizeof(ibss.ht_capa_mask));
11493 
11494 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11495 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11496 			return -EINVAL;
11497 		memcpy(&ibss.ht_capa,
11498 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11499 		       sizeof(ibss.ht_capa));
11500 	}
11501 
11502 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11503 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
11504 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11505 		return -EINVAL;
11506 
11507 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11508 		bool no_ht = false;
11509 
11510 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
11511 		if (IS_ERR(connkeys))
11512 			return PTR_ERR(connkeys);
11513 
11514 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
11515 		    no_ht) {
11516 			kfree_sensitive(connkeys);
11517 			return -EINVAL;
11518 		}
11519 	}
11520 
11521 	ibss.control_port =
11522 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
11523 
11524 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11525 		int r = validate_pae_over_nl80211(rdev, info);
11526 
11527 		if (r < 0) {
11528 			kfree_sensitive(connkeys);
11529 			return r;
11530 		}
11531 
11532 		ibss.control_port_over_nl80211 = true;
11533 	}
11534 
11535 	ibss.userspace_handles_dfs =
11536 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11537 
11538 	wdev_lock(dev->ieee80211_ptr);
11539 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
11540 	if (err)
11541 		kfree_sensitive(connkeys);
11542 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
11543 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11544 	wdev_unlock(dev->ieee80211_ptr);
11545 
11546 	return err;
11547 }
11548 
11549 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
11550 {
11551 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11552 	struct net_device *dev = info->user_ptr[1];
11553 
11554 	if (!rdev->ops->leave_ibss)
11555 		return -EOPNOTSUPP;
11556 
11557 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11558 		return -EOPNOTSUPP;
11559 
11560 	return cfg80211_leave_ibss(rdev, dev, false);
11561 }
11562 
11563 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
11564 {
11565 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11566 	struct net_device *dev = info->user_ptr[1];
11567 	int mcast_rate[NUM_NL80211_BANDS];
11568 	u32 nla_rate;
11569 
11570 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
11571 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
11572 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
11573 		return -EOPNOTSUPP;
11574 
11575 	if (!rdev->ops->set_mcast_rate)
11576 		return -EOPNOTSUPP;
11577 
11578 	memset(mcast_rate, 0, sizeof(mcast_rate));
11579 
11580 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
11581 		return -EINVAL;
11582 
11583 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
11584 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
11585 		return -EINVAL;
11586 
11587 	return rdev_set_mcast_rate(rdev, dev, mcast_rate);
11588 }
11589 
11590 static struct sk_buff *
11591 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
11592 			    struct wireless_dev *wdev, int approxlen,
11593 			    u32 portid, u32 seq, enum nl80211_commands cmd,
11594 			    enum nl80211_attrs attr,
11595 			    const struct nl80211_vendor_cmd_info *info,
11596 			    gfp_t gfp)
11597 {
11598 	struct sk_buff *skb;
11599 	void *hdr;
11600 	struct nlattr *data;
11601 
11602 	skb = nlmsg_new(approxlen + 100, gfp);
11603 	if (!skb)
11604 		return NULL;
11605 
11606 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
11607 	if (!hdr) {
11608 		kfree_skb(skb);
11609 		return NULL;
11610 	}
11611 
11612 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11613 		goto nla_put_failure;
11614 
11615 	if (info) {
11616 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
11617 				info->vendor_id))
11618 			goto nla_put_failure;
11619 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
11620 				info->subcmd))
11621 			goto nla_put_failure;
11622 	}
11623 
11624 	if (wdev) {
11625 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
11626 				      wdev_id(wdev), NL80211_ATTR_PAD))
11627 			goto nla_put_failure;
11628 		if (wdev->netdev &&
11629 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
11630 				wdev->netdev->ifindex))
11631 			goto nla_put_failure;
11632 	}
11633 
11634 	data = nla_nest_start_noflag(skb, attr);
11635 	if (!data)
11636 		goto nla_put_failure;
11637 
11638 	((void **)skb->cb)[0] = rdev;
11639 	((void **)skb->cb)[1] = hdr;
11640 	((void **)skb->cb)[2] = data;
11641 
11642 	return skb;
11643 
11644  nla_put_failure:
11645 	kfree_skb(skb);
11646 	return NULL;
11647 }
11648 
11649 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
11650 					   struct wireless_dev *wdev,
11651 					   enum nl80211_commands cmd,
11652 					   enum nl80211_attrs attr,
11653 					   unsigned int portid,
11654 					   int vendor_event_idx,
11655 					   int approxlen, gfp_t gfp)
11656 {
11657 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11658 	const struct nl80211_vendor_cmd_info *info;
11659 
11660 	switch (cmd) {
11661 	case NL80211_CMD_TESTMODE:
11662 		if (WARN_ON(vendor_event_idx != -1))
11663 			return NULL;
11664 		info = NULL;
11665 		break;
11666 	case NL80211_CMD_VENDOR:
11667 		if (WARN_ON(vendor_event_idx < 0 ||
11668 			    vendor_event_idx >= wiphy->n_vendor_events))
11669 			return NULL;
11670 		info = &wiphy->vendor_events[vendor_event_idx];
11671 		break;
11672 	default:
11673 		WARN_ON(1);
11674 		return NULL;
11675 	}
11676 
11677 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
11678 					   cmd, attr, info, gfp);
11679 }
11680 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
11681 
11682 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
11683 {
11684 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
11685 	void *hdr = ((void **)skb->cb)[1];
11686 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
11687 	struct nlattr *data = ((void **)skb->cb)[2];
11688 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
11689 
11690 	/* clear CB data for netlink core to own from now on */
11691 	memset(skb->cb, 0, sizeof(skb->cb));
11692 
11693 	nla_nest_end(skb, data);
11694 	genlmsg_end(skb, hdr);
11695 
11696 	if (nlhdr->nlmsg_pid) {
11697 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
11698 				nlhdr->nlmsg_pid);
11699 	} else {
11700 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
11701 			mcgrp = NL80211_MCGRP_VENDOR;
11702 
11703 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11704 					skb, 0, mcgrp, gfp);
11705 	}
11706 }
11707 EXPORT_SYMBOL(__cfg80211_send_event_skb);
11708 
11709 #ifdef CONFIG_NL80211_TESTMODE
11710 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
11711 {
11712 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11713 	struct wireless_dev *wdev;
11714 	int err;
11715 
11716 	lockdep_assert_held(&rdev->wiphy.mtx);
11717 
11718 	wdev = __cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
11719 					  info->attrs);
11720 
11721 	if (!rdev->ops->testmode_cmd)
11722 		return -EOPNOTSUPP;
11723 
11724 	if (IS_ERR(wdev)) {
11725 		err = PTR_ERR(wdev);
11726 		if (err != -EINVAL)
11727 			return err;
11728 		wdev = NULL;
11729 	} else if (wdev->wiphy != &rdev->wiphy) {
11730 		return -EINVAL;
11731 	}
11732 
11733 	if (!info->attrs[NL80211_ATTR_TESTDATA])
11734 		return -EINVAL;
11735 
11736 	rdev->cur_cmd_info = info;
11737 	err = rdev_testmode_cmd(rdev, wdev,
11738 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
11739 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
11740 	rdev->cur_cmd_info = NULL;
11741 
11742 	return err;
11743 }
11744 
11745 static int nl80211_testmode_dump(struct sk_buff *skb,
11746 				 struct netlink_callback *cb)
11747 {
11748 	struct cfg80211_registered_device *rdev;
11749 	struct nlattr **attrbuf = NULL;
11750 	int err;
11751 	long phy_idx;
11752 	void *data = NULL;
11753 	int data_len = 0;
11754 
11755 	rtnl_lock();
11756 
11757 	if (cb->args[0]) {
11758 		/*
11759 		 * 0 is a valid index, but not valid for args[0],
11760 		 * so we need to offset by 1.
11761 		 */
11762 		phy_idx = cb->args[0] - 1;
11763 
11764 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
11765 		if (!rdev) {
11766 			err = -ENOENT;
11767 			goto out_err;
11768 		}
11769 	} else {
11770 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
11771 				  GFP_KERNEL);
11772 		if (!attrbuf) {
11773 			err = -ENOMEM;
11774 			goto out_err;
11775 		}
11776 
11777 		err = nlmsg_parse_deprecated(cb->nlh,
11778 					     GENL_HDRLEN + nl80211_fam.hdrsize,
11779 					     attrbuf, nl80211_fam.maxattr,
11780 					     nl80211_policy, NULL);
11781 		if (err)
11782 			goto out_err;
11783 
11784 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
11785 		if (IS_ERR(rdev)) {
11786 			err = PTR_ERR(rdev);
11787 			goto out_err;
11788 		}
11789 		phy_idx = rdev->wiphy_idx;
11790 
11791 		if (attrbuf[NL80211_ATTR_TESTDATA])
11792 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
11793 	}
11794 
11795 	if (cb->args[1]) {
11796 		data = nla_data((void *)cb->args[1]);
11797 		data_len = nla_len((void *)cb->args[1]);
11798 	}
11799 
11800 	if (!rdev->ops->testmode_dump) {
11801 		err = -EOPNOTSUPP;
11802 		goto out_err;
11803 	}
11804 
11805 	while (1) {
11806 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
11807 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
11808 					   NL80211_CMD_TESTMODE);
11809 		struct nlattr *tmdata;
11810 
11811 		if (!hdr)
11812 			break;
11813 
11814 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
11815 			genlmsg_cancel(skb, hdr);
11816 			break;
11817 		}
11818 
11819 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
11820 		if (!tmdata) {
11821 			genlmsg_cancel(skb, hdr);
11822 			break;
11823 		}
11824 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
11825 		nla_nest_end(skb, tmdata);
11826 
11827 		if (err == -ENOBUFS || err == -ENOENT) {
11828 			genlmsg_cancel(skb, hdr);
11829 			break;
11830 		} else if (err) {
11831 			genlmsg_cancel(skb, hdr);
11832 			goto out_err;
11833 		}
11834 
11835 		genlmsg_end(skb, hdr);
11836 	}
11837 
11838 	err = skb->len;
11839 	/* see above */
11840 	cb->args[0] = phy_idx + 1;
11841  out_err:
11842 	kfree(attrbuf);
11843 	rtnl_unlock();
11844 	return err;
11845 }
11846 #endif
11847 
11848 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
11849 {
11850 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11851 	struct net_device *dev = info->user_ptr[1];
11852 	struct cfg80211_connect_params connect;
11853 	struct wiphy *wiphy;
11854 	struct cfg80211_cached_keys *connkeys = NULL;
11855 	u32 freq = 0;
11856 	int err;
11857 
11858 	memset(&connect, 0, sizeof(connect));
11859 
11860 	if (!info->attrs[NL80211_ATTR_SSID] ||
11861 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
11862 		return -EINVAL;
11863 
11864 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11865 		connect.auth_type =
11866 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11867 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
11868 					     NL80211_CMD_CONNECT))
11869 			return -EINVAL;
11870 	} else
11871 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
11872 
11873 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
11874 
11875 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
11876 	    !wiphy_ext_feature_isset(&rdev->wiphy,
11877 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
11878 		return -EINVAL;
11879 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
11880 
11881 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
11882 				      NL80211_MAX_NR_CIPHER_SUITES);
11883 	if (err)
11884 		return err;
11885 
11886 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11887 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11888 		return -EOPNOTSUPP;
11889 
11890 	wiphy = &rdev->wiphy;
11891 
11892 	connect.bg_scan_period = -1;
11893 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
11894 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
11895 		connect.bg_scan_period =
11896 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
11897 	}
11898 
11899 	if (info->attrs[NL80211_ATTR_MAC])
11900 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11901 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
11902 		connect.bssid_hint =
11903 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
11904 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11905 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11906 
11907 	if (info->attrs[NL80211_ATTR_IE]) {
11908 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11909 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11910 	}
11911 
11912 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
11913 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11914 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
11915 		    !wiphy_ext_feature_isset(&rdev->wiphy,
11916 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
11917 			return -EOPNOTSUPP;
11918 	} else {
11919 		connect.mfp = NL80211_MFP_NO;
11920 	}
11921 
11922 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
11923 		connect.prev_bssid =
11924 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11925 
11926 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
11927 		freq = MHZ_TO_KHZ(nla_get_u32(
11928 					info->attrs[NL80211_ATTR_WIPHY_FREQ]));
11929 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
11930 		freq +=
11931 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
11932 
11933 	if (freq) {
11934 		connect.channel = nl80211_get_valid_chan(wiphy, freq);
11935 		if (!connect.channel)
11936 			return -EINVAL;
11937 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
11938 		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
11939 		freq = MHZ_TO_KHZ(freq);
11940 		connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
11941 		if (!connect.channel_hint)
11942 			return -EINVAL;
11943 	}
11944 
11945 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
11946 		connect.edmg.channels =
11947 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
11948 
11949 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
11950 			connect.edmg.bw_config =
11951 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
11952 	}
11953 
11954 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11955 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
11956 		if (IS_ERR(connkeys))
11957 			return PTR_ERR(connkeys);
11958 	}
11959 
11960 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11961 		connect.flags |= ASSOC_REQ_DISABLE_HT;
11962 
11963 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11964 		memcpy(&connect.ht_capa_mask,
11965 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11966 		       sizeof(connect.ht_capa_mask));
11967 
11968 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11969 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
11970 			kfree_sensitive(connkeys);
11971 			return -EINVAL;
11972 		}
11973 		memcpy(&connect.ht_capa,
11974 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11975 		       sizeof(connect.ht_capa));
11976 	}
11977 
11978 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11979 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
11980 
11981 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11982 		connect.flags |= ASSOC_REQ_DISABLE_HE;
11983 
11984 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11985 		connect.flags |= ASSOC_REQ_DISABLE_EHT;
11986 
11987 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11988 		memcpy(&connect.vht_capa_mask,
11989 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11990 		       sizeof(connect.vht_capa_mask));
11991 
11992 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11993 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
11994 			kfree_sensitive(connkeys);
11995 			return -EINVAL;
11996 		}
11997 		memcpy(&connect.vht_capa,
11998 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11999 		       sizeof(connect.vht_capa));
12000 	}
12001 
12002 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
12003 		if (!((rdev->wiphy.features &
12004 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
12005 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
12006 		    !wiphy_ext_feature_isset(&rdev->wiphy,
12007 					     NL80211_EXT_FEATURE_RRM)) {
12008 			kfree_sensitive(connkeys);
12009 			return -EINVAL;
12010 		}
12011 		connect.flags |= ASSOC_REQ_USE_RRM;
12012 	}
12013 
12014 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
12015 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
12016 		kfree_sensitive(connkeys);
12017 		return -EOPNOTSUPP;
12018 	}
12019 
12020 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
12021 		/* bss selection makes no sense if bssid is set */
12022 		if (connect.bssid) {
12023 			kfree_sensitive(connkeys);
12024 			return -EINVAL;
12025 		}
12026 
12027 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
12028 				       wiphy, &connect.bss_select);
12029 		if (err) {
12030 			kfree_sensitive(connkeys);
12031 			return err;
12032 		}
12033 	}
12034 
12035 	if (wiphy_ext_feature_isset(&rdev->wiphy,
12036 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
12037 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
12038 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
12039 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
12040 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12041 		connect.fils_erp_username =
12042 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12043 		connect.fils_erp_username_len =
12044 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12045 		connect.fils_erp_realm =
12046 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12047 		connect.fils_erp_realm_len =
12048 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12049 		connect.fils_erp_next_seq_num =
12050 			nla_get_u16(
12051 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
12052 		connect.fils_erp_rrk =
12053 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12054 		connect.fils_erp_rrk_len =
12055 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12056 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
12057 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
12058 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
12059 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12060 		kfree_sensitive(connkeys);
12061 		return -EINVAL;
12062 	}
12063 
12064 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
12065 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12066 			kfree_sensitive(connkeys);
12067 			GENL_SET_ERR_MSG(info,
12068 					 "external auth requires connection ownership");
12069 			return -EINVAL;
12070 		}
12071 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
12072 	}
12073 
12074 	if (nla_get_flag(info->attrs[NL80211_ATTR_MLO_SUPPORT]))
12075 		connect.flags |= CONNECT_REQ_MLO_SUPPORT;
12076 
12077 	wdev_lock(dev->ieee80211_ptr);
12078 
12079 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
12080 			       connect.prev_bssid);
12081 	if (err)
12082 		kfree_sensitive(connkeys);
12083 
12084 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
12085 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
12086 		if (connect.bssid)
12087 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
12088 			       connect.bssid, ETH_ALEN);
12089 		else
12090 			eth_zero_addr(dev->ieee80211_ptr->disconnect_bssid);
12091 	}
12092 
12093 	wdev_unlock(dev->ieee80211_ptr);
12094 
12095 	return err;
12096 }
12097 
12098 static int nl80211_update_connect_params(struct sk_buff *skb,
12099 					 struct genl_info *info)
12100 {
12101 	struct cfg80211_connect_params connect = {};
12102 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12103 	struct net_device *dev = info->user_ptr[1];
12104 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12105 	bool fils_sk_offload;
12106 	u32 auth_type;
12107 	u32 changed = 0;
12108 	int ret;
12109 
12110 	if (!rdev->ops->update_connect_params)
12111 		return -EOPNOTSUPP;
12112 
12113 	if (info->attrs[NL80211_ATTR_IE]) {
12114 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12115 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12116 		changed |= UPDATE_ASSOC_IES;
12117 	}
12118 
12119 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
12120 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
12121 
12122 	/*
12123 	 * when driver supports fils-sk offload all attributes must be
12124 	 * provided. So the else covers "fils-sk-not-all" and
12125 	 * "no-fils-sk-any".
12126 	 */
12127 	if (fils_sk_offload &&
12128 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
12129 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
12130 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
12131 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12132 		connect.fils_erp_username =
12133 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12134 		connect.fils_erp_username_len =
12135 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
12136 		connect.fils_erp_realm =
12137 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12138 		connect.fils_erp_realm_len =
12139 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
12140 		connect.fils_erp_next_seq_num =
12141 			nla_get_u16(
12142 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
12143 		connect.fils_erp_rrk =
12144 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12145 		connect.fils_erp_rrk_len =
12146 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
12147 		changed |= UPDATE_FILS_ERP_INFO;
12148 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
12149 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
12150 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
12151 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
12152 		return -EINVAL;
12153 	}
12154 
12155 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
12156 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
12157 		if (!nl80211_valid_auth_type(rdev, auth_type,
12158 					     NL80211_CMD_CONNECT))
12159 			return -EINVAL;
12160 
12161 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
12162 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
12163 			return -EINVAL;
12164 
12165 		connect.auth_type = auth_type;
12166 		changed |= UPDATE_AUTH_TYPE;
12167 	}
12168 
12169 	wdev_lock(dev->ieee80211_ptr);
12170 	if (!wdev->connected)
12171 		ret = -ENOLINK;
12172 	else
12173 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
12174 	wdev_unlock(dev->ieee80211_ptr);
12175 
12176 	return ret;
12177 }
12178 
12179 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
12180 {
12181 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12182 	struct net_device *dev = info->user_ptr[1];
12183 	u16 reason;
12184 	int ret;
12185 
12186 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
12187 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
12188 		return -EPERM;
12189 
12190 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
12191 		reason = WLAN_REASON_DEAUTH_LEAVING;
12192 	else
12193 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
12194 
12195 	if (reason == 0)
12196 		return -EINVAL;
12197 
12198 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12199 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12200 		return -EOPNOTSUPP;
12201 
12202 	wdev_lock(dev->ieee80211_ptr);
12203 	ret = cfg80211_disconnect(rdev, dev, reason, true);
12204 	wdev_unlock(dev->ieee80211_ptr);
12205 	return ret;
12206 }
12207 
12208 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
12209 {
12210 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12211 	struct net *net;
12212 	int err;
12213 
12214 	if (info->attrs[NL80211_ATTR_PID]) {
12215 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
12216 
12217 		net = get_net_ns_by_pid(pid);
12218 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
12219 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
12220 
12221 		net = get_net_ns_by_fd(fd);
12222 	} else {
12223 		return -EINVAL;
12224 	}
12225 
12226 	if (IS_ERR(net))
12227 		return PTR_ERR(net);
12228 
12229 	err = 0;
12230 
12231 	/* check if anything to do */
12232 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
12233 		err = cfg80211_switch_netns(rdev, net);
12234 
12235 	put_net(net);
12236 	return err;
12237 }
12238 
12239 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
12240 {
12241 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12242 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
12243 			struct cfg80211_pmksa *pmksa) = NULL;
12244 	struct net_device *dev = info->user_ptr[1];
12245 	struct cfg80211_pmksa pmksa;
12246 
12247 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
12248 
12249 	if (!info->attrs[NL80211_ATTR_PMKID])
12250 		return -EINVAL;
12251 
12252 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
12253 
12254 	if (info->attrs[NL80211_ATTR_MAC]) {
12255 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
12256 	} else if (info->attrs[NL80211_ATTR_SSID] &&
12257 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
12258 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
12259 		    info->attrs[NL80211_ATTR_PMK])) {
12260 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
12261 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
12262 		pmksa.cache_id =
12263 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
12264 	} else {
12265 		return -EINVAL;
12266 	}
12267 	if (info->attrs[NL80211_ATTR_PMK]) {
12268 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
12269 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
12270 	}
12271 
12272 	if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
12273 		pmksa.pmk_lifetime =
12274 			nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
12275 
12276 	if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
12277 		pmksa.pmk_reauth_threshold =
12278 			nla_get_u8(
12279 				info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
12280 
12281 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12282 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
12283 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
12284 	      wiphy_ext_feature_isset(&rdev->wiphy,
12285 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
12286 		return -EOPNOTSUPP;
12287 
12288 	switch (info->genlhdr->cmd) {
12289 	case NL80211_CMD_SET_PMKSA:
12290 		rdev_ops = rdev->ops->set_pmksa;
12291 		break;
12292 	case NL80211_CMD_DEL_PMKSA:
12293 		rdev_ops = rdev->ops->del_pmksa;
12294 		break;
12295 	default:
12296 		WARN_ON(1);
12297 		break;
12298 	}
12299 
12300 	if (!rdev_ops)
12301 		return -EOPNOTSUPP;
12302 
12303 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
12304 }
12305 
12306 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
12307 {
12308 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12309 	struct net_device *dev = info->user_ptr[1];
12310 
12311 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12312 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12313 		return -EOPNOTSUPP;
12314 
12315 	if (!rdev->ops->flush_pmksa)
12316 		return -EOPNOTSUPP;
12317 
12318 	return rdev_flush_pmksa(rdev, dev);
12319 }
12320 
12321 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
12322 {
12323 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12324 	struct net_device *dev = info->user_ptr[1];
12325 	u8 action_code, dialog_token;
12326 	u32 peer_capability = 0;
12327 	u16 status_code;
12328 	u8 *peer;
12329 	int link_id;
12330 	bool initiator;
12331 
12332 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12333 	    !rdev->ops->tdls_mgmt)
12334 		return -EOPNOTSUPP;
12335 
12336 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
12337 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
12338 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
12339 	    !info->attrs[NL80211_ATTR_IE] ||
12340 	    !info->attrs[NL80211_ATTR_MAC])
12341 		return -EINVAL;
12342 
12343 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12344 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
12345 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
12346 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
12347 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
12348 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
12349 		peer_capability =
12350 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
12351 	link_id = nl80211_link_id_or_invalid(info->attrs);
12352 
12353 	return rdev_tdls_mgmt(rdev, dev, peer, link_id, action_code,
12354 			      dialog_token, status_code, peer_capability,
12355 			      initiator,
12356 			      nla_data(info->attrs[NL80211_ATTR_IE]),
12357 			      nla_len(info->attrs[NL80211_ATTR_IE]));
12358 }
12359 
12360 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
12361 {
12362 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12363 	struct net_device *dev = info->user_ptr[1];
12364 	enum nl80211_tdls_operation operation;
12365 	u8 *peer;
12366 
12367 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12368 	    !rdev->ops->tdls_oper)
12369 		return -EOPNOTSUPP;
12370 
12371 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
12372 	    !info->attrs[NL80211_ATTR_MAC])
12373 		return -EINVAL;
12374 
12375 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
12376 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12377 
12378 	return rdev_tdls_oper(rdev, dev, peer, operation);
12379 }
12380 
12381 static int nl80211_remain_on_channel(struct sk_buff *skb,
12382 				     struct genl_info *info)
12383 {
12384 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12385 	unsigned int link_id = nl80211_link_id(info->attrs);
12386 	struct wireless_dev *wdev = info->user_ptr[1];
12387 	struct cfg80211_chan_def chandef;
12388 	struct sk_buff *msg;
12389 	void *hdr;
12390 	u64 cookie;
12391 	u32 duration;
12392 	int err;
12393 
12394 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
12395 	    !info->attrs[NL80211_ATTR_DURATION])
12396 		return -EINVAL;
12397 
12398 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12399 
12400 	if (!rdev->ops->remain_on_channel ||
12401 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
12402 		return -EOPNOTSUPP;
12403 
12404 	/*
12405 	 * We should be on that channel for at least a minimum amount of
12406 	 * time (10ms) but no longer than the driver supports.
12407 	 */
12408 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12409 	    duration > rdev->wiphy.max_remain_on_channel_duration)
12410 		return -EINVAL;
12411 
12412 	err = nl80211_parse_chandef(rdev, info, &chandef);
12413 	if (err)
12414 		return err;
12415 
12416 	wdev_lock(wdev);
12417 	if (!cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12418 		const struct cfg80211_chan_def *oper_chandef, *compat_chandef;
12419 
12420 		oper_chandef = wdev_chandef(wdev, link_id);
12421 
12422 		if (WARN_ON(!oper_chandef)) {
12423 			/* cannot happen since we must beacon to get here */
12424 			WARN_ON(1);
12425 			wdev_unlock(wdev);
12426 			return -EBUSY;
12427 		}
12428 
12429 		/* note: returns first one if identical chandefs */
12430 		compat_chandef = cfg80211_chandef_compatible(&chandef,
12431 							     oper_chandef);
12432 
12433 		if (compat_chandef != &chandef) {
12434 			wdev_unlock(wdev);
12435 			return -EBUSY;
12436 		}
12437 	}
12438 	wdev_unlock(wdev);
12439 
12440 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12441 	if (!msg)
12442 		return -ENOMEM;
12443 
12444 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12445 			     NL80211_CMD_REMAIN_ON_CHANNEL);
12446 	if (!hdr) {
12447 		err = -ENOBUFS;
12448 		goto free_msg;
12449 	}
12450 
12451 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
12452 				     duration, &cookie);
12453 
12454 	if (err)
12455 		goto free_msg;
12456 
12457 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12458 			      NL80211_ATTR_PAD))
12459 		goto nla_put_failure;
12460 
12461 	genlmsg_end(msg, hdr);
12462 
12463 	return genlmsg_reply(msg, info);
12464 
12465  nla_put_failure:
12466 	err = -ENOBUFS;
12467  free_msg:
12468 	nlmsg_free(msg);
12469 	return err;
12470 }
12471 
12472 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
12473 					    struct genl_info *info)
12474 {
12475 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12476 	struct wireless_dev *wdev = info->user_ptr[1];
12477 	u64 cookie;
12478 
12479 	if (!info->attrs[NL80211_ATTR_COOKIE])
12480 		return -EINVAL;
12481 
12482 	if (!rdev->ops->cancel_remain_on_channel)
12483 		return -EOPNOTSUPP;
12484 
12485 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12486 
12487 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
12488 }
12489 
12490 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
12491 				       struct genl_info *info)
12492 {
12493 	struct cfg80211_bitrate_mask mask;
12494 	unsigned int link_id = nl80211_link_id(info->attrs);
12495 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12496 	struct net_device *dev = info->user_ptr[1];
12497 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12498 	int err;
12499 
12500 	if (!rdev->ops->set_bitrate_mask)
12501 		return -EOPNOTSUPP;
12502 
12503 	wdev_lock(wdev);
12504 	err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12505 					    NL80211_ATTR_TX_RATES, &mask,
12506 					    dev, true, link_id);
12507 	if (err)
12508 		goto out;
12509 
12510 	err = rdev_set_bitrate_mask(rdev, dev, link_id, NULL, &mask);
12511 out:
12512 	wdev_unlock(wdev);
12513 	return err;
12514 }
12515 
12516 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
12517 {
12518 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12519 	struct wireless_dev *wdev = info->user_ptr[1];
12520 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
12521 
12522 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
12523 		return -EINVAL;
12524 
12525 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
12526 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
12527 
12528 	switch (wdev->iftype) {
12529 	case NL80211_IFTYPE_STATION:
12530 	case NL80211_IFTYPE_ADHOC:
12531 	case NL80211_IFTYPE_P2P_CLIENT:
12532 	case NL80211_IFTYPE_AP:
12533 	case NL80211_IFTYPE_AP_VLAN:
12534 	case NL80211_IFTYPE_MESH_POINT:
12535 	case NL80211_IFTYPE_P2P_GO:
12536 	case NL80211_IFTYPE_P2P_DEVICE:
12537 		break;
12538 	case NL80211_IFTYPE_NAN:
12539 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12540 					     NL80211_EXT_FEATURE_SECURE_NAN))
12541 			return -EOPNOTSUPP;
12542 		break;
12543 	default:
12544 		return -EOPNOTSUPP;
12545 	}
12546 
12547 	/* not much point in registering if we can't reply */
12548 	if (!rdev->ops->mgmt_tx)
12549 		return -EOPNOTSUPP;
12550 
12551 	if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
12552 	    !wiphy_ext_feature_isset(&rdev->wiphy,
12553 				     NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
12554 		GENL_SET_ERR_MSG(info,
12555 				 "multicast RX registrations are not supported");
12556 		return -EOPNOTSUPP;
12557 	}
12558 
12559 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
12560 					   nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12561 					   nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12562 					   info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
12563 					   info->extack);
12564 }
12565 
12566 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
12567 {
12568 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12569 	struct wireless_dev *wdev = info->user_ptr[1];
12570 	struct cfg80211_chan_def chandef;
12571 	int err;
12572 	void *hdr = NULL;
12573 	u64 cookie;
12574 	struct sk_buff *msg = NULL;
12575 	struct cfg80211_mgmt_tx_params params = {
12576 		.dont_wait_for_ack =
12577 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
12578 	};
12579 
12580 	if (!info->attrs[NL80211_ATTR_FRAME])
12581 		return -EINVAL;
12582 
12583 	if (!rdev->ops->mgmt_tx)
12584 		return -EOPNOTSUPP;
12585 
12586 	switch (wdev->iftype) {
12587 	case NL80211_IFTYPE_P2P_DEVICE:
12588 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
12589 			return -EINVAL;
12590 		break;
12591 	case NL80211_IFTYPE_STATION:
12592 	case NL80211_IFTYPE_ADHOC:
12593 	case NL80211_IFTYPE_P2P_CLIENT:
12594 	case NL80211_IFTYPE_AP:
12595 	case NL80211_IFTYPE_AP_VLAN:
12596 	case NL80211_IFTYPE_MESH_POINT:
12597 	case NL80211_IFTYPE_P2P_GO:
12598 		break;
12599 	case NL80211_IFTYPE_NAN:
12600 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12601 					     NL80211_EXT_FEATURE_SECURE_NAN))
12602 			return -EOPNOTSUPP;
12603 		break;
12604 	default:
12605 		return -EOPNOTSUPP;
12606 	}
12607 
12608 	if (info->attrs[NL80211_ATTR_DURATION]) {
12609 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12610 			return -EINVAL;
12611 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12612 
12613 		/*
12614 		 * We should wait on the channel for at least a minimum amount
12615 		 * of time (10ms) but no longer than the driver supports.
12616 		 */
12617 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12618 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
12619 			return -EINVAL;
12620 	}
12621 
12622 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
12623 
12624 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12625 		return -EINVAL;
12626 
12627 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
12628 
12629 	/* get the channel if any has been specified, otherwise pass NULL to
12630 	 * the driver. The latter will use the current one
12631 	 */
12632 	chandef.chan = NULL;
12633 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12634 		err = nl80211_parse_chandef(rdev, info, &chandef);
12635 		if (err)
12636 			return err;
12637 	}
12638 
12639 	if (!chandef.chan && params.offchan)
12640 		return -EINVAL;
12641 
12642 	wdev_lock(wdev);
12643 	if (params.offchan &&
12644 	    !cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12645 		wdev_unlock(wdev);
12646 		return -EBUSY;
12647 	}
12648 
12649 	params.link_id = nl80211_link_id_or_invalid(info->attrs);
12650 	/*
12651 	 * This now races due to the unlock, but we cannot check
12652 	 * the valid links for the _station_ anyway, so that's up
12653 	 * to the driver.
12654 	 */
12655 	if (params.link_id >= 0 &&
12656 	    !(wdev->valid_links & BIT(params.link_id))) {
12657 		wdev_unlock(wdev);
12658 		return -EINVAL;
12659 	}
12660 	wdev_unlock(wdev);
12661 
12662 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
12663 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
12664 
12665 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
12666 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12667 		int i;
12668 
12669 		if (len % sizeof(u16))
12670 			return -EINVAL;
12671 
12672 		params.n_csa_offsets = len / sizeof(u16);
12673 		params.csa_offsets =
12674 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12675 
12676 		/* check that all the offsets fit the frame */
12677 		for (i = 0; i < params.n_csa_offsets; i++) {
12678 			if (params.csa_offsets[i] >= params.len)
12679 				return -EINVAL;
12680 		}
12681 	}
12682 
12683 	if (!params.dont_wait_for_ack) {
12684 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12685 		if (!msg)
12686 			return -ENOMEM;
12687 
12688 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12689 				     NL80211_CMD_FRAME);
12690 		if (!hdr) {
12691 			err = -ENOBUFS;
12692 			goto free_msg;
12693 		}
12694 	}
12695 
12696 	params.chan = chandef.chan;
12697 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
12698 	if (err)
12699 		goto free_msg;
12700 
12701 	if (msg) {
12702 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12703 				      NL80211_ATTR_PAD))
12704 			goto nla_put_failure;
12705 
12706 		genlmsg_end(msg, hdr);
12707 		return genlmsg_reply(msg, info);
12708 	}
12709 
12710 	return 0;
12711 
12712  nla_put_failure:
12713 	err = -ENOBUFS;
12714  free_msg:
12715 	nlmsg_free(msg);
12716 	return err;
12717 }
12718 
12719 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
12720 {
12721 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12722 	struct wireless_dev *wdev = info->user_ptr[1];
12723 	u64 cookie;
12724 
12725 	if (!info->attrs[NL80211_ATTR_COOKIE])
12726 		return -EINVAL;
12727 
12728 	if (!rdev->ops->mgmt_tx_cancel_wait)
12729 		return -EOPNOTSUPP;
12730 
12731 	switch (wdev->iftype) {
12732 	case NL80211_IFTYPE_STATION:
12733 	case NL80211_IFTYPE_ADHOC:
12734 	case NL80211_IFTYPE_P2P_CLIENT:
12735 	case NL80211_IFTYPE_AP:
12736 	case NL80211_IFTYPE_AP_VLAN:
12737 	case NL80211_IFTYPE_P2P_GO:
12738 	case NL80211_IFTYPE_P2P_DEVICE:
12739 		break;
12740 	case NL80211_IFTYPE_NAN:
12741 		if (!wiphy_ext_feature_isset(wdev->wiphy,
12742 					     NL80211_EXT_FEATURE_SECURE_NAN))
12743 			return -EOPNOTSUPP;
12744 		break;
12745 	default:
12746 		return -EOPNOTSUPP;
12747 	}
12748 
12749 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12750 
12751 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
12752 }
12753 
12754 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
12755 {
12756 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12757 	struct wireless_dev *wdev;
12758 	struct net_device *dev = info->user_ptr[1];
12759 	u8 ps_state;
12760 	bool state;
12761 	int err;
12762 
12763 	if (!info->attrs[NL80211_ATTR_PS_STATE])
12764 		return -EINVAL;
12765 
12766 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
12767 
12768 	wdev = dev->ieee80211_ptr;
12769 
12770 	if (!rdev->ops->set_power_mgmt)
12771 		return -EOPNOTSUPP;
12772 
12773 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
12774 
12775 	if (state == wdev->ps)
12776 		return 0;
12777 
12778 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
12779 	if (!err)
12780 		wdev->ps = state;
12781 	return err;
12782 }
12783 
12784 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
12785 {
12786 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12787 	enum nl80211_ps_state ps_state;
12788 	struct wireless_dev *wdev;
12789 	struct net_device *dev = info->user_ptr[1];
12790 	struct sk_buff *msg;
12791 	void *hdr;
12792 	int err;
12793 
12794 	wdev = dev->ieee80211_ptr;
12795 
12796 	if (!rdev->ops->set_power_mgmt)
12797 		return -EOPNOTSUPP;
12798 
12799 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12800 	if (!msg)
12801 		return -ENOMEM;
12802 
12803 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12804 			     NL80211_CMD_GET_POWER_SAVE);
12805 	if (!hdr) {
12806 		err = -ENOBUFS;
12807 		goto free_msg;
12808 	}
12809 
12810 	if (wdev->ps)
12811 		ps_state = NL80211_PS_ENABLED;
12812 	else
12813 		ps_state = NL80211_PS_DISABLED;
12814 
12815 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
12816 		goto nla_put_failure;
12817 
12818 	genlmsg_end(msg, hdr);
12819 	return genlmsg_reply(msg, info);
12820 
12821  nla_put_failure:
12822 	err = -ENOBUFS;
12823  free_msg:
12824 	nlmsg_free(msg);
12825 	return err;
12826 }
12827 
12828 static const struct nla_policy
12829 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
12830 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
12831 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
12832 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
12833 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
12834 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
12835 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
12836 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
12837 };
12838 
12839 static int nl80211_set_cqm_txe(struct genl_info *info,
12840 			       u32 rate, u32 pkts, u32 intvl)
12841 {
12842 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12843 	struct net_device *dev = info->user_ptr[1];
12844 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12845 
12846 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
12847 		return -EINVAL;
12848 
12849 	if (!rdev->ops->set_cqm_txe_config)
12850 		return -EOPNOTSUPP;
12851 
12852 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12853 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12854 		return -EOPNOTSUPP;
12855 
12856 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
12857 }
12858 
12859 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
12860 				    struct net_device *dev,
12861 				    struct cfg80211_cqm_config *cqm_config)
12862 {
12863 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12864 	s32 last, low, high;
12865 	u32 hyst;
12866 	int i, n, low_index;
12867 	int err;
12868 
12869 	/*
12870 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
12871 	 * event has been received yet, we should receive an event after a
12872 	 * connection is established and enough beacons received to calculate
12873 	 * the average.
12874 	 */
12875 	if (!cqm_config->last_rssi_event_value &&
12876 	    wdev->links[0].client.current_bss &&
12877 	    rdev->ops->get_station) {
12878 		struct station_info sinfo = {};
12879 		u8 *mac_addr;
12880 
12881 		mac_addr = wdev->links[0].client.current_bss->pub.bssid;
12882 
12883 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
12884 		if (err)
12885 			return err;
12886 
12887 		cfg80211_sinfo_release_content(&sinfo);
12888 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
12889 			cqm_config->last_rssi_event_value =
12890 				(s8) sinfo.rx_beacon_signal_avg;
12891 	}
12892 
12893 	last = cqm_config->last_rssi_event_value;
12894 	hyst = cqm_config->rssi_hyst;
12895 	n = cqm_config->n_rssi_thresholds;
12896 
12897 	for (i = 0; i < n; i++) {
12898 		i = array_index_nospec(i, n);
12899 		if (last < cqm_config->rssi_thresholds[i])
12900 			break;
12901 	}
12902 
12903 	low_index = i - 1;
12904 	if (low_index >= 0) {
12905 		low_index = array_index_nospec(low_index, n);
12906 		low = cqm_config->rssi_thresholds[low_index] - hyst;
12907 	} else {
12908 		low = S32_MIN;
12909 	}
12910 	if (i < n) {
12911 		i = array_index_nospec(i, n);
12912 		high = cqm_config->rssi_thresholds[i] + hyst - 1;
12913 	} else {
12914 		high = S32_MAX;
12915 	}
12916 
12917 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
12918 }
12919 
12920 static int nl80211_set_cqm_rssi(struct genl_info *info,
12921 				const s32 *thresholds, int n_thresholds,
12922 				u32 hysteresis)
12923 {
12924 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12925 	struct cfg80211_cqm_config *cqm_config = NULL, *old;
12926 	struct net_device *dev = info->user_ptr[1];
12927 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12928 	int i, err;
12929 	s32 prev = S32_MIN;
12930 
12931 	/* Check all values negative and sorted */
12932 	for (i = 0; i < n_thresholds; i++) {
12933 		if (thresholds[i] > 0 || thresholds[i] <= prev)
12934 			return -EINVAL;
12935 
12936 		prev = thresholds[i];
12937 	}
12938 
12939 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12940 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12941 		return -EOPNOTSUPP;
12942 
12943 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
12944 		n_thresholds = 0;
12945 
12946 	wdev_lock(wdev);
12947 	old = rcu_dereference_protected(wdev->cqm_config,
12948 					lockdep_is_held(&wdev->mtx));
12949 
12950 	/* if already disabled just succeed */
12951 	if (!n_thresholds && !old) {
12952 		err = 0;
12953 		goto unlock;
12954 	}
12955 
12956 	if (n_thresholds > 1) {
12957 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
12958 					     NL80211_EXT_FEATURE_CQM_RSSI_LIST) ||
12959 		    !rdev->ops->set_cqm_rssi_range_config) {
12960 			err = -EOPNOTSUPP;
12961 			goto unlock;
12962 		}
12963 	} else {
12964 		if (!rdev->ops->set_cqm_rssi_config) {
12965 			err = -EOPNOTSUPP;
12966 			goto unlock;
12967 		}
12968 	}
12969 
12970 	if (n_thresholds) {
12971 		cqm_config = kzalloc(struct_size(cqm_config, rssi_thresholds,
12972 						 n_thresholds),
12973 				     GFP_KERNEL);
12974 		if (!cqm_config) {
12975 			err = -ENOMEM;
12976 			goto unlock;
12977 		}
12978 
12979 		cqm_config->rssi_hyst = hysteresis;
12980 		cqm_config->n_rssi_thresholds = n_thresholds;
12981 		memcpy(cqm_config->rssi_thresholds, thresholds,
12982 		       flex_array_size(cqm_config, rssi_thresholds,
12983 				       n_thresholds));
12984 		cqm_config->use_range_api = n_thresholds > 1 ||
12985 					    !rdev->ops->set_cqm_rssi_config;
12986 
12987 		rcu_assign_pointer(wdev->cqm_config, cqm_config);
12988 
12989 		if (cqm_config->use_range_api)
12990 			err = cfg80211_cqm_rssi_update(rdev, dev, cqm_config);
12991 		else
12992 			err = rdev_set_cqm_rssi_config(rdev, dev,
12993 						       thresholds[0],
12994 						       hysteresis);
12995 	} else {
12996 		RCU_INIT_POINTER(wdev->cqm_config, NULL);
12997 		/* if enabled as range also disable via range */
12998 		if (old->use_range_api)
12999 			err = rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
13000 		else
13001 			err = rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
13002 	}
13003 
13004 	if (err) {
13005 		rcu_assign_pointer(wdev->cqm_config, old);
13006 		kfree_rcu(cqm_config, rcu_head);
13007 	} else {
13008 		kfree_rcu(old, rcu_head);
13009 	}
13010 unlock:
13011 	wdev_unlock(wdev);
13012 
13013 	return err;
13014 }
13015 
13016 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
13017 {
13018 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
13019 	struct nlattr *cqm;
13020 	int err;
13021 
13022 	cqm = info->attrs[NL80211_ATTR_CQM];
13023 	if (!cqm)
13024 		return -EINVAL;
13025 
13026 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
13027 					  nl80211_attr_cqm_policy,
13028 					  info->extack);
13029 	if (err)
13030 		return err;
13031 
13032 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
13033 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
13034 		const s32 *thresholds =
13035 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
13036 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
13037 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
13038 
13039 		if (len % 4)
13040 			return -EINVAL;
13041 
13042 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
13043 					    hysteresis);
13044 	}
13045 
13046 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
13047 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
13048 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
13049 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
13050 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
13051 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
13052 
13053 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
13054 	}
13055 
13056 	return -EINVAL;
13057 }
13058 
13059 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
13060 {
13061 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13062 	struct net_device *dev = info->user_ptr[1];
13063 	struct ocb_setup setup = {};
13064 	int err;
13065 
13066 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
13067 	if (err)
13068 		return err;
13069 
13070 	return cfg80211_join_ocb(rdev, dev, &setup);
13071 }
13072 
13073 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
13074 {
13075 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13076 	struct net_device *dev = info->user_ptr[1];
13077 
13078 	return cfg80211_leave_ocb(rdev, dev);
13079 }
13080 
13081 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
13082 {
13083 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13084 	struct net_device *dev = info->user_ptr[1];
13085 	struct mesh_config cfg;
13086 	struct mesh_setup setup;
13087 	int err;
13088 
13089 	/* start with default */
13090 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
13091 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
13092 
13093 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
13094 		/* and parse parameters if given */
13095 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
13096 		if (err)
13097 			return err;
13098 	}
13099 
13100 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
13101 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
13102 		return -EINVAL;
13103 
13104 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
13105 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
13106 
13107 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
13108 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
13109 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
13110 			return -EINVAL;
13111 
13112 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
13113 		setup.beacon_interval =
13114 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
13115 
13116 		err = cfg80211_validate_beacon_int(rdev,
13117 						   NL80211_IFTYPE_MESH_POINT,
13118 						   setup.beacon_interval);
13119 		if (err)
13120 			return err;
13121 	}
13122 
13123 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
13124 		setup.dtim_period =
13125 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
13126 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
13127 			return -EINVAL;
13128 	}
13129 
13130 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
13131 		/* parse additional setup parameters if given */
13132 		err = nl80211_parse_mesh_setup(info, &setup);
13133 		if (err)
13134 			return err;
13135 	}
13136 
13137 	if (setup.user_mpm)
13138 		cfg.auto_open_plinks = false;
13139 
13140 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
13141 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
13142 		if (err)
13143 			return err;
13144 	} else {
13145 		/* __cfg80211_join_mesh() will sort it out */
13146 		setup.chandef.chan = NULL;
13147 	}
13148 
13149 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
13150 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13151 		int n_rates =
13152 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
13153 		struct ieee80211_supported_band *sband;
13154 
13155 		if (!setup.chandef.chan)
13156 			return -EINVAL;
13157 
13158 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
13159 
13160 		err = ieee80211_get_ratemask(sband, rates, n_rates,
13161 					     &setup.basic_rates);
13162 		if (err)
13163 			return err;
13164 	}
13165 
13166 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
13167 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
13168 						    NL80211_ATTR_TX_RATES,
13169 						    &setup.beacon_rate,
13170 						    dev, false, 0);
13171 		if (err)
13172 			return err;
13173 
13174 		if (!setup.chandef.chan)
13175 			return -EINVAL;
13176 
13177 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
13178 					      &setup.beacon_rate);
13179 		if (err)
13180 			return err;
13181 	}
13182 
13183 	setup.userspace_handles_dfs =
13184 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
13185 
13186 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
13187 		int r = validate_pae_over_nl80211(rdev, info);
13188 
13189 		if (r < 0)
13190 			return r;
13191 
13192 		setup.control_port_over_nl80211 = true;
13193 	}
13194 
13195 	wdev_lock(dev->ieee80211_ptr);
13196 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
13197 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
13198 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
13199 	wdev_unlock(dev->ieee80211_ptr);
13200 
13201 	return err;
13202 }
13203 
13204 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
13205 {
13206 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13207 	struct net_device *dev = info->user_ptr[1];
13208 
13209 	return cfg80211_leave_mesh(rdev, dev);
13210 }
13211 
13212 #ifdef CONFIG_PM
13213 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
13214 					struct cfg80211_registered_device *rdev)
13215 {
13216 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
13217 	struct nlattr *nl_pats, *nl_pat;
13218 	int i, pat_len;
13219 
13220 	if (!wowlan->n_patterns)
13221 		return 0;
13222 
13223 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
13224 	if (!nl_pats)
13225 		return -ENOBUFS;
13226 
13227 	for (i = 0; i < wowlan->n_patterns; i++) {
13228 		nl_pat = nla_nest_start_noflag(msg, i + 1);
13229 		if (!nl_pat)
13230 			return -ENOBUFS;
13231 		pat_len = wowlan->patterns[i].pattern_len;
13232 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
13233 			    wowlan->patterns[i].mask) ||
13234 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13235 			    wowlan->patterns[i].pattern) ||
13236 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13237 				wowlan->patterns[i].pkt_offset))
13238 			return -ENOBUFS;
13239 		nla_nest_end(msg, nl_pat);
13240 	}
13241 	nla_nest_end(msg, nl_pats);
13242 
13243 	return 0;
13244 }
13245 
13246 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
13247 				   struct cfg80211_wowlan_tcp *tcp)
13248 {
13249 	struct nlattr *nl_tcp;
13250 
13251 	if (!tcp)
13252 		return 0;
13253 
13254 	nl_tcp = nla_nest_start_noflag(msg,
13255 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
13256 	if (!nl_tcp)
13257 		return -ENOBUFS;
13258 
13259 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
13260 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
13261 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
13262 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
13263 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
13264 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
13265 		    tcp->payload_len, tcp->payload) ||
13266 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
13267 			tcp->data_interval) ||
13268 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
13269 		    tcp->wake_len, tcp->wake_data) ||
13270 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
13271 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
13272 		return -ENOBUFS;
13273 
13274 	if (tcp->payload_seq.len &&
13275 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
13276 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
13277 		return -ENOBUFS;
13278 
13279 	if (tcp->payload_tok.len &&
13280 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
13281 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
13282 		    &tcp->payload_tok))
13283 		return -ENOBUFS;
13284 
13285 	nla_nest_end(msg, nl_tcp);
13286 
13287 	return 0;
13288 }
13289 
13290 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
13291 				  struct cfg80211_sched_scan_request *req)
13292 {
13293 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
13294 	int i;
13295 
13296 	if (!req)
13297 		return 0;
13298 
13299 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
13300 	if (!nd)
13301 		return -ENOBUFS;
13302 
13303 	if (req->n_scan_plans == 1 &&
13304 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
13305 			req->scan_plans[0].interval * 1000))
13306 		return -ENOBUFS;
13307 
13308 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
13309 		return -ENOBUFS;
13310 
13311 	if (req->relative_rssi_set) {
13312 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
13313 
13314 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
13315 			       req->relative_rssi))
13316 			return -ENOBUFS;
13317 
13318 		rssi_adjust.band = req->rssi_adjust.band;
13319 		rssi_adjust.delta = req->rssi_adjust.delta;
13320 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
13321 			    sizeof(rssi_adjust), &rssi_adjust))
13322 			return -ENOBUFS;
13323 	}
13324 
13325 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13326 	if (!freqs)
13327 		return -ENOBUFS;
13328 
13329 	for (i = 0; i < req->n_channels; i++) {
13330 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13331 			return -ENOBUFS;
13332 	}
13333 
13334 	nla_nest_end(msg, freqs);
13335 
13336 	if (req->n_match_sets) {
13337 		matches = nla_nest_start_noflag(msg,
13338 						NL80211_ATTR_SCHED_SCAN_MATCH);
13339 		if (!matches)
13340 			return -ENOBUFS;
13341 
13342 		for (i = 0; i < req->n_match_sets; i++) {
13343 			match = nla_nest_start_noflag(msg, i);
13344 			if (!match)
13345 				return -ENOBUFS;
13346 
13347 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
13348 				    req->match_sets[i].ssid.ssid_len,
13349 				    req->match_sets[i].ssid.ssid))
13350 				return -ENOBUFS;
13351 			nla_nest_end(msg, match);
13352 		}
13353 		nla_nest_end(msg, matches);
13354 	}
13355 
13356 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
13357 	if (!scan_plans)
13358 		return -ENOBUFS;
13359 
13360 	for (i = 0; i < req->n_scan_plans; i++) {
13361 		scan_plan = nla_nest_start_noflag(msg, i + 1);
13362 		if (!scan_plan)
13363 			return -ENOBUFS;
13364 
13365 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
13366 				req->scan_plans[i].interval) ||
13367 		    (req->scan_plans[i].iterations &&
13368 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
13369 				 req->scan_plans[i].iterations)))
13370 			return -ENOBUFS;
13371 		nla_nest_end(msg, scan_plan);
13372 	}
13373 	nla_nest_end(msg, scan_plans);
13374 
13375 	nla_nest_end(msg, nd);
13376 
13377 	return 0;
13378 }
13379 
13380 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
13381 {
13382 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13383 	struct sk_buff *msg;
13384 	void *hdr;
13385 	u32 size = NLMSG_DEFAULT_SIZE;
13386 
13387 	if (!rdev->wiphy.wowlan)
13388 		return -EOPNOTSUPP;
13389 
13390 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
13391 		/* adjust size to have room for all the data */
13392 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
13393 			rdev->wiphy.wowlan_config->tcp->payload_len +
13394 			rdev->wiphy.wowlan_config->tcp->wake_len +
13395 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
13396 	}
13397 
13398 	msg = nlmsg_new(size, GFP_KERNEL);
13399 	if (!msg)
13400 		return -ENOMEM;
13401 
13402 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13403 			     NL80211_CMD_GET_WOWLAN);
13404 	if (!hdr)
13405 		goto nla_put_failure;
13406 
13407 	if (rdev->wiphy.wowlan_config) {
13408 		struct nlattr *nl_wowlan;
13409 
13410 		nl_wowlan = nla_nest_start_noflag(msg,
13411 						  NL80211_ATTR_WOWLAN_TRIGGERS);
13412 		if (!nl_wowlan)
13413 			goto nla_put_failure;
13414 
13415 		if ((rdev->wiphy.wowlan_config->any &&
13416 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
13417 		    (rdev->wiphy.wowlan_config->disconnect &&
13418 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
13419 		    (rdev->wiphy.wowlan_config->magic_pkt &&
13420 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
13421 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
13422 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
13423 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
13424 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
13425 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
13426 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
13427 		    (rdev->wiphy.wowlan_config->rfkill_release &&
13428 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
13429 			goto nla_put_failure;
13430 
13431 		if (nl80211_send_wowlan_patterns(msg, rdev))
13432 			goto nla_put_failure;
13433 
13434 		if (nl80211_send_wowlan_tcp(msg,
13435 					    rdev->wiphy.wowlan_config->tcp))
13436 			goto nla_put_failure;
13437 
13438 		if (nl80211_send_wowlan_nd(
13439 			    msg,
13440 			    rdev->wiphy.wowlan_config->nd_config))
13441 			goto nla_put_failure;
13442 
13443 		nla_nest_end(msg, nl_wowlan);
13444 	}
13445 
13446 	genlmsg_end(msg, hdr);
13447 	return genlmsg_reply(msg, info);
13448 
13449 nla_put_failure:
13450 	nlmsg_free(msg);
13451 	return -ENOBUFS;
13452 }
13453 
13454 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
13455 				    struct nlattr *attr,
13456 				    struct cfg80211_wowlan *trig)
13457 {
13458 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
13459 	struct cfg80211_wowlan_tcp *cfg;
13460 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
13461 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
13462 	u32 size;
13463 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
13464 	int err, port;
13465 
13466 	if (!rdev->wiphy.wowlan->tcp)
13467 		return -EINVAL;
13468 
13469 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
13470 					  nl80211_wowlan_tcp_policy, NULL);
13471 	if (err)
13472 		return err;
13473 
13474 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
13475 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
13476 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
13477 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
13478 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
13479 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
13480 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
13481 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
13482 		return -EINVAL;
13483 
13484 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
13485 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
13486 		return -EINVAL;
13487 
13488 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
13489 			rdev->wiphy.wowlan->tcp->data_interval_max ||
13490 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
13491 		return -EINVAL;
13492 
13493 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
13494 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
13495 		return -EINVAL;
13496 
13497 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
13498 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
13499 		return -EINVAL;
13500 
13501 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
13502 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13503 
13504 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13505 		tokens_size = tokln - sizeof(*tok);
13506 
13507 		if (!tok->len || tokens_size % tok->len)
13508 			return -EINVAL;
13509 		if (!rdev->wiphy.wowlan->tcp->tok)
13510 			return -EINVAL;
13511 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
13512 			return -EINVAL;
13513 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
13514 			return -EINVAL;
13515 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
13516 			return -EINVAL;
13517 		if (tok->offset + tok->len > data_size)
13518 			return -EINVAL;
13519 	}
13520 
13521 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
13522 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
13523 		if (!rdev->wiphy.wowlan->tcp->seq)
13524 			return -EINVAL;
13525 		if (seq->len == 0 || seq->len > 4)
13526 			return -EINVAL;
13527 		if (seq->len + seq->offset > data_size)
13528 			return -EINVAL;
13529 	}
13530 
13531 	size = sizeof(*cfg);
13532 	size += data_size;
13533 	size += wake_size + wake_mask_size;
13534 	size += tokens_size;
13535 
13536 	cfg = kzalloc(size, GFP_KERNEL);
13537 	if (!cfg)
13538 		return -ENOMEM;
13539 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
13540 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
13541 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
13542 	       ETH_ALEN);
13543 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
13544 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
13545 	else
13546 		port = 0;
13547 #ifdef CONFIG_INET
13548 	/* allocate a socket and port for it and use it */
13549 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
13550 			    IPPROTO_TCP, &cfg->sock, 1);
13551 	if (err) {
13552 		kfree(cfg);
13553 		return err;
13554 	}
13555 	if (inet_csk_get_port(cfg->sock->sk, port)) {
13556 		sock_release(cfg->sock);
13557 		kfree(cfg);
13558 		return -EADDRINUSE;
13559 	}
13560 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
13561 #else
13562 	if (!port) {
13563 		kfree(cfg);
13564 		return -EINVAL;
13565 	}
13566 	cfg->src_port = port;
13567 #endif
13568 
13569 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
13570 	cfg->payload_len = data_size;
13571 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
13572 	memcpy((void *)cfg->payload,
13573 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
13574 	       data_size);
13575 	if (seq)
13576 		cfg->payload_seq = *seq;
13577 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
13578 	cfg->wake_len = wake_size;
13579 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
13580 	memcpy((void *)cfg->wake_data,
13581 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
13582 	       wake_size);
13583 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
13584 			 data_size + wake_size;
13585 	memcpy((void *)cfg->wake_mask,
13586 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
13587 	       wake_mask_size);
13588 	if (tok) {
13589 		cfg->tokens_size = tokens_size;
13590 		cfg->payload_tok = *tok;
13591 		memcpy(cfg->payload_tok.token_stream, tok->token_stream,
13592 		       tokens_size);
13593 	}
13594 
13595 	trig->tcp = cfg;
13596 
13597 	return 0;
13598 }
13599 
13600 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
13601 				   const struct wiphy_wowlan_support *wowlan,
13602 				   struct nlattr *attr,
13603 				   struct cfg80211_wowlan *trig)
13604 {
13605 	struct nlattr **tb;
13606 	int err;
13607 
13608 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
13609 	if (!tb)
13610 		return -ENOMEM;
13611 
13612 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
13613 		err = -EOPNOTSUPP;
13614 		goto out;
13615 	}
13616 
13617 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
13618 					  nl80211_policy, NULL);
13619 	if (err)
13620 		goto out;
13621 
13622 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
13623 						   wowlan->max_nd_match_sets);
13624 	err = PTR_ERR_OR_ZERO(trig->nd_config);
13625 	if (err)
13626 		trig->nd_config = NULL;
13627 
13628 out:
13629 	kfree(tb);
13630 	return err;
13631 }
13632 
13633 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
13634 {
13635 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13636 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
13637 	struct cfg80211_wowlan new_triggers = {};
13638 	struct cfg80211_wowlan *ntrig;
13639 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
13640 	int err, i;
13641 	bool prev_enabled = rdev->wiphy.wowlan_config;
13642 	bool regular = false;
13643 
13644 	if (!wowlan)
13645 		return -EOPNOTSUPP;
13646 
13647 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
13648 		cfg80211_rdev_free_wowlan(rdev);
13649 		rdev->wiphy.wowlan_config = NULL;
13650 		goto set_wakeup;
13651 	}
13652 
13653 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
13654 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
13655 					  nl80211_wowlan_policy, info->extack);
13656 	if (err)
13657 		return err;
13658 
13659 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
13660 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
13661 			return -EINVAL;
13662 		new_triggers.any = true;
13663 	}
13664 
13665 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
13666 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
13667 			return -EINVAL;
13668 		new_triggers.disconnect = true;
13669 		regular = true;
13670 	}
13671 
13672 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
13673 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
13674 			return -EINVAL;
13675 		new_triggers.magic_pkt = true;
13676 		regular = true;
13677 	}
13678 
13679 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
13680 		return -EINVAL;
13681 
13682 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
13683 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
13684 			return -EINVAL;
13685 		new_triggers.gtk_rekey_failure = true;
13686 		regular = true;
13687 	}
13688 
13689 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
13690 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
13691 			return -EINVAL;
13692 		new_triggers.eap_identity_req = true;
13693 		regular = true;
13694 	}
13695 
13696 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
13697 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
13698 			return -EINVAL;
13699 		new_triggers.four_way_handshake = true;
13700 		regular = true;
13701 	}
13702 
13703 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
13704 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
13705 			return -EINVAL;
13706 		new_triggers.rfkill_release = true;
13707 		regular = true;
13708 	}
13709 
13710 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
13711 		struct nlattr *pat;
13712 		int n_patterns = 0;
13713 		int rem, pat_len, mask_len, pkt_offset;
13714 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13715 
13716 		regular = true;
13717 
13718 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13719 				    rem)
13720 			n_patterns++;
13721 		if (n_patterns > wowlan->n_patterns)
13722 			return -EINVAL;
13723 
13724 		new_triggers.patterns = kcalloc(n_patterns,
13725 						sizeof(new_triggers.patterns[0]),
13726 						GFP_KERNEL);
13727 		if (!new_triggers.patterns)
13728 			return -ENOMEM;
13729 
13730 		new_triggers.n_patterns = n_patterns;
13731 		i = 0;
13732 
13733 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13734 				    rem) {
13735 			u8 *mask_pat;
13736 
13737 			err = nla_parse_nested_deprecated(pat_tb,
13738 							  MAX_NL80211_PKTPAT,
13739 							  pat,
13740 							  nl80211_packet_pattern_policy,
13741 							  info->extack);
13742 			if (err)
13743 				goto error;
13744 
13745 			err = -EINVAL;
13746 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
13747 			    !pat_tb[NL80211_PKTPAT_PATTERN])
13748 				goto error;
13749 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13750 			mask_len = DIV_ROUND_UP(pat_len, 8);
13751 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13752 				goto error;
13753 			if (pat_len > wowlan->pattern_max_len ||
13754 			    pat_len < wowlan->pattern_min_len)
13755 				goto error;
13756 
13757 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
13758 				pkt_offset = 0;
13759 			else
13760 				pkt_offset = nla_get_u32(
13761 					pat_tb[NL80211_PKTPAT_OFFSET]);
13762 			if (pkt_offset > wowlan->max_pkt_offset)
13763 				goto error;
13764 			new_triggers.patterns[i].pkt_offset = pkt_offset;
13765 
13766 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13767 			if (!mask_pat) {
13768 				err = -ENOMEM;
13769 				goto error;
13770 			}
13771 			new_triggers.patterns[i].mask = mask_pat;
13772 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13773 			       mask_len);
13774 			mask_pat += mask_len;
13775 			new_triggers.patterns[i].pattern = mask_pat;
13776 			new_triggers.patterns[i].pattern_len = pat_len;
13777 			memcpy(mask_pat,
13778 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13779 			       pat_len);
13780 			i++;
13781 		}
13782 	}
13783 
13784 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
13785 		regular = true;
13786 		err = nl80211_parse_wowlan_tcp(
13787 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
13788 			&new_triggers);
13789 		if (err)
13790 			goto error;
13791 	}
13792 
13793 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
13794 		regular = true;
13795 		err = nl80211_parse_wowlan_nd(
13796 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
13797 			&new_triggers);
13798 		if (err)
13799 			goto error;
13800 	}
13801 
13802 	/* The 'any' trigger means the device continues operating more or less
13803 	 * as in its normal operation mode and wakes up the host on most of the
13804 	 * normal interrupts (like packet RX, ...)
13805 	 * It therefore makes little sense to combine with the more constrained
13806 	 * wakeup trigger modes.
13807 	 */
13808 	if (new_triggers.any && regular) {
13809 		err = -EINVAL;
13810 		goto error;
13811 	}
13812 
13813 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
13814 	if (!ntrig) {
13815 		err = -ENOMEM;
13816 		goto error;
13817 	}
13818 	cfg80211_rdev_free_wowlan(rdev);
13819 	rdev->wiphy.wowlan_config = ntrig;
13820 
13821  set_wakeup:
13822 	if (rdev->ops->set_wakeup &&
13823 	    prev_enabled != !!rdev->wiphy.wowlan_config)
13824 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
13825 
13826 	return 0;
13827  error:
13828 	for (i = 0; i < new_triggers.n_patterns; i++)
13829 		kfree(new_triggers.patterns[i].mask);
13830 	kfree(new_triggers.patterns);
13831 	if (new_triggers.tcp && new_triggers.tcp->sock)
13832 		sock_release(new_triggers.tcp->sock);
13833 	kfree(new_triggers.tcp);
13834 	kfree(new_triggers.nd_config);
13835 	return err;
13836 }
13837 #endif
13838 
13839 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
13840 				       struct cfg80211_registered_device *rdev)
13841 {
13842 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
13843 	int i, j, pat_len;
13844 	struct cfg80211_coalesce_rules *rule;
13845 
13846 	if (!rdev->coalesce->n_rules)
13847 		return 0;
13848 
13849 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
13850 	if (!nl_rules)
13851 		return -ENOBUFS;
13852 
13853 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
13854 		nl_rule = nla_nest_start_noflag(msg, i + 1);
13855 		if (!nl_rule)
13856 			return -ENOBUFS;
13857 
13858 		rule = &rdev->coalesce->rules[i];
13859 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
13860 				rule->delay))
13861 			return -ENOBUFS;
13862 
13863 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
13864 				rule->condition))
13865 			return -ENOBUFS;
13866 
13867 		nl_pats = nla_nest_start_noflag(msg,
13868 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
13869 		if (!nl_pats)
13870 			return -ENOBUFS;
13871 
13872 		for (j = 0; j < rule->n_patterns; j++) {
13873 			nl_pat = nla_nest_start_noflag(msg, j + 1);
13874 			if (!nl_pat)
13875 				return -ENOBUFS;
13876 			pat_len = rule->patterns[j].pattern_len;
13877 			if (nla_put(msg, NL80211_PKTPAT_MASK,
13878 				    DIV_ROUND_UP(pat_len, 8),
13879 				    rule->patterns[j].mask) ||
13880 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13881 				    rule->patterns[j].pattern) ||
13882 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13883 					rule->patterns[j].pkt_offset))
13884 				return -ENOBUFS;
13885 			nla_nest_end(msg, nl_pat);
13886 		}
13887 		nla_nest_end(msg, nl_pats);
13888 		nla_nest_end(msg, nl_rule);
13889 	}
13890 	nla_nest_end(msg, nl_rules);
13891 
13892 	return 0;
13893 }
13894 
13895 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
13896 {
13897 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13898 	struct sk_buff *msg;
13899 	void *hdr;
13900 
13901 	if (!rdev->wiphy.coalesce)
13902 		return -EOPNOTSUPP;
13903 
13904 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13905 	if (!msg)
13906 		return -ENOMEM;
13907 
13908 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13909 			     NL80211_CMD_GET_COALESCE);
13910 	if (!hdr)
13911 		goto nla_put_failure;
13912 
13913 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
13914 		goto nla_put_failure;
13915 
13916 	genlmsg_end(msg, hdr);
13917 	return genlmsg_reply(msg, info);
13918 
13919 nla_put_failure:
13920 	nlmsg_free(msg);
13921 	return -ENOBUFS;
13922 }
13923 
13924 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
13925 {
13926 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
13927 	int i, j;
13928 	struct cfg80211_coalesce_rules *rule;
13929 
13930 	if (!coalesce)
13931 		return;
13932 
13933 	for (i = 0; i < coalesce->n_rules; i++) {
13934 		rule = &coalesce->rules[i];
13935 		for (j = 0; j < rule->n_patterns; j++)
13936 			kfree(rule->patterns[j].mask);
13937 		kfree(rule->patterns);
13938 	}
13939 	kfree(coalesce->rules);
13940 	kfree(coalesce);
13941 	rdev->coalesce = NULL;
13942 }
13943 
13944 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
13945 				       struct nlattr *rule,
13946 				       struct cfg80211_coalesce_rules *new_rule)
13947 {
13948 	int err, i;
13949 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13950 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
13951 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
13952 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13953 
13954 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
13955 					  rule, nl80211_coalesce_policy, NULL);
13956 	if (err)
13957 		return err;
13958 
13959 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
13960 		new_rule->delay =
13961 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
13962 	if (new_rule->delay > coalesce->max_delay)
13963 		return -EINVAL;
13964 
13965 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
13966 		new_rule->condition =
13967 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
13968 
13969 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
13970 		return -EINVAL;
13971 
13972 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13973 			    rem)
13974 		n_patterns++;
13975 	if (n_patterns > coalesce->n_patterns)
13976 		return -EINVAL;
13977 
13978 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
13979 				     GFP_KERNEL);
13980 	if (!new_rule->patterns)
13981 		return -ENOMEM;
13982 
13983 	new_rule->n_patterns = n_patterns;
13984 	i = 0;
13985 
13986 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13987 			    rem) {
13988 		u8 *mask_pat;
13989 
13990 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
13991 						  pat,
13992 						  nl80211_packet_pattern_policy,
13993 						  NULL);
13994 		if (err)
13995 			return err;
13996 
13997 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
13998 		    !pat_tb[NL80211_PKTPAT_PATTERN])
13999 			return -EINVAL;
14000 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
14001 		mask_len = DIV_ROUND_UP(pat_len, 8);
14002 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
14003 			return -EINVAL;
14004 		if (pat_len > coalesce->pattern_max_len ||
14005 		    pat_len < coalesce->pattern_min_len)
14006 			return -EINVAL;
14007 
14008 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
14009 			pkt_offset = 0;
14010 		else
14011 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
14012 		if (pkt_offset > coalesce->max_pkt_offset)
14013 			return -EINVAL;
14014 		new_rule->patterns[i].pkt_offset = pkt_offset;
14015 
14016 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
14017 		if (!mask_pat)
14018 			return -ENOMEM;
14019 
14020 		new_rule->patterns[i].mask = mask_pat;
14021 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
14022 		       mask_len);
14023 
14024 		mask_pat += mask_len;
14025 		new_rule->patterns[i].pattern = mask_pat;
14026 		new_rule->patterns[i].pattern_len = pat_len;
14027 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
14028 		       pat_len);
14029 		i++;
14030 	}
14031 
14032 	return 0;
14033 }
14034 
14035 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
14036 {
14037 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14038 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
14039 	struct cfg80211_coalesce new_coalesce = {};
14040 	struct cfg80211_coalesce *n_coalesce;
14041 	int err, rem_rule, n_rules = 0, i, j;
14042 	struct nlattr *rule;
14043 	struct cfg80211_coalesce_rules *tmp_rule;
14044 
14045 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
14046 		return -EOPNOTSUPP;
14047 
14048 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
14049 		cfg80211_rdev_free_coalesce(rdev);
14050 		rdev_set_coalesce(rdev, NULL);
14051 		return 0;
14052 	}
14053 
14054 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
14055 			    rem_rule)
14056 		n_rules++;
14057 	if (n_rules > coalesce->n_rules)
14058 		return -EINVAL;
14059 
14060 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
14061 				     GFP_KERNEL);
14062 	if (!new_coalesce.rules)
14063 		return -ENOMEM;
14064 
14065 	new_coalesce.n_rules = n_rules;
14066 	i = 0;
14067 
14068 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
14069 			    rem_rule) {
14070 		err = nl80211_parse_coalesce_rule(rdev, rule,
14071 						  &new_coalesce.rules[i]);
14072 		if (err)
14073 			goto error;
14074 
14075 		i++;
14076 	}
14077 
14078 	err = rdev_set_coalesce(rdev, &new_coalesce);
14079 	if (err)
14080 		goto error;
14081 
14082 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
14083 	if (!n_coalesce) {
14084 		err = -ENOMEM;
14085 		goto error;
14086 	}
14087 	cfg80211_rdev_free_coalesce(rdev);
14088 	rdev->coalesce = n_coalesce;
14089 
14090 	return 0;
14091 error:
14092 	for (i = 0; i < new_coalesce.n_rules; i++) {
14093 		tmp_rule = &new_coalesce.rules[i];
14094 		if (!tmp_rule)
14095 			continue;
14096 		for (j = 0; j < tmp_rule->n_patterns; j++)
14097 			kfree(tmp_rule->patterns[j].mask);
14098 		kfree(tmp_rule->patterns);
14099 	}
14100 	kfree(new_coalesce.rules);
14101 
14102 	return err;
14103 }
14104 
14105 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
14106 {
14107 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14108 	struct net_device *dev = info->user_ptr[1];
14109 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14110 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
14111 	struct cfg80211_gtk_rekey_data rekey_data = {};
14112 	int err;
14113 
14114 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
14115 		return -EINVAL;
14116 
14117 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
14118 					  info->attrs[NL80211_ATTR_REKEY_DATA],
14119 					  nl80211_rekey_policy, info->extack);
14120 	if (err)
14121 		return err;
14122 
14123 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
14124 	    !tb[NL80211_REKEY_DATA_KCK])
14125 		return -EINVAL;
14126 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
14127 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14128 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
14129 		return -ERANGE;
14130 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
14131 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
14132 	      nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN) &&
14133 	     !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KCK_32 &&
14134 	       nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN_32))
14135 		return -ERANGE;
14136 
14137 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
14138 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
14139 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
14140 	rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
14141 	rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
14142 	if (tb[NL80211_REKEY_DATA_AKM])
14143 		rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
14144 
14145 	wdev_lock(wdev);
14146 	if (!wdev->connected) {
14147 		err = -ENOTCONN;
14148 		goto out;
14149 	}
14150 
14151 	if (!rdev->ops->set_rekey_data) {
14152 		err = -EOPNOTSUPP;
14153 		goto out;
14154 	}
14155 
14156 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
14157  out:
14158 	wdev_unlock(wdev);
14159 	return err;
14160 }
14161 
14162 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
14163 					     struct genl_info *info)
14164 {
14165 	struct net_device *dev = info->user_ptr[1];
14166 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14167 
14168 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14169 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14170 		return -EINVAL;
14171 
14172 	if (wdev->ap_unexpected_nlportid)
14173 		return -EBUSY;
14174 
14175 	wdev->ap_unexpected_nlportid = info->snd_portid;
14176 	return 0;
14177 }
14178 
14179 static int nl80211_probe_client(struct sk_buff *skb,
14180 				struct genl_info *info)
14181 {
14182 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14183 	struct net_device *dev = info->user_ptr[1];
14184 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14185 	struct sk_buff *msg;
14186 	void *hdr;
14187 	const u8 *addr;
14188 	u64 cookie;
14189 	int err;
14190 
14191 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14192 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14193 		return -EOPNOTSUPP;
14194 
14195 	if (!info->attrs[NL80211_ATTR_MAC])
14196 		return -EINVAL;
14197 
14198 	if (!rdev->ops->probe_client)
14199 		return -EOPNOTSUPP;
14200 
14201 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14202 	if (!msg)
14203 		return -ENOMEM;
14204 
14205 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14206 			     NL80211_CMD_PROBE_CLIENT);
14207 	if (!hdr) {
14208 		err = -ENOBUFS;
14209 		goto free_msg;
14210 	}
14211 
14212 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
14213 
14214 	err = rdev_probe_client(rdev, dev, addr, &cookie);
14215 	if (err)
14216 		goto free_msg;
14217 
14218 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14219 			      NL80211_ATTR_PAD))
14220 		goto nla_put_failure;
14221 
14222 	genlmsg_end(msg, hdr);
14223 
14224 	return genlmsg_reply(msg, info);
14225 
14226  nla_put_failure:
14227 	err = -ENOBUFS;
14228  free_msg:
14229 	nlmsg_free(msg);
14230 	return err;
14231 }
14232 
14233 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
14234 {
14235 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14236 	struct cfg80211_beacon_registration *reg, *nreg;
14237 	int rv;
14238 
14239 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
14240 		return -EOPNOTSUPP;
14241 
14242 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
14243 	if (!nreg)
14244 		return -ENOMEM;
14245 
14246 	/* First, check if already registered. */
14247 	spin_lock_bh(&rdev->beacon_registrations_lock);
14248 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
14249 		if (reg->nlportid == info->snd_portid) {
14250 			rv = -EALREADY;
14251 			goto out_err;
14252 		}
14253 	}
14254 	/* Add it to the list */
14255 	nreg->nlportid = info->snd_portid;
14256 	list_add(&nreg->list, &rdev->beacon_registrations);
14257 
14258 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14259 
14260 	return 0;
14261 out_err:
14262 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14263 	kfree(nreg);
14264 	return rv;
14265 }
14266 
14267 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
14268 {
14269 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14270 	struct wireless_dev *wdev = info->user_ptr[1];
14271 	int err;
14272 
14273 	if (!rdev->ops->start_p2p_device)
14274 		return -EOPNOTSUPP;
14275 
14276 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14277 		return -EOPNOTSUPP;
14278 
14279 	if (wdev_running(wdev))
14280 		return 0;
14281 
14282 	if (rfkill_blocked(rdev->wiphy.rfkill))
14283 		return -ERFKILL;
14284 
14285 	err = rdev_start_p2p_device(rdev, wdev);
14286 	if (err)
14287 		return err;
14288 
14289 	wdev->is_running = true;
14290 	rdev->opencount++;
14291 
14292 	return 0;
14293 }
14294 
14295 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
14296 {
14297 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14298 	struct wireless_dev *wdev = info->user_ptr[1];
14299 
14300 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
14301 		return -EOPNOTSUPP;
14302 
14303 	if (!rdev->ops->stop_p2p_device)
14304 		return -EOPNOTSUPP;
14305 
14306 	cfg80211_stop_p2p_device(rdev, wdev);
14307 
14308 	return 0;
14309 }
14310 
14311 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
14312 {
14313 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14314 	struct wireless_dev *wdev = info->user_ptr[1];
14315 	struct cfg80211_nan_conf conf = {};
14316 	int err;
14317 
14318 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14319 		return -EOPNOTSUPP;
14320 
14321 	if (wdev_running(wdev))
14322 		return -EEXIST;
14323 
14324 	if (rfkill_blocked(rdev->wiphy.rfkill))
14325 		return -ERFKILL;
14326 
14327 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
14328 		return -EINVAL;
14329 
14330 	conf.master_pref =
14331 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14332 
14333 	if (info->attrs[NL80211_ATTR_BANDS]) {
14334 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14335 
14336 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14337 			return -EOPNOTSUPP;
14338 
14339 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14340 			return -EINVAL;
14341 
14342 		conf.bands = bands;
14343 	}
14344 
14345 	err = rdev_start_nan(rdev, wdev, &conf);
14346 	if (err)
14347 		return err;
14348 
14349 	wdev->is_running = true;
14350 	rdev->opencount++;
14351 
14352 	return 0;
14353 }
14354 
14355 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
14356 {
14357 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14358 	struct wireless_dev *wdev = info->user_ptr[1];
14359 
14360 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14361 		return -EOPNOTSUPP;
14362 
14363 	cfg80211_stop_nan(rdev, wdev);
14364 
14365 	return 0;
14366 }
14367 
14368 static int validate_nan_filter(struct nlattr *filter_attr)
14369 {
14370 	struct nlattr *attr;
14371 	int len = 0, n_entries = 0, rem;
14372 
14373 	nla_for_each_nested(attr, filter_attr, rem) {
14374 		len += nla_len(attr);
14375 		n_entries++;
14376 	}
14377 
14378 	if (len >= U8_MAX)
14379 		return -EINVAL;
14380 
14381 	return n_entries;
14382 }
14383 
14384 static int handle_nan_filter(struct nlattr *attr_filter,
14385 			     struct cfg80211_nan_func *func,
14386 			     bool tx)
14387 {
14388 	struct nlattr *attr;
14389 	int n_entries, rem, i;
14390 	struct cfg80211_nan_func_filter *filter;
14391 
14392 	n_entries = validate_nan_filter(attr_filter);
14393 	if (n_entries < 0)
14394 		return n_entries;
14395 
14396 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
14397 
14398 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
14399 	if (!filter)
14400 		return -ENOMEM;
14401 
14402 	i = 0;
14403 	nla_for_each_nested(attr, attr_filter, rem) {
14404 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
14405 		if (!filter[i].filter)
14406 			goto err;
14407 
14408 		filter[i].len = nla_len(attr);
14409 		i++;
14410 	}
14411 	if (tx) {
14412 		func->num_tx_filters = n_entries;
14413 		func->tx_filters = filter;
14414 	} else {
14415 		func->num_rx_filters = n_entries;
14416 		func->rx_filters = filter;
14417 	}
14418 
14419 	return 0;
14420 
14421 err:
14422 	i = 0;
14423 	nla_for_each_nested(attr, attr_filter, rem) {
14424 		kfree(filter[i].filter);
14425 		i++;
14426 	}
14427 	kfree(filter);
14428 	return -ENOMEM;
14429 }
14430 
14431 static int nl80211_nan_add_func(struct sk_buff *skb,
14432 				struct genl_info *info)
14433 {
14434 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14435 	struct wireless_dev *wdev = info->user_ptr[1];
14436 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
14437 	struct cfg80211_nan_func *func;
14438 	struct sk_buff *msg = NULL;
14439 	void *hdr = NULL;
14440 	int err = 0;
14441 
14442 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14443 		return -EOPNOTSUPP;
14444 
14445 	if (!wdev_running(wdev))
14446 		return -ENOTCONN;
14447 
14448 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
14449 		return -EINVAL;
14450 
14451 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
14452 					  info->attrs[NL80211_ATTR_NAN_FUNC],
14453 					  nl80211_nan_func_policy,
14454 					  info->extack);
14455 	if (err)
14456 		return err;
14457 
14458 	func = kzalloc(sizeof(*func), GFP_KERNEL);
14459 	if (!func)
14460 		return -ENOMEM;
14461 
14462 	func->cookie = cfg80211_assign_cookie(rdev);
14463 
14464 	if (!tb[NL80211_NAN_FUNC_TYPE]) {
14465 		err = -EINVAL;
14466 		goto out;
14467 	}
14468 
14469 
14470 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
14471 
14472 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
14473 		err = -EINVAL;
14474 		goto out;
14475 	}
14476 
14477 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
14478 	       sizeof(func->service_id));
14479 
14480 	func->close_range =
14481 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
14482 
14483 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
14484 		func->serv_spec_info_len =
14485 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
14486 		func->serv_spec_info =
14487 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
14488 				func->serv_spec_info_len,
14489 				GFP_KERNEL);
14490 		if (!func->serv_spec_info) {
14491 			err = -ENOMEM;
14492 			goto out;
14493 		}
14494 	}
14495 
14496 	if (tb[NL80211_NAN_FUNC_TTL])
14497 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
14498 
14499 	switch (func->type) {
14500 	case NL80211_NAN_FUNC_PUBLISH:
14501 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
14502 			err = -EINVAL;
14503 			goto out;
14504 		}
14505 
14506 		func->publish_type =
14507 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
14508 		func->publish_bcast =
14509 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
14510 
14511 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
14512 			func->publish_bcast) {
14513 			err = -EINVAL;
14514 			goto out;
14515 		}
14516 		break;
14517 	case NL80211_NAN_FUNC_SUBSCRIBE:
14518 		func->subscribe_active =
14519 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
14520 		break;
14521 	case NL80211_NAN_FUNC_FOLLOW_UP:
14522 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
14523 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
14524 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
14525 			err = -EINVAL;
14526 			goto out;
14527 		}
14528 
14529 		func->followup_id =
14530 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
14531 		func->followup_reqid =
14532 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
14533 		memcpy(func->followup_dest.addr,
14534 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
14535 		       sizeof(func->followup_dest.addr));
14536 		if (func->ttl) {
14537 			err = -EINVAL;
14538 			goto out;
14539 		}
14540 		break;
14541 	default:
14542 		err = -EINVAL;
14543 		goto out;
14544 	}
14545 
14546 	if (tb[NL80211_NAN_FUNC_SRF]) {
14547 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
14548 
14549 		err = nla_parse_nested_deprecated(srf_tb,
14550 						  NL80211_NAN_SRF_ATTR_MAX,
14551 						  tb[NL80211_NAN_FUNC_SRF],
14552 						  nl80211_nan_srf_policy,
14553 						  info->extack);
14554 		if (err)
14555 			goto out;
14556 
14557 		func->srf_include =
14558 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
14559 
14560 		if (srf_tb[NL80211_NAN_SRF_BF]) {
14561 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
14562 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
14563 				err = -EINVAL;
14564 				goto out;
14565 			}
14566 
14567 			func->srf_bf_len =
14568 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
14569 			func->srf_bf =
14570 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
14571 					func->srf_bf_len, GFP_KERNEL);
14572 			if (!func->srf_bf) {
14573 				err = -ENOMEM;
14574 				goto out;
14575 			}
14576 
14577 			func->srf_bf_idx =
14578 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
14579 		} else {
14580 			struct nlattr *attr, *mac_attr =
14581 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
14582 			int n_entries, rem, i = 0;
14583 
14584 			if (!mac_attr) {
14585 				err = -EINVAL;
14586 				goto out;
14587 			}
14588 
14589 			n_entries = validate_acl_mac_addrs(mac_attr);
14590 			if (n_entries <= 0) {
14591 				err = -EINVAL;
14592 				goto out;
14593 			}
14594 
14595 			func->srf_num_macs = n_entries;
14596 			func->srf_macs =
14597 				kcalloc(n_entries, sizeof(*func->srf_macs),
14598 					GFP_KERNEL);
14599 			if (!func->srf_macs) {
14600 				err = -ENOMEM;
14601 				goto out;
14602 			}
14603 
14604 			nla_for_each_nested(attr, mac_attr, rem)
14605 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
14606 				       sizeof(*func->srf_macs));
14607 		}
14608 	}
14609 
14610 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
14611 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
14612 					func, true);
14613 		if (err)
14614 			goto out;
14615 	}
14616 
14617 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
14618 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
14619 					func, false);
14620 		if (err)
14621 			goto out;
14622 	}
14623 
14624 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14625 	if (!msg) {
14626 		err = -ENOMEM;
14627 		goto out;
14628 	}
14629 
14630 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14631 			     NL80211_CMD_ADD_NAN_FUNCTION);
14632 	/* This can't really happen - we just allocated 4KB */
14633 	if (WARN_ON(!hdr)) {
14634 		err = -ENOMEM;
14635 		goto out;
14636 	}
14637 
14638 	err = rdev_add_nan_func(rdev, wdev, func);
14639 out:
14640 	if (err < 0) {
14641 		cfg80211_free_nan_func(func);
14642 		nlmsg_free(msg);
14643 		return err;
14644 	}
14645 
14646 	/* propagate the instance id and cookie to userspace  */
14647 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
14648 			      NL80211_ATTR_PAD))
14649 		goto nla_put_failure;
14650 
14651 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14652 	if (!func_attr)
14653 		goto nla_put_failure;
14654 
14655 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
14656 		       func->instance_id))
14657 		goto nla_put_failure;
14658 
14659 	nla_nest_end(msg, func_attr);
14660 
14661 	genlmsg_end(msg, hdr);
14662 	return genlmsg_reply(msg, info);
14663 
14664 nla_put_failure:
14665 	nlmsg_free(msg);
14666 	return -ENOBUFS;
14667 }
14668 
14669 static int nl80211_nan_del_func(struct sk_buff *skb,
14670 			       struct genl_info *info)
14671 {
14672 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14673 	struct wireless_dev *wdev = info->user_ptr[1];
14674 	u64 cookie;
14675 
14676 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14677 		return -EOPNOTSUPP;
14678 
14679 	if (!wdev_running(wdev))
14680 		return -ENOTCONN;
14681 
14682 	if (!info->attrs[NL80211_ATTR_COOKIE])
14683 		return -EINVAL;
14684 
14685 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
14686 
14687 	rdev_del_nan_func(rdev, wdev, cookie);
14688 
14689 	return 0;
14690 }
14691 
14692 static int nl80211_nan_change_config(struct sk_buff *skb,
14693 				     struct genl_info *info)
14694 {
14695 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14696 	struct wireless_dev *wdev = info->user_ptr[1];
14697 	struct cfg80211_nan_conf conf = {};
14698 	u32 changed = 0;
14699 
14700 	if (wdev->iftype != NL80211_IFTYPE_NAN)
14701 		return -EOPNOTSUPP;
14702 
14703 	if (!wdev_running(wdev))
14704 		return -ENOTCONN;
14705 
14706 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
14707 		conf.master_pref =
14708 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14709 		if (conf.master_pref <= 1 || conf.master_pref == 255)
14710 			return -EINVAL;
14711 
14712 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
14713 	}
14714 
14715 	if (info->attrs[NL80211_ATTR_BANDS]) {
14716 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14717 
14718 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14719 			return -EOPNOTSUPP;
14720 
14721 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14722 			return -EINVAL;
14723 
14724 		conf.bands = bands;
14725 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
14726 	}
14727 
14728 	if (!changed)
14729 		return -EINVAL;
14730 
14731 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
14732 }
14733 
14734 void cfg80211_nan_match(struct wireless_dev *wdev,
14735 			struct cfg80211_nan_match_params *match, gfp_t gfp)
14736 {
14737 	struct wiphy *wiphy = wdev->wiphy;
14738 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14739 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
14740 	struct sk_buff *msg;
14741 	void *hdr;
14742 
14743 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
14744 		return;
14745 
14746 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14747 	if (!msg)
14748 		return;
14749 
14750 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
14751 	if (!hdr) {
14752 		nlmsg_free(msg);
14753 		return;
14754 	}
14755 
14756 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14757 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14758 					 wdev->netdev->ifindex)) ||
14759 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14760 			      NL80211_ATTR_PAD))
14761 		goto nla_put_failure;
14762 
14763 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
14764 			      NL80211_ATTR_PAD) ||
14765 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
14766 		goto nla_put_failure;
14767 
14768 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
14769 	if (!match_attr)
14770 		goto nla_put_failure;
14771 
14772 	local_func_attr = nla_nest_start_noflag(msg,
14773 						NL80211_NAN_MATCH_FUNC_LOCAL);
14774 	if (!local_func_attr)
14775 		goto nla_put_failure;
14776 
14777 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
14778 		goto nla_put_failure;
14779 
14780 	nla_nest_end(msg, local_func_attr);
14781 
14782 	peer_func_attr = nla_nest_start_noflag(msg,
14783 					       NL80211_NAN_MATCH_FUNC_PEER);
14784 	if (!peer_func_attr)
14785 		goto nla_put_failure;
14786 
14787 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
14788 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
14789 		goto nla_put_failure;
14790 
14791 	if (match->info && match->info_len &&
14792 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
14793 		    match->info))
14794 		goto nla_put_failure;
14795 
14796 	nla_nest_end(msg, peer_func_attr);
14797 	nla_nest_end(msg, match_attr);
14798 	genlmsg_end(msg, hdr);
14799 
14800 	if (!wdev->owner_nlportid)
14801 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14802 					msg, 0, NL80211_MCGRP_NAN, gfp);
14803 	else
14804 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14805 				wdev->owner_nlportid);
14806 
14807 	return;
14808 
14809 nla_put_failure:
14810 	nlmsg_free(msg);
14811 }
14812 EXPORT_SYMBOL(cfg80211_nan_match);
14813 
14814 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
14815 				  u8 inst_id,
14816 				  enum nl80211_nan_func_term_reason reason,
14817 				  u64 cookie, gfp_t gfp)
14818 {
14819 	struct wiphy *wiphy = wdev->wiphy;
14820 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14821 	struct sk_buff *msg;
14822 	struct nlattr *func_attr;
14823 	void *hdr;
14824 
14825 	if (WARN_ON(!inst_id))
14826 		return;
14827 
14828 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14829 	if (!msg)
14830 		return;
14831 
14832 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
14833 	if (!hdr) {
14834 		nlmsg_free(msg);
14835 		return;
14836 	}
14837 
14838 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14839 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14840 					 wdev->netdev->ifindex)) ||
14841 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14842 			      NL80211_ATTR_PAD))
14843 		goto nla_put_failure;
14844 
14845 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14846 			      NL80211_ATTR_PAD))
14847 		goto nla_put_failure;
14848 
14849 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14850 	if (!func_attr)
14851 		goto nla_put_failure;
14852 
14853 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
14854 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
14855 		goto nla_put_failure;
14856 
14857 	nla_nest_end(msg, func_attr);
14858 	genlmsg_end(msg, hdr);
14859 
14860 	if (!wdev->owner_nlportid)
14861 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14862 					msg, 0, NL80211_MCGRP_NAN, gfp);
14863 	else
14864 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14865 				wdev->owner_nlportid);
14866 
14867 	return;
14868 
14869 nla_put_failure:
14870 	nlmsg_free(msg);
14871 }
14872 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
14873 
14874 static int nl80211_get_protocol_features(struct sk_buff *skb,
14875 					 struct genl_info *info)
14876 {
14877 	void *hdr;
14878 	struct sk_buff *msg;
14879 
14880 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14881 	if (!msg)
14882 		return -ENOMEM;
14883 
14884 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14885 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
14886 	if (!hdr)
14887 		goto nla_put_failure;
14888 
14889 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
14890 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
14891 		goto nla_put_failure;
14892 
14893 	genlmsg_end(msg, hdr);
14894 	return genlmsg_reply(msg, info);
14895 
14896  nla_put_failure:
14897 	kfree_skb(msg);
14898 	return -ENOBUFS;
14899 }
14900 
14901 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
14902 {
14903 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14904 	struct cfg80211_update_ft_ies_params ft_params;
14905 	struct net_device *dev = info->user_ptr[1];
14906 
14907 	if (!rdev->ops->update_ft_ies)
14908 		return -EOPNOTSUPP;
14909 
14910 	if (!info->attrs[NL80211_ATTR_MDID] ||
14911 	    !info->attrs[NL80211_ATTR_IE])
14912 		return -EINVAL;
14913 
14914 	memset(&ft_params, 0, sizeof(ft_params));
14915 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
14916 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14917 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14918 
14919 	return rdev_update_ft_ies(rdev, dev, &ft_params);
14920 }
14921 
14922 static int nl80211_crit_protocol_start(struct sk_buff *skb,
14923 				       struct genl_info *info)
14924 {
14925 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14926 	struct wireless_dev *wdev = info->user_ptr[1];
14927 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
14928 	u16 duration;
14929 	int ret;
14930 
14931 	if (!rdev->ops->crit_proto_start)
14932 		return -EOPNOTSUPP;
14933 
14934 	if (WARN_ON(!rdev->ops->crit_proto_stop))
14935 		return -EINVAL;
14936 
14937 	if (rdev->crit_proto_nlportid)
14938 		return -EBUSY;
14939 
14940 	/* determine protocol if provided */
14941 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
14942 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
14943 
14944 	if (proto >= NUM_NL80211_CRIT_PROTO)
14945 		return -EINVAL;
14946 
14947 	/* timeout must be provided */
14948 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
14949 		return -EINVAL;
14950 
14951 	duration =
14952 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
14953 
14954 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
14955 	if (!ret)
14956 		rdev->crit_proto_nlportid = info->snd_portid;
14957 
14958 	return ret;
14959 }
14960 
14961 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
14962 				      struct genl_info *info)
14963 {
14964 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14965 	struct wireless_dev *wdev = info->user_ptr[1];
14966 
14967 	if (!rdev->ops->crit_proto_stop)
14968 		return -EOPNOTSUPP;
14969 
14970 	if (rdev->crit_proto_nlportid) {
14971 		rdev->crit_proto_nlportid = 0;
14972 		rdev_crit_proto_stop(rdev, wdev);
14973 	}
14974 	return 0;
14975 }
14976 
14977 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
14978 				       struct nlattr *attr,
14979 				       struct netlink_ext_ack *extack)
14980 {
14981 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
14982 		if (attr->nla_type & NLA_F_NESTED) {
14983 			NL_SET_ERR_MSG_ATTR(extack, attr,
14984 					    "unexpected nested data");
14985 			return -EINVAL;
14986 		}
14987 
14988 		return 0;
14989 	}
14990 
14991 	if (!(attr->nla_type & NLA_F_NESTED)) {
14992 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
14993 		return -EINVAL;
14994 	}
14995 
14996 	return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
14997 }
14998 
14999 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
15000 {
15001 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15002 	struct wireless_dev *wdev =
15003 		__cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
15004 					   info->attrs);
15005 	int i, err;
15006 	u32 vid, subcmd;
15007 
15008 	if (!rdev->wiphy.vendor_commands)
15009 		return -EOPNOTSUPP;
15010 
15011 	if (IS_ERR(wdev)) {
15012 		err = PTR_ERR(wdev);
15013 		if (err != -EINVAL)
15014 			return err;
15015 		wdev = NULL;
15016 	} else if (wdev->wiphy != &rdev->wiphy) {
15017 		return -EINVAL;
15018 	}
15019 
15020 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
15021 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
15022 		return -EINVAL;
15023 
15024 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
15025 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
15026 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
15027 		const struct wiphy_vendor_command *vcmd;
15028 		void *data = NULL;
15029 		int len = 0;
15030 
15031 		vcmd = &rdev->wiphy.vendor_commands[i];
15032 
15033 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
15034 			continue;
15035 
15036 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
15037 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
15038 			if (!wdev)
15039 				return -EINVAL;
15040 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
15041 			    !wdev->netdev)
15042 				return -EINVAL;
15043 
15044 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
15045 				if (!wdev_running(wdev))
15046 					return -ENETDOWN;
15047 			}
15048 		} else {
15049 			wdev = NULL;
15050 		}
15051 
15052 		if (!vcmd->doit)
15053 			return -EOPNOTSUPP;
15054 
15055 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
15056 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
15057 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
15058 
15059 			err = nl80211_vendor_check_policy(vcmd,
15060 					info->attrs[NL80211_ATTR_VENDOR_DATA],
15061 					info->extack);
15062 			if (err)
15063 				return err;
15064 		}
15065 
15066 		rdev->cur_cmd_info = info;
15067 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
15068 		rdev->cur_cmd_info = NULL;
15069 		return err;
15070 	}
15071 
15072 	return -EOPNOTSUPP;
15073 }
15074 
15075 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
15076 				       struct netlink_callback *cb,
15077 				       struct cfg80211_registered_device **rdev,
15078 				       struct wireless_dev **wdev)
15079 {
15080 	struct nlattr **attrbuf;
15081 	u32 vid, subcmd;
15082 	unsigned int i;
15083 	int vcmd_idx = -1;
15084 	int err;
15085 	void *data = NULL;
15086 	unsigned int data_len = 0;
15087 
15088 	if (cb->args[0]) {
15089 		/* subtract the 1 again here */
15090 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
15091 		struct wireless_dev *tmp;
15092 
15093 		if (!wiphy)
15094 			return -ENODEV;
15095 		*rdev = wiphy_to_rdev(wiphy);
15096 		*wdev = NULL;
15097 
15098 		if (cb->args[1]) {
15099 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
15100 				if (tmp->identifier == cb->args[1] - 1) {
15101 					*wdev = tmp;
15102 					break;
15103 				}
15104 			}
15105 		}
15106 
15107 		/* keep rtnl locked in successful case */
15108 		return 0;
15109 	}
15110 
15111 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
15112 	if (!attrbuf)
15113 		return -ENOMEM;
15114 
15115 	err = nlmsg_parse_deprecated(cb->nlh,
15116 				     GENL_HDRLEN + nl80211_fam.hdrsize,
15117 				     attrbuf, nl80211_fam.maxattr,
15118 				     nl80211_policy, NULL);
15119 	if (err)
15120 		goto out;
15121 
15122 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
15123 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
15124 		err = -EINVAL;
15125 		goto out;
15126 	}
15127 
15128 	*wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(skb->sk), attrbuf);
15129 	if (IS_ERR(*wdev))
15130 		*wdev = NULL;
15131 
15132 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
15133 	if (IS_ERR(*rdev)) {
15134 		err = PTR_ERR(*rdev);
15135 		goto out;
15136 	}
15137 
15138 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
15139 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
15140 
15141 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
15142 		const struct wiphy_vendor_command *vcmd;
15143 
15144 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
15145 
15146 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
15147 			continue;
15148 
15149 		if (!vcmd->dumpit) {
15150 			err = -EOPNOTSUPP;
15151 			goto out;
15152 		}
15153 
15154 		vcmd_idx = i;
15155 		break;
15156 	}
15157 
15158 	if (vcmd_idx < 0) {
15159 		err = -EOPNOTSUPP;
15160 		goto out;
15161 	}
15162 
15163 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
15164 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15165 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
15166 
15167 		err = nl80211_vendor_check_policy(
15168 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
15169 				attrbuf[NL80211_ATTR_VENDOR_DATA],
15170 				cb->extack);
15171 		if (err)
15172 			goto out;
15173 	}
15174 
15175 	/* 0 is the first index - add 1 to parse only once */
15176 	cb->args[0] = (*rdev)->wiphy_idx + 1;
15177 	/* add 1 to know if it was NULL */
15178 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
15179 	cb->args[2] = vcmd_idx;
15180 	cb->args[3] = (unsigned long)data;
15181 	cb->args[4] = data_len;
15182 
15183 	/* keep rtnl locked in successful case */
15184 	err = 0;
15185 out:
15186 	kfree(attrbuf);
15187 	return err;
15188 }
15189 
15190 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
15191 				   struct netlink_callback *cb)
15192 {
15193 	struct cfg80211_registered_device *rdev;
15194 	struct wireless_dev *wdev;
15195 	unsigned int vcmd_idx;
15196 	const struct wiphy_vendor_command *vcmd;
15197 	void *data;
15198 	int data_len;
15199 	int err;
15200 	struct nlattr *vendor_data;
15201 
15202 	rtnl_lock();
15203 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
15204 	if (err)
15205 		goto out;
15206 
15207 	vcmd_idx = cb->args[2];
15208 	data = (void *)cb->args[3];
15209 	data_len = cb->args[4];
15210 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
15211 
15212 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
15213 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
15214 		if (!wdev) {
15215 			err = -EINVAL;
15216 			goto out;
15217 		}
15218 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
15219 		    !wdev->netdev) {
15220 			err = -EINVAL;
15221 			goto out;
15222 		}
15223 
15224 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
15225 			if (!wdev_running(wdev)) {
15226 				err = -ENETDOWN;
15227 				goto out;
15228 			}
15229 		}
15230 	}
15231 
15232 	while (1) {
15233 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
15234 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
15235 					   NL80211_CMD_VENDOR);
15236 		if (!hdr)
15237 			break;
15238 
15239 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15240 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
15241 					       wdev_id(wdev),
15242 					       NL80211_ATTR_PAD))) {
15243 			genlmsg_cancel(skb, hdr);
15244 			break;
15245 		}
15246 
15247 		vendor_data = nla_nest_start_noflag(skb,
15248 						    NL80211_ATTR_VENDOR_DATA);
15249 		if (!vendor_data) {
15250 			genlmsg_cancel(skb, hdr);
15251 			break;
15252 		}
15253 
15254 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
15255 				   (unsigned long *)&cb->args[5]);
15256 		nla_nest_end(skb, vendor_data);
15257 
15258 		if (err == -ENOBUFS || err == -ENOENT) {
15259 			genlmsg_cancel(skb, hdr);
15260 			break;
15261 		} else if (err <= 0) {
15262 			genlmsg_cancel(skb, hdr);
15263 			goto out;
15264 		}
15265 
15266 		genlmsg_end(skb, hdr);
15267 	}
15268 
15269 	err = skb->len;
15270  out:
15271 	rtnl_unlock();
15272 	return err;
15273 }
15274 
15275 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
15276 					   enum nl80211_commands cmd,
15277 					   enum nl80211_attrs attr,
15278 					   int approxlen)
15279 {
15280 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15281 
15282 	if (WARN_ON(!rdev->cur_cmd_info))
15283 		return NULL;
15284 
15285 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
15286 					   rdev->cur_cmd_info->snd_portid,
15287 					   rdev->cur_cmd_info->snd_seq,
15288 					   cmd, attr, NULL, GFP_KERNEL);
15289 }
15290 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
15291 
15292 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
15293 {
15294 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
15295 	void *hdr = ((void **)skb->cb)[1];
15296 	struct nlattr *data = ((void **)skb->cb)[2];
15297 
15298 	/* clear CB data for netlink core to own from now on */
15299 	memset(skb->cb, 0, sizeof(skb->cb));
15300 
15301 	if (WARN_ON(!rdev->cur_cmd_info)) {
15302 		kfree_skb(skb);
15303 		return -EINVAL;
15304 	}
15305 
15306 	nla_nest_end(skb, data);
15307 	genlmsg_end(skb, hdr);
15308 	return genlmsg_reply(skb, rdev->cur_cmd_info);
15309 }
15310 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
15311 
15312 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
15313 {
15314 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15315 
15316 	if (WARN_ON(!rdev->cur_cmd_info))
15317 		return 0;
15318 
15319 	return rdev->cur_cmd_info->snd_portid;
15320 }
15321 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
15322 
15323 static int nl80211_set_qos_map(struct sk_buff *skb,
15324 			       struct genl_info *info)
15325 {
15326 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15327 	struct cfg80211_qos_map *qos_map = NULL;
15328 	struct net_device *dev = info->user_ptr[1];
15329 	u8 *pos, len, num_des, des_len, des;
15330 	int ret;
15331 
15332 	if (!rdev->ops->set_qos_map)
15333 		return -EOPNOTSUPP;
15334 
15335 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
15336 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
15337 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
15338 
15339 		if (len % 2)
15340 			return -EINVAL;
15341 
15342 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
15343 		if (!qos_map)
15344 			return -ENOMEM;
15345 
15346 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
15347 		if (num_des) {
15348 			des_len = num_des *
15349 				sizeof(struct cfg80211_dscp_exception);
15350 			memcpy(qos_map->dscp_exception, pos, des_len);
15351 			qos_map->num_des = num_des;
15352 			for (des = 0; des < num_des; des++) {
15353 				if (qos_map->dscp_exception[des].up > 7) {
15354 					kfree(qos_map);
15355 					return -EINVAL;
15356 				}
15357 			}
15358 			pos += des_len;
15359 		}
15360 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
15361 	}
15362 
15363 	wdev_lock(dev->ieee80211_ptr);
15364 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
15365 	if (!ret)
15366 		ret = rdev_set_qos_map(rdev, dev, qos_map);
15367 	wdev_unlock(dev->ieee80211_ptr);
15368 
15369 	kfree(qos_map);
15370 	return ret;
15371 }
15372 
15373 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
15374 {
15375 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15376 	struct net_device *dev = info->user_ptr[1];
15377 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15378 	const u8 *peer;
15379 	u8 tsid, up;
15380 	u16 admitted_time = 0;
15381 	int err;
15382 
15383 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
15384 		return -EOPNOTSUPP;
15385 
15386 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
15387 	    !info->attrs[NL80211_ATTR_USER_PRIO])
15388 		return -EINVAL;
15389 
15390 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15391 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
15392 
15393 	/* WMM uses TIDs 0-7 even for TSPEC */
15394 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
15395 		/* TODO: handle 802.11 TSPEC/admission control
15396 		 * need more attributes for that (e.g. BA session requirement);
15397 		 * change the WMM adminssion test above to allow both then
15398 		 */
15399 		return -EINVAL;
15400 	}
15401 
15402 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15403 
15404 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
15405 		admitted_time =
15406 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
15407 		if (!admitted_time)
15408 			return -EINVAL;
15409 	}
15410 
15411 	wdev_lock(wdev);
15412 	switch (wdev->iftype) {
15413 	case NL80211_IFTYPE_STATION:
15414 	case NL80211_IFTYPE_P2P_CLIENT:
15415 		if (wdev->connected)
15416 			break;
15417 		err = -ENOTCONN;
15418 		goto out;
15419 	default:
15420 		err = -EOPNOTSUPP;
15421 		goto out;
15422 	}
15423 
15424 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
15425 
15426  out:
15427 	wdev_unlock(wdev);
15428 	return err;
15429 }
15430 
15431 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
15432 {
15433 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15434 	struct net_device *dev = info->user_ptr[1];
15435 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15436 	const u8 *peer;
15437 	u8 tsid;
15438 	int err;
15439 
15440 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
15441 		return -EINVAL;
15442 
15443 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15444 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15445 
15446 	wdev_lock(wdev);
15447 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
15448 	wdev_unlock(wdev);
15449 
15450 	return err;
15451 }
15452 
15453 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
15454 				       struct genl_info *info)
15455 {
15456 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15457 	struct net_device *dev = info->user_ptr[1];
15458 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15459 	struct cfg80211_chan_def chandef = {};
15460 	const u8 *addr;
15461 	u8 oper_class;
15462 	int err;
15463 
15464 	if (!rdev->ops->tdls_channel_switch ||
15465 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15466 		return -EOPNOTSUPP;
15467 
15468 	switch (dev->ieee80211_ptr->iftype) {
15469 	case NL80211_IFTYPE_STATION:
15470 	case NL80211_IFTYPE_P2P_CLIENT:
15471 		break;
15472 	default:
15473 		return -EOPNOTSUPP;
15474 	}
15475 
15476 	if (!info->attrs[NL80211_ATTR_MAC] ||
15477 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
15478 		return -EINVAL;
15479 
15480 	err = nl80211_parse_chandef(rdev, info, &chandef);
15481 	if (err)
15482 		return err;
15483 
15484 	/*
15485 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
15486 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
15487 	 * specification is not defined for them.
15488 	 */
15489 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
15490 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
15491 	    chandef.width != NL80211_CHAN_WIDTH_20)
15492 		return -EINVAL;
15493 
15494 	/* we will be active on the TDLS link */
15495 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
15496 					   wdev->iftype))
15497 		return -EINVAL;
15498 
15499 	/* don't allow switching to DFS channels */
15500 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
15501 		return -EINVAL;
15502 
15503 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15504 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
15505 
15506 	wdev_lock(wdev);
15507 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
15508 	wdev_unlock(wdev);
15509 
15510 	return err;
15511 }
15512 
15513 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
15514 					      struct genl_info *info)
15515 {
15516 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15517 	struct net_device *dev = info->user_ptr[1];
15518 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15519 	const u8 *addr;
15520 
15521 	if (!rdev->ops->tdls_channel_switch ||
15522 	    !rdev->ops->tdls_cancel_channel_switch ||
15523 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15524 		return -EOPNOTSUPP;
15525 
15526 	switch (dev->ieee80211_ptr->iftype) {
15527 	case NL80211_IFTYPE_STATION:
15528 	case NL80211_IFTYPE_P2P_CLIENT:
15529 		break;
15530 	default:
15531 		return -EOPNOTSUPP;
15532 	}
15533 
15534 	if (!info->attrs[NL80211_ATTR_MAC])
15535 		return -EINVAL;
15536 
15537 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15538 
15539 	wdev_lock(wdev);
15540 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
15541 	wdev_unlock(wdev);
15542 
15543 	return 0;
15544 }
15545 
15546 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
15547 					    struct genl_info *info)
15548 {
15549 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15550 	struct net_device *dev = info->user_ptr[1];
15551 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15552 	const struct nlattr *nla;
15553 	bool enabled;
15554 
15555 	if (!rdev->ops->set_multicast_to_unicast)
15556 		return -EOPNOTSUPP;
15557 
15558 	if (wdev->iftype != NL80211_IFTYPE_AP &&
15559 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
15560 		return -EOPNOTSUPP;
15561 
15562 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
15563 	enabled = nla_get_flag(nla);
15564 
15565 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
15566 }
15567 
15568 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
15569 {
15570 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15571 	struct net_device *dev = info->user_ptr[1];
15572 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15573 	struct cfg80211_pmk_conf pmk_conf = {};
15574 	int ret;
15575 
15576 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15577 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15578 		return -EOPNOTSUPP;
15579 
15580 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15581 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15582 		return -EOPNOTSUPP;
15583 
15584 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
15585 		return -EINVAL;
15586 
15587 	wdev_lock(wdev);
15588 	if (!wdev->connected) {
15589 		ret = -ENOTCONN;
15590 		goto out;
15591 	}
15592 
15593 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15594 	if (memcmp(pmk_conf.aa, wdev->u.client.connected_addr, ETH_ALEN)) {
15595 		ret = -EINVAL;
15596 		goto out;
15597 	}
15598 
15599 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
15600 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
15601 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
15602 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
15603 		ret = -EINVAL;
15604 		goto out;
15605 	}
15606 
15607 	if (info->attrs[NL80211_ATTR_PMKR0_NAME])
15608 		pmk_conf.pmk_r0_name =
15609 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
15610 
15611 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
15612 out:
15613 	wdev_unlock(wdev);
15614 	return ret;
15615 }
15616 
15617 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
15618 {
15619 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15620 	struct net_device *dev = info->user_ptr[1];
15621 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15622 	const u8 *aa;
15623 	int ret;
15624 
15625 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
15626 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15627 		return -EOPNOTSUPP;
15628 
15629 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15630 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15631 		return -EOPNOTSUPP;
15632 
15633 	if (!info->attrs[NL80211_ATTR_MAC])
15634 		return -EINVAL;
15635 
15636 	wdev_lock(wdev);
15637 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15638 	ret = rdev_del_pmk(rdev, dev, aa);
15639 	wdev_unlock(wdev);
15640 
15641 	return ret;
15642 }
15643 
15644 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
15645 {
15646 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15647 	struct net_device *dev = info->user_ptr[1];
15648 	struct cfg80211_external_auth_params params;
15649 
15650 	if (!rdev->ops->external_auth)
15651 		return -EOPNOTSUPP;
15652 
15653 	if (!info->attrs[NL80211_ATTR_SSID] &&
15654 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
15655 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
15656 		return -EINVAL;
15657 
15658 	if (!info->attrs[NL80211_ATTR_BSSID])
15659 		return -EINVAL;
15660 
15661 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
15662 		return -EINVAL;
15663 
15664 	memset(&params, 0, sizeof(params));
15665 
15666 	if (info->attrs[NL80211_ATTR_SSID]) {
15667 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
15668 		if (params.ssid.ssid_len == 0)
15669 			return -EINVAL;
15670 		memcpy(params.ssid.ssid,
15671 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
15672 		       params.ssid.ssid_len);
15673 	}
15674 
15675 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
15676 	       ETH_ALEN);
15677 
15678 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15679 
15680 	if (info->attrs[NL80211_ATTR_PMKID])
15681 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
15682 
15683 	return rdev_external_auth(rdev, dev, &params);
15684 }
15685 
15686 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
15687 {
15688 	bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
15689 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15690 	struct net_device *dev = info->user_ptr[1];
15691 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15692 	const u8 *buf;
15693 	size_t len;
15694 	u8 *dest;
15695 	u16 proto;
15696 	bool noencrypt;
15697 	u64 cookie = 0;
15698 	int link_id;
15699 	int err;
15700 
15701 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
15702 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
15703 		return -EOPNOTSUPP;
15704 
15705 	if (!rdev->ops->tx_control_port)
15706 		return -EOPNOTSUPP;
15707 
15708 	if (!info->attrs[NL80211_ATTR_FRAME] ||
15709 	    !info->attrs[NL80211_ATTR_MAC] ||
15710 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
15711 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
15712 		return -EINVAL;
15713 	}
15714 
15715 	wdev_lock(wdev);
15716 
15717 	switch (wdev->iftype) {
15718 	case NL80211_IFTYPE_AP:
15719 	case NL80211_IFTYPE_P2P_GO:
15720 	case NL80211_IFTYPE_MESH_POINT:
15721 		break;
15722 	case NL80211_IFTYPE_ADHOC:
15723 		if (wdev->u.ibss.current_bss)
15724 			break;
15725 		err = -ENOTCONN;
15726 		goto out;
15727 	case NL80211_IFTYPE_STATION:
15728 	case NL80211_IFTYPE_P2P_CLIENT:
15729 		if (wdev->connected)
15730 			break;
15731 		err = -ENOTCONN;
15732 		goto out;
15733 	default:
15734 		err = -EOPNOTSUPP;
15735 		goto out;
15736 	}
15737 
15738 	wdev_unlock(wdev);
15739 
15740 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15741 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15742 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15743 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
15744 	noencrypt =
15745 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
15746 
15747 	link_id = nl80211_link_id_or_invalid(info->attrs);
15748 
15749 	err = rdev_tx_control_port(rdev, dev, buf, len,
15750 				   dest, cpu_to_be16(proto), noencrypt, link_id,
15751 				   dont_wait_for_ack ? NULL : &cookie);
15752 	if (!err && !dont_wait_for_ack)
15753 		nl_set_extack_cookie_u64(info->extack, cookie);
15754 	return err;
15755  out:
15756 	wdev_unlock(wdev);
15757 	return err;
15758 }
15759 
15760 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
15761 					   struct genl_info *info)
15762 {
15763 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15764 	struct net_device *dev = info->user_ptr[1];
15765 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15766 	struct cfg80211_ftm_responder_stats ftm_stats = {};
15767 	unsigned int link_id = nl80211_link_id(info->attrs);
15768 	struct sk_buff *msg;
15769 	void *hdr;
15770 	struct nlattr *ftm_stats_attr;
15771 	int err;
15772 
15773 	if (wdev->iftype != NL80211_IFTYPE_AP ||
15774 	    !wdev->links[link_id].ap.beacon_interval)
15775 		return -EOPNOTSUPP;
15776 
15777 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
15778 	if (err)
15779 		return err;
15780 
15781 	if (!ftm_stats.filled)
15782 		return -ENODATA;
15783 
15784 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15785 	if (!msg)
15786 		return -ENOMEM;
15787 
15788 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
15789 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
15790 	if (!hdr)
15791 		goto nla_put_failure;
15792 
15793 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15794 		goto nla_put_failure;
15795 
15796 	ftm_stats_attr = nla_nest_start_noflag(msg,
15797 					       NL80211_ATTR_FTM_RESPONDER_STATS);
15798 	if (!ftm_stats_attr)
15799 		goto nla_put_failure;
15800 
15801 #define SET_FTM(field, name, type)					 \
15802 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15803 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
15804 			     ftm_stats.field))				 \
15805 		goto nla_put_failure; } while (0)
15806 #define SET_FTM_U64(field, name)					 \
15807 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15808 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
15809 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
15810 		goto nla_put_failure; } while (0)
15811 
15812 	SET_FTM(success_num, SUCCESS_NUM, u32);
15813 	SET_FTM(partial_num, PARTIAL_NUM, u32);
15814 	SET_FTM(failed_num, FAILED_NUM, u32);
15815 	SET_FTM(asap_num, ASAP_NUM, u32);
15816 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
15817 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
15818 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
15819 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
15820 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
15821 #undef SET_FTM
15822 
15823 	nla_nest_end(msg, ftm_stats_attr);
15824 
15825 	genlmsg_end(msg, hdr);
15826 	return genlmsg_reply(msg, info);
15827 
15828 nla_put_failure:
15829 	nlmsg_free(msg);
15830 	return -ENOBUFS;
15831 }
15832 
15833 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
15834 {
15835 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15836 	struct cfg80211_update_owe_info owe_info;
15837 	struct net_device *dev = info->user_ptr[1];
15838 
15839 	if (!rdev->ops->update_owe_info)
15840 		return -EOPNOTSUPP;
15841 
15842 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
15843 	    !info->attrs[NL80211_ATTR_MAC])
15844 		return -EINVAL;
15845 
15846 	memset(&owe_info, 0, sizeof(owe_info));
15847 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15848 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
15849 
15850 	if (info->attrs[NL80211_ATTR_IE]) {
15851 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
15852 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
15853 	}
15854 
15855 	return rdev_update_owe_info(rdev, dev, &owe_info);
15856 }
15857 
15858 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
15859 {
15860 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
15861 	struct net_device *dev = info->user_ptr[1];
15862 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15863 	struct station_info sinfo = {};
15864 	const u8 *buf;
15865 	size_t len;
15866 	u8 *dest;
15867 	int err;
15868 
15869 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
15870 		return -EOPNOTSUPP;
15871 
15872 	if (!info->attrs[NL80211_ATTR_MAC] ||
15873 	    !info->attrs[NL80211_ATTR_FRAME]) {
15874 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
15875 		return -EINVAL;
15876 	}
15877 
15878 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
15879 		return -EOPNOTSUPP;
15880 
15881 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15882 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15883 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15884 
15885 	if (len < sizeof(struct ethhdr))
15886 		return -EINVAL;
15887 
15888 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
15889 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
15890 		return -EINVAL;
15891 
15892 	err = rdev_get_station(rdev, dev, dest, &sinfo);
15893 	if (err)
15894 		return err;
15895 
15896 	cfg80211_sinfo_release_content(&sinfo);
15897 
15898 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
15899 }
15900 
15901 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
15902 			  struct nlattr *attrs[], struct net_device *dev,
15903 			  struct cfg80211_tid_cfg *tid_conf,
15904 			  struct genl_info *info, const u8 *peer,
15905 			  unsigned int link_id)
15906 {
15907 	struct netlink_ext_ack *extack = info->extack;
15908 	u64 mask;
15909 	int err;
15910 
15911 	if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
15912 		return -EINVAL;
15913 
15914 	tid_conf->config_override =
15915 			nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
15916 	tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
15917 
15918 	if (tid_conf->config_override) {
15919 		if (rdev->ops->reset_tid_config) {
15920 			err = rdev_reset_tid_config(rdev, dev, peer,
15921 						    tid_conf->tids);
15922 			if (err)
15923 				return err;
15924 		} else {
15925 			return -EINVAL;
15926 		}
15927 	}
15928 
15929 	if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
15930 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
15931 		tid_conf->noack =
15932 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
15933 	}
15934 
15935 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
15936 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
15937 		tid_conf->retry_short =
15938 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
15939 
15940 		if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
15941 			return -EINVAL;
15942 	}
15943 
15944 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
15945 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
15946 		tid_conf->retry_long =
15947 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
15948 
15949 		if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
15950 			return -EINVAL;
15951 	}
15952 
15953 	if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
15954 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
15955 		tid_conf->ampdu =
15956 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
15957 	}
15958 
15959 	if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
15960 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
15961 		tid_conf->rtscts =
15962 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
15963 	}
15964 
15965 	if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
15966 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
15967 		tid_conf->amsdu =
15968 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
15969 	}
15970 
15971 	if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
15972 		u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
15973 
15974 		tid_conf->txrate_type = nla_get_u8(attrs[idx]);
15975 
15976 		if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
15977 			attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
15978 			err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
15979 						    &tid_conf->txrate_mask, dev,
15980 						    true, link_id);
15981 			if (err)
15982 				return err;
15983 
15984 			tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
15985 		}
15986 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
15987 	}
15988 
15989 	if (peer)
15990 		mask = rdev->wiphy.tid_config_support.peer;
15991 	else
15992 		mask = rdev->wiphy.tid_config_support.vif;
15993 
15994 	if (tid_conf->mask & ~mask) {
15995 		NL_SET_ERR_MSG(extack, "unsupported TID configuration");
15996 		return -ENOTSUPP;
15997 	}
15998 
15999 	return 0;
16000 }
16001 
16002 static int nl80211_set_tid_config(struct sk_buff *skb,
16003 				  struct genl_info *info)
16004 {
16005 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16006 	struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
16007 	unsigned int link_id = nl80211_link_id(info->attrs);
16008 	struct net_device *dev = info->user_ptr[1];
16009 	struct cfg80211_tid_config *tid_config;
16010 	struct nlattr *tid;
16011 	int conf_idx = 0, rem_conf;
16012 	int ret = -EINVAL;
16013 	u32 num_conf = 0;
16014 
16015 	if (!info->attrs[NL80211_ATTR_TID_CONFIG])
16016 		return -EINVAL;
16017 
16018 	if (!rdev->ops->set_tid_config)
16019 		return -EOPNOTSUPP;
16020 
16021 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
16022 			    rem_conf)
16023 		num_conf++;
16024 
16025 	tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
16026 			     GFP_KERNEL);
16027 	if (!tid_config)
16028 		return -ENOMEM;
16029 
16030 	tid_config->n_tid_conf = num_conf;
16031 
16032 	if (info->attrs[NL80211_ATTR_MAC])
16033 		tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
16034 
16035 	wdev_lock(dev->ieee80211_ptr);
16036 
16037 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
16038 			    rem_conf) {
16039 		ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
16040 				       tid, NULL, NULL);
16041 
16042 		if (ret)
16043 			goto bad_tid_conf;
16044 
16045 		ret = parse_tid_conf(rdev, attrs, dev,
16046 				     &tid_config->tid_conf[conf_idx],
16047 				     info, tid_config->peer, link_id);
16048 		if (ret)
16049 			goto bad_tid_conf;
16050 
16051 		conf_idx++;
16052 	}
16053 
16054 	ret = rdev_set_tid_config(rdev, dev, tid_config);
16055 
16056 bad_tid_conf:
16057 	kfree(tid_config);
16058 	wdev_unlock(dev->ieee80211_ptr);
16059 	return ret;
16060 }
16061 
16062 static int nl80211_color_change(struct sk_buff *skb, struct genl_info *info)
16063 {
16064 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16065 	struct cfg80211_color_change_settings params = {};
16066 	struct net_device *dev = info->user_ptr[1];
16067 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16068 	struct nlattr **tb;
16069 	u16 offset;
16070 	int err;
16071 
16072 	if (!rdev->ops->color_change)
16073 		return -EOPNOTSUPP;
16074 
16075 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
16076 				     NL80211_EXT_FEATURE_BSS_COLOR))
16077 		return -EOPNOTSUPP;
16078 
16079 	if (wdev->iftype != NL80211_IFTYPE_AP)
16080 		return -EOPNOTSUPP;
16081 
16082 	if (!info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT] ||
16083 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR] ||
16084 	    !info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS])
16085 		return -EINVAL;
16086 
16087 	params.count = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT]);
16088 	params.color = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR]);
16089 
16090 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_next,
16091 				   info->extack);
16092 	if (err)
16093 		return err;
16094 
16095 	tb = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*tb), GFP_KERNEL);
16096 	if (!tb)
16097 		return -ENOMEM;
16098 
16099 	err = nla_parse_nested(tb, NL80211_ATTR_MAX,
16100 			       info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS],
16101 			       nl80211_policy, info->extack);
16102 	if (err)
16103 		goto out;
16104 
16105 	err = nl80211_parse_beacon(rdev, tb, &params.beacon_color_change,
16106 				   info->extack);
16107 	if (err)
16108 		goto out;
16109 
16110 	if (!tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
16111 		err = -EINVAL;
16112 		goto out;
16113 	}
16114 
16115 	if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) != sizeof(u16)) {
16116 		err = -EINVAL;
16117 		goto out;
16118 	}
16119 
16120 	offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
16121 	if (offset >= params.beacon_color_change.tail_len) {
16122 		err = -EINVAL;
16123 		goto out;
16124 	}
16125 
16126 	if (params.beacon_color_change.tail[offset] != params.count) {
16127 		err = -EINVAL;
16128 		goto out;
16129 	}
16130 
16131 	params.counter_offset_beacon = offset;
16132 
16133 	if (tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
16134 		if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) !=
16135 		    sizeof(u16)) {
16136 			err = -EINVAL;
16137 			goto out;
16138 		}
16139 
16140 		offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
16141 		if (offset >= params.beacon_color_change.probe_resp_len) {
16142 			err = -EINVAL;
16143 			goto out;
16144 		}
16145 
16146 		if (params.beacon_color_change.probe_resp[offset] !=
16147 		    params.count) {
16148 			err = -EINVAL;
16149 			goto out;
16150 		}
16151 
16152 		params.counter_offset_presp = offset;
16153 	}
16154 
16155 	wdev_lock(wdev);
16156 	err = rdev_color_change(rdev, dev, &params);
16157 	wdev_unlock(wdev);
16158 
16159 out:
16160 	kfree(params.beacon_next.mbssid_ies);
16161 	kfree(params.beacon_color_change.mbssid_ies);
16162 	kfree(params.beacon_next.rnr_ies);
16163 	kfree(params.beacon_color_change.rnr_ies);
16164 	kfree(tb);
16165 	return err;
16166 }
16167 
16168 static int nl80211_set_fils_aad(struct sk_buff *skb,
16169 				struct genl_info *info)
16170 {
16171 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16172 	struct net_device *dev = info->user_ptr[1];
16173 	struct cfg80211_fils_aad fils_aad = {};
16174 	u8 *nonces;
16175 
16176 	if (!info->attrs[NL80211_ATTR_MAC] ||
16177 	    !info->attrs[NL80211_ATTR_FILS_KEK] ||
16178 	    !info->attrs[NL80211_ATTR_FILS_NONCES])
16179 		return -EINVAL;
16180 
16181 	fils_aad.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16182 	fils_aad.kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
16183 	fils_aad.kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
16184 	nonces = nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
16185 	fils_aad.snonce = nonces;
16186 	fils_aad.anonce = nonces + FILS_NONCE_LEN;
16187 
16188 	return rdev_set_fils_aad(rdev, dev, &fils_aad);
16189 }
16190 
16191 static int nl80211_add_link(struct sk_buff *skb, struct genl_info *info)
16192 {
16193 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16194 	unsigned int link_id = nl80211_link_id(info->attrs);
16195 	struct net_device *dev = info->user_ptr[1];
16196 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16197 	int ret;
16198 
16199 	if (!(wdev->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
16200 		return -EINVAL;
16201 
16202 	switch (wdev->iftype) {
16203 	case NL80211_IFTYPE_AP:
16204 		break;
16205 	default:
16206 		return -EINVAL;
16207 	}
16208 
16209 	if (!info->attrs[NL80211_ATTR_MAC] ||
16210 	    !is_valid_ether_addr(nla_data(info->attrs[NL80211_ATTR_MAC])))
16211 		return -EINVAL;
16212 
16213 	wdev_lock(wdev);
16214 	wdev->valid_links |= BIT(link_id);
16215 	ether_addr_copy(wdev->links[link_id].addr,
16216 			nla_data(info->attrs[NL80211_ATTR_MAC]));
16217 
16218 	ret = rdev_add_intf_link(rdev, wdev, link_id);
16219 	if (ret) {
16220 		wdev->valid_links &= ~BIT(link_id);
16221 		eth_zero_addr(wdev->links[link_id].addr);
16222 	}
16223 	wdev_unlock(wdev);
16224 
16225 	return ret;
16226 }
16227 
16228 static int nl80211_remove_link(struct sk_buff *skb, struct genl_info *info)
16229 {
16230 	unsigned int link_id = nl80211_link_id(info->attrs);
16231 	struct net_device *dev = info->user_ptr[1];
16232 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16233 
16234 	/* cannot remove if there's no link */
16235 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16236 		return -EINVAL;
16237 
16238 	switch (wdev->iftype) {
16239 	case NL80211_IFTYPE_AP:
16240 		break;
16241 	default:
16242 		return -EINVAL;
16243 	}
16244 
16245 	wdev_lock(wdev);
16246 	cfg80211_remove_link(wdev, link_id);
16247 	wdev_unlock(wdev);
16248 
16249 	return 0;
16250 }
16251 
16252 static int
16253 nl80211_add_mod_link_station(struct sk_buff *skb, struct genl_info *info,
16254 			     bool add)
16255 {
16256 	struct link_station_parameters params = {};
16257 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16258 	struct net_device *dev = info->user_ptr[1];
16259 	int err;
16260 
16261 	if ((add && !rdev->ops->add_link_station) ||
16262 	    (!add && !rdev->ops->mod_link_station))
16263 		return -EOPNOTSUPP;
16264 
16265 	if (add && !info->attrs[NL80211_ATTR_MAC])
16266 		return -EINVAL;
16267 
16268 	if (!info->attrs[NL80211_ATTR_MLD_ADDR])
16269 		return -EINVAL;
16270 
16271 	if (add && !info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
16272 		return -EINVAL;
16273 
16274 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16275 
16276 	if (info->attrs[NL80211_ATTR_MAC]) {
16277 		params.link_mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
16278 		if (!is_valid_ether_addr(params.link_mac))
16279 			return -EINVAL;
16280 	}
16281 
16282 	if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
16283 		return -EINVAL;
16284 
16285 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16286 
16287 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
16288 		params.supported_rates =
16289 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16290 		params.supported_rates_len =
16291 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
16292 	}
16293 
16294 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
16295 		params.ht_capa =
16296 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
16297 
16298 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
16299 		params.vht_capa =
16300 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
16301 
16302 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
16303 		params.he_capa =
16304 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16305 		params.he_capa_len =
16306 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
16307 
16308 		if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
16309 			params.eht_capa =
16310 				nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16311 			params.eht_capa_len =
16312 				nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
16313 
16314 			if (!ieee80211_eht_capa_size_ok((const u8 *)params.he_capa,
16315 							(const u8 *)params.eht_capa,
16316 							params.eht_capa_len,
16317 							false))
16318 				return -EINVAL;
16319 		}
16320 	}
16321 
16322 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
16323 		params.he_6ghz_capa =
16324 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
16325 
16326 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
16327 		params.opmode_notif_used = true;
16328 		params.opmode_notif =
16329 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
16330 	}
16331 
16332 	err = nl80211_parse_sta_txpower_setting(info, &params.txpwr,
16333 						&params.txpwr_set);
16334 	if (err)
16335 		return err;
16336 
16337 	wdev_lock(dev->ieee80211_ptr);
16338 	if (add)
16339 		err = rdev_add_link_station(rdev, dev, &params);
16340 	else
16341 		err = rdev_mod_link_station(rdev, dev, &params);
16342 	wdev_unlock(dev->ieee80211_ptr);
16343 
16344 	return err;
16345 }
16346 
16347 static int
16348 nl80211_add_link_station(struct sk_buff *skb, struct genl_info *info)
16349 {
16350 	return nl80211_add_mod_link_station(skb, info, true);
16351 }
16352 
16353 static int
16354 nl80211_modify_link_station(struct sk_buff *skb, struct genl_info *info)
16355 {
16356 	return nl80211_add_mod_link_station(skb, info, false);
16357 }
16358 
16359 static int
16360 nl80211_remove_link_station(struct sk_buff *skb, struct genl_info *info)
16361 {
16362 	struct link_station_del_parameters params = {};
16363 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16364 	struct net_device *dev = info->user_ptr[1];
16365 	int ret;
16366 
16367 	if (!rdev->ops->del_link_station)
16368 		return -EOPNOTSUPP;
16369 
16370 	if (!info->attrs[NL80211_ATTR_MLD_ADDR] ||
16371 	    !info->attrs[NL80211_ATTR_MLO_LINK_ID])
16372 		return -EINVAL;
16373 
16374 	params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16375 	params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16376 
16377 	wdev_lock(dev->ieee80211_ptr);
16378 	ret = rdev_del_link_station(rdev, dev, &params);
16379 	wdev_unlock(dev->ieee80211_ptr);
16380 
16381 	return ret;
16382 }
16383 
16384 static int nl80211_set_hw_timestamp(struct sk_buff *skb,
16385 				    struct genl_info *info)
16386 {
16387 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16388 	struct net_device *dev = info->user_ptr[1];
16389 	struct cfg80211_set_hw_timestamp hwts = {};
16390 
16391 	if (!rdev->wiphy.hw_timestamp_max_peers)
16392 		return -EOPNOTSUPP;
16393 
16394 	if (!info->attrs[NL80211_ATTR_MAC] &&
16395 	    rdev->wiphy.hw_timestamp_max_peers != CFG80211_HW_TIMESTAMP_ALL_PEERS)
16396 		return -EOPNOTSUPP;
16397 
16398 	if (info->attrs[NL80211_ATTR_MAC])
16399 		hwts.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
16400 
16401 	hwts.enable =
16402 		nla_get_flag(info->attrs[NL80211_ATTR_HW_TIMESTAMP_ENABLED]);
16403 
16404 	return rdev_set_hw_timestamp(rdev, dev, &hwts);
16405 }
16406 
16407 #define NL80211_FLAG_NEED_WIPHY		0x01
16408 #define NL80211_FLAG_NEED_NETDEV	0x02
16409 #define NL80211_FLAG_NEED_RTNL		0x04
16410 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
16411 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
16412 					 NL80211_FLAG_CHECK_NETDEV_UP)
16413 #define NL80211_FLAG_NEED_WDEV		0x10
16414 /* If a netdev is associated, it must be UP, P2P must be started */
16415 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
16416 					 NL80211_FLAG_CHECK_NETDEV_UP)
16417 #define NL80211_FLAG_CLEAR_SKB		0x20
16418 #define NL80211_FLAG_NO_WIPHY_MTX	0x40
16419 #define NL80211_FLAG_MLO_VALID_LINK_ID	0x80
16420 #define NL80211_FLAG_MLO_UNSUPPORTED	0x100
16421 
16422 #define INTERNAL_FLAG_SELECTORS(__sel)			\
16423 	SELECTOR(__sel, NONE, 0) /* must be first */	\
16424 	SELECTOR(__sel, WIPHY,				\
16425 		 NL80211_FLAG_NEED_WIPHY)		\
16426 	SELECTOR(__sel, WDEV,				\
16427 		 NL80211_FLAG_NEED_WDEV)		\
16428 	SELECTOR(__sel, NETDEV,				\
16429 		 NL80211_FLAG_NEED_NETDEV)		\
16430 	SELECTOR(__sel, NETDEV_LINK,			\
16431 		 NL80211_FLAG_NEED_NETDEV |		\
16432 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16433 	SELECTOR(__sel, NETDEV_NO_MLO,			\
16434 		 NL80211_FLAG_NEED_NETDEV |		\
16435 		 NL80211_FLAG_MLO_UNSUPPORTED)	\
16436 	SELECTOR(__sel, WIPHY_RTNL,			\
16437 		 NL80211_FLAG_NEED_WIPHY |		\
16438 		 NL80211_FLAG_NEED_RTNL)		\
16439 	SELECTOR(__sel, WIPHY_RTNL_NOMTX,		\
16440 		 NL80211_FLAG_NEED_WIPHY |		\
16441 		 NL80211_FLAG_NEED_RTNL |		\
16442 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16443 	SELECTOR(__sel, WDEV_RTNL,			\
16444 		 NL80211_FLAG_NEED_WDEV |		\
16445 		 NL80211_FLAG_NEED_RTNL)		\
16446 	SELECTOR(__sel, NETDEV_RTNL,			\
16447 		 NL80211_FLAG_NEED_NETDEV |		\
16448 		 NL80211_FLAG_NEED_RTNL)		\
16449 	SELECTOR(__sel, NETDEV_UP,			\
16450 		 NL80211_FLAG_NEED_NETDEV_UP)		\
16451 	SELECTOR(__sel, NETDEV_UP_LINK,			\
16452 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16453 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16454 	SELECTOR(__sel, NETDEV_UP_NO_MLO,		\
16455 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16456 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16457 	SELECTOR(__sel, NETDEV_UP_NO_MLO_CLEAR,		\
16458 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16459 		 NL80211_FLAG_CLEAR_SKB |		\
16460 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16461 	SELECTOR(__sel, NETDEV_UP_NOTMX,		\
16462 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16463 		 NL80211_FLAG_NO_WIPHY_MTX)		\
16464 	SELECTOR(__sel, NETDEV_UP_NOTMX_NOMLO,		\
16465 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16466 		 NL80211_FLAG_NO_WIPHY_MTX |		\
16467 		 NL80211_FLAG_MLO_UNSUPPORTED)		\
16468 	SELECTOR(__sel, NETDEV_UP_CLEAR,		\
16469 		 NL80211_FLAG_NEED_NETDEV_UP |		\
16470 		 NL80211_FLAG_CLEAR_SKB)		\
16471 	SELECTOR(__sel, WDEV_UP,			\
16472 		 NL80211_FLAG_NEED_WDEV_UP)		\
16473 	SELECTOR(__sel, WDEV_UP_LINK,			\
16474 		 NL80211_FLAG_NEED_WDEV_UP |		\
16475 		 NL80211_FLAG_MLO_VALID_LINK_ID)	\
16476 	SELECTOR(__sel, WDEV_UP_RTNL,			\
16477 		 NL80211_FLAG_NEED_WDEV_UP |		\
16478 		 NL80211_FLAG_NEED_RTNL)		\
16479 	SELECTOR(__sel, WIPHY_CLEAR,			\
16480 		 NL80211_FLAG_NEED_WIPHY |		\
16481 		 NL80211_FLAG_CLEAR_SKB)
16482 
16483 enum nl80211_internal_flags_selector {
16484 #define SELECTOR(_, name, value)	NL80211_IFL_SEL_##name,
16485 	INTERNAL_FLAG_SELECTORS(_)
16486 #undef SELECTOR
16487 };
16488 
16489 static u32 nl80211_internal_flags[] = {
16490 #define SELECTOR(_, name, value)	[NL80211_IFL_SEL_##name] = value,
16491 	INTERNAL_FLAG_SELECTORS(_)
16492 #undef SELECTOR
16493 };
16494 
16495 static int nl80211_pre_doit(const struct genl_split_ops *ops,
16496 			    struct sk_buff *skb,
16497 			    struct genl_info *info)
16498 {
16499 	struct cfg80211_registered_device *rdev = NULL;
16500 	struct wireless_dev *wdev = NULL;
16501 	struct net_device *dev = NULL;
16502 	u32 internal_flags;
16503 	int err;
16504 
16505 	if (WARN_ON(ops->internal_flags >= ARRAY_SIZE(nl80211_internal_flags)))
16506 		return -EINVAL;
16507 
16508 	internal_flags = nl80211_internal_flags[ops->internal_flags];
16509 
16510 	rtnl_lock();
16511 	if (internal_flags & NL80211_FLAG_NEED_WIPHY) {
16512 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
16513 		if (IS_ERR(rdev)) {
16514 			err = PTR_ERR(rdev);
16515 			goto out_unlock;
16516 		}
16517 		info->user_ptr[0] = rdev;
16518 	} else if (internal_flags & NL80211_FLAG_NEED_NETDEV ||
16519 		   internal_flags & NL80211_FLAG_NEED_WDEV) {
16520 		wdev = __cfg80211_wdev_from_attrs(NULL, genl_info_net(info),
16521 						  info->attrs);
16522 		if (IS_ERR(wdev)) {
16523 			err = PTR_ERR(wdev);
16524 			goto out_unlock;
16525 		}
16526 
16527 		dev = wdev->netdev;
16528 		dev_hold(dev);
16529 		rdev = wiphy_to_rdev(wdev->wiphy);
16530 
16531 		if (internal_flags & NL80211_FLAG_NEED_NETDEV) {
16532 			if (!dev) {
16533 				err = -EINVAL;
16534 				goto out_unlock;
16535 			}
16536 
16537 			info->user_ptr[1] = dev;
16538 		} else {
16539 			info->user_ptr[1] = wdev;
16540 		}
16541 
16542 		if (internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
16543 		    !wdev_running(wdev)) {
16544 			err = -ENETDOWN;
16545 			goto out_unlock;
16546 		}
16547 
16548 		info->user_ptr[0] = rdev;
16549 	}
16550 
16551 	if (internal_flags & NL80211_FLAG_MLO_VALID_LINK_ID) {
16552 		struct nlattr *link_id = info->attrs[NL80211_ATTR_MLO_LINK_ID];
16553 
16554 		if (!wdev) {
16555 			err = -EINVAL;
16556 			goto out_unlock;
16557 		}
16558 
16559 		/* MLO -> require valid link ID */
16560 		if (wdev->valid_links &&
16561 		    (!link_id ||
16562 		     !(wdev->valid_links & BIT(nla_get_u8(link_id))))) {
16563 			err = -EINVAL;
16564 			goto out_unlock;
16565 		}
16566 
16567 		/* non-MLO -> no link ID attribute accepted */
16568 		if (!wdev->valid_links && link_id) {
16569 			err = -EINVAL;
16570 			goto out_unlock;
16571 		}
16572 	}
16573 
16574 	if (internal_flags & NL80211_FLAG_MLO_UNSUPPORTED) {
16575 		if (info->attrs[NL80211_ATTR_MLO_LINK_ID] ||
16576 		    (wdev && wdev->valid_links)) {
16577 			err = -EINVAL;
16578 			goto out_unlock;
16579 		}
16580 	}
16581 
16582 	if (rdev && !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16583 		wiphy_lock(&rdev->wiphy);
16584 		/* we keep the mutex locked until post_doit */
16585 		__release(&rdev->wiphy.mtx);
16586 	}
16587 	if (!(internal_flags & NL80211_FLAG_NEED_RTNL))
16588 		rtnl_unlock();
16589 
16590 	return 0;
16591 out_unlock:
16592 	rtnl_unlock();
16593 	dev_put(dev);
16594 	return err;
16595 }
16596 
16597 static void nl80211_post_doit(const struct genl_split_ops *ops,
16598 			      struct sk_buff *skb,
16599 			      struct genl_info *info)
16600 {
16601 	u32 internal_flags = nl80211_internal_flags[ops->internal_flags];
16602 
16603 	if (info->user_ptr[1]) {
16604 		if (internal_flags & NL80211_FLAG_NEED_WDEV) {
16605 			struct wireless_dev *wdev = info->user_ptr[1];
16606 
16607 			dev_put(wdev->netdev);
16608 		} else {
16609 			dev_put(info->user_ptr[1]);
16610 		}
16611 	}
16612 
16613 	if (info->user_ptr[0] &&
16614 	    !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16615 		struct cfg80211_registered_device *rdev = info->user_ptr[0];
16616 
16617 		/* we kept the mutex locked since pre_doit */
16618 		__acquire(&rdev->wiphy.mtx);
16619 		wiphy_unlock(&rdev->wiphy);
16620 	}
16621 
16622 	if (internal_flags & NL80211_FLAG_NEED_RTNL)
16623 		rtnl_unlock();
16624 
16625 	/* If needed, clear the netlink message payload from the SKB
16626 	 * as it might contain key data that shouldn't stick around on
16627 	 * the heap after the SKB is freed. The netlink message header
16628 	 * is still needed for further processing, so leave it intact.
16629 	 */
16630 	if (internal_flags & NL80211_FLAG_CLEAR_SKB) {
16631 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
16632 
16633 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
16634 	}
16635 }
16636 
16637 static int nl80211_set_sar_sub_specs(struct cfg80211_registered_device *rdev,
16638 				     struct cfg80211_sar_specs *sar_specs,
16639 				     struct nlattr *spec[], int index)
16640 {
16641 	u32 range_index, i;
16642 
16643 	if (!sar_specs || !spec)
16644 		return -EINVAL;
16645 
16646 	if (!spec[NL80211_SAR_ATTR_SPECS_POWER] ||
16647 	    !spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX])
16648 		return -EINVAL;
16649 
16650 	range_index = nla_get_u32(spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX]);
16651 
16652 	/* check if range_index exceeds num_freq_ranges */
16653 	if (range_index >= rdev->wiphy.sar_capa->num_freq_ranges)
16654 		return -EINVAL;
16655 
16656 	/* check if range_index duplicates */
16657 	for (i = 0; i < index; i++) {
16658 		if (sar_specs->sub_specs[i].freq_range_index == range_index)
16659 			return -EINVAL;
16660 	}
16661 
16662 	sar_specs->sub_specs[index].power =
16663 		nla_get_s32(spec[NL80211_SAR_ATTR_SPECS_POWER]);
16664 
16665 	sar_specs->sub_specs[index].freq_range_index = range_index;
16666 
16667 	return 0;
16668 }
16669 
16670 static int nl80211_set_sar_specs(struct sk_buff *skb, struct genl_info *info)
16671 {
16672 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
16673 	struct nlattr *spec[NL80211_SAR_ATTR_SPECS_MAX + 1];
16674 	struct nlattr *tb[NL80211_SAR_ATTR_MAX + 1];
16675 	struct cfg80211_sar_specs *sar_spec;
16676 	enum nl80211_sar_type type;
16677 	struct nlattr *spec_list;
16678 	u32 specs;
16679 	int rem, err;
16680 
16681 	if (!rdev->wiphy.sar_capa || !rdev->ops->set_sar_specs)
16682 		return -EOPNOTSUPP;
16683 
16684 	if (!info->attrs[NL80211_ATTR_SAR_SPEC])
16685 		return -EINVAL;
16686 
16687 	nla_parse_nested(tb, NL80211_SAR_ATTR_MAX,
16688 			 info->attrs[NL80211_ATTR_SAR_SPEC],
16689 			 NULL, NULL);
16690 
16691 	if (!tb[NL80211_SAR_ATTR_TYPE] || !tb[NL80211_SAR_ATTR_SPECS])
16692 		return -EINVAL;
16693 
16694 	type = nla_get_u32(tb[NL80211_SAR_ATTR_TYPE]);
16695 	if (type != rdev->wiphy.sar_capa->type)
16696 		return -EINVAL;
16697 
16698 	specs = 0;
16699 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem)
16700 		specs++;
16701 
16702 	if (specs > rdev->wiphy.sar_capa->num_freq_ranges)
16703 		return -EINVAL;
16704 
16705 	sar_spec = kzalloc(struct_size(sar_spec, sub_specs, specs), GFP_KERNEL);
16706 	if (!sar_spec)
16707 		return -ENOMEM;
16708 
16709 	sar_spec->type = type;
16710 	specs = 0;
16711 	nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem) {
16712 		nla_parse_nested(spec, NL80211_SAR_ATTR_SPECS_MAX,
16713 				 spec_list, NULL, NULL);
16714 
16715 		switch (type) {
16716 		case NL80211_SAR_TYPE_POWER:
16717 			if (nl80211_set_sar_sub_specs(rdev, sar_spec,
16718 						      spec, specs)) {
16719 				err = -EINVAL;
16720 				goto error;
16721 			}
16722 			break;
16723 		default:
16724 			err = -EINVAL;
16725 			goto error;
16726 		}
16727 		specs++;
16728 	}
16729 
16730 	sar_spec->num_sub_specs = specs;
16731 
16732 	rdev->cur_cmd_info = info;
16733 	err = rdev_set_sar_specs(rdev, sar_spec);
16734 	rdev->cur_cmd_info = NULL;
16735 error:
16736 	kfree(sar_spec);
16737 	return err;
16738 }
16739 
16740 #define SELECTOR(__sel, name, value) \
16741 	((__sel) == (value)) ? NL80211_IFL_SEL_##name :
16742 int __missing_selector(void);
16743 #define IFLAGS(__val) INTERNAL_FLAG_SELECTORS(__val) __missing_selector()
16744 
16745 static const struct genl_ops nl80211_ops[] = {
16746 	{
16747 		.cmd = NL80211_CMD_GET_WIPHY,
16748 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16749 		.doit = nl80211_get_wiphy,
16750 		.dumpit = nl80211_dump_wiphy,
16751 		.done = nl80211_dump_wiphy_done,
16752 		/* can be retrieved by unprivileged users */
16753 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16754 	},
16755 };
16756 
16757 static const struct genl_small_ops nl80211_small_ops[] = {
16758 	{
16759 		.cmd = NL80211_CMD_SET_WIPHY,
16760 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16761 		.doit = nl80211_set_wiphy,
16762 		.flags = GENL_UNS_ADMIN_PERM,
16763 	},
16764 	{
16765 		.cmd = NL80211_CMD_GET_INTERFACE,
16766 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16767 		.doit = nl80211_get_interface,
16768 		.dumpit = nl80211_dump_interface,
16769 		/* can be retrieved by unprivileged users */
16770 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16771 	},
16772 	{
16773 		.cmd = NL80211_CMD_SET_INTERFACE,
16774 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16775 		.doit = nl80211_set_interface,
16776 		.flags = GENL_UNS_ADMIN_PERM,
16777 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16778 					 NL80211_FLAG_NEED_RTNL),
16779 	},
16780 	{
16781 		.cmd = NL80211_CMD_NEW_INTERFACE,
16782 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16783 		.doit = nl80211_new_interface,
16784 		.flags = GENL_UNS_ADMIN_PERM,
16785 		.internal_flags =
16786 			IFLAGS(NL80211_FLAG_NEED_WIPHY |
16787 			       NL80211_FLAG_NEED_RTNL |
16788 			       /* we take the wiphy mutex later ourselves */
16789 			       NL80211_FLAG_NO_WIPHY_MTX),
16790 	},
16791 	{
16792 		.cmd = NL80211_CMD_DEL_INTERFACE,
16793 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16794 		.doit = nl80211_del_interface,
16795 		.flags = GENL_UNS_ADMIN_PERM,
16796 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16797 					 NL80211_FLAG_NEED_RTNL),
16798 	},
16799 	{
16800 		.cmd = NL80211_CMD_GET_KEY,
16801 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16802 		.doit = nl80211_get_key,
16803 		.flags = GENL_UNS_ADMIN_PERM,
16804 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16805 	},
16806 	{
16807 		.cmd = NL80211_CMD_SET_KEY,
16808 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16809 		.doit = nl80211_set_key,
16810 		.flags = GENL_UNS_ADMIN_PERM,
16811 		/* cannot use NL80211_FLAG_MLO_VALID_LINK_ID, depends on key */
16812 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16813 					 NL80211_FLAG_CLEAR_SKB),
16814 	},
16815 	{
16816 		.cmd = NL80211_CMD_NEW_KEY,
16817 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16818 		.doit = nl80211_new_key,
16819 		.flags = GENL_UNS_ADMIN_PERM,
16820 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16821 					 NL80211_FLAG_CLEAR_SKB),
16822 	},
16823 	{
16824 		.cmd = NL80211_CMD_DEL_KEY,
16825 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16826 		.doit = nl80211_del_key,
16827 		.flags = GENL_UNS_ADMIN_PERM,
16828 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16829 	},
16830 	{
16831 		.cmd = NL80211_CMD_SET_BEACON,
16832 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16833 		.flags = GENL_UNS_ADMIN_PERM,
16834 		.doit = nl80211_set_beacon,
16835 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16836 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16837 	},
16838 	{
16839 		.cmd = NL80211_CMD_START_AP,
16840 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16841 		.flags = GENL_UNS_ADMIN_PERM,
16842 		.doit = nl80211_start_ap,
16843 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16844 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16845 	},
16846 	{
16847 		.cmd = NL80211_CMD_STOP_AP,
16848 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16849 		.flags = GENL_UNS_ADMIN_PERM,
16850 		.doit = nl80211_stop_ap,
16851 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16852 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16853 	},
16854 	{
16855 		.cmd = NL80211_CMD_GET_STATION,
16856 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16857 		.doit = nl80211_get_station,
16858 		.dumpit = nl80211_dump_station,
16859 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16860 	},
16861 	{
16862 		.cmd = NL80211_CMD_SET_STATION,
16863 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16864 		.doit = nl80211_set_station,
16865 		.flags = GENL_UNS_ADMIN_PERM,
16866 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16867 	},
16868 	{
16869 		.cmd = NL80211_CMD_NEW_STATION,
16870 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16871 		.doit = nl80211_new_station,
16872 		.flags = GENL_UNS_ADMIN_PERM,
16873 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16874 	},
16875 	{
16876 		.cmd = NL80211_CMD_DEL_STATION,
16877 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16878 		.doit = nl80211_del_station,
16879 		.flags = GENL_UNS_ADMIN_PERM,
16880 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16881 	},
16882 	{
16883 		.cmd = NL80211_CMD_GET_MPATH,
16884 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16885 		.doit = nl80211_get_mpath,
16886 		.dumpit = nl80211_dump_mpath,
16887 		.flags = GENL_UNS_ADMIN_PERM,
16888 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16889 	},
16890 	{
16891 		.cmd = NL80211_CMD_GET_MPP,
16892 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16893 		.doit = nl80211_get_mpp,
16894 		.dumpit = nl80211_dump_mpp,
16895 		.flags = GENL_UNS_ADMIN_PERM,
16896 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16897 	},
16898 	{
16899 		.cmd = NL80211_CMD_SET_MPATH,
16900 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16901 		.doit = nl80211_set_mpath,
16902 		.flags = GENL_UNS_ADMIN_PERM,
16903 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16904 	},
16905 	{
16906 		.cmd = NL80211_CMD_NEW_MPATH,
16907 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16908 		.doit = nl80211_new_mpath,
16909 		.flags = GENL_UNS_ADMIN_PERM,
16910 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16911 	},
16912 	{
16913 		.cmd = NL80211_CMD_DEL_MPATH,
16914 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16915 		.doit = nl80211_del_mpath,
16916 		.flags = GENL_UNS_ADMIN_PERM,
16917 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16918 	},
16919 	{
16920 		.cmd = NL80211_CMD_SET_BSS,
16921 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16922 		.doit = nl80211_set_bss,
16923 		.flags = GENL_UNS_ADMIN_PERM,
16924 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16925 					 NL80211_FLAG_MLO_VALID_LINK_ID),
16926 	},
16927 	{
16928 		.cmd = NL80211_CMD_GET_REG,
16929 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16930 		.doit = nl80211_get_reg_do,
16931 		.dumpit = nl80211_get_reg_dump,
16932 		/* can be retrieved by unprivileged users */
16933 	},
16934 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
16935 	{
16936 		.cmd = NL80211_CMD_SET_REG,
16937 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16938 		.doit = nl80211_set_reg,
16939 		.flags = GENL_ADMIN_PERM,
16940 	},
16941 #endif
16942 	{
16943 		.cmd = NL80211_CMD_REQ_SET_REG,
16944 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16945 		.doit = nl80211_req_set_reg,
16946 		.flags = GENL_ADMIN_PERM,
16947 	},
16948 	{
16949 		.cmd = NL80211_CMD_RELOAD_REGDB,
16950 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16951 		.doit = nl80211_reload_regdb,
16952 		.flags = GENL_ADMIN_PERM,
16953 	},
16954 	{
16955 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
16956 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16957 		.doit = nl80211_get_mesh_config,
16958 		/* can be retrieved by unprivileged users */
16959 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16960 	},
16961 	{
16962 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
16963 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16964 		.doit = nl80211_update_mesh_config,
16965 		.flags = GENL_UNS_ADMIN_PERM,
16966 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16967 	},
16968 	{
16969 		.cmd = NL80211_CMD_TRIGGER_SCAN,
16970 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16971 		.doit = nl80211_trigger_scan,
16972 		.flags = GENL_UNS_ADMIN_PERM,
16973 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16974 	},
16975 	{
16976 		.cmd = NL80211_CMD_ABORT_SCAN,
16977 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16978 		.doit = nl80211_abort_scan,
16979 		.flags = GENL_UNS_ADMIN_PERM,
16980 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16981 	},
16982 	{
16983 		.cmd = NL80211_CMD_GET_SCAN,
16984 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16985 		.dumpit = nl80211_dump_scan,
16986 	},
16987 	{
16988 		.cmd = NL80211_CMD_START_SCHED_SCAN,
16989 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16990 		.doit = nl80211_start_sched_scan,
16991 		.flags = GENL_UNS_ADMIN_PERM,
16992 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16993 	},
16994 	{
16995 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
16996 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16997 		.doit = nl80211_stop_sched_scan,
16998 		.flags = GENL_UNS_ADMIN_PERM,
16999 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17000 	},
17001 	{
17002 		.cmd = NL80211_CMD_AUTHENTICATE,
17003 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17004 		.doit = nl80211_authenticate,
17005 		.flags = GENL_UNS_ADMIN_PERM,
17006 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17007 					 NL80211_FLAG_CLEAR_SKB),
17008 	},
17009 	{
17010 		.cmd = NL80211_CMD_ASSOCIATE,
17011 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17012 		.doit = nl80211_associate,
17013 		.flags = GENL_UNS_ADMIN_PERM,
17014 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17015 					 NL80211_FLAG_CLEAR_SKB),
17016 	},
17017 	{
17018 		.cmd = NL80211_CMD_DEAUTHENTICATE,
17019 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17020 		.doit = nl80211_deauthenticate,
17021 		.flags = GENL_UNS_ADMIN_PERM,
17022 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17023 	},
17024 	{
17025 		.cmd = NL80211_CMD_DISASSOCIATE,
17026 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17027 		.doit = nl80211_disassociate,
17028 		.flags = GENL_UNS_ADMIN_PERM,
17029 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17030 	},
17031 	{
17032 		.cmd = NL80211_CMD_JOIN_IBSS,
17033 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17034 		.doit = nl80211_join_ibss,
17035 		.flags = GENL_UNS_ADMIN_PERM,
17036 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17037 	},
17038 	{
17039 		.cmd = NL80211_CMD_LEAVE_IBSS,
17040 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17041 		.doit = nl80211_leave_ibss,
17042 		.flags = GENL_UNS_ADMIN_PERM,
17043 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17044 	},
17045 #ifdef CONFIG_NL80211_TESTMODE
17046 	{
17047 		.cmd = NL80211_CMD_TESTMODE,
17048 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17049 		.doit = nl80211_testmode_do,
17050 		.dumpit = nl80211_testmode_dump,
17051 		.flags = GENL_UNS_ADMIN_PERM,
17052 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17053 	},
17054 #endif
17055 	{
17056 		.cmd = NL80211_CMD_CONNECT,
17057 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17058 		.doit = nl80211_connect,
17059 		.flags = GENL_UNS_ADMIN_PERM,
17060 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17061 					 NL80211_FLAG_CLEAR_SKB),
17062 	},
17063 	{
17064 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
17065 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17066 		.doit = nl80211_update_connect_params,
17067 		.flags = GENL_ADMIN_PERM,
17068 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17069 					 NL80211_FLAG_CLEAR_SKB),
17070 	},
17071 	{
17072 		.cmd = NL80211_CMD_DISCONNECT,
17073 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17074 		.doit = nl80211_disconnect,
17075 		.flags = GENL_UNS_ADMIN_PERM,
17076 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17077 	},
17078 	{
17079 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
17080 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17081 		.doit = nl80211_wiphy_netns,
17082 		.flags = GENL_UNS_ADMIN_PERM,
17083 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17084 					 NL80211_FLAG_NEED_RTNL |
17085 					 NL80211_FLAG_NO_WIPHY_MTX),
17086 	},
17087 	{
17088 		.cmd = NL80211_CMD_GET_SURVEY,
17089 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17090 		.dumpit = nl80211_dump_survey,
17091 	},
17092 	{
17093 		.cmd = NL80211_CMD_SET_PMKSA,
17094 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17095 		.doit = nl80211_setdel_pmksa,
17096 		.flags = GENL_UNS_ADMIN_PERM,
17097 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17098 					 NL80211_FLAG_CLEAR_SKB),
17099 	},
17100 	{
17101 		.cmd = NL80211_CMD_DEL_PMKSA,
17102 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17103 		.doit = nl80211_setdel_pmksa,
17104 		.flags = GENL_UNS_ADMIN_PERM,
17105 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17106 	},
17107 	{
17108 		.cmd = NL80211_CMD_FLUSH_PMKSA,
17109 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17110 		.doit = nl80211_flush_pmksa,
17111 		.flags = GENL_UNS_ADMIN_PERM,
17112 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17113 	},
17114 	{
17115 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
17116 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17117 		.doit = nl80211_remain_on_channel,
17118 		.flags = GENL_UNS_ADMIN_PERM,
17119 		/* FIXME: requiring a link ID here is probably not good */
17120 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17121 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17122 	},
17123 	{
17124 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
17125 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17126 		.doit = nl80211_cancel_remain_on_channel,
17127 		.flags = GENL_UNS_ADMIN_PERM,
17128 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17129 	},
17130 	{
17131 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
17132 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17133 		.doit = nl80211_set_tx_bitrate_mask,
17134 		.flags = GENL_UNS_ADMIN_PERM,
17135 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17136 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17137 	},
17138 	{
17139 		.cmd = NL80211_CMD_REGISTER_FRAME,
17140 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17141 		.doit = nl80211_register_mgmt,
17142 		.flags = GENL_UNS_ADMIN_PERM,
17143 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
17144 	},
17145 	{
17146 		.cmd = NL80211_CMD_FRAME,
17147 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17148 		.doit = nl80211_tx_mgmt,
17149 		.flags = GENL_UNS_ADMIN_PERM,
17150 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17151 	},
17152 	{
17153 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
17154 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17155 		.doit = nl80211_tx_mgmt_cancel_wait,
17156 		.flags = GENL_UNS_ADMIN_PERM,
17157 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17158 	},
17159 	{
17160 		.cmd = NL80211_CMD_SET_POWER_SAVE,
17161 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17162 		.doit = nl80211_set_power_save,
17163 		.flags = GENL_UNS_ADMIN_PERM,
17164 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17165 	},
17166 	{
17167 		.cmd = NL80211_CMD_GET_POWER_SAVE,
17168 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17169 		.doit = nl80211_get_power_save,
17170 		/* can be retrieved by unprivileged users */
17171 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17172 	},
17173 	{
17174 		.cmd = NL80211_CMD_SET_CQM,
17175 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17176 		.doit = nl80211_set_cqm,
17177 		.flags = GENL_UNS_ADMIN_PERM,
17178 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17179 	},
17180 	{
17181 		.cmd = NL80211_CMD_SET_CHANNEL,
17182 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17183 		.doit = nl80211_set_channel,
17184 		.flags = GENL_UNS_ADMIN_PERM,
17185 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17186 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17187 	},
17188 	{
17189 		.cmd = NL80211_CMD_JOIN_MESH,
17190 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17191 		.doit = nl80211_join_mesh,
17192 		.flags = GENL_UNS_ADMIN_PERM,
17193 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17194 	},
17195 	{
17196 		.cmd = NL80211_CMD_LEAVE_MESH,
17197 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17198 		.doit = nl80211_leave_mesh,
17199 		.flags = GENL_UNS_ADMIN_PERM,
17200 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17201 	},
17202 	{
17203 		.cmd = NL80211_CMD_JOIN_OCB,
17204 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17205 		.doit = nl80211_join_ocb,
17206 		.flags = GENL_UNS_ADMIN_PERM,
17207 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17208 	},
17209 	{
17210 		.cmd = NL80211_CMD_LEAVE_OCB,
17211 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17212 		.doit = nl80211_leave_ocb,
17213 		.flags = GENL_UNS_ADMIN_PERM,
17214 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17215 	},
17216 #ifdef CONFIG_PM
17217 	{
17218 		.cmd = NL80211_CMD_GET_WOWLAN,
17219 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17220 		.doit = nl80211_get_wowlan,
17221 		/* can be retrieved by unprivileged users */
17222 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17223 	},
17224 	{
17225 		.cmd = NL80211_CMD_SET_WOWLAN,
17226 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17227 		.doit = nl80211_set_wowlan,
17228 		.flags = GENL_UNS_ADMIN_PERM,
17229 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17230 	},
17231 #endif
17232 	{
17233 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
17234 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17235 		.doit = nl80211_set_rekey_data,
17236 		.flags = GENL_UNS_ADMIN_PERM,
17237 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17238 					 NL80211_FLAG_CLEAR_SKB),
17239 	},
17240 	{
17241 		.cmd = NL80211_CMD_TDLS_MGMT,
17242 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17243 		.doit = nl80211_tdls_mgmt,
17244 		.flags = GENL_UNS_ADMIN_PERM,
17245 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17246 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17247 	},
17248 	{
17249 		.cmd = NL80211_CMD_TDLS_OPER,
17250 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17251 		.doit = nl80211_tdls_oper,
17252 		.flags = GENL_UNS_ADMIN_PERM,
17253 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17254 	},
17255 	{
17256 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
17257 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17258 		.doit = nl80211_register_unexpected_frame,
17259 		.flags = GENL_UNS_ADMIN_PERM,
17260 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17261 	},
17262 	{
17263 		.cmd = NL80211_CMD_PROBE_CLIENT,
17264 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17265 		.doit = nl80211_probe_client,
17266 		.flags = GENL_UNS_ADMIN_PERM,
17267 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17268 	},
17269 	{
17270 		.cmd = NL80211_CMD_REGISTER_BEACONS,
17271 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17272 		.doit = nl80211_register_beacons,
17273 		.flags = GENL_UNS_ADMIN_PERM,
17274 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17275 	},
17276 	{
17277 		.cmd = NL80211_CMD_SET_NOACK_MAP,
17278 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17279 		.doit = nl80211_set_noack_map,
17280 		.flags = GENL_UNS_ADMIN_PERM,
17281 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17282 	},
17283 	{
17284 		.cmd = NL80211_CMD_START_P2P_DEVICE,
17285 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17286 		.doit = nl80211_start_p2p_device,
17287 		.flags = GENL_UNS_ADMIN_PERM,
17288 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17289 					 NL80211_FLAG_NEED_RTNL),
17290 	},
17291 	{
17292 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
17293 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17294 		.doit = nl80211_stop_p2p_device,
17295 		.flags = GENL_UNS_ADMIN_PERM,
17296 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17297 					 NL80211_FLAG_NEED_RTNL),
17298 	},
17299 	{
17300 		.cmd = NL80211_CMD_START_NAN,
17301 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17302 		.doit = nl80211_start_nan,
17303 		.flags = GENL_ADMIN_PERM,
17304 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
17305 					 NL80211_FLAG_NEED_RTNL),
17306 	},
17307 	{
17308 		.cmd = NL80211_CMD_STOP_NAN,
17309 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17310 		.doit = nl80211_stop_nan,
17311 		.flags = GENL_ADMIN_PERM,
17312 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
17313 					 NL80211_FLAG_NEED_RTNL),
17314 	},
17315 	{
17316 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
17317 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17318 		.doit = nl80211_nan_add_func,
17319 		.flags = GENL_ADMIN_PERM,
17320 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17321 	},
17322 	{
17323 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
17324 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17325 		.doit = nl80211_nan_del_func,
17326 		.flags = GENL_ADMIN_PERM,
17327 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17328 	},
17329 	{
17330 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
17331 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17332 		.doit = nl80211_nan_change_config,
17333 		.flags = GENL_ADMIN_PERM,
17334 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17335 	},
17336 	{
17337 		.cmd = NL80211_CMD_SET_MCAST_RATE,
17338 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17339 		.doit = nl80211_set_mcast_rate,
17340 		.flags = GENL_UNS_ADMIN_PERM,
17341 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17342 	},
17343 	{
17344 		.cmd = NL80211_CMD_SET_MAC_ACL,
17345 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17346 		.doit = nl80211_set_mac_acl,
17347 		.flags = GENL_UNS_ADMIN_PERM,
17348 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17349 					 NL80211_FLAG_MLO_UNSUPPORTED),
17350 	},
17351 	{
17352 		.cmd = NL80211_CMD_RADAR_DETECT,
17353 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17354 		.doit = nl80211_start_radar_detection,
17355 		.flags = GENL_UNS_ADMIN_PERM,
17356 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17357 					 NL80211_FLAG_NO_WIPHY_MTX |
17358 					 NL80211_FLAG_MLO_UNSUPPORTED),
17359 	},
17360 	{
17361 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
17362 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17363 		.doit = nl80211_get_protocol_features,
17364 	},
17365 	{
17366 		.cmd = NL80211_CMD_UPDATE_FT_IES,
17367 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17368 		.doit = nl80211_update_ft_ies,
17369 		.flags = GENL_UNS_ADMIN_PERM,
17370 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17371 	},
17372 	{
17373 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
17374 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17375 		.doit = nl80211_crit_protocol_start,
17376 		.flags = GENL_UNS_ADMIN_PERM,
17377 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17378 	},
17379 	{
17380 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
17381 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17382 		.doit = nl80211_crit_protocol_stop,
17383 		.flags = GENL_UNS_ADMIN_PERM,
17384 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17385 	},
17386 	{
17387 		.cmd = NL80211_CMD_GET_COALESCE,
17388 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17389 		.doit = nl80211_get_coalesce,
17390 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17391 	},
17392 	{
17393 		.cmd = NL80211_CMD_SET_COALESCE,
17394 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17395 		.doit = nl80211_set_coalesce,
17396 		.flags = GENL_UNS_ADMIN_PERM,
17397 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17398 	},
17399 	{
17400 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
17401 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17402 		.doit = nl80211_channel_switch,
17403 		.flags = GENL_UNS_ADMIN_PERM,
17404 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17405 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17406 	},
17407 	{
17408 		.cmd = NL80211_CMD_VENDOR,
17409 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17410 		.doit = nl80211_vendor_cmd,
17411 		.dumpit = nl80211_vendor_cmd_dump,
17412 		.flags = GENL_UNS_ADMIN_PERM,
17413 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17414 					 NL80211_FLAG_CLEAR_SKB),
17415 	},
17416 	{
17417 		.cmd = NL80211_CMD_SET_QOS_MAP,
17418 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17419 		.doit = nl80211_set_qos_map,
17420 		.flags = GENL_UNS_ADMIN_PERM,
17421 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17422 	},
17423 	{
17424 		.cmd = NL80211_CMD_ADD_TX_TS,
17425 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17426 		.doit = nl80211_add_tx_ts,
17427 		.flags = GENL_UNS_ADMIN_PERM,
17428 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17429 					 NL80211_FLAG_MLO_UNSUPPORTED),
17430 	},
17431 	{
17432 		.cmd = NL80211_CMD_DEL_TX_TS,
17433 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17434 		.doit = nl80211_del_tx_ts,
17435 		.flags = GENL_UNS_ADMIN_PERM,
17436 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17437 	},
17438 	{
17439 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
17440 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17441 		.doit = nl80211_tdls_channel_switch,
17442 		.flags = GENL_UNS_ADMIN_PERM,
17443 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17444 	},
17445 	{
17446 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
17447 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17448 		.doit = nl80211_tdls_cancel_channel_switch,
17449 		.flags = GENL_UNS_ADMIN_PERM,
17450 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17451 	},
17452 	{
17453 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
17454 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17455 		.doit = nl80211_set_multicast_to_unicast,
17456 		.flags = GENL_UNS_ADMIN_PERM,
17457 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17458 	},
17459 	{
17460 		.cmd = NL80211_CMD_SET_PMK,
17461 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17462 		.doit = nl80211_set_pmk,
17463 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17464 					 NL80211_FLAG_CLEAR_SKB),
17465 	},
17466 	{
17467 		.cmd = NL80211_CMD_DEL_PMK,
17468 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17469 		.doit = nl80211_del_pmk,
17470 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17471 	},
17472 	{
17473 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
17474 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17475 		.doit = nl80211_external_auth,
17476 		.flags = GENL_ADMIN_PERM,
17477 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17478 	},
17479 	{
17480 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
17481 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17482 		.doit = nl80211_tx_control_port,
17483 		.flags = GENL_UNS_ADMIN_PERM,
17484 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17485 	},
17486 	{
17487 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
17488 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17489 		.doit = nl80211_get_ftm_responder_stats,
17490 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17491 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17492 	},
17493 	{
17494 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
17495 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17496 		.doit = nl80211_pmsr_start,
17497 		.flags = GENL_UNS_ADMIN_PERM,
17498 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17499 	},
17500 	{
17501 		.cmd = NL80211_CMD_NOTIFY_RADAR,
17502 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17503 		.doit = nl80211_notify_radar_detection,
17504 		.flags = GENL_UNS_ADMIN_PERM,
17505 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17506 	},
17507 	{
17508 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
17509 		.doit = nl80211_update_owe_info,
17510 		.flags = GENL_ADMIN_PERM,
17511 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17512 	},
17513 	{
17514 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
17515 		.doit = nl80211_probe_mesh_link,
17516 		.flags = GENL_UNS_ADMIN_PERM,
17517 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17518 	},
17519 	{
17520 		.cmd = NL80211_CMD_SET_TID_CONFIG,
17521 		.doit = nl80211_set_tid_config,
17522 		.flags = GENL_UNS_ADMIN_PERM,
17523 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17524 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17525 	},
17526 	{
17527 		.cmd = NL80211_CMD_SET_SAR_SPECS,
17528 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17529 		.doit = nl80211_set_sar_specs,
17530 		.flags = GENL_UNS_ADMIN_PERM,
17531 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17532 					 NL80211_FLAG_NEED_RTNL),
17533 	},
17534 	{
17535 		.cmd = NL80211_CMD_COLOR_CHANGE_REQUEST,
17536 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17537 		.doit = nl80211_color_change,
17538 		.flags = GENL_UNS_ADMIN_PERM,
17539 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17540 	},
17541 	{
17542 		.cmd = NL80211_CMD_SET_FILS_AAD,
17543 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17544 		.doit = nl80211_set_fils_aad,
17545 		.flags = GENL_UNS_ADMIN_PERM,
17546 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17547 	},
17548 	{
17549 		.cmd = NL80211_CMD_ADD_LINK,
17550 		.doit = nl80211_add_link,
17551 		.flags = GENL_UNS_ADMIN_PERM,
17552 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17553 	},
17554 	{
17555 		.cmd = NL80211_CMD_REMOVE_LINK,
17556 		.doit = nl80211_remove_link,
17557 		.flags = GENL_UNS_ADMIN_PERM,
17558 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17559 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17560 	},
17561 	{
17562 		.cmd = NL80211_CMD_ADD_LINK_STA,
17563 		.doit = nl80211_add_link_station,
17564 		.flags = GENL_UNS_ADMIN_PERM,
17565 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17566 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17567 	},
17568 	{
17569 		.cmd = NL80211_CMD_MODIFY_LINK_STA,
17570 		.doit = nl80211_modify_link_station,
17571 		.flags = GENL_UNS_ADMIN_PERM,
17572 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17573 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17574 	},
17575 	{
17576 		.cmd = NL80211_CMD_REMOVE_LINK_STA,
17577 		.doit = nl80211_remove_link_station,
17578 		.flags = GENL_UNS_ADMIN_PERM,
17579 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17580 					 NL80211_FLAG_MLO_VALID_LINK_ID),
17581 	},
17582 	{
17583 		.cmd = NL80211_CMD_SET_HW_TIMESTAMP,
17584 		.doit = nl80211_set_hw_timestamp,
17585 		.flags = GENL_UNS_ADMIN_PERM,
17586 		.internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17587 	},
17588 };
17589 
17590 static struct genl_family nl80211_fam __ro_after_init = {
17591 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
17592 	.hdrsize = 0,			/* no private header */
17593 	.version = 1,			/* no particular meaning now */
17594 	.maxattr = NL80211_ATTR_MAX,
17595 	.policy = nl80211_policy,
17596 	.netnsok = true,
17597 	.pre_doit = nl80211_pre_doit,
17598 	.post_doit = nl80211_post_doit,
17599 	.module = THIS_MODULE,
17600 	.ops = nl80211_ops,
17601 	.n_ops = ARRAY_SIZE(nl80211_ops),
17602 	.small_ops = nl80211_small_ops,
17603 	.n_small_ops = ARRAY_SIZE(nl80211_small_ops),
17604 	.resv_start_op = NL80211_CMD_REMOVE_LINK_STA + 1,
17605 	.mcgrps = nl80211_mcgrps,
17606 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
17607 	.parallel_ops = true,
17608 };
17609 
17610 /* notification functions */
17611 
17612 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
17613 			  enum nl80211_commands cmd)
17614 {
17615 	struct sk_buff *msg;
17616 	struct nl80211_dump_wiphy_state state = {};
17617 
17618 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
17619 		cmd != NL80211_CMD_DEL_WIPHY);
17620 
17621 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17622 	if (!msg)
17623 		return;
17624 
17625 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
17626 		nlmsg_free(msg);
17627 		return;
17628 	}
17629 
17630 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17631 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17632 }
17633 
17634 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
17635 				struct wireless_dev *wdev,
17636 				enum nl80211_commands cmd)
17637 {
17638 	struct sk_buff *msg;
17639 
17640 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17641 	if (!msg)
17642 		return;
17643 
17644 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
17645 		nlmsg_free(msg);
17646 		return;
17647 	}
17648 
17649 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17650 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
17651 }
17652 
17653 static int nl80211_add_scan_req(struct sk_buff *msg,
17654 				struct cfg80211_registered_device *rdev)
17655 {
17656 	struct cfg80211_scan_request *req = rdev->scan_req;
17657 	struct nlattr *nest;
17658 	int i;
17659 	struct cfg80211_scan_info *info;
17660 
17661 	if (WARN_ON(!req))
17662 		return 0;
17663 
17664 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
17665 	if (!nest)
17666 		goto nla_put_failure;
17667 	for (i = 0; i < req->n_ssids; i++) {
17668 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
17669 			goto nla_put_failure;
17670 	}
17671 	nla_nest_end(msg, nest);
17672 
17673 	if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
17674 		nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
17675 		if (!nest)
17676 			goto nla_put_failure;
17677 		for (i = 0; i < req->n_channels; i++) {
17678 			if (nla_put_u32(msg, i,
17679 				   ieee80211_channel_to_khz(req->channels[i])))
17680 				goto nla_put_failure;
17681 		}
17682 		nla_nest_end(msg, nest);
17683 	} else {
17684 		nest = nla_nest_start_noflag(msg,
17685 					     NL80211_ATTR_SCAN_FREQUENCIES);
17686 		if (!nest)
17687 			goto nla_put_failure;
17688 		for (i = 0; i < req->n_channels; i++) {
17689 			if (nla_put_u32(msg, i, req->channels[i]->center_freq))
17690 				goto nla_put_failure;
17691 		}
17692 		nla_nest_end(msg, nest);
17693 	}
17694 
17695 	if (req->ie &&
17696 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
17697 		goto nla_put_failure;
17698 
17699 	if (req->flags &&
17700 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
17701 		goto nla_put_failure;
17702 
17703 	info = rdev->int_scan_req ? &rdev->int_scan_req->info :
17704 		&rdev->scan_req->info;
17705 	if (info->scan_start_tsf &&
17706 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
17707 			       info->scan_start_tsf, NL80211_BSS_PAD) ||
17708 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
17709 		     info->tsf_bssid)))
17710 		goto nla_put_failure;
17711 
17712 	return 0;
17713  nla_put_failure:
17714 	return -ENOBUFS;
17715 }
17716 
17717 static int nl80211_prep_scan_msg(struct sk_buff *msg,
17718 				 struct cfg80211_registered_device *rdev,
17719 				 struct wireless_dev *wdev,
17720 				 u32 portid, u32 seq, int flags,
17721 				 u32 cmd)
17722 {
17723 	void *hdr;
17724 
17725 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
17726 	if (!hdr)
17727 		return -1;
17728 
17729 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17730 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17731 					 wdev->netdev->ifindex)) ||
17732 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17733 			      NL80211_ATTR_PAD))
17734 		goto nla_put_failure;
17735 
17736 	/* ignore errors and send incomplete event anyway */
17737 	nl80211_add_scan_req(msg, rdev);
17738 
17739 	genlmsg_end(msg, hdr);
17740 	return 0;
17741 
17742  nla_put_failure:
17743 	genlmsg_cancel(msg, hdr);
17744 	return -EMSGSIZE;
17745 }
17746 
17747 static int
17748 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
17749 			    struct cfg80211_sched_scan_request *req, u32 cmd)
17750 {
17751 	void *hdr;
17752 
17753 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17754 	if (!hdr)
17755 		return -1;
17756 
17757 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
17758 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
17759 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
17760 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
17761 			      NL80211_ATTR_PAD))
17762 		goto nla_put_failure;
17763 
17764 	genlmsg_end(msg, hdr);
17765 	return 0;
17766 
17767  nla_put_failure:
17768 	genlmsg_cancel(msg, hdr);
17769 	return -EMSGSIZE;
17770 }
17771 
17772 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
17773 			     struct wireless_dev *wdev)
17774 {
17775 	struct sk_buff *msg;
17776 
17777 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17778 	if (!msg)
17779 		return;
17780 
17781 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17782 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
17783 		nlmsg_free(msg);
17784 		return;
17785 	}
17786 
17787 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17788 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17789 }
17790 
17791 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
17792 				       struct wireless_dev *wdev, bool aborted)
17793 {
17794 	struct sk_buff *msg;
17795 
17796 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17797 	if (!msg)
17798 		return NULL;
17799 
17800 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17801 				  aborted ? NL80211_CMD_SCAN_ABORTED :
17802 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
17803 		nlmsg_free(msg);
17804 		return NULL;
17805 	}
17806 
17807 	return msg;
17808 }
17809 
17810 /* send message created by nl80211_build_scan_msg() */
17811 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
17812 			   struct sk_buff *msg)
17813 {
17814 	if (!msg)
17815 		return;
17816 
17817 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17818 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17819 }
17820 
17821 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
17822 {
17823 	struct sk_buff *msg;
17824 
17825 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17826 	if (!msg)
17827 		return;
17828 
17829 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
17830 		nlmsg_free(msg);
17831 		return;
17832 	}
17833 
17834 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
17835 				NL80211_MCGRP_SCAN, GFP_KERNEL);
17836 }
17837 
17838 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
17839 					  struct regulatory_request *request)
17840 {
17841 	/* Userspace can always count this one always being set */
17842 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
17843 		goto nla_put_failure;
17844 
17845 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
17846 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17847 			       NL80211_REGDOM_TYPE_WORLD))
17848 			goto nla_put_failure;
17849 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
17850 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17851 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
17852 			goto nla_put_failure;
17853 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
17854 		   request->intersect) {
17855 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17856 			       NL80211_REGDOM_TYPE_INTERSECTION))
17857 			goto nla_put_failure;
17858 	} else {
17859 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17860 			       NL80211_REGDOM_TYPE_COUNTRY) ||
17861 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
17862 				   request->alpha2))
17863 			goto nla_put_failure;
17864 	}
17865 
17866 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
17867 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
17868 
17869 		if (wiphy &&
17870 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
17871 			goto nla_put_failure;
17872 
17873 		if (wiphy &&
17874 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
17875 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
17876 			goto nla_put_failure;
17877 	}
17878 
17879 	return true;
17880 
17881 nla_put_failure:
17882 	return false;
17883 }
17884 
17885 /*
17886  * This can happen on global regulatory changes or device specific settings
17887  * based on custom regulatory domains.
17888  */
17889 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
17890 				     struct regulatory_request *request)
17891 {
17892 	struct sk_buff *msg;
17893 	void *hdr;
17894 
17895 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17896 	if (!msg)
17897 		return;
17898 
17899 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
17900 	if (!hdr)
17901 		goto nla_put_failure;
17902 
17903 	if (!nl80211_reg_change_event_fill(msg, request))
17904 		goto nla_put_failure;
17905 
17906 	genlmsg_end(msg, hdr);
17907 
17908 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
17909 				NL80211_MCGRP_REGULATORY);
17910 
17911 	return;
17912 
17913 nla_put_failure:
17914 	nlmsg_free(msg);
17915 }
17916 
17917 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
17918 				    struct net_device *netdev,
17919 				    const u8 *buf, size_t len,
17920 				    enum nl80211_commands cmd, gfp_t gfp,
17921 				    int uapsd_queues, const u8 *req_ies,
17922 				    size_t req_ies_len, bool reconnect)
17923 {
17924 	struct sk_buff *msg;
17925 	void *hdr;
17926 
17927 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
17928 	if (!msg)
17929 		return;
17930 
17931 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17932 	if (!hdr) {
17933 		nlmsg_free(msg);
17934 		return;
17935 	}
17936 
17937 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17938 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17939 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
17940 	    (req_ies &&
17941 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
17942 		goto nla_put_failure;
17943 
17944 	if (reconnect && nla_put_flag(msg, NL80211_ATTR_RECONNECT_REQUESTED))
17945 		goto nla_put_failure;
17946 
17947 	if (uapsd_queues >= 0) {
17948 		struct nlattr *nla_wmm =
17949 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
17950 		if (!nla_wmm)
17951 			goto nla_put_failure;
17952 
17953 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
17954 			       uapsd_queues))
17955 			goto nla_put_failure;
17956 
17957 		nla_nest_end(msg, nla_wmm);
17958 	}
17959 
17960 	genlmsg_end(msg, hdr);
17961 
17962 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17963 				NL80211_MCGRP_MLME, gfp);
17964 	return;
17965 
17966  nla_put_failure:
17967 	nlmsg_free(msg);
17968 }
17969 
17970 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
17971 			  struct net_device *netdev, const u8 *buf,
17972 			  size_t len, gfp_t gfp)
17973 {
17974 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17975 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0,
17976 				false);
17977 }
17978 
17979 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
17980 			   struct net_device *netdev,
17981 			   struct cfg80211_rx_assoc_resp *data)
17982 {
17983 	nl80211_send_mlme_event(rdev, netdev, data->buf, data->len,
17984 				NL80211_CMD_ASSOCIATE, GFP_KERNEL,
17985 				data->uapsd_queues,
17986 				data->req_ies, data->req_ies_len, false);
17987 }
17988 
17989 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
17990 			 struct net_device *netdev, const u8 *buf,
17991 			 size_t len, bool reconnect, gfp_t gfp)
17992 {
17993 	nl80211_send_mlme_event(rdev, netdev, buf, len,
17994 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0,
17995 				reconnect);
17996 }
17997 
17998 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
17999 			   struct net_device *netdev, const u8 *buf,
18000 			   size_t len, bool reconnect, gfp_t gfp)
18001 {
18002 	nl80211_send_mlme_event(rdev, netdev, buf, len,
18003 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0,
18004 				reconnect);
18005 }
18006 
18007 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
18008 				  size_t len)
18009 {
18010 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18011 	struct wiphy *wiphy = wdev->wiphy;
18012 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18013 	const struct ieee80211_mgmt *mgmt = (void *)buf;
18014 	u32 cmd;
18015 
18016 	if (WARN_ON(len < 2))
18017 		return;
18018 
18019 	if (ieee80211_is_deauth(mgmt->frame_control)) {
18020 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
18021 	} else if (ieee80211_is_disassoc(mgmt->frame_control)) {
18022 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
18023 	} else if (ieee80211_is_beacon(mgmt->frame_control)) {
18024 		if (wdev->unprot_beacon_reported &&
18025 		    elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
18026 			return;
18027 		cmd = NL80211_CMD_UNPROT_BEACON;
18028 		wdev->unprot_beacon_reported = jiffies;
18029 	} else {
18030 		return;
18031 	}
18032 
18033 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
18034 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
18035 				NULL, 0, false);
18036 }
18037 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
18038 
18039 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
18040 				      struct net_device *netdev, int cmd,
18041 				      const u8 *addr, gfp_t gfp)
18042 {
18043 	struct sk_buff *msg;
18044 	void *hdr;
18045 
18046 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18047 	if (!msg)
18048 		return;
18049 
18050 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18051 	if (!hdr) {
18052 		nlmsg_free(msg);
18053 		return;
18054 	}
18055 
18056 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18057 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18058 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
18059 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18060 		goto nla_put_failure;
18061 
18062 	genlmsg_end(msg, hdr);
18063 
18064 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18065 				NL80211_MCGRP_MLME, gfp);
18066 	return;
18067 
18068  nla_put_failure:
18069 	nlmsg_free(msg);
18070 }
18071 
18072 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
18073 			       struct net_device *netdev, const u8 *addr,
18074 			       gfp_t gfp)
18075 {
18076 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
18077 				  addr, gfp);
18078 }
18079 
18080 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
18081 				struct net_device *netdev, const u8 *addr,
18082 				gfp_t gfp)
18083 {
18084 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
18085 				  addr, gfp);
18086 }
18087 
18088 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
18089 				 struct net_device *netdev,
18090 				 struct cfg80211_connect_resp_params *cr,
18091 				 gfp_t gfp)
18092 {
18093 	struct sk_buff *msg;
18094 	void *hdr;
18095 	unsigned int link;
18096 	size_t link_info_size = 0;
18097 	const u8 *connected_addr = cr->valid_links ?
18098 				   cr->ap_mld_addr : cr->links[0].bssid;
18099 
18100 	if (cr->valid_links) {
18101 		for_each_valid_link(cr, link) {
18102 			/* Nested attribute header */
18103 			link_info_size += NLA_HDRLEN;
18104 			/* Link ID */
18105 			link_info_size += nla_total_size(sizeof(u8));
18106 			link_info_size += cr->links[link].addr ?
18107 					  nla_total_size(ETH_ALEN) : 0;
18108 			link_info_size += (cr->links[link].bssid ||
18109 					   cr->links[link].bss) ?
18110 					  nla_total_size(ETH_ALEN) : 0;
18111 			link_info_size += nla_total_size(sizeof(u16));
18112 		}
18113 	}
18114 
18115 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
18116 			cr->fils.kek_len + cr->fils.pmk_len +
18117 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
18118 			gfp);
18119 	if (!msg)
18120 		return;
18121 
18122 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
18123 	if (!hdr) {
18124 		nlmsg_free(msg);
18125 		return;
18126 	}
18127 
18128 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18129 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18130 	    (connected_addr &&
18131 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr)) ||
18132 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
18133 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
18134 			cr->status) ||
18135 	    (cr->status < 0 &&
18136 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
18137 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
18138 			  cr->timeout_reason))) ||
18139 	    (cr->req_ie &&
18140 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
18141 	    (cr->resp_ie &&
18142 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
18143 		     cr->resp_ie)) ||
18144 	    (cr->fils.update_erp_next_seq_num &&
18145 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
18146 			 cr->fils.erp_next_seq_num)) ||
18147 	    (cr->status == WLAN_STATUS_SUCCESS &&
18148 	     ((cr->fils.kek &&
18149 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
18150 		       cr->fils.kek)) ||
18151 	      (cr->fils.pmk &&
18152 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
18153 	      (cr->fils.pmkid &&
18154 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
18155 		goto nla_put_failure;
18156 
18157 	if (cr->valid_links) {
18158 		int i = 1;
18159 		struct nlattr *nested;
18160 
18161 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18162 		if (!nested)
18163 			goto nla_put_failure;
18164 
18165 		for_each_valid_link(cr, link) {
18166 			struct nlattr *nested_mlo_links;
18167 			const u8 *bssid = cr->links[link].bss ?
18168 					  cr->links[link].bss->bssid :
18169 					  cr->links[link].bssid;
18170 
18171 			nested_mlo_links = nla_nest_start(msg, i);
18172 			if (!nested_mlo_links)
18173 				goto nla_put_failure;
18174 
18175 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
18176 			    (bssid &&
18177 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
18178 			    (cr->links[link].addr &&
18179 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18180 				     cr->links[link].addr)) ||
18181 			    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
18182 					cr->links[link].status))
18183 				goto nla_put_failure;
18184 
18185 			nla_nest_end(msg, nested_mlo_links);
18186 			i++;
18187 		}
18188 		nla_nest_end(msg, nested);
18189 	}
18190 
18191 	genlmsg_end(msg, hdr);
18192 
18193 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18194 				NL80211_MCGRP_MLME, gfp);
18195 	return;
18196 
18197  nla_put_failure:
18198 	nlmsg_free(msg);
18199 }
18200 
18201 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
18202 			 struct net_device *netdev,
18203 			 struct cfg80211_roam_info *info, gfp_t gfp)
18204 {
18205 	struct sk_buff *msg;
18206 	void *hdr;
18207 	size_t link_info_size = 0;
18208 	unsigned int link;
18209 	const u8 *connected_addr = info->ap_mld_addr ?
18210 				   info->ap_mld_addr :
18211 				   (info->links[0].bss ?
18212 				    info->links[0].bss->bssid :
18213 				    info->links[0].bssid);
18214 
18215 	if (info->valid_links) {
18216 		for_each_valid_link(info, link) {
18217 			/* Nested attribute header */
18218 			link_info_size += NLA_HDRLEN;
18219 			/* Link ID */
18220 			link_info_size += nla_total_size(sizeof(u8));
18221 			link_info_size += info->links[link].addr ?
18222 					  nla_total_size(ETH_ALEN) : 0;
18223 			link_info_size += (info->links[link].bssid ||
18224 					   info->links[link].bss) ?
18225 					  nla_total_size(ETH_ALEN) : 0;
18226 		}
18227 	}
18228 
18229 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
18230 			info->fils.kek_len + info->fils.pmk_len +
18231 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
18232 			link_info_size, gfp);
18233 	if (!msg)
18234 		return;
18235 
18236 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
18237 	if (!hdr) {
18238 		nlmsg_free(msg);
18239 		return;
18240 	}
18241 
18242 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18243 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18244 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
18245 	    (info->req_ie &&
18246 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
18247 		     info->req_ie)) ||
18248 	    (info->resp_ie &&
18249 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
18250 		     info->resp_ie)) ||
18251 	    (info->fils.update_erp_next_seq_num &&
18252 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
18253 			 info->fils.erp_next_seq_num)) ||
18254 	    (info->fils.kek &&
18255 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
18256 		     info->fils.kek)) ||
18257 	    (info->fils.pmk &&
18258 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
18259 	    (info->fils.pmkid &&
18260 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
18261 		goto nla_put_failure;
18262 
18263 	if (info->valid_links) {
18264 		int i = 1;
18265 		struct nlattr *nested;
18266 
18267 		nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18268 		if (!nested)
18269 			goto nla_put_failure;
18270 
18271 		for_each_valid_link(info, link) {
18272 			struct nlattr *nested_mlo_links;
18273 			const u8 *bssid = info->links[link].bss ?
18274 					  info->links[link].bss->bssid :
18275 					  info->links[link].bssid;
18276 
18277 			nested_mlo_links = nla_nest_start(msg, i);
18278 			if (!nested_mlo_links)
18279 				goto nla_put_failure;
18280 
18281 			if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
18282 			    (bssid &&
18283 			     nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
18284 			    (info->links[link].addr &&
18285 			     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
18286 				     info->links[link].addr)))
18287 				goto nla_put_failure;
18288 
18289 			nla_nest_end(msg, nested_mlo_links);
18290 			i++;
18291 		}
18292 		nla_nest_end(msg, nested);
18293 	}
18294 
18295 	genlmsg_end(msg, hdr);
18296 
18297 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18298 				NL80211_MCGRP_MLME, gfp);
18299 	return;
18300 
18301  nla_put_failure:
18302 	nlmsg_free(msg);
18303 }
18304 
18305 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
18306 				  struct net_device *netdev, const u8 *bssid,
18307 				  const u8 *td_bitmap, u8 td_bitmap_len)
18308 {
18309 	struct sk_buff *msg;
18310 	void *hdr;
18311 
18312 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18313 	if (!msg)
18314 		return;
18315 
18316 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
18317 	if (!hdr) {
18318 		nlmsg_free(msg);
18319 		return;
18320 	}
18321 
18322 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18323 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18324 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18325 		goto nla_put_failure;
18326 
18327 	if ((td_bitmap_len > 0) && td_bitmap)
18328 		if (nla_put(msg, NL80211_ATTR_TD_BITMAP,
18329 			    td_bitmap_len, td_bitmap))
18330 			goto nla_put_failure;
18331 
18332 	genlmsg_end(msg, hdr);
18333 
18334 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18335 				NL80211_MCGRP_MLME, GFP_KERNEL);
18336 	return;
18337 
18338  nla_put_failure:
18339 	nlmsg_free(msg);
18340 }
18341 
18342 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
18343 			       struct net_device *netdev, u16 reason,
18344 			       const u8 *ie, size_t ie_len, bool from_ap)
18345 {
18346 	struct sk_buff *msg;
18347 	void *hdr;
18348 
18349 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
18350 	if (!msg)
18351 		return;
18352 
18353 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
18354 	if (!hdr) {
18355 		nlmsg_free(msg);
18356 		return;
18357 	}
18358 
18359 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18360 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18361 	    (reason &&
18362 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
18363 	    (from_ap &&
18364 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
18365 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
18366 		goto nla_put_failure;
18367 
18368 	genlmsg_end(msg, hdr);
18369 
18370 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18371 				NL80211_MCGRP_MLME, GFP_KERNEL);
18372 	return;
18373 
18374  nla_put_failure:
18375 	nlmsg_free(msg);
18376 }
18377 
18378 void cfg80211_links_removed(struct net_device *dev, u16 link_mask)
18379 {
18380 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18381 	struct wiphy *wiphy = wdev->wiphy;
18382 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18383 	struct sk_buff *msg;
18384 	struct nlattr *links;
18385 	void *hdr;
18386 
18387 	ASSERT_WDEV_LOCK(wdev);
18388 	trace_cfg80211_links_removed(dev, link_mask);
18389 
18390 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
18391 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
18392 		return;
18393 
18394 	if (WARN_ON(!wdev->valid_links || !link_mask ||
18395 		    (wdev->valid_links & link_mask) != link_mask ||
18396 		    wdev->valid_links == link_mask))
18397 		return;
18398 
18399 	cfg80211_wdev_release_link_bsses(wdev, link_mask);
18400 	wdev->valid_links &= ~link_mask;
18401 
18402 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18403 	if (!msg)
18404 		return;
18405 
18406 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_LINKS_REMOVED);
18407 	if (!hdr) {
18408 		nlmsg_free(msg);
18409 		return;
18410 	}
18411 
18412 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18413 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
18414 		goto nla_put_failure;
18415 
18416 	links = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
18417 	if (!links)
18418 		goto nla_put_failure;
18419 
18420 	while (link_mask) {
18421 		struct nlattr *link;
18422 		int link_id = __ffs(link_mask);
18423 
18424 		link = nla_nest_start(msg, link_id + 1);
18425 		if (!link)
18426 			goto nla_put_failure;
18427 
18428 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
18429 			goto nla_put_failure;
18430 
18431 		nla_nest_end(msg, link);
18432 		link_mask &= ~(1 << link_id);
18433 	}
18434 
18435 	nla_nest_end(msg, links);
18436 
18437 	genlmsg_end(msg, hdr);
18438 
18439 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18440 				NL80211_MCGRP_MLME, GFP_KERNEL);
18441 	return;
18442 
18443  nla_put_failure:
18444 	nlmsg_free(msg);
18445 }
18446 EXPORT_SYMBOL(cfg80211_links_removed);
18447 
18448 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
18449 			     struct net_device *netdev, const u8 *bssid,
18450 			     gfp_t gfp)
18451 {
18452 	struct sk_buff *msg;
18453 	void *hdr;
18454 
18455 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18456 	if (!msg)
18457 		return;
18458 
18459 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
18460 	if (!hdr) {
18461 		nlmsg_free(msg);
18462 		return;
18463 	}
18464 
18465 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18466 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18467 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18468 		goto nla_put_failure;
18469 
18470 	genlmsg_end(msg, hdr);
18471 
18472 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18473 				NL80211_MCGRP_MLME, gfp);
18474 	return;
18475 
18476  nla_put_failure:
18477 	nlmsg_free(msg);
18478 }
18479 
18480 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
18481 					const u8 *ie, u8 ie_len,
18482 					int sig_dbm, gfp_t gfp)
18483 {
18484 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18485 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18486 	struct sk_buff *msg;
18487 	void *hdr;
18488 
18489 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
18490 		return;
18491 
18492 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
18493 
18494 	msg = nlmsg_new(100 + ie_len, gfp);
18495 	if (!msg)
18496 		return;
18497 
18498 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
18499 	if (!hdr) {
18500 		nlmsg_free(msg);
18501 		return;
18502 	}
18503 
18504 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18505 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18506 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18507 	    (ie_len && ie &&
18508 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
18509 	    (sig_dbm &&
18510 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
18511 		goto nla_put_failure;
18512 
18513 	genlmsg_end(msg, hdr);
18514 
18515 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18516 				NL80211_MCGRP_MLME, gfp);
18517 	return;
18518 
18519  nla_put_failure:
18520 	nlmsg_free(msg);
18521 }
18522 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
18523 
18524 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
18525 				 struct net_device *netdev, const u8 *addr,
18526 				 enum nl80211_key_type key_type, int key_id,
18527 				 const u8 *tsc, gfp_t gfp)
18528 {
18529 	struct sk_buff *msg;
18530 	void *hdr;
18531 
18532 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18533 	if (!msg)
18534 		return;
18535 
18536 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
18537 	if (!hdr) {
18538 		nlmsg_free(msg);
18539 		return;
18540 	}
18541 
18542 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18543 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18544 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
18545 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
18546 	    (key_id != -1 &&
18547 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
18548 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
18549 		goto nla_put_failure;
18550 
18551 	genlmsg_end(msg, hdr);
18552 
18553 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18554 				NL80211_MCGRP_MLME, gfp);
18555 	return;
18556 
18557  nla_put_failure:
18558 	nlmsg_free(msg);
18559 }
18560 
18561 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
18562 				    struct ieee80211_channel *channel_before,
18563 				    struct ieee80211_channel *channel_after)
18564 {
18565 	struct sk_buff *msg;
18566 	void *hdr;
18567 	struct nlattr *nl_freq;
18568 
18569 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
18570 	if (!msg)
18571 		return;
18572 
18573 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
18574 	if (!hdr) {
18575 		nlmsg_free(msg);
18576 		return;
18577 	}
18578 
18579 	/*
18580 	 * Since we are applying the beacon hint to a wiphy we know its
18581 	 * wiphy_idx is valid
18582 	 */
18583 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
18584 		goto nla_put_failure;
18585 
18586 	/* Before */
18587 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
18588 	if (!nl_freq)
18589 		goto nla_put_failure;
18590 
18591 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
18592 		goto nla_put_failure;
18593 	nla_nest_end(msg, nl_freq);
18594 
18595 	/* After */
18596 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
18597 	if (!nl_freq)
18598 		goto nla_put_failure;
18599 
18600 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
18601 		goto nla_put_failure;
18602 	nla_nest_end(msg, nl_freq);
18603 
18604 	genlmsg_end(msg, hdr);
18605 
18606 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
18607 				NL80211_MCGRP_REGULATORY);
18608 
18609 	return;
18610 
18611 nla_put_failure:
18612 	nlmsg_free(msg);
18613 }
18614 
18615 static void nl80211_send_remain_on_chan_event(
18616 	int cmd, struct cfg80211_registered_device *rdev,
18617 	struct wireless_dev *wdev, u64 cookie,
18618 	struct ieee80211_channel *chan,
18619 	unsigned int duration, gfp_t gfp)
18620 {
18621 	struct sk_buff *msg;
18622 	void *hdr;
18623 
18624 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18625 	if (!msg)
18626 		return;
18627 
18628 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18629 	if (!hdr) {
18630 		nlmsg_free(msg);
18631 		return;
18632 	}
18633 
18634 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18635 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18636 					 wdev->netdev->ifindex)) ||
18637 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18638 			      NL80211_ATTR_PAD) ||
18639 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
18640 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
18641 			NL80211_CHAN_NO_HT) ||
18642 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
18643 			      NL80211_ATTR_PAD))
18644 		goto nla_put_failure;
18645 
18646 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
18647 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
18648 		goto nla_put_failure;
18649 
18650 	genlmsg_end(msg, hdr);
18651 
18652 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18653 				NL80211_MCGRP_MLME, gfp);
18654 	return;
18655 
18656  nla_put_failure:
18657 	nlmsg_free(msg);
18658 }
18659 
18660 void cfg80211_assoc_comeback(struct net_device *netdev,
18661 			     const u8 *ap_addr, u32 timeout)
18662 {
18663 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
18664 	struct wiphy *wiphy = wdev->wiphy;
18665 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18666 	struct sk_buff *msg;
18667 	void *hdr;
18668 
18669 	trace_cfg80211_assoc_comeback(wdev, ap_addr, timeout);
18670 
18671 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18672 	if (!msg)
18673 		return;
18674 
18675 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ASSOC_COMEBACK);
18676 	if (!hdr) {
18677 		nlmsg_free(msg);
18678 		return;
18679 	}
18680 
18681 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18682 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18683 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ap_addr) ||
18684 	    nla_put_u32(msg, NL80211_ATTR_TIMEOUT, timeout))
18685 		goto nla_put_failure;
18686 
18687 	genlmsg_end(msg, hdr);
18688 
18689 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18690 				NL80211_MCGRP_MLME, GFP_KERNEL);
18691 	return;
18692 
18693  nla_put_failure:
18694 	nlmsg_free(msg);
18695 }
18696 EXPORT_SYMBOL(cfg80211_assoc_comeback);
18697 
18698 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
18699 			       struct ieee80211_channel *chan,
18700 			       unsigned int duration, gfp_t gfp)
18701 {
18702 	struct wiphy *wiphy = wdev->wiphy;
18703 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18704 
18705 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
18706 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
18707 					  rdev, wdev, cookie, chan,
18708 					  duration, gfp);
18709 }
18710 EXPORT_SYMBOL(cfg80211_ready_on_channel);
18711 
18712 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
18713 					struct ieee80211_channel *chan,
18714 					gfp_t gfp)
18715 {
18716 	struct wiphy *wiphy = wdev->wiphy;
18717 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18718 
18719 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
18720 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
18721 					  rdev, wdev, cookie, chan, 0, gfp);
18722 }
18723 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
18724 
18725 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
18726 					struct ieee80211_channel *chan,
18727 					gfp_t gfp)
18728 {
18729 	struct wiphy *wiphy = wdev->wiphy;
18730 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18731 
18732 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
18733 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
18734 					  rdev, wdev, cookie, chan, 0, gfp);
18735 }
18736 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
18737 
18738 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
18739 		      struct station_info *sinfo, gfp_t gfp)
18740 {
18741 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18742 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18743 	struct sk_buff *msg;
18744 
18745 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
18746 
18747 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18748 	if (!msg)
18749 		return;
18750 
18751 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
18752 				 rdev, dev, mac_addr, sinfo) < 0) {
18753 		nlmsg_free(msg);
18754 		return;
18755 	}
18756 
18757 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18758 				NL80211_MCGRP_MLME, gfp);
18759 }
18760 EXPORT_SYMBOL(cfg80211_new_sta);
18761 
18762 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
18763 			    struct station_info *sinfo, gfp_t gfp)
18764 {
18765 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18766 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18767 	struct sk_buff *msg;
18768 	struct station_info empty_sinfo = {};
18769 
18770 	if (!sinfo)
18771 		sinfo = &empty_sinfo;
18772 
18773 	trace_cfg80211_del_sta(dev, mac_addr);
18774 
18775 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18776 	if (!msg) {
18777 		cfg80211_sinfo_release_content(sinfo);
18778 		return;
18779 	}
18780 
18781 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
18782 				 rdev, dev, mac_addr, sinfo) < 0) {
18783 		nlmsg_free(msg);
18784 		return;
18785 	}
18786 
18787 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18788 				NL80211_MCGRP_MLME, gfp);
18789 }
18790 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
18791 
18792 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
18793 			  enum nl80211_connect_failed_reason reason,
18794 			  gfp_t gfp)
18795 {
18796 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18797 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18798 	struct sk_buff *msg;
18799 	void *hdr;
18800 
18801 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
18802 	if (!msg)
18803 		return;
18804 
18805 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
18806 	if (!hdr) {
18807 		nlmsg_free(msg);
18808 		return;
18809 	}
18810 
18811 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18812 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
18813 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
18814 		goto nla_put_failure;
18815 
18816 	genlmsg_end(msg, hdr);
18817 
18818 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18819 				NL80211_MCGRP_MLME, gfp);
18820 	return;
18821 
18822  nla_put_failure:
18823 	nlmsg_free(msg);
18824 }
18825 EXPORT_SYMBOL(cfg80211_conn_failed);
18826 
18827 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
18828 				       const u8 *addr, gfp_t gfp)
18829 {
18830 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18831 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18832 	struct sk_buff *msg;
18833 	void *hdr;
18834 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
18835 
18836 	if (!nlportid)
18837 		return false;
18838 
18839 	msg = nlmsg_new(100, gfp);
18840 	if (!msg)
18841 		return true;
18842 
18843 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18844 	if (!hdr) {
18845 		nlmsg_free(msg);
18846 		return true;
18847 	}
18848 
18849 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18850 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18851 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18852 		goto nla_put_failure;
18853 
18854 	genlmsg_end(msg, hdr);
18855 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18856 	return true;
18857 
18858  nla_put_failure:
18859 	nlmsg_free(msg);
18860 	return true;
18861 }
18862 
18863 bool cfg80211_rx_spurious_frame(struct net_device *dev,
18864 				const u8 *addr, gfp_t gfp)
18865 {
18866 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18867 	bool ret;
18868 
18869 	trace_cfg80211_rx_spurious_frame(dev, addr);
18870 
18871 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18872 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
18873 		trace_cfg80211_return_bool(false);
18874 		return false;
18875 	}
18876 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
18877 					 addr, gfp);
18878 	trace_cfg80211_return_bool(ret);
18879 	return ret;
18880 }
18881 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
18882 
18883 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
18884 					const u8 *addr, gfp_t gfp)
18885 {
18886 	struct wireless_dev *wdev = dev->ieee80211_ptr;
18887 	bool ret;
18888 
18889 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
18890 
18891 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18892 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
18893 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
18894 		trace_cfg80211_return_bool(false);
18895 		return false;
18896 	}
18897 	ret = __nl80211_unexpected_frame(dev,
18898 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
18899 					 addr, gfp);
18900 	trace_cfg80211_return_bool(ret);
18901 	return ret;
18902 }
18903 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
18904 
18905 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
18906 		      struct wireless_dev *wdev, u32 nlportid,
18907 		      struct cfg80211_rx_info *info, gfp_t gfp)
18908 {
18909 	struct net_device *netdev = wdev->netdev;
18910 	struct sk_buff *msg;
18911 	void *hdr;
18912 
18913 	msg = nlmsg_new(100 + info->len, gfp);
18914 	if (!msg)
18915 		return -ENOMEM;
18916 
18917 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
18918 	if (!hdr) {
18919 		nlmsg_free(msg);
18920 		return -ENOMEM;
18921 	}
18922 
18923 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18924 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18925 					netdev->ifindex)) ||
18926 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18927 			      NL80211_ATTR_PAD) ||
18928 	    (info->have_link_id &&
18929 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, info->link_id)) ||
18930 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(info->freq)) ||
18931 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, info->freq % 1000) ||
18932 	    (info->sig_dbm &&
18933 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, info->sig_dbm)) ||
18934 	    nla_put(msg, NL80211_ATTR_FRAME, info->len, info->buf) ||
18935 	    (info->flags &&
18936 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, info->flags)) ||
18937 	    (info->rx_tstamp && nla_put_u64_64bit(msg,
18938 						  NL80211_ATTR_RX_HW_TIMESTAMP,
18939 						  info->rx_tstamp,
18940 						  NL80211_ATTR_PAD)) ||
18941 	    (info->ack_tstamp && nla_put_u64_64bit(msg,
18942 						   NL80211_ATTR_TX_HW_TIMESTAMP,
18943 						   info->ack_tstamp,
18944 						   NL80211_ATTR_PAD)))
18945 		goto nla_put_failure;
18946 
18947 	genlmsg_end(msg, hdr);
18948 
18949 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18950 
18951  nla_put_failure:
18952 	nlmsg_free(msg);
18953 	return -ENOBUFS;
18954 }
18955 
18956 static void nl80211_frame_tx_status(struct wireless_dev *wdev,
18957 				    struct cfg80211_tx_status *status,
18958 				    gfp_t gfp, enum nl80211_commands command)
18959 {
18960 	struct wiphy *wiphy = wdev->wiphy;
18961 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18962 	struct net_device *netdev = wdev->netdev;
18963 	struct sk_buff *msg;
18964 	void *hdr;
18965 
18966 	if (command == NL80211_CMD_FRAME_TX_STATUS)
18967 		trace_cfg80211_mgmt_tx_status(wdev, status->cookie,
18968 					      status->ack);
18969 	else
18970 		trace_cfg80211_control_port_tx_status(wdev, status->cookie,
18971 						      status->ack);
18972 
18973 	msg = nlmsg_new(100 + status->len, gfp);
18974 	if (!msg)
18975 		return;
18976 
18977 	hdr = nl80211hdr_put(msg, 0, 0, 0, command);
18978 	if (!hdr) {
18979 		nlmsg_free(msg);
18980 		return;
18981 	}
18982 
18983 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18984 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18985 				   netdev->ifindex)) ||
18986 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18987 			      NL80211_ATTR_PAD) ||
18988 	    nla_put(msg, NL80211_ATTR_FRAME, status->len, status->buf) ||
18989 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, status->cookie,
18990 			      NL80211_ATTR_PAD) ||
18991 	    (status->ack && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
18992 	    (status->tx_tstamp &&
18993 	     nla_put_u64_64bit(msg, NL80211_ATTR_TX_HW_TIMESTAMP,
18994 			       status->tx_tstamp, NL80211_ATTR_PAD)) ||
18995 	    (status->ack_tstamp &&
18996 	     nla_put_u64_64bit(msg, NL80211_ATTR_RX_HW_TIMESTAMP,
18997 			       status->ack_tstamp, NL80211_ATTR_PAD)))
18998 		goto nla_put_failure;
18999 
19000 	genlmsg_end(msg, hdr);
19001 
19002 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19003 				NL80211_MCGRP_MLME, gfp);
19004 	return;
19005 
19006 nla_put_failure:
19007 	nlmsg_free(msg);
19008 }
19009 
19010 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
19011 				     const u8 *buf, size_t len, bool ack,
19012 				     gfp_t gfp)
19013 {
19014 	struct cfg80211_tx_status status = {
19015 		.cookie = cookie,
19016 		.buf = buf,
19017 		.len = len,
19018 		.ack = ack
19019 	};
19020 
19021 	nl80211_frame_tx_status(wdev, &status, gfp,
19022 				NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
19023 }
19024 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
19025 
19026 void cfg80211_mgmt_tx_status_ext(struct wireless_dev *wdev,
19027 				 struct cfg80211_tx_status *status, gfp_t gfp)
19028 {
19029 	nl80211_frame_tx_status(wdev, status, gfp, NL80211_CMD_FRAME_TX_STATUS);
19030 }
19031 EXPORT_SYMBOL(cfg80211_mgmt_tx_status_ext);
19032 
19033 static int __nl80211_rx_control_port(struct net_device *dev,
19034 				     struct sk_buff *skb,
19035 				     bool unencrypted,
19036 				     int link_id,
19037 				     gfp_t gfp)
19038 {
19039 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19040 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19041 	struct ethhdr *ehdr = eth_hdr(skb);
19042 	const u8 *addr = ehdr->h_source;
19043 	u16 proto = be16_to_cpu(skb->protocol);
19044 	struct sk_buff *msg;
19045 	void *hdr;
19046 	struct nlattr *frame;
19047 
19048 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
19049 
19050 	if (!nlportid)
19051 		return -ENOENT;
19052 
19053 	msg = nlmsg_new(100 + skb->len, gfp);
19054 	if (!msg)
19055 		return -ENOMEM;
19056 
19057 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
19058 	if (!hdr) {
19059 		nlmsg_free(msg);
19060 		return -ENOBUFS;
19061 	}
19062 
19063 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19064 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19065 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19066 			      NL80211_ATTR_PAD) ||
19067 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19068 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
19069 	    (link_id >= 0 &&
19070 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
19071 	    (unencrypted && nla_put_flag(msg,
19072 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
19073 		goto nla_put_failure;
19074 
19075 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
19076 	if (!frame)
19077 		goto nla_put_failure;
19078 
19079 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
19080 	genlmsg_end(msg, hdr);
19081 
19082 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
19083 
19084  nla_put_failure:
19085 	nlmsg_free(msg);
19086 	return -ENOBUFS;
19087 }
19088 
19089 bool cfg80211_rx_control_port(struct net_device *dev, struct sk_buff *skb,
19090 			      bool unencrypted, int link_id)
19091 {
19092 	int ret;
19093 
19094 	trace_cfg80211_rx_control_port(dev, skb, unencrypted, link_id);
19095 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, link_id,
19096 					GFP_ATOMIC);
19097 	trace_cfg80211_return_bool(ret == 0);
19098 	return ret == 0;
19099 }
19100 EXPORT_SYMBOL(cfg80211_rx_control_port);
19101 
19102 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
19103 					    const char *mac, gfp_t gfp)
19104 {
19105 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19106 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19107 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19108 	void **cb;
19109 
19110 	if (!msg)
19111 		return NULL;
19112 
19113 	cb = (void **)msg->cb;
19114 
19115 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
19116 	if (!cb[0]) {
19117 		nlmsg_free(msg);
19118 		return NULL;
19119 	}
19120 
19121 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19122 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19123 		goto nla_put_failure;
19124 
19125 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19126 		goto nla_put_failure;
19127 
19128 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
19129 	if (!cb[1])
19130 		goto nla_put_failure;
19131 
19132 	cb[2] = rdev;
19133 
19134 	return msg;
19135  nla_put_failure:
19136 	nlmsg_free(msg);
19137 	return NULL;
19138 }
19139 
19140 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
19141 {
19142 	void **cb = (void **)msg->cb;
19143 	struct cfg80211_registered_device *rdev = cb[2];
19144 
19145 	nla_nest_end(msg, cb[1]);
19146 	genlmsg_end(msg, cb[0]);
19147 
19148 	memset(msg->cb, 0, sizeof(msg->cb));
19149 
19150 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19151 				NL80211_MCGRP_MLME, gfp);
19152 }
19153 
19154 void cfg80211_cqm_rssi_notify(struct net_device *dev,
19155 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
19156 			      s32 rssi_level, gfp_t gfp)
19157 {
19158 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19159 	struct cfg80211_cqm_config *cqm_config;
19160 
19161 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
19162 
19163 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
19164 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
19165 		return;
19166 
19167 	rcu_read_lock();
19168 	cqm_config = rcu_dereference(wdev->cqm_config);
19169 	if (cqm_config) {
19170 		cqm_config->last_rssi_event_value = rssi_level;
19171 		cqm_config->last_rssi_event_type = rssi_event;
19172 		wiphy_work_queue(wdev->wiphy, &wdev->cqm_rssi_work);
19173 	}
19174 	rcu_read_unlock();
19175 }
19176 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
19177 
19178 void cfg80211_cqm_rssi_notify_work(struct wiphy *wiphy, struct wiphy_work *work)
19179 {
19180 	struct wireless_dev *wdev = container_of(work, struct wireless_dev,
19181 						 cqm_rssi_work);
19182 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19183 	enum nl80211_cqm_rssi_threshold_event rssi_event;
19184 	struct cfg80211_cqm_config *cqm_config;
19185 	struct sk_buff *msg;
19186 	s32 rssi_level;
19187 
19188 	wdev_lock(wdev);
19189 	cqm_config = rcu_dereference_protected(wdev->cqm_config,
19190 					       lockdep_is_held(&wdev->mtx));
19191 	if (!cqm_config)
19192 		goto unlock;
19193 
19194 	if (cqm_config->use_range_api)
19195 		cfg80211_cqm_rssi_update(rdev, wdev->netdev, cqm_config);
19196 
19197 	rssi_level = cqm_config->last_rssi_event_value;
19198 	rssi_event = cqm_config->last_rssi_event_type;
19199 
19200 	msg = cfg80211_prepare_cqm(wdev->netdev, NULL, GFP_KERNEL);
19201 	if (!msg)
19202 		goto unlock;
19203 
19204 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
19205 			rssi_event))
19206 		goto nla_put_failure;
19207 
19208 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
19209 				      rssi_level))
19210 		goto nla_put_failure;
19211 
19212 	cfg80211_send_cqm(msg, GFP_KERNEL);
19213 
19214 	goto unlock;
19215 
19216  nla_put_failure:
19217 	nlmsg_free(msg);
19218  unlock:
19219 	wdev_unlock(wdev);
19220 }
19221 
19222 void cfg80211_cqm_txe_notify(struct net_device *dev,
19223 			     const u8 *peer, u32 num_packets,
19224 			     u32 rate, u32 intvl, gfp_t gfp)
19225 {
19226 	struct sk_buff *msg;
19227 
19228 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19229 	if (!msg)
19230 		return;
19231 
19232 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
19233 		goto nla_put_failure;
19234 
19235 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
19236 		goto nla_put_failure;
19237 
19238 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
19239 		goto nla_put_failure;
19240 
19241 	cfg80211_send_cqm(msg, gfp);
19242 	return;
19243 
19244  nla_put_failure:
19245 	nlmsg_free(msg);
19246 }
19247 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
19248 
19249 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
19250 				 const u8 *peer, u32 num_packets, gfp_t gfp)
19251 {
19252 	struct sk_buff *msg;
19253 
19254 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
19255 
19256 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
19257 	if (!msg)
19258 		return;
19259 
19260 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
19261 		goto nla_put_failure;
19262 
19263 	cfg80211_send_cqm(msg, gfp);
19264 	return;
19265 
19266  nla_put_failure:
19267 	nlmsg_free(msg);
19268 }
19269 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
19270 
19271 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
19272 {
19273 	struct sk_buff *msg;
19274 
19275 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
19276 	if (!msg)
19277 		return;
19278 
19279 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
19280 		goto nla_put_failure;
19281 
19282 	cfg80211_send_cqm(msg, gfp);
19283 	return;
19284 
19285  nla_put_failure:
19286 	nlmsg_free(msg);
19287 }
19288 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
19289 
19290 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
19291 				     struct net_device *netdev, const u8 *bssid,
19292 				     const u8 *replay_ctr, gfp_t gfp)
19293 {
19294 	struct sk_buff *msg;
19295 	struct nlattr *rekey_attr;
19296 	void *hdr;
19297 
19298 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19299 	if (!msg)
19300 		return;
19301 
19302 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
19303 	if (!hdr) {
19304 		nlmsg_free(msg);
19305 		return;
19306 	}
19307 
19308 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19309 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19310 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
19311 		goto nla_put_failure;
19312 
19313 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
19314 	if (!rekey_attr)
19315 		goto nla_put_failure;
19316 
19317 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
19318 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
19319 		goto nla_put_failure;
19320 
19321 	nla_nest_end(msg, rekey_attr);
19322 
19323 	genlmsg_end(msg, hdr);
19324 
19325 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19326 				NL80211_MCGRP_MLME, gfp);
19327 	return;
19328 
19329  nla_put_failure:
19330 	nlmsg_free(msg);
19331 }
19332 
19333 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
19334 			       const u8 *replay_ctr, gfp_t gfp)
19335 {
19336 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19337 	struct wiphy *wiphy = wdev->wiphy;
19338 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19339 
19340 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
19341 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
19342 }
19343 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
19344 
19345 static void
19346 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
19347 			       struct net_device *netdev, int index,
19348 			       const u8 *bssid, bool preauth, gfp_t gfp)
19349 {
19350 	struct sk_buff *msg;
19351 	struct nlattr *attr;
19352 	void *hdr;
19353 
19354 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19355 	if (!msg)
19356 		return;
19357 
19358 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
19359 	if (!hdr) {
19360 		nlmsg_free(msg);
19361 		return;
19362 	}
19363 
19364 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19365 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19366 		goto nla_put_failure;
19367 
19368 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
19369 	if (!attr)
19370 		goto nla_put_failure;
19371 
19372 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
19373 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
19374 	    (preauth &&
19375 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
19376 		goto nla_put_failure;
19377 
19378 	nla_nest_end(msg, attr);
19379 
19380 	genlmsg_end(msg, hdr);
19381 
19382 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19383 				NL80211_MCGRP_MLME, gfp);
19384 	return;
19385 
19386  nla_put_failure:
19387 	nlmsg_free(msg);
19388 }
19389 
19390 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
19391 				     const u8 *bssid, bool preauth, gfp_t gfp)
19392 {
19393 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19394 	struct wiphy *wiphy = wdev->wiphy;
19395 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19396 
19397 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
19398 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
19399 }
19400 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
19401 
19402 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
19403 				     struct net_device *netdev,
19404 				     unsigned int link_id,
19405 				     struct cfg80211_chan_def *chandef,
19406 				     gfp_t gfp,
19407 				     enum nl80211_commands notif,
19408 				     u8 count, bool quiet, u16 punct_bitmap)
19409 {
19410 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
19411 	struct sk_buff *msg;
19412 	void *hdr;
19413 
19414 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19415 	if (!msg)
19416 		return;
19417 
19418 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
19419 	if (!hdr) {
19420 		nlmsg_free(msg);
19421 		return;
19422 	}
19423 
19424 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
19425 		goto nla_put_failure;
19426 
19427 	if (wdev->valid_links &&
19428 	    nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
19429 		goto nla_put_failure;
19430 
19431 	if (nl80211_send_chandef(msg, chandef))
19432 		goto nla_put_failure;
19433 
19434 	if (notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) {
19435 		if (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count))
19436 			goto nla_put_failure;
19437 		if (quiet &&
19438 		    nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX))
19439 			goto nla_put_failure;
19440 	}
19441 
19442 	if (nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP, punct_bitmap))
19443 		goto nla_put_failure;
19444 
19445 	genlmsg_end(msg, hdr);
19446 
19447 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19448 				NL80211_MCGRP_MLME, gfp);
19449 	return;
19450 
19451  nla_put_failure:
19452 	nlmsg_free(msg);
19453 }
19454 
19455 void cfg80211_ch_switch_notify(struct net_device *dev,
19456 			       struct cfg80211_chan_def *chandef,
19457 			       unsigned int link_id, u16 punct_bitmap)
19458 {
19459 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19460 	struct wiphy *wiphy = wdev->wiphy;
19461 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19462 
19463 	ASSERT_WDEV_LOCK(wdev);
19464 	WARN_INVALID_LINK_ID(wdev, link_id);
19465 
19466 	trace_cfg80211_ch_switch_notify(dev, chandef, link_id, punct_bitmap);
19467 
19468 	switch (wdev->iftype) {
19469 	case NL80211_IFTYPE_STATION:
19470 	case NL80211_IFTYPE_P2P_CLIENT:
19471 		if (!WARN_ON(!wdev->links[link_id].client.current_bss))
19472 			cfg80211_update_assoc_bss_entry(wdev, link_id,
19473 							chandef->chan);
19474 		break;
19475 	case NL80211_IFTYPE_MESH_POINT:
19476 		wdev->u.mesh.chandef = *chandef;
19477 		wdev->u.mesh.preset_chandef = *chandef;
19478 		break;
19479 	case NL80211_IFTYPE_AP:
19480 	case NL80211_IFTYPE_P2P_GO:
19481 		wdev->links[link_id].ap.chandef = *chandef;
19482 		break;
19483 	case NL80211_IFTYPE_ADHOC:
19484 		wdev->u.ibss.chandef = *chandef;
19485 		break;
19486 	default:
19487 		WARN_ON(1);
19488 		break;
19489 	}
19490 
19491 	cfg80211_sched_dfs_chan_update(rdev);
19492 
19493 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19494 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0, false,
19495 				 punct_bitmap);
19496 }
19497 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
19498 
19499 void cfg80211_ch_switch_started_notify(struct net_device *dev,
19500 				       struct cfg80211_chan_def *chandef,
19501 				       unsigned int link_id, u8 count,
19502 				       bool quiet, u16 punct_bitmap)
19503 {
19504 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19505 	struct wiphy *wiphy = wdev->wiphy;
19506 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19507 
19508 	ASSERT_WDEV_LOCK(wdev);
19509 	WARN_INVALID_LINK_ID(wdev, link_id);
19510 
19511 	trace_cfg80211_ch_switch_started_notify(dev, chandef, link_id,
19512 						punct_bitmap);
19513 
19514 
19515 	nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19516 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY,
19517 				 count, quiet, punct_bitmap);
19518 }
19519 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
19520 
19521 int cfg80211_bss_color_notify(struct net_device *dev,
19522 			      enum nl80211_commands cmd, u8 count,
19523 			      u64 color_bitmap)
19524 {
19525 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19526 	struct wiphy *wiphy = wdev->wiphy;
19527 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19528 	struct sk_buff *msg;
19529 	void *hdr;
19530 
19531 	ASSERT_WDEV_LOCK(wdev);
19532 
19533 	trace_cfg80211_bss_color_notify(dev, cmd, count, color_bitmap);
19534 
19535 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19536 	if (!msg)
19537 		return -ENOMEM;
19538 
19539 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
19540 	if (!hdr)
19541 		goto nla_put_failure;
19542 
19543 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19544 		goto nla_put_failure;
19545 
19546 	if (cmd == NL80211_CMD_COLOR_CHANGE_STARTED &&
19547 	    nla_put_u32(msg, NL80211_ATTR_COLOR_CHANGE_COUNT, count))
19548 		goto nla_put_failure;
19549 
19550 	if (cmd == NL80211_CMD_OBSS_COLOR_COLLISION &&
19551 	    nla_put_u64_64bit(msg, NL80211_ATTR_OBSS_COLOR_BITMAP,
19552 			      color_bitmap, NL80211_ATTR_PAD))
19553 		goto nla_put_failure;
19554 
19555 	genlmsg_end(msg, hdr);
19556 
19557 	return genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
19558 				       msg, 0, NL80211_MCGRP_MLME, GFP_KERNEL);
19559 
19560 nla_put_failure:
19561 	nlmsg_free(msg);
19562 	return -EINVAL;
19563 }
19564 EXPORT_SYMBOL(cfg80211_bss_color_notify);
19565 
19566 void
19567 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
19568 		     const struct cfg80211_chan_def *chandef,
19569 		     enum nl80211_radar_event event,
19570 		     struct net_device *netdev, gfp_t gfp)
19571 {
19572 	struct sk_buff *msg;
19573 	void *hdr;
19574 
19575 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19576 	if (!msg)
19577 		return;
19578 
19579 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
19580 	if (!hdr) {
19581 		nlmsg_free(msg);
19582 		return;
19583 	}
19584 
19585 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19586 		goto nla_put_failure;
19587 
19588 	/* NOP and radar events don't need a netdev parameter */
19589 	if (netdev) {
19590 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
19591 
19592 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19593 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19594 				      NL80211_ATTR_PAD))
19595 			goto nla_put_failure;
19596 	}
19597 
19598 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
19599 		goto nla_put_failure;
19600 
19601 	if (nl80211_send_chandef(msg, chandef))
19602 		goto nla_put_failure;
19603 
19604 	genlmsg_end(msg, hdr);
19605 
19606 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19607 				NL80211_MCGRP_MLME, gfp);
19608 	return;
19609 
19610  nla_put_failure:
19611 	nlmsg_free(msg);
19612 }
19613 
19614 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
19615 				       struct sta_opmode_info *sta_opmode,
19616 				       gfp_t gfp)
19617 {
19618 	struct sk_buff *msg;
19619 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19620 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19621 	void *hdr;
19622 
19623 	if (WARN_ON(!mac))
19624 		return;
19625 
19626 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19627 	if (!msg)
19628 		return;
19629 
19630 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
19631 	if (!hdr) {
19632 		nlmsg_free(msg);
19633 		return;
19634 	}
19635 
19636 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19637 		goto nla_put_failure;
19638 
19639 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19640 		goto nla_put_failure;
19641 
19642 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19643 		goto nla_put_failure;
19644 
19645 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
19646 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
19647 		goto nla_put_failure;
19648 
19649 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
19650 	    nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
19651 		goto nla_put_failure;
19652 
19653 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
19654 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
19655 		goto nla_put_failure;
19656 
19657 	genlmsg_end(msg, hdr);
19658 
19659 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19660 				NL80211_MCGRP_MLME, gfp);
19661 
19662 	return;
19663 
19664 nla_put_failure:
19665 	nlmsg_free(msg);
19666 }
19667 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
19668 
19669 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
19670 			   u64 cookie, bool acked, s32 ack_signal,
19671 			   bool is_valid_ack_signal, gfp_t gfp)
19672 {
19673 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19674 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19675 	struct sk_buff *msg;
19676 	void *hdr;
19677 
19678 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
19679 
19680 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19681 
19682 	if (!msg)
19683 		return;
19684 
19685 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
19686 	if (!hdr) {
19687 		nlmsg_free(msg);
19688 		return;
19689 	}
19690 
19691 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19692 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19693 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19694 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
19695 			      NL80211_ATTR_PAD) ||
19696 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
19697 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
19698 						ack_signal)))
19699 		goto nla_put_failure;
19700 
19701 	genlmsg_end(msg, hdr);
19702 
19703 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19704 				NL80211_MCGRP_MLME, gfp);
19705 	return;
19706 
19707  nla_put_failure:
19708 	nlmsg_free(msg);
19709 }
19710 EXPORT_SYMBOL(cfg80211_probe_status);
19711 
19712 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
19713 				     size_t len, int freq, int sig_dbm)
19714 {
19715 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19716 	struct sk_buff *msg;
19717 	void *hdr;
19718 	struct cfg80211_beacon_registration *reg;
19719 
19720 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
19721 
19722 	spin_lock_bh(&rdev->beacon_registrations_lock);
19723 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
19724 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
19725 		if (!msg) {
19726 			spin_unlock_bh(&rdev->beacon_registrations_lock);
19727 			return;
19728 		}
19729 
19730 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
19731 		if (!hdr)
19732 			goto nla_put_failure;
19733 
19734 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19735 		    (freq &&
19736 		     (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
19737 				  KHZ_TO_MHZ(freq)) ||
19738 		      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
19739 				  freq % 1000))) ||
19740 		    (sig_dbm &&
19741 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
19742 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
19743 			goto nla_put_failure;
19744 
19745 		genlmsg_end(msg, hdr);
19746 
19747 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
19748 	}
19749 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19750 	return;
19751 
19752  nla_put_failure:
19753 	spin_unlock_bh(&rdev->beacon_registrations_lock);
19754 	nlmsg_free(msg);
19755 }
19756 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
19757 
19758 #ifdef CONFIG_PM
19759 static int cfg80211_net_detect_results(struct sk_buff *msg,
19760 				       struct cfg80211_wowlan_wakeup *wakeup)
19761 {
19762 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
19763 	struct nlattr *nl_results, *nl_match, *nl_freqs;
19764 	int i, j;
19765 
19766 	nl_results = nla_nest_start_noflag(msg,
19767 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
19768 	if (!nl_results)
19769 		return -EMSGSIZE;
19770 
19771 	for (i = 0; i < nd->n_matches; i++) {
19772 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
19773 
19774 		nl_match = nla_nest_start_noflag(msg, i);
19775 		if (!nl_match)
19776 			break;
19777 
19778 		/* The SSID attribute is optional in nl80211, but for
19779 		 * simplicity reasons it's always present in the
19780 		 * cfg80211 structure.  If a driver can't pass the
19781 		 * SSID, that needs to be changed.  A zero length SSID
19782 		 * is still a valid SSID (wildcard), so it cannot be
19783 		 * used for this purpose.
19784 		 */
19785 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
19786 			    match->ssid.ssid)) {
19787 			nla_nest_cancel(msg, nl_match);
19788 			goto out;
19789 		}
19790 
19791 		if (match->n_channels) {
19792 			nl_freqs = nla_nest_start_noflag(msg,
19793 							 NL80211_ATTR_SCAN_FREQUENCIES);
19794 			if (!nl_freqs) {
19795 				nla_nest_cancel(msg, nl_match);
19796 				goto out;
19797 			}
19798 
19799 			for (j = 0; j < match->n_channels; j++) {
19800 				if (nla_put_u32(msg, j, match->channels[j])) {
19801 					nla_nest_cancel(msg, nl_freqs);
19802 					nla_nest_cancel(msg, nl_match);
19803 					goto out;
19804 				}
19805 			}
19806 
19807 			nla_nest_end(msg, nl_freqs);
19808 		}
19809 
19810 		nla_nest_end(msg, nl_match);
19811 	}
19812 
19813 out:
19814 	nla_nest_end(msg, nl_results);
19815 	return 0;
19816 }
19817 
19818 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
19819 				   struct cfg80211_wowlan_wakeup *wakeup,
19820 				   gfp_t gfp)
19821 {
19822 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19823 	struct sk_buff *msg;
19824 	void *hdr;
19825 	int size = 200;
19826 
19827 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
19828 
19829 	if (wakeup)
19830 		size += wakeup->packet_present_len;
19831 
19832 	msg = nlmsg_new(size, gfp);
19833 	if (!msg)
19834 		return;
19835 
19836 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
19837 	if (!hdr)
19838 		goto free_msg;
19839 
19840 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19841 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19842 			      NL80211_ATTR_PAD))
19843 		goto free_msg;
19844 
19845 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
19846 					wdev->netdev->ifindex))
19847 		goto free_msg;
19848 
19849 	if (wakeup) {
19850 		struct nlattr *reasons;
19851 
19852 		reasons = nla_nest_start_noflag(msg,
19853 						NL80211_ATTR_WOWLAN_TRIGGERS);
19854 		if (!reasons)
19855 			goto free_msg;
19856 
19857 		if (wakeup->disconnect &&
19858 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
19859 			goto free_msg;
19860 		if (wakeup->magic_pkt &&
19861 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
19862 			goto free_msg;
19863 		if (wakeup->gtk_rekey_failure &&
19864 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
19865 			goto free_msg;
19866 		if (wakeup->eap_identity_req &&
19867 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
19868 			goto free_msg;
19869 		if (wakeup->four_way_handshake &&
19870 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
19871 			goto free_msg;
19872 		if (wakeup->rfkill_release &&
19873 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
19874 			goto free_msg;
19875 
19876 		if (wakeup->pattern_idx >= 0 &&
19877 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
19878 				wakeup->pattern_idx))
19879 			goto free_msg;
19880 
19881 		if (wakeup->tcp_match &&
19882 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
19883 			goto free_msg;
19884 
19885 		if (wakeup->tcp_connlost &&
19886 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
19887 			goto free_msg;
19888 
19889 		if (wakeup->tcp_nomoretokens &&
19890 		    nla_put_flag(msg,
19891 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
19892 			goto free_msg;
19893 
19894 		if (wakeup->packet) {
19895 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
19896 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
19897 
19898 			if (!wakeup->packet_80211) {
19899 				pkt_attr =
19900 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
19901 				len_attr =
19902 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
19903 			}
19904 
19905 			if (wakeup->packet_len &&
19906 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
19907 				goto free_msg;
19908 
19909 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
19910 				    wakeup->packet))
19911 				goto free_msg;
19912 		}
19913 
19914 		if (wakeup->net_detect &&
19915 		    cfg80211_net_detect_results(msg, wakeup))
19916 				goto free_msg;
19917 
19918 		nla_nest_end(msg, reasons);
19919 	}
19920 
19921 	genlmsg_end(msg, hdr);
19922 
19923 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19924 				NL80211_MCGRP_MLME, gfp);
19925 	return;
19926 
19927  free_msg:
19928 	nlmsg_free(msg);
19929 }
19930 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
19931 #endif
19932 
19933 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
19934 				enum nl80211_tdls_operation oper,
19935 				u16 reason_code, gfp_t gfp)
19936 {
19937 	struct wireless_dev *wdev = dev->ieee80211_ptr;
19938 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19939 	struct sk_buff *msg;
19940 	void *hdr;
19941 
19942 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
19943 					 reason_code);
19944 
19945 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19946 	if (!msg)
19947 		return;
19948 
19949 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
19950 	if (!hdr) {
19951 		nlmsg_free(msg);
19952 		return;
19953 	}
19954 
19955 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19956 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19957 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
19958 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
19959 	    (reason_code > 0 &&
19960 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
19961 		goto nla_put_failure;
19962 
19963 	genlmsg_end(msg, hdr);
19964 
19965 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19966 				NL80211_MCGRP_MLME, gfp);
19967 	return;
19968 
19969  nla_put_failure:
19970 	nlmsg_free(msg);
19971 }
19972 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
19973 
19974 static int nl80211_netlink_notify(struct notifier_block * nb,
19975 				  unsigned long state,
19976 				  void *_notify)
19977 {
19978 	struct netlink_notify *notify = _notify;
19979 	struct cfg80211_registered_device *rdev;
19980 	struct wireless_dev *wdev;
19981 	struct cfg80211_beacon_registration *reg, *tmp;
19982 
19983 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
19984 		return NOTIFY_DONE;
19985 
19986 	rcu_read_lock();
19987 
19988 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
19989 		struct cfg80211_sched_scan_request *sched_scan_req;
19990 
19991 		list_for_each_entry_rcu(sched_scan_req,
19992 					&rdev->sched_scan_req_list,
19993 					list) {
19994 			if (sched_scan_req->owner_nlportid == notify->portid) {
19995 				sched_scan_req->nl_owner_dead = true;
19996 				wiphy_work_queue(&rdev->wiphy,
19997 						 &rdev->sched_scan_stop_wk);
19998 			}
19999 		}
20000 
20001 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
20002 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
20003 
20004 			if (wdev->owner_nlportid == notify->portid) {
20005 				wdev->nl_owner_dead = true;
20006 				schedule_work(&rdev->destroy_work);
20007 			} else if (wdev->conn_owner_nlportid == notify->portid) {
20008 				schedule_work(&wdev->disconnect_wk);
20009 			}
20010 
20011 			cfg80211_release_pmsr(wdev, notify->portid);
20012 		}
20013 
20014 		spin_lock_bh(&rdev->beacon_registrations_lock);
20015 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
20016 					 list) {
20017 			if (reg->nlportid == notify->portid) {
20018 				list_del(&reg->list);
20019 				kfree(reg);
20020 				break;
20021 			}
20022 		}
20023 		spin_unlock_bh(&rdev->beacon_registrations_lock);
20024 	}
20025 
20026 	rcu_read_unlock();
20027 
20028 	/*
20029 	 * It is possible that the user space process that is controlling the
20030 	 * indoor setting disappeared, so notify the regulatory core.
20031 	 */
20032 	regulatory_netlink_notify(notify->portid);
20033 	return NOTIFY_OK;
20034 }
20035 
20036 static struct notifier_block nl80211_netlink_notifier = {
20037 	.notifier_call = nl80211_netlink_notify,
20038 };
20039 
20040 void cfg80211_ft_event(struct net_device *netdev,
20041 		       struct cfg80211_ft_event_params *ft_event)
20042 {
20043 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
20044 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20045 	struct sk_buff *msg;
20046 	void *hdr;
20047 
20048 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
20049 
20050 	if (!ft_event->target_ap)
20051 		return;
20052 
20053 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
20054 			GFP_KERNEL);
20055 	if (!msg)
20056 		return;
20057 
20058 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
20059 	if (!hdr)
20060 		goto out;
20061 
20062 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20063 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
20064 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
20065 		goto out;
20066 
20067 	if (ft_event->ies &&
20068 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
20069 		goto out;
20070 	if (ft_event->ric_ies &&
20071 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
20072 		    ft_event->ric_ies))
20073 		goto out;
20074 
20075 	genlmsg_end(msg, hdr);
20076 
20077 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
20078 				NL80211_MCGRP_MLME, GFP_KERNEL);
20079 	return;
20080  out:
20081 	nlmsg_free(msg);
20082 }
20083 EXPORT_SYMBOL(cfg80211_ft_event);
20084 
20085 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
20086 {
20087 	struct cfg80211_registered_device *rdev;
20088 	struct sk_buff *msg;
20089 	void *hdr;
20090 	u32 nlportid;
20091 
20092 	rdev = wiphy_to_rdev(wdev->wiphy);
20093 	if (!rdev->crit_proto_nlportid)
20094 		return;
20095 
20096 	nlportid = rdev->crit_proto_nlportid;
20097 	rdev->crit_proto_nlportid = 0;
20098 
20099 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20100 	if (!msg)
20101 		return;
20102 
20103 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
20104 	if (!hdr)
20105 		goto nla_put_failure;
20106 
20107 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20108 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
20109 			      NL80211_ATTR_PAD))
20110 		goto nla_put_failure;
20111 
20112 	genlmsg_end(msg, hdr);
20113 
20114 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
20115 	return;
20116 
20117  nla_put_failure:
20118 	nlmsg_free(msg);
20119 }
20120 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
20121 
20122 void nl80211_send_ap_stopped(struct wireless_dev *wdev, unsigned int link_id)
20123 {
20124 	struct wiphy *wiphy = wdev->wiphy;
20125 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20126 	struct sk_buff *msg;
20127 	void *hdr;
20128 
20129 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
20130 	if (!msg)
20131 		return;
20132 
20133 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
20134 	if (!hdr)
20135 		goto out;
20136 
20137 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20138 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
20139 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
20140 			      NL80211_ATTR_PAD) ||
20141 	    (wdev->valid_links &&
20142 	     nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)))
20143 		goto out;
20144 
20145 	genlmsg_end(msg, hdr);
20146 
20147 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
20148 				NL80211_MCGRP_MLME, GFP_KERNEL);
20149 	return;
20150  out:
20151 	nlmsg_free(msg);
20152 }
20153 
20154 int cfg80211_external_auth_request(struct net_device *dev,
20155 				   struct cfg80211_external_auth_params *params,
20156 				   gfp_t gfp)
20157 {
20158 	struct wireless_dev *wdev = dev->ieee80211_ptr;
20159 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
20160 	struct sk_buff *msg;
20161 	void *hdr;
20162 
20163 	if (!wdev->conn_owner_nlportid)
20164 		return -EINVAL;
20165 
20166 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20167 	if (!msg)
20168 		return -ENOMEM;
20169 
20170 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
20171 	if (!hdr)
20172 		goto nla_put_failure;
20173 
20174 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20175 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
20176 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
20177 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
20178 			params->action) ||
20179 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
20180 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
20181 		    params->ssid.ssid) ||
20182 	    (!is_zero_ether_addr(params->mld_addr) &&
20183 	     nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN, params->mld_addr)))
20184 		goto nla_put_failure;
20185 
20186 	genlmsg_end(msg, hdr);
20187 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
20188 			wdev->conn_owner_nlportid);
20189 	return 0;
20190 
20191  nla_put_failure:
20192 	nlmsg_free(msg);
20193 	return -ENOBUFS;
20194 }
20195 EXPORT_SYMBOL(cfg80211_external_auth_request);
20196 
20197 void cfg80211_update_owe_info_event(struct net_device *netdev,
20198 				    struct cfg80211_update_owe_info *owe_info,
20199 				    gfp_t gfp)
20200 {
20201 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
20202 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20203 	struct sk_buff *msg;
20204 	void *hdr;
20205 
20206 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
20207 
20208 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
20209 	if (!msg)
20210 		return;
20211 
20212 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
20213 	if (!hdr)
20214 		goto nla_put_failure;
20215 
20216 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
20217 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
20218 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
20219 		goto nla_put_failure;
20220 
20221 	if (!owe_info->ie_len ||
20222 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
20223 		goto nla_put_failure;
20224 
20225 	if (owe_info->assoc_link_id != -1) {
20226 		if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
20227 			       owe_info->assoc_link_id))
20228 			goto nla_put_failure;
20229 
20230 		if (!is_zero_ether_addr(owe_info->peer_mld_addr) &&
20231 		    nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
20232 			    owe_info->peer_mld_addr))
20233 			goto nla_put_failure;
20234 	}
20235 
20236 	genlmsg_end(msg, hdr);
20237 
20238 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
20239 				NL80211_MCGRP_MLME, gfp);
20240 	return;
20241 
20242 nla_put_failure:
20243 	genlmsg_cancel(msg, hdr);
20244 	nlmsg_free(msg);
20245 }
20246 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
20247 
20248 /* initialisation/exit functions */
20249 
20250 int __init nl80211_init(void)
20251 {
20252 	int err;
20253 
20254 	err = genl_register_family(&nl80211_fam);
20255 	if (err)
20256 		return err;
20257 
20258 	err = netlink_register_notifier(&nl80211_netlink_notifier);
20259 	if (err)
20260 		goto err_out;
20261 
20262 	return 0;
20263  err_out:
20264 	genl_unregister_family(&nl80211_fam);
20265 	return err;
20266 }
20267 
20268 void nl80211_exit(void)
20269 {
20270 	netlink_unregister_notifier(&nl80211_netlink_notifier);
20271 	genl_unregister_family(&nl80211_fam);
20272 }
20273